1 46 package org.mr.api.jms.selector.parser; 47 48 import org.mr.api.jms.selector.syntax.Identifiers; 49 import org.mr.api.jms.selector.syntax.Type; 50 import org.mr.api.jms.selector.syntax.TypeMismatchException; 51 55 final class TypeChecker { 56 57 58 61 private TypeChecker() { 62 } 64 65 72 public static void check(final SelectorAST node, final Type expected) throws TypeMismatchException { 73 74 Type type = node.getReturnType(); 75 if (type != expected && type != Type.UNDEFINED) { 76 String msg = "expecting a " + expected + " expression, found a " + type; 77 throw new TypeMismatchException(msg,node.getContext()); 78 } } 81 82 91 public static void check(final String operator, final SelectorAST node, final Type expected) throws TypeMismatchException { 92 93 Type type = node.getReturnType(); 94 if (type != expected && type != Type.UNDEFINED) { 95 String msg = "expecting a " + expected + " expression for operator " + operator + ", found a " + type; 96 throw new TypeMismatchException(msg,node.getContext()); 97 } } 100 101 111 public static void check(final String operator, final SelectorAST left, final SelectorAST right, final Type expected) throws TypeMismatchException { 112 113 check(operator, left, expected); 114 check(operator, right, expected); 115 } 117 118 126 public static void checkComparison(final String operator, final SelectorAST left,final SelectorAST right) throws TypeMismatchException { 127 128 Type leftHand = left.getReturnType(); 129 Type rightHand = right.getReturnType(); 130 131 if (leftHand == Type.UNDEFINED || rightHand == Type.UNDEFINED) { 132 } else if (leftHand == Type.STRING && rightHand == Type.STRING) { 135 checkStringComparison(operator, left, right); 136 } else if ((leftHand == Type.STRING && rightHand != Type.STRING) || (leftHand == Type.BOOLEAN && rightHand != Type.BOOLEAN) || (leftHand == Type.NUMERIC && rightHand != Type.NUMERIC)) { 138 String msg = "expecting a " + leftHand + " expression for operator " + operator + ", found a " + rightHand; 139 throw new TypeMismatchException(msg,right.getContext()); 140 } } 143 151 public static void checkStringComparison(final String operator, final SelectorAST left, final SelectorAST right) throws TypeMismatchException { 152 153 if (left.getType() == SelectorTokenTypes.IDENT && right.getType() == SelectorTokenTypes.STRING_LITERAL) { 154 checkIdentifierComparison(left, right); 155 } else if (left.getType() == SelectorTokenTypes.STRING_LITERAL && right.getType() == SelectorTokenTypes.IDENT) { 157 checkIdentifierComparison(right, left); 158 } } 161 162 169 public static void checkIdentifierComparison(final SelectorAST identifier, final SelectorAST literal) throws TypeMismatchException { 170 171 if (identifier.getText().equals(Identifiers.JMS_DELIVERY_MODE)) { 172 String value = literal.getText(); 173 if (!value.equals(Identifiers.PERSISTENT) && !value.equals(Identifiers.NON_PERSISTENT)) { 174 String msg = "Cannot compare JMSDeliveryMode with '" + value + "'"; 175 throw new TypeMismatchException(msg,identifier.getContext()); 176 } } }} | Popular Tags |