mna / pigeon

Command pigeon generates parsers in Go from a PEG grammar.
BSD 3-Clause "New" or "Revised" License
822 stars 66 forks source link

Ignore Braces in String and Rune Literals #115

Closed mavolin closed 10 months ago

mavolin commented 1 year ago

Similarly to #80, pigeon counts braces ({ and }) enclosed in string or rune literals as code, when it should just ignore them.

MWE

Code

{
    package foo
}

Test <- . {
    return "}", nil
}

Output

parse error(s):
 test.peg:6:14 (47): no match found, expected: "/", "/*", "//", "//{", ";", "\n", [ \t\r] or EOF

Fix

There are two possible fixes, both changing grammar/pigeon.go:325 and adding a new CodeStringLiteral rule:

- Code ← ( ( Comment / ![{}] SourceChar )+ / '{' Code '}' )*
+ Code ← ( ( Comment / CodeStringLiteral / ![{}] SourceChar )+ / '{' Code '}' )*

1. The 'Simple' Approach

CodeStringLiteral ← '"' (`\"` / [^"\r\n])* '"' /
                    '`' [^`]* '`' /
                    '\'' (`\'` / [^']+) '\''

2. Validated Strings

We could also validate the string/rune literals, since the necessary rules already exist:

CodeStringLiteral ← StringLiteral {
    return c.text, nil
}

I'm happy to make a PR. Note, however, that make seems to fail using first fix:

./bin/bootstrap-pigeon ./grammar/pigeon.peg > pigeon.go
go build -o bin/pigeon .
./bin/pigeon -nolint examples/calculator/calculator.peg > examples/calculator/calculator.go
parse error(s):
 examples/calculator/calculator.peg:1:1 (0): rule Grammar: undefined rule: __
make: *** [Makefile:83: examples/calculator/calculator.go] Error 3

I don't know if this is another bug, or if I'm doing something wrong.

breml commented 1 year ago

Thanks for the bug report. I have not yet had the time to dig into this in detail, so I can not yet confirm the issue. Never the less, feel free to create a PR with a test proofing the fix. Obviously the calculator example must not be broken with the fix applied.