| 1 | // $ANTLR 2.7.6 (2005-12-22): "constraint.g" -> "ConstraintTreeWalker.java"$ |
| 2 | |
| 3 | package net.sourceforge.hiveevents; |
| 4 | |
| 5 | import java.util.HashMap; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import net.sourceforge.hiveutils.util.PropertyPathUtils; |
| 9 | |
| 10 | import antlr.TreeParser; |
| 11 | import antlr.Token; |
| 12 | import antlr.collections.AST; |
| 13 | import antlr.RecognitionException; |
| 14 | import antlr.ANTLRException; |
| 15 | import antlr.NoViableAltException; |
| 16 | import antlr.MismatchedTokenException; |
| 17 | import antlr.SemanticException; |
| 18 | import antlr.collections.impl.BitSet; |
| 19 | import antlr.ASTPair; |
| 20 | import antlr.collections.impl.ASTArray; |
| 21 | |
| 22 | |
| 23 | public class ConstraintTreeWalker extends antlr.TreeParser implements ConstraintParserTokenTypes |
| 24 | { |
| 25 | |
| 26 | private final Map<String, Object> _variables = new HashMap<String, Object>(); |
| 27 | |
| 28 | public void initVariables(Map<String, Object> variables) |
| 29 | { |
| 30 | _variables.putAll(variables); |
| 31 | _variables.put("null", null); |
| 32 | } |
| 33 | |
| 34 | public void initVariable(String name, Object value) |
| 35 | { |
| 36 | _variables.put(name, value); |
| 37 | } |
| 38 | |
| 39 | private Comparable getProperty(String name) |
| 40 | { |
| 41 | int index = name.indexOf('.'); |
| 42 | if (index < 0) |
| 43 | return (Comparable) _variables.get(name); |
| 44 | |
| 45 | return (Comparable) PropertyPathUtils.read( |
| 46 | _variables.get(name.substring(0, index)), name.substring(index + 1)); |
| 47 | } |
| 48 | |
| 49 | private boolean getBoolean(String name) |
| 50 | { |
| 51 | return ((Boolean) getProperty(name)).booleanValue(); |
| 52 | } |
| 53 | |
| 54 | private int compare(Comparable o1, Comparable o2) |
| 55 | { |
| 56 | if (o1 == o2) |
| 57 | { |
| 58 | return 0; |
| 59 | } |
| 60 | else if (o1 != null && o2 != null) |
| 61 | { |
| 62 | Class c1 = o1.getClass(); |
| 63 | Class c2 = o2.getClass(); |
| 64 | if (c1.isEnum() && !c2.isEnum()) |
| 65 | { |
| 66 | o2 = getEnum(c1, o2); |
| 67 | } |
| 68 | else if (!c1.isEnum() && c2.isEnum()) |
| 69 | { |
| 70 | o1 = getEnum(c2, o1); |
| 71 | } |
| 72 | return o1.compareTo(o2); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | return (o1 == null ? -1 : +1); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private Enum getEnum(Class c, Object o) |
| 81 | { |
| 82 | Object[] enums = c.getEnumConstants(); |
| 83 | for (int i = 0; i < enums.length; i++) |
| 84 | { |
| 85 | if (((Enum) enums[i]).name().equals(o)) |
| 86 | { |
| 87 | return (Enum) enums[i]; |
| 88 | } |
| 89 | } |
| 90 | return null; |
| 91 | } |
| 92 | public ConstraintTreeWalker() { |
| 93 | tokenNames = _tokenNames; |
| 94 | } |
| 95 | |
| 96 | public final boolean expr(AST _t) throws RecognitionException { |
| 97 | boolean result; |
| 98 | |
| 99 | AST expr_AST_in = (_t == ASTNULL) ? null : (AST)_t; |
| 100 | AST name = null; |
| 101 | |
| 102 | boolean bool1, bool2; |
| 103 | Comparable o1, o2; |
| 104 | result = false; |
| 105 | |
| 106 | |
| 107 | try { // for error handling |
| 108 | if (_t==null) _t=ASTNULL; |
| 109 | switch ( _t.getType()) { |
| 110 | case OR: |
| 111 | { |
| 112 | AST __t41 = _t; |
| 113 | AST tmp15_AST_in = (AST)_t; |
| 114 | match(_t,OR); |
| 115 | _t = _t.getFirstChild(); |
| 116 | bool1=expr(_t); |
| 117 | _t = _retTree; |
| 118 | bool2=expr(_t); |
| 119 | _t = _retTree; |
| 120 | _t = __t41; |
| 121 | _t = _t.getNextSibling(); |
| 122 | |
| 123 | result = bool1 || bool2; |
| 124 | |
| 125 | break; |
| 126 | } |
| 127 | case AND: |
| 128 | { |
| 129 | AST __t42 = _t; |
| 130 | AST tmp16_AST_in = (AST)_t; |
| 131 | match(_t,AND); |
| 132 | _t = _t.getFirstChild(); |
| 133 | bool1=expr(_t); |
| 134 | _t = _retTree; |
| 135 | bool2=expr(_t); |
| 136 | _t = _retTree; |
| 137 | _t = __t42; |
| 138 | _t = _t.getNextSibling(); |
| 139 | |
| 140 | result = bool1 && bool2; |
| 141 | |
| 142 | break; |
| 143 | } |
| 144 | case LT: |
| 145 | { |
| 146 | AST __t43 = _t; |
| 147 | AST tmp17_AST_in = (AST)_t; |
| 148 | match(_t,LT); |
| 149 | _t = _t.getFirstChild(); |
| 150 | o1=genexpr(_t); |
| 151 | _t = _retTree; |
| 152 | o2=genexpr(_t); |
| 153 | _t = _retTree; |
| 154 | _t = __t43; |
| 155 | _t = _t.getNextSibling(); |
| 156 | |
| 157 | result = (compare(o1, o2) < 0); |
| 158 | |
| 159 | break; |
| 160 | } |
| 161 | case LE: |
| 162 | { |
| 163 | AST __t44 = _t; |
| 164 | AST tmp18_AST_in = (AST)_t; |
| 165 | match(_t,LE); |
| 166 | _t = _t.getFirstChild(); |
| 167 | o1=genexpr(_t); |
| 168 | _t = _retTree; |
| 169 | o2=genexpr(_t); |
| 170 | _t = _retTree; |
| 171 | _t = __t44; |
| 172 | _t = _t.getNextSibling(); |
| 173 | |
| 174 | result = (compare(o1, o2) <= 0); |
| 175 | |
| 176 | break; |
| 177 | } |
| 178 | case GT: |
| 179 | { |
| 180 | AST __t45 = _t; |
| 181 | AST tmp19_AST_in = (AST)_t; |
| 182 | match(_t,GT); |
| 183 | _t = _t.getFirstChild(); |
| 184 | o1=genexpr(_t); |
| 185 | _t = _retTree; |
| 186 | o2=genexpr(_t); |
| 187 | _t = _retTree; |
| 188 | _t = __t45; |
| 189 | _t = _t.getNextSibling(); |
| 190 | |
| 191 | result = (compare(o1, o2) > 0); |
| 192 | |
| 193 | break; |
| 194 | } |
| 195 | case GE: |
| 196 | { |
| 197 | AST __t46 = _t; |
| 198 | AST tmp20_AST_in = (AST)_t; |
| 199 | match(_t,GE); |
| 200 | _t = _t.getFirstChild(); |
| 201 | o1=genexpr(_t); |
| 202 | _t = _retTree; |
| 203 | o2=genexpr(_t); |
| 204 | _t = _retTree; |
| 205 | _t = __t46; |
| 206 | _t = _t.getNextSibling(); |
| 207 | |
| 208 | result = (compare(o1, o2) >= 0); |
| 209 | |
| 210 | break; |
| 211 | } |
| 212 | case EQ: |
| 213 | { |
| 214 | AST __t47 = _t; |
| 215 | AST tmp21_AST_in = (AST)_t; |
| 216 | match(_t,EQ); |
| 217 | _t = _t.getFirstChild(); |
| 218 | o1=genexpr(_t); |
| 219 | _t = _retTree; |
| 220 | o2=genexpr(_t); |
| 221 | _t = _retTree; |
| 222 | _t = __t47; |
| 223 | _t = _t.getNextSibling(); |
| 224 | |
| 225 | result = (compare(o1, o2) == 0); |
| 226 | |
| 227 | break; |
| 228 | } |
| 229 | case NE: |
| 230 | { |
| 231 | AST __t48 = _t; |
| 232 | AST tmp22_AST_in = (AST)_t; |
| 233 | match(_t,NE); |
| 234 | _t = _t.getFirstChild(); |
| 235 | o1=genexpr(_t); |
| 236 | _t = _retTree; |
| 237 | o2=genexpr(_t); |
| 238 | _t = _retTree; |
| 239 | _t = __t48; |
| 240 | _t = _t.getNextSibling(); |
| 241 | |
| 242 | result = (compare(o1, o2) != 0); |
| 243 | |
| 244 | break; |
| 245 | } |
| 246 | case BOOL_PROPERTY: |
| 247 | { |
| 248 | name = (AST)_t; |
| 249 | match(_t,BOOL_PROPERTY); |
| 250 | _t = _t.getNextSibling(); |
| 251 | |
| 252 | result = getBoolean(name.getText()); |
| 253 | |
| 254 | break; |
| 255 | } |
| 256 | default: |
| 257 | { |
| 258 | throw new NoViableAltException(_t); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | catch (RecognitionException ex) { |
| 263 | reportError(ex); |
| 264 | if (_t!=null) {_t = _t.getNextSibling();} |
| 265 | } |
| 266 | _retTree = _t; |
| 267 | return result; |
| 268 | } |
| 269 | |
| 270 | public final Comparable genexpr(AST _t) throws RecognitionException { |
| 271 | Comparable result; |
| 272 | |
| 273 | AST genexpr_AST_in = (_t == ASTNULL) ? null : (AST)_t; |
| 274 | AST str = null; |
| 275 | AST num = null; |
| 276 | AST name = null; |
| 277 | |
| 278 | result = null; |
| 279 | |
| 280 | |
| 281 | try { // for error handling |
| 282 | if (_t==null) _t=ASTNULL; |
| 283 | switch ( _t.getType()) { |
| 284 | case STR_LITERAL: |
| 285 | { |
| 286 | str = (AST)_t; |
| 287 | match(_t,STR_LITERAL); |
| 288 | _t = _t.getNextSibling(); |
| 289 | |
| 290 | String symbol = str.getText(); |
| 291 | result = symbol.substring(1, symbol.length() - 1); |
| 292 | |
| 293 | break; |
| 294 | } |
| 295 | case NUM_LITERAL: |
| 296 | { |
| 297 | num = (AST)_t; |
| 298 | match(_t,NUM_LITERAL); |
| 299 | _t = _t.getNextSibling(); |
| 300 | |
| 301 | result = Integer.valueOf(num.getText()); |
| 302 | |
| 303 | break; |
| 304 | } |
| 305 | case PROPERTY: |
| 306 | { |
| 307 | name = (AST)_t; |
| 308 | match(_t,PROPERTY); |
| 309 | _t = _t.getNextSibling(); |
| 310 | |
| 311 | result = getProperty(name.getText()); |
| 312 | |
| 313 | break; |
| 314 | } |
| 315 | default: |
| 316 | { |
| 317 | throw new NoViableAltException(_t); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | catch (RecognitionException ex) { |
| 322 | reportError(ex); |
| 323 | if (_t!=null) {_t = _t.getNextSibling();} |
| 324 | } |
| 325 | _retTree = _t; |
| 326 | return result; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | public static final String[] _tokenNames = { |
| 331 | "<0>", |
| 332 | "EOF", |
| 333 | "<2>", |
| 334 | "NULL_TREE_LOOKAHEAD", |
| 335 | "OR", |
| 336 | "AND", |
| 337 | "LPAREN", |
| 338 | "RPAREN", |
| 339 | "LT", |
| 340 | "LE", |
| 341 | "GT", |
| 342 | "GE", |
| 343 | "EQ", |
| 344 | "NE", |
| 345 | "BOOL_PROPERTY", |
| 346 | "PROPERTY", |
| 347 | "NUM_LITERAL", |
| 348 | "STR_LITERAL", |
| 349 | "WS", |
| 350 | "DOT", |
| 351 | "ID" |
| 352 | }; |
| 353 | |
| 354 | } |
| 355 | |