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