kach / nearley

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

[question] why am I getting multiple parser results which are exactly the same? #504

Open jo32 opened 4 years ago

jo32 commented 4 years ago

My ne files:

@builtin "number.ne"
@builtin "whitespace.ne"
@builtin "string.ne"

# exp
Exp -> Binop {% id %}

Binop -> ExpOr {% id %} 

# Parenthesized -> "(" Exp ")"

ExpOr -> ExpOr __ "or" __ ExpAnd
    | ExpAnd {% id %}

ExpAnd -> ExpAnd __ "and" __ ExpAnd
    | ExpComparison {% id %}

ExpComparison ->
      Name _ "<"  _ Value
    | Name _ ">"  _ Value
    | Name _ "<=" _ Value
    | Name _ ">=" _ Value
    | Name _ "~=" _ Value
    | Name _ "==" _ Value

# variables
Name -> _name {% function(d) {return {'name': d[0]}; } %}

_name -> [a-zA-Z_] {% id %}
    | _name [\w_] {% function(d) {return d[0] + d[1]; } %}

# values
Value -> int {% id %}
    | unsigned_decimal {% id %}
    | decimal {% id %}

test code

describe('test parser', () => {
    test('case', () => {
        const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
        console.log(nearley.Grammar);
        parser.feed("ab > 10 or cd >= 10 and abc < 9.99");
        let o = parser.results[0];
        for (let i of parser.results) {
            console.log(o);
            expect(i).toStrictEqual(o);
            o = i;
        }
    });
});

all results are the same

TheGrandmother commented 4 years ago

I had a similar issue. The issue is that there can be multiple parsing of identical output when you are doing faffing with the ast during parsing.

The issue for me for example was that i had a bunch of {% stripSpaces %} things every were so two different parsing of white-spaces would still get an identical output since they parsed the spaces (which were removed) in different orders.

I suspect you have a similar issue but I can't see anything just by inspection.