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