Update jflex to 1.8.2
[scilab.git] / scilab / modules / scinotes / src / java / jflex / scilab.jflex
1 //CHECKSTYLE:OFF
2
3 package org.scilab.modules.scinotes;
4
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Set;
11 import javax.swing.text.BadLocationException;
12 import javax.swing.text.Element;
13 import org.scilab.modules.commons.ScilabCommonsUtils;
14
15 @javax.annotation.Generated("JFlex")
16
17 %%
18
19 %public
20 %class ScilabLexer
21 %extends ScilabLexerConstants
22 %final
23 %unicode
24 %char
25 %type int
26 %pack
27
28 %{
29     public int start;
30     public int end;
31     public int beginString;
32     public static Set<String> commands = new HashSet<String>();
33     public static Set<String> macros = new HashSet<String>();
34     public static Set<String> variables = new HashSet<String>();
35     public Set<String> infile;
36
37     private ScilabDocument doc;
38     private boolean transposable;
39     private Element elem;
40     private boolean breakstring;
41     private boolean breakcomment;
42     private MatchingBlockScanner matchBlock;
43
44     static {
45         // For SciNotes colors in preferences
46         commands.add("cos");
47         macros.add("sind");
48     }
49
50     public ScilabLexer(ScilabDocument doc) {
51         this(doc, new MatchingBlockScanner(doc), true);
52     }
53
54     public ScilabLexer(ScilabDocument doc, boolean update) {
55         this(doc, new MatchingBlockScanner(doc), update);
56     }
57
58     public ScilabLexer(ScilabDocument doc, MatchingBlockScanner matchBlock, boolean update) {
59         this.doc = doc;
60         this.elem = doc.getDefaultRootElement();
61         this.infile = doc.getFunctionsInDoc();
62         this.matchBlock = matchBlock;
63         if (update) {
64                 update();
65         }
66     }
67
68     public static void update() {
69        if (ScilabCommonsUtils.isScilabThread()) {
70            String[] vars = ScilabKeywords.GetVariablesName();
71            String[] funs = ScilabKeywords.GetFunctionsName();
72            String[] macs = ScilabKeywords.GetMacrosName();
73            variables.clear();
74            commands.clear();
75            macros.clear();
76            if (vars != null) {
77                variables.addAll(Arrays.asList(vars));
78            }
79            if (funs != null) {
80                commands.addAll(Arrays.asList(funs));
81            }
82            if (macs != null) {
83                macros.addAll(Arrays.asList(macs));
84            }
85         }
86     }
87
88     public void setRange(int p0, int p1) {
89         start = p0;
90         end = p1;
91         transposable = false;
92         breakstring = false;
93         yyreset(new ScilabDocumentReader(doc, p0, p1));
94         int currentLine = elem.getElementIndex(start);
95         if (currentLine != 0) {
96            ScilabDocument.ScilabLeafElement e = (ScilabDocument.ScilabLeafElement) elem.getElement(currentLine - 1);
97            if (e.isBrokenString()) {
98               yybegin(QSTRING);
99            } else if (e.isBlockComment()) {
100               yybegin(BLOCKCOMMENT);
101            }
102         }
103     }
104
105     public int yychar() {
106         return (int) yychar;
107     }
108
109     public int scan() throws IOException {
110         int ret = yylex();
111         int lastPos = start + yychar() + yylength();
112         if (lastPos == end - 1) {
113            ((ScilabDocument.ScilabLeafElement) elem.getElement(elem.getElementIndex(start))).setBrokenString(breakstring);
114            breakstring = false;
115         } else if (lastPos == end) {
116            ((ScilabDocument.ScilabLeafElement) elem.getElement(elem.getElementIndex(start))).setBlockComment(yystate() == BLOCKCOMMENT);
117         }
118         return ret;
119     }
120
121     public boolean isLineFinishedByBlockComment(int start, int end) {
122         this.start = start;
123         this.end = end;
124         try {           
125            yyreset(new ScilabDocumentReader(doc, start, end));
126            int tok = 0;
127            while (tok != ScilabLexerConstants.EOF) {
128               tok = yylex();
129            }
130         } catch (Exception e) { }
131         
132         return yystate() == BLOCKCOMMENT;
133     }
134
135     public int getKeyword(int pos, boolean strict) {
136         // Pre condition
137         if (elem == null) {
138             return ScilabLexerConstants.DEFAULT;
139         }
140
141         Element line = elem.getElement(elem.getElementIndex(pos));
142         int end = line.getEndOffset();
143         int tok = -1;
144         start = line.getStartOffset();
145         int startL = start;
146         int s = -1;
147
148         try {
149            yyreset(new ScilabDocumentReader(doc, start, end));
150            if (!strict) {
151               pos++;
152            }
153
154            while (startL < pos && (s != startL || yystate() == BREAKSTRING)) {
155                s = startL;
156                tok = yylex();
157                startL = start + yychar() + yylength();
158            }
159
160            return tok;
161         } catch (IOException e) {
162            return ScilabLexerConstants.DEFAULT;
163         }
164      }
165
166      public static ScilabTokens getScilabTokens(String str) {
167          ScilabDocument doc = new ScilabDocument(false);
168          try {
169              doc.insertString(0, str, null);
170          } catch (BadLocationException e) { }
171          return getScilabTokens(doc);
172      }
173
174      public static ScilabTokens getScilabTokens(ScilabDocument doc) {
175          ScilabLexer lexer = new ScilabLexer(doc);
176          lexer.yyreset(new ScilabDocumentReader(doc, 0, doc.getLength()));
177          ScilabTokens tokens = new ScilabTokens();
178          int tok = -1;
179          try {
180             while (tok != ScilabLexerConstants.EOF) {
181                tok = lexer.yylex();
182                tokens.add(tok, lexer.yychar() + lexer.yylength());
183             }
184          } catch (IOException e) { }
185  
186          return tokens;
187      }
188
189      public static class ScilabTokens {
190         private List<Integer> tokenType = new ArrayList<Integer>();
191         private List<Integer> tokenPos = new ArrayList<Integer>();
192
193         ScilabTokens() { }
194
195         void add(final int type, final int pos) {
196            tokenType.add(type);
197            tokenPos.add(pos);
198         }
199
200         public final List<Integer> getTokenType() {
201            return tokenType;
202         }
203
204         public final List<Integer> getTokenPos() {
205            return tokenPos;
206         }
207      }
208 %}
209
210 /* main character classes */
211 eol = \n
212
213 open = "[" | "(" | "{"
214 close = "]" | ")" | "}"
215
216 comment = "//"
217 startcomment = ("/" "*"+)
218 endcomment = ("*"+) "/"
219
220 quote = "'"
221
222 dquote = "\""
223
224 cstes = "%t" | "%T" | "%f" | "%F" | "%e" | "%pi" | "%inf" | "%i" | "%z" | "%s" | "%nan" | "%eps" | "SCI" | "WSCI" | "SCIHOME" | "TMPDIR"
225
226 operator = ".'" | ".*" | "./" | ".\\" | ".^" | ".**" | "+" | "-" | "/" | "\\" | "*" | "^" | "**" | "==" | "~=" | "<>" | "<" | ">" | "<=" | ">=" | ".*." | "./." | ".\\." | "/." | "=" | "&" | "|" | "@" | "@=" | "~" | "&&" | "||"
227
228 functionKwds = "function" | "endfunction"
229
230 structureKwds = "then" | "do" | "catch" | "case" | "otherwise"
231
232 elseif = "elseif" | "else"
233
234 openCloseStructureKwds = "if" | "for" | "while" | "try" | "select" | "switch"
235
236 end = "end"
237
238 controlKwds = "abort" | "break" | "quit" | "return" | "resume" | "pause" | "continue" | "exit"
239
240 authors = "Michael Baudin" | "Michael BAUDIN" | "Paul Bignier" | "Paul BIGNIER" | "Adeline CARNIS" | "Adeline CARNIS" | "Yann Collette" | "Yann COLLETTE" | "Allan Cornet" | "Allan CORNET" | "Vincent Couvert" | "Vincent COUVERT" | "Clement David" | "Clement DAVID" | "Calixte Denizet" | "Calixte DENIZET" | "Cedric Delamarre" | "Cedric DELAMARRE" | "Antoine Elias" | "Antoine ELIAS" | "Simon Gareste" | "Simon GARESTE" | "Claude Gomez" | "Claude GOMEZ" | "Samuel Gougeon" | "Samuel GOUGEON" | "Bernard Hugueney" | "Bernard HUGUENEY" | "Bruno Jofret" | "Bruno JOFRET" | "Manuel Juliachs" | "Manuel JULIACHS" | "Sylvestre Koumar" | "Sylvestre KOUMAR" | "Pierre Lando" | "Pierre LANDO" | "Sylvestre Ledru" | "Sylvestre LEDRU" | "Vincent Lejeune" | "Vincent LEJEUNE" | "Vincent Liard" | "Vincent LIARD" | "Zhour Madini-Zouine" | "Zhour MADINI-ZOUINE" | "Pierre Marechal" | "Pierre MARECHAL" | "Stephane Mottelet" | "Stephane MOTTELET" | "Jerome Picard" | "Jerome PICARD" | "Allan Simon" | "Allan SIMON" | "Serge Steer" | "Serge STEER" | "Inria" | "INRIA" | "DIGITEO" | "Digiteo" | "ENPC" | "ESI Group"
241
242 break = ".."(".")*
243 breakinstring = {break}[ \t]*{comment}?
244
245 special = "$" | ":" | {break}
246
247 string = (([^ \t\'\"\r\n\.]*)|([\'\"]{2}))+
248
249 id = ([a-zA-Z%_#!?][a-zA-Z0-9_#!$?]*)|("$"[a-zA-Z0-9_#!$?]+)
250
251 badid = ([0-9$][a-zA-Z0-9_#!$?]+)
252 whitabs = (" "+"\t" | "\t"+" ")[ \t]*
253 badop = [+-]([\*\/\\\^] | "."[\*\+\-\/\\\^]) | ":=" | "->" | " !=" | ("&&" "&"+) | ("||" "|"+) | ([*+-/\\\^]"=")
254
255 dot = "."
256
257 url = ("http://"|"https://"|"ftp://"|"sftp://"|"ftps://"|"smb:///"|"file://")[^ \t\f\n\r\'\"]+
258 mail = "<"[ \t]*[a-zA-Z0-9_\.\-]+"@"([a-zA-Z0-9\-]+".")+[a-zA-Z]{2,5}[ \t]*">"
259
260 latex = "$"(([^$]*|"\\$")+)"$"
261 latexinstring = (\"|\')"$"(([^$\'\"]*|"\\$"|([\'\"]{2}))+)"$"(\"|\')
262
263 digit = [0-9]
264 exp = [dDeE][+-]?{digit}*
265 number = ({digit}+"."?{digit}*{exp}?)|("."{digit}+{exp}?)
266
267 %x QSTRING, COMMENT, BLOCKCOMMENT, FIELD, COMMANDS, COMMANDSWHITE, BREAKSTRING
268
269 %%
270
271 <YYINITIAL> {
272   {comment}                      {
273                                    transposable = false;
274                                    yypushback(2);
275                                    yybegin(COMMENT);
276                                  }
277
278   {startcomment}                 {
279                                    transposable = false;
280                                    yypushback(2);
281                                    yybegin(BLOCKCOMMENT);
282                                  }
283
284   {operator}                     {
285                                    transposable = false;
286                                    return ScilabLexerConstants.OPERATOR;
287                                  }
288
289   {functionKwds}                 {
290                                    transposable = false;
291                                    return ScilabLexerConstants.FKEYWORD;
292                                  }
293
294   {openCloseStructureKwds}       {
295                                    transposable = false;
296                                    return ScilabLexerConstants.OSKEYWORD;
297                                  }
298
299   {end}                          {
300                                    transposable = false;
301                                    if (matchBlock != null) {
302                                       MatchingBlockScanner.MatchingPositions pos = matchBlock.getMatchingBlock(start + yychar() + yylength(), false);
303                                       if (pos != null) {
304                                          try {
305                                              String match = doc.getText(pos.secondB, pos.secondE - pos.secondB);
306                                              if (match.equals("function")) {
307                                                 return ScilabLexerConstants.FKEYWORD;
308                                              }
309                                          } catch (BadLocationException e) { }
310                                       }
311                                    }
312                                    return ScilabLexerConstants.OSKEYWORD;
313                                  }
314
315   {structureKwds}                {
316                                    transposable = false;
317                                    return ScilabLexerConstants.SKEYWORD;
318                                  }
319
320   {elseif}                       {
321                                    transposable = false;
322                                    return ScilabLexerConstants.ELSEIF;
323                                  }
324
325   {controlKwds}                  {
326                                    transposable = false;
327                                    return ScilabLexerConstants.CKEYWORD;
328                                  }
329
330   {cstes}                        {
331                                    transposable = true;
332                                    return ScilabLexerConstants.CONSTANTES;
333                                  }
334
335   {id}                           {
336                                    transposable = true;
337                                    String str = yytext();
338                                    if (commands.contains(str)) {
339                                        yybegin(COMMANDS);
340                                        return ScilabLexerConstants.COMMANDS;
341                                    } else if (macros.contains(str)) {
342                                        yybegin(COMMANDS);
343                                        return ScilabLexerConstants.MACROS;
344                                    } else if (infile.contains(str)) {
345                                        yybegin(COMMANDS);
346                                        return ScilabLexerConstants.MACROINFILE;
347                                    } else {
348                                        List<String>[] arr = doc.getInOutArgs(start + yychar());
349                                        if (arr != null && (arr[0].contains(str) || arr[1].contains(str))) {
350                                            return ScilabLexerConstants.INPUTOUTPUTARGS;
351                                        } else if (variables.contains(str)) {
352                                            return ScilabLexerConstants.VARIABLES;
353                                        }
354                                    }
355                                    return ScilabLexerConstants.ID;
356                                  }
357
358   {number}                       {
359                                    transposable = true;
360                                    return ScilabLexerConstants.NUMBER;
361                                  }
362
363   {special}                      {
364                                    transposable = false;
365                                    return ScilabLexerConstants.SPECIAL;
366                                  }
367
368   {dot}                          {
369                                    transposable = false;
370                                    yybegin(FIELD);
371                                    return ScilabLexerConstants.OPERATOR;
372                                  }
373
374   {latexinstring}                {
375                                    return ScilabLexerConstants.LATEX;
376                                  }
377
378   {quote}                        {
379                                     if (transposable) {
380                                        return ScilabLexerConstants.TRANSP;
381                                     } else {
382                                        beginString = zzStartRead;
383                                        yybegin(QSTRING);
384                                        return ScilabLexerConstants.STRING;
385                                     }
386                                  }
387
388   {open}                         {
389                                    transposable = false;
390                                    return ScilabLexerConstants.OPENCLOSE;
391                                  }
392
393   {close}                        {
394                                    transposable = true;
395                                    return ScilabLexerConstants.OPENCLOSE;
396                                  }
397
398   {dquote}                       {
399                                    transposable = false;
400                                    beginString = zzStartRead;
401                                    yybegin(QSTRING);
402                                    return ScilabLexerConstants.STRING;
403                                  }
404
405   {badid}                        |
406   {badop}                        |
407   {whitabs}                      {
408                                    return ScilabLexerConstants.ERROR;
409                                  }
410
411   " "                            {
412                                    return ScilabLexerConstants.WHITE;
413                                  }
414
415   "\t"                           {
416                                    return ScilabLexerConstants.TAB;
417                                  }
418
419   .                              |
420   {eol}                          {
421                                    transposable = false;
422                                    return ScilabLexerConstants.DEFAULT;
423                                  }
424 }
425
426 <COMMANDS> {
427   [ \t]*"("                      {
428                                    yypushback(yylength());
429                                    yybegin(YYINITIAL);
430                                  }
431
432   " "                            {
433                                    yybegin(COMMANDSWHITE);
434                                    return ScilabLexerConstants.WHITE;
435                                  }
436
437   "\t"                           {
438                                    yybegin(COMMANDSWHITE);
439                                    return ScilabLexerConstants.TAB;
440                                  }
441   .
442                                  {
443                                    yypushback(1);
444                                    yybegin(YYINITIAL);
445                                  }
446
447   {eol}                          { }
448 }
449
450 <COMMANDSWHITE> {
451   {comment}                      {
452                                    transposable = false;
453                                    yypushback(2);
454                                    yybegin(COMMENT);
455                                  }
456
457   ([^ \t,;/]*) | ("/"[^ /]*)     {
458                                    return ScilabLexerConstants.STRING;
459                                  }
460
461   " "                            {
462                                    return ScilabLexerConstants.WHITE;
463                                  }
464
465   "\t"                           {
466                                    return ScilabLexerConstants.TAB;
467                                  }
468   .
469                                  {
470                                    yypushback(1);
471                                    yybegin(YYINITIAL);
472                                  }
473
474   {eol}                          { }
475 }
476
477 <FIELD> {
478   {id}                           {
479                                    transposable = true;
480                                    return ScilabLexerConstants.FIELD;
481                                  }
482
483   .                              {
484                                    yypushback(1);
485                                    yybegin(YYINITIAL);
486                                  }
487
488   {eol}                          { }
489 }
490
491 <QSTRING> {
492   {breakinstring}                {
493                                    yypushback(yylength());
494                                    yybegin(BREAKSTRING);
495                                    transposable = false;
496                                    return ScilabLexerConstants.STRING;
497                                  }
498
499   " "                            {
500                                    return ScilabLexerConstants.WHITE_STRING;
501                                  }
502
503   "\t"                           {
504                                    return ScilabLexerConstants.TAB_STRING;
505                                  }
506
507   {string}                       |
508   "."                            {
509                                    return ScilabLexerConstants.STRING;
510                                  }
511
512   (\'|\")                        {
513                                    transposable = false;
514                                    yybegin(YYINITIAL);
515                                    return ScilabLexerConstants.STRING;
516                                  }
517
518   .                              |
519   {eol}                          {
520                                    return ScilabLexerConstants.DEFAULT;
521                                  }
522 }
523
524 <BLOCKCOMMENT> {
525   {authors}                      {
526                                    return ScilabLexerConstants.AUTHORS;
527                                  }
528
529   {url}                          {
530                                    return ScilabLexerConstants.URL;
531                                  }
532
533   {mail}                         {
534                                    return ScilabLexerConstants.MAIL;
535                                  }
536
537   {latex}                        {
538                                    return ScilabLexerConstants.LATEX;
539                                  }
540
541   " "                            {
542                                    return ScilabLexerConstants.WHITE_COMMENT;
543                                  }
544
545   "\t"                           {
546                                    return ScilabLexerConstants.TAB_COMMENT;
547                                  }
548
549   {endcomment}                   {
550                                    yybegin(YYINITIAL);
551                                    return ScilabLexerConstants.COMMENT;
552                                  }
553
554   .                              |
555   {eol}                          {
556                                    return ScilabLexerConstants.COMMENT;
557                                  }
558 }
559
560 <COMMENT> {
561   {authors}                      {
562                                    return ScilabLexerConstants.AUTHORS;
563                                  }
564
565   {url}                          {
566                                    return ScilabLexerConstants.URL;
567                                  }
568
569   {mail}                         {
570                                    return ScilabLexerConstants.MAIL;
571                                  }
572
573   {latex}                        {
574                                    return ScilabLexerConstants.LATEX;
575                                  }
576
577   " "                            {
578                                    return ScilabLexerConstants.WHITE_COMMENT;
579                                  }
580
581   "\t"                           {
582                                    return ScilabLexerConstants.TAB_COMMENT;
583                                  }
584
585   .                              |
586   {eol}                          {
587                                    return ScilabLexerConstants.COMMENT;
588                                  }
589 }
590
591 <BREAKSTRING> {
592   {break}                        {
593                                    breakstring = true;
594                                    return ScilabLexerConstants.SPECIAL;
595                                  }
596
597   " "                            {
598                                    return ScilabLexerConstants.WHITE;
599                                  }
600
601   "\t"                           {
602                                    return ScilabLexerConstants.TAB;
603                                  }
604
605   {comment}                      {
606                                    transposable = false;
607                                    yypushback(2);
608                                    yybegin(COMMENT);
609                                  }
610
611   .                              |
612   {eol}                          {
613                                    return ScilabLexerConstants.DEFAULT;
614                                  }
615 }
616
617 <<EOF>>                          {
618                                    return ScilabLexerConstants.EOF;
619                                  }