kach / nearley

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

Consider expanding parsing API #306

Open tjvr opened 6 years ago

tjvr commented 6 years ago

As discussed in #305, we might like to have an alternative to finish() (which is undocumented), which throws an error if there are no results.

How do I tell nearley that there will be no more input, so that it can wrap up its parsing?


Or, perhaps this could be part of lightweight parsing API? For users who don't want streaming, instead of writing:

const parser = new Parser(myGrammar)
try {
  parser.feed(input)
} catch (err) {
  // handle SyntaxError
}
assert(parser.results.length <= 1); // if you're sure your grammar's not ambiguous
if (parser.results.length === 0) {
  // handle "Unexpected end of input"
}
var result = parser.results[0]

It'd be neat if they could write:

try {
  var result = grammar.parse(input)
} catch (err) {
 // handle any SyntaxError, including EOF
}

...perhaps with a corresponding allParses() which returns all results, rather than the only result.


This is quite possibly a v3 thing, since there are some other API changes I think would be good (for example, I'd like to have an "ordered choice / no ambiguity" mode).

matthew-dean commented 6 years ago

As to this:

try {
  var result = grammar.parse(input)
} catch (err) {
 // handle any SyntaxError, including EOF
}

It's unfortunate that parsing has to be wrapped in a try/catch at all, since that will de-optimize JavaScript JIT compiling. Ideally, the parser would never throw errors, but would return a callback (/ Promise?) with an Error object. I mean, unless the concern is that errors can be caused that aren't caused by Nearley, this will reduce JIT VM efficiency AFAIK.

tjvr commented 6 years ago

since that will de-optimize JavaScript JIT compiling

That certainly used to be true for V8, but it's no longer the case for newer JITs I believe.

Regardless, we still haven't done any real planning toward a v3, so I wouldn't be concerned 🙂

Sent with GitHawk

bd82 commented 6 years ago

That certainly used to be true for V8, but it's no longer the case for newer JITs I believe.

@tjvr is quite correct, See: https://www.nearform.com/blog/node-js-is-getting-a-new-v8-with-turbofan/

matthew-dean commented 6 years ago

@bd82 Well bless my grits. I guess this is an issue no longer.

bd82 commented 6 years ago

@matthew-dean Yeah it allowed me to remove some optimizations to mitigate performance loss due to try/catch usage and simplify the code in one of my projects 😄