Esempio n. 1
0
        // Here's where we do the real work...
        public static void  parseFile(string f, Stream s)
        {
            try
            {
                // Define a selector that can switch from the C# codelexer to the C# preprocessor lexer
                TokenStreamSelector selector = new TokenStreamSelector();

                TokenStream lexer;
                // Create a scanner that reads from the input stream passed to us
                if (useFlexLexer)
                {
                    CSharpFlexLexer flexLexer = new CSharpFlexLexer(new StreamReader(s));
                    flexLexer.SetFileInfo(new FileInfo(f));
                    lexer = flexLexer;
                }
                else
                {
                    CSharpLexer antlrLexer = new CSharpLexer(new StreamReader(s));

                    antlrLexer.Selector = selector;
                    antlrLexer.setFilename(f);
                    CSharpPreprocessorLexer preproLexer = new CSharpPreprocessorLexer(antlrLexer.getInputState());
                    preproLexer.Selector = selector;
                    CSharpPreprocessorHooverLexer hooverLexer = new CSharpPreprocessorHooverLexer(antlrLexer.getInputState());
                    hooverLexer.Selector = selector;

                    // use the special token object class
                    antlrLexer.setTokenCreator(new CustomHiddenStreamToken.CustomHiddenStreamTokenCreator());
                    antlrLexer.setTabSize(1);
                    preproLexer.setTokenCreator(new CustomHiddenStreamToken.CustomHiddenStreamTokenCreator());
                    preproLexer.setTabSize(1);
                    hooverLexer.setTokenCreator(new CustomHiddenStreamToken.CustomHiddenStreamTokenCreator());
                    hooverLexer.setTabSize(1);

                    // notify selector about various lexers; name them for convenient reference later
                    selector.addInputStream(antlrLexer, "codeLexer");
                    selector.addInputStream(preproLexer, "directivesLexer");
                    selector.addInputStream(hooverLexer, "hooverLexer");
                    selector.select("codeLexer");                     // start with main the CSharp code lexer
                    lexer = selector;
                }

                // create the stream filter; hide WS and SL_COMMENT
                TokenStreamHiddenTokenFilter filter;                 // = new TokenStreamHiddenTokenFilter(lexer);
                if (displayTokens)
                {
                    filter = new TokenStreamHiddenTokenFilter(new LoggingTokenStream(lexer));
                }
                else
                {
                    filter = new TokenStreamHiddenTokenFilter(lexer);
                }

                filter.hide(CSharpTokenTypes.WHITESPACE);
                filter.hide(CSharpTokenTypes.NEWLINE);
                filter.hide(CSharpTokenTypes.ML_COMMENT);
                filter.hide(CSharpTokenTypes.SL_COMMENT);

                // Create a parser that reads from the scanner
                CSharpParser parser = new CSharpParser(filter);

                // create trees that copy hidden tokens into tree also
                parser.setASTNodeClass(typeof(ASTNode).FullName);
                parser.setASTFactory(new ASTNodeFactory());
                CSharpParser.initializeASTFactory(parser.getASTFactory());
                parser.setFilename(f);
                //parser.getASTFactory().setASTNodeCreator(new ASTNode.ASTNodeCreator());

                // start parsing at the compilationUnit rule
                long startTime = DateTime.Now.Ticks;
                parser.compilationUnit();
                double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0;
                System.Console.Out.WriteLine("Parsed {0} in: {1} seconds.", f, elapsedTime);

                // do something with the tree
                Console.Error.WriteLine("       ... calling doTreeAction:");
                doTreeAction(f, (ASTNode)parser.getAST(), parser.getTokenNames());
            }
            catch (System.Exception e)
            {
                Console.Error.WriteLine("parser exception: " + e);
                Console.Error.WriteLine(e.StackTrace);                 // so we can get stack trace
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Parses the specified file
        /// </summary>
        /// <param name="f">File information.</param>
        /// <param name="s">Stream with the file code.</param>
        /// <param name="dynamic">Using "dynamic" to refer to a "dynamic var"</param>
        private void parseFile(FileInfo f, Stream s, bool dynamic)
        {
            ICSharpLexer  antlrLexer;
            ICSharpParser parser;
            TokenStreamHiddenTokenFilter filter;

            try {
                TokenStream lexer;

                // Create a scanner that reads from the input stream passed to us
                if (dynamic)
                {
                    antlrLexer = new CSharpLexerDynamic(new StreamReader(s));
                }
                else
                {
                    antlrLexer = new CSharpLexer(new StreamReader(s));
                }

                // Define a selector that can switch from the C# codelexer to the C# preprocessor lexer
                TokenStreamSelector selector = new TokenStreamSelector();
                antlrLexer.Selector = selector;
                antlrLexer.setFilename(f.Name);

                CSharpPreprocessorLexer preproLexer = new CSharpPreprocessorLexer(antlrLexer.getInputState());
                preproLexer.Selector = selector;
                CSharpPreprocessorHooverLexer hooverLexer = new CSharpPreprocessorHooverLexer(antlrLexer.getInputState());
                hooverLexer.Selector = selector;

                // use the special token object class
                antlrLexer.setTokenCreator(new CustomHiddenStreamToken.CustomHiddenStreamTokenCreator());
                antlrLexer.setTabSize(1);
                preproLexer.setTokenCreator(new CustomHiddenStreamToken.CustomHiddenStreamTokenCreator());
                preproLexer.setTabSize(1);
                hooverLexer.setTokenCreator(new CustomHiddenStreamToken.CustomHiddenStreamTokenCreator());
                hooverLexer.setTabSize(1);

                // notify selector about various lexers; name them for convenient reference later
                selector.addInputStream(antlrLexer, "codeLexer");
                selector.addInputStream(preproLexer, "directivesLexer");
                selector.addInputStream(hooverLexer, "hooverLexer");
                selector.select("codeLexer"); // start with main the CSharp code lexer
                lexer = selector;

                // create the stream filter; hide WS and SL_COMMENT
                filter = new TokenStreamHiddenTokenFilter(lexer);
                filter.hide(CSharpTokenTypes.WHITESPACE);
                filter.hide(CSharpTokenTypes.NEWLINE);
                filter.hide(CSharpTokenTypes.ML_COMMENT);
                filter.hide(CSharpTokenTypes.SL_COMMENT);

                //------------------------------------------------------------------

                // Create a parser that reads from the scanner
                if (dynamic)
                {
                    parser = new CSharpParserDynamic(filter);
                    DynVarManager.DynamicOption = true;
                }
                else
                {
                    parser = new CSharpParser(filter);
                }
                parser.setFilename(f.FullName);
                //parser.setFilename(f.Name);

                // Start parsing at the compilationUnit rule
                long startTime = DateTime.Now.Ticks;

                this.astList.Add(parser.compilationUnit());

    #if DEBUG
                double       elapsedTime   = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0;
                ConsoleColor previousColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.DarkGray;
                System.Console.Out.WriteLine("Parsed {0} in: {1} seconds.", f.Name, elapsedTime);
                Console.ForegroundColor = previousColor;
    #endif
            }
            catch (RecognitionException e)
            {
                ErrorManager.Instance.NotifyError(new ParserError(new Location(e.fileName, e.line, e.column), e.Message));
            }
        }