|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Operator.java | 75% | 86% | 100% | 88,4% |
|
1 | /* $Id: Operator.java,v 1.3 2006/10/20 20:23:05 m31 Exp $ | |
2 | * | |
3 | * This file is part of the project "Hilbert II" - http://www.qedeq.org | |
4 | * | |
5 | * Copyright 2000-2006, Michael Meyling <mime@qedeq.org>. | |
6 | * | |
7 | * "Hilbert II" is free software; you can redistribute | |
8 | * it and/or modify it under the terms of the GNU General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | */ | |
17 | ||
18 | package org.qedeq.kernel.parser; | |
19 | ||
20 | ||
21 | /** | |
22 | * This class describes an term or logical operator. An operator is of either | |
23 | * prefix, infix or postfix type and has a minimum and maximum number of operands. | |
24 | * It has a symbol or token that enables to recognize it and a QEDEQ representation. | |
25 | * | |
26 | * @version $Revision: 1.3 $ | |
27 | * @author Michael Meyling | |
28 | */ | |
29 | public final class Operator { | |
30 | ||
31 | /** Marks infix operator. */ | |
32 | public static final int INFIX = 0; | |
33 | ||
34 | /** Marks prefix operator. */ | |
35 | public static final int SIMPLE_PREFIX = 1; | |
36 | ||
37 | /** Marks postfix operator. */ | |
38 | public static final int POSTFIX = 2; | |
39 | ||
40 | /** Marks function operator. */ | |
41 | public static final int FUNCTION = 4; | |
42 | ||
43 | /** Prefix, function, infix or postfix. See {@link #SIMPLE_PREFIX}, {@link #FUNCTION}, | |
44 | * {@link #INFIX} and {@link #POSTFIX}. */ | |
45 | private final int type; | |
46 | ||
47 | /** Start symbol or token for this operator. */ | |
48 | private final String startSymbol; | |
49 | ||
50 | /** Separator symbol or token for this operator. This could be a comma for example. */ | |
51 | private final String separatorSymbol; | |
52 | ||
53 | /** End symbol or token for this operator. */ | |
54 | private final String endSymbol; | |
55 | ||
56 | /** Operator priority. Highest is 0. */ | |
57 | private final int priority; | |
58 | ||
59 | /** QEDEQ token for this operator. */ | |
60 | private final String qedeq; | |
61 | ||
62 | /** First QEDEQ argument. Can be <code>null</code>. */ | |
63 | private final String qedeqArgument; | |
64 | ||
65 | /** Minimum number of arguments. */ | |
66 | private final int min; | |
67 | ||
68 | /** Maximum number of arguments. */ | |
69 | private final int max; | |
70 | ||
71 | ||
72 | /** | |
73 | * Constructor. | |
74 | * | |
75 | * @param symbol Symbol or token for this operator. | |
76 | * @param qedeq QEDEQ operator symbol. | |
77 | * @param qedeqArgument First Argument in QEDEQ-Syntax - if any. | |
78 | * @param priority Operator priority, highest is 0. | |
79 | * @param type Prefix, infix or postfix. See {@link #SIMPLE_PREFIX}, {@link #FUNCTION}, | |
80 | * {@link #INFIX} and {@link #POSTFIX}. | |
81 | * @param min Minimum number of arguments for this operator. | |
82 | */ | |
83 | 456 | public Operator(final String symbol, |
84 | final String qedeq, | |
85 | final String qedeqArgument, | |
86 | final int priority, | |
87 | final int type, | |
88 | final int min) { | |
89 | 456 | this(symbol, qedeq, qedeqArgument, priority, type, min, -1); |
90 | } | |
91 | ||
92 | /** | |
93 | * Constructor. | |
94 | * | |
95 | * @param symbol Symbol or token for this operator. | |
96 | * @param qedeq QEDEQ operator symbol. | |
97 | * @param qedeqArgument First Argument in QEDEQ-Syntax - if any. | |
98 | * @param priority Operator priority, highest is 0. | |
99 | * @param type Prefix, infix or postfix. See {@link #SIMPLE_PREFIX}, | |
100 | * {@link #FUNCTION}, | |
101 | * {@link #INFIX} and {@link #POSTFIX}. | |
102 | * @param min Minimum number of arguments for this operator. | |
103 | * @param max Maximum number of arguments for this operator. | |
104 | */ | |
105 | 1074 | public Operator(final String symbol, |
106 | final String qedeq, | |
107 | final String qedeqArgument, | |
108 | final int priority, | |
109 | final int type, | |
110 | final int min, | |
111 | final int max) { | |
112 | 1074 | this(symbol, null, null, qedeq, qedeqArgument, priority, type, min, max); |
113 | } | |
114 | ||
115 | /** | |
116 | * Constructor for prefix operators like <code>{x | x > 0}</code>. | |
117 | * | |
118 | * @param startSymbol Starting symbol or token for this operator. | |
119 | * @param separatorSymbol Symbol or token that separates arguments for this operator. | |
120 | * @param endSymbol End symbol or token for this operator. | |
121 | * @param qedeq QEDEQ operator symbol. | |
122 | * @param qedeqArgument First Argument in QEDEQ-Syntax - if any | |
123 | * @param priority Operator priority, highest is 0. | |
124 | * @param min Minimum number of arguments for this operator. | |
125 | */ | |
126 | 81 | public Operator(final String startSymbol, |
127 | final String separatorSymbol, | |
128 | final String endSymbol, | |
129 | final String qedeq, | |
130 | final String qedeqArgument, | |
131 | final int priority, | |
132 | final int min) { | |
133 | 81 | this(startSymbol, separatorSymbol, endSymbol, qedeq, qedeqArgument, priority, SIMPLE_PREFIX, |
134 | min, -1); | |
135 | } | |
136 | ||
137 | /** | |
138 | * Constructor for prefix operators like <code>{x, y, z}</code>. | |
139 | * | |
140 | * @param startSymbol Starting symbol or token for this operator. | |
141 | * @param separatorSymbol Symbol or token that separates arguments for this operator. | |
142 | * @param endSymbol End symbol or token for this operator. | |
143 | * @param qedeq QEDEQ operator symbol. | |
144 | * @param qedeqArgument First Argument in QEDEQ-Syntax - if any. | |
145 | * @param priority Operator priority, highest is 0. | |
146 | * @param min Minimum number of arguments for this operator. | |
147 | * @param max Maximum number of arguments for this operator. | |
148 | */ | |
149 | 81 | public Operator(final String startSymbol, |
150 | final String separatorSymbol, | |
151 | final String endSymbol, | |
152 | final String qedeq, | |
153 | final String qedeqArgument, | |
154 | final int priority, | |
155 | final int min, | |
156 | final int max) { | |
157 | 81 | this(startSymbol, separatorSymbol, endSymbol, qedeq, qedeqArgument, priority, SIMPLE_PREFIX, |
158 | min, max); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Constructor. | |
163 | * | |
164 | * @param startSymbol Starting symbol or token for this operator. | |
165 | * @param separatorSymbol Symbol or token that separates arguments for this operator. | |
166 | * @param endSymbol End symbol or token for this operator. | |
167 | * @param qedeq QEDEQ operator symbol. | |
168 | * @param qedeqArgument First Argument in QEDEQ-Syntax - if any. | |
169 | * @param priority Operator priority, highest is 0. | |
170 | * @param type Prefix, infix or postfix. See {@link #SIMPLE_PREFIX}, | |
171 | * {@link #FUNCTION}, {@link #INFIX} and {@link #POSTFIX}. | |
172 | * @param min Minimum number of arguments for this operator. | |
173 | * @param max Maximum number of arguments for this operator. | |
174 | */ | |
175 | 1236 | public Operator(final String startSymbol, final String separatorSymbol, final String endSymbol, |
176 | final String qedeq, | |
177 | final String qedeqArgument, | |
178 | final int priority, | |
179 | final int type, | |
180 | final int min, | |
181 | final int max) { | |
182 | 1236 | this.startSymbol = startSymbol; |
183 | 1236 | this.separatorSymbol = separatorSymbol; |
184 | 1236 | this.endSymbol = endSymbol; |
185 | 1236 | this.type = type; |
186 | 1236 | this.qedeq = qedeq; |
187 | 1236 | this.qedeqArgument = qedeqArgument; |
188 | 1236 | this.priority = priority; |
189 | 1236 | this.min = min; |
190 | 1236 | this.max = max; |
191 | 1236 | switch (type) { |
192 | 648 | case INFIX: |
193 | 588 | case SIMPLE_PREFIX: |
194 | 0 | case FUNCTION: |
195 | 0 | case POSTFIX: |
196 | 1236 | break; |
197 | 0 | default: |
198 | 0 | throw new IllegalArgumentException("unknown operator type: " |
199 | + type); | |
200 | } | |
201 | 1236 | if (max != -1 && min > max) { |
202 | 0 | throw new IllegalArgumentException("Min greater than max: " + min + " > " + max); |
203 | } | |
204 | 1236 | if (isInfix() && min < 2) { |
205 | 0 | throw new IllegalArgumentException("Infix needs at least two arguments"); |
206 | } | |
207 | } | |
208 | ||
209 | /** | |
210 | * Returns symbol or token to identify this operator. | |
211 | * | |
212 | * @return Symbol or token to identify a start of this operator. | |
213 | */ | |
214 | 124052 | public final String getStartSymbol() { |
215 | 124052 | return startSymbol; |
216 | } | |
217 | ||
218 | /** | |
219 | * Returns symbol or token to separate different arguments for this operator. Can only be | |
220 | * different from <code>null</code> if this is a prefix operator. | |
221 | * | |
222 | * @return Symbol or token to identify the start of a new argument of this operator. | |
223 | */ | |
224 | 728 | public String getSeparatorSymbol() { |
225 | 728 | return separatorSymbol; |
226 | } | |
227 | ||
228 | /** | |
229 | * Returns symbol or token to identify the end of this operator. Can only be different from | |
230 | * <code>null</code> if this is a prefix operator. | |
231 | * | |
232 | * @return Symbol or token to identify the end of this operator. Maybe <code>null</code>. | |
233 | */ | |
234 | 986 | public String getEndSymbol() { |
235 | 986 | return endSymbol; |
236 | } | |
237 | ||
238 | /** | |
239 | * Is this an infix operator? | |
240 | * | |
241 | * @return Is this an infix operator? | |
242 | */ | |
243 | 1236 | public final boolean isInfix() { |
244 | 1236 | return type == INFIX; |
245 | } | |
246 | ||
247 | /** | |
248 | * Is this a prefix operator? | |
249 | * | |
250 | * @return Is this a prefix operator? | |
251 | */ | |
252 | 932 | public final boolean isPrefix() { |
253 | 932 | return type == SIMPLE_PREFIX || type == FUNCTION; |
254 | } | |
255 | ||
256 | /** | |
257 | * Is this a function operator? | |
258 | * | |
259 | * @return Is this a function operator? | |
260 | */ | |
261 | 334 | public final boolean isFunction() { |
262 | 334 | return type == FUNCTION; |
263 | } | |
264 | ||
265 | /** | |
266 | * Is this a postfix operator? | |
267 | * | |
268 | * @return Is this a postfix operator? | |
269 | */ | |
270 | 561 | public final boolean isPostfix() { |
271 | 561 | return type == POSTFIX; |
272 | } | |
273 | ||
274 | /** | |
275 | * Get operator priority. 0 is the highest priority. | |
276 | * | |
277 | * @return Priority. | |
278 | */ | |
279 | 2192 | public final int getPriority() { |
280 | 2192 | return this.priority; |
281 | } | |
282 | ||
283 | /** | |
284 | * Get minimum argument number. | |
285 | * | |
286 | * @return Minimum argument number. | |
287 | */ | |
288 | 748 | public final int getMin() { |
289 | 748 | return this.min; |
290 | } | |
291 | ||
292 | /** | |
293 | * Get maximum argument number. | |
294 | * | |
295 | * @return Maximum argument number. | |
296 | */ | |
297 | 3104 | public final int getMax() { |
298 | 3104 | return max; |
299 | } | |
300 | ||
301 | /** | |
302 | * Get QEDEQ operator name. | |
303 | * | |
304 | * @return QEDEQ operator name. | |
305 | */ | |
306 | 10303 | public final String getQedeq() { |
307 | 10303 | return qedeq; |
308 | } | |
309 | ||
310 | /** | |
311 | * Get first QEDEQ argument. | |
312 | * | |
313 | * @return First QEDEQ argument. Can be <code>null</code>. | |
314 | */ | |
315 | 17129 | public final String getQedeqArgument() { |
316 | 17129 | return qedeqArgument; |
317 | } | |
318 | ||
319 | 342 | public final String toString() { |
320 | 342 | final StringBuffer buffer = new StringBuffer(getStartSymbol()); |
321 | 342 | if (getSeparatorSymbol() != null) { |
322 | 100 | buffer.append(" ").append(getSeparatorSymbol()); |
323 | } | |
324 | 342 | if (getEndSymbol() != null) { |
325 | 100 | buffer.append(" ").append(getEndSymbol()); |
326 | } | |
327 | 342 | return buffer.toString(); |
328 | } | |
329 | ||
330 | } | |
331 |
|