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 Recognizer = require('./Recognizer').Recognizer;
var DefaultErrorStrategy = require('./error/ErrorStrategy').DefaultErrorStrategy;
var ATNDeserializer = require('./atn/ATNDeserializer').ATNDeserializer;
var ATNDeserializer = require('./atn/ATNDeserializer');
var ATNDeserializationOptions = require('./atn/ATNDeserializationOptions');
var TerminalNode = require('./tree/Tree').TerminalNode;
var ErrorNode = require('./tree/Tree').ErrorNode;

View File

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

View File

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