|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| package org.qedeq.kernel.parser; |
|
19 |
| |
|
20 |
| import java.util.ArrayList; |
|
21 |
| import java.util.List; |
|
22 |
| |
|
23 |
| import org.qedeq.kernel.log.Trace; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| public abstract class MathParser { |
|
32 |
| |
|
33 |
| |
|
34 |
| private final MementoTextInput input; |
|
35 |
| |
|
36 |
| |
|
37 |
| private List operators; |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
81
| public MathParser(final MementoTextInput input, final List operators) {
|
|
46 |
81
| this.input = input;
|
|
47 |
81
| this.operators = operators;
|
|
48 |
| } |
|
49 |
| |
|
50 |
257057
| protected final List getOperators() {
|
|
51 |
257057
| return operators;
|
|
52 |
| } |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
161
| public final Term readTerm() throws ParserException {
|
|
61 |
161
| final String method = "Term readTerm()";
|
|
62 |
161
| Trace.begin(this, method);
|
|
63 |
161
| try {
|
|
64 |
161
| final Term term = readMaximalTerm(0);
|
|
65 |
161
| if (eot(getToken())) {
|
|
66 |
86
| readToken();
|
|
67 |
| } |
|
68 |
161
| return term;
|
|
69 |
| } finally { |
|
70 |
161
| Trace.end(this, method);
|
|
71 |
| } |
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
1530
| private final Term readMaximalTerm(final int priority) throws ParserException {
|
|
85 |
1530
| final String method = "Term readMaximalTerm(int)";
|
|
86 |
1530
| Trace.begin(this, method);
|
|
87 |
1530
| Term term = null;
|
|
88 |
1530
| try {
|
|
89 |
1530
| if (eot(getToken())) {
|
|
90 |
6
| Trace.param(this, method, "read term", "null");
|
|
91 |
6
| return null;
|
|
92 |
| } |
|
93 |
1524
| term = readPrefixTerm();
|
|
94 |
1524
| term = addNextInfixTerms(priority, term);
|
|
95 |
1524
| Trace.param(this, method, "read term",
|
|
96 |
1524
| (term != null ? term.getQedeq() : "null"));
|
|
97 |
1524
| return term;
|
|
98 |
| } finally { |
|
99 |
1530
| Trace.end(this, method);
|
|
100 |
| } |
|
101 |
| } |
|
102 |
| |
|
103 |
1524
| private Term addNextInfixTerms(final int priority, final Term initialTerm)
|
|
104 |
| throws ParserException { |
|
105 |
1524
| final String method = "Term addNextInfixTerms(int, Term)";
|
|
106 |
1524
| Trace.begin(this, method);
|
|
107 |
1524
| Term term = initialTerm;
|
|
108 |
1524
| try {
|
|
109 |
1524
| Operator newOperator = null;
|
|
110 |
1524
| Operator oldOperator = null;
|
|
111 |
| |
|
112 |
1524
| do {
|
|
113 |
2085
| markPosition();
|
|
114 |
2085
| newOperator = readOperator();
|
|
115 |
2085
| Trace.param(this, method, "newOperator",
|
|
116 |
2085
| (newOperator != null ? newOperator.getQedeq() : "null"));
|
|
117 |
2085
| if (newOperator == null || newOperator.getPriority() <= priority) {
|
|
118 |
1487
| Trace.trace(this, method, "newOperator is null or of less priority");
|
|
119 |
1487
| rewindPosition();
|
|
120 |
1487
| Trace.param(this, method, "read term",
|
|
121 |
1487
| (term != null ? term.getQedeq() : "null"));
|
|
122 |
1487
| return term;
|
|
123 |
| } |
|
124 |
598
| if (newOperator.isPrefix()) {
|
|
125 |
37
| Trace.trace(this, method, "newOperator is prefix");
|
|
126 |
| |
|
127 |
37
| rewindPosition();
|
|
128 |
37
| Trace.param(this, method, "read term",
|
|
129 |
37
| (term != null ? term.getQedeq() : "null"));
|
|
130 |
37
| return term;
|
|
131 |
| } |
|
132 |
561
| if (newOperator.isPostfix()) {
|
|
133 |
0
| rewindPosition();
|
|
134 |
0
| throw new IllegalArgumentException("Postfix Operators not yet supported");
|
|
135 |
| } |
|
136 |
561
| clearMark();
|
|
137 |
561
| if (oldOperator == null || oldOperator.getPriority() >= newOperator.getPriority()) {
|
|
138 |
561
| Trace.trace(this, method, "oldOperator is null or has more priority than new");
|
|
139 |
561
| Term term2 = readMaximalTerm(newOperator.getPriority());
|
|
140 |
561
| if (term2 == null) {
|
|
141 |
| |
|
142 |
0
| throw new TooFewArgumentsException(getPosition(), 2);
|
|
143 |
| } |
|
144 |
561
| if (oldOperator == newOperator) {
|
|
145 |
28
| if (oldOperator.getMax() != -1 && term.size() + 1 >= oldOperator.getMax()) {
|
|
146 |
0
| throw new TooMuchArgumentsException(getPosition(),
|
|
147 |
| oldOperator.getMax() + 2); |
|
148 |
| } |
|
149 |
28
| Trace.trace(this, method, "new term is added to old term");
|
|
150 |
28
| term.addArgument(term2);
|
|
151 |
| } else { |
|
152 |
| |
|
153 |
533
| Trace.trace(this, method, "old term is first argument of new operator");
|
|
154 |
533
| term = new Term(newOperator, term);
|
|
155 |
533
| term.addArgument(term2);
|
|
156 |
| } |
|
157 |
| } else { |
|
158 |
0
| Trace.trace(this, method,
|
|
159 |
| "oldOperator is not null or has less priority than new"); |
|
160 |
0
| Term term2 = readMaximalTerm(newOperator.getPriority());
|
|
161 |
0
| if (term2 == null) {
|
|
162 |
| |
|
163 |
0
| throw new TooFewArgumentsException(getPosition(), 2);
|
|
164 |
| } |
|
165 |
0
| term = new Term(newOperator, term);
|
|
166 |
0
| term.addArgument(term2);
|
|
167 |
| } |
|
168 |
561
| oldOperator = newOperator;
|
|
169 |
| } while (true); |
|
170 |
| } finally { |
|
171 |
1524
| Trace.end(this, method);
|
|
172 |
| } |
|
173 |
| } |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
1524
| private final Term readPrefixTerm() throws ParserException {
|
|
183 |
1524
| final String method = "Term readPrefixTerm()";
|
|
184 |
1524
| Trace.begin(this, method);
|
|
185 |
1524
| Term term = null;
|
|
186 |
1524
| try {
|
|
187 |
1524
| final List readOperators = readOperators();
|
|
188 |
1524
| if (readOperators != null && readOperators.size() > 0) {
|
|
189 |
292
| Trace.trace(this, method, "operators found");
|
|
190 |
292
| term = readPrefixOperator(readOperators);
|
|
191 |
| } else { |
|
192 |
1232
| Trace.trace(this, method, "no operators found");
|
|
193 |
1232
| final String token;
|
|
194 |
1232
| token = readToken();
|
|
195 |
1232
| if (token == null) {
|
|
196 |
0
| Trace.param(this, method, "read term", "null");
|
|
197 |
0
| return null;
|
|
198 |
| } |
|
199 |
1232
| if ("(".equals(token)) {
|
|
200 |
334
| Trace.trace(this, method, "start bracket found: " + token);
|
|
201 |
334
| term = readMaximalTerm(0);
|
|
202 |
334
| final String lastToken = readToken();
|
|
203 |
334
| if (!")".equals(lastToken)) {
|
|
204 |
0
| throw new ClosingBracketMissingException(getPosition(), ")");
|
|
205 |
| } |
|
206 |
| |
|
207 |
898
| } else if ("[".equals(token)) {
|
|
208 |
0
| Trace.trace(this, method, "start bracket found: " + token);
|
|
209 |
0
| term = readMaximalTerm(0);
|
|
210 |
0
| final String lastToken = readToken();
|
|
211 |
0
| if (!"]".equals(lastToken)) {
|
|
212 |
0
| throw new ClosingBracketMissingException(getPosition(), "]");
|
|
213 |
| } |
|
214 |
| } else { |
|
215 |
898
| Trace.param(this, method, "atom", token);
|
|
216 |
898
| term = new Term(new Atom(token));
|
|
217 |
| } |
|
218 |
| } |
|
219 |
1524
| Trace.param(this, method, "read term",
|
|
220 |
1524
| (term != null ? term.getQedeq() : "null"));
|
|
221 |
1524
| return term;
|
|
222 |
| } finally { |
|
223 |
1524
| Trace.end(this, method);
|
|
224 |
| } |
|
225 |
| } |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
292
| private Term readPrefixOperator(final List operators) throws ParserException {
|
|
236 |
292
| Term term = null;
|
|
237 |
292
| markPosition();
|
|
238 |
334
| for (int number = 0; number < operators.size(); number++) {
|
|
239 |
334
| rewindPosition();
|
|
240 |
334
| markPosition();
|
|
241 |
334
| Operator operator = (Operator) operators.get(number);
|
|
242 |
334
| if (!operator.isPrefix()) {
|
|
243 |
0
| clearMark();
|
|
244 |
0
| throw new UnexpectedOperatorException(getPosition(), operator);
|
|
245 |
| } |
|
246 |
334
| term = new Term(operator);
|
|
247 |
334
| if (operator.isFunction()) {
|
|
248 |
0
| List list = readTupel();
|
|
249 |
0
| if (list == null) {
|
|
250 |
0
| list = new ArrayList();
|
|
251 |
| } |
|
252 |
0
| if (list.size() < operator.getMin()) {
|
|
253 |
0
| if (number + 1 < operators.size()) {
|
|
254 |
0
| continue;
|
|
255 |
| } |
|
256 |
0
| clearMark();
|
|
257 |
0
| throw new TooFewArgumentsException(getPosition(),
|
|
258 |
| operator.getMin()); |
|
259 |
| } |
|
260 |
0
| if (operator.getMax() != -1 && list.size() > operator.getMax()) {
|
|
261 |
0
| if (number + 1 < operators.size()) {
|
|
262 |
0
| continue;
|
|
263 |
| } |
|
264 |
0
| clearMark();
|
|
265 |
0
| throw new TooMuchArgumentsException(getPosition(),
|
|
266 |
| operator.getMax()); |
|
267 |
| } |
|
268 |
0
| for (int i = 0; i < list.size(); i++) {
|
|
269 |
0
| term.addArgument((Term) list.get(i));
|
|
270 |
| } |
|
271 |
0
| break;
|
|
272 |
| } |
|
273 |
334
| int i = 0;
|
|
274 |
334
| while (i < operator.getMin()) {
|
|
275 |
414
| if (i > 0 && operator.getSeparatorSymbol() != null) {
|
|
276 |
42
| final String separator = getToken();
|
|
277 |
42
| if (!operator.getSeparatorSymbol().equals(separator)) {
|
|
278 |
0
| if (number + 1 < operators.size()) {
|
|
279 |
0
| continue;
|
|
280 |
| } |
|
281 |
0
| clearMark();
|
|
282 |
0
| throw new SeparatorNotFoundException(getPosition(),
|
|
283 |
| operator.getSeparatorSymbol()); |
|
284 |
| } |
|
285 |
42
| readToken();
|
|
286 |
| } |
|
287 |
414
| final Term add = readMaximalTerm(operator.getPriority());
|
|
288 |
414
| if (add == null) {
|
|
289 |
0
| if (number + 1 < operators.size()) {
|
|
290 |
0
| continue;
|
|
291 |
| } |
|
292 |
0
| clearMark();
|
|
293 |
0
| throw new TooFewArgumentsException(getPosition(), operator.getMin());
|
|
294 |
| } |
|
295 |
414
| term.addArgument(add);
|
|
296 |
414
| i++;
|
|
297 |
| } |
|
298 |
334
| while (operator.getMax() == -1 || i < operator.getMax()) {
|
|
299 |
110
| if (i > 0 && operator.getSeparatorSymbol() != null) {
|
|
300 |
54
| final String separator = getToken();
|
|
301 |
54
| if (!operator.getSeparatorSymbol().equals(separator)) {
|
|
302 |
48
| break;
|
|
303 |
| } |
|
304 |
6
| readToken();
|
|
305 |
| } |
|
306 |
62
| if (operator.getEndSymbol() != null) {
|
|
307 |
56
| final String end = getToken();
|
|
308 |
56
| if (operator.getEndSymbol().equals(end)) {
|
|
309 |
2
| break;
|
|
310 |
| } |
|
311 |
| } |
|
312 |
60
| final Term add = readMaximalTerm(operator.getPriority());
|
|
313 |
60
| if (add == null) {
|
|
314 |
4
| break;
|
|
315 |
| } |
|
316 |
56
| term.addArgument(add);
|
|
317 |
56
| i++;
|
|
318 |
| } |
|
319 |
334
| if (operator.getEndSymbol() != null) {
|
|
320 |
92
| final String end = getToken();
|
|
321 |
92
| if (!operator.getEndSymbol().equals(end)) {
|
|
322 |
42
| if (number + 1 < operators.size()) {
|
|
323 |
42
| continue;
|
|
324 |
| } |
|
325 |
0
| clearMark();
|
|
326 |
0
| throw new EndSymbolNotFoundException(getPosition(),
|
|
327 |
| operator.getEndSymbol()); |
|
328 |
| } |
|
329 |
50
| readToken();
|
|
330 |
| } |
|
331 |
292
| break;
|
|
332 |
| } |
|
333 |
292
| clearMark();
|
|
334 |
292
| return term;
|
|
335 |
| } |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
0
| private final List readTupel() throws ParserException {
|
|
345 |
0
| final String method = "List readTupel()";
|
|
346 |
0
| Trace.begin(this, method);
|
|
347 |
0
| try {
|
|
348 |
0
| final String firstToken;
|
|
349 |
0
| firstToken = getToken();
|
|
350 |
0
| if (!"(".equals(firstToken)) {
|
|
351 |
0
| Trace.trace(this, method, "no start bracket found");
|
|
352 |
0
| return null;
|
|
353 |
| } |
|
354 |
0
| readToken();
|
|
355 |
0
| List list = new ArrayList();
|
|
356 |
0
| Term term = null;
|
|
357 |
0
| while (null != (term = readMaximalTerm(0))) {
|
|
358 |
0
| list.add(term);
|
|
359 |
0
| final String separatorToken = getToken();
|
|
360 |
0
| if (!",".equals(separatorToken)) {
|
|
361 |
0
| break;
|
|
362 |
| } |
|
363 |
0
| readToken();
|
|
364 |
| } |
|
365 |
0
| final String lastToken = readToken();
|
|
366 |
0
| if (!")".equals(lastToken)) {
|
|
367 |
0
| throw new ClosingBracketMissingException(getPosition(), ")");
|
|
368 |
| } |
|
369 |
0
| return list;
|
|
370 |
| } finally { |
|
371 |
0
| Trace.end(this, method);
|
|
372 |
| } |
|
373 |
| } |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
2085
| private final Operator readOperator() {
|
|
381 |
2085
| final String method = "Operator readOperator()";
|
|
382 |
2085
| Trace.begin(this, method);
|
|
383 |
| |
|
384 |
2085
| try {
|
|
385 |
2085
| markPosition();
|
|
386 |
2085
| final String token;
|
|
387 |
2085
| token = readToken();
|
|
388 |
2085
| if (token == null) {
|
|
389 |
194
| rewindPosition();
|
|
390 |
194
| Trace.trace(this, method, "no operator found");
|
|
391 |
194
| return null;
|
|
392 |
| } |
|
393 |
1891
| final Operator operator = getOperator(token);
|
|
394 |
1891
| if (operator == null) {
|
|
395 |
1024
| rewindPosition();
|
|
396 |
1024
| Trace.trace(this, method, "no operator found");
|
|
397 |
1024
| return null;
|
|
398 |
| } |
|
399 |
867
| clearMark();
|
|
400 |
867
| Trace.param(this, method, "operator", operator.getQedeq());
|
|
401 |
867
| return operator;
|
|
402 |
| } finally { |
|
403 |
2085
| Trace.end(this, method);
|
|
404 |
| } |
|
405 |
| } |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
1524
| private final List readOperators() {
|
|
415 |
1524
| final String method = "List readOperators()";
|
|
416 |
1524
| Trace.begin(this, method);
|
|
417 |
| |
|
418 |
1524
| try {
|
|
419 |
1524
| markPosition();
|
|
420 |
1524
| final String token;
|
|
421 |
1524
| token = readToken();
|
|
422 |
1524
| if (token == null) {
|
|
423 |
0
| rewindPosition();
|
|
424 |
0
| Trace.trace(this, method, "no operators found");
|
|
425 |
0
| return null;
|
|
426 |
| } |
|
427 |
1524
| final List operators = getOperators(token);
|
|
428 |
1524
| if (operators == null || operators.size() == 0) {
|
|
429 |
1232
| rewindPosition();
|
|
430 |
1232
| Trace.trace(this, method, "no operators found");
|
|
431 |
1232
| return null;
|
|
432 |
| } |
|
433 |
292
| clearMark();
|
|
434 |
292
| for (int i = 0; i < operators.size(); i++) {
|
|
435 |
342
| Trace.param(this, method, "operator[" + i + "]", operators.get(i));
|
|
436 |
| } |
|
437 |
292
| return operators;
|
|
438 |
| } finally { |
|
439 |
1524
| Trace.end(this, method);
|
|
440 |
| } |
|
441 |
| } |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| protected abstract Operator getOperator(String token); |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| protected abstract List getOperators(String token); |
|
460 |
| |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
| protected abstract String readToken(); |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
| |
|
479 |
1935
| public final String getToken() {
|
|
480 |
1935
| markPosition();
|
|
481 |
1935
| final String result = readToken();
|
|
482 |
1935
| rewindPosition();
|
|
483 |
1935
| return result;
|
|
484 |
| } |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
12966
| protected final void markPosition() {
|
|
490 |
12966
| input.markPosition();
|
|
491 |
| } |
|
492 |
| |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
| |
|
497 |
| |
|
498 |
7378
| protected final long rewindPosition() {
|
|
499 |
7378
| return input.rewindPosition();
|
|
500 |
| } |
|
501 |
| |
|
502 |
| |
|
503 |
| |
|
504 |
| |
|
505 |
5588
| protected final void clearMark() {
|
|
506 |
5588
| input.clearMark();
|
|
507 |
| } |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
0
| private long getPosition() {
|
|
515 |
0
| return input.getPosition();
|
|
516 |
| } |
|
517 |
| |
|
518 |
| |
|
519 |
| |
|
520 |
| |
|
521 |
| |
|
522 |
| |
|
523 |
| |
|
524 |
| |
|
525 |
50909
| protected final int getChar() {
|
|
526 |
50909
| return input.getChar();
|
|
527 |
| } |
|
528 |
| |
|
529 |
| |
|
530 |
| |
|
531 |
| |
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| |
|
536 |
11360
| protected final int readChar() {
|
|
537 |
11360
| return input.readChar();
|
|
538 |
| } |
|
539 |
| |
|
540 |
| |
|
541 |
| |
|
542 |
| |
|
543 |
| |
|
544 |
| |
|
545 |
| |
|
546 |
| protected abstract boolean eot(String token); |
|
547 |
| |
|
548 |
| |
|
549 |
| |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
17182
| public final boolean eof() {
|
|
554 |
17182
| return input.eof();
|
|
555 |
| } |
|
556 |
| |
|
557 |
| |
|
558 |
| |
|
559 |
| |
|
560 |
| |
|
561 |
| |
|
562 |
79
| public final int getRewindStackSize() {
|
|
563 |
79
| return input.getRewindStackSize();
|
|
564 |
| } |
|
565 |
| |
|
566 |
| } |