-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParserDriver.java
68 lines (61 loc) · 2.04 KB
/
ParserDriver.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
61
62
63
64
65
66
67
68
// ParserDriver.java - Test whether the parser can handle a file.
//
// To use this:
// find srcdir -name '*.rs' | CLASSPATH=".:$CLASSPATH" java ParserDriver
// You'll need ANTLR4 in your CLASSPATH too.
import org.antlr.v4.runtime.*;
import java.io.*;
import java.lang.*;
import java.util.BitSet;
import java.util.Scanner;
class CountingErrorListener extends BaseErrorListener {
public int count = 0;
public void syntaxError(Recognizer<?,?> recognizer, Object offendingSymbol,
int line, int charPositionInLine,
String msg, RecognitionException e) {
count++;
}
}
public class ParserDriver {
public static boolean canParse(String filename) throws IOException {
System.out.println(filename + ": ");
ANTLRFileStream infile = new ANTLRFileStream(filename);
RustLexer lexer = new RustLexer(infile);
CountingErrorListener lexerErrors = new CountingErrorListener();
lexer.addErrorListener(lexerErrors);
CommonTokenStream tokens = new CommonTokenStream(lexer);
RustParser parser = new RustParser(tokens);
try {
parser.crate();
} catch (RecognitionException oops) {
System.out.println(oops);
return false;
}
int n = lexerErrors.count + parser.getNumberOfSyntaxErrors();
if (n == 0) {
System.out.println("ok");
return true;
} else {
System.out.println("" + n + " error(s)");
return false;
}
}
static int main() {
boolean ok = true;
try {
Scanner stdin = new Scanner(System.in);
while (stdin.hasNextLine()) {
String filename = stdin.nextLine();
if (!canParse(filename))
ok = false;
}
} catch (IOException exc) {
exc.printStackTrace();
return 2;
}
return ok ? 0 : 1;
}
public static void main(String[] args) {
System.exit(main());
}
}