antlr-ng
Version:
Next generation ANTLR Tool
116 lines (115 loc) • 3.37 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { CommonToken, Token } from "antlr4ng";
import { TreePattern } from "./TreePattern.js";
import { TreePatternLexer } from "./TreePatternLexer.js";
import { WildcardTreePattern } from "./WildcardTreePattern.js";
class TreePatternParser {
static {
__name(this, "TreePatternParser");
}
tokenizer;
ttype;
wizard;
constructor(tokenizer, wizard) {
this.tokenizer = tokenizer;
this.wizard = wizard;
this.ttype = tokenizer.nextToken();
}
pattern() {
if (this.ttype === TreePatternLexer.BEGIN) {
return this.parseTree();
} else if (this.ttype === TreePatternLexer.ID) {
const node = this.parseNode();
if (this.ttype === TreePatternLexer.EOF) {
return node;
}
return null;
}
return null;
}
parseTree() {
if (this.ttype !== TreePatternLexer.BEGIN) {
throw new Error("no BEGIN");
}
this.ttype = this.tokenizer.nextToken();
const root = this.parseNode();
if (root === null) {
return null;
}
while (this.ttype === TreePatternLexer.BEGIN || this.ttype === TreePatternLexer.ID || this.ttype === TreePatternLexer.PERCENT || this.ttype === TreePatternLexer.DOT) {
if (this.ttype === TreePatternLexer.BEGIN) {
const subtree = this.parseTree();
if (subtree) {
root.addChild(subtree);
}
} else {
const child = this.parseNode();
if (child === null) {
return null;
}
root.addChild(child);
}
}
if (this.ttype !== TreePatternLexer.END) {
throw new Error("no END");
}
this.ttype = this.tokenizer.nextToken();
return root;
}
parseNode() {
let label = null;
if (this.ttype === TreePatternLexer.PERCENT) {
this.ttype = this.tokenizer.nextToken();
if (this.ttype !== TreePatternLexer.ID) {
return null;
}
label = this.tokenizer.stringValue;
this.ttype = this.tokenizer.nextToken();
if (this.ttype !== TreePatternLexer.COLON) {
return null;
}
this.ttype = this.tokenizer.nextToken();
}
if (this.ttype === TreePatternLexer.DOT) {
this.ttype = this.tokenizer.nextToken();
const wildcardPayload = CommonToken.fromType(0, ".");
const node2 = new WildcardTreePattern(wildcardPayload);
if (label !== null) {
node2.label = label;
}
return node2;
}
if (this.ttype !== TreePatternLexer.ID) {
return null;
}
const tokenName = this.tokenizer.stringValue;
this.ttype = this.tokenizer.nextToken();
if (tokenName === "nil") {
return new TreePattern();
}
let text = tokenName;
let arg = null;
if (this.ttype === TreePatternLexer.ARG) {
arg = this.tokenizer.stringValue;
text = arg;
this.ttype = this.tokenizer.nextToken();
}
const treeNodeType = this.wizard.getTokenType(tokenName);
if (treeNodeType === Token.INVALID_TYPE) {
return null;
}
const token = CommonToken.fromType(treeNodeType, text);
const node = new TreePattern(token);
if (label !== null) {
node.label = label;
}
if (arg !== null) {
node.hasTextArg = true;
}
return node;
}
}
export {
TreePatternParser
};