1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.qedeq.kernel.bo.parser; |
19 |
|
|
20 |
|
import java.util.ArrayList; |
21 |
|
import java.util.List; |
22 |
|
|
23 |
|
import org.qedeq.base.io.TextInput; |
24 |
|
import org.qedeq.base.trace.Trace; |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
@link |
74 |
|
|
75 |
|
@version |
76 |
|
@author |
77 |
|
|
|
|
| 78,2% |
Uncovered Elements: 37 (170) |
Complexity: 49 |
Complexity Density: 0,45 |
|
78 |
|
public class LatexMathParser extends MathParser { |
79 |
|
|
80 |
|
|
81 |
|
private static final Class CLASS = LatexMathParser.class; |
82 |
|
|
83 |
|
|
84 |
|
private static final String SPECIALCHARACTERS = "(),{}\\~%$&"; |
85 |
|
|
86 |
|
|
87 |
|
private int tokenWhiteSpaceLines; |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
@param |
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
95 |
11
|
public LatexMathParser(final TextInput input, final List operators) {... |
96 |
11
|
super(new MementoTextInput(input), operators); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@param |
104 |
|
@param |
105 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
106 |
0
|
public LatexMathParser(final StringBuffer buffer, final List operators) {... |
107 |
0
|
this(new TextInput(buffer), operators); |
108 |
|
} |
109 |
|
|
|
|
| 74,7% |
Uncovered Elements: 21 (83) |
Complexity: 24 |
Complexity Density: 0,41 |
|
110 |
658
|
protected final String readToken() {... |
111 |
658
|
final String method = "readToken()"; |
112 |
658
|
Trace.begin(CLASS, this, method); |
113 |
658
|
StringBuffer token = new StringBuffer(); |
114 |
658
|
tokenWhiteSpaceLines = 0; |
115 |
658
|
try { |
116 |
658
|
do { |
117 |
836
|
tokenWhiteSpaceLines += readPureWhitespace(); |
118 |
836
|
if (tokenWhiteSpaceLines > 1) { |
119 |
0
|
break; |
120 |
|
} |
121 |
836
|
if (eof()) { |
122 |
32
|
if (token.length() <= 0) { |
123 |
32
|
token = null; |
124 |
|
} |
125 |
32
|
break; |
126 |
|
} |
127 |
804
|
final int c = getChar(); |
128 |
804
|
if (Character.isDigit((char) c)) { |
129 |
8
|
token.append((char) readChar()); |
130 |
8
|
if (Character.isDigit((char) getChar())) { |
131 |
0
|
continue; |
132 |
|
} |
133 |
8
|
break; |
134 |
|
} |
135 |
796
|
if (SPECIALCHARACTERS.indexOf(c) >= 0) { |
136 |
697
|
switch (c) { |
137 |
0
|
case '&': |
138 |
0
|
case '%': |
139 |
22
|
case '~': |
140 |
134
|
case '$': |
141 |
156
|
readChar(); |
142 |
156
|
continue; |
143 |
252
|
case '\\': |
144 |
252
|
final String t = readBackslashToken(); |
145 |
252
|
if (t.equals(" ") || t.equals("quad") || t.equals("qquad")) { |
146 |
34
|
continue; |
147 |
|
} |
148 |
218
|
token.append(t); |
149 |
218
|
if ('_' == getChar() || '^' == getChar()) { |
150 |
0
|
token.append((char) readChar()); |
151 |
0
|
continue; |
152 |
|
} |
153 |
218
|
break; |
154 |
0
|
case '{': |
155 |
0
|
readChar(); |
156 |
0
|
token.append("("); |
157 |
0
|
break; |
158 |
0
|
case '}': |
159 |
0
|
readChar(); |
160 |
0
|
token.append(")"); |
161 |
0
|
break; |
162 |
289
|
default: |
163 |
289
|
readChar(); |
164 |
289
|
token.append((char) c); |
165 |
289
|
if ('_' == getChar() || '^' == getChar()) { |
166 |
0
|
token.append((char) readChar()); |
167 |
0
|
continue; |
168 |
|
} |
169 |
|
} |
170 |
507
|
break; |
171 |
|
} |
172 |
99
|
token.append((char) readChar()); |
173 |
99
|
if ('_' == getChar() || '^' == getChar()) { |
174 |
8
|
token.append((char) readChar()); |
175 |
8
|
continue; |
176 |
|
} |
177 |
91
|
break; |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
198
|
} while (!eof()); |
199 |
658
|
Trace.param(CLASS, this, method, "return token", token); |
200 |
658
|
return (token != null ? token.toString() : null); |
201 |
|
} finally { |
202 |
658
|
Trace.end(CLASS, this, method); |
203 |
|
} |
204 |
|
} |
205 |
|
|
|
|
| 77,8% |
Uncovered Elements: 6 (27) |
Complexity: 6 |
Complexity Density: 0,32 |
|
206 |
252
|
private String readBackslashToken() {... |
207 |
252
|
final String method = "readBackslashToken()"; |
208 |
252
|
Trace.begin(CLASS, this, method); |
209 |
252
|
if (getChar() != '\\') { |
210 |
0
|
throw new IllegalArgumentException("\\ expected"); |
211 |
|
} |
212 |
252
|
readChar(); |
213 |
252
|
if (eof()) { |
214 |
0
|
Trace.param(CLASS, this, method, "return", null); |
215 |
0
|
Trace.end(CLASS, this, method); |
216 |
0
|
return null; |
217 |
|
} |
218 |
252
|
if (!Character.isLetter((char) getChar())) { |
219 |
34
|
Trace.param(CLASS, this, method, "return", (char) getChar()); |
220 |
34
|
Trace.end(CLASS, this, method); |
221 |
34
|
return "" + ((char) readChar()); |
222 |
|
} |
223 |
218
|
final StringBuffer buffer = new StringBuffer(); |
224 |
218
|
do { |
225 |
880
|
buffer.append((char) readChar()); |
226 |
880
|
} while (!eof() && Character.isLetter((char) getChar())); |
227 |
218
|
Trace.param(CLASS, this, method, "return", buffer.toString()); |
228 |
218
|
Trace.end(CLASS, this, method); |
229 |
218
|
return buffer.toString(); |
230 |
|
} |
231 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 4 |
Complexity Density: 0,67 |
|
232 |
836
|
private int readPureWhitespace() {... |
233 |
836
|
int lines = 0; |
234 |
1130
|
while (getChar() != -1 && Character.isWhitespace((char) getChar())) { |
235 |
294
|
if ('\n' == (char) getChar()) { |
236 |
60
|
lines++; |
237 |
|
} |
238 |
294
|
readChar(); |
239 |
|
} |
240 |
836
|
return lines; |
241 |
|
} |
242 |
|
|
|
|
| 81,8% |
Uncovered Elements: 4 (22) |
Complexity: 6 |
Complexity Density: 0,5 |
|
243 |
177
|
protected final Operator getOperator(final String token) {... |
244 |
177
|
Operator result = null; |
245 |
177
|
if (token == null) { |
246 |
0
|
return null; |
247 |
|
} |
248 |
3249
|
for (int i = 0; i < getOperators().size(); i++) { |
249 |
3156
|
if (token.equals(((Operator) getOperators().get(i)).getStartSymbol())) { |
250 |
84
|
result = (Operator) getOperators().get(i); |
251 |
84
|
break; |
252 |
|
} |
253 |
|
} |
254 |
177
|
if (result != null) { |
255 |
84
|
return result; |
256 |
|
} |
257 |
|
|
258 |
93
|
if (SPECIALCHARACTERS.indexOf(token) < 0) { |
259 |
0
|
return new Operator(token, null, null, "VAR", token, 200, 0, 0); |
260 |
|
} |
261 |
93
|
return null; |
262 |
|
} |
263 |
|
|
|
|
| 76,5% |
Uncovered Elements: 4 (17) |
Complexity: 6 |
Complexity Density: 0,67 |
|
264 |
140
|
protected final List getOperators(final String token) {... |
265 |
140
|
final List result = new ArrayList(); |
266 |
140
|
if (token == null) { |
267 |
0
|
return result; |
268 |
|
} |
269 |
3780
|
for (int i = 0; i < getOperators().size(); i++) { |
270 |
3640
|
if (token.equals(((Operator) getOperators().get(i)).getStartSymbol())) { |
271 |
102
|
result.add(getOperators().get(i)); |
272 |
|
} |
273 |
|
} |
274 |
|
|
275 |
140
|
if (result.size() <= 0 && SPECIALCHARACTERS.indexOf(token) < 0) { |
276 |
0
|
result.add(new Operator(token, null, null, "VAR", token, 200, 0, 0)); |
277 |
|
} |
278 |
140
|
return result; |
279 |
|
} |
280 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
281 |
162
|
protected boolean eot(final String token) {... |
282 |
162
|
return token == null || token.trim().length() == 0; |
283 |
|
} |
284 |
|
|
285 |
|
} |