refactor DFASerializer and LexerDFASerializer to ES6 classes

This commit is contained in:
Camilo Roca 2020-02-10 21:54:23 +01:00
parent e5910c3019
commit 772286cf00
1 changed files with 57 additions and 56 deletions

View File

@ -6,73 +6,74 @@
// A DFA walker that knows how to dump them to serialized strings.#/
function DFASerializer(dfa, literalNames, symbolicNames) {
this.dfa = dfa;
this.literalNames = literalNames || [];
this.symbolicNames = symbolicNames || [];
return this;
}
class DFASerializer {
constructor(dfa, literalNames, symbolicNames) {
this.dfa = dfa;
this.literalNames = literalNames || [];
this.symbolicNames = symbolicNames || [];
}
DFASerializer.prototype.toString = function() {
if(this.dfa.s0 === 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');
}
}
toString() {
if(this.dfa.s0 === null) {
return null;
}
}
return buf.length===0 ? null : buf;
};
DFASerializer.prototype.getEdgeLabel = function(i) {
if (i===0) {
return "EOF";
} else if(this.literalNames !==null || this.symbolicNames!==null) {
return this.literalNames[i-1] || this.symbolicNames[i-1];
} else {
return String.fromCharCode(i-1);
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');
}
}
}
}
return buf.length===0 ? null : buf;
}
};
DFASerializer.prototype.getStateString = function(s) {
var baseStateStr = ( s.isAcceptState ? ":" : "") + "s" + s.stateNumber + ( s.requiresFullContext ? "^" : "");
if(s.isAcceptState) {
if (s.predicates !== null) {
return baseStateStr + "=>" + s.predicates.toString();
getEdgeLabel(i) {
if (i===0) {
return "EOF";
} else if(this.literalNames !==null || this.symbolicNames!==null) {
return this.literalNames[i-1] || this.symbolicNames[i-1];
} else {
return baseStateStr + "=>" + s.prediction.toString();
return String.fromCharCode(i-1);
}
} else {
return baseStateStr;
}
};
function LexerDFASerializer(dfa) {
DFASerializer.call(this, dfa, null);
return this;
getStateString(s) {
var baseStateStr = ( s.isAcceptState ? ":" : "") + "s" + s.stateNumber + ( s.requiresFullContext ? "^" : "");
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);
LexerDFASerializer.prototype.constructor = LexerDFASerializer;
class LexerDFASerializer extends DFASerializer {
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.LexerDFASerializer = LexerDFASerializer;