Skip to content

Commit

Permalink
BNF parser: produce stricter action code for arrow action in any jiso…
Browse files Browse the repository at this point in the history
…n grammar so that we can more easily check coding mistakes in a production's arrow code, e.g. unwanted semicolons, multiple statements, etc., which can currently slip through the net and cause hard-to-diagnose havoc down the line.
  • Loading branch information
GerHobbelt committed Dec 25, 2017
1 parent c53fce7 commit 1c8c89d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/happyhappy.jison
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ e
;

phrase
: 'happy' 'happy' 'joy' 'joy' -> [$1, $2, $3, $4].join(' ');
: 'happy' 'happy' 'joy' 'joy' -> [$1, $2, $3, $4].join(' ')
;
28 changes: 14 additions & 14 deletions examples/parser-to-lexer-communication-test.jison
Original file line number Diff line number Diff line change
Expand Up @@ -44,53 +44,53 @@


S
: init x x e -> $e;
: init x x e -> $e
;

init
: %epsilon
;

x
: %epsilon -> '<X-epsilon>';
: %epsilon -> '<X-epsilon>'
;

e
: cmd e -> $cmd + ' | ' + $e;
| %epsilon -> '<E-epsilon>';
: cmd e -> $cmd + ' | ' + $e
| %epsilon -> '<E-epsilon>'
;

cmd
: a -> $a;
| f_a -> $f_a;
| b -> $b;
| f_b -> $f_b;
: a -> $a
| f_a -> $f_a
| b -> $b
| f_b -> $f_b
| error { yyerrok; yyclearin; $$ = 'ERROR'; }
;

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

f_a
: A lb e rb -> 'A' + $lb + $e + $rb;
: A lb e rb -> 'A' + $lb + $e + $rb
;

b
: B -> 'B[' + $B + ']';
: B -> 'B[' + $B + ']'
;

f_b
: B lb e rb -> 'B' + $lb + $e + $rb;
: B lb e rb -> 'B' + $lb + $e + $rb
;

lb
: '(' { yylexer.pushState('alt'); $$ = '('; }
| BEGIN -> '{';
| BEGIN -> '{'
;

rb
: ')' -> ')';
: ')' -> ')'
| END { yylexer.popState(); $$ = '}'; }
;

Expand Down
5 changes: 4 additions & 1 deletion packages/ebnf-parser/bnf.y
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,10 @@ action
: action_ne
{ $$ = $action_ne; }
| ARROW_ACTION
{ $$ = '$$ = ' + $ARROW_ACTION; }
// add braces around ARROW_ACTION so that the action chunk test/compiler
// will uncover any illegal action code following the arrow operator, e.g.
// multiple statements separated by semicolon.
{ $$ = '$$ = (' + $ARROW_ACTION + ');'; }
| %epsilon
{ $$ = ''; }
;
Expand Down

0 comments on commit 1c8c89d

Please sign in to comment.