forked from bkiers/python3-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrun4py.java
More file actions
59 lines (48 loc) · 2.25 KB
/
grun4py.java
File metadata and controls
59 lines (48 loc) · 2.25 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
// ******* GRUN (Grammar Unit Test) for Python *******
import org.antlr.v4.runtime.*;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;
public class grun4py {
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Error: Please provide an input file path");
System.exit(1);
}
try {
final Path path = Paths.get(args[0]);
CharStream input = CharStreams.fromPath(path, Charset.forName("utf-8"));
PythonLexer lexer = new PythonLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PythonParser parser = new PythonParser(tokens);
tokens.fill(); // Test the lexer grammar
for (Token t : tokens.getTokens()) {
System.out.println(formatToken(t));
}
parser.file_input(); // Test the parser grammar
System.exit(parser.getNumberOfSyntaxErrors());
} catch (Exception ex) {
System.err.println("Error: " + ex.getMessage());
System.exit(1); // Error occurred, returning non-zero exit code
}
}
private static String formatToken(Token token) {
String tokenText = replaceSpecialCharacters(token.getText());
String tokenName = token.getType() == Token.EOF ? "EOF" : PythonLexer.VOCABULARY.getSymbolicName(token.getType());
String channelText = token.getChannel() == Token.DEFAULT_CHANNEL ?
"" :
"channel=" + PythonLexer.channelNames[token.getChannel()] + ",";
// Modified format: [@TokenIndex,StartIndex:StopIndex='Text',<TokenName>,channel=ChannelName,Line:Column]
return String.format("[@%d,%d:%d='%s',<%s>,%s%d:%d]",
token.getTokenIndex(), token.getStartIndex(), token.getStopIndex(),
tokenText, tokenName, channelText, token.getLine(), token.getCharPositionInLine());
}
private static String replaceSpecialCharacters(String text) {
return text.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t")
.replace("\f", "\\f");
}
}