2012-08-28 16:56:56 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
|
|
import org.luaj.vm2.ast.*;
|
2012-09-01 15:56:09 +00:00
|
|
|
import org.luaj.vm2.ast.Exp.AnonFuncDef;
|
|
|
|
|
import org.luaj.vm2.ast.Stat.FuncDef;
|
|
|
|
|
import org.luaj.vm2.ast.Stat.LocalFuncDef;
|
2012-08-28 16:56:56 +00:00
|
|
|
import org.luaj.vm2.parser.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
|
2012-10-27 15:03:57 +00:00
|
|
|
// Create a LuaParser. This will fill in line and column number
|
|
|
|
|
// information for most exceptions.
|
2012-09-01 16:51:45 +00:00
|
|
|
LuaParser parser = new LuaParser(new FileInputStream(file));
|
2012-08-28 16:56:56 +00:00
|
|
|
|
|
|
|
|
// Perform the parsing.
|
|
|
|
|
Chunk chunk = parser.Chunk();
|
|
|
|
|
|
2012-09-01 15:56:09 +00:00
|
|
|
// Print out line info for all function definitions.
|
2012-08-28 16:56:56 +00:00
|
|
|
chunk.accept( new Visitor() {
|
2012-09-01 15:56:09 +00:00
|
|
|
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);
|
|
|
|
|
}
|
2012-08-28 16:56:56 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
} catch ( ParseException e ) {
|
2012-10-27 15:03:57 +00:00
|
|
|
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);
|
2012-08-28 16:56:56 +00:00
|
|
|
|
|
|
|
|
} catch ( IOException e ) {
|
|
|
|
|
System.out.println( "IOException occurred: "+e );
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|