kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.57k stars 232 forks source link

syntax error but it shouldn't #516

Closed Floffah closed 4 years ago

Floffah commented 4 years ago

So I had a grammar file which worked fine and would turn DECLARE hello INITIALLY "world" into [[{"type": "declare","name": "hello","value": {"type": "data","dtype": "string","string": "world"}}]]

However, I decided to move each part into a seperate file and now i get

Error: invalid syntax at line 1 col 1:

  DECLARE hello INITIALLY "world"
  ^
    at Lexer._token (D:\projects\unfinished\lang\reflang\node_modules\moo\moo.js:533:13)
    at Lexer.next (D:\projects\unfinished\lang\reflang\node_modules\moo\moo.js:480:19)
    at Parser.feed (D:\projects\unfinished\lang\reflang\node_modules\nearley\lib\nearley.js:273:30)
    at Object.<anonymous> (D:\projects\unfinished\lang\reflang\src\parse.js:9:8)

You can see the old grammar here

And this is the new (cut down for just declare) grammar lang.ne

@{%
    const moo = require('moo');
    const lexer = moo.compile({
        nl:{ match: /\n/, lineBreaks: true },
    });
%}

@lexer lexer

@include "./variables.ne"

process -> main:+ {%id%}

main -> declare {%id%}
    | %nl {% function (d) { return "newline" } %}

variables.ne

@{%
    const varlex = moo.compile({
        ws:     /[ \t]+/,
        number: /[0-9]+/,
        word: /[a-z]+/,
        nl:{ match: /\n/, lineBreaks: true },

        declare: "DECLARE",
        init: "INITIALLY",
        set: "SET",
    });
%}

@lexer varlex

@include "./value.ne"

declare -> "DECLARE" %ws %word %ws "INITIALLY" %ws value {% (d) => { return { type: 'declare', name: d[2].value, value: d[6] } } %}

set -> "SET" %ws %word %ws %to %ws value {% function(d) { return { type: 'set', name: d[2].value, value: d[6] } } %}

What am I doing wrong?

Floffah commented 4 years ago

I fixed it by merging all of the moo lexer variables into one file and referencing that in the other files