|
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.utility.TextInput; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| public final class SimpleMathParser extends MathParser { |
|
33 |
| |
|
34 |
| |
|
35 |
| private static final String SEPARATORS = "()[],{}"; |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
30
| public SimpleMathParser(final TextInput input, final List operators) {
|
|
44 |
30
| super(new MementoTextInput(input), operators);
|
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| } |
|
63 |
| |
|
64 |
1305
| protected final String readToken() {
|
|
65 |
1305
| int lines = 0;
|
|
66 |
1305
| while (getChar() != -1 && Character.isWhitespace((char) getChar())) {
|
|
67 |
872
| if ('\n' == (char) getChar()) {
|
|
68 |
198
| lines++;
|
|
69 |
| } |
|
70 |
872
| readChar();
|
|
71 |
| } |
|
72 |
1305
| if (lines > 1) {
|
|
73 |
13
| return "";
|
|
74 |
| } |
|
75 |
1292
| if (eof()) {
|
|
76 |
132
| return null;
|
|
77 |
| } |
|
78 |
1160
| if (SEPARATORS.indexOf(getChar()) >= 0) {
|
|
79 |
237
| System.out.println("Read token: " + (char) getChar());
|
|
80 |
237
| return "" + (char) readChar();
|
|
81 |
| } |
|
82 |
923
| final StringBuffer token = new StringBuffer();
|
|
83 |
923
| while (!eof() && !Character.isWhitespace((char) getChar())
|
|
84 |
| && SEPARATORS.indexOf(getChar()) < 0) { |
|
85 |
1059
| token.append((char) readChar());
|
|
86 |
1059
| if (null != getOperator(token.toString())) {
|
|
87 |
252
| if (getChar() >= 0) {
|
|
88 |
252
| final char c = (char) getChar();
|
|
89 |
252
| if (null != getOperator(token.toString() + c)) {
|
|
90 |
30
| continue;
|
|
91 |
| } |
|
92 |
| } |
|
93 |
222
| break;
|
|
94 |
| } |
|
95 |
| } |
|
96 |
923
| System.out.println("Read token: " + token.toString());
|
|
97 |
923
| return token.toString();
|
|
98 |
| } |
|
99 |
| |
|
100 |
1632
| protected final Operator getOperator(final String token) {
|
|
101 |
1632
| Operator result = null;
|
|
102 |
1632
| if (token == null) {
|
|
103 |
0
| return result;
|
|
104 |
| } |
|
105 |
1632
| for (int i = 0; i < getOperators().size(); i++) {
|
|
106 |
18684
| if (token.equals(((Operator) getOperators().get(i)).getStartSymbol())) {
|
|
107 |
472
| result = (Operator) getOperators().get(i);
|
|
108 |
472
| break;
|
|
109 |
| } |
|
110 |
| } |
|
111 |
1632
| return result;
|
|
112 |
| } |
|
113 |
| |
|
114 |
256
| protected final List getOperators(final String token) {
|
|
115 |
256
| final List result = new ArrayList();
|
|
116 |
256
| if (token == null) {
|
|
117 |
0
| return result;
|
|
118 |
| } |
|
119 |
256
| for (int i = 0; i < getOperators().size(); i++) {
|
|
120 |
3584
| if (token.equals(((Operator) getOperators().get(i)).getStartSymbol())) {
|
|
121 |
32
| result.add(getOperators().get(i));
|
|
122 |
| } |
|
123 |
| } |
|
124 |
256
| return result;
|
|
125 |
| } |
|
126 |
| |
|
127 |
320
| protected boolean eot(final String token) {
|
|
128 |
320
| return token == null || token.trim().length() == 0;
|
|
129 |
| } |
|
130 |
| |
|
131 |
| } |