Mercurial Hosting > luan
comparison src/luan/parser/ParseException.java @ 104:754e6030c029
remove parboiled and rewrite parser;
add jline;
git-svn-id: https://luan-java.googlecode.com/svn/trunk@105 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 12 May 2014 05:57:53 +0000 |
parents | |
children | 735708619119 |
comparison
equal
deleted
inserted
replaced
103:c6c0aed1dbec | 104:754e6030c029 |
---|---|
1 package luan.parser; | |
2 | |
3 | |
4 public class ParseException extends Exception { | |
5 public final String text; | |
6 public final int iCurrent; | |
7 public final int iHigh; | |
8 | |
9 ParseException(String msg,String text,int iCurrent,int iHigh) { | |
10 super(msg); | |
11 this.text = text; | |
12 this.iCurrent = iCurrent; | |
13 this.iHigh = iHigh; | |
14 //System.out.println("iCurrent = "+iCurrent); | |
15 //System.out.println("iHigh = "+iHigh); | |
16 } | |
17 | |
18 private class Location { | |
19 final int line; | |
20 final int pos; | |
21 | |
22 Location(int index) { | |
23 int line = 0; | |
24 int i = -1; | |
25 while(true) { | |
26 int j = text.indexOf('\n',i+1); | |
27 if( j == -1 || j > index ) | |
28 break; | |
29 i = j; | |
30 line++; | |
31 } | |
32 this.line = line; | |
33 this.pos = index - i - 1; | |
34 } | |
35 } | |
36 | |
37 private String[] lines() { | |
38 return text.split("\n",-1); | |
39 } | |
40 | |
41 public String getFancyMessage() { | |
42 Location loc = new Location(iCurrent); | |
43 String line = lines()[loc.line]; | |
44 String msg = getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ")\n"; | |
45 StringBuilder sb = new StringBuilder(msg); | |
46 sb.append( line + "\n" ); | |
47 for( int i=0; i<loc.pos; i++ ) { | |
48 sb.append( line.charAt(i)=='\t' ? '\t' : ' ' ); | |
49 } | |
50 sb.append("^\n"); | |
51 return sb.toString(); | |
52 } | |
53 } |