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