Mercurial Hosting > luan
annotate src/luan/impl/ParseException.java @ 1654:3f28ea044a32
docs UI
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 04 Apr 2022 22:19:32 -0600 |
parents | 1a68fc55a80c |
children |
rev | line source |
---|---|
166
4eaee12f6c65
move luan/interp to impl
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
165
diff
changeset
|
1 package luan.impl; |
104
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
2 |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
3 |
165
94bbc4cbc106
merge luan/parser into interp
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
118
diff
changeset
|
4 public final class ParseException extends Exception { |
645 | 5 // public final LuanSource src; |
6 public final String sourceName; | |
7 public final String text; | |
104
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
8 public final int iCurrent; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
9 public final int iHigh; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
10 |
694
b620b8e1010f
remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
11 ParseException(String msg,String text,String sourceName,int iCurrent,int iHigh) { |
104
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
12 super(msg); |
645 | 13 // this.src = src; |
694
b620b8e1010f
remove Luan.load() allow_expression param and add Luan.eval()
Franklin Schmidt <fschmidt@gmail.com>
parents:
645
diff
changeset
|
14 this.text = text; |
645 | 15 this.sourceName = sourceName; |
104
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
16 this.iCurrent = iCurrent; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
17 this.iHigh = iHigh; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
18 //System.out.println("iCurrent = "+iCurrent); |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
19 //System.out.println("iHigh = "+iHigh); |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
20 } |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
21 |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
22 private class Location { |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
23 final int line; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
24 final int pos; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
25 |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
26 Location(int index) { |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
27 int line = 0; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
28 int i = -1; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
29 while(true) { |
645 | 30 int j = text.indexOf('\n',i+1); |
118
735708619119
add Debug.debug()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
104
diff
changeset
|
31 if( j == -1 || j >= index ) |
104
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
32 break; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
33 i = j; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
34 line++; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
35 } |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
36 this.line = line; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
37 this.pos = index - i - 1; |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
38 } |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
39 } |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
40 |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
41 private String[] lines() { |
645 | 42 return text.split("\n",-1); |
104
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
43 } |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
44 |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
45 public String getFancyMessage() { |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
46 Location loc = new Location(iCurrent); |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
47 String line = lines()[loc.line]; |
645 | 48 String msg = getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ") in " + sourceName + "\n"; |
104
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
49 StringBuilder sb = new StringBuilder(msg); |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
50 sb.append( line + "\n" ); |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
51 for( int i=0; i<loc.pos; i++ ) { |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
52 sb.append( line.charAt(i)=='\t' ? '\t' : ' ' ); |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
53 } |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
54 sb.append("^\n"); |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
55 return sb.toString(); |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
56 } |
754e6030c029
remove parboiled and rewrite parser;
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff
changeset
|
57 } |