antlr-ng
Version:
Next generation ANTLR Tool
125 lines (124 loc) • 4.03 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import {
ActionTransition,
AtomTransition,
BlockEndState,
BlockStartState,
EpsilonTransition,
HashSet,
NotSetTransition,
PlusBlockStartState,
PlusLoopbackState,
RuleStartState,
RuleStopState,
RuleTransition,
SetTransition,
StarBlockStartState,
StarLoopbackState,
StarLoopEntryState
} from "antlr4ng";
class ATNPrinter {
constructor(g, start) {
this.g = g;
this.start = start;
}
static {
__name(this, "ATNPrinter");
}
work;
marked;
asString() {
this.marked = new HashSet();
this.work = [];
this.work.push(this.start);
const vocabulary = this.g.getVocabulary();
const result = [];
while (this.work.length > 0) {
const s = this.work.shift();
if (!s || this.marked.contains(s)) {
continue;
}
this.marked.add(s);
const targets = /* @__PURE__ */ new Set();
for (const t of s.transitions) {
if (!(s instanceof RuleStopState)) {
if (t instanceof RuleTransition) {
this.work.push(t.followState);
} else {
this.work.push(t.target);
}
} else {
if (targets.has(t.target.stateNumber)) {
continue;
}
targets.add(t.target.stateNumber);
}
const stateString = this.getStateString(s);
if (t instanceof EpsilonTransition) {
result.push(stateString + "->" + this.getStateString(t.target));
} else if (t instanceof RuleTransition) {
result.push(stateString + "-" + this.g.getRule(t.ruleIndex).name + "->" + this.getStateString(t.target));
} else if (t instanceof ActionTransition) {
result.push(stateString + "-" + t.toString() + "->" + this.getStateString(t.target));
} else if (t instanceof SetTransition) {
const not = t instanceof NotSetTransition;
if (this.g.isLexer()) {
result.push(stateString + "-" + (not ? "~" : "") + t.toString() + "->" + this.getStateString(t.target));
} else {
result.push(stateString + "-" + (not ? "~" : "") + t.label.toStringWithVocabulary(vocabulary) + "->" + this.getStateString(t.target));
}
} else if (t instanceof AtomTransition) {
const label = this.g.getTokenDisplayName(t.labelValue);
result.push(stateString + "-" + label + "->" + this.getStateString(t.target));
} else {
result.push(stateString + `-${t}->${this.getStateString(t.target)}`);
}
}
}
return result.join("\n") + "\n";
}
getStateString(s) {
const n = s.stateNumber;
let stateStr = "s" + n;
if (s instanceof StarBlockStartState) {
stateStr = "StarBlockStart_" + n;
} else {
if (s instanceof PlusBlockStartState) {
stateStr = "PlusBlockStart_" + n;
} else {
if (s instanceof BlockStartState) {
stateStr = "BlockStart_" + n;
} else {
if (s instanceof BlockEndState) {
stateStr = "BlockEnd_" + n;
} else {
if (s instanceof RuleStartState) {
stateStr = "RuleStart_" + this.g.getRule(s.ruleIndex).name + "_" + n;
} else {
if (s instanceof RuleStopState) {
stateStr = "RuleStop_" + this.g.getRule(s.ruleIndex).name + "_" + n;
} else {
if (s instanceof PlusLoopbackState) {
stateStr = "PlusLoopBack_" + n;
} else {
if (s instanceof StarLoopbackState) {
stateStr = "StarLoopBack_" + n;
} else {
if (s instanceof StarLoopEntryState) {
stateStr = "StarLoopEntry_" + n;
}
}
}
}
}
}
}
}
}
return stateStr;
}
}
export {
ATNPrinter
};