Improve sample code for LuaParser.

This commit is contained in:
James Roseborough
2012-10-27 15:03:57 +00:00
parent c7165c0cd5
commit d4b8ec31dd
2 changed files with 10 additions and 12 deletions

View File

@@ -638,9 +638,10 @@ For example, to parse a file and print all variable names, use code like:
} }
} ); } );
} catch ( ParseException e ) { } catch ( ParseException e ) {
System.out.println( "parse failed: "+e System.out.println("parse failed: " + e.getMessage() + "\n"
+" line "+e.currentToken.beginLine + "Token Image: '" + e.currentToken.image + "'\n"
+" col "+e.currentToken.beginColumn); + "Location: " + e.currentToken.beginLine + ":" + e.currentToken.beginColumn
+ "-" + e.currentToken.endLine + "," + e.currentToken.endColumn);
} }
</pre> </pre>

View File

@@ -22,8 +22,8 @@ public class SampleParser {
try { try {
final String file = args[0]; final String file = args[0];
// Create a custom LuaParser subclass that intercepts parse exceptions and // Create a LuaParser. This will fill in line and column number
// extracts the line and column number information from the parse token. // information for most exceptions.
LuaParser parser = new LuaParser(new FileInputStream(file)); LuaParser parser = new LuaParser(new FileInputStream(file));
// Perform the parsing. // Perform the parsing.
@@ -55,13 +55,10 @@ public class SampleParser {
} ); } );
} catch ( ParseException e ) { } catch ( ParseException e ) {
System.out.println( "Parse failed : "+e ); System.out.println("parse failed: " + e.getMessage() + "\n"
System.out.println( "Begin Line: "+e.currentToken.beginColumn ); + "Token Image: '" + e.currentToken.image + "'\n"
System.out.println( "Begin Column: "+e.currentToken.endColumn ); + "Location: " + e.currentToken.beginLine + ":" + e.currentToken.beginColumn
System.out.println( "End Line: "+e.currentToken.beginColumn ); + "-" + e.currentToken.endLine + "," + e.currentToken.endColumn);
System.out.println( "End Column: "+e.currentToken.endColumn );
System.out.println( "Token Image: '"+e.currentToken.image+"'" );
System.out.println( "Message: "+e.getMessage() );
} catch ( IOException e ) { } catch ( IOException e ) {
System.out.println( "IOException occurred: "+e ); System.out.println( "IOException occurred: "+e );