0

I'm writing a script in JS that utilizes Jison (https://zaa.ch/jison/) as the parser generator; I couldn't find anything in its docs that looks like the following:

// index.js - the script using the jison parser 

let myParser = require('/path/to/parser').parser; 

// some logic here to determine what the state should be 

myParser.setState('someState');
myParser.parser('someInput'); 
myParser.popState(); 

// etc.

I have some logic that cleans up responses from a server and determines some information about that output prior to sending it to my parser. Is it possible to set my parser's state outside of the .jison file itself?

Thanks!


Edit: adding more info below:

I noticed in the code generated by Jison, the parser function/object being exported has a lexer field. The lexer has methods pushState() and popState(). I tried calling that, but I get the following error:

Example:

let myParser = require('/path/to/parser').parser; 

myParser.lexer.pushState('someState');
myParser.parse('someInput');
myParser.lexer.popState();

Output:

node index.js
C:\path\to\my\script\TheParser.js:608
        this.conditionStack.push(condition);
                            ^

TypeError: Cannot read property 'push' of undefined
Vee
  • 729
  • 10
  • 27

1 Answers1

1

You can't use begin/pushState before the lexer has been initialised, which happens when its setInput method is called. I guess you could call that method yourself, although the parser will call it again regardless.

rici
  • 234,347
  • 28
  • 237
  • 341
  • That makes sense - thanks for the help. I've come up with a way to parse what I need without needing to set the state ahead of time; I'll remember this next time I attempt a solution like this! – Vee Feb 07 '20 at 17:58