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.CFiles.ToList(); options = o.CompilerOptions.ToList(); ast_output_file = o.AstOutFile; }) .WithNotParsed(a => { System.Console.Error.WriteLine(a); }); Runtime.Redirect r = null; if (ast_output_file != null) { r = new Runtime.Redirect(ast_output_file); } foreach (var filename in arguments) { var include_dirs = CPP.tokenFactory.IncludeDirs; CPP.tokenFactory.pushFilename(filename); var ts = CPP.load(filename, include_dirs); System.Console.Error.WriteLine(String.Join("\n", ts.Select(x => x.ToString()))); PreprocessedCharStream cinput = new PreprocessedCharStream(ts); var clexer = new gcpp.CPP14Lexer(cinput); // force creation of CPPTokensm set file,line clexer.TokenFactory = new CTokenFactory(cinput); CommonTokenStream ctokens = new CommonTokenStream(clexer); var cparser = new gcpp.CPP14Parser(ctokens); cparser.RemoveErrorListeners(); cparser.AddErrorListener(new CErrorListener()); var t = cparser.translationunit(); var sb = new StringBuilder(); var ser = new Runtime.AstHelpers(); ser.ParenthesizedAST(sb, filename, t, ctokens); System.Console.WriteLine(sb.ToString()); } if (r != null) { r.Dispose(); } }
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(); }