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