/** $x.y, x can be surrounding rule, token/rule/label ref. y is visible * attr in that dictionary. Can't see args on rule refs. */ public virtual Attribute ResolveToAttribute(string x, string y, ActionAST node) { if (tokenRefs.ContainsKey(x) && tokenRefs[x] != null) { // token ref in this alt? return rule.GetPredefinedScope(LabelType.TOKEN_LABEL).Get(y); } if (ruleRefs.ContainsKey(x) && ruleRefs[x] != null) { // rule ref in this alt? // look up rule, ask it to resolve y (must be retval or predefined) return rule.g.GetRule(x).ResolveRetvalOrProperty(y); } LabelElementPair anyLabelDef = GetAnyLabelDef(x); if (anyLabelDef != null && anyLabelDef.type == LabelType.RULE_LABEL) { return rule.g.GetRule(anyLabelDef.element.Text).ResolveRetvalOrProperty(y); } else if (anyLabelDef != null) { AttributeDict scope = rule.GetPredefinedScope(anyLabelDef.type); if (scope == null) { return null; } return scope.Get(y); } return null; }
public ExceptionClause(OutputModelFactory factory, ActionAST catchArg, ActionAST catchAction) : base(factory, catchArg) { this.catchArg = new Action(factory, catchArg); this.catchAction = new Action(factory, catchAction); }
public override void DiscoverRule(RuleAST rule, GrammarAST ID, IList<GrammarAST> modifiers, ActionAST arg, ActionAST returns, GrammarAST thrws, GrammarAST options, ActionAST locals, IList<GrammarAST> actions, GrammarAST block) { currentRule = g.GetRule(ID.Text); }
public ActionSniffer(Grammar g, Rule r, Alternative alt, ActionAST node, IToken actionToken) { this.g = g; this.r = r; this.alt = alt; this.node = node; this.actionToken = actionToken; this.errMgr = g.tool.errMgr; }
public virtual bool ResolvesToAttributeDict(string x, ActionAST node) { if (ResolvesToToken(x, node)) return true; if (ruleRefs.ContainsKey(x) && ruleRefs[x] != null) return true; // rule ref in this alt? LabelElementPair anyLabelDef = GetAnyLabelDef(x); if (anyLabelDef != null && anyLabelDef.type == LabelType.RULE_LABEL) return true; return false; }
public virtual bool ResolvesToToken(string x, ActionAST node) { if (tokenRefs.ContainsKey(x) && tokenRefs[x] != null) return true; LabelElementPair anyLabelDef = GetAnyLabelDef(x); if (anyLabelDef != null && anyLabelDef.type == LabelType.TOKEN_LABEL) return true; return false; }
public static bool ActionIsContextDependent(ActionAST actionAST) { ANTLRStringStream @in = new ANTLRStringStream(actionAST.Token.Text); @in.Line = actionAST.Token.Line; @in.CharPositionInLine = actionAST.Token.CharPositionInLine; var listener = new ContextDependentListener(); ActionSplitter splitter = new ActionSplitter(@in, listener); // forces eval, triggers listener methods splitter.GetActionTokens(); return listener.dependent; }
public Action(OutputModelFactory factory, ActionAST ast) : base(factory, ast) { RuleFunction rf = factory.GetCurrentRuleFunction(); if (ast != null) { chunks = ActionTranslator.TranslateAction(factory, rf, ast.Token, ast); } else { chunks = new List<ActionChunk>(); } //System.out.println("actions="+chunks); }
public override void DiscoverRule(RuleAST rule, GrammarAST ID, IList<GrammarAST> modifiers, ActionAST arg, ActionAST returns, GrammarAST thrws, GrammarAST options, ActionAST locals, IList<GrammarAST> actions, GrammarAST block) { int numAlts = block.ChildCount; Rule r; if (LeftRecursiveRuleAnalyzer.HasImmediateRecursiveRuleRefs(rule, ID.Text)) { r = new LeftRecursiveRule(g, ID.Text, rule); } else { r = new Rule(g, ID.Text, rule, numAlts); } rules[r.name] = r; if (arg != null) { r.args = ScopeParser.ParseTypedArgList(arg, arg.Text, g); r.args.type = AttributeDict.DictType.ARG; r.args.ast = arg; arg.resolver = r.alt[currentOuterAltNumber]; } if (returns != null) { r.retvals = ScopeParser.ParseTypedArgList(returns, returns.Text, g); r.retvals.type = AttributeDict.DictType.RET; r.retvals.ast = returns; } if (locals != null) { r.locals = ScopeParser.ParseTypedArgList(locals, locals.Text, g); r.locals.type = AttributeDict.DictType.LOCAL; r.locals.ast = locals; } foreach (GrammarAST a in actions) { // a = ^(AT ID ACTION) ActionAST action = (ActionAST)a.GetChild(1); r.namedActions[a.GetChild(0).Text] = action; action.resolver = r; } }
public Action(OutputModelFactory factory, StructDecl ctx, string action) : base(factory, null) { ActionAST ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action)); RuleFunction rf = factory.GetCurrentRuleFunction(); if (rf != null) { // we can translate ast.resolver = rf.rule; chunks = ActionTranslator.TranslateActionChunk(factory, rf, action, ast); } else { chunks = new List<ActionChunk>(); chunks.Add(new ActionText(ctx, action)); } }
public static IList<ActionChunk> TranslateAction(OutputModelFactory factory, RuleFunction rf, IToken tokenWithinAction, ActionAST node) { string action = tokenWithinAction.Text; if (action != null && action.Length > 0 && action[0] == '{') { int firstCurly = action.IndexOf('{'); int lastCurly = action.LastIndexOf('}'); if (firstCurly >= 0 && lastCurly >= 0) { action = action.Substring(firstCurly + 1, lastCurly - firstCurly - 1); // trim {...} } } return TranslateActionChunk(factory, rf, action, node); }
public override void RuleRef(GrammarAST @ref, ActionAST arg) { CheckInvalidRuleRef(@ref.Token); }
public virtual bool ResolvesToAttributeDict(string x, ActionAST node) { if (ResolvesToToken(x, node)) return true; return false; }
public virtual bool ResolvesToToken(string x, ActionAST node) { LabelElementPair anyLabelDef = GetAnyLabelDef(x); if (anyLabelDef != null && anyLabelDef.type == LabelType.TOKEN_LABEL) return true; return false; }
// ACTIONS public virtual IList<SrcOp> Action(ActionAST ast) { return null; }
public override void RuleRef(GrammarAST @ref, ActionAST arg) { // if ( inContext("DOT ...") ) qualifiedRulerefs.add((GrammarAST)ref.getParent()); rulerefs.Add(@ref); if (currentRule != null) { currentRule.alt[currentOuterAltNumber].ruleRefs.Map(@ref.Text, @ref); } }
public override void RuleCatch(GrammarAST arg, ActionAST action) { GrammarAST catchme = (GrammarAST)action.Parent; currentRule.exceptions.Add(catchme); action.resolver = currentRule; }
public override Handle Action(ActionAST action) { int ruleIndex = currentRule.index; int actionIndex = g.lexerActions[action]; LexerCustomAction lexerAction = new LexerCustomAction(ruleIndex, actionIndex); return Action(action, lexerAction); }
public IList <IToken> chunks; // useful for ANTLR IDE developers public ActionAST(ActionAST node) : base(node) { this.resolver = node.resolver; this.chunks = node.chunks; }
public override void RuleRef(GrammarAST @ref, ActionAST arg) { if (@ref is GrammarASTWithOptions) { GrammarASTWithOptions grammarASTWithOptions = (GrammarASTWithOptions)@ref; if (bool.Parse(grammarASTWithOptions.GetOptionString(LeftFactoringRuleTransformer.SUPPRESS_ACCESSOR) ?? "false")) { return; } } frequencies.Peek().Add(RuleFunction.GetLabelName(grammar, @ref)); minFrequencies.Peek().Add(RuleFunction.GetLabelName(grammar, @ref)); }
public override void ActionInAlt(ActionAST action) { if (inFragmentRule) { string fileName = action.Token.InputStream.SourceName; string ruleName = currentRuleName; g.tool.errMgr.GrammarError(ErrorType.FRAGMENT_ACTION_IGNORED, fileName, action.Token, ruleName); } }
public override Handle Action(string action) { if (string.IsNullOrWhiteSpace(action)) { ATNState left = NewState(null); ATNState right = NewState(null); Epsilon(left, right); return new Handle(left, right); } // define action AST for this rule as if we had found in grammar ActionAST ast = new ActionAST(new CommonToken(ANTLRParser.ACTION, action)); currentRule.DefineActionInAlt(currentOuterAlt, ast); return Action(ast); }
public virtual void DefineActionInAlt(int currentAlt, ActionAST actionAST) { actions.Add(actionAST); alt[currentAlt].actions.Add(actionAST); if (g.IsLexer()) { DefineLexerAction(actionAST); } }
public override void FinallyAction(ActionAST action) { currentRule.finallyAction = action; action.resolver = currentRule; }
/** Lexer actions are numbered across rules 0..n-1 */ public virtual void DefineLexerAction(ActionAST actionAST) { actionIndex = g.lexerActions.Count; if (!g.lexerActions.ContainsKey(actionAST)) { g.lexerActions[actionAST] = actionIndex; } }
public override void GlobalNamedAction(GrammarAST scope, GrammarAST ID, ActionAST action) { namedActions.Add((GrammarAST)ID.Parent); action.resolver = g; }
/** $x Attribute: rule arguments, return values, predefined rule prop. */ public virtual Attribute ResolveToAttribute(string x, ActionAST node) { if (args != null) { Attribute a = args.Get(x); if (a != null) return a; } if (retvals != null) { Attribute a = retvals.Get(x); if (a != null) return a; } if (locals != null) { Attribute a = locals.Get(x); if (a != null) return a; } AttributeDict properties = GetPredefinedScope(LabelType.RULE_LABEL); return properties.Get(x); }
public override void ActionInAlt(ActionAST action) { currentRule.DefineActionInAlt(currentOuterAltNumber, action); action.resolver = currentRule.alt[currentOuterAltNumber]; }
/** $x.y Attribute: x is surrounding rule, label ref (in any alts) */ public virtual Attribute ResolveToAttribute(string x, string y, ActionAST node) { LabelElementPair anyLabelDef = GetAnyLabelDef(x); if (anyLabelDef != null) { if (anyLabelDef.type == LabelType.RULE_LABEL) { return g.GetRule(anyLabelDef.element.Text).ResolveRetvalOrProperty(y); } else { AttributeDict scope = GetPredefinedScope(anyLabelDef.type); if (scope == null) { return null; } return scope.Get(y); } } return null; }
public virtual IList<SrcOp> Sempred(ActionAST ast) { return null; }
public virtual bool ResolvesToListLabel(string x, ActionAST node) { LabelElementPair anyLabelDef = GetAnyLabelDef(x); return anyLabelDef != null && (anyLabelDef.type == LabelType.RULE_LIST_LABEL || anyLabelDef.type == LabelType.TOKEN_LIST_LABEL); }