mirror of
https://github.com/HeyPuter/puter.git
synced 2026-01-29 00:48:52 -06:00
Throw errors if parsing fails
Not consuming all input is now treated as an error, unless the `must_consume_all_input` flag is set to false when parsing.
This commit is contained in:
@@ -57,13 +57,22 @@ export class GrammarContext {
|
||||
}
|
||||
}
|
||||
|
||||
return (stream, entry_symbol) => {
|
||||
return (stream, entry_symbol, { must_consume_all_input = true } = {}) => {
|
||||
const entry_parser = symbol_registry[entry_symbol];
|
||||
if (!entry_parser) {
|
||||
throw new Error(`Entry symbol '${entry_symbol}' not found in grammar.`);
|
||||
}
|
||||
const result = entry_parser.parse(stream);
|
||||
// TODO: Ensure all the stream has been consumed
|
||||
|
||||
if (result.status !== VALUE) {
|
||||
throw new Error('Failed to parse input against grammar.');
|
||||
}
|
||||
|
||||
// Ensure the entire stream is consumed.
|
||||
if (must_consume_all_input && !stream.is_eof()) {
|
||||
throw new Error('Parsing did not consume all input.');
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user