lezer-parser/lezer

SyntaxError: No parse at x

milahu opened this issue · 3 comments

im trying to parse the nix language and im failing to parse 1 + 2

running npm run test throws

SyntaxError: No parse at 1

which is not helpful, and i found no docs for this error

how can i see the actual vs expected syntax tree?

reproduce

git clone https://github.com/milahu/lezer-nix --depth 1
cd lezer-nix
npm install
npm run build
npm run test

nix.grammar

@top Nix { expression* }

expression { Add | Int }

@precedence {
  Add @left
}

Add { expression !Add "+" expression }

@tokens {
  Int {
    std.digit+
  }
}

expression.txt

# int
1
==>
Nix(Int)

# add
1 + 2
==>
Nix(Add(Int,Int))

aah! whitespace ...

this works

# add
1+2
==>
Nix(Add(Int,Int))

You get this if you set strict to true. There is no syntax tree when a strict parse fails.

can we make @lezer/generator/dist/test.js more verbose?

            run: function (parser) {
                var strict = !/|\.\.\./.test(expected);
                if (parser.configure && (strict || config))
                    parser = parser.configure(__assign({ strict: strict }, config));
                //testTree(parser.parse(text), expected, mayIgnore);
                let actual;
                try {
                    // FIXME parser.configure is redundant
                    actual = parser.configure(__assign({ strict: strict }, config)).parse(text);
                }
                catch (e) {
                    // https://github.com/lezer-parser/lr/blob/main/src/parse.ts#L300
                    if (e.message.startsWith("No parse at ")) {
                        const pos = parseInt(e.message.slice("No parse at ".length));
                        e.message += `\n      ${text}\n      ${" ".repeat(pos)}^`;
                    }
                    throw e;
                }
                testTree(actual, expected, mayIgnore);
            }

sample output

  1) expression
       add:
     SyntaxError: No parse at 1
      1 + 2
       ^
      at Parse.advance
      at Parser.parse
      at run
      at Context.<anonymous>
      at processImmediate