Mercurial Hosting > luan
annotate src/goodjava/parser/ParseException.java @ 1528:3bd4d7963456
use goodjava/lucene/api
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 26 Jul 2020 23:11:53 -0600 |
parents | 27efb1fcbcb5 |
children | 764723436f05 |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1114
diff
changeset
|
1 package goodjava.parser; |
585 | 2 |
3 | |
4 public final class ParseException extends Exception { | |
5 public final String text; | |
6 public final int errorIndex; | |
7 public final int highIndex; | |
8 | |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
9 public ParseException(Parser parser,String msg) { |
585 | 10 super(msg); |
11 this.text = parser.text; | |
12 this.errorIndex = parser.currentIndex(); | |
13 this.highIndex = parser.highIndex(); | |
14 } | |
15 | |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
16 public ParseException(Parser parser,Exception cause) { |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
17 this(parser,cause.getMessage(),cause); |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
18 } |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
19 |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
20 public ParseException(Parser parser,String msg,Exception cause) { |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
21 super(msg,cause); |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
22 this.text = parser.text; |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
23 this.errorIndex = parser.currentIndex(); |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
24 this.highIndex = parser.highIndex(); |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
25 } |
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
26 |
585 | 27 private class Location { |
28 final int line; | |
29 final int pos; | |
30 | |
31 Location(int index) { | |
32 int line = 0; | |
33 int i = -1; | |
34 while(true) { | |
35 int j = text.indexOf('\n',i+1); | |
36 if( j == -1 || j >= index ) | |
37 break; | |
38 i = j; | |
39 line++; | |
40 } | |
41 this.line = line; | |
42 this.pos = index - i - 1; | |
43 } | |
44 } | |
45 | |
46 private String[] lines() { | |
47 return text.split("\n",-1); | |
48 } | |
49 | |
50 @Override public String getMessage() { | |
1114
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
51 String line; |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
52 int pos; |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
53 StringBuilder sb = new StringBuilder(super.getMessage()); |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
54 if( text.indexOf('\n') == -1 ) { |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
55 line = text; |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
56 pos = errorIndex; |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
57 sb.append( " (position " + (pos+1) + ")\n" ); |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
58 } else { |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
59 Location loc = new Location(errorIndex); |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
60 line = lines()[loc.line]; |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
61 pos = loc.pos; |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
62 sb.append( " (line " + (loc.line+1) + ", pos " + (pos+1) + ")\n" ); |
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
63 } |
585 | 64 sb.append( line + "\n" ); |
1114
540e1940170f
improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
65 for( int i=0; i<pos; i++ ) { |
585 | 66 sb.append( line.charAt(i)=='\t' ? '\t' : ' ' ); |
67 } | |
68 sb.append("^\n"); | |
69 return sb.toString(); | |
70 } | |
71 } |