antlr-ng
Version:
Next generation ANTLR Tool
114 lines (113 loc) • 3.58 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { LabelType } from "./LabelType.js";
class Alternative {
static {
__name(this, "Alternative");
}
ast;
/** Token IDs, string literals in this alt. */
tokenRefs = /* @__PURE__ */ new Map();
/** Does not include labels. */
tokenRefsInActions = /* @__PURE__ */ new Map();
/** All rule refs in this alt. */
ruleRefs = /* @__PURE__ */ new Map();
// does not include labels
ruleRefsInActions = /* @__PURE__ */ new Map();
/** A list of all LabelElementPair attached to tokens like id=ID, ids+=ID */
labelDefs = /* @__PURE__ */ new Map();
/**
* Track all executable actions other than named actions like @init
* and catch/finally (not in an alt). Also tracks predicates, rewrite actions.
* We need to examine these actions before code generation so
* that we can detect refs to $rule.attr etc...
*
* This tracks per alt
*/
actions = new Array();
/** What alternative number is this outermost alt? Used in templates. */
altNum;
rule;
constructor(r, altNum) {
this.rule = r;
this.altNum = altNum;
}
resolvesToToken(x, node) {
if (this.tokenRefs.has(x)) {
return true;
}
const anyLabelDef = this.getAnyLabelDef(x);
if (anyLabelDef !== null && anyLabelDef.type === LabelType.TokenLabel) {
return true;
}
return false;
}
resolvesToAttributeDict(x, node) {
if (this.resolvesToToken(x, node)) {
return true;
}
if (this.ruleRefs.has(x)) {
return true;
}
const anyLabelDef = this.getAnyLabelDef(x);
if (anyLabelDef !== null && anyLabelDef.type === LabelType.RuleLabel) {
return true;
}
return false;
}
resolveToAttribute(...args) {
if (args.length === 2) {
const [x2, node] = args;
return this.rule.resolveToAttribute(x2, node);
}
const [x, y, _node] = args;
if (this.tokenRefs.get(x)) {
return this.rule.getPredefinedScope(LabelType.TokenLabel)?.get(y) ?? null;
}
if (this.ruleRefs.get(x)) {
return this.rule.g.getRule(x).resolveRetvalOrProperty(y);
}
const anyLabelDef = this.getAnyLabelDef(x);
if (anyLabelDef !== null && anyLabelDef.type === LabelType.RuleLabel) {
return this.rule.g.getRule(anyLabelDef.element.getText()).resolveRetvalOrProperty(y);
} else {
if (anyLabelDef !== null) {
const scope = this.rule.getPredefinedScope(anyLabelDef.type);
if (scope === null) {
return null;
}
return scope.get(y);
}
}
return null;
}
resolvesToLabel(x, node) {
const anyLabelDef = this.getAnyLabelDef(x);
return anyLabelDef !== null && (anyLabelDef.type === LabelType.TokenLabel || anyLabelDef.type === LabelType.RuleLabel);
}
resolvesToListLabel(x, node) {
const anyLabelDef = this.getAnyLabelDef(x);
return anyLabelDef !== null && (anyLabelDef.type === LabelType.RuleListLabel || anyLabelDef.type === LabelType.TokenListLabel);
}
getAnyLabelDef(x) {
const labels = this.labelDefs.get(x);
if (labels) {
return labels[0];
}
return null;
}
/** x can be rule ref or rule label. */
resolveToRule(x) {
if (this.ruleRefs.get(x)) {
return this.rule.g.getRule(x);
}
const anyLabelDef = this.getAnyLabelDef(x);
if (anyLabelDef && anyLabelDef.type === LabelType.RuleLabel) {
return this.rule.g.getRule(anyLabelDef.element.getText());
}
return null;
}
}
export {
Alternative
};