-
Notifications
You must be signed in to change notification settings - Fork 7
/
SampleParser.java
69 lines (58 loc) · 2.2 KB
/
SampleParser.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
69
import java.io.*;
import org.luaj.vm2.ast.*;
import org.luaj.vm2.ast.Exp.AnonFuncDef;
import org.luaj.vm2.ast.Stat.FuncDef;
import org.luaj.vm2.ast.Stat.LocalFuncDef;
import org.luaj.vm2.parser.*;
/**
* Sample luaj program that uses the LuaParser class for parsing, and intercepts the
* generated ParseExceptions and fills in the file, line and column information where
* the exception occurred.
*
* @see LuaParser
*/
public class SampleParser {
static public void main(String[] args) {
if (args.length == 0) {
System.out.println("usage: SampleParser luafile");
return;
}
try {
final String file = args[0];
// Create a LuaParser. This will fill in line and column number
// information for most exceptions.
LuaParser parser = new LuaParser(new FileInputStream(file));
// Perform the parsing.
Chunk chunk = parser.Chunk();
// Print out line info for all function definitions.
chunk.accept( new Visitor() {
public void visit(AnonFuncDef exp) {
System.out.println("Anonymous function definition at "
+ exp.beginLine + "." + exp.beginColumn + ","
+ exp.endLine + "." + exp.endColumn);
}
public void visit(FuncDef stat) {
System.out.println("Function definition '" + stat.name.name.name + "' at "
+ stat.beginLine + "." + stat.beginColumn + ","
+ stat.endLine + "." + stat.endColumn);
System.out.println("\tName location "
+ stat.name.beginLine + "." + stat.name.beginColumn + ","
+ stat.name.endLine + "." + stat.name.endColumn);
}
public void visit(LocalFuncDef stat) {
System.out.println("Local function definition '" + stat.name.name + "' at "
+ stat.beginLine + "." + stat.beginColumn + ","
+ stat.endLine + "." + stat.endColumn);
}
} );
} catch ( ParseException e ) {
System.out.println("parse failed: " + e.getMessage() + "\n"
+ "Token Image: '" + e.currentToken.image + "'\n"
+ "Location: " + e.currentToken.beginLine + ":" + e.currentToken.beginColumn
+ "-" + e.currentToken.endLine + "," + e.currentToken.endColumn);
} catch ( IOException e ) {
System.out.println( "IOException occurred: "+e );
e.printStackTrace();
}
}
}