question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

How to deal with invalid syntax due to end of stream?

See original GitHub issue

This 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:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
tjvrcommented, Oct 4, 2017

So an empty array can only be caused by an unexpected end of input (or no input), right?

Assuming feed() didnā€™t throw an error, then yes.

Glad I could help! šŸ˜ƒ

1reaction
tjvrcommented, Oct 3, 2017

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:

  • make a Parser instance from your Grammar instance
  • feed() your source.
  • Make sure to catch syntax errors
  • Call finish(), or access the results property, to get the array of parses.
  • Make sure thereā€™s exactly one: zero means we couldnā€™t parse it (unexpected end of input); more than one means the input was ambiguous.

Hope that helps! šŸ˜ƒ

Sent with GitHawk

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid Syntax in Python: Common Reasons for SyntaxError
This means that the Python interpreter got to the end of a line (EOL) before an open string was closed. To fix this,...
Read more >
streaming.py: SyntaxError: invalid syntax #1064 - GitHub
Traceback (most recent call last): File "main.py", line 7, in import tweepy FileĀ ...
Read more >
Python SyntaxError: invalid syntax end='' - Stack Overflow
It seems like you're using Python 2.x, not Python 3.x. Check your python version: >>> import sys >>> sys.version '2.7.5 (default, May 15...
Read more >
What is a Syntax Error in Python? (Examples + How to solve it)
In this lesson we will be covering one of the most basic errors you will probably get in Python; the Syntax Error. A...
Read more >
FAQ: JDBC Driver Data Stream Syntax Error - IBM
This FAQ provides a list of causes and solutions for various DRDA syntax error reason codes returned from the JDBC driver.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found