Merge remote-tracking branch 'origin/6.1'
[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" | "%chars" | "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
243 todo = ("TODO" | "todo" | "Todo")[ \t:]+[^\n]*
244
245 break = ".."(".")*
246 breakinstring = {break}[ \t]*{comment}?
247
248 special = "$" | ":" | {break}
249
250 string = (([^ \t\'\"\r\n\.]*)|([\'\"]{2}))+
251
252 id = ([a-zA-Z%_#!?][a-zA-Z0-9_#!$?]*)|("$"[a-zA-Z0-9_#!$?]+)
253
254 badid = ([0-9$][a-zA-Z0-9_#!$?]+)
255 whitabs = (" "+"\t" | "\t"+" ")[ \t]*
256 badop = [+-]([\*\/\\\^] | "."[\*\+\-\/\\\^]) | ":=" | "->" | " !=" | ("&&" "&"+) | ("||" "|"+) | ([*+-/\\\^]"=")
257
258 dot = "."
259
260 url = ("http://"|"https://"|"ftp://"|"sftp://"|"ftps://"|"smb:///"|"file://")[^ \t\f\n\r\'\"]+
261 mail = "<"[ \t]*[a-zA-Z0-9_\.\-]+"@"([a-zA-Z0-9\-]+".")+[a-zA-Z]{2,5}[ \t]*">"
262
263 latex = "$"(([^$]*|"\\$")+)"$"
264 latexinstring = (\"|\')"$"(([^$\'\"]*|"\\$"|([\'\"]{2}))+)"$"(\"|\')
265
266 digit = [0-9]
267 exp = [dDeE][+-]?{digit}*
268 number = ({digit}+"."?{digit}*{exp}?)|("."{digit}+{exp}?)
269
270 %x QSTRING, COMMENT, BLOCKCOMMENT, FIELD, COMMANDS, COMMANDSWHITE, BREAKSTRING
271
272 %%
273
274 <YYINITIAL> {
275   {comment}                      {
276                                    transposable = false;
277                                    yypushback(2);
278                                    yybegin(COMMENT);
279                                  }
280
281   {startcomment}                 {
282                                    transposable = false;
283                                    yypushback(2);
284                                    yybegin(BLOCKCOMMENT);
285                                  }
286
287   {operator}                     {
288                                    transposable = false;
289                                    return ScilabLexerConstants.OPERATOR;
290                                  }
291
292   {functionKwds}                 {
293                                    transposable = false;
294                                    return ScilabLexerConstants.FKEYWORD;
295                                  }
296
297   {openCloseStructureKwds}       {
298                                    transposable = false;
299                                    return ScilabLexerConstants.OSKEYWORD;
300                                  }
301
302   {end}                          {
303                                    transposable = false;
304                                    if (matchBlock != null) {
305                                       MatchingBlockScanner.MatchingPositions pos = matchBlock.getMatchingBlock(start + yychar() + yylength(), false);
306                                       if (pos != null) {
307                                          try {
308                                              String match = doc.getText(pos.secondB, pos.secondE - pos.secondB);
309                                              if (match.equals("function")) {
310                                                 return ScilabLexerConstants.FKEYWORD;
311                                              }
312                                          } catch (BadLocationException e) { }
313                                       }
314                                    }
315                                    return ScilabLexerConstants.OSKEYWORD;
316                                  }
317
318   {structureKwds}                {
319                                    transposable = false;
320                                    return ScilabLexerConstants.SKEYWORD;
321                                  }
322
323   {elseif}                       {
324                                    transposable = false;
325                                    return ScilabLexerConstants.ELSEIF;
326                                  }
327
328   {controlKwds}                  {
329                                    transposable = false;
330                                    return ScilabLexerConstants.CKEYWORD;
331                                  }
332
333   {cstes}                        {
334                                    transposable = true;
335                                    return ScilabLexerConstants.CONSTANTES;
336                                  }
337
338   {id}                           {
339                                    transposable = true;
340                                    String str = yytext();
341                                    if (commands.contains(str)) {
342                                        yybegin(COMMANDS);
343                                        return ScilabLexerConstants.COMMANDS;
344                                    } else if (macros.contains(str)) {
345                                        yybegin(COMMANDS);
346                                        return ScilabLexerConstants.MACROS;
347                                    } else if (infile.contains(str)) {
348                                        yybegin(COMMANDS);
349                                        return ScilabLexerConstants.MACROINFILE;
350                                    } else {
351                                        List<String>[] arr = doc.getInOutArgs(start + yychar());
352                                        if (arr != null && (arr[0].contains(str) || arr[1].contains(str))) {
353                                            return ScilabLexerConstants.INPUTOUTPUTARGS;
354                                        } else if (variables.contains(str)) {
355                                            return ScilabLexerConstants.VARIABLES;
356                                        }
357                                    }
358                                    return ScilabLexerConstants.ID;
359                                  }
360
361   {number}                       {
362                                    transposable = true;
363                                    return ScilabLexerConstants.NUMBER;
364                                  }
365
366   {special}                      {
367                                    transposable = false;
368                                    return ScilabLexerConstants.SPECIAL;
369                                  }
370
371   {dot}                          {
372                                    transposable = false;
373                                    yybegin(FIELD);
374                                    return ScilabLexerConstants.OPERATOR;
375                                  }
376
377   {latexinstring}                {
378                                    return ScilabLexerConstants.LATEX;
379                                  }
380
381   {quote}                        {
382                                     if (transposable) {
383                                        return ScilabLexerConstants.TRANSP;
384                                     } else {
385                                        beginString = zzStartRead;
386                                        yybegin(QSTRING);
387                                        return ScilabLexerConstants.STRING;
388                                     }
389                                  }
390
391   {open}                         {
392                                    transposable = false;
393                                    return ScilabLexerConstants.OPENCLOSE;
394                                  }
395
396   {close}                        {
397                                    transposable = true;
398                                    return ScilabLexerConstants.OPENCLOSE;
399                                  }
400
401   {dquote}                       {
402                                    transposable = false;
403                                    beginString = zzStartRead;
404                                    yybegin(QSTRING);
405                                    return ScilabLexerConstants.STRING;
406                                  }
407
408   {badid}                        |
409   {badop}                        |
410   {whitabs}                      {
411                                    return ScilabLexerConstants.ERROR;
412                                  }
413
414   " "                            {
415                                    return ScilabLexerConstants.WHITE;
416                                  }
417
418   "\t"                           {
419                                    return ScilabLexerConstants.TAB;
420                                  }
421
422   .                              |
423   {eol}                          {
424                                    transposable = false;
425                                    return ScilabLexerConstants.DEFAULT;
426                                  }
427 }
428
429 <COMMANDS> {
430   [ \t]*"("                      {
431                                    yypushback(yylength());
432                                    yybegin(YYINITIAL);
433                                  }
434
435   " "                            {
436                                    yybegin(COMMANDSWHITE);
437                                    return ScilabLexerConstants.WHITE;
438                                  }
439
440   "\t"                           {
441                                    yybegin(COMMANDSWHITE);
442                                    return ScilabLexerConstants.TAB;
443                                  }
444   .
445                                  {
446                                    yypushback(1);
447                                    yybegin(YYINITIAL);
448                                  }
449
450   {eol}                          { }
451 }
452
453 <COMMANDSWHITE> {
454   {comment}                      {
455                                    transposable = false;
456                                    yypushback(2);
457                                    yybegin(COMMENT);
458                                  }
459
460   ([^ \t,;/]*) | ("/"[^ /]*)     {
461                                    return ScilabLexerConstants.STRING;
462                                  }
463
464   " "                            {
465                                    return ScilabLexerConstants.WHITE;
466                                  }
467
468   "\t"                           {
469                                    return ScilabLexerConstants.TAB;
470                                  }
471   .
472                                  {
473                                    yypushback(1);
474                                    yybegin(YYINITIAL);
475                                  }
476
477   {eol}                          { }
478 }
479
480 <FIELD> {
481   {id}                           {
482                                    transposable = true;
483                                    return ScilabLexerConstants.FIELD;
484                                  }
485
486   .                              {
487                                    yypushback(1);
488                                    yybegin(YYINITIAL);
489                                  }
490
491   {eol}                          { }
492 }
493
494 <QSTRING> {
495   {breakinstring}                {
496                                    yypushback(yylength());
497                                    yybegin(BREAKSTRING);
498                                    transposable = false;
499                                    return ScilabLexerConstants.STRING;
500                                  }
501
502   " "                            {
503                                    return ScilabLexerConstants.WHITE_STRING;
504                                  }
505
506   "\t"                           {
507                                    return ScilabLexerConstants.TAB_STRING;
508                                  }
509
510   {string}                       |
511   "."                            {
512                                    return ScilabLexerConstants.STRING;
513                                  }
514
515   (\'|\")                        {
516                                    transposable = false;
517                                    yybegin(YYINITIAL);
518                                    return ScilabLexerConstants.STRING;
519                                  }
520
521   .                              |
522   {eol}                          {
523                                    return ScilabLexerConstants.DEFAULT;
524                                  }
525 }
526
527 <BLOCKCOMMENT> {
528   {authors}                      {
529                                    return ScilabLexerConstants.AUTHORS;
530                                  }
531
532   {todo}                         {
533                                    return ScilabLexerConstants.TODO;
534                                  }
535
536   {url}                          {
537                                    return ScilabLexerConstants.URL;
538                                  }
539
540   {mail}                         {
541                                    return ScilabLexerConstants.MAIL;
542                                  }
543
544   {latex}                        {
545                                    return ScilabLexerConstants.LATEX;
546                                  }
547
548   " "                            {
549                                    return ScilabLexerConstants.WHITE_COMMENT;
550                                  }
551
552   "\t"                           {
553                                    return ScilabLexerConstants.TAB_COMMENT;
554                                  }
555
556   {endcomment}                   {
557                                    yybegin(YYINITIAL);
558                                    return ScilabLexerConstants.COMMENT;
559                                  }
560
561   .                              |
562   {eol}                          {
563                                    return ScilabLexerConstants.COMMENT;
564                                  }
565 }
566
567 <COMMENT> {
568   {authors}                      {
569                                    return ScilabLexerConstants.AUTHORS;
570                                  }
571
572   {todo}                         {
573                                    return ScilabLexerConstants.TODO;
574                                  }
575
576   {url}                          {
577                                    return ScilabLexerConstants.URL;
578                                  }
579
580   {mail}                         {
581                                    return ScilabLexerConstants.MAIL;
582                                  }
583
584   {latex}                        {
585                                    return ScilabLexerConstants.LATEX;
586                                  }
587
588   " "                            {
589                                    return ScilabLexerConstants.WHITE_COMMENT;
590                                  }
591
592   "\t"                           {
593                                    return ScilabLexerConstants.TAB_COMMENT;
594                                  }
595
596   .                              |
597   {eol}                          {
598                                    return ScilabLexerConstants.COMMENT;
599                                  }
600 }
601
602 <BREAKSTRING> {
603   {break}                        {
604                                    breakstring = true;
605                                    return ScilabLexerConstants.SPECIAL;
606                                  }
607
608   " "                            {
609                                    return ScilabLexerConstants.WHITE;
610                                  }
611
612   "\t"                           {
613                                    return ScilabLexerConstants.TAB;
614                                  }
615
616   {comment}                      {
617                                    transposable = false;
618                                    yypushback(2);
619                                    yybegin(COMMENT);
620                                  }
621
622   .                              |
623   {eol}                          {
624                                    return ScilabLexerConstants.DEFAULT;
625                                  }
626 }
627
628 <<EOF>>                          {
629                                    return ScilabLexerConstants.EOF;
630                                  }