8000 Fix a bug with named NEVER_MATCH expressions by markw65 · Pull Request #454 · peggyjs/peggy · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix a bug with named NEVER_MATCH expressions #454

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

Merged
merged 3 commits into from
Apr 17, 2025

Conversation

markw65
Copy link
@markw65 markw65 commented Dec 12, 2023

Found while working on #452.

The condition was backwards, so the error got misreported:

Before the fix:

% echo 'start "start" = []' | node bin/peggy.js -t "x"
Error running test
Error: Expected , or undefined but "x" found.
 --> command line:1:1
  |
1 | x
  | ^

After the fix:

% echo 'start "start" = []' | node bin/peggy.js -t "x"
Error running test
Error: Expected start but "x" found.
 --> command line:1:1
  |
1 | x
  | ^

There was already a test, but it was testing for the incorrect result...

Copy link
Member
@Mingun Mingun left a comment

Choose a reason for hiding this comment

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

Unfortunately, the new error also not so good, because it is misleading. The grammar

start 'start' = []

cannot have any expectations because it just cannot match any input. Probably, it would be better to emit compiler error here instead. This grammar does not expect "start" rule at this position, it expect literally nothing. Even EOF is not expected there.

@@ -618,7 +618,7 @@ function generateBytecode(ast, options) {
[op.SILENT_FAILS_ON],
generate(node.expression, context),
[op.SILENT_FAILS_OFF],
buildCondition(match, [op.IF_ERROR], [op.FAIL, nameIndex], [])
buildCondition(-match, [op.IF_ERROR], [op.FAIL, nameIndex], [])
);
Copy link
Member
@Mingun Mingun Dec 12, 2023

Choose a reason for hiding this comment

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

Good catch! The bug probably was introduced during rebase while adapt initial PR to the new code after another merged PR.

Actually, the whole buildSequence could be replaced by [op.FAIL, nameIndex] if node always fail and there are no actions or semantic predicates (because they can have side effects), but anyway, it is better to do in a separate pass.

Copy link
Author

Choose a reason for hiding this comment

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

if node always fail and there are no actions or semantic predicates (because they can have side effects)

There are already comments about adding side-effect analysis so we can do that kind of thing...

@markw65
Copy link
Author
markw65 commented Dec 12, 2023

This grammar does not expect "start" rule at this position, it expect literally nothing

But that was true whether or not it's a named rule.

Probably, it would be better to emit compiler error here instead

I guess thats ok if it's a start rule that always fails - but my examples only used start rules to exemplify the problem. You could use it as a catch all where a promising start goes off the rails:

echo 'start = [ab] fail / [cde]; fail "bzzt try again" = []' | node bin/peggy.js -t "ax"

Ok, its still not a great example, but it could be useful to have a named, NEVER_MATCH expression.

@hildjj
Copy link
Contributor
hildjj commented Apr 8, 2025

@Mingun did you end up being happy with where this patch is at the moment?

@Mingun
Copy link
Member
Mingun commented Apr 9, 2025

Yes, please merge.

@hildjj
Copy link
Contributor
hildjj commented Apr 9, 2025

@markw65 can you please rebase this onto main? I'll deal with the conflicts with #593.

@hildjj
Copy link
Contributor
hildjj commented Apr 17, 2025

I've got a clean rebase on top of main as it sits at the moment. I'm going to try pushing it to @markw65 's branch, which is likely going to fail. If so, I guess I'll start a new PR with my copy of the branch, and try to ensure the authorship is kept intact as much as possible.

@hildjj hildjj force-pushed the named-never-match branch from 390fc54 to aa39652 Compare April 17, 2025 20:02
@hildjj
Copy link
Contributor
hildjj commented Apr 17, 2025

OK, that looks like it worked. Merging.

@hildjj hildjj merged commit 334393f into peggyjs:main Apr 17, 2025
10 checks passed
@markw65
Copy link
Author
markw65 commented Apr 17, 2025

Sorry, I've been occupied elsewhere, but thanks for merging.

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.

3 participants
0