Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse error when using arrow function in rules #23

Open
Mutefish0 opened this issue Oct 16, 2017 · 3 comments
Open

Parse error when using arrow function in rules #23

Mutefish0 opened this issue Oct 16, 2017 · 3 comments

Comments

@Mutefish0
Copy link

It will parse error:

let grammar = {
    lex: {
        rules: [
            ['\\s+', ''],
            ['\\d+', () => 'NUMBER'],
            ['\\+', () => '+'],
            ['$', () => 'EOF'],
        ]
    },
    operators: [
        ['left', '+']
    ],
    bnf: {
        'es': [
            ['e EOF', 'return $1']
        ],
        'e': [
            ['e + e', '$$ = $1 + $3'],
            ['NUMBER', '$$ = Number(yytext)']
        ]
    }
}

while this is ok:

let grammar = {
    lex: {
        rules: [
            ['\\s+', ''],
            ['\\d+', function () { return 'NUMBER'}],
            ['\\+', function () { return '+' }],
            ['$',  function () { return 'EOF' }],
        ]
    },
    operators: [
        ['left', '+']
    ],
    bnf: {
        'es': [
            ['e EOF', 'return $1']
        ],
        'e': [
            ['e + e', '$$ = $1 + $3'],
            ['NUMBER', '$$ = Number(yytext)']
        ]
    }
}
@Mutefish0 Mutefish0 changed the title Parse error when using arrow function in rulse Parse error when using arrow function in rules Oct 16, 2017
@GerHobbelt
Copy link
Contributor

The lexer generator (jison-lex) specifically looks for the return 'LABEL' pattern in the lexer rule action code blocks to replace the returned string with a token (number) when the lexer is combined with a grammar. This MAY be the cause of your trouble, though the parser run-time kernel has (IIRC) code to map token string to token number after the fact to cover any such lexer token return slip-ups before they enter the grammar parser proper.

While I say this, I wonder why that bit of code apparently doesn't kick in in your grammar/circumstances, so further diagnosis is required to answer this one without hand-waving like I do now.

@GerHobbelt
Copy link
Contributor

Upon further diagnosis this turns up:
the code generator specifically looks for the function () {...} pattern if the rule action is defined as a function instead of a string and therefor does not (yet) support Arrow Functions as in your example above.

Relevant code snippet in regexp-lexer, taken from the GerHobbelt/jison fork (TODO comment added today):

        newRules.push(m);
        if (typeof rule[1] === 'function') {
            // TODO: also cope with Arrow Functions (and inline those as well?) -- see also https://github.com/zaach/jison-lex/issues/23
            rule[1] = String(rule[1]).replace(/^\s*function\s*\(\)\s?\{/, '').replace(/\}\s*$/, '');
        }
        action = rule[1];
        action = action.replace(/return\s*'((?:\\'|[^']+)+)'/g, tokenNumberReplacement);
        action = action.replace(/return\s*"((?:\\"|[^"]+)+)"/g, tokenNumberReplacement);

GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Dec 13, 2017
GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Dec 13, 2017
…pt and JSON/JSON5 lexer specs to be processed by jison / jison-lex: this allows us to load and execute the fail & pass examples mentioned in zaach/jison-lex#23, for starters.
GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Dec 13, 2017
…unctions in the lexer spec, both simple functions, e.g. `() => 'TOKEN'` and "complex/multiline" arrow functions, e.g. `() => { statements; return 'TOKEN'; ... }`
GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Dec 13, 2017
GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Dec 13, 2017
…or tweak to test/showcase `e + e` action code expansion with named variables: `$e1 + $e2` instead of the more cryptic (old-skool yacc style action code) `$1 + $3`
GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Dec 13, 2017
…JISON API a la JISON-LEX API for testing: in the test code we `require()` such grammar specs. Also adjust the test code to otherwise facilitate the grammars that come with this issue, which are expected to produce a numeric value rather than a simple boolean TRUE.
@GerHobbelt
Copy link
Contributor

FYI: this issue is now fixed in jison-gho (https://www.npmjs.com/package/jison-gho) since NPM build 0.6.1-211 i.e. 'build 211'.

(Your examples have been included as /examples/issue-lex-23*.js and altered versions for jison-lex specifically: '/packages/jison-lex/tests/spec/issue-23*.js`)

GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Dec 22, 2017
…st for jison-lex issue zaach/jison-lex#23 waiting; turned on to (minimally) improve test coverage.

Also updated Makefile(s) to truly collect all coverage data when running `make test-nyc`: had seen the `nyc` commandline help, but the hint about `--clean` hadn't landed until I read https://gist.github.com/rundef/22545366591d73330a48b8948fa060a7#gistcomment-1856708
GerHobbelt added a commit to GerHobbelt/jison that referenced this issue Dec 25, 2017
…)` API to support ES6 arrow functions and extract the code content of any such function for inlining. This code will be used in jison and jison-lex to support arrow functions passed via the programmer API (a la zaach/jison-lex#23 )

Also extended the unit tests to cover various function type input scenarios.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants