refactored ATN to be an es6 class

This commit is contained in:
Camilo Roca 2020-03-06 18:10:34 +01:00
parent 225249fdae
commit e7b935e7e2
1 changed files with 93 additions and 93 deletions

View File

@ -6,7 +6,8 @@
var LL1Analyzer = require('./../LL1Analyzer').LL1Analyzer;
var IntervalSet = require('./../IntervalSet').IntervalSet;
function ATN(grammarType , maxTokenType) {
class ATN {
constructor(grammarType , maxTokenType) {
// Used for runtime deserialization of ATNs from strings///
// The type of the ATN.
@ -33,85 +34,64 @@ function ATN(grammarType , maxTokenType) {
// be referenced by action transitions in the ATN.
this.lexerActions = null;
this.modeToStartState = [];
return this;
}
// Compute the set of valid tokens that can occur starting in state {@code s}.
// If {@code ctx} is null, the set of tokens will not include what can follow
// the rule surrounding {@code s}. In other words, the set will be
// restricted to tokens reachable staying within {@code s}'s rule.
ATN.prototype.nextTokensInContext = function(s, ctx) {
nextTokensInContext(s, ctx) {
var anal = new LL1Analyzer(this);
return anal.LOOK(s, null, ctx);
};
}
// Compute the set of valid tokens that can occur starting in {@code s} and
// staying in same rule. {@link Token//EPSILON} is in set if we reach end of
// rule.
ATN.prototype.nextTokensNoContext = function(s) {
nextTokensNoContext(s) {
if (s.nextTokenWithinRule !== null ) {
return s.nextTokenWithinRule;
}
s.nextTokenWithinRule = this.nextTokensInContext(s, null);
s.nextTokenWithinRule.readOnly = true;
return s.nextTokenWithinRule;
};
}
ATN.prototype.nextTokens = function(s, ctx) {
nextTokens(s, ctx) {
if ( ctx===undefined ) {
return this.nextTokensNoContext(s);
} else {
return this.nextTokensInContext(s, ctx);
}
};
}
ATN.prototype.addState = function( state) {
addState(state) {
if ( state !== null ) {
state.atn = this;
state.stateNumber = this.states.length;
}
this.states.push(state);
};
}
ATN.prototype.removeState = function( state) {
removeState(state) {
this.states[state.stateNumber] = null; // just free mem, don't shift states in list
};
}
ATN.prototype.defineDecisionState = function( s) {
defineDecisionState(s) {
this.decisionToState.push(s);
s.decision = this.decisionToState.length-1;
return s.decision;
};
}
ATN.prototype.getDecisionState = function( decision) {
getDecisionState(decision) {
if (this.decisionToState.length===0) {
return null;
} else {
return this.decisionToState[decision];
}
};
}
// Computes the set of input symbols which could follow ATN state number
// {@code stateNumber} in the specified full {@code context}. This method
// considers the complete parser context, but does not evaluate semantic
// predicates (i.e. all predicates encountered during the calculation are
// assumed true). If a path in the ATN exists from the starting state to the
// {@link RuleStopState} of the outermost context without matching any
// symbols, {@link Token//EOF} is added to the returned set.
//
// <p>If {@code context} is {@code null}, it is treated as
// {@link ParserRuleContext//EMPTY}.</p>
//
// @param stateNumber the ATN state number
// @param context the full parse context
// @return The set of potentially valid input symbols which could follow the
// specified state in the specified context.
// @throws IllegalArgumentException if the ATN does not contain a state with
// number {@code stateNumber}
var Token = require('./../Token').Token;
ATN.prototype.getExpectedTokens = function( stateNumber, ctx ) {
getExpectedTokens(stateNumber, ctx ) {
if ( stateNumber < 0 || stateNumber >= this.states.length ) {
throw("Invalid state number.");
}
@ -135,7 +115,27 @@ ATN.prototype.getExpectedTokens = function( stateNumber, ctx ) {
expected.addOne(Token.EOF);
}
return expected;
};
}
}
// Computes the set of input symbols which could follow ATN state number
// {@code stateNumber} in the specified full {@code context}. This method
// considers the complete parser context, but does not evaluate semantic
// predicates (i.e. all predicates encountered during the calculation are
// assumed true). If a path in the ATN exists from the starting state to the
// {@link RuleStopState} of the outermost context without matching any
// symbols, {@link Token//EOF} is added to the returned set.
//
// <p>If {@code context} is {@code null}, it is treated as
// {@link ParserRuleContext//EMPTY}.</p>
//
// @param stateNumber the ATN state number
// @param context the full parse context
// @return The set of potentially valid input symbols which could follow the
// specified state in the specified context.
// @throws IllegalArgumentException if the ATN does not contain a state with
// number {@code stateNumber}
var Token = require('./../Token').Token;
ATN.INVALID_ALT_NUMBER = 0;