public grammar_Adaptor(ANTLRParser outer) { _outer = outer; }
public virtual void ParseAndBuildAST( TextReader r ) { // BUILD AST FROM GRAMMAR ANTLRLexer lexer = new ANTLRLexer( new Antlr.Runtime.ANTLRReaderStream( r ) ); lexer.Filename = this.FileName; // use the rewrite engine because we want to buffer up all tokens // in case they have a merged lexer/parser, send lexer rules to // new grammar. //lexer.setTokenObjectClass( "antlr.TokenWithIndex" ); tokenBuffer = new Antlr.Runtime.CommonTokenStream( lexer ); //tokenBuffer = new TokenStreamRewriteEngine( lexer ); //tokenBuffer.Discard( ANTLRParser.WS, ANTLRParser.ML_COMMENT, ANTLRParser.COMMENT, ANTLRParser.SL_COMMENT ); //tokenBuffer.discard( ANTLRParser.WS ); //tokenBuffer.discard( ANTLRParser.ML_COMMENT ); //tokenBuffer.discard( ANTLRParser.COMMENT ); //tokenBuffer.discard( ANTLRParser.SL_COMMENT ); ANTLRParser parser = new ANTLRParser( tokenBuffer ); parser.FileName = this.FileName; Antlr.Runtime.IAstRuleReturnScope<GrammarAST> result = null; try { result = parser.grammar_( this ); } //catch ( TokenStreamException tse ) //{ // ErrorManager.internalError( "unexpected stream error from parsing " + fileName, tse ); //} catch ( RecognitionException re ) { ErrorManager.InternalError( "unexpected parser recognition error from " + fileName, re ); } DealWithTreeFilterMode(); // tree grammar and filter=true? if ( lexer.hasASTOperator && !BuildAST ) { object value = GetOption( "output" ); if ( value == null ) { ErrorManager.GrammarWarning( ErrorManager.MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION, this, null ); SetOption( "output", "AST", null ); } else { ErrorManager.GrammarError( ErrorManager.MSG_AST_OP_WITH_NON_AST_OUTPUT_OPTION, this, null, value ); } } grammarTree = result.Tree; if (grammarTree != null && AntlrTool.internalOption_PrintGrammarTree) Console.WriteLine("grammar tree: " + grammarTree.ToStringTree()); grammarTree.SetUnknownTokenBoundaries(); FileName = lexer.Filename; // the lexer #src might change name if ( grammarTree.FindFirstType( ANTLRParser.RULE ) == null ) { ErrorManager.Error( ErrorManager.MSG_NO_RULES, FileName ); } }
public GrammarAST ParseArtificialRule(string ruleText) { ANTLRLexer lexer = new ANTLRLexer(new Antlr.Runtime.ANTLRStringStream(ruleText)); TokenStreamRewriteEngine tokbuf = new TokenStreamRewriteEngine(lexer); tokbuf.Discard(ANTLRParser.WS, ANTLRParser.ML_COMMENT, ANTLRParser.COMMENT, ANTLRParser.SL_COMMENT); ANTLRParser parser = new ANTLRParser(new Antlr.Runtime.CommonTokenStream(tokbuf)); parser.Grammar = this; parser.GrammarType = this.type; try { Antlr.Runtime.IAstRuleReturnScope<GrammarAST> result = parser.rule(); return result.Tree; } catch (Exception e) { ErrorManager.Error(ErrorManager.MSG_ERROR_CREATING_ARTIFICIAL_RULE, e); return null; } }
/** Given a template constructor action like %foo(a={...}) in * an action, translate it to the appropriate template constructor * from the templateLib. This translates a *piece* of the action. */ public virtual StringTemplate TranslateTemplateConstructor( string ruleName, int outerAltNum, IToken actionToken, string templateActionText ) { GrammarAST rewriteTree = null; { // first, parse with antlr.g //[email protected]("translate template: "+templateActionText); ANTLRLexer lexer = new ANTLRLexer( new Antlr.Runtime.ANTLRStringStream( templateActionText ) ); lexer.Filename = grammar.FileName; //lexer.setTokenObjectClass( "antlr.TokenWithIndex" ); //TokenStreamRewriteEngine tokenBuffer = new TokenStreamRewriteEngine( lexer ); //tokenBuffer.discard( ANTLRParser.WS ); //tokenBuffer.discard( ANTLRParser.ML_COMMENT ); //tokenBuffer.discard( ANTLRParser.COMMENT ); //tokenBuffer.discard( ANTLRParser.SL_COMMENT ); ANTLRParser parser = new ANTLRParser( new Antlr.Runtime.CommonTokenStream( lexer ) ); parser.FileName = grammar.FileName; //parser.setASTNodeClass( "org.antlr.tool.GrammarAST" ); try { ANTLRParser.rewrite_template_return result = parser.rewrite_template(); rewriteTree = (GrammarAST)result.Tree; } catch ( RecognitionException /*re*/ ) { ErrorManager.GrammarError( ErrorManager.MSG_INVALID_TEMPLATE_ACTION, grammar, actionToken, templateActionText ); } catch ( Exception tse ) { ErrorManager.InternalError( "can't parse template action", tse ); } } { // then translate via codegen.g CodeGenTreeWalker gen = new CodeGenTreeWalker( new Antlr.Runtime.Tree.CommonTreeNodeStream( rewriteTree ) ); gen.Init( grammar ); gen.currentRuleName = ruleName; gen.outerAltNum = outerAltNum; StringTemplate st = null; try { st = gen.rewrite_template(); } catch ( RecognitionException re ) { ErrorManager.Error( ErrorManager.MSG_BAD_AST_STRUCTURE, re ); } return st; } }
protected virtual IList<GrammarAST> GetArtificialRulesForSyntacticPredicates(ANTLRParser parser, IDictionary<string, GrammarAST> nameToSynpredASTMap ) { IList<GrammarAST> rules = new List<GrammarAST>(); if ( nameToSynpredASTMap == null ) { return rules; } bool isLexer = grammarTree.Type == ANTLRParser.LEXER_GRAMMAR; foreach ( var synpred in nameToSynpredASTMap ) { string synpredName = synpred.Key; GrammarAST fragmentAST = (GrammarAST)synpred.Value; GrammarAST ruleAST = parser.CreateSimpleRuleAST( synpredName, fragmentAST, isLexer ); rules.Add( ruleAST ); } return rules; }
public virtual void ParseAndBuildAST( TextReader r ) { // BUILD AST FROM GRAMMAR ANTLRLexer lexer = new ANTLRLexer( new Antlr.Runtime.ANTLRReaderStream( r ) ); lexer.Filename = this.FileName; // use the rewrite engine because we want to buffer up all tokens // in case they have a merged lexer/parser, send lexer rules to // new grammar. //lexer.setTokenObjectClass( "antlr.TokenWithIndex" ); tokenBuffer = new Antlr.Runtime.CommonTokenStream( lexer ); //tokenBuffer = new TokenStreamRewriteEngine( lexer ); //tokenBuffer.Discard( ANTLRParser.WS, ANTLRParser.ML_COMMENT, ANTLRParser.COMMENT, ANTLRParser.SL_COMMENT ); //tokenBuffer.discard( ANTLRParser.WS ); //tokenBuffer.discard( ANTLRParser.ML_COMMENT ); //tokenBuffer.discard( ANTLRParser.COMMENT ); //tokenBuffer.discard( ANTLRParser.SL_COMMENT ); ANTLRParser parser = new ANTLRParser( tokenBuffer ); parser.FileName = this.FileName; ANTLRParser.grammar__return result = null; try { result = parser.grammar_( this ); } //catch ( TokenStreamException tse ) //{ // ErrorManager.internalError( "unexpected stream error from parsing " + fileName, tse ); //} catch ( RecognitionException re ) { ErrorManager.InternalError( "unexpected parser recognition error from " + fileName, re ); } DealWithTreeFilterMode(); // tree grammar and filter=true? if ( lexer.hasASTOperator && !BuildAST ) { object value = GetOption( "output" ); if ( value == null ) { ErrorManager.GrammarWarning( ErrorManager.MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION, this, null ); SetOption( "output", "AST", null ); } else { ErrorManager.GrammarError( ErrorManager.MSG_AST_OP_WITH_NON_AST_OUTPUT_OPTION, this, null, value ); } } //grammarTree = (GrammarAST)parser.getAST(); grammarTree = (GrammarAST)result.tree; FileName = lexer.Filename; // the lexer #src might change name if ( grammarTree == null || grammarTree.FindFirstType( ANTLRParser.RULE ) == null ) { ErrorManager.Error( ErrorManager.MSG_NO_RULES, FileName ); return; } // Get syn pred rules and add to existing tree IList<GrammarAST> synpredRules = GetArtificialRulesForSyntacticPredicates( parser, nameToSynpredASTMap ); for ( int i = 0; i < synpredRules.Count; i++ ) { GrammarAST rAST = (GrammarAST)synpredRules[i]; grammarTree.AddChild( rAST ); } }
/** Parse a rule we add artificially that is a list of the other lexer * rules like this: "Tokens : ID | INT | SEMI ;" nextToken() will invoke * this to set the current token. Add char literals before * the rule references. * * If in filter mode, we want every alt to backtrack and we need to * do k=1 to force the "first token def wins" rule. Otherwise, the * longest-match rule comes into play with LL(*). * * The ANTLRParser antlr.g file now invokes this when parsing a lexer * grammar, which I think is proper even though it peeks at the info * that later phases will (re)compute. It gets a list of lexer rules * and builds a string representing the rule; then it creates a parser * and adds the resulting tree to the grammar's tree. */ public GrammarAST AddArtificialMatchTokensRule( GrammarAST grammarAST, IList<string> ruleNames, IList<string> delegateNames, bool filterMode ) { StringTemplate matchTokenRuleST = null; if ( filterMode ) { matchTokenRuleST = new StringTemplate( ArtificialTokensRuleName + " options {k=1; backtrack=true;} : <rules; separator=\"|\">;", typeof( AngleBracketTemplateLexer ) ); } else { matchTokenRuleST = new StringTemplate( ArtificialTokensRuleName + " : <rules; separator=\"|\">;", typeof( AngleBracketTemplateLexer ) ); } // Now add token rule references for ( int i = 0; i < ruleNames.Count; i++ ) { string rname = (string)ruleNames[i]; matchTokenRuleST.SetAttribute( "rules", rname ); } for ( int i = 0; i < delegateNames.Count; i++ ) { string dname = (string)delegateNames[i]; matchTokenRuleST.SetAttribute( "rules", dname + ".Tokens" ); } //[email protected]("tokens rule: "+matchTokenRuleST.toString()); //ANTLRLexer lexer = new ANTLRLexer( new StringReader( matchTokenRuleST.toString() ) ); //lexer.setTokenObjectClass( "antlr.TokenWithIndex" ); //TokenStreamRewriteEngine tokbuf = // new TokenStreamRewriteEngine( lexer ); //tokbuf.discard( ANTLRParser.WS ); //tokbuf.discard( ANTLRParser.ML_COMMENT ); //tokbuf.discard( ANTLRParser.COMMENT ); //tokbuf.discard( ANTLRParser.SL_COMMENT ); //ANTLRParser parser = new ANTLRParser( tokbuf ); ANTLRLexer lexer = new ANTLRLexer( new Antlr.Runtime.ANTLRStringStream( matchTokenRuleST.ToString() ) ); TokenStreamRewriteEngine tokbuf = new TokenStreamRewriteEngine( lexer ); tokbuf.Discard( ANTLRParser.WS, ANTLRParser.ML_COMMENT, ANTLRParser.COMMENT, ANTLRParser.SL_COMMENT ); ANTLRParser parser = new ANTLRParser( new Antlr.Runtime.CommonTokenStream( tokbuf ) ); parser.Grammar = this; parser.GrammarType = GrammarType.Lexer; ANTLRParser.rule_return result = null; try { result = parser.rule(); if ( Tool.internalOption_PrintGrammarTree ) { Console.Out.WriteLine( "Tokens rule: " + ( (ITree)result.Tree ).ToStringTree() ); } GrammarAST p = grammarAST; while ( p.Type != ANTLRParser.LEXER_GRAMMAR ) { p = (GrammarAST)p.getNextSibling(); } p.AddChild( (Antlr.Runtime.Tree.ITree)result.Tree ); } catch ( Exception e ) { ErrorManager.Error( ErrorManager.MSG_ERROR_CREATING_ARTIFICIAL_RULE, e ); } return (GrammarAST)result.Tree; }
public grammar_Adaptor( ANTLRParser outer ) { _outer = outer; }