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