oranoran / antlr4-autosuggest-js

JavaScript auto-suggest engine for ANTLR4 grammars
MIT License
42 stars 16 forks source link

Cannot read property 'ruleToStartState' of undefined in TokenSuggester #2

Closed debashish2014 closed 6 years ago

debashish2014 commented 6 years ago

Hi @oranoran,

There is an issue with below function of TokenSuggester. The atn property is not available in lexer file which is auto-generated by antlr4 ( v 4.7). It appears that in your project the lexer files in testGrammars folder currently have atn property defined which I believe is manually added, so the test cases are not failing.

If we use 'this._lexer._interp.atn.ruleToStartState' instead of 'this._lexer.atn.ruleToStartState' then it should work properly in all the cases.

TokenSuggester.prototype._findLexerStateByRuleNumber = function (ruleNumber) {
    return this._lexer.atn.ruleToStartState.slice(ruleNumber, ruleNumber + 1)[0];
};

Same applies to below function as well.

TokenSuggester.prototype._toLexerState = function (parserState) {
    var lexerState = this._lexer.atn.states.find((x) => { return (x.stateNumber === parserState.stateNumber); });
    if (lexerState == null) {
        debug('No lexer state matches parser state ' + parserState + ', not suggesting completions.');
    }
    return lexerState;
};
oranoran commented 6 years ago

Nice catch. Actually, if you use version 4.7.1 this shouldn't happen, as I contributed a patch to ANTLR4 that fixes this.

Can you check if this solves your problem?

debashish2014 commented 6 years ago

Thank you @oranoran. Will verify and let you know.

debashish2014 commented 6 years ago

Hi @oranoran, I have verified it. It works properly with antlr4 v 4.7.1. Thank you 👍