zaach / jison

Bison in JavaScript.
http://jison.org
4.36k stars 450 forks source link

parser ARROW_ACTION does not detect incorrect usage #363

Open GerHobbelt opened 6 years ago

GerHobbelt commented 6 years ago

jison has an ARROW_ACTION feature (see ebnf_parser) where you can write a grammar production with shorthand action code like this:

a
    : A                         -> 'A[' + $A + ']';
    ;

which, similar to ES6 arrow JavaScript functions, is shorthand for this jison production/action:

a
    : A                         { $$ = 'A[' + $A + ']'; }
    ;

However, the ARROW_ACTION code is a simple code injection into the generated parser, such that this code is accepted by jison:

a
    : A                         -> parser.trace('a:A');                          calc('A[' + $A + ']');
    ;

which is treated as:

a
    : A                         { $$ = parser.trace('a:A');                          calc('A[' + $A + ']'); }
    ;

which is VERY PROBABLY not as intended, resulting in unexpected behaviour.

Suggested fix direction:

Do not accept compound statements like these, but only accept a single (possibly comma-separated) JavaScript expression, e.g.:

a
    : A                         -> parser.trace('a:A'), calc('A[' + $A + ']')
    ;

so that the $$ rule's result would be assigned the value returned by calc(...) in there.