Closed joagre closed 7 months ago
I don't think it is a problem related to lookahead...
The a[777].foo
correctly matches FunctionCall
rule, but it doesn't match Indexing
. That is partially because Indexing
tries to first match only index access, and it never gets to FunctionCall
. Switching the order will help: Indexing <- FunctionCall / Symbol _ "[" _ Expr _ "]"
. But this is not the only problem, which makes it much harder to debug, because even if you fix one problem, it still doesn't work :slightly_smiling_face:
I'd suggest to rewrite the grammar slightly to separate the index access from field access. Currently they're kind of intertwined (HasField
contains Indexing
). I can't tell you how exactly, I'd have to give it much more time than I have available, sorry.
Thanks for the input. I can see now that my grammar snippet is ambiguous/faulty and need to be rewritten. From a completelly naively point of view I would like to have an even more ambiguous grammar though:
Program <- _ TopLevelExpr (_ "," _ TopLevelExpr)* EOF
TopLevelExpr <- Expr / Binding
# Precedence ordered
Expr <- Add
Add <- Multiplicate (_ "+" _ Multiplicate)*
Multiplicate <- Indexing (_ "*" _ Indexing)*
Indexing <- Expr _ "[" _ Expr _ "]" / FunctionCall
FunctionCall <- FunctionName _ "(" _ ExprSequence? _ ")" / FieldAccess
FieldAccess <- Expr (_ "." _ Expr)* / Primary
FunctionName <- Symbol
Symbol <- [a-zA-Z_][a-zA-Z_0-9_]*
ExprSequence <- Expr (_ "," _ Expr)*
Primary <- Literal / Symbol
Literal <- NumberLiteral
NumberLiteral <- [0-9]+
Binding <- MatchPattern _ "=" _ Expr
MatchPattern <- Literal / FieldAccess / Symbol
_ <- WS*
WS <- [ \t\r\n]
EOF <- _ !.
:-)
It would require a parser with indefinite backtracking. Maybe not even that would suffice. Life is hard.
Thanks
I have been trying to write a small PEG to parse the following in a file named
simple.sa
:It works except for the last a[777].foo.
At times I think there must be a bug in packcc's lookahead pattern support and then I realize it must be me misunderstanding something central. I wish there was more documentation on how to successfully use positive and negative lookahead. I'm stumped. Can anyone have mercy on me and point out what I need to do to the PEG below to work as expected?
Cheers /J