Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
86 lines (71 loc) · 2.93 KB
/
Copy pathMain.java
File metadata and controls
86 lines (71 loc) · 2.93 KB
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
import java.io.File;
import java.util.concurrent.Callable;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.Symbol;
import parser.Parser;
import parser.Program;
import parser.Symbols;
import lexer.Lexer;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(footer = "Copyright(c) 2022", description = "Lexer for the java language")
class Main implements Callable<Integer> {
@Parameters(paramLabel = "FILE", description = "Files whose contents to display", arity = "1..*")
File[] files;
@Option(names = "--help", usageHelp = true, description = "display this help and exit")
boolean help;
@Option(names = { "-p", "--parse" }, description = "Parse the file instead of lexing it")
boolean parse;
@Option(names = { "-d", "--debug" }, description = "Print debugging information")
boolean debug;
@Override
public Integer call() throws Exception {
if (help) {
CommandLine.usage(this, System.out);
return 0;
}
ComplexSymbolFactory csf = new ComplexSymbolFactory();
for (File file : files) {
Lexer scanner = null;
try {
java.io.Reader reader = new java.io.FileReader(file);
scanner = new Lexer(reader, csf);
System.out.println("==================== " + file.getPath() + " ====================");
if (parse) {
Parser parser = new Parser(scanner, csf);
Program program = (Program) parser.parse().value;
program.execute();
} else {
System.out.printf("%-15s %-15s %4s:%s\n", "Type", "Value", "line", "column");
Symbol token = scanner.next_token();
while (token.sym != Symbols.EOF) {
if (token.value != null) {
System.out.printf("%s %s%n", token, token.value);
} else {
System.out.printf("%s%n", token);
}
token = scanner.next_token();
}
}
} catch (java.io.FileNotFoundException e) {
System.out.println("File not found : \"" + file.getPath() + "\"");
} catch (java.io.IOException e) {
System.out.println("I/O error while scanning file \"" + file.getPath() + "\"");
System.out.println(e);
} catch (Exception e) {
if (debug) {
System.out.println("Unexpected exception:");
e.printStackTrace();
}
}
System.out.println();
}
return 0;
}
public static void main(String... args) {
int exitCode = new CommandLine(new Main()).execute(args);
System.exit(exitCode);
}
}