comparison src/luan/lib/parser/ParseException.java @ 1114:540e1940170f

improve ParseException
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Aug 2017 16:21:45 -0600
parents 88b5b81cad4a
children
comparison
equal deleted inserted replaced
1113:22652f4020fb 1114:540e1940170f
46 private String[] lines() { 46 private String[] lines() {
47 return text.split("\n",-1); 47 return text.split("\n",-1);
48 } 48 }
49 49
50 @Override public String getMessage() { 50 @Override public String getMessage() {
51 Location loc = new Location(errorIndex); 51 String line;
52 String line = lines()[loc.line]; 52 int pos;
53 String msg = super.getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ")\n"; 53 StringBuilder sb = new StringBuilder(super.getMessage());
54 StringBuilder sb = new StringBuilder(msg); 54 if( text.indexOf('\n') == -1 ) {
55 line = text;
56 pos = errorIndex;
57 sb.append( " (position " + (pos+1) + ")\n" );
58 } else {
59 Location loc = new Location(errorIndex);
60 line = lines()[loc.line];
61 pos = loc.pos;
62 sb.append( " (line " + (loc.line+1) + ", pos " + (pos+1) + ")\n" );
63 }
55 sb.append( line + "\n" ); 64 sb.append( line + "\n" );
56 for( int i=0; i<loc.pos; i++ ) { 65 for( int i=0; i<pos; i++ ) {
57 sb.append( line.charAt(i)=='\t' ? '\t' : ' ' ); 66 sb.append( line.charAt(i)=='\t' ? '\t' : ' ' );
58 } 67 }
59 sb.append("^\n"); 68 sb.append("^\n");
60 return sb.toString(); 69 return sb.toString();
61 } 70 }