GerHobbelt / jison

bison / YACC / LEX in JavaScript (LALR(1), SLR(1), etc. lexer/parser generator)
https://gerhobbelt.github.io/jison/
MIT License
118 stars 20 forks source link

action code in grammar doesn't expand `@label` ref inside template string with comments #30

Open GerHobbelt opened 6 years ago

GerHobbelt commented 6 years ago

Offending bnf.y grammar excerpt where @action after the @0 IS NOT expanded into proper JavaScript code like it should:

handle_action
    : handle prec action
        {
            $$ = [($handle.length ? $handle.join(' ') : '')];
            if ($action) {
                var rv = checkActionBlock($action, @action);
                if (rv) {
                    yyerror(rmCommonWS`
                        production rule action code block does not compile: ${rv}

                          Erroneous area:
                        ${yylexer.prettyPrintRange(@action, @handle)}
                    `);
                }
                $$.push($action);
            }
            if ($prec) {
                if ($handle.length === 0) {
                    yyerror(rmCommonWS`
                        You cannot specify a precedence override for an epsilon (a.k.a. empty) rule!

                          Erroneous area:
                        ${yylexer.prettyPrintRange(@handle, @0 /* @handle is very probably NULL! We need this one for some decent location info! */, @action /* ditto! */)}
                    `);
                }
                $$.push($prec);
            }
            if ($$.length === 1) {
                $$ = $$[0];
            }
        }

Removing the comment immediately following @0 fixes the problem.

This must be a problem with my code rewriter logic in there; it's still regex-based and apparently goes bonkers when you mix comments with ES6 string templates. Grmbl. I was planning to migrate to using recast AST rewriting already, but that will take some effort / time that is in short supply. :-(