Example #1
0
        public static IEnumerable <ClassInfo> OuterClassInfosFromCSharpSource(
            string source, string filePath)
        {
            try
            {
                var codeArray   = source.ToCharArray();
                var inputStream = new AntlrInputStream(codeArray, codeArray.Length);

                var lexer                   = new CSharpLexer(inputStream);
                var commonTokenStream       = new CommonTokenStream(lexer);
                var compilationUnitListener = new CompilationUnitListener(filePath);


                var parser = new CSharpParser(commonTokenStream);
                parser.RemoveErrorListeners();
                parser.AddErrorListener(new ErrorListener());
                parser.compilation_unit().EnterRule(compilationUnitListener);

                return(compilationUnitListener.OuterClassInfos);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return(null);
        }
Example #2
0
        static void Main(string[] args)
        {
            using (StreamReader fileStream = new StreamReader("Program.cs"))
            {
                AntlrInputStream inputStream = new AntlrInputStream(fileStream);

                CSharpLexer       lexer             = new CSharpLexer(inputStream);
                CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
                CSharpParser      parser            = new CSharpParser(commonTokenStream);

                parser.RemoveErrorListeners();
                parser.AddErrorListener(new ErrorListener()); // add ours

                var compilationUnit = parser.compilation_unit();
            }
        }
Example #3
0
        static void HandleFileCs(TestListener listener, string filePath)
        {
            FileInfo info = new FileInfo(filePath);

            if (info.Extension == ".cs")
            {
                AntlrFileStream   stream = new AntlrFileStream(filePath);
                CSharpLexer       lexer  = new CSharpLexer(stream);
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                CSharpParser      parser = new CSharpParser(tokens);
                parser.RemoveErrorListeners();
                parser.AddErrorListener(new CustomError());
                CSharpParser.Compilation_unitContext startContext = parser.compilation_unit();
                ParseTreeWalker walker = new ParseTreeWalker();
                walker.Walk(listener, startContext);
            }
        }