Update jflex to 1.8.2
[scilab.git] / scilab / modules / helptools / src / java / jflex / scilab.jflex
1 //CHECKSTYLE:OFF
2
3 package org.scilab.modules.helptools.scilab;
4
5 import java.util.Arrays;
6 import java.util.Set;
7 import java.util.Map;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.io.BufferedReader;
13 import java.io.FileReader;
14 import java.io.Reader;
15 import java.io.StringReader;
16 import java.io.IOException;
17
18 @javax.annotation.Generated("JFlex")
19 %%
20
21 %public
22 %class ScilabLexer
23 %final
24 %unicode
25 %char
26 %type void
27
28 %{
29     private static Set<String> commands;
30     private static Set<String> macros;
31
32     private int beginString;
33     private boolean transposable;
34     private boolean breakstring;
35     private boolean breakargs;
36     private int whitesOnFirstLine;
37     private int saveLexState;
38
39     private String id;
40     private List<String> returnValues = new ArrayList<String>();
41     private List<String> argsValues = new ArrayList<String>();
42     private List<String> localFun = new ArrayList<String>();
43
44     private AbstractScilabCodeHandler handler;
45
46     public ScilabLexer(String[] primFile, String[] macroFile) {
47        if (commands == null) {
48           commands = new HashSet<String>();
49           macros = new HashSet<String>();
50           loadNames(primFile, commands);
51           loadNames(macroFile, macros);
52        }
53     }
54
55     public ScilabLexer(String primFile, String macroFile) {
56        this(new String[]{primFile}, new String[]{macroFile});
57     }
58
59     public ScilabLexer(Set<String> primitives, Set<String> macros) {
60        commands = primitives;
61        this.macros = macros;
62     }
63
64     private void loadNames(String[] files, Set<String> set) {
65        for (int i = 0; i < files.length; i++) {
66           loadNames(files[i], set);
67        }
68     }
69
70     private void loadNames(String file, Set<String> set) {
71        if (file == null) {
72           return;
73        }
74        BufferedReader input = null;
75        try {
76           input = new BufferedReader(new FileReader(file));
77           String line = null;
78           while ((line = input.readLine()) != null) {
79              set.add(line);
80           }
81        } catch (IOException e) {
82           System.err.println(e);
83        }
84        if (input != null) {
85           try {
86              input.close();
87           } catch (IOException e) {
88              System.err.println(e);
89           }
90        }
91     }
92
93     private String trimEnd(String str) {
94         int end = str.length() - 1;
95         int i = end;
96         for (; i >= 0; i--) {
97            char c = str.charAt(i);
98            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
99               break;
100            }
101         }
102         if (i != end) {
103            return str.substring(0, i + 1);
104         }
105
106         return str;
107     }
108
109     public String convert(AbstractScilabCodeHandler h, String code) {
110         Reader reader = new StringReader(trimEnd(code));
111         String str = convert(h, reader, true);
112         try {
113           reader.close();
114         } catch (IOException e) { }
115
116         return str;
117     }
118
119     public String convert(AbstractScilabCodeHandler h, Reader code, boolean ret) {
120         if (code == null) {
121            return null;
122         } else {
123            handler = h;
124            transposable = false;
125            breakargs = false;
126            breakstring = false;
127            whitesOnFirstLine = 0;
128            localFun.clear();
129            yyreset(code);
130            yybegin(CLEANFIRST);
131            try {
132                yylex();
133            } catch (IOException e) {
134                return null;
135            }
136            if (ret) {
137                return h.toString();
138            }
139            return "";
140         }
141     }
142 %}
143
144 /* main character classes */
145 eol = "\r" | "\n" | "\r\n"
146
147 open = "[" | "(" | "{"
148 close = "]" | ")" | "}"
149
150 comment = "//"
151
152 quote = "'"
153
154 dquote = "\""
155
156 cstes = "%t" | "%T" | "%f" | "%F" | "%e" | "%pi" | "%inf" | "%i" | "%z" | "%s" | "%nan" | "%eps" | "SCI" | "WSCI" | "SCIHOME" | "TMPDIR"
157
158 dottransp = ".'"
159 lt = "<"
160 gt = ">"
161 neq = "<>"
162 leq = "<="
163 geq = ">="
164 amp = "&"
165
166 operator = ".*" | "./" | ".\\" | ".^" | ".**" | "+" | "-" | "/" | "\\" | "*" | "^" | "**" | "==" | "~=" | ".*." | "./." | ".\\." | "/." | "=" | "|" | "@" | "@=" | "~"
167
168 structureKwds = "if" | "for" | "while" | "try" | "select" | "end" | "then" | "do" | "catch" | "case" | "elseif" | "else"
169
170 controlKwds = "abort" | "break" | "quit" | "return" | "resume" | "pause" | "continue" | "exit"
171
172 break = ".."(".")*
173 breakinstring = {break}[ \t]*({comment} | {eol})
174
175 special = "$" | ":" | {break}
176
177 string = (([^\t\'\"\r\n<>&\.]*)|([\'\"]{2})|("."[^\t\'\"\r\n<>&\.]))+
178
179 argstring = ([^ \t,;/\n\r<>&]*) | ("/"[^ \t,;/\n\r<>&]*)
180
181 id = ([a-zA-Z%_#!?][a-zA-Z0-9_#!$?]*)|("$"[a-zA-Z0-9_#!$?]+)
182
183 dot = "."
184
185 digit = [0-9]
186 exp = [dDeE][+-]?{digit}*
187 number = ({digit}+"."?{digit}*{exp}?)|("."{digit}+{exp}?)
188
189 /* function declaration */
190 white = [ \t]+
191 funcomments = {white}* "//"
192 fun = "function" {white}
193 funb = "function["
194 endfun = "endfunction"
195
196 htmlentity = "&"[#a-zA-Z0-9]*";"
197
198 %x QSTRING, COMMENT, FIELD, COMMANDS, COMMANDSWHITE, BREAKSTRING, FUNCTION, TYPEID, FUNNAME, RETS, ARGS, BREAKINARGS, WHITESEOL, CLEANFIRST, CLEAN
199
200 %%
201
202 <CLEANFIRST> {
203   {white}*{eol}+                 { }
204
205   {white}                        {
206                                    whitesOnFirstLine = yylength();
207                                    yybegin(YYINITIAL);
208                                  }
209
210   .                              {
211                                    yypushback(1);
212                                    yybegin(YYINITIAL);
213                                  }
214 }
215
216 <CLEAN> {
217   {eol}+                         {
218                                    handler.handleNothing("\n");
219                                  }
220
221   {white}                        {
222                                    int len = yylength() - whitesOnFirstLine;
223                                    if (len > 0) {
224                                       yypushback(len);
225                                    }
226                                    yybegin(saveLexState);
227                                  }
228
229   .                              {
230                                    yypushback(1);
231                                    yybegin(saveLexState);
232                                  }
233 }
234
235 <YYINITIAL> {
236   {htmlentity}                   {
237                                    transposable = false;
238                                    handler.handleDefault(yytext());
239                                  }
240
241   {comment}                      {
242                                    transposable = false;
243                                    yypushback(2);
244                                    yybegin(COMMENT);
245                                  }
246
247   {dottransp}                    {
248                                    transposable = false;
249                                    handler.handleOperator(".&#0039;");
250                                  }
251
252   {lt}                           {
253                                    transposable = false;
254                                    handler.handleOperator("&#0060;");
255                                  }
256
257   {gt}                           {
258                                    transposable = false;
259                                    handler.handleOperator("&#0062;");
260                                  }
261
262   {leq}                          {
263                                    transposable = false;
264                                    handler.handleOperator("&#0060;=");
265                                  }
266
267   {geq}                          {
268                                    transposable = false;
269                                    handler.handleOperator("&#0062;=");
270                                  }
271
272   {neq}                          {
273                                    transposable = false;
274                                    handler.handleOperator("&#0060;&#0062;");
275                                  }
276
277   {amp}                          {
278                                    transposable = false;
279                                    handler.handleOperator("&#0038;");
280                                  }
281
282   {operator}                     {
283                                    transposable = false;
284                                    handler.handleOperator(yytext());
285                                  }
286
287   {funb}                         {
288                                    transposable = false;
289                                    handler.handleFKeywords("function");
290                                    handler.handleNothing(" ");
291                                    handler.handleOpenClose("[");
292                                    yybegin(RETS);
293                                  }
294
295   {fun}                          {
296                                    transposable = false;
297                                    handler.handleFKeywords("function");
298                                    handler.handleNothing(" ");
299                                    yybegin(FUNCTION);
300                                  }
301
302   {endfun}                       {
303                                    transposable = false;
304                                    returnValues.clear();
305                                    argsValues.clear();
306                                    handler.handleFKeywords("endfunction");
307                                  }
308
309   {structureKwds}                {
310                                    transposable = false;
311                                    handler.handleSKeywords(yytext());
312                                  }
313
314   {controlKwds}                  {
315                                    transposable = false;
316                                    handler.handleCKeywords(yytext());
317                                  }
318
319   {cstes}                        {
320                                    transposable = true;
321                                    handler.handleConstants(yytext());
322                                  }
323
324   {id}                           {
325                                    transposable = true;
326                                    String str = yytext();
327                                    if (commands.contains(str)) {
328                                        yybegin(COMMANDS);
329                                        handler.handleCommand(str);
330                                    } else if (macros.contains(str)) {
331                                        yybegin(COMMANDS);
332                                        handler.handleMacro(str);
333                                    } else if (localFun.contains(str)) {
334                                         yybegin(COMMANDS);
335                                         handler.handleFunctionId(str);
336                                    } else {
337                                        if (returnValues.contains(str) || argsValues.contains(str)) {
338                                            handler.handleInputOutputArgs(str);
339                                        }  else {
340                                            handler.handleId(str);
341                                        }
342                                    }
343                                  }
344
345   {number}                       {
346                                    transposable = true;
347                                    handler.handleNumber(yytext());
348                                  }
349
350   {special}                      {
351                                    transposable = false;
352                                    handler.handleSpecial(yytext());
353                                  }
354
355   {dot}                          {
356                                    transposable = false;
357                                    yybegin(FIELD);
358                                    handler.handleOperator(yytext());
359                                  }
360
361   {quote}                        {
362                                    if (transposable) {
363                                        handler.handleOperator("&#0039;");
364                                    } else {
365                                        beginString = zzStartRead;
366                                        yybegin(QSTRING);
367                                        handler.handleString("&#0039;");
368                                    }
369                                  }
370
371   {open}                         {
372                                    transposable = false;
373                                    handler.handleOpenClose(yytext());
374                                  }
375
376   {close}                        {
377                                    transposable = true;
378                                    handler.handleOpenClose(yytext());
379                                  }
380
381   {dquote}                       {
382                                    transposable = false;
383                                    beginString = zzStartRead;
384                                    yybegin(QSTRING);
385                                    handler.handleString("&#0034;");
386                                  }
387
388   " "                            {
389                                    transposable = false;
390                                    handler.handleNothing(" ");
391                                  }
392
393   "\t"                           {
394                                    transposable = false;
395                                    handler.handleNothing("    ");
396                                  }
397
398   "\0"                           {
399                                    return;
400                                  }
401
402   .                              {
403                                    transposable = false;
404                                    handler.handleDefault(yytext());
405                                  }
406
407   {eol}                          {
408                                    handler.handleNothing("\n");
409                                    saveLexState = YYINITIAL;
410                                    yybegin(CLEAN);
411                                  }
412
413 }
414
415 <FUNCTION> {
416   "["                            {
417                                    handler.handleOpenClose("[");
418                                    yybegin(RETS);
419                                  }
420
421   {id}                           {
422                                    id = yytext();
423                                    yybegin(TYPEID);
424                                  }
425
426   .                              |
427   {eol}                          {
428                                    yypushback(1);
429                                    yybegin(YYINITIAL);
430                                  }
431 }
432
433 <TYPEID> {
434   "="                            {
435                                    returnValues.add(id);
436                                    handler.handleInputOutputArgsDecl(id);
437                                    handler.handleOperator("=");
438                                    yybegin(FUNNAME);
439                                  }
440
441   "("                            {
442                                    localFun.add(id);
443                                    handler.handleFunctionIdDecl(id);
444                                    handler.handleOpenClose("(");
445                                    yybegin(ARGS);
446                                  }
447
448   [ \t]                          { }
449
450   {funcomments}                  {
451                                    localFun.add(id);
452                                    handler.handleFunctionIdDecl(id);
453                                    yypushback(yylength());
454                                    yybegin(YYINITIAL);
455                                  }
456
457   .                              |
458   {eol}                          {
459                                    yypushback(1);
460                                    yybegin(YYINITIAL);
461                                  }
462 }
463
464 <FUNNAME> {
465   {id}                           {
466                                    id = yytext();
467                                    localFun.add(id);
468                                    handler.handleFunctionIdDecl(id);
469                                  }
470
471   [ \t]                          { }
472
473   "("                            {
474                                    handler.handleOpenClose("(");
475                                    yybegin(ARGS);
476                                  }
477
478   {funcomments}                  {
479                                    yypushback(yylength());
480                                    yybegin(YYINITIAL);
481                                  }
482
483   .                              |
484   {eol}                          {
485                                    yypushback(1);
486                                    yybegin(YYINITIAL);
487                                  }
488 }
489
490 <ARGS> {
491   {id}                           {
492                                    id = yytext();
493                                    argsValues.add(id);
494                                    handler.handleInputOutputArgsDecl(id);
495                                  }
496
497   ","                            {
498                                    handler.handleDefault(", ");
499                                  }
500
501   {white}                        { }
502
503   {break}                        {
504                                    handler.handleSpecial(yytext());
505                                    yybegin(BREAKINARGS);
506                                  }
507
508   ")"                            {
509                                    handler.handleOpenClose(")");
510                                    yybegin(YYINITIAL);
511                                  }
512
513   .                              |
514   {eol}                          {
515                                    yypushback(1);
516                                    yybegin(YYINITIAL);
517                                  }
518 }
519
520 <BREAKINARGS> {
521   " "                            {
522                                    handler.handleNothing(" ");
523                                  }
524
525   {comment}                      {
526                                    breakargs = true;
527                                    yypushback(2);
528                                    yybegin(COMMENT);
529                                  }
530
531   {white}*{eol}                  {
532                                    handler.handleNothing("\n");
533                                    saveLexState = WHITESEOL;
534                                    yybegin(CLEAN);
535                                  }
536
537   .                              {
538                                    yypushback(1);
539                                    yybegin(YYINITIAL);
540                                  }
541 }
542
543 <WHITESEOL> {
544   " "                            {
545                                    handler.handleNothing(" ");
546                                  }
547
548   .                              |
549   {eol}                          {
550                                    yypushback(1);
551                                    yybegin(ARGS);
552                                  }
553 }
554
555 <RETS> {
556   {id}                           {
557                                    id = yytext();
558                                    returnValues.add(id);
559                                    handler.handleInputOutputArgsDecl(id);
560                                  }
561
562   ","                            {
563                                    handler.handleDefault(", ");
564                                  }
565
566   "]"                            {
567                                    handler.handleOpenClose("]");
568                                  }
569
570   "["                            {
571                                    handler.handleOpenClose("[");
572                                  }
573
574   "="                            {
575                                    handler.handleOperator("=");
576                                    yybegin(FUNNAME);
577                                  }
578
579   "..."                          {
580                                    handler.handleNothing("...");
581                                  }
582
583   {white}                        { }
584
585   .                              |
586   {eol}                          {
587                                    yypushback(1);
588                                    yybegin(YYINITIAL);
589                                  }
590 }
591
592 <COMMANDS> {
593   [ \t]*"("                      {
594                                    yypushback(yylength());
595                                    yybegin(YYINITIAL);
596                                  }
597
598   " "                            {
599                                    yybegin(COMMANDSWHITE);
600                                    handler.handleNothing(" ");
601                                  }
602
603   "\t"                           {
604                                    yybegin(COMMANDSWHITE);
605                                    handler.handleNothing("    ");
606                                  }
607   .
608                                  {
609                                    yypushback(1);
610                                    yybegin(YYINITIAL);
611                                  }
612
613   {eol}                          {
614                                    handler.handleNothing("\n");
615                                    saveLexState = YYINITIAL;
616                                    yybegin(CLEAN);
617                                  }
618 }
619
620 <COMMANDSWHITE> {
621   {comment}                      {
622                                    transposable = false;
623                                    yypushback(2);
624                                    yybegin(COMMENT);
625                                  }
626
627   {argstring}                    {
628                                    handler.handleString(yytext());
629                                  }
630
631   {lt}                           {
632                                    transposable = false;
633                                    handler.handleString("&#0060;");
634                                  }
635
636   {gt}                           {
637                                    transposable = false;
638                                    handler.handleString("&#0062;");
639                                  }
640
641   {amp}                          {
642                                    transposable = false;
643                                    handler.handleString("&#0038;");
644                                  }
645
646   " "                            {
647                                    handler.handleNothing(" ");
648                                  }
649
650   "\t"                           {
651                                    handler.handleNothing("    ");
652                                  }
653
654   {eol}                          {
655                                    handler.handleNothing("\n");
656                                    saveLexState = YYINITIAL;
657                                    yybegin(CLEAN);
658                                  }
659   .
660                                  {
661                                    yypushback(1);
662                                    yybegin(YYINITIAL);
663                                  }
664
665 }
666
667 <FIELD> {
668   {id}                           {
669                                    handler.handleField(yytext());
670                                  }
671
672   .                              {
673                                    yypushback(1);
674                                    yybegin(YYINITIAL);
675                                  }
676
677  {eol}                           {
678                                    handler.handleNothing("\n");
679                                    saveLexState = YYINITIAL;
680                                    yybegin(CLEAN);
681                                  }
682 }
683
684 <QSTRING> {
685   {breakinstring}                {
686                                    yypushback(yylength());
687                                    yybegin(BREAKSTRING);
688                                    transposable = false;
689                                    handler.handleString(yytext());
690                                  }
691
692   {lt}                           {
693                                    transposable = false;
694                                    handler.handleString("&#0060;");
695                                  }
696
697   {gt}                           {
698                                    transposable = false;
699                                    handler.handleString("&#0062;");
700                                  }
701
702   {amp}                          {
703                                    transposable = false;
704                                    handler.handleString("&#0038;");
705                                  }
706
707   {string}                       |
708   "."                            {
709                                    handler.handleString(yytext());
710                                  }
711
712   {quote}                        {
713                                    transposable = false;
714                                    yybegin(YYINITIAL);
715                                    handler.handleString("&#0039;");
716                                  }
717
718   {dquote}                       {
719                                    transposable = false;
720                                    yybegin(YYINITIAL);
721                                    handler.handleString("&#0034;");
722                                  }
723
724   "\0"                           {
725                                    return;
726                                  }
727
728   .                              {
729                                    handler.handleString(yytext());
730                                  }
731
732  {eol}                           {
733                                    handler.handleNothing("\n");
734                                    saveLexState = YYINITIAL;
735                                    yybegin(CLEAN);
736                                  }
737 }
738
739 <COMMENT> {
740   [^&<>\'\"\n\0]*                {
741                                    handler.handleComment(yytext());
742                                  }
743
744   {lt}                           {
745                                    handler.handleComment("&#0060;");
746                                  }
747
748   {gt}                           {
749                                    handler.handleComment("&#0062;");
750                                  }
751
752   {amp}                          {
753                                    handler.handleComment("&#0038;");
754                                  }
755
756   {quote}                        {
757                                    handler.handleComment("&#0039;");
758                                  }
759
760   {dquote}                       {
761                                    handler.handleComment("&#0034;");
762                                  }
763
764   {eol}                          {
765                                    if (breakstring) {
766                                      saveLexState = QSTRING;
767                                      breakstring = false;
768                                    } else if (breakargs) {
769                                      saveLexState = WHITESEOL;
770                                      breakargs = false;
771                                    } else {
772                                      saveLexState = YYINITIAL;
773                                    }
774                                    handler.handleNothing("\n");
775                                    yybegin(CLEAN);
776                                  }
777
778   "\0"                           {
779                                    return;
780                                  }
781 }
782
783 <BREAKSTRING> {
784   {break}                        {
785                                    breakstring = true;
786                                    handler.handleSpecial(yytext());
787                                  }
788
789   " "                            {
790                                    handler.handleNothing(" ");
791                                  }
792
793   "\t"                           {
794                                    handler.handleNothing("    ");
795                                  }
796
797   {comment}                      {
798                                    transposable = false;
799                                    yypushback(2);
800                                    yybegin(COMMENT);
801                                  }
802
803   .                              {
804                                    handler.handleDefault(yytext());
805                                  }
806
807   {eol}                          {
808                                    if (breakstring) {
809                                       breakstring = false;
810                                       saveLexState = QSTRING;
811                                    } else {
812                                       saveLexState = YYINITIAL;
813                                    }
814                                    handler.handleNothing("\n");
815                                    yybegin(CLEAN);
816                                  }
817
818   "\0"                           {
819                                    return;
820                                  }
821 }
822
823 <<EOF>>                          {
824                                    return;
825                                  }