UNPKG

antlr-ng

Version:

Next generation ANTLR Tool

136 lines (135 loc) 4.27 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); import { GrammarTreeVisitor } from "../tree/walkers/GrammarTreeVisitor.js"; import { ActionAST } from "../tool/ast/ActionAST.js"; import { LabelElementPair } from "../tool/LabelElementPair.js"; class SymbolCollector extends GrammarTreeVisitor { static { __name(this, "SymbolCollector"); } /** which grammar are we checking */ g; // stuff to collect ruleRefs = new Array(); qualifiedRuleRefs = new Array(); terminals = new Array(); tokenIDRefs = new Array(); strings = /* @__PURE__ */ new Set(); tokensDefs = new Array(); channelDefs = new Array(); // context currentRule = null; /** Track action name node in @parser::members {...} or @members {...} */ namedActions = new Array(); constructor(g) { super(g.tool.errorManager); this.g = g; } process(ast) { this.visitGrammar(ast); } globalNamedAction(scope, id, action) { action.setScope(scope); this.namedActions.push(id.parent); action.resolver = this.g; } defineToken(id) { this.terminals.push(id); this.tokenIDRefs.push(id); this.tokensDefs.push(id); } defineChannel(id) { this.channelDefs.push(id); } discoverRule(rule, id, modifiers, arg, returns, throws, options, locals, actions, block) { this.currentRule = this.g.getRule(id.getText()); } discoverLexerRule(rule, id, modifiers, options, block) { this.currentRule = this.g.getRule(id.getText()); } discoverOuterAlt(alt) { this.currentRule.alt[this.currentOuterAltNumber].ast = alt; } actionInAlt(action) { this.currentRule.defineActionInAlt(this.currentOuterAltNumber, action); action.resolver = this.currentRule.alt[this.currentOuterAltNumber]; } sempredInAlt(pred) { this.currentRule.definePredicateInAlt(this.currentOuterAltNumber, pred); pred.resolver = this.currentRule.alt[this.currentOuterAltNumber]; } ruleCatch(arg, action) { const catchMe = action.parent; this.currentRule.exceptions.push(catchMe); action.resolver = this.currentRule; } finallyAction(action) { this.currentRule.finallyAction = action; action.resolver = this.currentRule; } label(op, id, element) { const lp = new LabelElementPair(this.g, id, element, op.getType()); const list = this.currentRule.alt[this.currentOuterAltNumber].labelDefs.get(id.getText()); if (list) { list.push(lp); } else { this.currentRule.alt[this.currentOuterAltNumber].labelDefs.set(id.getText(), [lp]); } } stringRef(ref) { this.terminals.push(ref); this.strings.add(ref.getText()); if (this.currentRule) { const list = this.currentRule.alt[this.currentOuterAltNumber].tokenRefs.get(ref.getText()); if (list) { list.push(ref); } else { this.currentRule.alt[this.currentOuterAltNumber].tokenRefs.set(ref.getText(), [ref]); } } } tokenRef(ref) { this.terminals.push(ref); this.tokenIDRefs.push(ref); if (this.currentRule) { const list = this.currentRule.alt[this.currentOuterAltNumber].tokenRefs.get(ref.getText()); if (list) { list.push(ref); } else { this.currentRule.alt[this.currentOuterAltNumber].tokenRefs.set(ref.getText(), [ref]); } } } ruleRef(ref, arg) { this.ruleRefs.push(ref); if (this.currentRule !== null) { const list = this.currentRule.alt[this.currentOuterAltNumber].ruleRefs.get(ref.getText()); if (list) { list.push(ref); } else { this.currentRule.alt[this.currentOuterAltNumber].ruleRefs.set(ref.getText(), [ref]); } } } grammarOption(_id, valueAST) { this.setActionResolver(valueAST); } ruleOption(_id, valueAST) { this.setActionResolver(valueAST); } blockOption(_id, valueAST) { this.setActionResolver(valueAST); } elementOption(t, id, valueAST) { this.setActionResolver(valueAST); } /** In case of option id={...}, set resolve in case they use $foo */ setActionResolver(valueAST) { if (valueAST instanceof ActionAST) { valueAST.resolver = this.currentRule.alt[this.currentOuterAltNumber]; } } } export { SymbolCollector };