lys-lang / node-ebnf

Create AST PEG Parsers from formal grammars in JavaScript
https://menduz.com/ebnf-highlighter/
MIT License
103 stars 9 forks source link

AST result set to null on EOF rather than reporting errors #50

Closed JeffDemanche closed 21 hours ago

JeffDemanche commented 21 hours ago

Hello, perhaps this is reflects a poor understanding on my part, in which case I'm seeking clarification.

export const bnf = `
ifl ::= filter EOF

filter ::= "highlights" (WS* highlights_expression)? (WS* highlights_sort)? | "arcs"

highlights_expression ::= (highlights_criterion | highlights_expression_conjunction | highlights_expression_disjunction)

highlights_expression_conjunction ::= "(" highlights_expression WS* "and" WS* highlights_expression ")"
highlights_expression_disjunction ::= "(" highlights_expression WS* "or" WS* highlights_expression ")"

highlights_criterion ::= highlights_criterion_dates | highlights_criterion_relation

highlights_criterion_dates ::= "from" WS* day WS* "to" WS* day
highlights_criterion_relation ::= "which" WS* predicate WS* relation_object

highlights_sort ::= "sorted by" WS* highlights_sort_type highlights_sort_as_of?
highlights_sort_as_of ::= WS* "as of" WS* day
highlights_sort_type ::= "importance" | "recent"

predicate ::= "are" | "relate to"
relation_object ::= STRING

day ::= STRING

STRING    ::= '"' ([1-9] | [a-z])+ '"'
WS        ::= [#x20#x09#x0A#x0D]+
`;

const parser = new Grammars.W3C.Parser(bnf, {});

const ast = parser.getAST("highlights bad input");

console.log(ast);

In this example, ast becomes null. I would expect it instead be defined with Unexpected token as an error on "bad input"?

If I remove EOF from the first rule in the grammar ast is non-null, but reports Unexpected end of input for any tokens past highlights.

Thank you

JeffDemanche commented 21 hours ago

Closing this, the issue was with using WS* over WS+. I change them and removed EOF.