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")
16 @SuppressWarnings("fallthrough")
22 %extends ScilabLexerConstants
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;
38 private ScilabDocument doc;
39 private boolean transposable;
41 private boolean breakstring;
42 private boolean breakcomment;
43 private MatchingBlockScanner matchBlock;
46 // For SciNotes colors in preferences
51 public ScilabLexer(ScilabDocument doc) {
52 this(doc, new MatchingBlockScanner(doc), true);
55 public ScilabLexer(ScilabDocument doc, boolean update) {
56 this(doc, new MatchingBlockScanner(doc), update);
59 public ScilabLexer(ScilabDocument doc, MatchingBlockScanner matchBlock, boolean update) {
61 this.elem = doc.getDefaultRootElement();
62 this.infile = doc.getFunctionsInDoc();
63 this.matchBlock = matchBlock;
69 public static void update() {
70 if (ScilabCommonsUtils.isScilabThread()) {
71 String[] vars = ScilabKeywords.GetVariablesName();
72 String[] funs = ScilabKeywords.GetFunctionsName();
73 String[] macs = ScilabKeywords.GetMacrosName();
78 variables.addAll(Arrays.asList(vars));
81 commands.addAll(Arrays.asList(funs));
84 macros.addAll(Arrays.asList(macs));
89 public void setRange(int p0, int p1) {
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()) {
100 } else if (e.isBlockComment()) {
101 yybegin(BLOCKCOMMENT);
106 public int yychar() {
110 public int scan() throws IOException {
112 int lastPos = start + yychar + yylength();
113 if (lastPos == end - 1) {
114 ((ScilabDocument.ScilabLeafElement) elem.getElement(elem.getElementIndex(start))).setBrokenString(breakstring);
116 } else if (lastPos == end) {
117 ((ScilabDocument.ScilabLeafElement) elem.getElement(elem.getElementIndex(start))).setBlockComment(yystate() == BLOCKCOMMENT);
122 public boolean isLineFinishedByBlockComment(int start, int end) {
126 yyreset(new ScilabDocumentReader(doc, start, end));
128 while (tok != ScilabLexerConstants.EOF) {
131 } catch (Exception e) { }
133 return yystate() == BLOCKCOMMENT;
136 public int getKeyword(int pos, boolean strict) {
139 return ScilabLexerConstants.DEFAULT;
142 Element line = elem.getElement(elem.getElementIndex(pos));
143 int end = line.getEndOffset();
145 start = line.getStartOffset();
150 yyreset(new ScilabDocumentReader(doc, start, end));
155 while (startL < pos && (s != startL || yystate() == BREAKSTRING)) {
158 startL = start + yychar + yylength();
162 } catch (IOException e) {
163 return ScilabLexerConstants.DEFAULT;
167 public static ScilabTokens getScilabTokens(String str) {
168 ScilabDocument doc = new ScilabDocument(false);
170 doc.insertString(0, str, null);
171 } catch (BadLocationException e) { }
172 return getScilabTokens(doc);
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();
181 while (tok != ScilabLexerConstants.EOF) {
183 tokens.add(tok, lexer.yychar + lexer.yylength());
185 } catch (IOException e) { }
190 public static class ScilabTokens {
191 private List<Integer> tokenType = new ArrayList<Integer>();
192 private List<Integer> tokenPos = new ArrayList<Integer>();
196 void add(final int type, final int pos) {
201 public final List<Integer> getTokenType() {
205 public final List<Integer> getTokenPos() {
211 /* main character classes */
214 open = "[" | "(" | "{"
215 close = "]" | ")" | "}"
218 startcomment = ("/" "*"+)
219 endcomment = ("*"+) "/"
225 cstes = "%t" | "%T" | "%f" | "%F" | "%e" | "%pi" | "%inf" | "%i" | "%z" | "%s" | "%nan" | "%eps" | "SCI" | "WSCI" | "SCIHOME" | "TMPDIR"
227 operator = ".'" | ".*" | "./" | ".\\" | ".^" | ".**" | "+" | "-" | "/" | "\\" | "*" | "^" | "**" | "==" | "~=" | "<>" | "<" | ">" | "<=" | ">=" | ".*." | "./." | ".\\." | "/." | "=" | "&" | "|" | "@" | "@=" | "~" | "&&" | "||"
229 functionKwds = "function" | "endfunction"
231 structureKwds = "then" | "do" | "catch" | "case" | "otherwise"
233 elseif = "elseif" | "else"
235 openCloseStructureKwds = "if" | "for" | "while" | "try" | "select" | "switch"
239 controlKwds = "abort" | "break" | "quit" | "return" | "resume" | "pause" | "continue" | "exit"
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"
244 breakinstring = {break}[ \t]*{comment}?
246 special = "$" | ":" | {break}
248 string = (([^ \t\'\"\r\n\.]*)|([\'\"]{2}))+
250 id = ([a-zA-Z%_#!?][a-zA-Z0-9_#!$?]*)|("$"[a-zA-Z0-9_#!$?]+)
252 badid = ([0-9$][a-zA-Z0-9_#!$?]+)
253 whitabs = (" "+"\t" | "\t"+" ")[ \t]*
254 badop = [+-]([\*\/\\\^] | "."[\*\+\-\/\\\^]) | ":=" | "->" | " !=" | ("&&" "&"+) | ("||" "|"+) | ([*+-/\\\^]"=")
258 url = ("http://"|"https://"|"ftp://"|"sftp://"|"ftps://"|"smb:///"|"file://")[^ \t\f\n\r\'\"]+
259 mail = "<"[ \t]*[a-zA-Z0-9_\.\-]+"@"([a-zA-Z0-9\-]+".")+[a-zA-Z]{2,5}[ \t]*">"
261 latex = "$"(([^$]*|"\\$")+)"$"
262 latexinstring = (\"|\')"$"(([^$\'\"]*|"\\$"|([\'\"]{2}))+)"$"(\"|\')
265 exp = [dDeE][+-]?{digit}*
266 number = ({digit}+"."?{digit}*{exp}?)|("."{digit}+{exp}?)
268 %x QSTRING, COMMENT, BLOCKCOMMENT, FIELD, COMMANDS, COMMANDSWHITE, BREAKSTRING
274 transposable = false;
280 transposable = false;
282 yybegin(BLOCKCOMMENT);
286 transposable = false;
287 return ScilabLexerConstants.OPERATOR;
291 transposable = false;
292 return ScilabLexerConstants.FKEYWORD;
295 {openCloseStructureKwds} {
296 transposable = false;
297 return ScilabLexerConstants.OSKEYWORD;
301 transposable = false;
302 if (matchBlock != null) {
303 MatchingBlockScanner.MatchingPositions pos = matchBlock.getMatchingBlock(start + yychar + yylength(), false);
306 String match = doc.getText(pos.secondB, pos.secondE - pos.secondB);
307 if (match.equals("function")) {
308 return ScilabLexerConstants.FKEYWORD;
310 } catch (BadLocationException e) { }
313 return ScilabLexerConstants.OSKEYWORD;
317 transposable = false;
318 return ScilabLexerConstants.SKEYWORD;
322 transposable = false;
323 return ScilabLexerConstants.ELSEIF;
327 transposable = false;
328 return ScilabLexerConstants.CKEYWORD;
333 return ScilabLexerConstants.CONSTANTES;
338 String str = yytext();
339 if (commands.contains(str)) {
341 return ScilabLexerConstants.COMMANDS;
342 } else if (macros.contains(str)) {
344 return ScilabLexerConstants.MACROS;
345 } else if (infile.contains(str)) {
347 return ScilabLexerConstants.MACROINFILE;
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;
356 return ScilabLexerConstants.ID;
361 return ScilabLexerConstants.NUMBER;
365 transposable = false;
366 return ScilabLexerConstants.SPECIAL;
370 transposable = false;
372 return ScilabLexerConstants.OPERATOR;
376 return ScilabLexerConstants.LATEX;
381 return ScilabLexerConstants.TRANSP;
383 beginString = zzStartRead;
385 return ScilabLexerConstants.STRING;
390 transposable = false;
391 return ScilabLexerConstants.OPENCLOSE;
396 return ScilabLexerConstants.OPENCLOSE;
400 transposable = false;
401 beginString = zzStartRead;
403 return ScilabLexerConstants.STRING;
409 return ScilabLexerConstants.ERROR;
413 return ScilabLexerConstants.WHITE;
417 return ScilabLexerConstants.TAB;
422 transposable = false;
423 return ScilabLexerConstants.DEFAULT;
429 yypushback(yylength());
434 yybegin(COMMANDSWHITE);
435 return ScilabLexerConstants.WHITE;
439 yybegin(COMMANDSWHITE);
440 return ScilabLexerConstants.TAB;
453 transposable = false;
458 ([^ \t,;/]*) | ("/"[^ /]*) {
459 return ScilabLexerConstants.STRING;
463 return ScilabLexerConstants.WHITE;
467 return ScilabLexerConstants.TAB;
481 return ScilabLexerConstants.FIELD;
494 yypushback(yylength());
495 yybegin(BREAKSTRING);
496 transposable = false;
497 return ScilabLexerConstants.STRING;
501 return ScilabLexerConstants.WHITE_STRING;
505 return ScilabLexerConstants.TAB_STRING;
510 return ScilabLexerConstants.STRING;
514 transposable = false;
516 return ScilabLexerConstants.STRING;
521 return ScilabLexerConstants.DEFAULT;
527 return ScilabLexerConstants.AUTHORS;
531 return ScilabLexerConstants.URL;
535 return ScilabLexerConstants.MAIL;
539 return ScilabLexerConstants.LATEX;
543 return ScilabLexerConstants.WHITE_COMMENT;
547 return ScilabLexerConstants.TAB_COMMENT;
552 return ScilabLexerConstants.COMMENT;
557 return ScilabLexerConstants.COMMENT;
563 return ScilabLexerConstants.AUTHORS;
567 return ScilabLexerConstants.URL;
571 return ScilabLexerConstants.MAIL;
575 return ScilabLexerConstants.LATEX;
579 return ScilabLexerConstants.WHITE_COMMENT;
583 return ScilabLexerConstants.TAB_COMMENT;
588 return ScilabLexerConstants.COMMENT;
595 return ScilabLexerConstants.SPECIAL;
599 return ScilabLexerConstants.WHITE;
603 return ScilabLexerConstants.TAB;
607 transposable = false;
614 return ScilabLexerConstants.DEFAULT;
619 return ScilabLexerConstants.EOF;