From 1c8c89dc62cfa4c9fa1e2888eb5459536853beb1 Mon Sep 17 00:00:00 2001 From: Ger Hobbelt Date: Mon, 25 Dec 2017 01:20:37 +0100 Subject: [PATCH] BNF parser: produce stricter action code for arrow action in any jison 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. --- examples/happyhappy.jison | 2 +- .../parser-to-lexer-communication-test.jison | 28 +++++++++---------- packages/ebnf-parser/bnf.y | 5 +++- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/examples/happyhappy.jison b/examples/happyhappy.jison index 5f28b0522..2c11db0df 100644 --- a/examples/happyhappy.jison +++ b/examples/happyhappy.jison @@ -44,5 +44,5 @@ e ; phrase - : 'happy' 'happy' 'joy' 'joy' -> [$1, $2, $3, $4].join(' '); + : 'happy' 'happy' 'joy' 'joy' -> [$1, $2, $3, $4].join(' ') ; diff --git a/examples/parser-to-lexer-communication-test.jison b/examples/parser-to-lexer-communication-test.jison index 7d6c7e953..44fd5ee75 100644 --- a/examples/parser-to-lexer-communication-test.jison +++ b/examples/parser-to-lexer-communication-test.jison @@ -44,7 +44,7 @@ S - : init x x e -> $e; + : init x x e -> $e ; init @@ -52,45 +52,45 @@ init ; x - : %epsilon -> ''; + : %epsilon -> '' ; e - : cmd e -> $cmd + ' | ' + $e; - | %epsilon -> ''; + : cmd e -> $cmd + ' | ' + $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(); $$ = '}'; } ; diff --git a/packages/ebnf-parser/bnf.y b/packages/ebnf-parser/bnf.y index eb9494410..2e2a29b9e 100644 --- a/packages/ebnf-parser/bnf.y +++ b/packages/ebnf-parser/bnf.y @@ -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 { $$ = ''; } ;