-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserTester.java
60 lines (55 loc) · 2.11 KB
/
ParserTester.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import javax.swing.JFileChooser;
public class ParserTester{
/**
* For testing your parser without requiring the world or the game:
* Calling main with no arguments will repeatedly ask the user
* to choose a program file and will then attempt to parse it and print it.
*
* Calling main with arguments will treat each argument as a file name
* and will attempt to parse and print each file.
*/
public static void main(String[] args) {
Parser parser = new Parser();
System.out.println("=================");
if (args.length > 0) {
for (String arg : args) {
File file = new File(arg);
if (file.exists()) {testParserOnFile(parser, file);}
else {System.out.println("Can't find file '" + file + "'");}
}
} else {
while (true) {
JFileChooser chooser = new JFileChooser(RoboGame.CODE_DIRECTORY);
int res = chooser.showOpenDialog(null);
if (res != JFileChooser.APPROVE_OPTION) { break; }
testParserOnFile(parser,chooser.getSelectedFile());
}
}
System.out.println("Done");
}
/**
* Tests the parser on a single file.
*/
static void testParserOnFile(Parser parser, File file){
System.out.println("Parsing '" + file + "'");
try{
Scanner scan = new Scanner(file);
ProgramNode prog = parser.parse(scan);
System.out.println("Parsing completed ");
if (prog == null) {System.out.println("No program generated"); }
else {System.out.println("Program: \n" + prog); }
scan.close();
}
catch (FileNotFoundException e) {
System.out.println("Robot program source file not found");
}
catch (ParserFailureException e) {
System.out.println("Parser error:");
System.out.println(e.getMessage());
}
System.out.println("=================");
}
}