|
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 |
| import org.qedeq.kernel.utility.TextInput; |
|
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 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| public final class LatexMathParser extends MathParser { |
|
79 |
| |
|
80 |
| |
|
81 |
| private static final String SPECIALCHARACTERS = "(),{}\\~%$&"; |
|
82 |
| |
|
83 |
| |
|
84 |
| private int tokenWhiteSpaceLines; |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
0
| public LatexMathParser(final TextInput input, final List operators) {
|
|
93 |
0
| super(new MementoTextInput(input), operators);
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
0
| public LatexMathParser(final StringBuffer buffer, final List operators) {
|
|
104 |
0
| this(new TextInput(buffer), operators);
|
|
105 |
| } |
|
106 |
| |
|
107 |
0
| protected final String readToken() {
|
|
108 |
0
| final String method = "readToken()";
|
|
109 |
0
| Trace.begin(this, method);
|
|
110 |
0
| StringBuffer token = new StringBuffer();
|
|
111 |
0
| tokenWhiteSpaceLines = 0;
|
|
112 |
0
| try {
|
|
113 |
0
| do {
|
|
114 |
0
| tokenWhiteSpaceLines += readPureWhitespace();
|
|
115 |
0
| if (tokenWhiteSpaceLines > 1) {
|
|
116 |
0
| break;
|
|
117 |
| } |
|
118 |
0
| if (eof()) {
|
|
119 |
0
| if (token.length() <= 0) {
|
|
120 |
0
| token = null;
|
|
121 |
| } |
|
122 |
0
| break;
|
|
123 |
| } |
|
124 |
0
| final int c = getChar();
|
|
125 |
0
| if (Character.isDigit((char) c)) {
|
|
126 |
0
| token.append((char) readChar());
|
|
127 |
0
| if (Character.isDigit((char) getChar())) {
|
|
128 |
0
| continue;
|
|
129 |
| } |
|
130 |
0
| break;
|
|
131 |
| } |
|
132 |
0
| if (SPECIALCHARACTERS.indexOf(c) >= 0) {
|
|
133 |
0
| switch (c) {
|
|
134 |
0
| case '&':
|
|
135 |
0
| case '%':
|
|
136 |
0
| case '~':
|
|
137 |
0
| case '$':
|
|
138 |
0
| readChar();
|
|
139 |
0
| continue;
|
|
140 |
0
| case '\\':
|
|
141 |
0
| final String t = readBackslashToken();
|
|
142 |
0
| if (t.equals(" ") || t.equals("quad") || t.equals("qquad")) {
|
|
143 |
0
| continue;
|
|
144 |
| } |
|
145 |
0
| token.append(t);
|
|
146 |
0
| if ('_' == getChar() || '^' == getChar()) {
|
|
147 |
0
| token.append((char) readChar());
|
|
148 |
0
| continue;
|
|
149 |
| } |
|
150 |
0
| break;
|
|
151 |
0
| case '{':
|
|
152 |
0
| readChar();
|
|
153 |
0
| token.append("(");
|
|
154 |
0
| break;
|
|
155 |
0
| case '}':
|
|
156 |
0
| readChar();
|
|
157 |
0
| token.append(")");
|
|
158 |
0
| break;
|
|
159 |
0
| default:
|
|
160 |
0
| readChar();
|
|
161 |
0
| token.append((char) c);
|
|
162 |
0
| if ('_' == getChar() || '^' == getChar()) {
|
|
163 |
0
| token.append((char) readChar());
|
|
164 |
0
| continue;
|
|
165 |
| } |
|
166 |
| } |
|
167 |
0
| break;
|
|
168 |
| } |
|
169 |
0
| token.append((char) readChar());
|
|
170 |
0
| if ('_' == getChar() || '^' == getChar()) {
|
|
171 |
0
| token.append((char) readChar());
|
|
172 |
0
| continue;
|
|
173 |
| } |
|
174 |
0
| break;
|
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
0
| } while (!eof());
|
|
196 |
0
| Trace.param(this, method, "Read token", token);
|
|
197 |
0
| return (token != null ? token.toString() : null);
|
|
198 |
| } finally { |
|
199 |
0
| Trace.end(this, method);
|
|
200 |
| } |
|
201 |
| } |
|
202 |
| |
|
203 |
0
| private String readBackslashToken() {
|
|
204 |
0
| final String method = "readBackslashToken()";
|
|
205 |
0
| Trace.begin(this, method);
|
|
206 |
0
| if (getChar() != '\\') {
|
|
207 |
0
| throw new IllegalArgumentException("\\ expected");
|
|
208 |
| } |
|
209 |
0
| readChar();
|
|
210 |
0
| if (eof()) {
|
|
211 |
0
| Trace.param(this, method, "return", null);
|
|
212 |
0
| Trace.end(this, method);
|
|
213 |
0
| return null;
|
|
214 |
| } |
|
215 |
0
| if (!Character.isLetter((char) getChar())) {
|
|
216 |
0
| Trace.param(this, method, "return", (char) getChar());
|
|
217 |
0
| Trace.end(this, method);
|
|
218 |
0
| return "" + ((char) readChar());
|
|
219 |
| } |
|
220 |
0
| final StringBuffer buffer = new StringBuffer();
|
|
221 |
0
| do {
|
|
222 |
0
| buffer.append((char) readChar());
|
|
223 |
0
| } while (!eof() && Character.isLetter((char) getChar()));
|
|
224 |
0
| Trace.param(this, method, "return", buffer.toString());
|
|
225 |
0
| Trace.end(this, method);
|
|
226 |
0
| return buffer.toString();
|
|
227 |
| } |
|
228 |
| |
|
229 |
0
| private int readPureWhitespace() {
|
|
230 |
0
| int lines = 0;
|
|
231 |
0
| while (getChar() != -1 && Character.isWhitespace((char) getChar())) {
|
|
232 |
0
| if ('\n' == (char) getChar()) {
|
|
233 |
0
| lines++;
|
|
234 |
| } |
|
235 |
0
| readChar();
|
|
236 |
| } |
|
237 |
0
| return lines;
|
|
238 |
| } |
|
239 |
| |
|
240 |
0
| protected final Operator getOperator(final String token) {
|
|
241 |
0
| Operator result = null;
|
|
242 |
0
| if (token == null) {
|
|
243 |
0
| return result;
|
|
244 |
| } |
|
245 |
0
| for (int i = 0; i < getOperators().size(); i++) {
|
|
246 |
0
| if (token.equals(((Operator) getOperators().get(i)).getStartSymbol())) {
|
|
247 |
0
| result = (Operator) getOperators().get(i);
|
|
248 |
0
| break;
|
|
249 |
| } |
|
250 |
| } |
|
251 |
0
| return result;
|
|
252 |
| } |
|
253 |
| |
|
254 |
0
| protected final List getOperators(final String token) {
|
|
255 |
0
| final List result = new ArrayList();
|
|
256 |
0
| if (token == null) {
|
|
257 |
0
| return result;
|
|
258 |
| } |
|
259 |
0
| for (int i = 0; i < getOperators().size(); i++) {
|
|
260 |
0
| if (token.equals(((Operator) getOperators().get(i)).getStartSymbol())) {
|
|
261 |
0
| result.add(getOperators().get(i));
|
|
262 |
| } |
|
263 |
| } |
|
264 |
0
| return result;
|
|
265 |
| } |
|
266 |
| |
|
267 |
0
| protected boolean eot(final String token) {
|
|
268 |
0
| return token == null || token.trim().length() == 0;
|
|
269 |
| } |
|
270 |
| |
|
271 |
| } |