refactor DFASerializer and LexerDFASerializer to ES6 classes
This commit is contained in:
parent
e5910c3019
commit
772286cf00
|
@ -6,73 +6,74 @@
|
||||||
// A DFA walker that knows how to dump them to serialized strings.#/
|
// A DFA walker that knows how to dump them to serialized strings.#/
|
||||||
|
|
||||||
|
|
||||||
function DFASerializer(dfa, literalNames, symbolicNames) {
|
class DFASerializer {
|
||||||
this.dfa = dfa;
|
constructor(dfa, literalNames, symbolicNames) {
|
||||||
this.literalNames = literalNames || [];
|
this.dfa = dfa;
|
||||||
this.symbolicNames = symbolicNames || [];
|
this.literalNames = literalNames || [];
|
||||||
return this;
|
this.symbolicNames = symbolicNames || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
DFASerializer.prototype.toString = function() {
|
toString() {
|
||||||
if(this.dfa.s0 === null) {
|
if(this.dfa.s0 === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
var buf = "";
|
|
||||||
var states = this.dfa.sortedStates();
|
|
||||||
for(var i=0;i<states.length;i++) {
|
|
||||||
var s = states[i];
|
|
||||||
if(s.edges!==null) {
|
|
||||||
var n = s.edges.length;
|
|
||||||
for(var j=0;j<n;j++) {
|
|
||||||
var t = s.edges[j] || null;
|
|
||||||
if(t!==null && t.stateNumber !== 0x7FFFFFFF) {
|
|
||||||
buf = buf.concat(this.getStateString(s));
|
|
||||||
buf = buf.concat("-");
|
|
||||||
buf = buf.concat(this.getEdgeLabel(j));
|
|
||||||
buf = buf.concat("->");
|
|
||||||
buf = buf.concat(this.getStateString(t));
|
|
||||||
buf = buf.concat('\n');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
var buf = "";
|
||||||
return buf.length===0 ? null : buf;
|
var states = this.dfa.sortedStates();
|
||||||
};
|
for(var i=0;i<states.length;i++) {
|
||||||
|
var s = states[i];
|
||||||
DFASerializer.prototype.getEdgeLabel = function(i) {
|
if(s.edges!==null) {
|
||||||
if (i===0) {
|
var n = s.edges.length;
|
||||||
return "EOF";
|
for(var j=0;j<n;j++) {
|
||||||
} else if(this.literalNames !==null || this.symbolicNames!==null) {
|
var t = s.edges[j] || null;
|
||||||
return this.literalNames[i-1] || this.symbolicNames[i-1];
|
if(t!==null && t.stateNumber !== 0x7FFFFFFF) {
|
||||||
} else {
|
buf = buf.concat(this.getStateString(s));
|
||||||
return String.fromCharCode(i-1);
|
buf = buf.concat("-");
|
||||||
|
buf = buf.concat(this.getEdgeLabel(j));
|
||||||
|
buf = buf.concat("->");
|
||||||
|
buf = buf.concat(this.getStateString(t));
|
||||||
|
buf = buf.concat('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf.length===0 ? null : buf;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
DFASerializer.prototype.getStateString = function(s) {
|
getEdgeLabel(i) {
|
||||||
var baseStateStr = ( s.isAcceptState ? ":" : "") + "s" + s.stateNumber + ( s.requiresFullContext ? "^" : "");
|
if (i===0) {
|
||||||
if(s.isAcceptState) {
|
return "EOF";
|
||||||
if (s.predicates !== null) {
|
} else if(this.literalNames !==null || this.symbolicNames!==null) {
|
||||||
return baseStateStr + "=>" + s.predicates.toString();
|
return this.literalNames[i-1] || this.symbolicNames[i-1];
|
||||||
} else {
|
} else {
|
||||||
return baseStateStr + "=>" + s.prediction.toString();
|
return String.fromCharCode(i-1);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return baseStateStr;
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
function LexerDFASerializer(dfa) {
|
getStateString(s) {
|
||||||
DFASerializer.call(this, dfa, null);
|
var baseStateStr = ( s.isAcceptState ? ":" : "") + "s" + s.stateNumber + ( s.requiresFullContext ? "^" : "");
|
||||||
return this;
|
if(s.isAcceptState) {
|
||||||
|
if (s.predicates !== null) {
|
||||||
|
return baseStateStr + "=>" + s.predicates.toString();
|
||||||
|
} else {
|
||||||
|
return baseStateStr + "=>" + s.prediction.toString();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return baseStateStr;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LexerDFASerializer.prototype = Object.create(DFASerializer.prototype);
|
class LexerDFASerializer extends DFASerializer {
|
||||||
LexerDFASerializer.prototype.constructor = LexerDFASerializer;
|
constructor(dfa) {
|
||||||
|
super(dfa, null);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
getEdgeLabel(i) {
|
||||||
|
return "'" + String.fromCharCode(i) + "'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LexerDFASerializer.prototype.getEdgeLabel = function(i) {
|
|
||||||
return "'" + String.fromCharCode(i) + "'";
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.DFASerializer = DFASerializer;
|
exports.DFASerializer = DFASerializer;
|
||||||
exports.LexerDFASerializer = LexerDFASerializer;
|
exports.LexerDFASerializer = LexerDFASerializer;
|
||||||
|
|
Loading…
Reference in New Issue