|
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 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 |
1537
| protected final String readToken() {
|
|
65 |
1537
| int lines = 0;
|
|
66 |
1537
| while (getChar() != -1 && Character.isWhitespace((char) getChar())) {
|
|
67 |
1038
| if ('\n' == (char) getChar()) {
|
|
68 |
218
| lines++;
|
|
69 |
| } |
|
70 |
1038
| readChar();
|
|
71 |
| } |
|
72 |
1537
| if (lines > 1) {
|
|
73 |
13
| return "";
|
|
74 |
| } |
|
75 |
1524
| if (eof()) {
|
|
76 |
132
| return null;
|
|
77 |
| } |
|
78 |
1392
| if (SEPARATORS.indexOf(getChar()) >= 0) {
|
|
79 |
265
| return "" + (char) readChar();
|
|
80 |
| } |
|
81 |
1127
| final StringBuffer token = new StringBuffer();
|
|
82 |
1127
| while (!eof() && !Character.isWhitespace((char) getChar())
|
|
83 |
| && SEPARATORS.indexOf(getChar()) < 0) { |
|
84 |
1267
| token.append((char) readChar());
|
|
85 |
1267
| if (null != getOperator(token.toString())) {
|
|
86 |
252
| if (getChar() >= 0) {
|
|
87 |
252
| final char c = (char) getChar();
|
|
88 |
252
| if (null != getOperator(token.toString() + c)) {
|
|
89 |
30
| continue;
|
|
90 |
| } |
|
91 |
| } |
|
92 |
222
| break;
|
|
93 |
| } |
|
94 |
| } |
|
95 |
1127
| return token.toString();
|
|
96 |
| } |
|
97 |
| |
|
98 |
1840
| protected final Operator getOperator(final String token) {
|
|
99 |
1840
| Operator result = null;
|
|
100 |
1840
| if (token == null) {
|
|
101 |
0
| return result;
|
|
102 |
| } |
|
103 |
1840
| for (int i = 0; i < getOperators().size(); i++) {
|
|
104 |
21596
| if (token.equals(((Operator) getOperators().get(i)).getStartSymbol())) {
|
|
105 |
472
| result = (Operator) getOperators().get(i);
|
|
106 |
472
| break;
|
|
107 |
| } |
|
108 |
| } |
|
109 |
1840
| return result;
|
|
110 |
| } |
|
111 |
| |
|
112 |
256
| protected final List getOperators(final String token) {
|
|
113 |
256
| final List result = new ArrayList();
|
|
114 |
256
| if (token == null) {
|
|
115 |
0
| return result;
|
|
116 |
| } |
|
117 |
256
| for (int i = 0; i < getOperators().size(); i++) {
|
|
118 |
3584
| if (token.equals(((Operator) getOperators().get(i)).getStartSymbol())) {
|
|
119 |
32
| result.add(getOperators().get(i));
|
|
120 |
| } |
|
121 |
| } |
|
122 |
256
| return result;
|
|
123 |
| } |
|
124 |
| |
|
125 |
320
| protected boolean eot(final String token) {
|
|
126 |
320
| return token == null || token.trim().length() == 0;
|
|
127 |
| } |
|
128 |
| |
|
129 |
| } |