Inheritance: ANTLRParser
Example #1
0
 public virtual GrammarRootAST Parse(string fileName, ICharStream @in)
 {
     try
     {
         GrammarASTAdaptor adaptor = new GrammarASTAdaptor(@in);
         ToolANTLRLexer lexer = new ToolANTLRLexer(@in, this);
         CommonTokenStream tokens = new CommonTokenStream(lexer);
         lexer.tokens = tokens;
         ToolANTLRParser p = new ToolANTLRParser(tokens, this);
         p.TreeAdaptor = adaptor;
         try
         {
             var r = p.grammarSpec();
             GrammarAST root = (GrammarAST)r.Tree;
             if (root is GrammarRootAST)
             {
                 ((GrammarRootAST)root).hasErrors = lexer.NumberOfSyntaxErrors > 0 || p.NumberOfSyntaxErrors > 0;
                 Debug.Assert(((GrammarRootAST)root).tokenStream == tokens);
                 if (grammarOptions != null)
                 {
                     ((GrammarRootAST)root).cmdLineOptions = grammarOptions;
                 }
                 return ((GrammarRootAST)root);
             }
         }
         catch (v3TreeGrammarException e)
         {
             errMgr.GrammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
         }
         return null;
     }
     catch (RecognitionException)
     {
         // TODO: do we gen errors now?
         ErrorManager.InternalError("can't generate this message at moment; antlr recovers");
     }
     return null;
 }
 public virtual RuleAST ParseArtificialRule(Grammar g, string ruleText)
 {
     ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
     GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.CharStream);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     lexer.tokens = tokens;
     ToolANTLRParser p = new ToolANTLRParser(tokens, tool);
     p.TreeAdaptor = adaptor;
     IToken ruleStart = null;
     try
     {
         Antlr.Runtime.AstParserRuleReturnScope<GrammarAST, IToken> r = p.rule();
         RuleAST tree = (RuleAST)r.Tree;
         ruleStart = r.Start;
         GrammarTransformPipeline.SetGrammarPtr(g, tree);
         GrammarTransformPipeline.AugmentTokensWithOriginalPosition(g, tree);
         return tree;
     }
     catch (Exception e)
     {
         tool.errMgr.ToolError(ErrorType.INTERNAL_ERROR,
                               e,
                               ruleStart,
                               "error parsing rule created during left-recursion detection: " + ruleText);
     }
     return null;
 }