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