antlr-ng
Version:
Next generation ANTLR Tool
50 lines (49 loc) • 1.56 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { CommonToken } from "antlr4ng";
import { CommonTreeAdaptor } from "../tree/CommonTreeAdaptor.js";
import { ANTLRv4Parser } from "../generated/ANTLRv4Parser.js";
import { ClassFactory } from "../ClassFactory.js";
import { GrammarAST } from "../tool/ast/GrammarAST.js";
import { RuleAST } from "../tool/ast/RuleAST.js";
import { TerminalAST } from "../tool/ast/TerminalAST.js";
class GrammarASTAdaptor extends CommonTreeAdaptor {
static {
__name(this, "GrammarASTAdaptor");
}
inputStream;
constructor(inputStream) {
super();
this.inputStream = inputStream;
}
create(...args) {
if (args.length === 1) {
const [token] = args;
return new GrammarAST(token);
}
if (args.length === 2 && typeof args[1] === "string") {
const [tokenType, text] = args;
let t;
if (tokenType === ANTLRv4Parser.RULE_REF) {
t = new RuleAST(CommonToken.fromType(tokenType, text));
} else {
if (tokenType === ANTLRv4Parser.STRING_LITERAL) {
t = new TerminalAST(CommonToken.fromType(tokenType, text));
} else {
t = super.create(tokenType, text);
}
}
t.token.inputStream = this.inputStream ?? null;
return t;
}
return super.create.apply(this, args);
}
static {
ClassFactory.createGrammarASTAdaptor = (input) => {
return new GrammarASTAdaptor(input);
};
}
}
export {
GrammarASTAdaptor
};