Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always include whitespace in string literals #688

Merged
merged 2 commits into from
Jul 12, 2022

Conversation

sloretz
Copy link
Contributor

@sloretz sloretz commented Jul 12, 2022

Alternative to #677
Fixes #676

rosidl_parser randomly ignores whitespace at the start of string literals. This example matches the string literal as Token('__ANON_6', ' e') or Token('__ANON_6', 'e') when run multiple times; however, stripping the whitespace is incorrect.

from rosidl_parser.parser import get_ast_from_idl_string

idl_str ='''module MRE {
const string foo = "    e";
};'''
print(get_ast_from_idl_string(idl_str))

I think the cause is a combination of ignoring whitespace

and the way the string_literal rule is constructed

string_literal: "\"\"" | "\"" /(\\\"|[^"])+/ "\""

The string_literal rule has two parts. The first part matches an empty string "", and the second part matches a non-empty string. The second part says to match three things. First match a quote ". Next match at least one of: an escaped quote \", or a character that is not a quote. Finally match another quote ". I think the problem is the second rule being divided into three parts. It ambiguous if the parser should ignore the whitespace between the first quote and the string content, or if it should include the whitespace via the not-a-quote regex.

Lark has a built in rule for exactly this situation: https://github.com/lark-parser/lark/blob/953171821ed307f700fddf27f3fcb9483346bd46/lark/grammars/common.lark#L26-L29

We could %import common.ESCAPED_STRING for string_literal, but whide_string_literal probably has the same problem. If I defined wide_string_literal as "L" ESCAPED_STRING then it would also match L "white space between L and first quote", which is also incorrect. I've chosen to copy the rules from lark into our grammar instead.

There's a stackoverlow post explaining how lark's ESCAPED_STRING rule works: https://stackoverflow.com/a/61374198

@sloretz
Copy link
Contributor Author

sloretz commented Jul 12, 2022

@pablogs9 this seems to resolve the issue on my machine. Mind checking if it works for your use case?

@pablogs9
Copy link
Member

pablogs9 commented Jul 12, 2022

@sloretz LGTM, thanks a lot for this.

Are you going to backport to humble?

Signed-off-by: Shane Loretz <[email protected]>
@sloretz
Copy link
Contributor Author

sloretz commented Jul 12, 2022

I added tests for whitespace at the start and end of the string. With rolling as it is before this PR I only see the whitespace at the start of the string and wide string tests fail. I left the whitespace at the end of the string tests in since there's no harm in testing it. All tests pass with this PR.

@sloretz
Copy link
Contributor Author

sloretz commented Jul 12, 2022

Are you going to backport to humble?

I think this change is backportable to humble.

@sloretz
Copy link
Contributor Author

sloretz commented Jul 12, 2022

CI (build: --packages-above-and-dependencies rosidl_parser test: --packages-above rosidl_parser)

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Windows Build Status

@sloretz
Copy link
Contributor Author

sloretz commented Jul 12, 2022

Linux CI failure is known to be flaky: ros2/rosbag2#862. CI LGTM!

Copy link
Member

@wjwwood wjwwood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

code changes are arcane, but the test cases make sense to me.

+1 for backporting, if anyone was relying on this behavior it was a bug and therefore I think it's ok to break them.

@sloretz sloretz merged commit 5181196 into rolling Jul 12, 2022
@delete-merged-branch delete-merged-branch bot deleted the sloretz__always_include_whitespace_in_strings branch July 12, 2022 21:56
@sloretz
Copy link
Contributor Author

sloretz commented Jul 12, 2022

@Mergifyio backport humble galactic foxy

mergify bot pushed a commit that referenced this pull request Jul 12, 2022
* Always include whitespace in string literals

Signed-off-by: Shane Loretz <[email protected]>

* Add tests for #676

Signed-off-by: Shane Loretz <[email protected]>
(cherry picked from commit 5181196)
mergify bot pushed a commit that referenced this pull request Jul 12, 2022
* Always include whitespace in string literals

Signed-off-by: Shane Loretz <[email protected]>

* Add tests for #676

Signed-off-by: Shane Loretz <[email protected]>
(cherry picked from commit 5181196)
mergify bot pushed a commit that referenced this pull request Jul 12, 2022
* Always include whitespace in string literals

Signed-off-by: Shane Loretz <[email protected]>

* Add tests for #676

Signed-off-by: Shane Loretz <[email protected]>
(cherry picked from commit 5181196)
@mergify
Copy link
Contributor

mergify bot commented Jul 12, 2022

backport humble galactic foxy

✅ Backports have been created

sloretz added a commit that referenced this pull request Jul 13, 2022
* Always include whitespace in string literals

Signed-off-by: Shane Loretz <[email protected]>

* Add tests for #676

Signed-off-by: Shane Loretz <[email protected]>
(cherry picked from commit 5181196)

Co-authored-by: Shane Loretz <[email protected]>
sloretz added a commit that referenced this pull request Jul 13, 2022
* Always include whitespace in string literals

Signed-off-by: Shane Loretz <[email protected]>

* Add tests for #676

Signed-off-by: Shane Loretz <[email protected]>
(cherry picked from commit 5181196)

Co-authored-by: Shane Loretz <[email protected]>
sloretz added a commit that referenced this pull request Jul 13, 2022
* Always include whitespace in string literals

Signed-off-by: Shane Loretz <[email protected]>

* Add tests for #676

Signed-off-by: Shane Loretz <[email protected]>
(cherry picked from commit 5181196)

Co-authored-by: Shane Loretz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rosidl_generator_c does not produce replicable generated files
3 participants