annotate src/goodjava/parser/ParseException.java @ 1805:7793bcb5f34a

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 08 May 2024 11:13:16 -0600
parents 764723436f05
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1114
diff changeset
1 package goodjava.parser;
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 public final class ParseException extends Exception {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 public final String text;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 public final int errorIndex;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 public final int highIndex;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 super(msg);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 this.text = parser.text;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 this.errorIndex = parser.currentIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 this.highIndex = parser.highIndex();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 private class Location {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 final int line;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 final int pos;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 Location(int index) {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 int line = 0;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 int i = -1;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 while(true) {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 int j = text.indexOf('\n',i+1);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 if( j == -1 || j >= index )
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 break;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 i = j;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 line++;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 this.line = line;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 this.pos = index - i - 1;
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 private String[] lines() {
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 return text.split("\n",-1);
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
1774
764723436f05 better json_parse exception
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
50 public String getSuperMessage() {
764723436f05 better json_parse exception
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
51 return super.getMessage();
764723436f05 better json_parse exception
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
52 }
764723436f05 better json_parse exception
Franklin Schmidt <fschmidt@gmail.com>
parents: 1402
diff changeset
53
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 @Override public String getMessage() {
1114
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
55 String line;
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
56 int pos;
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
57 StringBuilder sb = new StringBuilder(super.getMessage());
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
58 if( text.indexOf('\n') == -1 ) {
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
59 line = text;
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
60 pos = errorIndex;
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
61 sb.append( " (position " + (pos+1) + ")\n" );
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
62 } else {
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
63 Location loc = new Location(errorIndex);
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
64 line = lines()[loc.line];
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
65 pos = loc.pos;
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
66 sb.append( " (line " + (loc.line+1) + ", pos " + (pos+1) + ")\n" );
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
67 }
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 sb.append( line + "\n" );
1114
540e1940170f improve ParseException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1111
diff changeset
69 for( int i=0; i<pos; i++ ) {
585
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 sb.append( line.charAt(i)=='\t' ? '\t' : ' ' );
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 sb.append("^\n");
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 return sb.toString();
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 }
bb3818249dfb add Parsers
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 }