3 package org.scilab.modules.scinotes;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashSet;
11 import javax.swing.text.BadLocationException;
12 import javax.swing.text.Element;
13 import org.scilab.modules.commons.ScilabCommonsUtils;
15 @javax.annotation.Generated("JFlex")
21 %extends ScilabLexerConstants
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;
37 private ScilabDocument doc;
38 private boolean transposable;
40 private boolean breakstring;
41 private boolean breakcomment;
42 private MatchingBlockScanner matchBlock;
45 // For SciNotes colors in preferences
50 public ScilabLexer(ScilabDocument doc) {
51 this(doc, new MatchingBlockScanner(doc), true);
54 public ScilabLexer(ScilabDocument doc, boolean update) {
55 this(doc, new MatchingBlockScanner(doc), update);
58 public ScilabLexer(ScilabDocument doc, MatchingBlockScanner matchBlock, boolean update) {
60 this.elem = doc.getDefaultRootElement();
61 this.infile = doc.getFunctionsInDoc();
62 this.matchBlock = matchBlock;
68 public static void update() {
69 if (ScilabCommonsUtils.isScilabThread()) {
70 String[] vars = ScilabKeywords.GetVariablesName();
71 String[] funs = ScilabKeywords.GetFunctionsName();
72 String[] macs = ScilabKeywords.GetMacrosName();
77 variables.addAll(Arrays.asList(vars));
80 commands.addAll(Arrays.asList(funs));
83 macros.addAll(Arrays.asList(macs));
88 public void setRange(int p0, int p1) {
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()) {
99 } else if (e.isBlockComment()) {
100 yybegin(BLOCKCOMMENT);
105 public int yychar() {
109 public int scan() throws IOException {
111 int lastPos = start + yychar() + yylength();
112 if (lastPos == end - 1) {
113 ((ScilabDocument.ScilabLeafElement) elem.getElement(elem.getElementIndex(start))).setBrokenString(breakstring);
115 } else if (lastPos == end) {
116 ((ScilabDocument.ScilabLeafElement) elem.getElement(elem.getElementIndex(start))).setBlockComment(yystate() == BLOCKCOMMENT);
121 public boolean isLineFinishedByBlockComment(int start, int end) {
125 yyreset(new ScilabDocumentReader(doc, start, end));
127 while (tok != ScilabLexerConstants.EOF) {
130 } catch (Exception e) { }
132 return yystate() == BLOCKCOMMENT;
135 public int getKeyword(int pos, boolean strict) {
138 return ScilabLexerConstants.DEFAULT;
141 Element line = elem.getElement(elem.getElementIndex(pos));
142 int end = line.getEndOffset();
144 start = line.getStartOffset();
149 yyreset(new ScilabDocumentReader(doc, start, end));
154 while (startL < pos && (s != startL || yystate() == BREAKSTRING)) {
157 startL = start + yychar() + yylength();
161 } catch (IOException e) {
162 return ScilabLexerConstants.DEFAULT;
166 public static ScilabTokens getScilabTokens(String str) {
167 ScilabDocument doc = new ScilabDocument(false);
169 doc.insertString(0, str, null);
170 } catch (BadLocationException e) { }
171 return getScilabTokens(doc);
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();
180 while (tok != ScilabLexerConstants.EOF) {
182 tokens.add(tok, lexer.yychar() + lexer.yylength());
184 } catch (IOException e) { }
189 public static class ScilabTokens {
190 private List<Integer> tokenType = new ArrayList<Integer>();
191 private List<Integer> tokenPos = new ArrayList<Integer>();
195 void add(final int type, final int pos) {
200 public final List<Integer> getTokenType() {
204 public final List<Integer> getTokenPos() {
210 /* main character classes */
213 open = "[" | "(" | "{"
214 close = "]" | ")" | "}"
217 startcomment = ("/" "*"+)
218 endcomment = ("*"+) "/"
224 cstes = "%t" | "%T" | "%f" | "%F" | "%e" | "%pi" | "%inf" | "%i" | "%z" | "%s" | "%nan" | "%eps" | "SCI" | "WSCI" | "SCIHOME" | "TMPDIR"
226 operator = ".'" | ".*" | "./" | ".\\" | ".^" | ".**" | "+" | "-" | "/" | "\\" | "*" | "^" | "**" | "==" | "~=" | "<>" | "<" | ">" | "<=" | ">=" | ".*." | "./." | ".\\." | "/." | "=" | "&" | "|" | "@" | "@=" | "~" | "&&" | "||"
228 functionKwds = "function" | "endfunction"
230 structureKwds = "then" | "do" | "catch" | "case" | "otherwise"
232 elseif = "elseif" | "else"
234 openCloseStructureKwds = "if" | "for" | "while" | "try" | "select" | "switch"
238 controlKwds = "abort" | "break" | "quit" | "return" | "resume" | "pause" | "continue" | "exit"
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"
243 todo = ("TODO" | "todo" | "Todo")[ \t:]+[^\n]*
246 breakinstring = {break}[ \t]*{comment}?
248 special = "$" | ":" | {break}
250 string = (([^ \t\'\"\r\n\.]*)|([\'\"]{2}))+
252 id = ([a-zA-Z%_#!?][a-zA-Z0-9_#!$?]*)|("$"[a-zA-Z0-9_#!$?]+)
254 badid = ([0-9$][a-zA-Z0-9_#!$?]+)
255 whitabs = (" "+"\t" | "\t"+" ")[ \t]*
256 badop = [+-]([\*\/\\\^] | "."[\*\+\-\/\\\^]) | ":=" | "->" | " !=" | ("&&" "&"+) | ("||" "|"+) | ([*+-/\\\^]"=")
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]*">"
263 latex = "$"(([^$]*|"\\$")+)"$"
264 latexinstring = (\"|\')"$"(([^$\'\"]*|"\\$"|([\'\"]{2}))+)"$"(\"|\')
267 exp = [dDeE][+-]?{digit}*
268 number = ({digit}+"."?{digit}*{exp}?)|("."{digit}+{exp}?)
270 %x QSTRING, COMMENT, BLOCKCOMMENT, FIELD, COMMANDS, COMMANDSWHITE, BREAKSTRING
276 transposable = false;
282 transposable = false;
284 yybegin(BLOCKCOMMENT);
288 transposable = false;
289 return ScilabLexerConstants.OPERATOR;
293 transposable = false;
294 return ScilabLexerConstants.FKEYWORD;
297 {openCloseStructureKwds} {
298 transposable = false;
299 return ScilabLexerConstants.OSKEYWORD;
303 transposable = false;
304 if (matchBlock != null) {
305 MatchingBlockScanner.MatchingPositions pos = matchBlock.getMatchingBlock(start + yychar() + yylength(), false);
308 String match = doc.getText(pos.secondB, pos.secondE - pos.secondB);
309 if (match.equals("function")) {
310 return ScilabLexerConstants.FKEYWORD;
312 } catch (BadLocationException e) { }
315 return ScilabLexerConstants.OSKEYWORD;
319 transposable = false;
320 return ScilabLexerConstants.SKEYWORD;
324 transposable = false;
325 return ScilabLexerConstants.ELSEIF;
329 transposable = false;
330 return ScilabLexerConstants.CKEYWORD;
335 return ScilabLexerConstants.CONSTANTES;
340 String str = yytext();
341 if (commands.contains(str)) {
343 return ScilabLexerConstants.COMMANDS;
344 } else if (macros.contains(str)) {
346 return ScilabLexerConstants.MACROS;
347 } else if (infile.contains(str)) {
349 return ScilabLexerConstants.MACROINFILE;
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;
358 return ScilabLexerConstants.ID;
363 return ScilabLexerConstants.NUMBER;
367 transposable = false;
368 return ScilabLexerConstants.SPECIAL;
372 transposable = false;
374 return ScilabLexerConstants.OPERATOR;
378 return ScilabLexerConstants.LATEX;
383 return ScilabLexerConstants.TRANSP;
385 beginString = zzStartRead;
387 return ScilabLexerConstants.STRING;
392 transposable = false;
393 return ScilabLexerConstants.OPENCLOSE;
398 return ScilabLexerConstants.OPENCLOSE;
402 transposable = false;
403 beginString = zzStartRead;
405 return ScilabLexerConstants.STRING;
411 return ScilabLexerConstants.ERROR;
415 return ScilabLexerConstants.WHITE;
419 return ScilabLexerConstants.TAB;
424 transposable = false;
425 return ScilabLexerConstants.DEFAULT;
431 yypushback(yylength());
436 yybegin(COMMANDSWHITE);
437 return ScilabLexerConstants.WHITE;
441 yybegin(COMMANDSWHITE);
442 return ScilabLexerConstants.TAB;
455 transposable = false;
460 ([^ \t,;/]*) | ("/"[^ /]*) {
461 return ScilabLexerConstants.STRING;
465 return ScilabLexerConstants.WHITE;
469 return ScilabLexerConstants.TAB;
483 return ScilabLexerConstants.FIELD;
496 yypushback(yylength());
497 yybegin(BREAKSTRING);
498 transposable = false;
499 return ScilabLexerConstants.STRING;
503 return ScilabLexerConstants.WHITE_STRING;
507 return ScilabLexerConstants.TAB_STRING;
512 return ScilabLexerConstants.STRING;
516 transposable = false;
518 return ScilabLexerConstants.STRING;
523 return ScilabLexerConstants.DEFAULT;
529 return ScilabLexerConstants.AUTHORS;
533 return ScilabLexerConstants.TODO;
537 return ScilabLexerConstants.URL;
541 return ScilabLexerConstants.MAIL;
545 return ScilabLexerConstants.LATEX;
549 return ScilabLexerConstants.WHITE_COMMENT;
553 return ScilabLexerConstants.TAB_COMMENT;
558 return ScilabLexerConstants.COMMENT;
563 return ScilabLexerConstants.COMMENT;
569 return ScilabLexerConstants.AUTHORS;
573 return ScilabLexerConstants.TODO;
577 return ScilabLexerConstants.URL;
581 return ScilabLexerConstants.MAIL;
585 return ScilabLexerConstants.LATEX;
589 return ScilabLexerConstants.WHITE_COMMENT;
593 return ScilabLexerConstants.TAB_COMMENT;
598 return ScilabLexerConstants.COMMENT;
605 return ScilabLexerConstants.SPECIAL;
609 return ScilabLexerConstants.WHITE;
613 return ScilabLexerConstants.TAB;
617 transposable = false;
624 return ScilabLexerConstants.DEFAULT;
629 return ScilabLexerConstants.EOF;