/**
         * <pre>
         * (RULE e int _p (returns int v)
         *  (BLOCK
         *    (ALT
         *      (BLOCK
         *          (ALT INT {$v = $INT.int;})
         *          (ALT '(' (= x e) ')' {$v = $x.v;})
         *          (ALT ID))
         *      (* (BLOCK
         *			(OPTIONS ...)
         *          (ALT {7 &gt;= $_p}? '*' (= b e) {$v = $a.v * $b.v;})
         *          (ALT {6 &gt;= $_p}? '+' (= b e) {$v = $a.v + $b.v;})
         *          (ALT {3 &gt;= $_p}? '++') (ALT {2 &gt;= $_p}? '--'))))))
         * </pre>
         */
        public virtual void SetAltASTPointers(LeftRecursiveRule r, RuleAST t)
        {
            //System.Console.WriteLine("RULE: " + t.ToStringTree());
            BlockAST ruleBlk    = (BlockAST)t.GetFirstChildWithType(ANTLRParser.BLOCK);
            AltAST   mainAlt    = (AltAST)ruleBlk.GetChild(0);
            BlockAST primaryBlk = (BlockAST)mainAlt.GetChild(0);
            BlockAST opsBlk     = (BlockAST)mainAlt.GetChild(1).GetChild(0); // (* BLOCK ...)

            for (int i = 0; i < r.recPrimaryAlts.Count; i++)
            {
                LeftRecursiveRuleAltInfo altInfo = r.recPrimaryAlts[i];
                altInfo.altAST = (AltAST)primaryBlk.GetChild(i);
                altInfo.altAST.leftRecursiveAltInfo         = altInfo;
                altInfo.originalAltAST.leftRecursiveAltInfo = altInfo;
                //altInfo.originalAltAST.Parent = altInfo.altAST.Parent;
                //System.Console.WriteLine(altInfo.altAST.ToStringTree());
            }
            for (int i = 0; i < r.recOpAlts.Count; i++)
            {
                LeftRecursiveRuleAltInfo altInfo = r.recOpAlts.GetElement(i);
                altInfo.altAST = (AltAST)opsBlk.GetChild(i);
                altInfo.altAST.leftRecursiveAltInfo         = altInfo;
                altInfo.originalAltAST.leftRecursiveAltInfo = altInfo;
                //altInfo.originalAltAST.Parent = altInfo.altAST.Parent;
                //System.Console.WriteLine(altInfo.altAST.ToStringTree());
            }
        }
Ejemplo n.º 2
0
 /**
  * {@code (BLOCK (ALT .))} or {@code (BLOCK (ALT 'a') (ALT .))}.
  */
 public static bool BlockHasWildcardAlt([NotNull] GrammarAST block)
 {
     foreach (object alt in block.Children)
     {
         if (!(alt is AltAST))
         {
             continue;
         }
         AltAST altAST = (AltAST)alt;
         if (altAST.ChildCount == 1 || (altAST.ChildCount == 2 && altAST.GetChild(0).Type == ANTLRParser.ELEMENT_OPTIONS))
         {
             ITree e = altAST.GetChild(altAST.ChildCount - 1);
             if (e.Type == ANTLRParser.WILDCARD)
             {
                 return(true);
             }
         }
     }
     return(false);
 }