Mercurial Hosting > luan
comparison src/luan/impl/ParseException.java @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | core/src/luan/impl/ParseException.java@b620b8e1010f |
children |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 package luan.impl; | |
2 | |
3 | |
4 public final class ParseException extends Exception { | |
5 // public final LuanSource src; | |
6 public final String sourceName; | |
7 public final String text; | |
8 public final int iCurrent; | |
9 public final int iHigh; | |
10 | |
11 ParseException(String msg,String text,String sourceName,int iCurrent,int iHigh) { | |
12 super(msg); | |
13 // this.src = src; | |
14 this.text = text; | |
15 this.sourceName = sourceName; | |
16 this.iCurrent = iCurrent; | |
17 this.iHigh = iHigh; | |
18 //System.out.println("iCurrent = "+iCurrent); | |
19 //System.out.println("iHigh = "+iHigh); | |
20 } | |
21 | |
22 private class Location { | |
23 final int line; | |
24 final int pos; | |
25 | |
26 Location(int index) { | |
27 int line = 0; | |
28 int i = -1; | |
29 while(true) { | |
30 int j = text.indexOf('\n',i+1); | |
31 if( j == -1 || j >= index ) | |
32 break; | |
33 i = j; | |
34 line++; | |
35 } | |
36 this.line = line; | |
37 this.pos = index - i - 1; | |
38 } | |
39 } | |
40 | |
41 private String[] lines() { | |
42 return text.split("\n",-1); | |
43 } | |
44 | |
45 public String getFancyMessage() { | |
46 Location loc = new Location(iCurrent); | |
47 String line = lines()[loc.line]; | |
48 String msg = getMessage() + " (line " + (loc.line+1) + ", pos " + (loc.pos+1) + ") in " + sourceName + "\n"; | |
49 StringBuilder sb = new StringBuilder(msg); | |
50 sb.append( line + "\n" ); | |
51 for( int i=0; i<loc.pos; i++ ) { | |
52 sb.append( line.charAt(i)=='\t' ? '\t' : ' ' ); | |
53 } | |
54 sb.append("^\n"); | |
55 return sb.toString(); | |
56 } | |
57 } |