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 a bug with named NEVER_MATCH expressions #454

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

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.

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.

2 participants