File: Main.java

package info (click to toggle)
antlr 2.7.7%2Bdfsg-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,016 kB
  • sloc: java: 54,649; cs: 12,537; makefile: 8,854; cpp: 7,359; pascal: 5,273; sh: 4,333; python: 4,297; lisp: 1,969; xml: 220; lex: 192; ansic: 127
file content (115 lines) | stat: -rw-r--r-- 3,218 bytes parent folder | download | duplicates (12)
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
package tinybasic;

import java.io.*;
import antlr.collections.AST;
import antlr.collections.impl.*;
import antlr.debug.misc.*;
import antlr.*;

class Main {

    Context theContext=new Context();
    static boolean showTree = false;
    
    Main(String f){
	try {
	    doFile(new File(f));
	}
	catch(Exception e) {
		System.err.println("exception: "+e);
		e.printStackTrace(System.err);   // so we can get stack trace
	}
    }
    
    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 ) {
				System.err.println("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 {
						new Main(args[i]); // parse it
					}
				} }
			else
				System.err.println("Usage: java TinyBasicParser [-showtree] "+
                                   "<directory or file name>");
		//}
		//catch(Exception e) {
		//	System.err.println("exception: "+e);
		//	e.printStackTrace(System.err);   // so we can get stack trace
		//}
	}


	// This method decides what action to take based on the type of
	//   file we are looking at
	public void doFile(File f)
							  throws Exception {
		// If this is a directory, walk each file/dir in that directory
		if (f.isDirectory()) {
			String files[] = f.list();
			for(int i=0; i < files.length; i++)
				doFile(new File(f, files[i]));
		}

		// otherwise, if this is a java file, parse it!
		else if ((f.getName().length()>4) &&
				f.getName().substring(f.getName().length()-4).equals(".bas")) {
			System.err.println("   "+f.getAbsolutePath());
			parseFile(f.getName(), new FileInputStream(f));
		}
	}

	// Here's where we do the real work...
	public void parseFile(String f, InputStream s)
								 throws Exception {
		try {
			// Create a scanner that reads from the input stream passed to us
			TinyBasicLexer lexer = new TinyBasicLexer(s);

			// Create a parser that reads from the scanner
			TinyBasicParser parser = new TinyBasicParser(lexer);

			// start parsing at the compilationUnit rule
			parser.compilationUnit(theContext);
			
			// do something with the tree
			doTreeAction(f, parser.getAST(), parser.getTokenNames());
			//System.out.println(parser.getAST().toStringList());
		}
		catch (Exception e) {
			System.err.println("parser exception: "+e);
			e.printStackTrace();   // so we can get stack trace		
		}
	}
	
	public void doTreeAction(String f, AST t, String[] tokenNames) {
		if ( t==null ) return;
		if ( showTree ) {
			((CommonAST)t).setVerboseStringConversion(true, tokenNames);
			ASTFactory factory = new ASTFactory();
			AST r = factory.create(0,"AST ROOT");
			r.setFirstChild(t);
			ASTFrame frame = new ASTFrame("TinyBasic AST", r);
			frame.setVisible(true);
			//System.out.println(t.toStringList());
		}
		TinyBasicTreeWalker tparse = new TinyBasicTreeWalker();
		try {
			tparse.compilationUnit(t,theContext);
			//System.out.println("successful walk of result AST for "+f);
		}
		catch (ANTLRException e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
		}

	}
}