refactor DFA function prototype into es6 classes
This commit is contained in:
parent
1177c0b58d
commit
a8e8402b77
|
@ -4,43 +4,43 @@
|
|||
* can be found in the LICENSE.txt file in the project root.
|
||||
*/
|
||||
|
||||
var Set = require("../Utils").Set;
|
||||
var DFAState = require('./DFAState').DFAState;
|
||||
var StarLoopEntryState = require('../atn/ATNState').StarLoopEntryState;
|
||||
var ATNConfigSet = require('./../atn/ATNConfigSet').ATNConfigSet;
|
||||
var DFASerializer = require('./DFASerializer').DFASerializer;
|
||||
var LexerDFASerializer = require('./DFASerializer').LexerDFASerializer;
|
||||
const Set = require("../Utils").Set;
|
||||
const DFAState = require('./DFAState').DFAState;
|
||||
const StarLoopEntryState = require('../atn/ATNState').StarLoopEntryState;
|
||||
const ATNConfigSet = require('./../atn/ATNConfigSet').ATNConfigSet;
|
||||
const DFASerializer = require('./DFASerializer').DFASerializer;
|
||||
const LexerDFASerializer = require('./DFASerializer').LexerDFASerializer;
|
||||
|
||||
|
||||
|
||||
function DFA(atnStartState, decision) {
|
||||
if (decision === undefined) {
|
||||
decision = 0;
|
||||
class DFA {
|
||||
constructor(atnStartState, decision) {
|
||||
if (decision === undefined) {
|
||||
decision = 0;
|
||||
}
|
||||
// From which ATN state did we create this DFA?
|
||||
this.atnStartState = atnStartState;
|
||||
this.decision = decision;
|
||||
// A set of all DFA states. Use {@link Map} so we can get old state back
|
||||
// ({@link Set} only allows you to see if it's there).
|
||||
this._states = new Set();
|
||||
this.s0 = null;
|
||||
// {@code true} if this DFA is for a precedence decision; otherwise,
|
||||
// {@code false}. This is the backing field for {@link //isPrecedenceDfa},
|
||||
// {@link //setPrecedenceDfa}.
|
||||
this.precedenceDfa = false;
|
||||
if (atnStartState instanceof StarLoopEntryState)
|
||||
{
|
||||
if (atnStartState.isPrecedenceDecision) {
|
||||
this.precedenceDfa = true;
|
||||
const precedenceState = new DFAState(null, new ATNConfigSet());
|
||||
precedenceState.edges = [];
|
||||
precedenceState.isAcceptState = false;
|
||||
precedenceState.requiresFullContext = false;
|
||||
this.s0 = precedenceState;
|
||||
}
|
||||
}
|
||||
}
|
||||
// From which ATN state did we create this DFA?
|
||||
this.atnStartState = atnStartState;
|
||||
this.decision = decision;
|
||||
// A set of all DFA states. Use {@link Map} so we can get old state back
|
||||
// ({@link Set} only allows you to see if it's there).
|
||||
this._states = new Set();
|
||||
this.s0 = null;
|
||||
// {@code true} if this DFA is for a precedence decision; otherwise,
|
||||
// {@code false}. This is the backing field for {@link //isPrecedenceDfa},
|
||||
// {@link //setPrecedenceDfa}.
|
||||
this.precedenceDfa = false;
|
||||
if (atnStartState instanceof StarLoopEntryState)
|
||||
{
|
||||
if (atnStartState.isPrecedenceDecision) {
|
||||
this.precedenceDfa = true;
|
||||
var precedenceState = new DFAState(null, new ATNConfigSet());
|
||||
precedenceState.edges = [];
|
||||
precedenceState.isAcceptState = false;
|
||||
precedenceState.requiresFullContext = false;
|
||||
this.s0 = precedenceState;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
// Get the start state for a specific precedence value.
|
||||
//
|
||||
|
@ -50,17 +50,16 @@ function DFA(atnStartState, decision) {
|
|||
//
|
||||
// @throws IllegalStateException if this is not a precedence DFA.
|
||||
// @see //isPrecedenceDfa()
|
||||
|
||||
DFA.prototype.getPrecedenceStartState = function(precedence) {
|
||||
if (!(this.precedenceDfa)) {
|
||||
throw ("Only precedence DFAs may contain a precedence start state.");
|
||||
getPrecedenceStartState(precedence) {
|
||||
if (!(this.precedenceDfa)) {
|
||||
throw ("Only precedence DFAs may contain a precedence start state.");
|
||||
}
|
||||
// s0.edges is never null for a precedence DFA
|
||||
if (precedence < 0 || precedence >= this.s0.edges.length) {
|
||||
return null;
|
||||
}
|
||||
return this.s0.edges[precedence] || null;
|
||||
}
|
||||
// s0.edges is never null for a precedence DFA
|
||||
if (precedence < 0 || precedence >= this.s0.edges.length) {
|
||||
return null;
|
||||
}
|
||||
return this.s0.edges[precedence] || null;
|
||||
};
|
||||
|
||||
// Set the start state for a specific precedence value.
|
||||
//
|
||||
|
@ -71,19 +70,19 @@ DFA.prototype.getPrecedenceStartState = function(precedence) {
|
|||
// @throws IllegalStateException if this is not a precedence DFA.
|
||||
// @see //isPrecedenceDfa()
|
||||
//
|
||||
DFA.prototype.setPrecedenceStartState = function(precedence, startState) {
|
||||
if (!(this.precedenceDfa)) {
|
||||
throw ("Only precedence DFAs may contain a precedence start state.");
|
||||
}
|
||||
if (precedence < 0) {
|
||||
return;
|
||||
}
|
||||
setPrecedenceStartState(precedence, startState) {
|
||||
if (!(this.precedenceDfa)) {
|
||||
throw ("Only precedence DFAs may contain a precedence start state.");
|
||||
}
|
||||
if (precedence < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// synchronization on s0 here is ok. when the DFA is turned into a
|
||||
// precedence DFA, s0 will be initialized once and not updated again
|
||||
// s0.edges is never null for a precedence DFA
|
||||
this.s0.edges[precedence] = startState;
|
||||
};
|
||||
// synchronization on s0 here is ok. when the DFA is turned into a
|
||||
// precedence DFA, s0 will be initialized once and not updated again
|
||||
// s0.edges is never null for a precedence DFA
|
||||
this.s0.edges[precedence] = startState;
|
||||
}
|
||||
|
||||
//
|
||||
// Sets whether this is a precedence DFA. If the specified value differs
|
||||
|
@ -101,53 +100,52 @@ DFA.prototype.setPrecedenceStartState = function(precedence, startState) {
|
|||
//
|
||||
// @param precedenceDfa {@code true} if this is a precedence DFA; otherwise,
|
||||
// {@code false}
|
||||
|
||||
DFA.prototype.setPrecedenceDfa = function(precedenceDfa) {
|
||||
if (this.precedenceDfa!==precedenceDfa) {
|
||||
this._states = new DFAStatesSet();
|
||||
if (precedenceDfa) {
|
||||
var precedenceState = new DFAState(null, new ATNConfigSet());
|
||||
precedenceState.edges = [];
|
||||
precedenceState.isAcceptState = false;
|
||||
precedenceState.requiresFullContext = false;
|
||||
this.s0 = precedenceState;
|
||||
} else {
|
||||
this.s0 = null;
|
||||
setPrecedenceDfa(precedenceDfa) {
|
||||
if (this.precedenceDfa!==precedenceDfa) {
|
||||
this._states = new DFAStatesSet();
|
||||
if (precedenceDfa) {
|
||||
const precedenceState = new DFAState(null, new ATNConfigSet());
|
||||
precedenceState.edges = [];
|
||||
precedenceState.isAcceptState = false;
|
||||
precedenceState.requiresFullContext = false;
|
||||
this.s0 = precedenceState;
|
||||
} else {
|
||||
this.s0 = null;
|
||||
}
|
||||
this.precedenceDfa = precedenceDfa;
|
||||
}
|
||||
this.precedenceDfa = precedenceDfa;
|
||||
}
|
||||
};
|
||||
|
||||
Object.defineProperty(DFA.prototype, "states", {
|
||||
get : function() {
|
||||
return this._states;
|
||||
}
|
||||
});
|
||||
|
||||
// Return a list of all states in this DFA, ordered by state number.
|
||||
DFA.prototype.sortedStates = function() {
|
||||
var list = this._states.values();
|
||||
return list.sort(function(a, b) {
|
||||
return a.stateNumber - b.stateNumber;
|
||||
});
|
||||
};
|
||||
|
||||
DFA.prototype.toString = function(literalNames, symbolicNames) {
|
||||
literalNames = literalNames || null;
|
||||
symbolicNames = symbolicNames || null;
|
||||
if (this.s0 === null) {
|
||||
return "";
|
||||
sortedStates() {
|
||||
const list = this._states.values();
|
||||
return list.sort(function(a, b) {
|
||||
return a.stateNumber - b.stateNumber;
|
||||
});
|
||||
}
|
||||
var serializer = new DFASerializer(this, literalNames, symbolicNames);
|
||||
return serializer.toString();
|
||||
};
|
||||
|
||||
DFA.prototype.toLexerString = function() {
|
||||
if (this.s0 === null) {
|
||||
return "";
|
||||
toString(literalNames, symbolicNames) {
|
||||
literalNames = literalNames || null;
|
||||
symbolicNames = symbolicNames || null;
|
||||
if (this.s0 === null) {
|
||||
return "";
|
||||
}
|
||||
const serializer = new DFASerializer(this, literalNames, symbolicNames);
|
||||
return serializer.toString();
|
||||
}
|
||||
var serializer = new LexerDFASerializer(this);
|
||||
return serializer.toString();
|
||||
};
|
||||
|
||||
toLexerString() {
|
||||
if (this.s0 === null) {
|
||||
return "";
|
||||
}
|
||||
const serializer = new LexerDFASerializer(this);
|
||||
return serializer.toString();
|
||||
}
|
||||
|
||||
get states(){
|
||||
return this._states;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
exports.DFA = DFA;
|
||||
|
|
Loading…
Reference in New Issue