001// lexical analyzer  for GSQLParser component java version
002
003/****************************************************}
004{   Lexical analizer for GSQLParser component java version    }
005{   Copyright (c) 2004-2016 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 TLexerCouchbase 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        
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/couchbase/couchbase_lex_table.txt";
038                  if (TBaseType.enterprise_edition||TBaseType.couchbase_edition){
039                inittable();
040          }
041    }
042
043
044    public TLexerCouchbase(){
045          super();
046          dbvendor = EDbVendor.dbvcouchbase;
047          sourcetokens = new TSourceTokenList();
048
049    }
050
051
052    public TSourceTokenList sourcetokens;
053
054
055    
056public boolean canBeColumnName(int tokencode){
057    //http://blog.csdn.net/superbeck/article/details/5387476
058    boolean ret = false;
059    int modifiers = keyword_type_identifier | keyword_type_column ;
060    Integer s = keywordTypeList.get(tokencode);
061     if (s != null){
062        int modifier = s;
063        ret = (modifiers & modifier) == modifier;
064    }
065
066    return ret;
067}
068
069 public  int iskeyword(String str){
070        int ret = -1;
071       Integer s = keywordValueList.get(str.toUpperCase(Locale.ENGLISH));
072        if( s != null){
073            ret = s;
074        }
075        return ret;// -1 means not a keyword
076     }
077
078     public int getkeywordvalue(String keyword){
079        int ret = 0;
080        Integer s = keywordValueList.get(keyword.toUpperCase(Locale.ENGLISH));
081        if( s != null){
082            ret = s;
083         }
084        return ret;// 0 means not a keyword
085     }
086         
087    public static EKeywordType getKeywordType(String keyword){
088        return TCustomLexer.getKeywordType(keyword,keywordValueList,keywordTypeList);
089    }            
090
091
092    int issystemvariable(String str){
093        return -1;// -1 means not a system variable
094    }
095
096    static void yystateLookupConfigure() {
097        int yystates = yytl.length;
098        yystateTable = new int[257][yystates];
099
100        // initialize to empty
101        for(int i = 0; i < yystates; i++) {
102            for (int j = 0; j < 257; j++)
103                yystateTable[j][i] = -1;
104        }
105
106        for(int i = 0; i < yystates; i++) {
107            int low = yytl[i];
108            int high = yyth[i];
109            for (int j = low; j <= high; j++) {
110                for (char c: yyt[j].cc) {
111                    yystateTable[c][i] = j;
112                }
113            }
114        }
115    }
116        
117    int yylex(){
118          int yyn;
119           while (true) { // top level while
120              yynew();
121              while (true){  //scan
122                 for(yyn = yykl[yystate]; yyn <= yykh[yystate]; yyn++){
123                     yymark(yyk[yyn]);
124                 }
125
126                 for(yyn=yymh[yystate]; yyn>= yyml[yystate]; yyn--){
127                    yymatch(yym[yyn]);
128                 }
129
130                 if(yytl[yystate] > yyth[yystate]){
131                     break;
132                 }
133
134                 yyscan();
135//                 yyn = yytl[yystate];
136                 totablechar();
137//                 while( (yyn <= yyth[yystate]) && (!(charinarray(yytablechar,yyt[yyn].cc))) ){
138//                   yyn++;
139//                 }
140//                 if (yyn > yyth[yystate]){
141//                     break;
142//                 }
143
144                 yyn = yystateTable[yytablechar][yystate];
145                 if (yyn == -1)
146                     break;
147                                         
148                 yystate = yyt[yyn].s;
149              } //scan
150
151              while (true){ //action
152                int yyrule;
153                if ( (yyrule = yyfind()) != -1 ){
154                   yyaction(yyrule);
155                   if (yyreject){
156                       continue;
157                   }
158                }else if( (!yydefault() ) && (yywrap()) ){
159                   yyclear();
160                   returni(0);
161                }
162                break;
163              }
164
165              if (!yydone) {
166                  continue;
167              }
168              break;
169            } // top level while
170
171           return yyretval;
172        }
173
174    static void inittable(){
175                
176                //if (yynmarks > 0) return; //init table already
177
178        String line;
179        boolean inyyk=false,inyym=false,inyykl=false,inyykh=false,inyyml=false,inyymh=false,inyytl=false,inyyth=false,inyytint=false,inyyt=false,inkeyword=false;
180        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;
181        int c=0;
182        keywordValueList.clear();
183        keywordTypeList.clear();
184        
185        BufferedReader br = new BufferedReader(new InputStreamReader(TLexerCouchbase.class.getResourceAsStream(table_file)));
186
187            try{
188                while( (line = br.readLine()) != null){
189                          if (line.trim().startsWith("yynmarks=")){
190                             String[] ss = line.split("[=;]");
191                              yynmarks=Integer.parseInt(ss[1].trim());
192                              yyk = new int[yynmarks+1];
193                          }else if (line.trim().startsWith("yynmatches=")){
194                              String[] ss = line.split("[=;]");
195                               yynmatches=Integer.parseInt(ss[1].trim());
196                               yym = new int[yynmatches+1];
197                          }else if (line.trim().startsWith("yyntrans=")){
198                              String[] ss = line.split("[=;]");
199                               yyntrans=Integer.parseInt(ss[1].trim());
200                               yytint = new int[yyntrans+1];
201                               yyt = new TYytRec[yyntrans+1];
202                          }else if (line.trim().startsWith("yynstates=")){
203                              String[] ss = line.split("[=;]");
204                               yynstates=Integer.parseInt(ss[1].trim());
205                               yykl = new int[yynstates];
206                               yykh = new int[yynstates];
207                              yyml = new int[yynstates];
208                              yymh = new int[yynstates];
209                              yytl = new int[yynstates];
210                              yyth = new int[yynstates];
211                          }else if (line.trim().startsWith("<end>")){
212                              if (inyyk){
213                                  inyyk = false;
214                                 if (yynmarks+1 != yyk_count ){
215                                    System.out.println("required1:"+(yynmarks)+" actually:"+(yyk_count-1));
216                                 }
217                              }
218                              else if(inyym){
219                                     inyym = false;
220                                    if (yynmatches+1 != yym_count ){
221                                       System.out.println("required2:"+(yynmatches)+" actually:"+(yym_count-1));
222                                    }
223                              }
224                              else if(inyykl){
225                                     inyykl = false;
226                                    if (yynstates != yykl_count ){
227                                       System.out.println("required3:"+(yynstates)+" actually:"+(yykl_count));
228                                    }
229                              }
230                              else if(inyykh){
231                                     inyykh = false;
232                                    if (yynstates != yykh_count ){
233                                       System.out.println("required4:"+(yynstates)+" actually:"+(yykh_count));
234                                    }
235                              }
236                              else if(inyyml){
237                                     inyyml = false;
238                                    if (yynstates != yyml_count ){
239                                       System.out.println("required5:"+(yynstates)+" actually:"+(yyml_count));
240                                    }
241                              }
242                              else if(inyymh){
243                                     inyymh = false;
244                                    if (yynstates != yymh_count ){
245                                       System.out.println("required:"+(yynstates)+" actually:"+(yymh_count));
246                                    }
247                              }
248                              else if(inyytl){
249                                     inyytl = false;
250                                    if (yynstates != yytl_count ){
251                                       System.out.println("required6:"+(yynstates)+" actually:"+(yytl_count));
252                                    }
253                              }
254                              else if(inyyth){
255                                     inyyth = false;
256                                    if (yynstates != yyth_count ){
257                                       System.out.println("required7:"+(yynstates)+" actually:"+(yyth_count));
258                                    }
259                              }
260                              else if(inyytint){
261                                     inyytint = false;
262                                    if (yyntrans + 1 != yytint_count ){
263                                       System.out.println("required8:"+(yyntrans)+" actually:"+(yytint_count-1));
264                                    }
265                              }
266                              else if(inyyt){
267                                     inyyt = false;
268                                    if (yyntrans+1 != yyt_count ){
269                                       System.out.println("required9:"+(yyntrans)+" actually:"+(yyt_count-1));
270                                    }
271                              }
272                              else if(inkeyword){
273                                     inkeyword = false;
274                              }
275                          }else if(line.trim().startsWith("yyk =")){
276                             inyyk = true; 
277                          }else if(line.trim().startsWith("yym =")){
278                             inyym = true;
279                          }else if(line.trim().startsWith("yykl =")){
280                             inyykl = true;
281                          }else if(line.trim().startsWith("yykh =")){
282                             inyykh = true;
283                          }else if(line.trim().startsWith("yyml =")){
284                             inyyml = true;
285                          }else if(line.trim().startsWith("yymh =")){
286                             inyymh = true;
287                          }else if(line.trim().startsWith("yytl =")){
288                             inyytl = true;
289                          }else if(line.trim().startsWith("yyth =")){
290                             inyyth = true;
291                          }else if(line.trim().startsWith("yytint =")){
292                             inyytint = true;
293                          }else if(line.trim().startsWith("yyt =")){
294                             inyyt = true;
295                        }else if(line.trim().startsWith("keywordsvalue =")){
296                           inkeyword = true;
297                        }else if(inyyk){
298                             String[] ss = line.split("[,]");
299                               for(int j=0;j<ss.length;j++){
300                                   // System.out.println(ss[j].trim());
301                                 yyk[yyk_count++] = Integer.parseInt(ss[j].trim());
302                               }
303                          }else if(inyym){
304                               String[] ss = line.split("[,]");
305                                 for(int j=0;j<ss.length;j++){
306                                     // System.out.println(ss[j].trim());
307                                   yym[yym_count++] = Integer.parseInt(ss[j].trim());
308                                 }
309                          }else if(inyykl){
310                               String[] ss = line.split("[,]");
311                                 for(int j=0;j<ss.length;j++){
312                                    //  System.out.println(ss[j].trim());
313                                   yykl[yykl_count++] = Integer.parseInt(ss[j].trim());
314                                 }
315                          }else if(inyykh){
316                               String[] ss = line.split("[,]");
317                                 for(int j=0;j<ss.length;j++){
318                                     // System.out.println(ss[j].trim());
319                                   yykh[yykh_count++] = Integer.parseInt(ss[j].trim());
320                                 }
321                          }else if(inyyml){
322                               String[] ss = line.split("[,]");
323                                 for(int j=0;j<ss.length;j++){
324                                     // System.out.println(ss[j].trim());
325                                   yyml[yyml_count++] = Integer.parseInt(ss[j].trim());
326                                 }
327                          }else if(inyymh){
328                               String[] ss = line.split("[,]");
329                                 for(int j=0;j<ss.length;j++){
330                                     // System.out.println(ss[j].trim());
331                                   yymh[yymh_count++] = Integer.parseInt(ss[j].trim());
332                                 }
333                          }else if(inyytl){
334                               String[] ss = line.split("[,]");
335                                 for(int j=0;j<ss.length;j++){
336                                     // System.out.println(ss[j].trim());
337                                   yytl[yytl_count++] = Integer.parseInt(ss[j].trim());
338                                 }
339                          }else if(inyyth){
340                               String[] ss = line.split("[,]");
341                                 for(int j=0;j<ss.length;j++){
342                                     // System.out.println(ss[j].trim());
343                                   yyth[yyth_count++] = Integer.parseInt(ss[j].trim());
344                                 }
345                          }else if(inyytint){
346                               String[] ss = line.split("[,]");
347                                 for(int j=0;j<ss.length;j++){
348                                     // System.out.println(ss[j].trim());
349                                   yytint[yytint_count++] = Integer.parseInt(ss[j].trim());
350                                 }
351                          }else if(inyyt){
352                                //System.out.println(line.trim());
353
354                              c = 0;
355                              String[] st = line.trim().split(",,");
356                              char[] tmp = new char[st.length];
357                              for(int i=0;i<st.length;i++){
358
359                                  if(st[i].startsWith("\'")) {
360                                      if(st[i].length() == 3){  // 'a'
361                                          tmp[c++] = st[i].charAt(1);
362                                      }else if(st[i].length() == 4) { // '\\'
363                                          tmp[c++] = st[i].charAt(2);
364                                      }else{
365                                         System.out.println(" read yytstr error, error string is "+st[i]+ "line: "+ yyt_count);
366                                      }
367                                  }else{
368                                      try{
369                                           tmp[c++] = (char)Integer.parseInt(st[i]);   // char in number like 32 that represent space
370                                          } catch (NumberFormatException nfe) {
371                                             System.out.println("NumberFormatException: " + nfe.getMessage());
372                                          }
373                                  }
374                              } //while hasmoreTokens
375
376                          //yyt[lineno] = new YYTrec(tmp,yytint[lineno]);
377                              yyt[yyt_count] = new TYytRec(tmp,yytint[yyt_count]);
378                              yyt_count++;
379
380                          }else if(inkeyword){
381                              String[] ss =line.split("[=]");
382
383                              int val1 = -1;
384                              int val2 = -1;
385                              try {
386                                  val1 = Integer.parseInt(ss[1]);
387                                  val2 = Integer.parseInt(ss[2]);
388                              }
389                              catch (NumberFormatException nfe) {
390                                  System.out.println("NumberFormatException: " + nfe.getMessage());
391                              }
392                              keywordValueList.put(ss[0].toUpperCase(),val1);
393                              keywordTypeList.put(val1,val2);
394                             }
395                }
396                }catch(IOException e){
397                  System.out.println(e.toString());
398                }
399                                
400                                yystateLookupConfigure();
401
402    }
403
404
405
406    void yyaction(int yyruleno){
407
408
409      int ic;
410      char[] tmparray = {'=','+','-','*','/','>','<'};
411
412      yylvalstr = getyytext();
413  /* actions: */
414  switch(yyruleno){
415  case 1:
416                
417        {
418                    addlit(yylvalstr,yytextlen);
419                    if (xcdepth <= 0)
420                       {
421                        start(init);
422                        yylvalstr = litbufdup();
423                        returni(cmtslashstar);
424                       }
425                    else
426                       xcdepth--;
427
428                  break;
429        }
430
431
432  case 2:
433                
434                  {
435                         xcdepth++;
436                          yyless(2);
437                          addlit(yylvalstr,yytextlen);
438                      break;
439                   }
440
441  case 3:
442                        
443                  {
444
445                      if (getyysstate() == xq)
446                      {
447                          nchars = yytextlen;
448                          addlit(yylvalstr, yytextlen-1);
449                          yyless(nchars-1);
450                          return;//exit;
451                      }
452
453                      xcdepth = 0;
454                      start(xc);
455                      startlit();
456                      yyless(2);
457                      addlit(yylvalstr,yytextlen);
458
459                  break;
460                  }
461
462  case 4:
463                
464                  {
465                    addlit(yylvalstr,yytextlen);
466                    break;
467                  }
468
469  case 5:
470            
471                  {
472                      addlitchar(yylvalstr.charAt(0));
473                      break;
474                  }
475
476
477  case 6:
478            
479        {
480                                        //token_start := yylvalStr;
481                                        if (getyysstate() == xq)
482            {
483                                                
484                                              nchars = yytextlen;
485                                              addlit(yylvalstr, yytextlen-1);
486                yyless(nchars-1);
487                                              return;//exit;    
488                                           }
489            start(xq);
490            startlit();
491                                          addlit(yylvalstr,yytextlen);
492                break;
493        }
494
495
496  case 7:
497                       
498        {
499                                        start(init);
500                                        addlit(yylvalstr, yytextlen);
501                                        yylvalstr = litbufdup();
502                                        returni(ident);
503                break;
504        }
505
506  case 8:
507                        
508        {
509                                        //token_start := yylvalStr;
510                                        start(xbacktick);
511                                        startlit();
512                                        addlit(yylvalstr, yytextlen);
513                break;
514        }
515
516
517  case 9:
518                       
519        {
520                addlit(yylvalstr, yytextlen);
521                break;
522  }
523
524  case 10:
525                       
526        {
527    addlit(yylvalstr, yytextlen);
528                break;
529   }
530
531
532  case 11:
533                
534                  {
535                      start(init);
536                      addlit(yylvalstr, yytextlen);
537                      yylvalstr = litbufdup();
538                      returni(sconst);
539                      break;
540                  }
541
542  case 12:
543                
544                    {
545                      addlit(yylvalstr,yytextlen);
546                      break;
547                    }
548        
549  case 13:
550                        
551                {
552
553          if (
554              (getyysstate() == xd)
555                                                )
556          {
557              addlit(yylvalstr, 1);
558              yyless(1);
559              return;//exit;
560          }
561          
562                        start(xq);
563                        startlit();
564                        addlit(yylvalstr, yytextlen);
565                        
566                        break;
567                }
568
569
570  case 14:
571                
572                  {
573                      addlit(yylvalstr, yytextlen);
574                      break;
575                  }
576
577  case 15:
578                
579                  {
580                    dummych1 = get_char();
581                    unget_char(dummych1);
582                    if (dummych1 == (char)10)
583                      {
584                        if (insqlpluscmd){
585                          nchars = yytextlen;
586                          if(yylvalstr.charAt(nchars-1) == (char)13){
587                              yyless(nchars - 1);
588                              yylvalstr = yylvalstr.substring(0,nchars);
589                          }
590                            start(init);
591                            addlit(yylvalstr, nchars-1);
592                            yylvalstr = litbufdup();
593                            returni(sconst); //in sqlplus command, characters between ' and return is treated as a string
594
595                        }else{
596                        dummych1 = get_char();
597                        addlit(yylvalstr+dummych1, yytextlen+1);
598                        }
599                      } else if (dummych1 == '\\')
600                          {
601                              dummych1 = get_char();
602                              dummych2 = get_char();
603                              addlit(yylvalstr+dummych1+dummych2, yytextlen+2);
604                          }
605                        else
606                            { addlit(yylvalstr, yytextlen);}
607
608                  break;
609                 }
610  case 16:
611                 
612        {
613                dummych1 = get_char();
614                unget_char(dummych1);
615                if ((dummych1 == '\'') && (yylvalstr.charAt(yytextlen-1)== '\\')) // if ' after \, just eat '
616                {
617                  if ((yytextlen > 1) && (yylvalstr.charAt(yytextlen-2)== '\\')){ // just eat \\
618                        addlit(yylvalstr, yytextlen);
619                  }else{
620                                dummych2 = get_char();
621                                addlit(yylvalstr+dummych2, yytextlen+1);
622                  }
623                }else{
624                        addlit(yylvalstr, yytextlen);
625                }
626                break;
627        }
628
629  case 17:
630                 
631     {
632        dummych1 = get_char();
633        addlit(yylvalstr+dummych1, yytextlen+1);
634        break;
635     }
636         
637  case 18:
638                
639                  {
640                      start(init);
641                      addlit(yylvalstr, yytextlen);
642                      if ((literallen == 0) && (!insqlpluscmd))
643                        {returni (error);}
644                     // if (literallen >= namedatalen)
645                     // {
646                     //    setlengthofliteralbuf(namedatalen);
647                     //    literallen = namedatalen;
648                     // }
649                      yylvalstr = litbufdup();
650                      returni (sconst);
651
652                      break;
653                  }
654
655  case 19:
656               
657                  {
658                      addlit(yylvalstr, yytextlen);
659                      break;
660                  }
661
662  case 20:
663                        
664                  {
665                      start(xd);
666                      startlit();
667                      addlit(yylvalstr, yytextlen);
668                      break;
669                  }
670  case 21:
671                
672                  {
673                    dummych1 = get_char();
674                    unget_char(dummych1);
675                    if (dummych1 == (char)10)
676                      {
677                          if (insqlpluscmd){
678                            nchars = yytextlen;
679                            if(yylvalstr.charAt(nchars-1) == (char)13){
680                                yyless(nchars - 1);
681                                yylvalstr = yylvalstr.substring(0,nchars);
682                            }
683                              start(init);
684                              addlit(yylvalstr, nchars-1);
685                              yylvalstr = litbufdup();
686                              returni(sconst); //in sqlplus command, characters between ' and return is treated as a string
687
688                          }else{
689                          dummych1 = get_char();
690                          addlit(yylvalstr+dummych1, yytextlen+1);
691                          }
692
693                      } else
694                        addlit(yylvalstr, yytextlen);
695
696                  break;
697                  }
698
699  case 22:
700          
701          {
702            returni(lexnewline);
703            break;
704          }
705
706  case 23:
707        
708          {
709              returni(lexspace);
710              break;
711          }
712
713  case 24:
714                
715          {
716          if ((getyysstate() == xq)
717              || (getyysstate() == xd)
718              || (getyysstate() == xc)
719              || (getyysstate() == xbacktick)
720             )
721          {
722              addlit(yylvalstr, 1);
723              yyless(1);
724              return;//exit;
725          }
726
727          returni(cmtdoublehyphen);
728          break;
729          }
730
731  case 25:
732        
733          {
734               returnc(yylvalstr.charAt(0));
735               break;
736          }
737
738
739  case 26:
740                       
741          {
742            returni(cmpop);
743            break;
744          }
745        
746  case 27:
747                        
748          {
749            returni(concatenationop);
750            break;
751          }
752
753  case 28:
754                        
755          {
756               returni(iconst);
757               break;
758          }
759
760  case 29:
761                        
762          {
763            ///*  for i in 1..5 loop, we can't recognize 1. as a decimal,but 1 as decimal
764            nchars = yytextlen;
765            if (yylvalstr.charAt(nchars-1) == '.')
766              {
767                dummych1 = get_char();
768                unget_char(dummych1);
769                if (dummych1 == '.')
770                  {
771                      yyless(nchars-1);
772                    yylvalstr = yylvalstr.substring(0,nchars - 1);
773                    returni (iconst);
774                    return;//exit;
775                  }
776              }
777            returni (fconst);
778           break;
779          }
780
781  case 30:
782                        
783          {
784           returni (fconst);
785           break;
786          }
787
788
789  case 31:
790              
791          {
792            int rw;
793             
794             nchars = yytextlen;
795             
796             if ((tmpDelimiter.length()>0) && (yylvalstr.endsWith(tmpDelimiter))){
797                 yyless(nchars-tmpDelimiter.length());
798                 yylvalstr = yylvalstr.substring(0,nchars-tmpDelimiter.length());
799             }
800             
801            if ( (rw = iskeyword(yylvalstr)) != -1)   { returni(rw);}
802            else returni(ident);
803            break;
804          }
805
806
807  case 32:
808              
809          {
810            returni(NEXT_PARAM);
811            break;
812          }
813
814  case 33:
815                    
816          {
817            returni(POSITIONAL_PARAM);
818            break;
819          }
820
821          
822  case 34:
823               
824          {
825            returni(NAMED_PARAM);
826            break;
827          }
828                    
829  case 35:
830                        
831          {
832            returni( error);
833            break;
834          }
835
836    default:{
837     System.out.println("fatal error in yyaction");
838    }
839   }//switch
840}/*yyaction*/;
841
842
843
844        }