Mercurial Hosting > luan
annotate src/goodjava/parser/Parser.java @ 1541:dc23c96f5021
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 13 Sep 2020 15:50:52 -0600 |
parents | 27efb1fcbcb5 |
children |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1212
diff
changeset
|
1 package goodjava.parser; |
585 | 2 |
3 | |
4 public class Parser { | |
5 public final String text; | |
6 private final int len; | |
7 private int[] stack = new int[256]; | |
8 private int frame = 0; | |
9 private int iHigh; | |
10 | |
1110
38a42f437fd2
queryparser now uses parsers.Parser
Franklin Schmidt <fschmidt@gmail.com>
parents:
775
diff
changeset
|
11 public Parser(String text) { |
585 | 12 this.text = text; |
13 this.len = text.length(); | |
14 } | |
15 | |
16 private int i() { | |
17 return stack[frame]; | |
18 } | |
19 | |
20 private void i(int i) { | |
21 stack[frame] += i; | |
22 if( iHigh < stack[frame] ) | |
23 iHigh = stack[frame]; | |
24 } | |
25 | |
26 public int begin() { | |
27 frame++; | |
28 if( frame == stack.length ) { | |
29 int[] a = new int[2*frame]; | |
30 System.arraycopy(stack,0,a,0,frame); | |
31 stack = a; | |
32 } | |
33 stack[frame] = stack[frame-1]; | |
34 return i(); | |
35 } | |
36 | |
37 public void rollback() { | |
1212
220228bf1af9
better urlDecode error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1111
diff
changeset
|
38 stack[frame] = frame==0 ? 0 : stack[frame-1]; |
585 | 39 } |
40 | |
41 public <T> T success(T t) { | |
42 success(); | |
43 return t; | |
44 } | |
45 | |
46 public boolean success() { | |
47 frame--; | |
48 stack[frame] = stack[frame+1]; | |
49 return true; | |
50 } | |
51 | |
52 public <T> T failure(T t) { | |
53 failure(); | |
54 return t; | |
55 } | |
56 | |
57 public boolean failure() { | |
58 frame--; | |
59 return false; | |
60 } | |
61 | |
62 public int currentIndex() { | |
63 return i(); | |
64 } | |
65 /* | |
66 public int errorIndex() { | |
67 return frame > 0 ? stack[frame-1] : 0; | |
68 } | |
69 */ | |
70 public int highIndex() { | |
71 return iHigh; | |
72 } | |
73 | |
74 public char lastChar() { | |
75 return text.charAt(i()-1); | |
76 } | |
77 | |
78 public char currentChar() { | |
79 return text.charAt(i()); | |
80 } | |
81 | |
82 public boolean endOfInput() { | |
83 return i() >= len; | |
84 } | |
85 | |
86 public boolean match(char c) { | |
87 if( endOfInput() || text.charAt(i()) != c ) | |
88 return false; | |
89 i(1); | |
90 return true; | |
91 } | |
92 | |
93 public boolean match(String s) { | |
94 int n = s.length(); | |
95 if( !text.regionMatches(i(),s,0,n) ) | |
96 return false; | |
97 i(n); | |
98 return true; | |
99 } | |
100 | |
101 public boolean matchIgnoreCase(String s) { | |
102 int n = s.length(); | |
103 if( !text.regionMatches(true,i(),s,0,n) ) | |
104 return false; | |
105 i(n); | |
106 return true; | |
107 } | |
108 | |
109 public boolean anyOf(String s) { | |
110 if( endOfInput() || s.indexOf(text.charAt(i())) == -1 ) | |
111 return false; | |
112 i(1); | |
113 return true; | |
114 } | |
115 | |
116 public boolean noneOf(String s) { | |
117 if( endOfInput() || s.indexOf(text.charAt(i())) != -1 ) | |
118 return false; | |
119 i(1); | |
120 return true; | |
121 } | |
122 | |
123 public boolean inCharRange(char cLow, char cHigh) { | |
124 if( endOfInput() ) | |
125 return false; | |
126 char c = text.charAt(i()); | |
127 if( !(cLow <= c && c <= cHigh) ) | |
128 return false; | |
129 i(1); | |
130 return true; | |
131 } | |
132 | |
133 public boolean anyChar() { | |
134 if( endOfInput() ) | |
135 return false; | |
136 i(1); | |
137 return true; | |
138 } | |
139 | |
140 public boolean test(char c) { | |
141 return !endOfInput() && text.charAt(i()) == c; | |
142 } | |
143 | |
144 public boolean test(String s) { | |
145 return text.regionMatches(i(),s,0,s.length()); | |
146 } | |
147 | |
148 public boolean testIgnoreCase(String s) { | |
149 return text.regionMatches(true,i(),s,0,s.length()); | |
150 } | |
151 | |
152 public String textFrom(int start) { | |
153 return text.substring(start,i()); | |
154 } | |
155 | |
156 } |