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