Scinotes: fix a NPE when editing huge file
[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         // Pre condition
138         if (elem == null) {
139             return ScilabLexerConstants.DEFAULT;
140         }
141
142         Element line = elem.getElement(elem.getElementIndex(pos));
143         int end = line.getEndOffset();
144         int tok = -1;
145         start = line.getStartOffset();
146         int startL = start;
147         int s = -1;
148
149         try {
150            yyreset(new ScilabDocumentReader(doc, start, end));
151            if (!strict) {
152               pos++;
153            }
154
155            while (startL < pos && (s != startL || yystate() == BREAKSTRING)) {
156                s = startL;
157                tok = yylex();
158                startL = start + yychar + yylength();
159            }
160
161            return tok;
162         } catch (IOException e) {
163            return ScilabLexerConstants.DEFAULT;
164         }
165      }
166
167      public static ScilabTokens getScilabTokens(String str) {
168          ScilabDocument doc = new ScilabDocument(false);
169          try {
170              doc.insertString(0, str, null);
171          } catch (BadLocationException e) { }
172          return getScilabTokens(doc);
173      }
174
175      public static ScilabTokens getScilabTokens(ScilabDocument doc) {
176          ScilabLexer lexer = new ScilabLexer(doc);
177          lexer.yyreset(new ScilabDocumentReader(doc, 0, doc.getLength()));
178          ScilabTokens tokens = new ScilabTokens();
179          int tok = -1;
180          try {
181             while (tok != ScilabLexerConstants.EOF) {
182                tok = lexer.yylex();
183                tokens.add(tok, lexer.yychar + lexer.yylength());
184             }
185          } catch (IOException e) { }
186  
187          return tokens;
188      }
189
190      public static class ScilabTokens {
191         private List<Integer> tokenType = new ArrayList<Integer>();
192         private List<Integer> tokenPos = new ArrayList<Integer>();
193
194         ScilabTokens() { }
195
196         void add(final int type, final int pos) {
197            tokenType.add(type);
198            tokenPos.add(pos);
199         }
200
201         public final List<Integer> getTokenType() {
202            return tokenType;
203         }
204
205         public final List<Integer> getTokenPos() {
206            return tokenPos;
207         }
208      }
209 %}
210
211 /* main character classes */
212 eol = \n
213
214 open = "[" | "(" | "{"
215 close = "]" | ")" | "}"
216
217 comment = "//"
218 startcomment = ("/" "*"+)
219 endcomment = ("*"+) "/"
220
221 quote = "'"
222
223 dquote = "\""
224
225 cstes = "%t" | "%T" | "%f" | "%F" | "%e" | "%pi" | "%inf" | "%i" | "%z" | "%s" | "%nan" | "%eps" | "SCI" | "WSCI" | "SCIHOME" | "TMPDIR"
226
227 operator = ".'" | ".*" | "./" | ".\\" | ".^" | ".**" | "+" | "-" | "/" | "\\" | "*" | "^" | "**" | "==" | "~=" | "<>" | "<" | ">" | "<=" | ">=" | ".*." | "./." | ".\\." | "/." | "=" | "&" | "|" | "@" | "@=" | "~" | "&&" | "||"
228
229 functionKwds = "function" | "endfunction"
230
231 structureKwds = "then" | "do" | "catch" | "case" | "otherwise"
232
233 elseif = "elseif" | "else"
234
235 openCloseStructureKwds = "if" | "for" | "while" | "try" | "select" | "switch"
236
237 end = "end"
238
239 controlKwds = "abort" | "break" | "quit" | "return" | "resume" | "pause" | "continue" | "exit"
240
241 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"
242
243 break = ".."(".")*
244 breakinstring = {break}[ \t]*{comment}?
245
246 special = "$" | ":" | {break}
247
248 string = (([^ \t\'\"\r\n\.]*)|([\'\"]{2}))+
249
250 id = ([a-zA-Z%_#!?][a-zA-Z0-9_#!$?]*)|("$"[a-zA-Z0-9_#!$?]+)
251
252 badid = ([0-9$][a-zA-Z0-9_#!$?]+)
253 whitabs = (" "+"\t" | "\t"+" ")[ \t]*
254 badop = [+-]([\*\/\\\^] | "."[\*\+\-\/\\\^]) | ":=" | "->" | " !=" | ("&&" "&"+) | ("||" "|"+) | ([*+-/\\\^]"=")
255
256 dot = "."
257
258 url = "http://"[^ \t\f\n\r\'\"]+
259 mail = "<"[ \t]*[a-zA-Z0-9_\.\-]+"@"([a-zA-Z0-9\-]+".")+[a-zA-Z]{2,5}[ \t]*">"
260
261 latex = "$"(([^$]*|"\\$")+)"$"
262 latexinstring = (\"|\')"$"(([^$\'\"]*|"\\$"|([\'\"]{2}))+)"$"(\"|\')
263
264 digit = [0-9]
265 exp = [dDeE][+-]?{digit}*
266 number = ({digit}+"."?{digit}*{exp}?)|("."{digit}+{exp}?)
267
268 %x QSTRING, COMMENT, BLOCKCOMMENT, FIELD, COMMANDS, COMMANDSWHITE, BREAKSTRING
269
270 %%
271
272 <YYINITIAL> {
273   {comment}                      {
274                                    transposable = false;
275                                    yypushback(2);
276                                    yybegin(COMMENT);
277                                  }
278
279   {startcomment}                 {
280                                    transposable = false;
281                                    yypushback(2);
282                                    yybegin(BLOCKCOMMENT);
283                                  }
284
285   {operator}                     {
286                                    transposable = false;
287                                    return ScilabLexerConstants.OPERATOR;
288                                  }
289
290   {functionKwds}                 {
291                                    transposable = false;
292                                    return ScilabLexerConstants.FKEYWORD;
293                                  }
294
295   {openCloseStructureKwds}       {
296                                    transposable = false;
297                                    return ScilabLexerConstants.OSKEYWORD;
298                                  }
299
300   {end}                          {
301                                    transposable = false;
302                                    if (matchBlock != null) {
303                                       MatchingBlockScanner.MatchingPositions pos = matchBlock.getMatchingBlock(start + yychar + yylength(), false);
304                                       if (pos != null) {
305                                          try {
306                                              String match = doc.getText(pos.secondB, pos.secondE - pos.secondB);
307                                              if (match.equals("function")) {
308                                                 return ScilabLexerConstants.FKEYWORD;
309                                              }
310                                          } catch (BadLocationException e) { }
311                                       }
312                                    }
313                                    return ScilabLexerConstants.OSKEYWORD;
314                                  }
315
316   {structureKwds}                {
317                                    transposable = false;
318                                    return ScilabLexerConstants.SKEYWORD;
319                                  }
320
321   {elseif}                       {
322                                    transposable = false;
323                                    return ScilabLexerConstants.ELSEIF;
324                                  }
325
326   {controlKwds}                  {
327                                    transposable = false;
328                                    return ScilabLexerConstants.CKEYWORD;
329                                  }
330
331   {cstes}                        {
332                                    transposable = true;
333                                    return ScilabLexerConstants.CONSTANTES;
334                                  }
335
336   {id}                           {
337                                    transposable = true;
338                                    String str = yytext();
339                                    if (commands.contains(str)) {
340                                        yybegin(COMMANDS);
341                                        return ScilabLexerConstants.COMMANDS;
342                                    } else if (macros.contains(str)) {
343                                        yybegin(COMMANDS);
344                                        return ScilabLexerConstants.MACROS;
345                                    } else if (infile.contains(str)) {
346                                        yybegin(COMMANDS);
347                                        return ScilabLexerConstants.MACROINFILE;
348                                    } else {
349                                        List<String>[] arr = doc.getInOutArgs(start + yychar);
350                                        if (arr != null && (arr[0].contains(str) || arr[1].contains(str))) {
351                                            return ScilabLexerConstants.INPUTOUTPUTARGS;
352                                        } else if (variables.contains(str)) {
353                                            return ScilabLexerConstants.VARIABLES;
354                                        }
355                                    }
356                                    return ScilabLexerConstants.ID;
357                                  }
358
359   {number}                       {
360                                    transposable = true;
361                                    return ScilabLexerConstants.NUMBER;
362                                  }
363
364   {special}                      {
365                                    transposable = false;
366                                    return ScilabLexerConstants.SPECIAL;
367                                  }
368
369   {dot}                          {
370                                    transposable = false;
371                                    yybegin(FIELD);
372                                    return ScilabLexerConstants.OPERATOR;
373                                  }
374
375   {latexinstring}                {
376                                    return ScilabLexerConstants.LATEX;
377                                  }
378
379   {quote}                        {
380                                     if (transposable) {
381                                        return ScilabLexerConstants.TRANSP;
382                                     } else {
383                                        beginString = zzStartRead;
384                                        yybegin(QSTRING);
385                                        return ScilabLexerConstants.STRING;
386                                     }
387                                  }
388
389   {open}                         {
390                                    transposable = false;
391                                    return ScilabLexerConstants.OPENCLOSE;
392                                  }
393
394   {close}                        {
395                                    transposable = true;
396                                    return ScilabLexerConstants.OPENCLOSE;
397                                  }
398
399   {dquote}                       {
400                                    transposable = false;
401                                    beginString = zzStartRead;
402                                    yybegin(QSTRING);
403                                    return ScilabLexerConstants.STRING;
404                                  }
405
406   {badid}                        |
407   {badop}                        |
408   {whitabs}                      {
409                                    return ScilabLexerConstants.ERROR;
410                                  }
411
412   " "                            {
413                                    return ScilabLexerConstants.WHITE;
414                                  }
415
416   "\t"                           {
417                                    return ScilabLexerConstants.TAB;
418                                  }
419
420   .                              |
421   {eol}                          {
422                                    transposable = false;
423                                    return ScilabLexerConstants.DEFAULT;
424                                  }
425 }
426
427 <COMMANDS> {
428   [ \t]*"("                      {
429                                    yypushback(yylength());
430                                    yybegin(YYINITIAL);
431                                  }
432
433   " "                            {
434                                    yybegin(COMMANDSWHITE);
435                                    return ScilabLexerConstants.WHITE;
436                                  }
437
438   "\t"                           {
439                                    yybegin(COMMANDSWHITE);
440                                    return ScilabLexerConstants.TAB;
441                                  }
442   .
443                                  {
444                                    yypushback(1);
445                                    yybegin(YYINITIAL);
446                                  }
447
448   {eol}                          { }
449 }
450
451 <COMMANDSWHITE> {
452   {comment}                      {
453                                    transposable = false;
454                                    yypushback(2);
455                                    yybegin(COMMENT);
456                                  }
457
458   ([^ \t,;/]*) | ("/"[^ /]*)     {
459                                    return ScilabLexerConstants.STRING;
460                                  }
461
462   " "                            {
463                                    return ScilabLexerConstants.WHITE;
464                                  }
465
466   "\t"                           {
467                                    return ScilabLexerConstants.TAB;
468                                  }
469   .
470                                  {
471                                    yypushback(1);
472                                    yybegin(YYINITIAL);
473                                  }
474
475   {eol}                          { }
476 }
477
478 <FIELD> {
479   {id}                           {
480                                    transposable = true;
481                                    return ScilabLexerConstants.FIELD;
482                                  }
483
484   .                              {
485                                    yypushback(1);
486                                    yybegin(YYINITIAL);
487                                  }
488
489   {eol}                          { }
490 }
491
492 <QSTRING> {
493   {breakinstring}                {
494                                    yypushback(yylength());
495                                    yybegin(BREAKSTRING);
496                                    transposable = false;
497                                    return ScilabLexerConstants.STRING;
498                                  }
499
500   " "                            {
501                                    return ScilabLexerConstants.WHITE_STRING;
502                                  }
503
504   "\t"                           {
505                                    return ScilabLexerConstants.TAB_STRING;
506                                  }
507
508   {string}                       |
509   "."                            {
510                                    return ScilabLexerConstants.STRING;
511                                  }
512
513   (\'|\")                        {
514                                    transposable = false;
515                                    yybegin(YYINITIAL);
516                                    return ScilabLexerConstants.STRING;
517                                  }
518
519   .                              |
520   {eol}                          {
521                                    return ScilabLexerConstants.DEFAULT;
522                                  }
523 }
524
525 <BLOCKCOMMENT> {
526   {authors}                      {
527                                    return ScilabLexerConstants.AUTHORS;
528                                  }
529
530   {url}                          {
531                                    return ScilabLexerConstants.URL;
532                                  }
533
534   {mail}                         {
535                                    return ScilabLexerConstants.MAIL;
536                                  }
537
538   {latex}                        {
539                                    return ScilabLexerConstants.LATEX;
540                                  }
541
542   " "                            {
543                                    return ScilabLexerConstants.WHITE_COMMENT;
544                                  }
545
546   "\t"                           {
547                                    return ScilabLexerConstants.TAB_COMMENT;
548                                  }
549
550   {endcomment}                   {
551                                    yybegin(YYINITIAL);
552                                    return ScilabLexerConstants.COMMENT;
553                                  }
554
555   .                              |
556   {eol}                          {
557                                    return ScilabLexerConstants.COMMENT;
558                                  }
559 }
560
561 <COMMENT> {
562   {authors}                      {
563                                    return ScilabLexerConstants.AUTHORS;
564                                  }
565
566   {url}                          {
567                                    return ScilabLexerConstants.URL;
568                                  }
569
570   {mail}                         {
571                                    return ScilabLexerConstants.MAIL;
572                                  }
573
574   {latex}                        {
575                                    return ScilabLexerConstants.LATEX;
576                                  }
577
578   " "                            {
579                                    return ScilabLexerConstants.WHITE_COMMENT;
580                                  }
581
582   "\t"                           {
583                                    return ScilabLexerConstants.TAB_COMMENT;
584                                  }
585
586   .                              |
587   {eol}                          {
588                                    return ScilabLexerConstants.COMMENT;
589                                  }
590 }
591
592 <BREAKSTRING> {
593   {break}                        {
594                                    breakstring = true;
595                                    return ScilabLexerConstants.SPECIAL;
596                                  }
597
598   " "                            {
599                                    return ScilabLexerConstants.WHITE;
600                                  }
601
602   "\t"                           {
603                                    return ScilabLexerConstants.TAB;
604                                  }
605
606   {comment}                      {
607                                    transposable = false;
608                                    yypushback(2);
609                                    yybegin(COMMENT);
610                                  }
611
612   .                              |
613   {eol}                          {
614                                    return ScilabLexerConstants.DEFAULT;
615                                  }
616 }
617
618 <<EOF>>                          {
619                                    return ScilabLexerConstants.EOF;
620                                  }