001// lexical analyzer for GSQLParser component java version 002 003/**************************************************** 004 Lexical analizer for GSQLParser Java component 005 Copyright (c) 2004-2023 by Gudu software 006****************************************************/ 007 008package gudusoft.gsqlparser; 009 010import java.util.ArrayList; 011import java.util.HashMap; 012import java.io.InputStreamReader; 013 014import java.util.Locale; 015import java.io.BufferedReader; 016import java.io.IOException; 017 018 019public class TLexerPostgresql extends TCustomLexer{ 020 static int yynmarks = 0 ; 021 static int yynmatches ; 022 static int yyntrans ; 023 static int yynstates ; 024 static int[] yyk,yym ; // 1 based 025 static int[] yytint; // 1 based 026 static TYytRec[] yyt ; // 1 based 027 static int[] yykl,yykh,yyml,yymh,yytl,yyth ; // 0 based 028 private static String[] keywordlist; 029 static String table_file; 030 static HashMap<String, Integer> keywordValueList; 031 static HashMap<Integer, Integer> keywordTypeList; 032 static int[][] yystateTable; 033 static HashMap<String, Integer> operatorList; 034 035 boolean isEQuote = false; 036 int languageAfterDoCount = 0; 037 // boolean isReadyForFunctionBody = false, isInFunctionBody = false; 038 // int functionBodyDelimiterIndex = -1; 039 // ArrayList<String> functionBodyDelimiter = new ArrayList<>(); 040 041 static { 042 keywordValueList = new HashMap<String, Integer>(); 043 keywordTypeList = new HashMap<Integer, Integer>(); 044 045 operatorList = new HashMap<String, Integer>(); 046 operatorList.put("+", Integer.valueOf('+')); 047 operatorList.put("-", Integer.valueOf('-')); 048 operatorList.put("*", Integer.valueOf('*')); 049 operatorList.put("/", Integer.valueOf('/')); 050 operatorList.put("%", Integer.valueOf('%')); 051 operatorList.put("^", Integer.valueOf('^')); 052 operatorList.put("@", Integer.valueOf('@')); 053 operatorList.put("&", Integer.valueOf('&')); 054 operatorList.put("|", Integer.valueOf('|')); 055 operatorList.put("~", Integer.valueOf('~')); 056 operatorList.put("!", Integer.valueOf('!')); 057 operatorList.put(">", Integer.valueOf('>')); 058 operatorList.put("<", Integer.valueOf('<')); 059 operatorList.put("#", Integer.valueOf('#')); 060 operatorList.put("?", Integer.valueOf('?')); 061 operatorList.put("<=", TBaseType.less_equal); 062 operatorList.put("<<", TBaseType.OP_LESS_LESS); 063 operatorList.put("<<<", TBaseType.OP_LESS_LESS_LESS); 064 operatorList.put("<<|", TBaseType.OP_LESS_LESS_BAR); 065 operatorList.put("<<=", TBaseType.OP_LESS_LESS_EQUAL); 066 operatorList.put("<>", TBaseType.not_equal); 067 operatorList.put("<->", TBaseType.OP_LESS_MINUS_GREAT); 068 operatorList.put("<-", TBaseType.assign_sign); 069 operatorList.put("<@", TBaseType.OP_LESS_AT); 070 operatorList.put("<^", TBaseType.OP_LESS_CARET); 071 operatorList.put("<%", TBaseType.OP_LESS_PERCENT); 072 operatorList.put("<#>", TBaseType.OP_LESS_HASH_GREAT); 073 operatorList.put("<=>", TBaseType.OP_LESS_EQUAL_GREAT); 074 operatorList.put("<+>", TBaseType.OP_LESS_PLUS_GREAT); 075 operatorList.put("<~>", TBaseType.OP_LESS_TILDE_GREAT); 076 operatorList.put("<%>", TBaseType.OP_LESS_PERCENT_GREAT); 077 operatorList.put(">=", TBaseType.great_equal); 078 operatorList.put(">%", TBaseType.OP_GREAT_PERCENT); 079 operatorList.put(">>", TBaseType.OP_GREAT_GREAT); 080 operatorList.put(">>>", TBaseType.OP_GREAT_GREAT_GREAT); 081 operatorList.put(">>=", TBaseType.OP_GREAT_GREAT_EQUAL); 082 operatorList.put(">^", TBaseType.OP_GREAT_CARET); 083 operatorList.put("!=", TBaseType.not_equal); 084 operatorList.put("!!", TBaseType.OP_EXCLAMATION_EXCLAMATION); 085 operatorList.put("!~", TBaseType.OP_EXCLAMATION_TILDE); 086 operatorList.put("!~~", TBaseType.OP_EXCLAMATION_TIDLE_TIDLE); 087 operatorList.put("!~*", TBaseType.OP_EXCLAMATION_TIDLE_STAR); 088 operatorList.put("!~~*", TBaseType.OP_EXCLAMATION_TIDLE_TIDLE_STAR); 089 operatorList.put("^=", TBaseType.not_equal); 090 operatorList.put("*=", TBaseType.OP_STAR_EQUAL); 091 operatorList.put("*<", TBaseType.OP_STAR_LESS); 092 operatorList.put("*>", TBaseType.OP_STAR_GREAT); 093 operatorList.put("*<>", TBaseType.OP_STAR_LESS_GREAT); 094 operatorList.put("*<=", TBaseType.OP_STAR_LESS_EQUAL); 095 operatorList.put("*>=", TBaseType.OP_STAR_GREAT_EQUAL); 096 operatorList.put("|/", TBaseType.OP_SQUARE_ROOT); 097 operatorList.put("||", TBaseType.concatenationop); 098 operatorList.put("||/", TBaseType.OP_CUBE_ROOT); 099 operatorList.put("|>>", TBaseType.OP_BAR_GREAT_GREAT); 100 operatorList.put("|&>", TBaseType.OP_BAR_PUNCTUATION_GREAT); 101 operatorList.put("~~", TBaseType.OP_TILDE_TILDE); 102 operatorList.put("~~*", TBaseType.OP_TILDE_TILDE_STAR); 103 operatorList.put("~*", TBaseType.OP_TILDE_STAR); 104 operatorList.put("~=", TBaseType.OP_TILDE_EQUAL); 105 operatorList.put("~>~", TBaseType.OP_TILDE_GREAT_TILDE); 106 operatorList.put("~<~", TBaseType.OP_TILDE_LESS_TILDE); 107 operatorList.put("~>=~", TBaseType.OP_TILDE_GREAT_EQUAL_TILDE); 108 operatorList.put("~<=~", TBaseType.OP_TILDE_LESS_EQUAL_TILDE); 109 operatorList.put("@@", TBaseType.OP_AT_AT); 110 operatorList.put("@@@", TBaseType.OP_AT_AT_AT); 111 operatorList.put("@>", TBaseType.OP_AT_GREAT); 112 operatorList.put("@?", TBaseType.OP_AT_QUESTION); 113 operatorList.put("@-@", TBaseType.OP_AT_MINUS_AT); 114 operatorList.put("##", TBaseType.OP_POUND_POUND); 115 operatorList.put("#>", TBaseType.OP_POUND_GREAT); 116 operatorList.put("#>>", TBaseType.OP_POUND_GREAT_GREAT); 117 operatorList.put("#-", TBaseType.OP_POUND_MINUS); 118 operatorList.put("&&", TBaseType.OP_PUNCTUATION_PUNCTUATION); 119 operatorList.put("&<", TBaseType.OP_PUNCTUATION_LESS); 120 operatorList.put("&<|", TBaseType.OP_PUNCTUATION_LESS_BAR); 121 operatorList.put("&>", TBaseType.OP_PUNCTUATION_GREAT); 122 operatorList.put("?#", TBaseType.OP_QUESTION_POUND); 123 operatorList.put("?-", TBaseType.OP_QUESTION_MINUS); 124 operatorList.put("?-|", TBaseType.OP_QUESTION_MINUS_BAR); 125 operatorList.put("?||", TBaseType.OP_QUESTION_BAR_BAR); 126 operatorList.put("?|", TBaseType.OP_QUESTION_BAR); 127 operatorList.put("?&", TBaseType.OP_QUESTION_PUNCTUATION); 128 operatorList.put("->", TBaseType.OP_MINUS_GREAT); 129 operatorList.put("->>", TBaseType.OP_MINUS_GREAT_GREAT); 130 operatorList.put("-|-", TBaseType.OP_MINUS_BAR_MINUS); 131 132 table_file = "/gudusoft/gsqlparser/parser/postgresql/postgresql_lex_table.txt"; 133 if (TBaseType.enterprise_edition||TBaseType.postgresql_edition){ 134 inittable(); 135 } 136 } 137 138 public TLexerPostgresql(){ 139 super(); 140 dbvendor = EDbVendor.dbvpostgresql; 141 } 142 143public boolean canBeColumnName(int tokencode){ 144 //http://blog.csdn.net/superbeck/article/details/5387476 145 boolean ret = false; 146 int modifiers = keyword_type_identifier | keyword_type_column ; 147 Integer s = keywordTypeList.get(tokencode); 148 if (s != null){ 149 int modifier = s; 150 ret = (modifiers & modifier) == modifier; 151 } 152 153 return ret; 154} 155 156 public int iskeyword(String str){ 157 int ret = -1; 158 Integer s = keywordValueList.get(str.toUpperCase(Locale.ENGLISH)); 159 if( s != null){ 160 ret = s; 161 } 162 return ret;// -1 means not a keyword 163 } 164 165 public int getkeywordvalue(String keyword){ 166 int ret = 0; 167 Integer s = keywordValueList.get(keyword.toUpperCase(Locale.ENGLISH)); 168 if( s != null){ 169 ret = s; 170 } 171 return ret;// 0 means not a keyword 172 } 173 174 public static EKeywordType getKeywordType(String keyword){ 175 return TCustomLexer.getKeywordType(keyword,keywordValueList,keywordTypeList); 176 } 177 178 static void yystateLookupConfigure() { 179 int yystates = yytl.length; 180 yystateTable = new int[257][yystates]; 181 182 // initialize to empty 183 for(int i = 0; i < yystates; i++) { 184 for (int j = 0; j < 257; j++) 185 yystateTable[j][i] = -1; 186 } 187 188 for(int i = 0; i < yystates; i++) { 189 int low = yytl[i]; 190 int high = yyth[i]; 191 for (int j = low; j <= high; j++) { 192 for (char c: yyt[j].cc) { 193 yystateTable[c][i] = j; 194 } 195 } 196 } 197 } 198 199 int yylex(){ 200 int yyn; 201 while (true) { // top level while 202 yynew(); 203 while (true){ //scan 204 for(yyn = yykl[yystate]; yyn <= yykh[yystate]; yyn++){ 205 yymark(yyk[yyn]); 206 } 207 208 for(yyn=yymh[yystate]; yyn>= yyml[yystate]; yyn--){ 209 yymatch(yym[yyn]); 210 } 211 212 if(yytl[yystate] > yyth[yystate]){ 213 break; 214 } 215 216 yyscan(); 217// yyn = yytl[yystate]; 218 totablechar(); 219// while( (yyn <= yyth[yystate]) && (!(charinarray(yytablechar,yyt[yyn].cc))) ){ 220// yyn++; 221// } 222// if (yyn > yyth[yystate]){ 223// break; 224// } 225 226 yyn = yystateTable[yytablechar][yystate]; 227 if (yyn == -1) 228 break; 229 230 yystate = yyt[yyn].s; 231 } //scan 232 233 while (true){ //action 234 int yyrule; 235 if ( (yyrule = yyfind()) != -1 ){ 236 yyaction(yyrule); 237 if (yyreject){ 238 continue; 239 } 240 }else if( (!yydefault() ) && (yywrap()) ){ 241 yyclear(); 242 returni(0); 243 } 244 break; 245 } 246 247 if (!yydone) { 248 continue; 249 } 250 break; 251 } // top level while 252 253 return yyretval; 254 } 255 256 static void inittable(){ 257 258 //if (yynmarks > 0) return; //init table already 259 260 String line; 261 boolean inyyk=false,inyym=false,inyykl=false,inyykh=false,inyyml=false,inyymh=false,inyytl=false,inyyth=false,inyytint=false,inyyt=false,inkeyword=false; 262 int yyk_count=0,yym_count=0,yykl_count=0,yykh_count=0,yyml_count=0,yymh_count=0,yytl_count=0,yyth_count=0,yytint_count=0,yyt_count=0; 263 int c=0; 264 keywordValueList.clear(); 265 keywordTypeList.clear(); 266 267 BufferedReader br = new BufferedReader(new InputStreamReader(TLexerPostgresql.class.getResourceAsStream(table_file))); 268 269 try{ 270 while( (line = br.readLine()) != null){ 271 if (line.trim().startsWith("yynmarks=")){ 272 String[] ss = line.split("[=;]"); 273 yynmarks=Integer.parseInt(ss[1].trim()); 274 yyk = new int[yynmarks+1]; 275 }else if (line.trim().startsWith("yynmatches=")){ 276 String[] ss = line.split("[=;]"); 277 yynmatches=Integer.parseInt(ss[1].trim()); 278 yym = new int[yynmatches+1]; 279 }else if (line.trim().startsWith("yyntrans=")){ 280 String[] ss = line.split("[=;]"); 281 yyntrans=Integer.parseInt(ss[1].trim()); 282 yytint = new int[yyntrans+1]; 283 yyt = new TYytRec[yyntrans+1]; 284 }else if (line.trim().startsWith("yynstates=")){ 285 String[] ss = line.split("[=;]"); 286 yynstates=Integer.parseInt(ss[1].trim()); 287 yykl = new int[yynstates]; 288 yykh = new int[yynstates]; 289 yyml = new int[yynstates]; 290 yymh = new int[yynstates]; 291 yytl = new int[yynstates]; 292 yyth = new int[yynstates]; 293 }else if (line.trim().startsWith("<end>")){ 294 if (inyyk){ 295 inyyk = false; 296 if (yynmarks+1 != yyk_count ){ 297 System.out.println("required1:"+(yynmarks)+" actually:"+(yyk_count-1)); 298 } 299 } 300 else if(inyym){ 301 inyym = false; 302 if (yynmatches+1 != yym_count ){ 303 System.out.println("required2:"+(yynmatches)+" actually:"+(yym_count-1)); 304 } 305 } 306 else if(inyykl){ 307 inyykl = false; 308 if (yynstates != yykl_count ){ 309 System.out.println("required3:"+(yynstates)+" actually:"+(yykl_count)); 310 } 311 } 312 else if(inyykh){ 313 inyykh = false; 314 if (yynstates != yykh_count ){ 315 System.out.println("required4:"+(yynstates)+" actually:"+(yykh_count)); 316 } 317 } 318 else if(inyyml){ 319 inyyml = false; 320 if (yynstates != yyml_count ){ 321 System.out.println("required5:"+(yynstates)+" actually:"+(yyml_count)); 322 } 323 } 324 else if(inyymh){ 325 inyymh = false; 326 if (yynstates != yymh_count ){ 327 System.out.println("required:"+(yynstates)+" actually:"+(yymh_count)); 328 } 329 } 330 else if(inyytl){ 331 inyytl = false; 332 if (yynstates != yytl_count ){ 333 System.out.println("required6:"+(yynstates)+" actually:"+(yytl_count)); 334 } 335 } 336 else if(inyyth){ 337 inyyth = false; 338 if (yynstates != yyth_count ){ 339 System.out.println("required7:"+(yynstates)+" actually:"+(yyth_count)); 340 } 341 } 342 else if(inyytint){ 343 inyytint = false; 344 if (yyntrans + 1 != yytint_count ){ 345 System.out.println("required8:"+(yyntrans)+" actually:"+(yytint_count-1)); 346 } 347 } 348 else if(inyyt){ 349 inyyt = false; 350 if (yyntrans+1 != yyt_count ){ 351 System.out.println("required9:"+(yyntrans)+" actually:"+(yyt_count-1)); 352 } 353 } 354 else if(inkeyword){ 355 inkeyword = false; 356 } 357 }else if(line.trim().startsWith("yyk =")){ 358 inyyk = true; 359 }else if(line.trim().startsWith("yym =")){ 360 inyym = true; 361 }else if(line.trim().startsWith("yykl =")){ 362 inyykl = true; 363 }else if(line.trim().startsWith("yykh =")){ 364 inyykh = true; 365 }else if(line.trim().startsWith("yyml =")){ 366 inyyml = true; 367 }else if(line.trim().startsWith("yymh =")){ 368 inyymh = true; 369 }else if(line.trim().startsWith("yytl =")){ 370 inyytl = true; 371 }else if(line.trim().startsWith("yyth =")){ 372 inyyth = true; 373 }else if(line.trim().startsWith("yytint =")){ 374 inyytint = true; 375 }else if(line.trim().startsWith("yyt =")){ 376 inyyt = true; 377 }else if(line.trim().startsWith("keywordsvalue =")){ 378 inkeyword = true; 379 }else if(inyyk){ 380 String[] ss = line.split("[,]"); 381 for(int j=0;j<ss.length;j++){ 382 // System.out.println(ss[j].trim()); 383 yyk[yyk_count++] = Integer.parseInt(ss[j].trim()); 384 } 385 }else if(inyym){ 386 String[] ss = line.split("[,]"); 387 for(int j=0;j<ss.length;j++){ 388 // System.out.println(ss[j].trim()); 389 yym[yym_count++] = Integer.parseInt(ss[j].trim()); 390 } 391 }else if(inyykl){ 392 String[] ss = line.split("[,]"); 393 for(int j=0;j<ss.length;j++){ 394 // System.out.println(ss[j].trim()); 395 yykl[yykl_count++] = Integer.parseInt(ss[j].trim()); 396 } 397 }else if(inyykh){ 398 String[] ss = line.split("[,]"); 399 for(int j=0;j<ss.length;j++){ 400 // System.out.println(ss[j].trim()); 401 yykh[yykh_count++] = Integer.parseInt(ss[j].trim()); 402 } 403 }else if(inyyml){ 404 String[] ss = line.split("[,]"); 405 for(int j=0;j<ss.length;j++){ 406 // System.out.println(ss[j].trim()); 407 yyml[yyml_count++] = Integer.parseInt(ss[j].trim()); 408 } 409 }else if(inyymh){ 410 String[] ss = line.split("[,]"); 411 for(int j=0;j<ss.length;j++){ 412 // System.out.println(ss[j].trim()); 413 yymh[yymh_count++] = Integer.parseInt(ss[j].trim()); 414 } 415 }else if(inyytl){ 416 String[] ss = line.split("[,]"); 417 for(int j=0;j<ss.length;j++){ 418 // System.out.println(ss[j].trim()); 419 yytl[yytl_count++] = Integer.parseInt(ss[j].trim()); 420 } 421 }else if(inyyth){ 422 String[] ss = line.split("[,]"); 423 for(int j=0;j<ss.length;j++){ 424 // System.out.println(ss[j].trim()); 425 yyth[yyth_count++] = Integer.parseInt(ss[j].trim()); 426 } 427 }else if(inyytint){ 428 String[] ss = line.split("[,]"); 429 for(int j=0;j<ss.length;j++){ 430 // System.out.println(ss[j].trim()); 431 yytint[yytint_count++] = Integer.parseInt(ss[j].trim()); 432 } 433 }else if(inyyt){ 434 //System.out.println(line.trim()); 435 436 c = 0; 437 String[] st = line.trim().split(",,"); 438 char[] tmp = new char[st.length]; 439 for(int i=0;i<st.length;i++){ 440 441 if(st[i].startsWith("\'")) { 442 if(st[i].length() == 3){ // 'a' 443 tmp[c++] = st[i].charAt(1); 444 }else if(st[i].length() == 4) { // '\\' 445 tmp[c++] = st[i].charAt(2); 446 }else{ 447 System.out.println(" read yytstr error, error string is "+st[i]+ "line: "+ yyt_count); 448 } 449 }else{ 450 try{ 451 tmp[c++] = (char)Integer.parseInt(st[i]); // char in number like 32 that represent space 452 } catch (NumberFormatException nfe) { 453 System.out.println("NumberFormatException: " + nfe.getMessage()); 454 } 455 } 456 } //while hasmoreTokens 457 458 //yyt[lineno] = new YYTrec(tmp,yytint[lineno]); 459 yyt[yyt_count] = new TYytRec(tmp,yytint[yyt_count]); 460 yyt_count++; 461 462 }else if(inkeyword){ 463 String[] ss =line.split("[=]"); 464 465 int val1 = -1; 466 int val2 = -1; 467 try { 468 val1 = Integer.parseInt(ss[1]); 469 val2 = Integer.parseInt(ss[2]); 470 } 471 catch (NumberFormatException nfe) { 472 System.out.println("NumberFormatException: " + nfe.getMessage()); 473 } 474 keywordValueList.put(ss[0].toUpperCase(),val1); 475 keywordTypeList.put(val1,val2); 476 } 477 } 478 }catch(IOException e){ 479 System.out.println(e.toString()); 480 } 481 482 yystateLookupConfigure(); 483 484 } 485 486 487 void yyaction(int yyruleno){ 488 489 490 int ic; 491 char[] tmparray = {'=','+','-','*','/','>','<'}; 492 493 yylvalstr = getyytext(); 494 /* actions: */ 495 switch(yyruleno){ 496 case 1: 497 498 { 499 if (yylvalstr.equalsIgnoreCase(dolqstart)) 500 { 501 //dolqstart = ""; 502 start(init); 503 addlit(yylvalstr,yytextlen); 504 yylvalstr = litbufdup(); 505 returni(sconst); 506 } 507 else 508 { 509 //nchars = yytextlen; 510 addlit(yylvalstr, yytextlen-1); 511 yyless(yytextlen-1); 512 return; 513 } 514 //System.out.println("<xdolq>{dolqdelim}: "+dolqstart); 515 break; 516 } 517 518 case 2: 519 520 { 521 522 if (isReadyForFunctionBody) { // meet the first $$ 523 //isInFunctionBody = true; 524 isReadyForFunctionBody = false; 525 functionBodyDelimiterIndex++; 526 functionBodyDelimiter.add(yylvalstr); 527 //System.out.println("start function body:"+functionBodyDelimiter.get(functionBodyDelimiterIndex)); 528 returni(postgresql_function_delimiter); 529 }else if ((functionBodyDelimiterIndex>=0)&&(functionBodyDelimiter.get(functionBodyDelimiterIndex).equalsIgnoreCase(yylvalstr))){ // meet the second $$ 530 //isInFunctionBody = false; 531 //System.out.println("end function body:"+functionBodyDelimiter.get(functionBodyDelimiterIndex)); 532 functionBodyDelimiter.remove(functionBodyDelimiterIndex); 533 functionBodyDelimiterIndex--; 534 535 returni(postgresql_function_delimiter); 536 }else { 537 if (getyysstate() == xq) { 538 addlit(yylvalstr, yytextlen); 539 } else { 540 start(xdolq); 541 startlit(); 542 dolqstart = yylvalstr; 543 addlit(yylvalstr,yytextlen); 544 //System.out.println("dolqdelim: "+dolqstart); 545 } 546 } 547 548 break; 549 } 550 551 case 3: 552 553 { 554 addlit(yylvalstr, yytextlen); 555 //System.out.println("<xdolq>{dolqinside}: "+yylvalstr); 556 break; 557 } 558 559 case 4: 560 561 { 562 addlit(yylvalstr, yytextlen); 563 break; 564 } 565 566 case 5: 567 568 { 569 addlitchar(yylvalstr.charAt(0)); 570 break; 571 } 572 573 574 case 6: 575 576 { 577 if (getyysstate() == xq) 578 { 579 nchars = yytextlen; 580 addlit(yylvalstr, yytextlen-1); 581 yyless(nchars-1); 582 return;//exit; 583 } 584 585 start(xq); 586 startlit(); 587 addlit(yylvalstr,yytextlen); 588 break; 589 } 590 591 case 7: 592 593 { 594 if (getyysstate() == xq) 595 { 596 nchars = yytextlen; 597 addlit(yylvalstr, yytextlen-1); 598 yyless(nchars-1); 599 return;//exit; 600 } 601 602 start(xq); 603 startlit(); 604 addlit(yylvalstr,yytextlen); 605 break; 606 } 607 608 case 8: 609 610 { 611 if (getyysstate() == xq) 612 { 613 nchars = yytextlen; 614 addlit(yylvalstr, yytextlen-1); 615 yyless(nchars-1); 616 return;//exit; 617 } 618 619 start(xq); 620 startlit(); 621 addlit(yylvalstr,yytextlen); 622 break; 623 } 624 625 case 9: 626 627 { 628 addlit(yylvalstr,yytextlen); 629 if (xcdepth <= 0) 630 { 631 start(init); 632 yylvalstr = litbufdup(); 633 returni(cmtslashstar); 634 } 635 else 636 xcdepth--; 637 638 break; 639 } 640 641 642 case 10: 643 644 { 645 if (yylvalstr.startsWith("/*")){ 646 xcdepth++; 647 yyless(2); 648 addlit(yylvalstr,yytextlen); 649 }else{ 650 yyless(1); 651 addlit(yylvalstr,1); 652 } 653 654 break; 655 656 } 657 658 case 11: 659 660 { 661 662 if (getyysstate() == xq) 663 { 664 nchars = yytextlen; 665 addlit(yylvalstr, yytextlen-1); 666 yyless(nchars-1); 667 return;//exit; 668 } 669 670 xcdepth = 0; 671 start(xc); 672 startlit(); 673 yyless(2); 674 addlit(yylvalstr,yytextlen); 675 676 break; 677 } 678 679 case 12: 680 681 { 682 addlit(yylvalstr,yytextlen); 683 684 break; 685 } 686 687 case 13: 688 689 { 690 addlitchar(yylvalstr.charAt(0)); 691 692 break; 693 } 694 695 case 14: 696 697 { 698 start(init); 699 isEQuote = false; 700 addlit(yylvalstr, yytextlen); 701 yylvalstr = litbufdup(); 702 if( yylvalstr.startsWith("b")|| (yylvalstr.startsWith("B"))){ 703 returni(bconst); 704 }else if( yylvalstr.startsWith("x")|| (yylvalstr.startsWith("X"))){ 705 returni(xconst); 706 }else 707 { 708 returni(sconst); 709 } 710 break; 711 } 712 713 case 15: 714 715 { 716 if (insqlpluscmd){ 717 yyless(0); 718 yylvalstr = litbufdup(); 719 start(init); 720 returni(sconst); 721 }else{ 722 addlit(yylvalstr,yytextlen); 723 } 724 725 break; 726 } 727 case 16: 728 729 { 730 if (getyysstate() == xq) 731 { 732 nchars = yytextlen; 733 addlit(yylvalstr, yytextlen-1); 734 yyless(nchars-1); 735 return;//exit; 736 } 737 738 start(xq); 739 if (yylvalstr.startsWith("E")||yylvalstr.startsWith("e")) {isEQuote = true;} 740 startlit(); 741 addlit(yylvalstr,yytextlen); 742 743 dummych1 = get_char(); 744 if (dummych1 == '\\') // recognize string like '\' 745 { 746 dummych2 = get_char(); 747 if (dummych2 == '\'') 748 { 749 // start(init); 750 addlit("\\", 1); 751 addlit("\'", 1); 752 //yylvalstr = litbufdup(); 753 //returni(sconst); 754 } 755 else 756 { 757 unget_char(dummych2); 758 unget_char(dummych1); 759 } 760 } 761 else 762 { unget_char(dummych1);} 763 764 break; 765 } 766 767 case 17: 768 769 { 770 start(xq); 771 startlit(); 772 addlit(yylvalstr, yytextlen); 773 dummych1 = get_char(); 774 if (dummych1 == '\\') // recognize string like '\' 775 { 776 dummych2 = get_char(); 777 if (dummych2 == '\'') 778 { 779 start(init); 780 addlit("\\", 1); 781 addlit("\'", 1); 782 yylvalstr = litbufdup(); 783 returni(sconst); 784 } 785 else 786 { 787 unget_char(dummych2); 788 unget_char(dummych1); 789 } 790 } 791 else 792 { unget_char(dummych1);} 793 794 break; 795 } 796 797 case 18: 798 799 { 800 addlit(yylvalstr, yytextlen); 801 break; 802 } 803 804 case 19: 805 806 { 807 dummych1 = get_char(); 808 unget_char(dummych1); 809 if (dummych1 == (char)10) 810 { 811 if (insqlpluscmd){ 812 nchars = yytextlen; 813 if(yylvalstr.charAt(nchars-1) == (char)13){ 814 yyless(nchars - 1); 815 yylvalstr = yylvalstr.substring(0,nchars); 816 } 817 start(init); 818 addlit(yylvalstr, nchars-1); 819 yylvalstr = litbufdup(); 820 returni(sconst); //in sqlplus command, characters between ' and return is treated as a string 821 822 }else{ 823 dummych1 = get_char(); 824 addlit(yylvalstr+dummych1, yytextlen+1); 825 } 826 }else if ((dummych1 == '\\')&&(isEQuote)) 827 { 828 dummych1 = get_char(); 829 dummych2 = get_char(); 830 addlit(yylvalstr+dummych1+dummych2, yytextlen+2); 831 if (dummych2 == '\'') { 832 dummych3 = get_char(); 833 if (dummych3 != '\'') 834 unget_char(dummych3); 835 } 836 837 //addlit(yylvalstr, yytextlen); 838 } 839 else 840 { addlit(yylvalstr, yytextlen);} 841 842 break; 843 } 844 845 case 20: 846 847 { 848 addlit(yylvalstr, yytextlen); 849 //System.out.println("Found xqliteral"+yylvalstr); 850 break; 851 } 852 853 854 855 case 21: 856 857 { 858 dummych1 = get_char(); 859 unget_char(dummych1); 860 if (dummych1 == '\''){ 861 dummych1 = get_char(); 862 addlit(yylvalstr+dummych1, yytextlen+1); 863 }else{ 864 865 start(init); 866 addlit(yylvalstr, yytextlen); 867 if ((literallen == 0) && (!insqlpluscmd)) 868 {returni (error);} 869 if (literallen >= namedatalen) 870 { 871 setlengthofliteralbuf(namedatalen); 872 literallen = namedatalen; 873 } 874 yylvalstr = litbufdup(); 875 returni (ident); 876 877 } 878 879 880 break; 881 } 882 883 884 case 22: 885 886 { 887 if (insqlpluscmd){ 888 yyless(0); 889 yylvalstr = litbufdup(); 890 start(init); 891 returni(sconst); 892 }else{ 893 addlit(yylvalstr, yytextlen); 894 } 895 896 break; 897 } 898 899 case 23: 900 901 { 902 addlit(yylvalstr, yytextlen); 903 break; 904 } 905 906 case 24: 907 908 { 909 /* Unicode escape identifier U&"..." - treated same as double-quoted identifier */ 910 start(xd); 911 startlit(); 912 addlit(yylvalstr, yytextlen); 913 break; 914 } 915 916 case 25: 917 918 { 919 start(xd); 920 startlit(); 921 addlit(yylvalstr, yytextlen); 922 break; 923 } 924 925 case 26: 926 927 { 928 dummych1 = get_char(); 929 unget_char(dummych1); 930 if (dummych1 == (char)10) 931 { 932 if (insqlpluscmd){ 933 nchars = yytextlen; 934 if(yylvalstr.charAt(nchars-1) == (char)13){ 935 yyless(nchars - 1); 936 yylvalstr = yylvalstr.substring(0,nchars); 937 } 938 start(init); 939 addlit(yylvalstr, nchars-1); 940 yylvalstr = litbufdup(); 941 returni(sconst); //in sqlplus command, characters between ' and return is treated as a string 942 943 }else{ 944 dummych1 = get_char(); 945 addlit(yylvalstr+dummych1, yytextlen+1); 946 } 947 948 } else 949 addlit(yylvalstr, yytextlen); 950 951 break; 952 } 953 954 case 27: 955 956 { 957 returni(lexnewline); 958 break; 959 } 960 961 case 28: 962 963 { 964 returni(lexspace); 965 break; 966 } 967 968 case 29: 969 970 { 971 if ((getyysstate() == xq) 972 || (getyysstate() == xd) 973 || (getyysstate() == xc) 974 ) 975 { 976 addlit(yylvalstr, 1); 977 yyless(1); 978 return;//exit; 979 } 980 981 returni(cmtdoublehyphen); 982 break; 983 } 984 985 case 30: 986 987 { 988 returnc(yylvalstr.charAt(0)); 989 break; 990 } 991 992 case 31: 993 994 { 995 returni(cmpop); 996 break; 997 } 998 999 case 32: 1000 1001 { 1002 1003 if (getyysstate() == xc) 1004 { 1005 slashstar = yylvalstr.indexOf("*/"); 1006 if (slashstar >= 0) 1007 { 1008 start(init); 1009 addlit(yylvalstr,slashstar+2); 1010 yylvalstr = litbufdup(); 1011 yyless(slashstar+2); 1012 returni(cmtslashstar); 1013 } 1014 else 1015 { 1016 addlit(yylvalstr,1); 1017 yyless(1); 1018 } 1019 } 1020 else if (getyysstate() == xq) 1021 { 1022 addlit(yylvalstr,1); 1023 yyless(1); 1024 } 1025 else 1026 { 1027 nchars = yytextlen; 1028 slashstar = yylvalstr.indexOf("/*"); 1029 dashdash = yylvalstr.indexOf("--"); 1030 if ((slashstar > 0) && (dashdash > 0)) 1031 { 1032 //if both appear, take the first one 1033 if (slashstar > dashdash) 1034 {slashstar = dashdash;} 1035 } 1036 else 1037 { 1038 // if slashstar=0 then slashstar := dashdash; 1039 // add (getyysstate <> xc) to avoid something like this */--,here */ should be handled instead of -- 1040 if ((slashstar > 0) && (getyysstate() != xc)) { 1041 nchars = slashstar; 1042 } 1043 } 1044 1045 boolean searchOp = false; 1046 while (nchars > 0){ 1047 String opStr = yylvalstr.substring(0,nchars); 1048 Integer opCode = operatorList.get(opStr); 1049 if (opCode != null){ 1050 // this is not an operator 1051 if (nchars < yytextlen){ 1052 yyless(nchars); 1053 yylvalstr = yylvalstr.substring(0,nchars); 1054 } 1055 searchOp = true; 1056 returni(opCode); 1057 break; 1058 }else{ 1059 1060 } 1061 nchars--; 1062 } 1063 1064 if (searchOp) break; 1065 1066 nchars = yytextlen; // reset nchars 1067 1068 while ((nchars > 1) 1069 && ( (yylvalstr.charAt(nchars-1) == '+' ) 1070 || (yylvalstr.charAt(nchars-1) =='-')) 1071 && (getyysstate() != xc)) 1072 { 1073 for (ic = nchars - 1; ic>=1; ic--) 1074 { 1075 if (isopchar(yylvalstr.charAt(ic-1))) break; 1076 } 1077 if (ic >= 1) break; 1078 nchars--; 1079 } 1080 1081 if (nchars < yytextlen) 1082 { 1083 //Strip the unwanted chars from the token 1084 yyless(nchars); 1085 yylvalstr = yylvalstr.substring(0,nchars); 1086 } 1087 1088 ///* 1089 // * If what we have left is only one char, and it's 1090 // * one of the characters matching "self", then 1091 // * return it as a character token the same way 1092 // * that the "self" rule would have. 1093 // * make sure @ return as self char, by james wang 1094 // */ 1095 if ((nchars == 1) && (isselfchar(yylvalstr.charAt(0)) || (yylvalstr.charAt(0) == '@'))) 1096 { 1097 returnc(yylvalstr.charAt(0)); 1098 } 1099 else if ( 1100 (nchars >= 2) 1101 &&( 1102 charinarray(yylvalstr.charAt(nchars-1-1), tmparray) 1103 && ((yylvalstr.charAt(nchars-1) == ':') 1104 ||(yylvalstr.charAt(nchars-1) == '.') 1105 ) 1106 ) 1107 ) 1108 { 1109 yyless(nchars-1); 1110 yylvalstr = yylvalstr.substring(0,nchars-1); 1111 if (nchars == 2) 1112 returnc(yylvalstr.charAt(0)); 1113 else 1114 returni(op); 1115 } 1116 else if ( 1117 (nchars >= 2) 1118 && ( 1119 charinarray(yylvalstr.charAt(nchars-1-1),tmparray) 1120 && (yylvalstr.charAt(nchars-1) == '&') 1121 ) 1122 ) 1123 { 1124 yyless(nchars-1); 1125 yylvalstr = yylvalstr.substring(0,nchars-1); 1126 if (nchars == 2) 1127 returnc(yylvalstr.charAt(0)); 1128 else 1129 returni(op); 1130 } 1131 else if ( (nchars == 2) && (yylvalstr.charAt(0) == '.') && (yylvalstr.charAt(1) == '*')) 1132 { 1133 yyless(1); 1134 returnc(yylvalstr.charAt(0)); 1135 } 1136 else if ((nchars == 2) && ((yylvalstr.charAt(0) == '=') && ( (yylvalstr.charAt(1) == '?')) )) 1137 { 1138 yyless(1); 1139 returnc(yylvalstr.charAt(0)); 1140 } 1141 else if ( (nchars >= 2) && ((yylvalstr.charAt(0) == '@') && (yylvalstr.charAt(1) != '>') && (yylvalstr.charAt(1) != '@'))) 1142 { 1143 yyless(1); 1144 returnc(yylvalstr.charAt(0)); 1145 } 1146 else if ( (nchars >= 2) && (yylvalstr.charAt(0) == '?')&&(yylvalstr.charAt(1) != '&')&&(yylvalstr.charAt(1) != '|')) 1147 { 1148 yyless(1); 1149 returnc(yylvalstr.charAt(0)); 1150 } 1151 else if (((nchars > 2) && (yylvalstr.charAt(0) == '*')) 1152 && (yylvalstr.charAt(1) == '/') 1153 && (getyysstate() == xc) 1154 ) 1155 { //in comment, and find */ , then it must the end of comment 1156 yyless(2); 1157 addlit(yylvalstr,yytextlen); 1158 if (xcdepth <= 0) 1159 { 1160 start(init); 1161 yylvalstr = litbufdup(); 1162 returni(cmtslashstar); 1163 } 1164 else 1165 xcdepth--; 1166 } 1167 else 1168 returni(op); 1169 } 1170 1171 break; 1172 } 1173 1174 case 33: 1175 1176 { 1177 returni(iconst); 1178 break; 1179 } 1180 1181 case 34: 1182 1183 { 1184 ///* for i in 1..5 loop, we can't recognize 1. as a decimal,but 1 as decimal 1185 nchars = yytextlen; 1186 if (yylvalstr.charAt(nchars-1) == '.') 1187 { 1188 dummych1 = get_char(); 1189 unget_char(dummych1); 1190 if (dummych1 == '.') 1191 { 1192 yyless(nchars-1); 1193 yylvalstr = yylvalstr.substring(0,nchars - 1); 1194 returni (iconst); 1195 return;//exit; 1196 } 1197 } 1198 returni (fconst); 1199 break; 1200 } 1201 1202 case 35: 1203 1204 { 1205 returni (fconst); 1206 break; 1207 } 1208 1209 case 36: 1210 1211 { 1212 boolean dollarConstant = false; 1213 if (getyysstate() == xdolq){ 1214 int p = yylvalstr.indexOf("$"); 1215 if (p > 0){ 1216 dollarConstant = true; 1217 addlit(yylvalstr, p); 1218 yyless(p); 1219 return; 1220 } 1221 } 1222 1223 if (functionBodyDelimiterIndex >= 0){ // deal case like: END$BODY$ 1224 int p = yylvalstr.indexOf("$"); 1225 if (p > 0){ 1226 yylvalstr = yylvalstr.substring(0,p); 1227 addlit(yylvalstr, p); 1228 yyless(p); 1229 } 1230 } 1231 1232 if (!dollarConstant){ 1233 int rw; 1234 if ( (rw = iskeyword(yylvalstr)) != -1) { 1235 //System.out.println(yylvalstr+",rw:"+rw); 1236 if ((rw == TBaseType.rrw_as)||(rw == TBaseType.rrw_do)){ 1237 isReadyForFunctionBody = true; 1238 languageAfterDoCount = 0; 1239 }else if (isReadyForFunctionBody && rw == TBaseType.rrw_postgresql_language){ 1240 // DO LANGUAGE <name> $$ - keep flag, allow 1 identifier 1241 languageAfterDoCount = 1; 1242 }else{ 1243 isReadyForFunctionBody = false; 1244 languageAfterDoCount = 0; 1245 } 1246 returni(rw); 1247 } 1248 else 1249 { 1250 if (languageAfterDoCount > 0) { 1251 languageAfterDoCount--; 1252 // Keep isReadyForFunctionBody true for language name 1253 } else { 1254 isReadyForFunctionBody = false; 1255 } 1256 returni(ident); 1257 } 1258 } 1259 break; 1260 } 1261 1262 case 37: 1263 1264 { 1265 if (getyysstate() == xdolq){ 1266 addlit(yylvalstr, yytextlen); 1267 return; 1268 }else{ 1269 returni (param); 1270 } 1271 1272 break; 1273 } 1274 1275 1276 case 38: 1277 1278 { 1279 returni (typecast); 1280 break; 1281 } 1282 1283 case 39: 1284 1285 { 1286 returni (double_dot); 1287 break; 1288 } 1289 1290 case 40: 1291 1292 { 1293 returni (assign_sign); 1294 break; 1295 } 1296 1297 case 41: 1298 1299 { 1300 returni(ident); 1301 break; 1302 } 1303 1304 case 42: 1305 1306 { 1307 returni (bind_v); 1308 break; 1309 } 1310 1311 case 43: 1312 1313 { 1314 returni (error); 1315 break; 1316 } 1317 1318 default:{ 1319 System.out.println("fatal error in yyaction"); 1320 } 1321 }//switch 1322}/*yyaction*/; 1323 1324 1325 1326 }