Exemple #1
0
        /** For references to tokens rather than by label such as $ID, we
         *  need to get the existing label for the ID ref or create a new
         *  one.
         */
        public virtual string GetElementLabel(string refdSymbol,
                                              int outerAltNum,
                                              CodeGenerator generator)
        {
            GrammarAST uniqueRefAST;

            if (Grammar.type != GrammarType.Lexer &&
                Rule.GetRuleType(refdSymbol) == RuleType.Lexer)
            {
                // symbol is a token
                IList <GrammarAST> tokenRefs = GetTokenRefsInAlt(refdSymbol, outerAltNum);
                uniqueRefAST = tokenRefs[0];
            }
            else
            {
                // symbol is a rule
                IList <GrammarAST> ruleRefs = GetRuleRefsInAlt(refdSymbol, outerAltNum);
                uniqueRefAST = ruleRefs[0];
            }
            if (uniqueRefAST.code == null)
            {
                // no code?  must not have gen'd yet; forward ref
                return(null);
            }
            string labelName         = null;
            string existingLabelName =
                (string)uniqueRefAST.code.GetAttribute("label");

            // reuse any label or list label if it exists
            if (existingLabelName != null)
            {
                labelName = existingLabelName;
            }
            else
            {
                // else create new label
                labelName = generator.CreateUniqueLabel(refdSymbol);
                CommonToken label = new CommonToken(ANTLRParser.ID, labelName);
                if (Grammar.type != GrammarType.Lexer &&
                    Rule.GetRuleType(refdSymbol) == Tool.RuleType.Lexer)
                {
                    Grammar.DefineTokenRefLabel(Name, label, uniqueRefAST);
                }
                else
                {
                    Grammar.DefineRuleRefLabel(Name, label, uniqueRefAST);
                }
                uniqueRefAST.code.SetAttribute("label", labelName);
            }
            return(labelName);
        }
Exemple #2
0
 /** For references to tokens rather than by label such as $ID, we
  *  need to get the existing label for the ID ref or create a new
  *  one.
  */
 public virtual string GetElementLabel( string refdSymbol,
                               int outerAltNum,
                               CodeGenerator generator )
 {
     GrammarAST uniqueRefAST;
     if ( grammar.type != GrammarType.Lexer &&
          char.IsUpper( refdSymbol[0] ) )
     {
         // symbol is a token
         IList tokenRefs = GetTokenRefsInAlt( refdSymbol, outerAltNum );
         uniqueRefAST = (GrammarAST)tokenRefs[0];
     }
     else
     {
         // symbol is a rule
         IList ruleRefs = GetRuleRefsInAlt( refdSymbol, outerAltNum );
         uniqueRefAST = (GrammarAST)ruleRefs[0];
     }
     if ( uniqueRefAST.code == null )
     {
         // no code?  must not have gen'd yet; forward ref
         return null;
     }
     string labelName = null;
     string existingLabelName =
         (string)uniqueRefAST.code.GetAttribute( "label" );
     // reuse any label or list label if it exists
     if ( existingLabelName != null )
     {
         labelName = existingLabelName;
     }
     else
     {
         // else create new label
         labelName = generator.CreateUniqueLabel( refdSymbol );
         CommonToken label = new CommonToken( ANTLRParser.ID, labelName );
         if ( grammar.type != GrammarType.Lexer &&
              char.IsUpper( refdSymbol[0] ) )
         {
             grammar.DefineTokenRefLabel( Name, label, uniqueRefAST );
         }
         else
         {
             grammar.DefineRuleRefLabel( Name, label, uniqueRefAST );
         }
         uniqueRefAST.code.SetAttribute( "label", labelName );
     }
     return labelName;
 }