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 OrProductionRules extends ProductionRules{ 15 18 Object []comparableRules; 19 20 23 Object []recursiveObjects; 24 25 28 Object []nonRecursiveObjects; 29 30 Object []nonRecWithBest; 31 32 Object []nonRecWithoutBest; 33 34 35 OrProductionRules(ClassLoader classLoader0){ 36 super(classLoader0); 37 } 38 39 44 public Object parsePart(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 45 if ( pe.bestOptionFlag ){ 46 if ( pe.hashMap.containsKey(nameOfRule) ) 47 return withBestOption(pe); 48 else 49 return withoutBestOptionForSQL(pe); 50 } 51 return withoutBestOptionForToken(pe); 52 } 53 54 58 59 private Object withoutBestOptionForToken(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 60 Object value = null; 61 int ruleSize = nonRecursiveObjects == null ? -1 : nonRecursiveObjects.length; 62 for (int i = 0; i < ruleSize; i++) { 63 value = ( (ProductionRules) nonRecursiveObjects[i]).parse(pe); 64 if (! (value instanceof ParseException)) { 65 if (!recursiveflag) 66 return value; 67 pe.recursiveObject = value; 68 pe.recursionState = nameOfRule; 69 i = -1; } 71 } 72 return value == null ? pe.tokenFlag ? applyBinary(pe) : pe.parseException : value; 73 } 74 75 76 79 80 private Object withoutBestOptionForSQL(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 81 Object object1 = pe.recursiveObject; String recState = pe.recursionState; Object asdf = null; 84 Object o = comparableRules == null ? null : applyBinary(pe); 85 if ( o != null ){ 86 if ( !(o instanceof ParseException) ) 87 return o; 88 } 89 int ruleSize = nonRecursiveObjects == null ? -1 : nonRecursiveObjects.length - 1; 90 for(int i = ruleSize; i >= 0 ; i--){ 91 ProductionRules object = (ProductionRules)nonRecursiveObjects[i]; 92 Object value = object.parse(pe); 93 if(!(value instanceof ParseException)){ 94 asdf = value; 95 if(!recursiveflag) 96 return asdf; 97 object1 = asdf; 98 recState = nameOfRule; 99 } 100 pe.recursiveObject = object1; 101 pe.recursionState = recState; 102 } 103 if (asdf == null) 104 return pe.parseException; else 106 return parseForRecusriveRules(asdf,pe); } 108 109 110 117 public Object getRecursiveObject(String nameOfRule,DBStack occuredRules){ 118 if ( rules == null || rules.length == 0 ) 119 return null; 120 int size = rules.length - 1; 121 ArrayList list = new ArrayList (); 122 for(int i = 0; i < rules.length; ++i){ 123 ProductionRules production = (ProductionRules)rules[i]; 124 if ( !list.contains(production) && ! production.ruleKey.equalsIgnoreCase(nameOfRule) 125 && ! this.nameOfRule.equalsIgnoreCase(production.ruleKey) 126 && ! occuredRules.contains(production.ruleKey) ) { 127 occuredRules.push(production.ruleKey); 128 Object []object = (Object [])production.getRecursiveObject(nameOfRule,occuredRules); 129 if ( object != null ){ 130 for(int k = 0; k < object.length; ++k) 131 if ( !list.contains(object[k]) ) 132 list.add(object[k]); 133 } 134 } 135 } 136 if ( !list.isEmpty() ){ 137 if ( Arrays.equals(list.toArray(),rules) ) 138 return new Object []{this}; 139 return list.toArray(); 140 } 141 return null; 142 } 143 144 151 public Object getNonRecursiveObject(String nameOfRule,DBStack occuredRules){ 152 if ( rules == null || rules.length == 0 ) 153 return null; 154 int size = rules.length; 155 ArrayList list = new ArrayList (); 156 for(int i = 0; i < size ; ++i){ 157 ProductionRules production = (ProductionRules)rules[i]; 158 if ( !list.contains(production) && ! production.ruleKey.equalsIgnoreCase(nameOfRule) 159 && ! this.nameOfRule.equalsIgnoreCase(production.ruleKey) 160 && ! occuredRules.contains(production.ruleKey) ) { 161 occuredRules.push(production.ruleKey); 162 Object []object = (Object [])production.getNonRecursiveObject(nameOfRule,occuredRules); 163 if ( object != null ){ 164 for(int k = 0; k < object.length; ++k) 165 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 337 private Object withBestOption(ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 338 Object object1 = pe.recursiveObject; String recState = pe.recursionState; Best best = new Best(); 341 int position = pe.position; 342 int ruleSize = nonRecursiveObjects == null ? - 1 : nonRecursiveObjects.length - 1; 343 for(int i = ruleSize ; i >= 0; i--){ 344 Object value = ((ProductionRules)nonRecursiveObjects[i]).parse(pe); 345 if(position != pe.position && !(value instanceof ParseException)){ 346 best.update(value,pe.position); 347 pe.position = position; 348 } 349 pe.recursiveObject = object1; 350 pe.recursionState = recState; 351 } 352 applyBinary(pe,best); 353 if(best.sposition > position){ 354 pe.position = best.sposition; 355 if(!recursiveflag) 356 return best.sobject ; 357 pe.recursiveObject = best.sobject; 358 pe.recursionState = nameOfRule; 359 return parseForRecusriveRules(best,pe); 360 } 361 return pe.parseException; 362 } 363 364 365 368 private Object parseForRecusriveRules(Object asdf,ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 369 Best best = new Best(); 370 best.update(asdf,pe.position); 371 while(true){ 372 int position = pe.position; 373 int prvPosition = best.sposition; 374 String st = pe.recursionState; 375 Object asq = pe.recursiveObject; 376 int ruleSize = recursiveObjects == null ? -1 : recursiveObjects.length - 1; 377 for(int i = ruleSize ; i >= 0 ; i--){ 378 ProductionRules object = (ProductionRules)recursiveObjects[i]; 379 Object value = object.parse(pe); 380 if(position != pe.position && !(value instanceof ParseException)){ 381 best.update(value,pe.position); 382 pe.position = position; 383 } 384 pe.recursiveObject = asq; 385 pe.recursionState = st; 386 } 387 if(best.sposition > prvPosition){ 388 pe.position = best.sposition; 389 asdf = best.sobject ; 390 if (!recursiveflag){ 391 pe.recursiveObject = null; 392 pe.recursionState = null; 393 return asdf; 394 } 395 pe.recursiveObject = asdf; 396 pe.recursionState = nameOfRule; 397 } 398 else{ 399 if (asdf == null){ 400 pe.recursiveObject = null; 401 pe.recursionState = null; 402 return pe.parseException; 403 } 404 else{ 405 pe.recursiveObject = null; 406 pe.recursionState = null; 407 return asdf; 408 } 409 } 410 } 411 } 412 413 416 417 private Object parseForRecusriveRules(Best best,ParseElements pe)throws com.daffodilwoods.database.resource.DException{ 418 Object asdf = best.sobject; 419 while(true){ 420 int position = pe.position; 421 int prvPosition = best.sposition; 422 String st = pe.recursionState; 423 Object asq = pe.recursiveObject; 424 int ruleSize = recursiveObjects == null ? -1 : recursiveObjects.length - 1; 425 for(int i = ruleSize ; i >= 0 ; i--){ 426 Object value = ((ProductionRules)recursiveObjects[i]).parse(pe); 427 if(position != pe.position && !(value instanceof ParseException)){ 428 best.update(value,pe.position); 429 pe.position = position; 430 } 431 pe.recursiveObject = asq; 432 pe.recursionState = st; 433 } 434 if(best.sposition > prvPosition){ 435 pe.position = best.sposition; 436 asdf = best.sobject ; 437 if (!recursiveflag){ 438 pe.recursiveObject = null; 439 pe.recursionState = null; 440 return asdf; 441 } 442 pe.recursiveObject = asdf; 443 pe.recursionState = nameOfRule; 444 } 445 else{ 446 if (asdf == null){ 447 pe.recursiveObject = null; 448 pe.recursionState = null; 449 return pe.parseException; 450 } 451 else{ 452 pe.recursiveObject = null; 453 pe.recursionState = null; 454 return asdf; 455 } 456 } 457 } 458 } 459 460 461 public String toString(){ 462 return nameOfRule; 463 } 464 465 469 public static class Best{ 470 Object sobject ; 471 int sposition = -1; 472 void update (Object object,int position){ 473 if(position >= sposition){ 474 sobject = object; 475 sposition = position; 476 } 477 } 478 479 public String toString(){ 480 StringBuffer str=new StringBuffer ("[Best "); 481 str.append(sposition).append("--").append(sobject).append("]"); 482 return str.toString(); 483 } 484 } 485 486 } 487 | Popular Tags |