3 package org.scilab.modules.scinotes;
5 import java.util.Arrays;
6 import java.util.HashSet;
7 import java.util.Iterator;
8 import java.util.ArrayList;
11 import java.io.IOException;
12 import javax.swing.text.BadLocationException;
13 import javax.swing.text.Element;
15 import org.scilab.modules.commons.ScilabCommonsUtils;
17 @javax.annotation.Generated("JFlex")
18 @SuppressWarnings("fallthrough")
24 %extends ScilabLexerConstants
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;
40 private ScilabDocument doc;
41 private boolean transposable;
43 private boolean breakstring;
45 public ScilabLexer(ScilabDocument doc) {
49 public ScilabLexer(ScilabDocument doc, boolean update) {
51 this.elem = doc.getDefaultRootElement();
52 this.infile = doc.getFunctionsInDoc();
58 public static void update() {
59 if (ScilabCommonsUtils.isScilabThread()) {
60 String[] vars = ScilabKeywords.GetVariablesName();
61 String[] funs = ScilabKeywords.GetFunctionsName();
62 String[] macs = ScilabKeywords.GetMacrosName();
67 variables.addAll(Arrays.asList(vars));
70 commands.addAll(Arrays.asList(funs));
73 macros.addAll(Arrays.asList(macs));
78 public void setRange(int p0, int p1) {
83 yyreset(new ScilabDocumentReader(doc, p0, p1));
84 int currentLine = elem.getElementIndex(start);
85 if (currentLine != 0 && ((ScilabDocument.ScilabLeafElement) elem.getElement(currentLine - 1)).isBrokenString()) {
94 public int scan() throws IOException {
96 if (start + yychar + yylength() == end - 1) {
97 ((ScilabDocument.ScilabLeafElement) elem.getElement(elem.getElementIndex(start))).setBrokenString(breakstring);
103 public int getKeyword(int pos, boolean strict) {
104 Element line = elem.getElement(elem.getElementIndex(pos));
105 int end = line.getEndOffset();
107 start = line.getStartOffset();
112 yyreset(new ScilabDocumentReader(doc, start, end));
117 while (startL < pos && (s != startL || yystate() == BREAKSTRING)) {
120 startL = start + yychar + yylength();
124 } catch (IOException e) {
125 return ScilabLexerConstants.DEFAULT;
129 public static ScilabTokens getScilabTokens(String str) {
130 ScilabDocument doc = new ScilabDocument(false);
132 doc.insertString(0, str, null);
133 } catch (BadLocationException e) { }
134 return getScilabTokens(doc);
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();
143 while (tok != ScilabLexerConstants.EOF) {
145 tokens.add(tok, lexer.yychar + lexer.yylength());
147 } catch (IOException e) { }
152 public static class ScilabTokens {
153 private List<Integer> tokenType = new ArrayList<Integer>();
154 private List<Integer> tokenPos = new ArrayList<Integer>();
158 void add(final int type, final int pos) {
163 public final List<Integer> getTokenType() {
167 public final List<Integer> getTokenPos() {
173 /* main character classes */
176 open = "[" | "(" | "{"
177 close = "]" | ")" | "}"
185 cstes = "%t" | "%T" | "%f" | "%F" | "%e" | "%pi" | "%inf" | "%i" | "%z" | "%s" | "%nan" | "%eps" | "SCI" | "WSCI" | "SCIHOME" | "TMPDIR"
187 operator = ".'" | ".*" | "./" | ".\\" | ".^" | ".**" | "+" | "-" | "/" | "\\" | "*" | "^" | "**" | "==" | "~=" | "<>" | "<" | ">" | "<=" | ">=" | ".*." | "./." | ".\\." | "/." | "=" | "&" | "|" | "@" | "@=" | "~"
189 functionKwds = "function" | "endfunction"
191 structureKwds = "then" | "do" | "catch" | "case"
193 elseif = "elseif" | "else"
195 openCloseStructureKwds = "if" | "for" | "while" | "try" | "select" | "end"
197 controlKwds = "abort" | "break" | "quit" | "return" | "resume" | "pause" | "continue" | "exit"
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"
201 error = "Scilab Entreprises" | "Scilab Entreprise" | "Scilab Enterprise"
202 todo = ("TODO" | "todo" | "Todo")[ \t:]+[^\n]*
205 breakinstring = {break}[ \t]*({comment} | {eol})
207 special = "$" | ":" | {break}
209 string = (([^ \t\'\"\r\n\.]*)|([\'\"]{2}))+
211 id = ([a-zA-Z%_#!?][a-zA-Z0-9_#!$?]*)|("$"[a-zA-Z0-9_#!$?]+)
213 badid = ([0-9$][a-zA-Z0-9_#!$?]+)
214 whitabs = (" "+"\t" | "\t"+" ")[ \t]*
215 badop = [+-]([\*\/\\\^] | "."[\*\+\-\/\\\^]) | ":=" | "->" | " !=" | "&&" | "||" | ([*+-/\\\^]"=")
219 url = "http://"[^ \t\f\n\r\'\"]+
220 mail = "<"[ \t]*[a-zA-Z0-9_\.\-]+"@"([a-zA-Z0-9\-]+".")+[a-zA-Z]{2,5}[ \t]*">"
222 latex = "$"(([^$]*|"\\$")+)"$"
223 latexinstring = (\"|\')"$"(([^$\'\"]*|"\\$"|([\'\"]{2}))+)"$"(\"|\')
226 exp = [dDeE][+-]?{digit}*
227 number = ({digit}+"."?{digit}*{exp}?)|("."{digit}+{exp}?)
229 %x QSTRING, COMMENT, FIELD, COMMANDS, COMMANDSWHITE, BREAKSTRING
235 transposable = false;
241 transposable = false;
242 return ScilabLexerConstants.OPERATOR;
246 transposable = false;
247 return ScilabLexerConstants.FKEYWORD;
250 {openCloseStructureKwds} {
251 transposable = false;
252 return ScilabLexerConstants.OSKEYWORD;
256 transposable = false;
257 return ScilabLexerConstants.SKEYWORD;
261 transposable = false;
262 return ScilabLexerConstants.ELSEIF;
266 transposable = false;
267 return ScilabLexerConstants.CKEYWORD;
272 return ScilabLexerConstants.CONSTANTES;
277 String str = yytext();
278 if (commands.contains(str)) {
280 return ScilabLexerConstants.COMMANDS;
281 } else if (macros.contains(str)) {
283 return ScilabLexerConstants.MACROS;
284 } else if (infile.contains(str)) {
286 return ScilabLexerConstants.MACROINFILE;
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;
295 return ScilabLexerConstants.ID;
300 return ScilabLexerConstants.NUMBER;
304 transposable = false;
305 return ScilabLexerConstants.SPECIAL;
309 transposable = false;
311 return ScilabLexerConstants.OPERATOR;
315 return ScilabLexerConstants.LATEXINSTRING;
320 return ScilabLexerConstants.TRANSP;
322 beginString = zzStartRead;
324 return ScilabLexerConstants.STRING;
329 transposable = false;
330 return ScilabLexerConstants.OPENCLOSE;
335 return ScilabLexerConstants.OPENCLOSE;
339 transposable = false;
340 beginString = zzStartRead;
342 return ScilabLexerConstants.STRING;
348 return ScilabLexerConstants.ERROR;
352 transposable = false;
353 return ScilabLexerConstants.WHITE;
357 transposable = false;
358 return ScilabLexerConstants.TAB;
363 transposable = false;
364 return ScilabLexerConstants.DEFAULT;
370 yypushback(yylength());
375 yybegin(COMMANDSWHITE);
376 return ScilabLexerConstants.WHITE;
380 yybegin(COMMANDSWHITE);
381 return ScilabLexerConstants.TAB;
394 transposable = false;
399 ([^ \t,;/]*) | ("/"[^ /]*) {
400 return ScilabLexerConstants.STRING;
404 return ScilabLexerConstants.WHITE;
408 return ScilabLexerConstants.TAB;
421 return ScilabLexerConstants.FIELD;
433 yypushback(yylength());
434 yybegin(BREAKSTRING);
435 transposable = false;
436 return ScilabLexerConstants.STRING;
440 return ScilabLexerConstants.WHITE_STRING;
444 return ScilabLexerConstants.TAB_STRING;
449 return ScilabLexerConstants.STRING;
453 transposable = false;
455 return ScilabLexerConstants.STRING;
460 return ScilabLexerConstants.DEFAULT;
466 return ScilabLexerConstants.TODO;
470 return ScilabLexerConstants.ERROR;
475 return ScilabLexerConstants.AUTHORS;
479 return ScilabLexerConstants.URL;
483 return ScilabLexerConstants.MAIL;
487 return ScilabLexerConstants.LATEX;
491 return ScilabLexerConstants.WHITE_COMMENT;
495 return ScilabLexerConstants.TAB_COMMENT;
499 return ScilabLexerConstants.COMMENT;
504 return ScilabLexerConstants.DEFAULT;
511 return ScilabLexerConstants.SPECIAL;
515 return ScilabLexerConstants.WHITE;
519 return ScilabLexerConstants.TAB;
523 transposable = false;
530 return ScilabLexerConstants.DEFAULT;
535 return ScilabLexerConstants.EOF;