1 using System; 2 using FileInfo = System.IO.FileInfo; 3 using Directory = System.IO.Directory; 4 using FileStream = System.IO.FileStream; 5 using FileMode = System.IO.FileMode; 6 using FileAccess = System.IO.FileAccess; 7 using Stream = System.IO.Stream; 8 using StreamReader = System.IO.StreamReader; 9 10 using BaseAST = antlr.BaseAST; 11 using CommonAST = antlr.CommonAST; 12 using ASTFactory = antlr.ASTFactory; 13 using RecognitionException = antlr.RecognitionException; 14 using AST = antlr.collections.AST; 15 using ASTFrame = antlr.debug.misc.ASTFrame; 16 17 // bug(?) in DotGNU 0.6 - "using antlr" will workaround the problem. 18 #if __CSCC__ 19 using antlr; 20 #endif 21 22 class AppMain 23 { 24 25 internal static bool showTree = false; 26 Main(string[] args)27 public static void Main(string[] args) 28 { 29 // Use a try/catch block for parser exceptions 30 try 31 { 32 // if we have at least one command-line argument 33 if (args.Length > 0) 34 { 35 Console.Error.WriteLine("Parsing..."); 36 // for each directory/file specified on the command line 37 for (int i = 0; i < args.Length; i++) 38 { 39 if (args[i].Equals("-showtree")) 40 { 41 showTree = true; 42 } 43 else 44 { 45 doFile(new FileInfo(args[i])); // parse it 46 } 47 } 48 } 49 else 50 Console.Error.WriteLine("Usage: java Main [-showtree] " + "<directory or file name>"); 51 } 52 catch (System.Exception e) 53 { 54 Console.Error.WriteLine("exception: " + e); 55 Console.Error.WriteLine(e.StackTrace); // so we can get stack trace 56 } 57 /* 58 finally 59 { 60 Console.ReadLine(); 61 } 62 */ 63 } 64 65 66 // This method decides what action to take based on the type of 67 // file we are looking at doFile(FileInfo f)68 public static void doFile(FileInfo f) 69 { 70 // If this is a directory, walk each file/dir in that directory 71 if (Directory.Exists(f.FullName)) 72 { 73 string[] files = Directory.GetFileSystemEntries(f.FullName); 74 for (int i = 0; i < files.Length; i++) 75 doFile(new FileInfo(f.FullName + "\\" + files[i])); 76 } 77 else if ((f.Name.Length > 5) && f.Name.Substring(f.Name.Length - 5).Equals(".java")) 78 { 79 Console.Error.WriteLine(" " + f.FullName); 80 parseFile(f.Name, new FileStream(f.FullName, FileMode.Open, FileAccess.Read)); 81 } 82 } 83 84 // Here's where we do the real work... parseFile(string f, Stream s)85 public static void parseFile(string f, Stream s) 86 { 87 try 88 { 89 // Create a scanner that reads from the input stream passed to us 90 JavaLexer lexer = new JavaLexer(new StreamReader(s)); 91 lexer.setFilename(f); 92 93 // Create a parser that reads from the scanner 94 JavaRecognizer parser = new JavaRecognizer(lexer); 95 parser.setFilename(f); 96 97 // start parsing at the compilationUnit rule 98 parser.compilationUnit(); 99 100 // do something with the tree 101 doTreeAction(f, parser.getAST(), parser.getTokenNames()); 102 } 103 catch (System.Exception e) 104 { 105 Console.Error.WriteLine("parser exception: " + e); 106 Console.Error.WriteLine(e.StackTrace); // so we can get stack trace 107 } 108 } 109 doTreeAction(string f, AST t, string[] tokenNames)110 public static void doTreeAction(string f, AST t, string[] tokenNames) 111 { 112 if (t == null) 113 return ; 114 if (showTree) 115 { 116 BaseAST.setVerboseStringConversion(true, tokenNames); 117 ASTFactory factory = new ASTFactory(); 118 AST r = factory.create(0, "AST ROOT"); 119 r.setFirstChild(t); 120 ASTFrame frame = new ASTFrame("Java AST", r); 121 frame.ShowDialog(); 122 //frame.Visible = true; 123 // System.out.println(t.toStringList()); 124 } 125 JavaTreeParser tparse = new JavaTreeParser(); 126 JavaRecognizer.initializeASTFactory(tparse.getASTFactory()); 127 try 128 { 129 tparse.compilationUnit(t); 130 // System.out.println("successful walk of result AST for "+f); 131 } 132 catch (RecognitionException e) 133 { 134 Console.Error.WriteLine(e.Message); 135 Console.Error.WriteLine(e.StackTrace); 136 } 137 138 } 139 }