refactored ATNDeserializer.js to use es6 classes

fix: dont wrap the class in an object for export
This commit is contained in:
Camilo Roca 2020-03-06 19:33:38 +01:00
parent d61e4d5049
commit 4f8527e9ee
4 changed files with 539 additions and 538 deletions

View File

@ -7,7 +7,7 @@ var Token = require('./Token').Token;
var ParseTreeListener = require('./tree/Tree').ParseTreeListener; var ParseTreeListener = require('./tree/Tree').ParseTreeListener;
var Recognizer = require('./Recognizer').Recognizer; var Recognizer = require('./Recognizer').Recognizer;
var DefaultErrorStrategy = require('./error/ErrorStrategy').DefaultErrorStrategy; var DefaultErrorStrategy = require('./error/ErrorStrategy').DefaultErrorStrategy;
var ATNDeserializer = require('./atn/ATNDeserializer').ATNDeserializer; var ATNDeserializer = require('./atn/ATNDeserializer');
var ATNDeserializationOptions = require('./atn/ATNDeserializationOptions'); var ATNDeserializationOptions = require('./atn/ATNDeserializationOptions');
var TerminalNode = require('./tree/Tree').TerminalNode; var TerminalNode = require('./tree/Tree').TerminalNode;
var ErrorNode = require('./tree/Tree').ErrorNode; var ErrorNode = require('./tree/Tree').ErrorNode;

View File

@ -249,5 +249,7 @@ class OrderedATNConfigSet extends ATNConfigSet {
} }
} }
module.exports.ATNConfigSet = ATNConfigSet; module.exports = {
module.exports.OrderedATNConfigSet = OrderedATNConfigSet; ATNConfigSet,
OrderedATNConfigSet
}

View File

@ -73,7 +73,8 @@ function initArray( length, value) {
return tmp.map(function(i) {return value;}); return tmp.map(function(i) {return value;});
} }
function ATNDeserializer (options) { class ATNDeserializer {
constructor(options) {
if ( options=== undefined || options === null ) { if ( options=== undefined || options === null ) {
options = ATNDeserializationOptions.defaultOptions; options = ATNDeserializationOptions.defaultOptions;
@ -81,32 +82,31 @@ function ATNDeserializer (options) {
this.deserializationOptions = options; this.deserializationOptions = options;
this.stateFactories = null; this.stateFactories = null;
this.actionFactories = null; this.actionFactories = null;
return this;
} }
// Determines if a particular serialized representation of an ATN supports /**
// a particular feature, identified by the {@link UUID} used for serializing * Determines if a particular serialized representation of an ATN supports
// the ATN at the time the feature was first introduced. * a particular feature, identified by the {@link UUID} used for serializing
// * the ATN at the time the feature was first introduced.
// @param feature The {@link UUID} marking the first time the feature was *
// supported in the serialized ATN. * @param feature The {@link UUID} marking the first time the feature was
// @param actualUuid The {@link UUID} of the actual serialized ATN which is * supported in the serialized ATN.
// currently being deserialized. * @param actualUuid The {@link UUID} of the actual serialized ATN which is
// @return {@code true} if the {@code actualUuid} value represents a * currently being deserialized.
// serialized ATN at or after the feature identified by {@code feature} was * @return {@code true} if the {@code actualUuid} value represents a
// introduced; otherwise, {@code false}. * serialized ATN at or after the feature identified by {@code feature} was
* introduced; otherwise, {@code false}.
ATNDeserializer.prototype.isFeatureSupported = function(feature, actualUuid) { */
isFeatureSupported(feature, actualUuid) {
var idx1 = SUPPORTED_UUIDS.indexOf(feature); var idx1 = SUPPORTED_UUIDS.indexOf(feature);
if (idx1<0) { if (idx1<0) {
return false; return false;
} }
var idx2 = SUPPORTED_UUIDS.indexOf(actualUuid); var idx2 = SUPPORTED_UUIDS.indexOf(actualUuid);
return idx2 >= idx1; return idx2 >= idx1;
}; }
ATNDeserializer.prototype.deserialize = function(data) { deserialize(data) {
this.reset(data); this.reset(data);
this.checkVersion(); this.checkVersion();
this.checkUUID(); this.checkUUID();
@ -133,9 +133,9 @@ ATNDeserializer.prototype.deserialize = function(data) {
this.verifyATN(atn); this.verifyATN(atn);
} }
return atn; return atn;
}; }
ATNDeserializer.prototype.reset = function(data) { reset(data) {
var adjust = function(c) { var adjust = function(c) {
var v = c.charCodeAt(0); var v = c.charCodeAt(0);
return v>1 ? v-2 : v + 65534; return v>1 ? v-2 : v + 65534;
@ -145,31 +145,31 @@ ATNDeserializer.prototype.reset = function(data) {
temp[0] = data.charCodeAt(0); temp[0] = data.charCodeAt(0);
this.data = temp; this.data = temp;
this.pos = 0; this.pos = 0;
}; }
ATNDeserializer.prototype.checkVersion = function() { checkVersion() {
var version = this.readInt(); var version = this.readInt();
if ( version !== SERIALIZED_VERSION ) { if ( version !== SERIALIZED_VERSION ) {
throw ("Could not deserialize ATN with version " + version + " (expected " + SERIALIZED_VERSION + ")."); throw ("Could not deserialize ATN with version " + version + " (expected " + SERIALIZED_VERSION + ").");
} }
}; }
ATNDeserializer.prototype.checkUUID = function() { checkUUID() {
var uuid = this.readUUID(); var uuid = this.readUUID();
if (SUPPORTED_UUIDS.indexOf(uuid)<0) { if (SUPPORTED_UUIDS.indexOf(uuid)<0) {
throw ("Could not deserialize ATN with UUID: " + uuid + throw ("Could not deserialize ATN with UUID: " + uuid +
" (expected " + SERIALIZED_UUID + " or a legacy UUID).", uuid, SERIALIZED_UUID); " (expected " + SERIALIZED_UUID + " or a legacy UUID).", uuid, SERIALIZED_UUID);
} }
this.uuid = uuid; this.uuid = uuid;
}; }
ATNDeserializer.prototype.readATN = function() { readATN() {
var grammarType = this.readInt(); var grammarType = this.readInt();
var maxTokenType = this.readInt(); var maxTokenType = this.readInt();
return new ATN(grammarType, maxTokenType); return new ATN(grammarType, maxTokenType);
}; }
ATNDeserializer.prototype.readStates = function(atn) { readStates(atn) {
var j, pair, stateNumber; var j, pair, stateNumber;
var loopBackStateNumbers = []; var loopBackStateNumbers = [];
var endStateNumbers = []; var endStateNumbers = [];
@ -218,9 +218,9 @@ ATNDeserializer.prototype.readStates = function(atn) {
stateNumber = this.readInt(); stateNumber = this.readInt();
atn.states[stateNumber].isPrecedenceRule = true; atn.states[stateNumber].isPrecedenceRule = true;
} }
}; }
ATNDeserializer.prototype.readRules = function(atn) { readRules(atn) {
var i; var i;
var nrules = this.readInt(); var nrules = this.readInt();
if (atn.grammarType === ATNType.LEXER ) { if (atn.grammarType === ATNType.LEXER ) {
@ -248,17 +248,17 @@ ATNDeserializer.prototype.readRules = function(atn) {
atn.ruleToStopState[state.ruleIndex] = state; atn.ruleToStopState[state.ruleIndex] = state;
atn.ruleToStartState[state.ruleIndex].stopState = state; atn.ruleToStartState[state.ruleIndex].stopState = state;
} }
}; }
ATNDeserializer.prototype.readModes = function(atn) { readModes(atn) {
var nmodes = this.readInt(); var nmodes = this.readInt();
for (var i=0; i<nmodes; i++) { for (var i=0; i<nmodes; i++) {
var s = this.readInt(); var s = this.readInt();
atn.modeToStartState.push(atn.states[s]); atn.modeToStartState.push(atn.states[s]);
} }
}; }
ATNDeserializer.prototype.readSets = function(atn, sets, readUnicode) { readSets(atn, sets, readUnicode) {
var m = this.readInt(); var m = this.readInt();
for (var i=0; i<m; i++) { for (var i=0; i<m; i++) {
var iset = new IntervalSet(); var iset = new IntervalSet();
@ -274,9 +274,9 @@ ATNDeserializer.prototype.readSets = function(atn, sets, readUnicode) {
iset.addRange(i1, i2); iset.addRange(i1, i2);
} }
} }
}; }
ATNDeserializer.prototype.readEdges = function(atn, sets) { readEdges(atn, sets) {
var i, j, state, trans, target; var i, j, state, trans, target;
var nedges = this.readInt(); var nedges = this.readInt();
for (i=0; i<nedges; i++) { for (i=0; i<nedges; i++) {
@ -340,9 +340,9 @@ ATNDeserializer.prototype.readEdges = function(atn, sets) {
} }
} }
} }
}; }
ATNDeserializer.prototype.readDecisions = function(atn) { readDecisions(atn) {
var ndecisions = this.readInt(); var ndecisions = this.readInt();
for (var i=0; i<ndecisions; i++) { for (var i=0; i<ndecisions; i++) {
var s = this.readInt(); var s = this.readInt();
@ -350,9 +350,9 @@ ATNDeserializer.prototype.readDecisions = function(atn) {
atn.decisionToState.push(decState); atn.decisionToState.push(decState);
decState.decision = i; decState.decision = i;
} }
}; }
ATNDeserializer.prototype.readLexerActions = function(atn) { readLexerActions(atn) {
if (atn.grammarType === ATNType.LEXER) { if (atn.grammarType === ATNType.LEXER) {
var count = this.readInt(); var count = this.readInt();
atn.lexerActions = initArray(count, null); atn.lexerActions = initArray(count, null);
@ -370,9 +370,9 @@ ATNDeserializer.prototype.readLexerActions = function(atn) {
atn.lexerActions[i] = lexerAction; atn.lexerActions[i] = lexerAction;
} }
} }
}; }
ATNDeserializer.prototype.generateRuleBypassTransitions = function(atn) { generateRuleBypassTransitions(atn) {
var i; var i;
var count = atn.ruleToStartState.length; var count = atn.ruleToStartState.length;
for(i=0; i<count; i++) { for(i=0; i<count; i++) {
@ -381,9 +381,9 @@ ATNDeserializer.prototype.generateRuleBypassTransitions = function(atn) {
for(i=0; i<count; i++) { for(i=0; i<count; i++) {
this.generateRuleBypassTransition(atn, i); this.generateRuleBypassTransition(atn, i);
} }
}; }
ATNDeserializer.prototype.generateRuleBypassTransition = function(atn, idx) { generateRuleBypassTransition(atn, idx) {
var i, state; var i, state;
var bypassStart = new BasicBlockStartState(); var bypassStart = new BasicBlockStartState();
bypassStart.ruleIndex = idx; bypassStart.ruleIndex = idx;
@ -450,9 +450,9 @@ ATNDeserializer.prototype.generateRuleBypassTransition = function(atn, idx) {
atn.addState(matchState); atn.addState(matchState);
matchState.addTransition(new AtomTransition(bypassStop, atn.ruleToTokenType[idx])); matchState.addTransition(new AtomTransition(bypassStop, atn.ruleToTokenType[idx]));
bypassStart.addTransition(new EpsilonTransition(matchState)); bypassStart.addTransition(new EpsilonTransition(matchState));
}; }
ATNDeserializer.prototype.stateIsEndStateFor = function(state, idx) { stateIsEndStateFor(state, idx) {
if ( state.ruleIndex !== idx) { if ( state.ruleIndex !== idx) {
return null; return null;
} }
@ -469,16 +469,15 @@ ATNDeserializer.prototype.stateIsEndStateFor = function(state, idx) {
} else { } else {
return null; return null;
} }
}; }
// /**
// Analyze the {@link StarLoopEntryState} states in the specified ATN to set * Analyze the {@link StarLoopEntryState} states in the specified ATN to set
// the {@link StarLoopEntryState//isPrecedenceDecision} field to the * the {@link StarLoopEntryState//isPrecedenceDecision} field to the
// correct value. * correct value.
// * @param atn The ATN.
// @param atn The ATN. */
// markPrecedenceDecisions(atn) {
ATNDeserializer.prototype.markPrecedenceDecisions = function(atn) {
for(var i=0; i<atn.states.length; i++) { for(var i=0; i<atn.states.length; i++) {
var state = atn.states[i]; var state = atn.states[i];
if (!( state instanceof StarLoopEntryState)) { if (!( state instanceof StarLoopEntryState)) {
@ -487,7 +486,6 @@ ATNDeserializer.prototype.markPrecedenceDecisions = function(atn) {
// We analyze the ATN to determine if this ATN decision state is the // We analyze the ATN to determine if this ATN decision state is the
// decision for the closure block that determines whether a // decision for the closure block that determines whether a
// precedence rule should continue or complete. // precedence rule should continue or complete.
//
if ( atn.ruleToStartState[state.ruleIndex].isPrecedenceRule) { if ( atn.ruleToStartState[state.ruleIndex].isPrecedenceRule) {
var maybeLoopEndState = state.transitions[state.transitions.length - 1].target; var maybeLoopEndState = state.transitions[state.transitions.length - 1].target;
if (maybeLoopEndState instanceof LoopEndState) { if (maybeLoopEndState instanceof LoopEndState) {
@ -498,9 +496,9 @@ ATNDeserializer.prototype.markPrecedenceDecisions = function(atn) {
} }
} }
} }
}; }
ATNDeserializer.prototype.verifyATN = function(atn) { verifyATN(atn) {
if (!this.deserializationOptions.verifyATN) { if (!this.deserializationOptions.verifyATN) {
return; return;
} }
@ -542,44 +540,34 @@ ATNDeserializer.prototype.verifyATN = function(atn) {
this.checkCondition(state.transitions.length <= 1 || (state instanceof RuleStopState)); this.checkCondition(state.transitions.length <= 1 || (state instanceof RuleStopState));
} }
} }
}; }
ATNDeserializer.prototype.checkCondition = function(condition, message) { checkCondition(condition, message) {
if (!condition) { if (!condition) {
if (message === undefined || message===null) { if (message === undefined || message===null) {
message = "IllegalState"; message = "IllegalState";
} }
throw (message); throw (message);
} }
}; }
ATNDeserializer.prototype.readInt = function() { readInt() {
return this.data[this.pos++]; return this.data[this.pos++];
}; }
ATNDeserializer.prototype.readInt32 = function() { readInt32() {
var low = this.readInt(); var low = this.readInt();
var high = this.readInt(); var high = this.readInt();
return low | (high << 16); return low | (high << 16);
}; }
ATNDeserializer.prototype.readLong = function() { readLong() {
var low = this.readInt32(); var low = this.readInt32();
var high = this.readInt32(); var high = this.readInt32();
return (low & 0x00000000FFFFFFFF) | (high << 32); return (low & 0x00000000FFFFFFFF) | (high << 32);
};
function createByteToHex() {
var bth = [];
for (var i = 0; i < 256; i++) {
bth[i] = (i + 0x100).toString(16).substr(1).toUpperCase();
}
return bth;
} }
var byteToHex = createByteToHex(); readUUID() {
ATNDeserializer.prototype.readUUID = function() {
var bb = []; var bb = [];
for(var i=7;i>=0;i--) { for(var i=7;i>=0;i--) {
var int = this.readInt(); var int = this.readInt();
@ -595,9 +583,9 @@ ATNDeserializer.prototype.readUUID = function() {
byteToHex[bb[10]] + byteToHex[bb[11]] + byteToHex[bb[10]] + byteToHex[bb[11]] +
byteToHex[bb[12]] + byteToHex[bb[13]] + byteToHex[bb[12]] + byteToHex[bb[13]] +
byteToHex[bb[14]] + byteToHex[bb[15]]; byteToHex[bb[14]] + byteToHex[bb[15]];
}; }
ATNDeserializer.prototype.edgeFactory = function(atn, type, src, trg, arg1, arg2, arg3, sets) { edgeFactory(atn, type, src, trg, arg1, arg2, arg3, sets) {
var target = atn.states[trg]; var target = atn.states[trg];
switch(type) { switch(type) {
case Transition.EPSILON: case Transition.EPSILON:
@ -623,9 +611,9 @@ ATNDeserializer.prototype.edgeFactory = function(atn, type, src, trg, arg1, arg2
default: default:
throw "The specified transition type: " + type + " is not valid."; throw "The specified transition type: " + type + " is not valid.";
} }
}; }
ATNDeserializer.prototype.stateFactory = function(type, ruleIndex) { stateFactory(type, ruleIndex) {
if (this.stateFactories === null) { if (this.stateFactories === null) {
var sf = []; var sf = [];
sf[ATNState.INVALID_TYPE] = null; sf[ATNState.INVALID_TYPE] = null;
@ -652,9 +640,9 @@ ATNDeserializer.prototype.stateFactory = function(type, ruleIndex) {
return s; return s;
} }
} }
}; }
ATNDeserializer.prototype.lexerActionFactory = function(type, data1, data2) { lexerActionFactory(type, data1, data2) {
if (this.actionFactories === null) { if (this.actionFactories === null) {
var af = []; var af = [];
af[LexerActionType.CHANNEL] = function(data1, data2) { return new LexerChannelAction(data1); }; af[LexerActionType.CHANNEL] = function(data1, data2) { return new LexerChannelAction(data1); };
@ -672,7 +660,18 @@ ATNDeserializer.prototype.lexerActionFactory = function(type, data1, data2) {
} else { } else {
return this.actionFactories[type](data1, data2); return this.actionFactories[type](data1, data2);
} }
}; }
}
function createByteToHex() {
var bth = [];
for (var i = 0; i < 256; i++) {
bth[i] = (i + 0x100).toString(16).substr(1).toUpperCase();
}
return bth;
}
var byteToHex = createByteToHex();
exports.ATNDeserializer = ATNDeserializer; module.exports = ATNDeserializer;

View File

@ -4,7 +4,7 @@
*/ */
exports.ATN = require('./ATN'); exports.ATN = require('./ATN');
exports.ATNDeserializer = require('./ATNDeserializer').ATNDeserializer; exports.ATNDeserializer = require('./ATNDeserializer');
exports.LexerATNSimulator = require('./LexerATNSimulator').LexerATNSimulator; exports.LexerATNSimulator = require('./LexerATNSimulator').LexerATNSimulator;
exports.ParserATNSimulator = require('./ParserATNSimulator').ParserATNSimulator; exports.ParserATNSimulator = require('./ParserATNSimulator').ParserATNSimulator;
exports.PredictionMode = require('./PredictionMode').PredictionMode; exports.PredictionMode = require('./PredictionMode').PredictionMode;