| 1 package com.daffodilwoods.daffodildb.utils.parser; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.sql99.dcl.sqlcontrolstatement.*; 6 import com.daffodilwoods.daffodildb.server.sql99.token.*; 7 import com.daffodilwoods.database.utility.P; 8 9 public class TokenGenerator { 10 11 static char[] delimiterTokens = new char[] { 12 '(', ',', ' ', ')', ';', '\n', '\t', '.', '\'', '"', 13, 10, '+', 13 '-', '*', '/', '%', '=', '<', '>', '!', ':', '|', '&'}; 14 15 static TreeMap reserveWords,nonReserveWords; 16 17 public TokenGenerator(){ 18 Arrays.sort(delimiterTokens); 19 } 20 21 22 public Object [] getTokensOfQueryAsObjects(String query) { 23 Object abc[] = (Object [])getTokens(query); 24 ArrayList tokens = (ArrayList) abc[0]; 25 ArrayList toReturn = new ArrayList(tokens.size()); 26 String [] str = (String []) tokens.toArray(new String [0]); 27 int i=0; 28 while(i!=tokens.size()){ 29 if (GetObjectsForNumeric(str[i], toReturn)) { 30 i++; 31 continue; 32 } 33 if (reserveWords.containsKey(str[i])) { 34 SRESERVEDWORD1206543922 sres = new SRESERVEDWORD1206543922(); 35 sres._SRESERVEDWORD12065439220 = str[i]; 36 if (str[i].equalsIgnoreCase("Date")) { 37 if(i<tokens.size()-1 && str[i+1].startsWith("'")){ 38 dateliteral ob = new dateliteral(); 39 datestring o = new datestring(); 40 o._datestring0 = getCharacterStringLiteral(str[i+1]); 41 ob._datestring0 = o; 42 ob._SRESERVEDWORD12065439221 = sres; 43 toReturn.add(ob); 44 i++; 45 }else 46 toReturn.add(sres); 47 } 48 else if (str[i].equalsIgnoreCase("Time")) { 49 if(i<tokens.size()-1 && str[i+1].startsWith("'")){ 50 timeliteral ob = new timeliteral(); 51 timestring o = new timestring(); 52 o._timestring0 = getCharacterStringLiteral(str[i+1]); 53 ob._timestring0 = o; 54 ob._SRESERVEDWORD12065439221 = sres; 55 toReturn.add(ob); 56 i++; 57 }else 58 toReturn.add(sres); 59 } 60 else if (str[i].equalsIgnoreCase("TimeStamp")) { 61 if(i<tokens.size()-1 && str[i+1].startsWith("'")){ 62 timestampliteral ob = new timestampliteral(); 63 timestampstring o = new timestampstring(); 64 o._timestampstring0 = getCharacterStringLiteral(str[i+1]); 65 ob._timestampstring0 = o; 66 ob._SRESERVEDWORD12065439221 = sres; 67 toReturn.add(ob); 68 i++; 69 }else 70 toReturn.add(sres); 71 }else{ 72 toReturn.add(sres); 73 } 74 } 75 else if (nonReserveWords.containsKey(str[i])) { 76 SNONRESERVEDWORD136444255 snres = new SNONRESERVEDWORD136444255(); 77 snres._SNONRESERVEDWORD1364442550 = str[i].toString(); 78 toReturn.add(snres); 79 } 80 else if (GetObjectsForDelimiter(str[i], toReturn)) { 81 } 82 else { 83 regularidentifier rid = new regularidentifier(); 84 rid._regularidentifier0 = str[i]; 85 toReturn.add(rid); 86 } 87 i++; 88 } 89 abc[0]=toReturn; 90 return abc; 91 } 92 93 private boolean GetObjectsForNumeric(String str, ArrayList arr) { 94 try{ 95 int index=str.indexOf("."); 96 int Eindex=str.toLowerCase().indexOf("e"); 97 98 if (index>=0) { 99 Speriod_991726143 a = new Speriod_991726143(); 100 a._Speriod_9917261430 = "."; 101 if (index == 0 && str.length() == 1){ 102 arr.add(a); 103 return true; 104 } 105 else{ 106 if (Eindex > 0) { 107 Se101 e = new Se101(); 108 e._Se1010 = "E"; 109 exponent ex = new exponent(); 110 signedinteger sint = getsignedinteger(str,Eindex); 111 ex._exponent0 = sint; 112 mantissa man = new mantissa(); 113 if (index == 0) 114 man._mantissa0 = getSperiod_991726143unsignedinteger(str, a, index,Eindex); 115 else 116 man._mantissa0=getRepdigitSperiod_991726143OptRepdigit(str,a,index,Eindex); 117 approximatenumericliteral obj = new approximatenumericliteral(); 118 obj._mantissa2 = man; 119 obj._erule1 = e; 120 obj._exponent0 = ex; 121 arr.add(obj); 122 return true; 123 }else{ 124 if(index==0) 125 arr.add(getSperiod_991726143unsignedinteger(str,a,index,str.length())); 126 else 127 arr.add(getRepdigitSperiod_991726143OptRepdigit(str,a,index,str.length())); 128 return true; 129 } 130 } 131 } 132 else if(Eindex>0){ 133 Se101 e = new Se101(); 134 e._Se1010 = "E"; 135 exponent ex = new exponent(); 136 signedinteger sint = getsignedinteger(str,Eindex); 137 ex._exponent0 = sint; 138 mantissa man = new mantissa(); 139 unsignedinteger oo=new unsignedinteger(); 140 oo._unsignedinteger0=getDigits(str.substring(0,Eindex).toCharArray()); 141 man._mantissa0=oo; 142 approximatenumericliteral obj = new approximatenumericliteral(); 143 obj._mantissa2 = man; 144 obj._erule1 = e; 145 obj._exponent0 = ex; 146 arr.add(obj); 147 return true; 148 }else if(str.length()>1 && Character.toLowerCase(str.charAt(str.length()-1))=='k'){ 149 unsignedintegermultiplier obj=new unsignedintegermultiplier(); 150 unsignedinteger o=new unsignedinteger(); 151 o._unsignedinteger0=getDigits(str.substring(0,str.length()-1).toCharArray()); 152 SNONRESERVEDWORD136444255 mchar=new SNONRESERVEDWORD136444255(); 153 mchar._SNONRESERVEDWORD1364442550="K"; 154 obj._unsignedinteger1=o; 155 obj._multiplier0=mchar; 156 arr.add(obj); 157 return true; 158 }else if(str.length()>1 && Character.toLowerCase(str.charAt(str.length()-1))=='m'){ 159 unsignedintegermultiplier obj=new unsignedintegermultiplier(); 160 unsignedinteger o=new unsignedinteger(); 161 o._unsignedinteger0=getDigits(str.substring(0,str.length()-1).toCharArray()); 162 SNONRESERVEDWORD136444255 mchar=new SNONRESERVEDWORD136444255(); 163 mchar._SNONRESERVEDWORD1364442550="M"; 164 obj._unsignedinteger1=o; 165 obj._multiplier0=mchar; 166 arr.add(obj); 167 return true; 168 }else if(str.length()>1 && Character.toLowerCase(str.charAt(str.length()-1))=='g'){ 169 unsignedintegermultiplier obj=new unsignedintegermultiplier(); 170 unsignedinteger o=new unsignedinteger(); 171 o._unsignedinteger0=getDigits(str.substring(0,str.length()-1).toCharArray()); 172 SNONRESERVEDWORD136444255 mchar=new SNONRESERVEDWORD136444255(); 173 mchar._SNONRESERVEDWORD1364442550="G"; 174 obj._unsignedinteger1=o; 175 obj._multiplier0=mchar; 176 arr.add(obj); 177 return true; 178 }else{ 179 unsignedinteger obj=new unsignedinteger(); 180 obj._unsignedinteger0=getDigits(str.toCharArray()); 181 arr.add(obj); 182 return true; 183 } 184 }catch(Exception e){ 185 return false; 186 } 187 } 188 189 190 191 private digit[] getDigits(char[] asas) { 192 int len = asas.length; 193 digit[] asasa = new digit[len]; 194 for (int i = 0; i < len; i++) { 195 switch (asas[i]) { 196 case '0': 197 S048 qqq0 = new S048(); 198 qqq0._S0480 = "0"; 199 asasa[i] = qqq0; 200 break; 201 case '1': 202 S149 qqq1 = new S149(); 203 qqq1._S1490 = "1"; 204 asasa[i] = qqq1; 205 break; 206 case '2': 207 S250 qqq2 = new S250(); 208 qqq2._S2500 = "2"; 209 asasa[i] = qqq2; 210 break; 211 case '3': 212 S351 qqq3 = new S351(); 213 qqq3._S3510 = "3"; 214 asasa[i] = qqq3; 215 break; 216 case '4': 217 S452 qqq4 = new S452(); 218 qqq4._S4520 = "4"; 219 asasa[i] = qqq4; 220 break; 221 case '5': 222 S553 qqq5 = new S553(); 223 qqq5._S5530 = "5"; 224 asasa[i] = qqq5; 225 break; 226 case '6': 227 S654 qqq6 = new S654(); 228 qqq6._S6540 = "6"; 229 asasa[i] = qqq6; 230 break; 231 case '7': 232 S755 qqq7 = new S755(); 233 qqq7._S7550 = "7"; 234 asasa[i] = qqq7; 235 break; 236 case '8': 237 S856 qqq8 = new S856(); 238 qqq8._S8560 = "8"; 239 asasa[i] = qqq8; 240 break; 241 case '9': 242 S957 qqq9 = new S957(); 243 qqq9._S9570 = "9"; 244 asasa[i] = qqq9; 245 break; 246 default: 247 throw new RuntimeException ("invalid number '" + i+"'"); 248 } 249 } 250 return asasa; 251 } 252 253 254 private characterstringliteral getCharacterStringLiteral(String str){ 255 characterstringliteral obj = new characterstringliteral(); 256 obj._characterstringliteral0 = str.substring(1, str.length() - 1); 257 return obj; 258 } 259 260 261 private signedinteger getsignedinteger(String str,int index){ 262 signedinteger obj = new signedinteger(); 263 int i=1; 264 if(str.indexOf('+')>=0){ 265 Splussign_1537298301 sign=new Splussign_1537298301(); 266 sign._Splussign_15372983010="+"; 267 obj._Optsign1=sign; 268 i++; 269 }else if(str.indexOf("-")>=0){ 270 Sminussign1770466157 sign=new Sminussign1770466157(); 271 sign._Sminussign17704661570="-"; 272 obj._Optsign1=sign; 273 i++; 274 } 275 obj._Repdigit0 = getDigits(str.substring(index+i,str.length()).toCharArray()); 276 return obj; 277 } 278 279 280 private Speriod_991726143unsignedinteger getSperiod_991726143unsignedinteger(String str,Speriod_991726143 a,int index,int Eindex){ 281 Speriod_991726143unsignedinteger obj = new Speriod_991726143unsignedinteger(); 282 obj._Speriod_9917261431 = a; 283 unsignedinteger ui = new unsignedinteger(); 284 String kk = str.substring(1, Eindex); 285 ui._unsignedinteger0 = getDigits(kk.toCharArray()); 286 obj._unsignedinteger0 = ui; 287 return obj; 288 } 289 290 291 private RepdigitSperiod_991726143OptRepdigit getRepdigitSperiod_991726143OptRepdigit(String str,Speriod_991726143 a,int index,int Eindex){ 292 RepdigitSperiod_991726143OptRepdigit obj = new RepdigitSperiod_991726143OptRepdigit(); 293 obj._Speriod_9917261431 = a; 294 obj._OptRepdigit0 = getDigits(str.substring(index + 1, Eindex).toCharArray()); 295 obj._Repdigit2 = getDigits(str.substring(0, index).toCharArray()); 296 return obj; 297 } 298 299 300 private boolean GetObjectsForDelimiter(String p, ArrayList arr) { 301 if (p.charAt(0)=='\'') { 302 arr.add(getCharacterStringLiteral(p)); 303 return true; 304 } 305 else if (p.charAt(0)=='"') { 306 delimitedidentifier obj = new delimitedidentifier(); 307 308 p = p.substring(1, p.lastIndexOf("\"")); 309 obj._delimitedidentifier0 = p; 310 arr.add(obj); 311 return true; 312 } 313 314 315 if (p.length() == 2){ 316 if (p.charAt(0)=='|' && p.charAt(1)=='|') { 317 Sconcatenationoperator_247443092 obj = new Sconcatenationoperator_247443092(); 318 obj._Sconcatenationoperator_2474430920 = p; 319 arr.add(obj); 320 } 321 else if (p.charAt(0)=='!' && p.charAt(1)=='=') { 322 Snotequalto_108361092 obj = new Snotequalto_108361092(); 323 obj._Snotequalto_1083610920 = p; 324 arr.add(obj); 325 } 326 else if (p.charAt(0)=='<' && p.charAt(1)=='>') { 327 Snotequalsoperator1686450328 obj = new Snotequalsoperator1686450328(); 328 obj._Snotequalsoperator16864503280 = p; 329 arr.add(obj); 330 } 331 else if (p.charAt(0)=='<' && p.charAt(1)=='=') { 332 Slessthanorequalsoperator_1757179136 obj = new Slessthanorequalsoperator_1757179136(); 333 obj._Slessthanorequalsoperator_17571791360 = p; 334 arr.add(obj); 335 } 336 else if (p.charAt(0)=='>' && p.charAt(1)=='=') { 337 Sgreaterthanorequalsoperator_16283039 obj = new Sgreaterthanorequalsoperator_16283039(); 338 obj._Sgreaterthanorequalsoperator_162830390 = p; 339 arr.add(obj); 340 }else if (p.charAt(0)==':' && p.charAt(1)==':') { 341 Sdoublecolon_429400496 obj = new Sdoublecolon_429400496(); 342 obj._Sdoublecolon_4294004960 = p; 343 arr.add(obj); 344 }else 345 return false; 346 return true; 347 } 348 349 else if(p.length()==1){ 350 if (p.charAt(0) == '<') { 351 Slessthanoperator_1361504772 obj = new Slessthanoperator_1361504772(); 352 obj._Slessthanoperator_13615047720 = p; 353 arr.add(obj); 354 } 355 else if (p.charAt(0) == '>') { 356 Sgreaterthanoperator_1568169315 obj = new 357 Sgreaterthanoperator_1568169315(); 358 obj._Sgreaterthanoperator_15681693150 = p; 359 arr.add(obj); 360 } 361 else if (p.charAt(0) == '=') { 362 Sequalsoperator182046693 obj = new Sequalsoperator182046693(); 363 obj._Sequalsoperator1820466930 = p; 364 arr.add(obj); 365 } 366 else if (p.charAt(0) == '|') { 367 Sverticalbar_37763991 obj = new Sverticalbar_37763991(); 368 obj._Sverticalbar_377639910 = p; 369 arr.add(obj); 370 } 371 else if (p.charAt(0) == '%') { 372 Spercent_678927291 obj = new Spercent_678927291(); 373 obj._Spercent_6789272910 = p; 374 arr.add(obj); 375 } 376 else if (p.charAt(0) == '&') { 377 Sampersand1079887221 obj = new Sampersand1079887221(); 378 obj._Sampersand10798872210 = p; 379 arr.add(obj); 380 } 381 else if (p.charAt(0) == '*') { 382 Sasterisk_344623246 obj = new Sasterisk_344623246(); 383 obj._Sasterisk_3446232460 = p; 384 arr.add(obj); 385 } 386 else if (p.charAt(0) == '+') { 387 Splussign_1537298301 obj = new Splussign_1537298301(); 388 obj._Splussign_15372983010 = p; 389 arr.add(obj); 390 } 391 else if (p.charAt(0) == ',') { 392 Scomma94843605 obj = new Scomma94843605(); 393 obj._Scomma948436050 = p; 394 arr.add(obj); 395 } 396 else if (p.charAt(0) == '-') { 397 Sminussign1770466157 obj = new Sminussign1770466157(); 398 obj._Sminussign17704661570 = p; 399 arr.add(obj); 400 } 401 else if (p.charAt(0) == '.') { 402 Speriod_991726143 obj = new Speriod_991726143(); 403 obj._Speriod_9917261430 = p; 404 arr.add(obj); 405 } 406 else if (p.charAt(0) == '/') { 407 Ssolidus_2030455159 obj = new Ssolidus_2030455159(); 408 obj._Ssolidus_20304551590 = p; 409 arr.add(obj); 410 } 411 else if (p.charAt(0) == ';') { 412 Ssemicolon_714091343 obj = new Ssemicolon_714091343(); 413 obj._Ssemicolon_7140913430 = p; 414 arr.add(obj); 415 } 416 else if (p.charAt(0) == '?') { 417 variablecolumn obj = new variablecolumn(); 418 Squestionmark_1088734521 o = new Squestionmark_1088734521(); 419 o._Squestionmark_10887345210 = p; 420 obj._variablecolumn0 = o; 421 arr.add(obj); 422 } 423 else if (p.charAt(0) == ':') { 424 Scolon94842719 obj = new Scolon94842719(); 425 obj._Scolon948427190 = p; 426 arr.add(obj); 427 } 428 else if (p.charAt(0) == '(') { 429 Sleftparen653880241 obj = new Sleftparen653880241(); 430 obj._Sleftparen6538802410 = p; 431 arr.add(obj); 432 } 433 else if (p.charAt(0) == ')') { 434 Srightparen_1874859514 obj = new Srightparen_1874859514(); 435 obj._Srightparen_18748595140 = p; 436 arr.add(obj); 437 } 438 else 439 return false; 440 return true; 441 } 442 return false; 443 } 444 445 446 private Object getTokens(String query) { 447 char[] ch = query.toCharArray(); 448 449 ArrayList tokens = new ArrayList(); 450 ArrayList indexes = new ArrayList(); 451 452 453 int prevPos = 0; 454 int i = 0; 455 456 while (i != ch.length) { 457 try { 458 if (!checkForValidity(ch[i])) 459 throw new ParseException(); 460 461 464 if (ch[i] == '"') { 465 addToken(tokens, query.substring(prevPos, i).trim(), prevPos, indexes); 466 i = handleForQuotes('"', ch, i, prevPos, tokens, indexes); 467 if(i>=ch.length) 468 break; 469 prevPos = i; 470 } 471 472 if (ch[i] == '\'') { 473 addToken(tokens, query.substring(prevPos, i).trim(), prevPos, indexes); 474 i = handleForQuotes('\'', ch, i, prevPos, tokens, indexes); 475 if(i>=ch.length) 476 break; 477 prevPos = i; 478 } 479 480 481 if (ch[i]=='!' || ch[i]=='>' || ch[i]=='<') { 482 if (ch[i + 1] == '=' || (ch[i] == '<' && ch[i + 1] == '>')) { 483 addToken(tokens, query.substring(prevPos, i).trim(), prevPos, indexes); 484 prevPos = i; 485 i += 2; 486 addToken(tokens, query.substring(prevPos, i).trim(), prevPos,indexes); 487 prevPos = i; 488 continue; 489 } 490 } 491 492 493 if (ch[i] == ':' || ch[i] == '|' && ch[i + 1] == ch[i]) { 494 addToken(tokens, query.substring(prevPos, i).trim(), prevPos, indexes); 495 prevPos = i; 496 i += 2; 497 addToken(tokens, query.substring(prevPos, i).trim(), prevPos, indexes); 498 prevPos = i; 499 continue; 500 } 501 502 503 if(checkForNumeric(ch[i])){ i++; 505 StringBuffer str=new StringBuffer (query.substring(prevPos, i).trim()); 506 while (i < ch.length) { 507 if (checkForNumeric(ch[i]) || 508 (checkForPlusMinus(ch[i]) && (str.indexOf("e")>=0 || str.indexOf("E")>=0) && (checkForNumeric(ch[i+1]) || checkForPlusMinus(ch[i+1]) || checkForDelimiter(ch[i+1]))) 509 || ((checkForNumeric_E_Period(ch[i]) || (checkForMultipliers(ch[i]) && str.indexOf(".")<0)) && (checkForNumeric(ch[i+1]) || checkForPlusMinus(ch[i+1]) || checkForDelimiter(ch[i+1])))) 510 str.append(ch[i++]); 511 else if(checkForAlphabet(str.charAt(0)) && checkForAlphabet(ch[i])) 512 str.append(ch[i++]); 513 else if(!checkForAlphabet(str.charAt(0)) && Character.isWhitespace(ch[i])) 514 i++; 515 else 516 break; 517 } 518 addToken(tokens, str.toString(), prevPos, indexes); 519 if (i >= ch.length) 520 break; 521 prevPos = i; 522 continue; 523 } 524 525 526 527 if (ch[i] == '.') { 528 if (checkForNumeric(ch[i + 1])) { 529 i++; 530 continue; 531 }else{ 532 addToken(tokens, query.substring(prevPos, i).trim(), prevPos,indexes); 533 prevPos=i; 534 } 535 } 536 537 538 if (checkForDelimiter(ch[i])) { 539 addToken(tokens, query.substring(prevPos, i).trim(), prevPos, indexes); 540 prevPos = i; 541 if (!Character.isWhitespace(ch[i])) { 542 addToken(tokens, query.substring(prevPos, prevPos + 1).trim(), prevPos, indexes); 543 prevPos = i + 1; 544 } 545 } 546 547 i++; 548 if (i == ch.length) 549 addToken(tokens, query.substring(prevPos, i).trim(), prevPos, indexes); 550 } 551 catch (Exception pp) { 552 System.err.println("Incorrect syntax at position " + (i + 1) + " near '" + ch[i] + "'"); 553 return null; 554 } 555 } 556 return new Object []{tokens,indexes}; 557 } 558 559 560 561 private boolean checkForDelimiter(char c) { 562 return Arrays.binarySearch(delimiterTokens,c)>=0; 563 } 564 565 566 public static int[] convertToIntArray(Integer [] obj) { 567 int ret[]=new int[obj.length]; 568 for(int i=0;i<obj.length;i++) 569 ret[i]=obj[i].intValue(); 570 return ret; 571 } 572 573 574 public static int[] convertToIntArray(Object [] obj) { 575 int ret[]=new int[obj.length]; 576 for(int i=0;i<obj.length;i++) 577 ret[i]=obj[i].hashCode(); 578 return ret; 579 } 580 581 582 private boolean checkForNumeric(char c) { 583 return (c>=48 && c<=57); 584 } 585 586 587 private boolean checkForPlusMinus(char c) { 588 return (c=='+' || c=='-'); 589 } 590 591 592 private boolean checkForAlphabet(char c) { 593 return ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c == 95); 594 } 595 596 597 private boolean checkForValidity(char c) { 598 if ( (c >= 65 && c <= 90) || (c >= 48 && c <= 57) || (c >= 97 && c <= 122) || c == 95 || c == 63) 599 return true; 600 return checkForDelimiter(c); 601 } 602 603 604 private boolean checkForNumeric_E_Period(char c) { 605 return ((c>=48 && c<=57) || c=='.' || Character.toLowerCase(c)=='e'); 606 } 607 608 609 private boolean checkForMultipliers(char c) { 610 return (Character.toLowerCase(c)=='g' || Character.toLowerCase(c)=='k' || Character.toLowerCase(c)=='m'); 611 } 612 613 614 private void addToken(ArrayList arr, String str, int index, ArrayList inarr) { 615 if (!str.equalsIgnoreCase("")) { 616 arr.add(str); 617 inarr.add(new Integer (index)); 618 } 619 } 620 621 622 private int handleForQuotes(char base, char[] ch, int i, int prevPos,ArrayList tokens, ArrayList indexes) { 623 StringBuffer stbfr = new StringBuffer (); 624 stbfr.append(base); 625 i++; 626 while (i < ch.length) { 627 if (ch[i] == base) { 628 i++; 629 if (i < ch.length && ch[i] == base) 630 stbfr.append(ch[i++]); 631 else 632 break; 633 } 634 stbfr.append(ch[i++]); 635 } 636 stbfr.append(base); 637 addToken(tokens, stbfr.toString().trim(), prevPos, indexes); 638 return i; 639 } 640 641 642 private static void traverseArrayList(Object [] arr) { 643 for (int i = 0; i < arr.length; i++) { 644 ; ArrayList arr2 = (ArrayList) arr[i]; 646 for (int j = 0; j < arr2.size(); j++) { 647 ; } 649 } 650 } 651 } 652 | Popular Tags |