antlr-ng
Version:
Next generation ANTLR Tool
257 lines (256 loc) • 9.79 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
import { CharStream } from "antlr4ng";
import { ActionSplitter } from "../generated/ActionSplitter.js";
import { DictType } from "../misc/types.js";
import { IssueCode } from "../tool/Issues.js";
import { ActionText } from "./model/chunk/ActionText.js";
import { ArgRef } from "./model/chunk/ArgRef.js";
import { LabelRef } from "./model/chunk/LabelRef.js";
import { ListLabelRef } from "./model/chunk/ListLabelRef.js";
import { LocalRef } from "./model/chunk/LocalRef.js";
import { NonLocalAttrRef } from "./model/chunk/NonLocalAttrRef.js";
import { QRetValueRef } from "./model/chunk/QRetValueRef.js";
import { RetValueRef } from "./model/chunk/RetValueRef.js";
import { RulePropertyRefCtx } from "./model/chunk/RulePropertyRefCtx.js";
import { RulePropertyRefParser } from "./model/chunk/RulePropertyRefParser.js";
import { RulePropertyRefStart } from "./model/chunk/RulePropertyRefStart.js";
import { RulePropertyRefStop } from "./model/chunk/RulePropertyRefStop.js";
import { RulePropertyRefText } from "./model/chunk/RulePropertyRefText.js";
import { SetAttr } from "./model/chunk/SetAttr.js";
import { SetNonLocalAttr } from "./model/chunk/SetNonLocalAttr.js";
import { ThisRulePropertyRefCtx } from "./model/chunk/ThisRulePropertyRefCtx.js";
import { ThisRulePropertyRefParser } from "./model/chunk/ThisRulePropertyRefParser.js";
import { ThisRulePropertyRefStart } from "./model/chunk/ThisRulePropertyRefStart.js";
import { ThisRulePropertyRefStop } from "./model/chunk/ThisRulePropertyRefStop.js";
import { ThisRulePropertyRefText } from "./model/chunk/ThisRulePropertyRefText.js";
import { TokenPropertyRefChannel } from "./model/chunk/TokenPropertyRefChannel.js";
import { TokenPropertyRefIndex } from "./model/chunk/TokenPropertyRefIndex.js";
import { TokenPropertyRefInt } from "./model/chunk/TokenPropertyRefInt.js";
import { TokenPropertyRefLine } from "./model/chunk/TokenPropertyRefLine.js";
import { TokenPropertyRefPos } from "./model/chunk/TokenPropertyRefPos.js";
import { TokenPropertyRefText } from "./model/chunk/TokenPropertyRefText.js";
import { TokenPropertyRefType } from "./model/chunk/TokenPropertyRefType.js";
import { TokenRef } from "./model/chunk/TokenRef.js";
class ActionTranslator {
static {
__name(this, "ActionTranslator");
}
static thisRulePropToModelMap = /* @__PURE__ */ new Map([
["start", ThisRulePropertyRefStart],
["stop", ThisRulePropertyRefStop],
["text", ThisRulePropertyRefText],
["ctx", ThisRulePropertyRefCtx],
["parser", ThisRulePropertyRefParser]
]);
static rulePropToModelMap = /* @__PURE__ */ new Map([
["start", RulePropertyRefStart],
["stop", RulePropertyRefStop],
["text", RulePropertyRefText],
["ctx", RulePropertyRefCtx],
["parser", RulePropertyRefParser]
]);
static tokenPropToModelMap = /* @__PURE__ */ new Map([
["text", TokenPropertyRefText],
["type", TokenPropertyRefType],
["line", TokenPropertyRefLine],
["index", TokenPropertyRefIndex],
["pos", TokenPropertyRefPos],
["channel", TokenPropertyRefChannel],
["int", TokenPropertyRefInt]
]);
gen;
target;
node;
rf;
chunks = new Array();
factory;
nodeContext;
constructor(factory, node) {
this.factory = factory;
this.node = node;
this.gen = factory.getGenerator();
this.target = this.gen.target;
}
static translateAction(factory, rf, tokenWithinAction, node) {
let action = tokenWithinAction.text;
if (action.startsWith("{")) {
const firstCurly = action.indexOf("{");
const lastCurly = action.lastIndexOf("}");
if (firstCurly >= 0 && lastCurly >= 0) {
action = action.substring(firstCurly + 1, lastCurly);
}
}
return ActionTranslator.translateActionChunk(factory, rf, action, node, tokenWithinAction);
}
static translateActionChunk(factory, rf, action, node, refToken) {
const translator = new ActionTranslator(factory, node);
translator.rf = rf ?? void 0;
factory.g.tool.logInfo({ component: "action-translator", msg: "translate " + action });
const altLabel = node.getAltLabel();
if (rf) {
translator.nodeContext = rf.ruleCtx;
if (altLabel !== null) {
translator.nodeContext = rf.altLabelCtxs.get(altLabel);
}
}
const input = CharStream.fromString(action);
const trigger = new ActionSplitter(input);
trigger.getActionTokens(translator, refToken);
return translator.chunks;
}
attr(_expr, x) {
this.gen.g?.tool.logInfo({ component: "action-translator", msg: "attr " + x.text });
const a = this.node.resolver.resolveToAttribute(x.text, this.node);
const name = x.text;
const escapedName = this.target.escapeIfNeeded(name);
if (a !== null) {
switch (a.dict.type) {
case DictType.Argument: {
this.chunks.push(new ArgRef(this.nodeContext, name, escapedName));
break;
}
case DictType.Return: {
this.chunks.push(new RetValueRef(this.rf.ruleCtx, name, escapedName));
break;
}
case DictType.Local: {
this.chunks.push(new LocalRef(this.nodeContext, name, escapedName));
break;
}
case DictType.PredefinedRule: {
this.chunks.push(this.getRulePropertyRef(void 0, x.text));
break;
}
default: {
break;
}
}
}
if (this.node.resolver.resolvesToToken(name, this.node)) {
const tokenLabel = this.getTokenLabel(name);
this.chunks.push(new TokenRef(this.nodeContext, tokenLabel, this.target.escapeIfNeeded(tokenLabel)));
return;
}
if (this.node.resolver.resolvesToLabel(name, this.node)) {
const tokenLabel = this.getTokenLabel(name);
this.chunks.push(new LabelRef(this.nodeContext, tokenLabel, this.target.escapeIfNeeded(tokenLabel)));
return;
}
if (this.node.resolver.resolvesToListLabel(name, this.node)) {
this.chunks.push(new ListLabelRef(this.nodeContext, name, escapedName));
return;
}
const r = this.factory.g.getRule(name);
if (r !== null) {
const ruleLabel = this.getRuleLabel(name);
this.chunks.push(new LabelRef(this.nodeContext, ruleLabel, this.target.escapeIfNeeded(ruleLabel)));
}
}
qualifiedAttr(expr, x, y) {
this.gen.g?.tool.logInfo({ component: "action-translator", msg: "q-attr " + x.text + "." + y.text });
if (this.node.resolver.resolveToAttribute(x.text, this.node) !== null) {
this.attr(expr, x);
this.chunks.push(new ActionText(this.nodeContext, "." + y.text));
return;
}
const a = this.node.resolver.resolveToAttribute(x.text, y.text, this.node);
if (a === null) {
this.gen.g.tool.errorManager.grammarError(
IssueCode.UnknownSimpleAttribute,
this.gen.g.fileName,
x,
x.text,
"rule"
);
return;
}
switch (a.dict.type) {
case DictType.Argument: {
this.chunks.push(new ArgRef(this.nodeContext, y.text, this.target.escapeIfNeeded(y.text)));
break;
}
// Has to be current rule.
case DictType.Return: {
this.chunks.push(new QRetValueRef(
this.nodeContext,
this.getRuleLabel(x.text),
y.text,
this.target.escapeIfNeeded(y.text)
));
break;
}
case DictType.PredefinedRule: {
this.chunks.push(this.getRulePropertyRef(x.text, y.text));
break;
}
case DictType.Token: {
this.chunks.push(this.getTokenPropertyRef(x.text, y.text));
break;
}
default: {
break;
}
}
}
setAttr(_expr, x, rhs) {
this.gen.g?.tool.logInfo({ component: "action-translator", msg: "setAttr " + x.text + " " + rhs.text });
const rhsChunks = ActionTranslator.translateActionChunk(this.factory, this.rf, rhs.text, this.node, x);
const s = new SetAttr(this.nodeContext, x.text, this.target.escapeIfNeeded(x.text), rhsChunks);
this.chunks.push(s);
}
nonLocalAttr(_expr, x, y) {
this.gen.g?.tool.logInfo({ component: "action-translator", msg: "nonLocalAttr " + x.text + "::" + y.text });
const r = this.factory.g.getRule(x.text);
this.chunks.push(new NonLocalAttrRef(
this.nodeContext,
x.text,
y.text,
this.target.escapeIfNeeded(y.text),
r.index
));
}
setNonLocalAttr(_expr, x, y, rhs) {
this.gen.g?.tool.logInfo({
component: "action-translator",
msg: "setNonLocalAttr " + x.text + "::" + y.text + "=" + rhs
});
const r = this.factory.g.getRule(x.text);
const rhsChunks = ActionTranslator.translateActionChunk(this.factory, this.rf, rhs, this.node);
const s = new SetNonLocalAttr(
this.nodeContext,
x.text,
y.text,
this.target.escapeIfNeeded(y.text),
r.index,
rhsChunks
);
this.chunks.push(s);
}
text(text) {
this.chunks.push(new ActionText(this.nodeContext, text));
}
getTokenLabel(x) {
if (this.node.resolver.resolvesToLabel(x, this.node)) {
return x;
}
return this.target.getImplicitTokenLabel(x);
}
getRuleLabel(x) {
if (this.node.resolver.resolvesToLabel(x, this.node)) {
return x;
}
return this.target.getImplicitRuleLabel(x);
}
getTokenPropertyRef(x, y) {
const c = ActionTranslator.tokenPropToModelMap.get(y);
return new c(this.nodeContext, this.getTokenLabel(x));
}
getRulePropertyRef(x, prop) {
const c = (x !== void 0 ? ActionTranslator.rulePropToModelMap : ActionTranslator.thisRulePropToModelMap).get(prop);
return new c(this.nodeContext, this.getRuleLabel(x ?? prop));
}
}
export {
ActionTranslator
};