How to deal with invalid syntax due to end of stream?
See original GitHub issueThis issue requires a bit of preamble, but please bear with me š
Take this simple grammar:
grammar.ne
main -> grouping:+
grouping -> lparen text rparen
text -> [a-zA-Z]:+
lparen -> "("
rparen -> ")"
If we compile it and create a parser that uses it:
parser.js
const nearley = require("nearley");
const grammar = require("./grammar");
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
module.exports = parser;
And feed it some data, (e.g. using the node repl), it works as expected:
const parser = require("./parser")
parser.feed("(hi)"); // [[[[["("],[["h","i"]],[")"]]]]]
If we omit the opening parenthesis, thereby providing it with invalid syntax, it throws an error as expected:
const parser = require("./parser")
parser.feed("hi)");
/*
Error: invalid syntax at line 1 col 1:
hi)
^
Unexpected "h"
*/
However, if we omit the closing parenthesis instead (parser.feed("(hi");
), no error is thrown and parser.results
remains an empty array.
I found a similar description in a GitHub issue (#253), which lead me to #261 in which the āEmpty parser.results
ā entry was checked. So I searched the docs for āemptyā and found this section https://nearley.js.org/docs/parser#catching-errors which states:
If there are no possible parsings given the current input, but in the future there might be results if you feed it more strings, then nearley will temporarily set the results array to the empty array, [].
This leads me to believe that what I described is happening because the parser is waiting for future input that might contain the closing parenthesis. The section also states:
If there are no possible parsings, and there is no way to ārecoverā by feeding more data, then nearley will throw an error whose offset property is the index of the offending token.
My question is this: How do I tell nearley that there will be no more input, so that it can wrap up its parsing?
If nearley knows there wonāt be any more data, it would be great to see some sort of error instead of an empty array. Perhaps something along the lines of Error: invalid syntax ... expected ")"
or Error: unexpected end of stream
.
P.S.
While looking through the source code, I found a finish
method, but it doesnāt seem to be documented and I donāt know enough about the project to figure out exactly what it does. Calling it just seems to return an empty array in this case.
https://github.com/Hardmath123/nearley/blob/ee5afdb3c7546cb03f33464219f22190791b424f/lib/nearley.js#L368-L382
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Comments:8
Assuming
feed()
didnāt throw an error, then yes.Glad I could help! š
Thatās interesting, I thought
finish()
did throw an error when there are no results. I agree thatās how it should work!@Hardmath123 What do you think about changing this (or deprecating
finish
, and adding a different method with this behaviour)?We should document this better, too! (PRs welcome, just ask.)
@mickdekkers Thank you for your detailed description, and for reading the docs so throughly! At the moment, parsing works as follows:
feed()
your source.catch
syntax errorsfinish()
, or access theresults
property, to get the array of parses.Hope that helps! š
Sent with GitHawk