1 package com.daffodilwoods.daffodildb.utils.parser; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import com.daffodilwoods.database.utility.P; 6 import com.daffodilwoods.daffodildb.utils.DBStack; 7 8 13 14 public class OrProductionRulesWithHashMap extends ProductionRulesWithHashMap{ 15 18 Object []comparableRules; 19 20 23 Object []recursiveObjects; 24 25 28 Object []nonRecursiveObjects; 29 30 Object []nonRecWithBest; 31 32 Object []nonRecWithoutBest; 33 34 OrProductionRulesWithHashMap(ClassLoader classLoader0){ 35 super(classLoader0); 36 } 37 38 43 public Object parsePart(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 44 if ( pe.bestOptionFlag ){ 45 if ( pe.hashMap.containsKey(nameOfRule) ) 46 return withBestOption(pe); 47 else 48 return withoutBestOptionForSQL(pe); 49 } 50 return withoutBestOptionForToken(pe); 51 } 52 53 57 58 private Object withoutBestOptionForToken(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 59 Object value = null; 60 int ruleSize = nonRecursiveObjects == null ? -1 : nonRecursiveObjects.length; 61 for (int i = 0; i < ruleSize; i++) { 62 value = ( (ProductionRules) nonRecursiveObjects[i]).parse(pe); 63 if (! (value instanceof ParseException)) { 64 if (!recursiveflag) 65 return value; 66 pe.recursiveObject = value; 67 pe.recursionState = nameOfRule; 68 i = -1; } 70 } 71 return value == null ? pe.tokenFlag ? applyBinary(pe) : pe.parseException : value; 72 } 73 74 75 78 79 private Object withoutBestOptionForSQL(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 80 Object object1 = pe.recursiveObject; String recState = pe.recursionState; Object asdf = null; 83 Object o = comparableRules == null ? null : applyBinary(pe); 84 if ( o != null ){ 85 if ( !(o instanceof ParseException) ) 86 return o; 87 } 88 int ruleSize = nonRecursiveObjects == null ? -1 : nonRecursiveObjects.length - 1; 89 for(int i = ruleSize; i >= 0 ; i--){ 90 ProductionRules object = (ProductionRules)nonRecursiveObjects[i]; 91 Object value = object.parse(pe); 92 if(!(value instanceof ParseException)){ 93 asdf = value; 94 if(!recursiveflag) 95 return asdf; 96 object1 = asdf; 97 recState = nameOfRule; 98 } 99 pe.recursiveObject = object1; 100 pe.recursionState = recState; 101 } 102 if (asdf == null) 103 return pe.parseException; else 105 return parseForRecusriveRules(asdf,pe); } 107 108 115 public Object getRecursiveObject(String nameOfRule,DBStack occuredRules){ 116 if ( rules == null || rules.length == 0 ) 117 return null; 118 int size = rules.length - 1; 119 ArrayList list = new ArrayList (); 120 for(int i = 0; i < rules.length; ++i){ 121 ProductionRules production = (ProductionRules)rules[i]; 122 if ( !list.contains(production) && ! production.ruleKey.equalsIgnoreCase(nameOfRule) 123 && ! this.ruleKey.equalsIgnoreCase(production.ruleKey) 124 && ! occuredRules.contains(production.ruleKey) ) { 125 occuredRules.push(production.ruleKey); 126 Object []object = (Object [])production.getRecursiveObject(nameOfRule,occuredRules); 127 if ( object != null ){ 128 for(int k = 0; k < object.length; ++k) 129 if ( !list.contains(object[k]) ) 131 list.add(object[k]); 132 } 133 } 134 } 135 if ( !list.isEmpty() ){ 136 if ( Arrays.equals(list.toArray(),rules) ) 137 return new Object []{this}; 138 return list.toArray(); 139 } 140 return null; 141 } 142 143 150 public Object getNonRecursiveObject(String nameOfRule,DBStack occuredRules){ 151 if ( rules == null || rules.length == 0 ) 152 return null; 153 int size = rules.length; 154 ArrayList list = new ArrayList (); 155 for(int i = 0; i < size ; ++i){ 156 ProductionRules production = (ProductionRules)rules[i]; 157 if ( !list.contains(production) && ! production.ruleKey.equalsIgnoreCase(nameOfRule) 158 && ! this.ruleKey.equalsIgnoreCase(production.ruleKey) 159 && ! occuredRules.contains(production.ruleKey) ) { 160 occuredRules.push(production.ruleKey); 161 Object []object = (Object [])production.getNonRecursiveObject(nameOfRule,occuredRules); 162 if ( object != null ){ 163 for(int k = 0; k < object.length; ++k) 164 if ( !list.contains(object[k]) ) 166 list.add(object[k]); 167 168 } 169 } 170 } 171 if ( !list.isEmpty() ){ 172 DBStack a = new DBStack(); 173 a.push(this.ruleKey); 174 Object arr[] = (Object [])getRecursiveObject(this.ruleKey,a); 175 if ( arr != null ) 176 return new Object []{this}; 177 return list.toArray(); 178 } 179 return null; 180 } 181 182 188 private Object applyBinary(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 189 if ( comparableRules == null ) 190 return pe.parseException; 191 int low = 0 , high = comparableRules.length - 1; 192 while(low <= high){ 193 int mid = (low + high) / 2; 194 ProductionRules object = (ProductionRules)comparableRules[mid]; 195 Object obj = object.parse(pe); 196 if(obj instanceof ParseException){ 197 ParseException parseException = (ParseException)obj; 198 if ( parseException.returnType == 2 ){ 199 Object o = specialHandling(mid-1,mid+1,pe,low,high); 200 if ( !(o instanceof ParseException) ) 201 return o; 202 parseException = (ParseException)o; 203 } 204 if(parseException.returnType < 0) 205 high = mid - 1; 206 else if(parseException.returnType > 0) 207 low = mid + 1; 208 else 209 return obj; 210 } 211 else{ 212 if ( !object.recursiveflag ) 213 return obj; 214 else{ 215 pe.recursiveObject = obj; 216 pe.recursionState = nameOfRule; 217 return parseForRecusriveRules(obj,pe); 218 } 219 } 220 } 221 return pe.parseException; 222 } 223 224 228 private Object specialHandling(int below,int above,ParseElements pe,int low,int high)throws com.daffodilwoods.database.resource.DException{ 229 ProductionRules object = null; 230 if ( below >= 0 ){ 231 object = (ProductionRules)comparableRules[below]; 232 Object obj = object.parse(pe); 233 if ( !(obj instanceof ParseException) ) 234 return obj; 235 } 236 if ( above < comparableRules.length ){ 237 object = (ProductionRules)comparableRules[above]; 238 Object obj = object.parse(pe); 239 if ( !(obj instanceof ParseException) ) 240 return obj; 241 } 242 return parseForRestOfComparableRules(pe,low,high); 243 } 244 245 246 private Object parseForRestOfComparableRules(ParseElements pe,int low,int high)throws com.daffodilwoods.database.resource.DException{ 247 for(int i = low; i < high; ++i){ 248 ProductionRules object = (ProductionRules)comparableRules[i]; 249 Object obj = object.parse(pe); 250 if ( !(obj instanceof ParseException) ) 251 if ( !object.recursiveflag ) 252 return obj; 253 else{ 254 pe.recursiveObject = obj; 255 pe.recursionState = nameOfRule; 256 return parseForRecusriveRules(obj,pe); 257 } 258 } 259 return pe.parseException; 260 } 261 262 265 private Object applyBinary(ParseElements pe,Best best)throws com.daffodilwoods.database.resource.DException{ 266 if ( comparableRules == null ) return pe.parseException; 267 int low = 0 , high = comparableRules.length - 1; 268 int position = pe.position; 269 while(low <= high){ 270 int mid = (low + high) / 2; 271 ProductionRules object = (ProductionRules)comparableRules[mid]; 272 Object obj = object.parse(pe); 273 if(obj instanceof ParseException){ 274 ParseException parseException = (ParseException)obj; 275 if ( !pe.tokenFlag && parseException.returnType == 2 ){ 276 specialHandling(mid-1,mid+1,pe,best,position); return best.sobject; 278 } 279 if(parseException.returnType < 0) 280 high = mid - 1; 281 else if(parseException.returnType > 0) 282 low = mid + 1; 283 else 284 return obj; 285 } 286 else{ 287 if ( position != pe.position) best.update(obj,pe.position); 289 if ( !object.recursiveflag ){ 290 specialHandling(mid - 1,mid + 1,pe,best,position); 291 return best.sobject; 292 } 293 pe.recursiveObject = best.sobject; 294 pe.recursionState = nameOfRule; 295 return parseForRecusriveRules(best,pe); 296 } 297 } 298 return pe.parseException; 299 } 300 301 305 private void specialHandling(int below,int above,ParseElements pe,Best best,int position)throws com.daffodilwoods.database.resource.DException{ 306 ProductionRules object = null; 307 pe.position = position; 308 if ( below >= 0 ){ 309 object = (ProductionRules)comparableRules[below]; 310 Object obj = object.parse(pe); 311 if ( !(obj instanceof ParseException) ){ 312 if ( position != pe.position) best.update(obj,pe.position); 314 } 315 } 316 if ( above < comparableRules.length ){ 317 object = (ProductionRules)comparableRules[above]; 318 pe.position = position; 319 Object obj = object.parse(pe); 320 if ( !(obj instanceof ParseException) ){ 321 if ( position != pe.position) best.update(obj,pe.position); 323 } 324 } 325 } 326 327 328 329 338 339 private Object withBestOption(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 340 Object object1 = pe.recursiveObject; String recState = pe.recursionState; Best best = new Best(); 343 int position = pe.position; 344 int ruleSize = nonRecursiveObjects == null ? - 1 : nonRecursiveObjects.length - 1; 345 for(int i = ruleSize ; i >= 0; i--){ 346 ProductionRules object = (ProductionRules)nonRecursiveObjects[i]; 347 Object value = object.parse(pe); 348 if(position != pe.position && !(value instanceof ParseException)){ 349 best.update(value,pe.position); 350 pe.position = position; 351 } 352 pe.recursiveObject = object1; 353 pe.recursionState = recState; 354 } 355 applyBinary(pe,best); 356 if(best.sposition > position){ 357 pe.position = best.sposition; 358 if(!recursiveflag) 359 return best.sobject ; 360 position = best.sposition; 361 pe.recursiveObject = best.sobject; 362 pe.recursionState = nameOfRule; 363 return parseForRecusriveRules(best,pe); 364 } 365 return pe.parseException; 366 } 367 368 369 372 private Object parseForRecusriveRules(Object asdf,ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 373 Best best = new Best(); 374 best.update(asdf,pe.position); 375 while(true){ 376 int position = pe.position; 377 int prvPosition = best.sposition; 378 String st = pe.recursionState; 379 Object asq = pe.recursiveObject; 380 int ruleSize = recursiveObjects == null ? -1 : recursiveObjects.length - 1; 381 for(int i = ruleSize ; i >= 0 ; i--){ 382 ProductionRules object = (ProductionRules)recursiveObjects[i]; 383 Object value = object.parse(pe); 384 if(position != pe.position && !(value instanceof ParseException)){ 385 best.update(value,pe.position); 386 pe.position = position; 387 } 388 pe.recursiveObject = asq; 389 pe.recursionState = st; 390 } 391 if(best.sposition > prvPosition){ 392 pe.position = best.sposition; 393 asdf = best.sobject ; 394 if (!recursiveflag){ 395 pe.recursiveObject = null; 396 pe.recursionState = null; 397 return asdf; 398 } 399 pe.recursiveObject = asdf; 400 pe.recursionState = nameOfRule; 401 } 402 else{ 403 if (asdf == null){ 404 pe.recursiveObject = null; 405 pe.recursionState = null; 406 return pe.parseException; 407 } 408 else{ 409 pe.recursiveObject = null; 410 pe.recursionState = null; 411 return asdf; 412 } 413 } 414 } 415 } 416 417 418 421 private Object parseForRecusriveRules(Best best,ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 422 Object asdf = best.sobject; 423 while(true){ 424 int position = pe.position; 425 int prvPosition = best.sposition; 426 String st = pe.recursionState; 427 Object asq = pe.recursiveObject; 428 int ruleSize = recursiveObjects == null ? -1 : recursiveObjects.length - 1; 429 for(int i = ruleSize ; i >= 0 ; i--){ 430 ProductionRules object = (ProductionRules)recursiveObjects[i]; 431 Object value = object.parse(pe); 432 if(position != pe.position && !(value instanceof ParseException)){ 433 best.update(value,pe.position); 434 pe.position = position; 435 } 436 pe.recursiveObject = asq; 437 pe.recursionState = st; 438 } 439 if(best.sposition > prvPosition){ 440 pe.position = best.sposition; 441 asdf = best.sobject ; 442 if (!recursiveflag){ 443 pe.recursiveObject = null; 444 pe.recursionState = null; 445 return asdf; 446 } 447 pe.recursiveObject = asdf; 448 pe.recursionState = nameOfRule; 449 } 450 else{ 451 if (asdf == null){ 452 pe.recursiveObject = null; 453 pe.recursionState = null; 454 return pe.parseException; 455 } 456 else{ 457 pe.recursiveObject = null; 458 pe.recursionState = null; 459 return asdf; 460 } 461 } 462 } 463 } 464 465 public String toString(){ 466 return nameOfRule; 467 } 468 469 473 public static class Best{ 474 Object sobject ; 475 int sposition = -1; 476 void update (Object object,int position){ 477 if(position >= sposition){ 478 sobject = object; 479 sposition = position; 480 } 481 } 482 483 public String toString() { 484 StringBuffer str = new StringBuffer ("[Best "); 485 str.append(sposition).append("--").append(sobject).append("]"); 486 return str.toString(); 487 } 488 } 489 } 490 | Popular Tags |