Mercurial Hosting > luan
comparison src/luan/impl/Parser.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/Parser.java@b620b8e1010f |
children |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 package luan.impl; | |
2 | |
3 | |
4 final class Parser { | |
5 | |
6 private static class Frame { | |
7 int i; | |
8 StringBuilder sb; | |
9 } | |
10 | |
11 // private final LuanSource src; | |
12 public final String text; | |
13 public final String sourceName; | |
14 private final int len; | |
15 private Frame[] stack = new Frame[256]; | |
16 private int frame = 0; | |
17 private int iHigh; | |
18 | |
19 public Parser(String text,String sourceName) { | |
20 // this.src = src; | |
21 this.text = text; | |
22 this.sourceName = sourceName; | |
23 this.len = text.length(); | |
24 stack[0] = new Frame(); | |
25 } | |
26 | |
27 private int i() { | |
28 return stack[frame].i; | |
29 } | |
30 | |
31 private void i(int i) { | |
32 Frame f = stack[frame]; | |
33 f.i += i; | |
34 if( iHigh < f.i ) | |
35 iHigh = f.i; | |
36 } | |
37 | |
38 public int begin() { | |
39 frame++; | |
40 if( frame == stack.length ) { | |
41 Frame[] a = new Frame[2*frame]; | |
42 System.arraycopy(stack,0,a,0,frame); | |
43 stack = a; | |
44 } | |
45 Frame f = new Frame(); | |
46 f.i = stack[frame-1].i; | |
47 stack[frame] = f; | |
48 return i(); | |
49 } | |
50 | |
51 public void rollback() { | |
52 Frame f = stack[frame]; | |
53 f.i = stack[frame-1].i; | |
54 f.sb = null; | |
55 } | |
56 | |
57 public <T> T success(T t) { | |
58 success(); | |
59 return t; | |
60 } | |
61 | |
62 public boolean success() { | |
63 Frame f = stack[frame]; | |
64 if( f.sb != null && f.sb.length() > 0 ) throw new RuntimeException("sb not emtpy"); | |
65 frame--; | |
66 stack[frame].i = f.i; | |
67 return true; | |
68 } | |
69 | |
70 public <T> T failure(T t) { | |
71 failure(); | |
72 return t; | |
73 } | |
74 | |
75 public boolean failure() { | |
76 frame--; | |
77 return false; | |
78 } | |
79 | |
80 public ParseException exception(String msg) { | |
81 return new ParseException(msg,text,sourceName,i(),iHigh); | |
82 } | |
83 | |
84 public ParseException exception() { | |
85 return exception("Invalid input"); | |
86 } | |
87 | |
88 public StringBuilder sb() { | |
89 Frame f = stack[frame]; | |
90 if( f.sb == null ) | |
91 f.sb = new StringBuilder(); | |
92 return f.sb; | |
93 } | |
94 | |
95 public void upSb() { | |
96 Frame f = stack[frame]; | |
97 StringBuilder sb = f.sb; | |
98 if( sb != null && sb.length() > 0 ) { | |
99 Frame fUp = stack[frame-1]; | |
100 if( fUp.sb == null ) | |
101 fUp.sb = sb; | |
102 else | |
103 fUp.sb.append(sb.toString()); | |
104 f.sb = null; | |
105 } | |
106 } | |
107 | |
108 public int currentIndex() { | |
109 return i(); | |
110 } | |
111 | |
112 public char lastChar() { | |
113 return text.charAt(i()-1); | |
114 } | |
115 | |
116 public char currentChar() { | |
117 return text.charAt(i()); | |
118 } | |
119 | |
120 public boolean endOfInput() { | |
121 return i() >= len; | |
122 } | |
123 | |
124 public boolean match(char c) { | |
125 if( endOfInput() || text.charAt(i()) != c ) | |
126 return false; | |
127 i(1); | |
128 return true; | |
129 } | |
130 | |
131 public boolean match(String s) { | |
132 int n = s.length(); | |
133 if( !text.regionMatches(i(),s,0,n) ) | |
134 return false; | |
135 i(n); | |
136 return true; | |
137 } | |
138 | |
139 public boolean matchIgnoreCase(String s) { | |
140 int n = s.length(); | |
141 if( !text.regionMatches(true,i(),s,0,n) ) | |
142 return false; | |
143 i(n); | |
144 return true; | |
145 } | |
146 | |
147 public boolean anyOf(String s) { | |
148 if( endOfInput() || s.indexOf(text.charAt(i())) == -1 ) | |
149 return false; | |
150 i(1); | |
151 return true; | |
152 } | |
153 | |
154 public boolean noneOf(String s) { | |
155 if( endOfInput() || s.indexOf(text.charAt(i())) != -1 ) | |
156 return false; | |
157 i(1); | |
158 return true; | |
159 } | |
160 | |
161 public boolean inCharRange(char cLow, char cHigh) { | |
162 if( endOfInput() ) | |
163 return false; | |
164 char c = text.charAt(i()); | |
165 if( !(cLow <= c && c <= cHigh) ) | |
166 return false; | |
167 i(1); | |
168 return true; | |
169 } | |
170 | |
171 public boolean anyChar() { | |
172 if( endOfInput() ) | |
173 return false; | |
174 i(1); | |
175 return true; | |
176 } | |
177 | |
178 public boolean test(char c) { | |
179 return !endOfInput() && text.charAt(i()) == c; | |
180 } | |
181 | |
182 public boolean test(String s) { | |
183 return text.regionMatches(i(),s,0,s.length()); | |
184 } | |
185 | |
186 public String textFrom(int start) { | |
187 return text.substring(start,i()); | |
188 } | |
189 | |
190 } |