1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
42 */
43
44 package org.exolab.jms.selector.parser;
45
46 import org.exolab.jms.selector.Identifiers;
47 import org.exolab.jms.selector.Type;
48 import org.exolab.jms.selector.TypeMismatchException;
49
50
51 /***
52 * Utility class for performing type checking
53 *
54 * @version $Revision: 1.6 $ $Date: 2003/08/10 12:57:58 $
55 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
56 * @see SelectorAST
57 * @see SelectorTreeParser
58 * @see Type
59 * @see TypeMismatchException
60 */
61 final class TypeChecker {
62
63 /***
64 * Private constructor
65 */
66 private TypeChecker() {
67 }
68
69 /***
70 * Checks the return type of an expression against the expected type
71 *
72 * @param node the expression node
73 * @param expected the expected type of the expression
74 * @throws TypeMismatchException if the return type doesn't match that
75 * expected
76 */
77 public static void check(final SelectorAST node, final Type expected)
78 throws TypeMismatchException {
79
80 Type type = node.getReturnType();
81 if (type != expected && type != Type.UNDEFINED) {
82 String msg = "expecting a " + expected + " expression, found a "
83 + type;
84 throw new TypeMismatchException(node.getContext(), msg);
85 }
86 }
87
88 /***
89 * Checks the return type of an expression against the expected type
90 *
91 * @param operator the expression operator
92 * @param node the expression node
93 * @param expected the expected type of the expression
94 * @throws TypeMismatchException if the return type doesn't match that
95 * expected
96 */
97 public static void check(final String operator, final SelectorAST node,
98 final Type expected)
99 throws TypeMismatchException {
100
101 Type type = node.getReturnType();
102 if (type != expected && type != Type.UNDEFINED) {
103 String msg = "expecting a " + expected
104 + " expression for operator " + operator + ", found a " + type;
105 throw new TypeMismatchException(node.getContext(), msg);
106 }
107 }
108
109 /***
110 * Checks the types of left and right hand sides of an expression against
111 * the expected type
112 *
113 * @param operator the expression operator
114 * @param left the left hand side of the expression
115 * @param right the right hand side of the expression
116 * @param expected the expected type of the expression
117 * @throws TypeMismatchException if a type doesn't match that expected
118 */
119 public static void check(final String operator, final SelectorAST left,
120 final SelectorAST right, final Type expected)
121 throws TypeMismatchException {
122
123 check(operator, left, expected);
124 check(operator, right, expected);
125 }
126
127 /***
128 * Verifies if two expressions can be compared
129 *
130 * @param operator the comparison operator
131 * @param left the left hand side of the expression
132 * @param right the right hand side of the expression
133 * @throws TypeMismatchException if the expressions can't be compared
134 */
135 public static void checkComparison(final String operator,
136 final SelectorAST left,
137 final SelectorAST right)
138 throws TypeMismatchException {
139
140 Type lhs = left.getReturnType();
141 Type rhs = right.getReturnType();
142
143 if (lhs == Type.UNDEFINED || rhs == Type.UNDEFINED) {
144 // can't evaluate this at parse time.
145 } else if (lhs == Type.STRING && rhs == Type.STRING) {
146 checkStringComparison(operator, left, right);
147 } else if ((lhs == Type.STRING && rhs != Type.STRING)
148 || (lhs == Type.BOOLEAN && rhs != Type.BOOLEAN)
149 || (lhs == Type.NUMERIC && rhs != Type.NUMERIC)) {
150 String msg = "expecting a " + lhs + " expression for operator "
151 + operator + ", found a " + rhs;
152 throw new TypeMismatchException(right.getContext(), msg);
153 }
154 }
155
156 /***
157 * Verifies if two string expressions can be compared
158 *
159 * @param operator the comparison operator
160 * @param left the left hand side of the expression
161 * @param right the right hand side of the expression
162 * @throws TypeMismatchException if the expressions can't be compared
163 */
164 public static void checkStringComparison(final String operator,
165 final SelectorAST left,
166 final SelectorAST right)
167 throws TypeMismatchException {
168 if (left.getType() == SelectorTokenTypes.IDENT
169 && right.getType() == SelectorTokenTypes.STRING_LITERAL) {
170 checkIdentifierComparison(left, right);
171 } else if (left.getType() == SelectorTokenTypes.STRING_LITERAL
172 && right.getType() == SelectorTokenTypes.IDENT) {
173 checkIdentifierComparison(right, left);
174 }
175 }
176
177 /***
178 * Verifies that an identifier may be compared to a string literal
179 *
180 * @param identifier the identifier
181 * @param literal the string literal
182 * @throws TypeMismatchException if the expressions can't be compared
183 */
184 public static void checkIdentifierComparison(final SelectorAST identifier,
185 final SelectorAST literal)
186 throws TypeMismatchException {
187 if (identifier.getText().equals(Identifiers.JMS_DELIVERY_MODE)) {
188 String value = literal.getText();
189 if (!value.equals(Identifiers.PERSISTENT)
190 && !value.equals(Identifiers.NON_PERSISTENT)) {
191 String msg = "Cannot compare JMSDeliveryMode with '"
192 + value + "'";
193 throw new TypeMismatchException(identifier.getContext(), msg);
194 }
195 }
196 }
197
198 } //-- TypeChecker
This page was automatically generated by Maven