File: Main.cs

package info (click to toggle)
antlr 2.7.2-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,320 kB
  • ctags: 7,191
  • sloc: java: 36,740; cs: 7,160; cpp: 5,580; sh: 2,619; lisp: 1,969; makefile: 987; xml: 198; lex: 146; ansic: 67
file content (127 lines) | stat: -rwxr-xr-x 3,600 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using FileInfo		= System.IO.FileInfo;
using Directory		= System.IO.Directory;
using FileStream	= System.IO.FileStream;
using FileMode		= System.IO.FileMode;
using FileAccess	= System.IO.FileAccess;
using Stream		= System.IO.Stream;
using StreamReader	= System.IO.StreamReader;

using BaseAST				= antlr.BaseAST;
using CommonAST				= antlr.CommonAST;
using ASTFactory			= antlr.ASTFactory;
using RecognitionException	= antlr.RecognitionException;
using AST					= antlr.collections.AST;
using ASTFrame				= antlr.debug.misc.ASTFrame;

class AppMain
{
	
	internal static bool showTree = false;

	public static void  Main(string[] args)
	{
		// Use a try/catch block for parser exceptions
		try
		{
			// if we have at least one command-line argument
			if (args.Length > 0)
			{
				Console.Error.WriteLine("Parsing...");				
				// for each directory/file specified on the command line
				 for (int i = 0; i < args.Length; i++)
				{
					if (args[i].Equals("-showtree"))
					{
						showTree = true;
					}
					else
					{
						doFile(new FileInfo(args[i])); // parse it
					}
				}
			}
			else
				Console.Error.WriteLine("Usage: java Main [-showtree] " + "<directory or file name>");
		}
		catch (System.Exception e)
		{
			Console.Error.WriteLine("exception: " + e);
			Console.Error.WriteLine(e.StackTrace); // so we can get stack trace
		}
	}
	
	
	// This method decides what action to take based on the type of
	//   file we are looking at
	public static void  doFile(FileInfo f)
	{
		// If this is a directory, walk each file/dir in that directory
		if (Directory.Exists(f.FullName))
		{
			string[] files = Directory.GetFileSystemEntries(f.FullName);
			 for (int i = 0; i < files.Length; i++)
				doFile(new FileInfo(f.FullName + "\\" + files[i]));
		}
		else if ((f.Name.Length > 5) && f.Name.Substring(f.Name.Length - 5).Equals(".java"))
		{
			Console.Error.WriteLine("   " + f.FullName);
			parseFile(f.Name, new FileStream(f.FullName, FileMode.Open, FileAccess.Read));
		}
	}
	
	// Here's where we do the real work...
	public static void  parseFile(string f, Stream s)
	{
		try
		{
			// Create a scanner that reads from the input stream passed to us
			JavaLexer lexer = new JavaLexer(new StreamReader(s));
			lexer.setFilename(f);
			
			// Create a parser that reads from the scanner
			JavaRecognizer parser = new JavaRecognizer(lexer);
			parser.setFilename(f);
			
			// start parsing at the compilationUnit rule
			parser.compilationUnit();
			
			// do something with the tree
			doTreeAction(f, 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		
		}
	}
	
	public static void  doTreeAction(string f, AST t, string[] tokenNames)
	{
		if (t == null)
			return ;
		if (showTree)
		{
			BaseAST.setVerboseStringConversion(true, tokenNames);
			ASTFactory factory = new ASTFactory();
			AST r = factory.create(0, "AST ROOT");
			r.setFirstChild(t);
			ASTFrame frame = new ASTFrame("Java AST", r);
			frame.ShowDialog();
			//frame.Visible = true;
			// System.out.println(t.toStringList());
		}
		JavaTreeParser tparse = new JavaTreeParser();
		try
		{
			tparse.compilationUnit(t);
			// System.out.println("successful walk of result AST for "+f);
		}
		catch (RecognitionException e)
		{
			Console.Error.WriteLine(e.Message);
			Console.Error.WriteLine(e.StackTrace);
		}
		
	}
}