source: other-projects/rsyntax-textarea/src/java/org/fife/ui/rsyntaxtextarea/modes/ActionScriptTokenMaker.flex@ 25584

Last change on this file since 25584 was 25584, checked in by davidb, 12 years ago

Initial cut an a text edit area for GLI that supports color syntax highlighting

File size: 14.0 KB
Line 
1/*
2 * 04/27/2010
3 *
4 * ActionScriptTokenMaker.java - Scanner for ActionScript.
5 *
6 * This library is distributed under a modified BSD license. See the included
7 * RSyntaxTextArea.License.txt file for details.
8 */
9package org.fife.ui.rsyntaxtextarea.modes;
10
11import java.io.*;
12import javax.swing.text.Segment;
13
14import org.fife.ui.rsyntaxtextarea.*;
15
16
17/**
18 * Scanner for the ActionScript.<p>
19 *
20 * This implementation was created using
21 * <a href="http://www.jflex.de/">JFlex</a> 1.4.1; however, the generated file
22 * was modified for performance. Memory allocation needs to be almost
23 * completely removed to be competitive with the handwritten lexers (subclasses
24 * of <code>AbstractTokenMaker</code>, so this class has been modified so that
25 * Strings are never allocated (via yytext()), and the scanner never has to
26 * worry about refilling its buffer (needlessly copying chars around).
27 * We can achieve this because RText always scans exactly 1 line of tokens at a
28 * time, and hands the scanner this line as an array of characters (a Segment
29 * really). Since tokens contain pointers to char arrays instead of Strings
30 * holding their contents, there is no need for allocating new memory for
31 * Strings.<p>
32 *
33 * The actual algorithm generated for scanning has, of course, not been
34 * modified.<p>
35 *
36 * If you wish to regenerate this file yourself, keep in mind the following:
37 * <ul>
38 * <li>The generated ActionScriptTokenMaker.java</code> file will contain two
39 * definitions of both <code>zzRefill</code> and <code>yyreset</code>.
40 * You should hand-delete the second of each definition (the ones
41 * generated by the lexer), as these generated methods modify the input
42 * buffer, which we'll never have to do.</li>
43 * <li>You should also change the declaration/definition of zzBuffer to NOT
44 * be initialized. This is a needless memory allocation for us since we
45 * will be pointing the array somewhere else anyway.</li>
46 * <li>You should NOT call <code>yylex()</code> on the generated scanner
47 * directly; rather, you should use <code>getTokenList</code> as you would
48 * with any other <code>TokenMaker</code> instance.</li>
49 * </ul>
50 *
51 * @author Robert Futrell
52 * @version 0.5
53 *
54 */
55%%
56
57%public
58%class ActionScriptTokenMaker
59%extends AbstractJFlexCTokenMaker
60%unicode
61%type org.fife.ui.rsyntaxtextarea.Token
62
63
64%{
65
66
67 /**
68 * Constructor. This must be here because JFlex does not generate a
69 * no-parameter constructor.
70 */
71 public ActionScriptTokenMaker() {
72 }
73
74
75 /**
76 * Adds the token specified to the current linked list of tokens.
77 *
78 * @param tokenType The token's type.
79 * @see #addToken(int, int, int)
80 */
81 private void addHyperlinkToken(int start, int end, int tokenType) {
82 int so = start + offsetShift;
83 addToken(zzBuffer, start,end, tokenType, so, true);
84 }
85
86
87 /**
88 * Adds the token specified to the current linked list of tokens.
89 *
90 * @param tokenType The token's type.
91 */
92 private void addToken(int tokenType) {
93 addToken(zzStartRead, zzMarkedPos-1, tokenType);
94 }
95
96
97 /**
98 * Adds the token specified to the current linked list of tokens.
99 *
100 * @param tokenType The token's type.
101 * @see #addHyperlinkToken(int, int, int)
102 */
103 private void addToken(int start, int end, int tokenType) {
104 int so = start + offsetShift;
105 addToken(zzBuffer, start,end, tokenType, so, false);
106 }
107
108
109 /**
110 * Adds the token specified to the current linked list of tokens.
111 *
112 * @param array The character array.
113 * @param start The starting offset in the array.
114 * @param end The ending offset in the array.
115 * @param tokenType The token's type.
116 * @param startOffset The offset in the document at which this token
117 * occurs.
118 * @param hyperlink Whether this token is a hyperlink.
119 */
120 public void addToken(char[] array, int start, int end, int tokenType,
121 int startOffset, boolean hyperlink) {
122 super.addToken(array, start,end, tokenType, startOffset, hyperlink);
123 zzStartRead = zzMarkedPos;
124 }
125
126
127 /**
128 * Returns the text to place at the beginning and end of a
129 * line to "comment" it in a this programming language.
130 *
131 * @return The start and end strings to add to a line to "comment"
132 * it out.
133 */
134 public String[] getLineCommentStartAndEnd() {
135 return new String[] { "//", null };
136 }
137
138
139 /**
140 * Returns the first token in the linked list of tokens generated
141 * from <code>text</code>. This method must be implemented by
142 * subclasses so they can correctly implement syntax highlighting.
143 *
144 * @param text The text from which to get tokens.
145 * @param initialTokenType The token type we should start with.
146 * @param startOffset The offset into the document at which
147 * <code>text</code> starts.
148 * @return The first <code>Token</code> in a linked list representing
149 * the syntax highlighted text.
150 */
151 public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
152
153 resetTokenList();
154 this.offsetShift = -text.offset + startOffset;
155
156 // Start off in the proper state.
157 int state = Token.NULL;
158 switch (initialTokenType) {
159 case Token.COMMENT_MULTILINE:
160 state = MLC;
161 start = text.offset;
162 break;
163 default:
164 state = Token.NULL;
165 }
166
167 s = text;
168 try {
169 yyreset(zzReader);
170 yybegin(state);
171 return yylex();
172 } catch (IOException ioe) {
173 ioe.printStackTrace();
174 return new DefaultToken();
175 }
176
177 }
178
179
180 /**
181 * Refills the input buffer.
182 *
183 * @return <code>true</code> if EOF was reached, otherwise
184 * <code>false</code>.
185 */
186 private boolean zzRefill() {
187 return zzCurrentPos>=s.offset+s.count;
188 }
189
190
191 /**
192 * Resets the scanner to read from a new input stream.
193 * Does not close the old reader.
194 *
195 * All internal variables are reset, the old input stream
196 * <b>cannot</b> be reused (internal buffer is discarded and lost).
197 * Lexical state is set to <tt>YY_INITIAL</tt>.
198 *
199 * @param reader the new input stream
200 */
201 public final void yyreset(java.io.Reader reader) {
202 // 's' has been updated.
203 zzBuffer = s.array;
204 /*
205 * We replaced the line below with the two below it because zzRefill
206 * no longer "refills" the buffer (since the way we do it, it's always
207 * "full" the first time through, since it points to the segment's
208 * array). So, we assign zzEndRead here.
209 */
210 //zzStartRead = zzEndRead = s.offset;
211 zzStartRead = s.offset;
212 zzEndRead = zzStartRead + s.count - 1;
213 zzCurrentPos = zzMarkedPos = zzPushbackPos = s.offset;
214 zzLexicalState = YYINITIAL;
215 zzReader = reader;
216 zzAtBOL = true;
217 zzAtEOF = false;
218 }
219
220
221%}
222
223Letter = [A-Za-z]
224LetterOrUnderscore = ({Letter}|"_")
225NonzeroDigit = [1-9]
226Digit = ("0"|{NonzeroDigit})
227HexDigit = ({Digit}|[A-Fa-f])
228OctalDigit = ([0-7])
229AnyCharacterButApostropheOrBackSlash = ([^\\'])
230AnyCharacterButDoubleQuoteOrBackSlash = ([^\\\"\n])
231EscapedSourceCharacter = ("u"{HexDigit}{HexDigit}{HexDigit}{HexDigit})
232Escape = ("\\"(([btnfr\"'\\])|([0123]{OctalDigit}?{OctalDigit}?)|({OctalDigit}{OctalDigit}?)|{EscapedSourceCharacter}))
233NonSeparator = ([^\t\f\r\n\ \(\)\{\}\[\]\;\,\.\=\>\<\!\~\?\:\+\-\*\/\&\|\^\%\"\']|"#"|"\\")
234IdentifierStart = ({LetterOrUnderscore}|"$")
235IdentifierPart = ({IdentifierStart}|{Digit}|("\\"{EscapedSourceCharacter}))
236
237LineTerminator = (\n)
238WhiteSpace = ([ \t\f])
239
240CharLiteral = ([\']({AnyCharacterButApostropheOrBackSlash}|{Escape})[\'])
241UnclosedCharLiteral = ([\'][^\'\n]*)
242ErrorCharLiteral = ({UnclosedCharLiteral}[\'])
243StringLiteral = ([\"]({AnyCharacterButDoubleQuoteOrBackSlash}|{Escape})*[\"])
244UnclosedStringLiteral = ([\"]([\\].|[^\\\"])*[^\"]?)
245ErrorStringLiteral = ({UnclosedStringLiteral}[\"])
246
247MLCBegin = ("/*")
248MLCEnd = ("*/")
249LineCommentBegin = ("//")
250IntegerHelper1 = (({NonzeroDigit}{Digit}*)|"0")
251IntegerHelper2 = ("0"(([xX]{HexDigit}+)|({OctalDigit}*)))
252IntegerLiteral = ({IntegerHelper1}[lL]?)
253HexLiteral = ({IntegerHelper2}[lL]?)
254FloatHelper1 = ([fFdD]?)
255FloatHelper2 = ([eE][+-]?{Digit}+{FloatHelper1})
256FloatLiteral1 = ({Digit}+"."({FloatHelper1}|{FloatHelper2}|{Digit}+({FloatHelper1}|{FloatHelper2})))
257FloatLiteral2 = ("."{Digit}+({FloatHelper1}|{FloatHelper2}))
258FloatLiteral3 = ({Digit}+{FloatHelper2})
259FloatLiteral = ({FloatLiteral1}|{FloatLiteral2}|{FloatLiteral3}|({Digit}+[fFdD]))
260ErrorNumberFormat = (({IntegerLiteral}|{HexLiteral}|{FloatLiteral}){NonSeparator}+)
261BooleanLiteral = ("true"|"false")
262
263Separator = ([\(\)\{\}\[\]])
264Separator2 = ([\;,.])
265
266NonAssignmentOperator = ("+"|"-"|"<="|"^"|"++"|"<"|"*"|">="|"%"|"--"|">"|"/"|"!="|"?"|">>"|"!"|"&"|"=="|":"|">>"|"~"|"|"|"&&"|">>>")
267AssignmentOperator = ("="|"-="|"*="|"/="|"|="|"&="|"^="|"+="|"%="|"<<="|">>="|">>>=")
268Operator = ({NonAssignmentOperator}|{AssignmentOperator})
269
270Identifier = ({IdentifierStart}{IdentifierPart}*)
271ErrorIdentifier = ({NonSeparator}+)
272
273URLGenDelim = ([:\/\?#\[\]@])
274URLSubDelim = ([\!\$&'\(\)\*\+,;=])
275URLUnreserved = ({LetterOrUnderscore}|{Digit}|[\-\.\~])
276URLCharacter = ({URLGenDelim}|{URLSubDelim}|{URLUnreserved}|[%])
277URLCharacters = ({URLCharacter}*)
278URLEndCharacter = ([\/\$]|{Letter}|{Digit})
279URL = (((https?|f(tp|ile))"://"|"www.")({URLCharacters}{URLEndCharacter})?)
280
281
282%state MLC
283%state EOL_COMMENT
284
285%%
286
287<YYINITIAL> {
288
289 /* Keywords */
290 "add" |
291 "and" |
292 "break" |
293 "case" |
294 "catch" |
295 "class" |
296 "const" |
297 "continue" |
298 "default" |
299 "delete" |
300 "do" |
301 "dynamic" |
302 "else" |
303 "eq" |
304 "extends" |
305 "final" |
306 "finally" |
307 "for" |
308 "for each" |
309 "function" |
310 "ge" |
311 "get" |
312 "gt" |
313 "if" |
314 "ifFrameLoaded" |
315 "implements" |
316 "import" |
317 "in" |
318 "include" |
319 "interface" |
320 "internal" |
321 "label" |
322 "le" |
323 "lt" |
324 "namespace" |
325 "native" |
326 "ne" |
327 "new" |
328 "not" |
329 "on" |
330 "onClipEvent" |
331 "or" |
332 "override" |
333 "package" |
334 "private" |
335 "protected" |
336 "public" |
337 "return" |
338 "set" |
339 "static" |
340 "super" |
341 "switch" |
342 "tellTarget" |
343 "this" |
344 "throw" |
345 "try" |
346 "typeof" |
347 "use" |
348 "var" |
349 "void" |
350 "while" |
351 "with" |
352
353 "null" |
354 "undefined" { addToken(Token.RESERVED_WORD); }
355
356 /* Built-in objects (good idea not to use these names!) */
357 "Array" |
358 "Boolean" |
359 "Color" |
360 "Date" |
361 "Function" |
362 "int" |
363 "Key" |
364 "MovieClip" |
365 "Math" |
366 "Mouse" |
367 "Null" |
368 "Number" |
369 "Object" |
370 "Selection" |
371 "Sound" |
372 "String" |
373 "uint" |
374 "Vector" |
375 "void" |
376 "XML" |
377 "XMLNode" |
378 "XMLSocket" { addToken(Token.DATA_TYPE); }
379
380 /* Global functions */
381 "call" |
382 "escape" |
383 "eval" |
384 "fscommand" |
385 "getProperty" |
386 "getTimer" |
387 "getURL" |
388 "getVersion" |
389 "gotoAndPlay" |
390 "gotoAndStop" |
391 "#include" |
392 "int" |
393 "isFinite" |
394 "isNaN" |
395 "loadMovie" |
396 "loadMovieNum" |
397 "loadVariables" |
398 "loadVariablesNum" |
399 "maxscroll" |
400 "newline" |
401 "nextFrame" |
402 "nextScene" |
403 "Number" |
404 "parseFloat" |
405 "parseInt" |
406 "play" |
407 "prevFrame" |
408 "prevScene" |
409 "print" |
410 "printAsBitmap" |
411 "printAsBitmapNum" |
412 "printNum" |
413 "random" |
414 "removeMovieClip" |
415 "scroll" |
416 "setProperty" |
417 "startDrag" |
418 "stop" |
419 "stopAllSounds" |
420 "stopDrag" |
421 "String" |
422 "targetPath" |
423 "tellTarget" |
424 "toggleHighQuality" |
425 "trace" |
426 "unescape" |
427 "unloadMovie" |
428 "unloadMovieNum" |
429 "updateAfterEvent" { addToken(Token.FUNCTION); }
430
431 /* Booleans. */
432 {BooleanLiteral} { addToken(Token.LITERAL_BOOLEAN); }
433
434 {LineTerminator} { addNullToken(); return firstToken; }
435
436 {Identifier} { addToken(Token.IDENTIFIER); }
437
438 {WhiteSpace}+ { addToken(Token.WHITESPACE); }
439
440 /* String/Character literals. */
441 {CharLiteral} { addToken(Token.LITERAL_CHAR); }
442 {UnclosedCharLiteral} { addToken(Token.ERROR_CHAR); addNullToken(); return firstToken; }
443 {ErrorCharLiteral} { addToken(Token.ERROR_CHAR); }
444 {StringLiteral} { addToken(Token.LITERAL_STRING_DOUBLE_QUOTE); }
445 {UnclosedStringLiteral} { addToken(Token.ERROR_STRING_DOUBLE); addNullToken(); return firstToken; }
446 {ErrorStringLiteral} { addToken(Token.ERROR_STRING_DOUBLE); }
447
448 /* Comment literals. */
449 "/**/" { addToken(Token.COMMENT_MULTILINE); }
450 {MLCBegin} { start = zzMarkedPos-2; yybegin(MLC); }
451 {LineCommentBegin} { start = zzMarkedPos-2; yybegin(EOL_COMMENT); }
452
453 /* Separators. */
454 {Separator} { addToken(Token.SEPARATOR); }
455 {Separator2} { addToken(Token.IDENTIFIER); }
456
457 /* Operators. */
458 {Operator} { addToken(Token.OPERATOR); }
459
460 /* Numbers */
461 {IntegerLiteral} { addToken(Token.LITERAL_NUMBER_DECIMAL_INT); }
462 {HexLiteral} { addToken(Token.LITERAL_NUMBER_HEXADECIMAL); }
463 {FloatLiteral} { addToken(Token.LITERAL_NUMBER_FLOAT); }
464 {ErrorNumberFormat} { addToken(Token.ERROR_NUMBER_FORMAT); }
465
466 {ErrorIdentifier} { addToken(Token.ERROR_IDENTIFIER); }
467
468 /* Ended with a line not in a string or comment. */
469 <<EOF>> { addNullToken(); return firstToken; }
470
471 /* Catch any other (unhandled) characters and flag them as bad. */
472 . { addToken(Token.ERROR_IDENTIFIER); }
473
474}
475
476
477<MLC> {
478
479 [^hwf\n\*]+ {}
480 {URL} { int temp=zzStartRead; addToken(start,zzStartRead-1, Token.COMMENT_MULTILINE); addHyperlinkToken(temp,zzMarkedPos-1, Token.COMMENT_MULTILINE); start = zzMarkedPos; }
481 [hwf] {}
482
483 \n { addToken(start,zzStartRead-1, Token.COMMENT_MULTILINE); return firstToken; }
484 {MLCEnd} { yybegin(YYINITIAL); addToken(start,zzStartRead+1, Token.COMMENT_MULTILINE); }
485 \* {}
486 <<EOF>> { addToken(start,zzStartRead-1, Token.COMMENT_MULTILINE); return firstToken; }
487
488}
489
490
491<EOL_COMMENT> {
492 [^hwf\n]+ {}
493 {URL} { int temp=zzStartRead; addToken(start,zzStartRead-1, Token.COMMENT_EOL); addHyperlinkToken(temp,zzMarkedPos-1, Token.COMMENT_EOL); start = zzMarkedPos; }
494 [hwf] {}
495 \n { addToken(start,zzStartRead-1, Token.COMMENT_EOL); addNullToken(); return firstToken; }
496 <<EOF>> { addToken(start,zzStartRead-1, Token.COMMENT_EOL); addNullToken(); return firstToken; }
497
498}
Note: See TracBrowser for help on using the repository browser.