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

Fix C++ syntax collision #150

Merged
merged 6 commits into from
May 15, 2024

Conversation

dacharyc
Copy link
Contributor

Closes #145

This adds regex to avoid matching Bluehawk tags surrounded by :: - it should only match tags with a single : surrounding them. This resolves C++ syntax issues, as C++ uses :: to reference class members.

Added a test - with the old patterns, it fails as expected, but with the new patterns, the integration test runs.

This also coincidentally fixes 1 of the failing tests reported in #149.

Upon initially adding the new regex, I got a few warnings from Chevrotain which Bluehawk reported as Errors. First was Unable to identify line terminator usage in pattern, detailed in the Chevrotain docs here: https://chevrotain.io/docs/guide/resolving_lexer_errors.html#IDENTIFY_TERMINATOR

Adding line_breaks: false, resolved this warning/error.

Then I got Failed parsing < /.../ > Using the regexp-to-ast library, detailed in the Chevrotain docs here: https://chevrotain.io/docs/guide/resolving_lexer_errors.html#REGEXP_PARSING

Per the docs recommendation, I added the start_chars_hint property to resolve these errors.

With these changes, the only failing tests are now tests that were failing before this PR.

@@ -116,9 +144,13 @@ describe("custom comment lexer", () => {

it("rejects comment patterns that conflict with other tokens", () => {
expect(() => {
makeLexer([makeLineCommentToken(TAG_PATTERN)]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note for reviewer: this test was failing independent of my changes. I've updated it to try/catch and match on the error message, and now it's passing.

@@ -84,12 +84,12 @@ describe("JSON attribute lists", () => {
const result = visitor.visit(cst, source);
expect(result.tagNodes[0].attributes).toBeUndefined();
expect(result.errors[0].message).toBe(
"Expected double-quoted property name in JSON"
"Expected double-quoted property name in JSON at position 19 (line 5 column 3)"
Copy link
Contributor Author

@dacharyc dacharyc May 14, 2024

Choose a reason for hiding this comment

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

Note for reviewer: this file had the other tests that were failing, as reported in #149.

The failures related to the error messages make sense, as it seems we have added reporting for the position of the error so these SHOULD be failing and adding the position should make them pass.

But the changes to the errors[0].location don't make sense to me. For example, the error message for the error at this index position reports the error location that matches the old location values - i.e. line 5, column 3. But for reasons I don't understand, the actual location is now different for each of the failing tests I've updated here. I believe it's related to this file: https://github.com/mongodb-university/Bluehawk/blob/main/src/bluehawk/parser/visitor/innerOffsetToOuterLocation.ts

But I don't fully understand the calculations we're doing there, so I'm not sure what to change. And/or if it's related to the const we're using to offset for weird newlines:

const re = /(\r\n|\r|\n)/g; // account for weird newlines

So I've updated the expectations to match what is actually being returned, but it seems like there may be a logic error somewhere that needs to be fixed. That doesn't seem super urgent to me, though, as the position reported in the error message - which is what the user will see - does seem to match the location of the error, so for end user reporting issues, I think this is ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Buuuut now I'm noticing that these tests were passing in CI, and now they're failing there. But they were failing locally while they passed in CI, and now I've "fixed" them so they're passing locally.

Something in the environment or a version mismatch, maybe?

@dacharyc dacharyc merged commit 38e6ca6 into mongodb-university:main May 15, 2024
2 checks passed
@dacharyc dacharyc deleted the add-negative-lookbehind branch May 15, 2024 13:58
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.

Fix C++/Bluehawk markup collisions
2 participants