Exemple #1
0
	private static CSharpAST GetAST(string filename)
	{
		FileStream s = new FileStream(filename, FileMode.Open, FileAccess.Read);
		CSharpLexer lexer = new CSharpLexer(s);
		lexer.setFilename(filename);
		CSharpParser parser = new CSharpParser(lexer);
		parser.setFilename(filename);
		parser.compilation_unit();
		s.Close();
		CSharpAST antlrTree = (CSharpAST)(parser.getAST());
		antlrTree.FileName = filename;
		return antlrTree;
	}
Exemple #2
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
            }
        }