Is it possible to programmatically set the state of a Jison parser in your JS code?

134 Views Asked by At

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
1

There are 1 best solutions below

1
rici On

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.