Tatsu Parser, unclear why it isn't moving to the next rule in the line?

64 Views Asked by At

I am writing a code parser/formatter for a language that doesn't have one, OSTW (Overwatch higher level language for workshop code). So that I can be lazy and have pretty code.

I am pretty new to this idea, so if tatsu is a poor choice for this usecase, please let me know, I am rather ignorant. I have been going back and forth between the grammar syntax and some of the tutorials and it isn't clicking for me yet.

My sample document:

doSomething(param1,param2,arg=stuff,arg2=stuff2);

My EBNF:

@@grammar::Ostw

@@eol_comments :: /\/\/.*?$/

start = statement $ ;

statement = func:alpha '(' ','%{param:alpha}* [',' ','%{kwarg}*] ')' eol ;
eol = ';' ;
kwarg = key:alpha '=' val:alpha  ;
alpha = (numbers|letters) ;
numbers = /\d+/ ;
letters = /\w+/ ;

The grammar compiles successfully, but when I attempt to parse my code, I get this output:

FailedToken: (1:30) expecting ')' :
doSomething(param1,param2,arg=stuff,arg2=stuff2);
                             ^
statement
start

My expectation would be that, since the = is not a valid character for the alpha rule, it would go to the next thing in the list, since it is an unknown number of entries of either types.

My intention is to have my parser expect similarly to Python, params then keyword arguments.

I think I missed a paragraph somewhere in something basic is what it feels like!

Thanks for any help! Mriswithe

1

There are 1 best solutions below

1
On

PEG grammars are greedy and committing. Parsing with the posted grammar will never reach the kwarg part because the param:alpha part will have succeeded first.

Perhaps you should use something like:

%{param:alpha !'='}*