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); }
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(); } }
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); } }
static void Main(string[] args) { List <string> options = new List <string>(); List <string> arguments = new List <string>(); string ast_output_file = null; CommandLine.Parser.Default.ParseArguments <Options>(args) .WithParsed <Options>(o => { arguments = o.CsharpFiles.ToList(); ast_output_file = o.AstOutFile; }) .WithNotParsed(a => { System.Console.Error.WriteLine(a); }); Runtime.Redirect r = new Runtime.Redirect(ast_output_file); foreach (var file_name in arguments) { var code_as_string = File.ReadAllText(file_name); var input = new AntlrInputStream(code_as_string); var lexer = new CSharpLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new CSharpParser(tokens); var listener = new ErrorListener <IToken>(); parser.AddErrorListener(listener); CSharpParser.Compilation_unitContext tree = parser.compilation_unit(); if (listener.had_error) { return; } var sb = new StringBuilder(); var ser = new Runtime.AstHelpers(); ser.ParenthesizedAST(sb, file_name, tree, tokens); System.Console.Error.WriteLine(sb.ToString()); } r.Dispose(); }
static void Try(string ffn) { var sourceCode = System.IO.File.ReadAllText(ffn); List <IToken> codeTokens = new List <IToken>(); List <IToken> commentTokens = new List <IToken>(); Lexer preprocessorLexer = new CSharpLexer(new AntlrInputStream(sourceCode)); // Collect all tokens with lexer (CSharpLexer.g4). var tokens = preprocessorLexer.GetAllTokens(); var directiveTokens = new List <IToken>(); var directiveTokenSource = new ListTokenSource(directiveTokens); var directiveTokenStream = new CommonTokenStream(directiveTokenSource, CSharpLexer.DIRECTIVE); CSharpPreprocessorParser preprocessorParser = new CSharpPreprocessorParser(directiveTokenStream); int index = 0; bool compiliedTokens = true; while (index < tokens.Count) { var token = tokens[index]; if (token.Type == CSharpLexer.SHARP) { directiveTokens.Clear(); int directiveTokenIndex = index + 1; // Collect all preprocessor directive tokens. while (directiveTokenIndex < tokens.Count && tokens[directiveTokenIndex].Type != CSharpLexer.Eof && tokens[directiveTokenIndex].Type != CSharpLexer.DIRECTIVE_NEW_LINE && tokens[directiveTokenIndex].Type != CSharpLexer.SHARP) { if (tokens[directiveTokenIndex].Channel == CSharpLexer.COMMENTS_CHANNEL) { commentTokens.Add(tokens[directiveTokenIndex]); } else if (tokens[directiveTokenIndex].Channel != Lexer.Hidden) { directiveTokens.Add(tokens[directiveTokenIndex]); } directiveTokenIndex++; } directiveTokenSource = new ListTokenSource(directiveTokens); directiveTokenStream = new CommonTokenStream(directiveTokenSource, CSharpLexer.DIRECTIVE); preprocessorParser.TokenStream = directiveTokenStream; preprocessorParser.Reset(); // Parse condition in preprocessor directive (based on CSharpPreprocessorParser.g4 grammar). CSharpPreprocessorParser.Preprocessor_directiveContext directive = preprocessorParser.preprocessor_directive(); // if true than next code is valid and not ignored. compiliedTokens = directive.value; var directiveStr = tokens[index + 1].Text.Trim(); if ("line".Equals(directiveStr) || "error".Equals(directiveStr) || "warning".Equals(directiveStr) || "define".Equals(directiveStr) || "endregion".Equals(directiveStr) || "endif".Equals(directiveStr) || "pragma".Equals(directiveStr)) { compiliedTokens = true; } string conditionalSymbol = null; if ("define".Equals(tokens[index + 1].Text)) { // add to the conditional symbols conditionalSymbol = tokens[index + 2].Text; preprocessorParser.ConditionalSymbols.Add(conditionalSymbol); } if ("undef".Equals(tokens[index + 1].Text)) { conditionalSymbol = tokens[index + 2].Text; preprocessorParser.ConditionalSymbols.Remove(conditionalSymbol); } index = directiveTokenIndex - 1; } else if (token.Channel == CSharpLexer.COMMENTS_CHANNEL) { commentTokens.Add(token); // Colect comment tokens (if required). } else if (token.Channel != Lexer.Hidden && token.Type != CSharpLexer.DIRECTIVE_NEW_LINE && compiliedTokens) { codeTokens.Add(token); // Collect code tokens. } index++; } // At second stage tokens parsed in usual way. var codeTokenSource = new ListTokenSource(codeTokens); var codeTokenStream = new CommonTokenStream(codeTokenSource); CSharpParser parser = new CSharpParser(codeTokenStream); // Parse syntax tree (CSharpParser.g4) var listener = new ErrorListener <IToken>(parser, preprocessorLexer, codeTokenStream); parser.AddErrorListener(listener); var tree = parser.compilation_unit(); if (listener.had_error) { System.Console.WriteLine("error in parse."); } else { System.Console.WriteLine("parse completed."); } System.Console.WriteLine(codeTokenStream.OutputTokens(preprocessorLexer)); System.Console.WriteLine(tree.OutputTree(codeTokenStream, preprocessorLexer)); }