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