Mercurial Hosting > luan
comparison core/src/luan/LuanSource.java @ 171:3dcb0f9bee82
add core component
git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Sun, 22 Jun 2014 05:41:22 +0000 |
parents | src/luan/LuanSource.java@1f8b6edc2b08 |
children | 04b86428dc50 |
comparison
equal
deleted
inserted
replaced
170:7c792a328a83 | 171:3dcb0f9bee82 |
---|---|
1 package luan; | |
2 | |
3 | |
4 public final class LuanSource { | |
5 public final String name; | |
6 public final String text; | |
7 | |
8 public LuanSource(String name,String text) { | |
9 this.name = name; | |
10 this.text = text; | |
11 } | |
12 | |
13 public static final class CompilerElement extends LuanElement { | |
14 public final LuanSource source; | |
15 | |
16 public CompilerElement(LuanSource source) { | |
17 if( source==null ) | |
18 throw new NullPointerException("source is null"); | |
19 this.source = source; | |
20 } | |
21 | |
22 @Override String location() { | |
23 return "Compiling " + source.name; | |
24 } | |
25 } | |
26 | |
27 public static final class Element extends LuanElement { | |
28 public final LuanSource source; | |
29 public final int start; | |
30 public final int end; | |
31 | |
32 public Element(LuanSource source,int start,int end) { | |
33 if( source==null ) | |
34 throw new NullPointerException("source is null"); | |
35 this.source = source; | |
36 this.start = start; | |
37 while( end > 0 && Character.isWhitespace(source.text.charAt(end-1)) ) { | |
38 end--; | |
39 } | |
40 this.end = end; | |
41 } | |
42 | |
43 public String text() { | |
44 return source.text.substring(start,end); | |
45 } | |
46 | |
47 @Override String location() { | |
48 return source.name + ':' + lineNumber(); | |
49 } | |
50 | |
51 private int lineNumber() { | |
52 int line = 0; | |
53 int i = -1; | |
54 do { | |
55 line++; | |
56 i = source.text.indexOf('\n',i+1); | |
57 } while( i != -1 && i < start ); | |
58 return line; | |
59 } | |
60 | |
61 } | |
62 } |