public InvokeRule(ParserFactory factory, GrammarAST ast, GrammarAST labelAST) : base(factory, ast) { if (ast.atnState != null) { RuleTransition ruleTrans = (RuleTransition)ast.atnState.Transition(0); stateNumber = ast.atnState.stateNumber; } this.name = ast.Text; Rule r = factory.GetGrammar().GetRule(name); ctxName = factory.GetTarget().GetRuleFunctionContextStructName(r); // TODO: move to factory RuleFunction rf = factory.GetCurrentRuleFunction(); if (labelAST != null) { // for x=r, define <rule-context-type> x and list_x string label = labelAST.Text; if (labelAST.Parent.Type == ANTLRParser.PLUS_ASSIGN) { factory.DefineImplicitLabel(ast, this); string listLabel = factory.GetTarget().GetListLabel(label); RuleContextListDecl l = new RuleContextListDecl(factory, listLabel, ctxName); rf.AddContextDecl(ast.GetAltLabel(), l); } else { RuleContextDecl d = new RuleContextDecl(factory, label, ctxName); labels.Add(d); rf.AddContextDecl(ast.GetAltLabel(), d); } } ActionAST arg = (ActionAST)ast.GetFirstChildWithType(ANTLRParser.ARG_ACTION); if (arg != null) { argExprsChunks = ActionTranslator.TranslateAction(factory, rf, arg.Token, arg); } // If action refs rule as rulename not label, we need to define implicit label if (factory.GetCurrentOuterMostAlt().ruleRefsInActions.ContainsKey(ast.Text)) { string label = factory.GetTarget().GetImplicitRuleLabel(ast.Text); RuleContextDecl d = new RuleContextDecl(factory, label, ctxName); labels.Add(d); rf.AddContextDecl(ast.GetAltLabel(), d); } }
/** * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*))) * Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*))) */ public static bool HasImmediateRecursiveRuleRefs(GrammarAST t, string ruleName) { if (t == null) return false; GrammarAST blk = (GrammarAST)t.GetFirstChildWithType(BLOCK); if (blk == null) return false; int n = blk.Children.Count; for (int i = 0; i < n; i++) { GrammarAST alt = (GrammarAST)blk.Children[i]; ITree first = alt.GetChild(0); if (first == null) continue; if (first.Type == ELEMENT_OPTIONS) { first = alt.GetChild(1); if (first == null) { continue; } } if (first.Type == RULE_REF && first.Text.Equals(ruleName)) return true; ITree rref = first.GetChild(1); if (rref != null && rref.Type == RULE_REF && rref.Text.Equals(ruleName)) return true; } return false; }