kach / nearley

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

Grammar parsing fine in playground but not in node.js/browser #518

Closed brucou closed 4 years ago

brucou commented 4 years ago

@kach Lovely people, not sure if that is the right place to post (let me know if there is a stack overflow for it), but here we go:

I have the following grammar working fine in the playground:

MAIN -> OneTransitionLabel                             {% d => d[0] %}
| MultipleTransitionLabel                              {% d => d[0] %}
MultipleTransitionLabel -> ("|" OneTransitionLabel):+  {% d => d[0].map(x => x[1]) %}
OneTransitionLabel ->
  EventClause "[" GuardClause "]" _ "/" ActionsClause  {% d => ({event: d[0][0].join(''), guard: d[2][0].join(''), actions: d[6][0].join('')}) %}
| EventClause "[" GuardClause "]" _                    {% d => ({event: d[0][0].join(''), guard: d[2][0].join(''), actions: ""}) %}
| EventClause "/" ActionsClause                        {% d => ({event: d[0][0].join(''), guard: "", actions: d[2][0].join('')}) %}
| EventClause                                          {% d => ({event: d[0][0].join(''), guard: "", actions: ""}) %}
| "[" GuardClause "]" _ "/" ActionsClause              {% d => ({event: "", guard: d[1][0].join(''), actions: d[5][0].join('')}) %}
| "/" ActionsClause                                    {% d => ({event: "", guard: "", actions: d[1][0].join('')}) %}
| "[" GuardClause "]"                                  {% d => ({event: "", guard: d[1][0].join(''), actions: ""}) %}

EventClause -> [^\/\[\]\|]:+
GuardClause -> [^\[\]]:*
ActionsClause -> [^\/\|\[\]]:*
StringLiteral -> [\w]:+
  _ -> [\s]:*

For instance the string | click inc / increment counter, render | click dec [a] / decrement counter, render is correctly parsed in the playground into:

[Object, Object]
  0: Object
    event: " click inc "
    guard: ""
    actions: " increment counter, render "
  1: Object
    event: " click dec "
    guard: "a"
    actions: " decrement counter, render"

However, when running this grammar in node or the browser, the parser fails. The error is reproduced in this sandbox: https://codesandbox.io/s/nearley-parsing-error-7fql6. The error message is as follows:

index.js:27 Error: Syntax error at line 1 col 57:

  | click inc / increment counter, render | click dec [a] / decrement counter, render
                                                          ^
Unexpected "/". Instead, I was expecting to see one of the following:

A character matching /[^\/]/ based on:
    ActionsClause$ebnf$1 → ActionsClause$ebnf$1 ● /[^\/]/
    ActionsClause →  ● ActionsClause$ebnf$1
    MAIN → EventClause "/" ● ActionsClause

    at Parser.feed (VM217 nearley.js:380)

In case this matters, I run node 10.15 on windows 10.

What could be possibly wrong? I also fail to decipher the error messages.. Can anybody please help?

brucou commented 4 years ago

@kach alright I found the mistake, I changed the grammar.ne and forgot to generate the grammar.js with nearleyc... so it was applying the previous compiled grammar to the new grammar.ne...

I don't know if there is an easy way to avoid that in the future. The only thing I can think about is a watcher which automatically compiles the grammar when it has changed. Closing this then.