Example #1
0
        public static bool BlockHasSynPred(GrammarAST blockAST)
        {
            GrammarAST c1 = blockAST.FindFirstType(ANTLRParser.SYN_SEMPRED);
            GrammarAST c2 = blockAST.FindFirstType(ANTLRParser.BACKTRACK_SEMPRED);
            if (c1 != null || c2 != null)
                return true;
            //		System.out.println(blockAST.enclosingRuleName+
            //						   " "+blockAST.getLine()+":"+blockAST.getColumn()+" no preds AST="+blockAST.toStringTree());

            return false;
        }
Example #2
0
        public static bool BlockHasSynPred(GrammarAST blockAST)
        {
            GrammarAST c1 = blockAST.FindFirstType(ANTLRParser.SYN_SEMPRED);
            GrammarAST c2 = blockAST.FindFirstType(ANTLRParser.BACKTRACK_SEMPRED);

            if (c1 != null || c2 != null)
            {
                return(true);
            }
            //		System.out.println(blockAST.enclosingRuleName+
            //						   " "+blockAST.getLine()+":"+blockAST.getColumn()+" no preds AST="+blockAST.toStringTree());

            return(false);
        }
Example #3
0
        public virtual bool HasRewrite(int i)
        {
            GrammarAST blk = Tree.FindFirstType(ANTLRParser.BLOCK);
            GrammarAST alt = blk.GetBlockAlt(i);
            GrammarAST rew = (GrammarAST)alt.Parent.GetChild(alt.ChildIndex + 1);

            if (rew != null && rew.Type == ANTLRParser.REWRITES)
            {
                return(true);
            }

            if (alt.FindFirstType(ANTLRParser.REWRITES) != null)
            {
                return(true);
            }

            return(false);
        }
Example #4
0
        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 );
            }
        }
Example #5
0
        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 );
            }
        }