zaach / jison

Bison in JavaScript.
http://jison.org
4.36k stars 450 forks source link

lexer.pushState does not work as expected (or intended?) #317

Open rubenvereecken opened 8 years ago

rubenvereecken commented 8 years ago

The pushState or begin method of the lexer pushes a key onto the conditionStack. Whenever the current rule set is looked up (internally), this key is looked up in the conditions object. However, this object is never changed so this lookup will always fail, resulting in no rules and a crash.

I found myself having to overwrite the method in a very simple manner.

    lexer.pushState = function(key, condition) {
      if (!condition) {
        condition = {
          ctx: key, 
          rules: lexer._currentRules()
        };
      }
      lexer.conditions[key] = condition;
      lexer.conditionStack.push(key); // This line is the body of the original function
    };

As you can see, it's backwards compatible since the condition parameter would just be undefined. The if-statement is just to make it easier, the only big change is that the conditions object gets the rules added.

Shall I send a PR with this functionality? I found it really useful to have some context-dependent logic in the lexer so I can minimize my grammar.