01: package org.ofbiz.rules.parse.tokens;
02:
03: import java.io.*;
04:
05: /**
06: * <p><b>Title:</b> Symbol State
07: * <p><b>Description:</b> None
08: * <p>Copyright (c) 1999 Steven J. Metsker.
09: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
10: *
11: * <p>Permission is hereby granted, free of charge, to any person obtaining a
12: * copy of this software and associated documentation files (the "Software"),
13: * to deal in the Software without restriction, including without limitation
14: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15: * and/or sell copies of the Software, and to permit persons to whom the
16: * Software is furnished to do so, subject to the following conditions:
17: *
18: * <p>The above copyright notice and this permission notice shall be included
19: * in all copies or substantial portions of the Software.
20: *
21: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
25: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
26: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
27: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28: *
29: * <br>
30: * The idea of a symbol is a character that stands on its
31: * own, such as an ampersand or a parenthesis. For example,
32: * when tokenizing the expression <code>(isReady)&
33: * (isWilling) </code>, a typical tokenizer would return 7
34: * tokens, including one for each parenthesis and one for
35: * the ampersand. Thus a series of symbols such as
36: * <code>)&( </code> becomes three tokens, while a series
37: * of letters such as <code>isReady</code> becomes a single
38: * word token.
39: * <p>
40: * Multi-character symbols are an exception to the rule
41: * that a symbol is a standalone character. For example, a
42: * tokenizer may want less-than-or-equals to tokenize as a
43: * single token. This class provides a method for
44: * establishing which multi-character symbols an object of
45: * this class should treat as single symbols. This allows,
46: * for example, <code>"cat <= dog"</code> to tokenize as
47: * three tokens, rather than splitting the less-than and
48: * equals symbols into separate tokens.
49: * <p>
50: * By default, this state recognizes the following multi-
51: * character symbols: <code>!=, :-, <=, >=</code>
52: *
53: * @author Steven J. Metsker
54: * @version 1.0
55: */
56: public class SymbolState extends TokenizerState {
57: SymbolRootNode symbols = new SymbolRootNode();
58:
59: /**
60: * Constructs a symbol state with a default idea of what
61: * multi-character symbols to accept (as described in the
62: * class comment).
63: *
64: * @return a state for recognizing a symbol
65: */
66: public SymbolState() {
67: add("!=");
68: add(":-");
69: add("<=");
70: add(">=");
71: }
72:
73: /**
74: * Add a multi-character symbol.
75: *
76: * @param String the symbol to add, such as "=:="
77: */
78: public void add(String s) {
79: symbols.add(s);
80: }
81:
82: /**
83: * Return a symbol token from a reader.
84: *
85: * @return a symbol token from a reader
86: */
87: public Token nextToken(PushbackReader r, int first, Tokenizer t)
88: throws IOException {
89:
90: String s = symbols.nextSymbol(r, first);
91:
92: return new Token(Token.TT_SYMBOL, s, 0);
93: }
94: }
|