source: other-projects/gli-rsyntax-textarea/src/java/org/fife/ui/rsyntaxtextarea/modes/PropertiesFileTokenMaker.java@ 25801

Last change on this file since 25801 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: 19.1 KB
Line 
1/* The following code was generated by JFlex 1.4.1 on 1/28/11 4:16 PM */
2
3/*
4 * 03/21/2005
5 *
6 * PropertiesFileTokenMaker.java - Scanner for properties files.
7 *
8 * This library is distributed under a modified BSD license. See the included
9 * RSyntaxTextArea.License.txt file for details.
10 */
11package org.fife.ui.rsyntaxtextarea.modes;
12
13import java.io.*;
14import javax.swing.text.Segment;
15
16import org.fife.ui.rsyntaxtextarea.*;
17
18
19/**
20 * This class splits up text into tokens representing a Java properties file.<p>
21 *
22 * This implementation was created using
23 * <a href="http://www.jflex.de/">JFlex</a> 1.4.1; however, the generated file
24 * was modified for performance. Memory allocation needs to be almost
25 * completely removed to be competitive with the handwritten lexers (subclasses
26 * of <code>AbstractTokenMaker</code>, so this class has been modified so that
27 * Strings are never allocated (via yytext()), and the scanner never has to
28 * worry about refilling its buffer (needlessly copying chars around).
29 * We can achieve this because RText always scans exactly 1 line of tokens at a
30 * time, and hands the scanner this line as an array of characters (a Segment
31 * really). Since tokens contain pointers to char arrays instead of Strings
32 * holding their contents, there is no need for allocating new memory for
33 * Strings.<p>
34 *
35 * The actual algorithm generated for scanning has, of course, not been
36 * modified.<p>
37 *
38 * If you wish to regenerate this file yourself, keep in mind the following:
39 * <ul>
40 * <li>The generated <code>PropertiesFileTokenMaker.java</code> file will
41 * contain two definitions of both <code>zzRefill</code> and
42 * <code>yyreset</code>. You should hand-delete the second of each
43 * definition (the ones generated by the lexer), as these generated
44 * methods modify the input buffer, which we'll never have to do.
45 * <li>You should also change the declaration/definition of zzBuffer to NOT
46 * be initialized. This is a needless memory allocation for us since we
47 * will be pointing the array somewhere else anyway.
48 * <li>You should NOT call <code>yylex()</code> on the generated scanner
49 * directly; rather, you should use <code>getTokenList</code> as you would
50 * with any other <code>TokenMaker</code> instance.
51 * </ul>
52 *
53 * @author Robert Futrell
54 * @version 0.4
55 *
56 */
57
58public class PropertiesFileTokenMaker extends AbstractJFlexTokenMaker implements TokenMaker {
59
60 /** This character denotes the end of file */
61 public static final int YYEOF = -1;
62
63 /** initial size of the lookahead buffer */
64 private static final int ZZ_BUFFERSIZE = 16384;
65
66 /** lexical states */
67 public static final int YYINITIAL = 0;
68 public static final int VALUE = 1;
69
70 /**
71 * Translates characters to character classes
72 */
73 private static final String ZZ_CMAP_PACKED =
74 "\11\0\1\3\1\2\25\0\1\3\1\4\1\0\1\4\3\0\1\5"+
75 "\22\0\1\1\2\0\1\1\36\0\1\6\36\0\1\7\1\0\1\10"+
76 "\uff82\0";
77
78 /**
79 * Translates characters to character classes
80 */
81 private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED);
82
83 /**
84 * Translates DFA states to action switch labels.
85 */
86 private static final int [] ZZ_ACTION = zzUnpackAction();
87
88 private static final String ZZ_ACTION_PACKED_0 =
89 "\1\1\1\0\1\1\1\2\1\3\1\4\1\5\1\6"+
90 "\1\7\1\10\1\6\1\5\1\10";
91
92 private static int [] zzUnpackAction() {
93 int [] result = new int[13];
94 int offset = 0;
95 offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result);
96 return result;
97 }
98
99 private static int zzUnpackAction(String packed, int offset, int [] result) {
100 int i = 0; /* index in packed string */
101 int j = offset; /* index in unpacked array */
102 int l = packed.length();
103 while (i < l) {
104 int count = packed.charAt(i++);
105 int value = packed.charAt(i++);
106 do result[j++] = value; while (--count > 0);
107 }
108 return j;
109 }
110
111
112 /**
113 * Translates a state to a row index in the transition table
114 */
115 private static final int [] ZZ_ROWMAP = zzUnpackRowMap();
116
117 private static final String ZZ_ROWMAP_PACKED_0 =
118 "\0\0\0\11\0\22\0\33\0\44\0\55\0\66\0\77"+
119 "\0\110\0\121\0\33\0\33\0\33";
120
121 private static int [] zzUnpackRowMap() {
122 int [] result = new int[13];
123 int offset = 0;
124 offset = zzUnpackRowMap(ZZ_ROWMAP_PACKED_0, offset, result);
125 return result;
126 }
127
128 private static int zzUnpackRowMap(String packed, int offset, int [] result) {
129 int i = 0; /* index in packed string */
130 int j = offset; /* index in unpacked array */
131 int l = packed.length();
132 while (i < l) {
133 int high = packed.charAt(i++) << 16;
134 result[j++] = high | packed.charAt(i++);
135 }
136 return j;
137 }
138
139 /**
140 * The transition table of the DFA
141 */
142 private static final int [] ZZ_TRANS = zzUnpackTrans();
143
144 private static final String ZZ_TRANS_PACKED_0 =
145 "\1\3\1\4\1\0\1\5\1\6\4\3\5\7\1\10"+
146 "\1\11\1\12\1\7\1\3\4\0\4\3\14\0\1\5"+
147 "\5\0\2\6\1\0\6\6\5\7\3\0\1\7\5\10"+
148 "\1\13\3\10\2\14\1\0\6\14\10\12\1\15";
149
150 private static int [] zzUnpackTrans() {
151 int [] result = new int[90];
152 int offset = 0;
153 offset = zzUnpackTrans(ZZ_TRANS_PACKED_0, offset, result);
154 return result;
155 }
156
157 private static int zzUnpackTrans(String packed, int offset, int [] result) {
158 int i = 0; /* index in packed string */
159 int j = offset; /* index in unpacked array */
160 int l = packed.length();
161 while (i < l) {
162 int count = packed.charAt(i++);
163 int value = packed.charAt(i++);
164 value--;
165 do result[j++] = value; while (--count > 0);
166 }
167 return j;
168 }
169
170
171 /* error codes */
172 private static final int ZZ_UNKNOWN_ERROR = 0;
173 private static final int ZZ_NO_MATCH = 1;
174 private static final int ZZ_PUSHBACK_2BIG = 2;
175
176 /* error messages for the codes above */
177 private static final String ZZ_ERROR_MSG[] = {
178 "Unkown internal scanner error",
179 "Error: could not match input",
180 "Error: pushback value was too large"
181 };
182
183 /**
184 * ZZ_ATTRIBUTE[aState] contains the attributes of state <code>aState</code>
185 */
186 private static final int [] ZZ_ATTRIBUTE = zzUnpackAttribute();
187
188 private static final String ZZ_ATTRIBUTE_PACKED_0 =
189 "\1\1\1\0\1\1\1\11\6\1\3\11";
190
191 private static int [] zzUnpackAttribute() {
192 int [] result = new int[13];
193 int offset = 0;
194 offset = zzUnpackAttribute(ZZ_ATTRIBUTE_PACKED_0, offset, result);
195 return result;
196 }
197
198 private static int zzUnpackAttribute(String packed, int offset, int [] result) {
199 int i = 0; /* index in packed string */
200 int j = offset; /* index in unpacked array */
201 int l = packed.length();
202 while (i < l) {
203 int count = packed.charAt(i++);
204 int value = packed.charAt(i++);
205 do result[j++] = value; while (--count > 0);
206 }
207 return j;
208 }
209
210 /** the input device */
211 private java.io.Reader zzReader;
212
213 /** the current state of the DFA */
214 private int zzState;
215
216 /** the current lexical state */
217 private int zzLexicalState = YYINITIAL;
218
219 /** this buffer contains the current text to be matched and is
220 the source of the yytext() string */
221 private char zzBuffer[] = new char[ZZ_BUFFERSIZE];
222
223 /** the textposition at the last accepting state */
224 private int zzMarkedPos;
225
226 /** the current text position in the buffer */
227 private int zzCurrentPos;
228
229 /** startRead marks the beginning of the yytext() string in the buffer */
230 private int zzStartRead;
231
232 /** endRead marks the last character in the buffer, that has been read
233 from input */
234 private int zzEndRead;
235
236 /** zzAtEOF == true <=> the scanner is at the EOF */
237 private boolean zzAtEOF;
238
239 /* user code: */
240
241
242 /**
243 * Constructor. This must be here because JFlex does not generate a
244 * no-parameter constructor.
245 */
246 public PropertiesFileTokenMaker() {
247 super();
248 }
249
250
251 /**
252 * Adds the token specified to the current linked list of tokens.
253 *
254 * @param tokenType The token's type.
255 */
256 private void addToken(int tokenType) {
257 addToken(zzStartRead, zzMarkedPos-1, tokenType);
258 }
259
260
261 /**
262 * Adds the token specified to the current linked list of tokens.
263 *
264 * @param tokenType The token's type.
265 */
266 private void addToken(int start, int end, int tokenType) {
267 int so = start + offsetShift;
268 addToken(zzBuffer, start,end, tokenType, so);
269 }
270
271
272 /**
273 * Adds the token specified to the current linked list of tokens.
274 *
275 * @param array The character array.
276 * @param start The starting offset in the array.
277 * @param end The ending offset in the array.
278 * @param tokenType The token's type.
279 * @param startOffset The offset in the document at which this token
280 * occurs.
281 */
282 public void addToken(char[] array, int start, int end, int tokenType, int startOffset) {
283 super.addToken(array, start,end, tokenType, startOffset);
284 zzStartRead = zzMarkedPos;
285 }
286
287
288 /**
289 * Returns the text to place at the beginning and end of a
290 * line to "comment" it in a this programming language.
291 *
292 * @return The start and end strings to add to a line to "comment"
293 * it out.
294 */
295 public String[] getLineCommentStartAndEnd() {
296 return new String[] { "#", null };
297 }
298
299
300 /**
301 * Returns the first token in the linked list of tokens generated
302 * from <code>text</code>. This method must be implemented by
303 * subclasses so they can correctly implement syntax highlighting.
304 *
305 * @param text The text from which to get tokens.
306 * @param initialTokenType The token type we should start with.
307 * @param startOffset The offset into the document at which
308 * <code>text</code> starts.
309 * @return The first <code>Token</code> in a linked list representing
310 * the syntax highlighted text.
311 */
312 public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
313
314 resetTokenList();
315 this.offsetShift = -text.offset + startOffset;
316
317 // Start off in the proper state.
318 int state = Token.NULL;
319 switch (initialTokenType) {
320 case Token.LITERAL_STRING_DOUBLE_QUOTE:
321 state = VALUE;
322 start = text.offset;
323 break;
324 default:
325 state = Token.NULL;
326 }
327
328 s = text;
329 try {
330 yyreset(zzReader);
331 yybegin(state);
332 return yylex();
333 } catch (IOException ioe) {
334 ioe.printStackTrace();
335 return new DefaultToken();
336 }
337
338 }
339
340
341 /**
342 * Refills the input buffer.
343 *
344 * @return <code>true</code> if EOF was reached, otherwise
345 * <code>false</code>.
346 * @exception IOException if any I/O-Error occurs.
347 */
348 private boolean zzRefill() {
349 return zzCurrentPos>=s.offset+s.count;
350 }
351
352
353 /**
354 * Resets the scanner to read from a new input stream.
355 * Does not close the old reader.
356 *
357 * All internal variables are reset, the old input stream
358 * <b>cannot</b> be reused (internal buffer is discarded and lost).
359 * Lexical state is set to <tt>YY_INITIAL</tt>.
360 *
361 * @param reader the new input stream
362 */
363 public final void yyreset(java.io.Reader reader) {
364 // 's' has been updated.
365 zzBuffer = s.array;
366 /*
367 * We replaced the line below with the two below it because zzRefill
368 * no longer "refills" the buffer (since the way we do it, it's always
369 * "full" the first time through, since it points to the segment's
370 * array). So, we assign zzEndRead here.
371 */
372 //zzStartRead = zzEndRead = s.offset;
373 zzStartRead = s.offset;
374 zzEndRead = zzStartRead + s.count - 1;
375 zzCurrentPos = zzMarkedPos = s.offset;
376 zzLexicalState = YYINITIAL;
377 zzReader = reader;
378 zzAtEOF = false;
379 }
380
381
382
383
384 /**
385 * Creates a new scanner
386 * There is also a java.io.InputStream version of this constructor.
387 *
388 * @param in the java.io.Reader to read input from.
389 */
390 public PropertiesFileTokenMaker(java.io.Reader in) {
391 this.zzReader = in;
392 }
393
394 /**
395 * Creates a new scanner.
396 * There is also java.io.Reader version of this constructor.
397 *
398 * @param in the java.io.Inputstream to read input from.
399 */
400 public PropertiesFileTokenMaker(java.io.InputStream in) {
401 this(new java.io.InputStreamReader(in));
402 }
403
404 /**
405 * Unpacks the compressed character translation table.
406 *
407 * @param packed the packed character translation table
408 * @return the unpacked character translation table
409 */
410 private static char [] zzUnpackCMap(String packed) {
411 char [] map = new char[0x10000];
412 int i = 0; /* index in packed string */
413 int j = 0; /* index in unpacked array */
414 while (i < 42) {
415 int count = packed.charAt(i++);
416 char value = packed.charAt(i++);
417 do map[j++] = value; while (--count > 0);
418 }
419 return map;
420 }
421
422
423 /**
424 * Closes the input stream.
425 */
426 public final void yyclose() throws java.io.IOException {
427 zzAtEOF = true; /* indicate end of file */
428 zzEndRead = zzStartRead; /* invalidate buffer */
429
430 if (zzReader != null)
431 zzReader.close();
432 }
433
434
435 /**
436 * Returns the current lexical state.
437 */
438 public final int yystate() {
439 return zzLexicalState;
440 }
441
442
443 /**
444 * Enters a new lexical state
445 *
446 * @param newState the new lexical state
447 */
448 public final void yybegin(int newState) {
449 zzLexicalState = newState;
450 }
451
452
453 /**
454 * Returns the text matched by the current regular expression.
455 */
456 public final String yytext() {
457 return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead );
458 }
459
460
461 /**
462 * Returns the character at position <tt>pos</tt> from the
463 * matched text.
464 *
465 * It is equivalent to yytext().charAt(pos), but faster
466 *
467 * @param pos the position of the character to fetch.
468 * A value from 0 to yylength()-1.
469 *
470 * @return the character at position pos
471 */
472 public final char yycharat(int pos) {
473 return zzBuffer[zzStartRead+pos];
474 }
475
476
477 /**
478 * Returns the length of the matched text region.
479 */
480 public final int yylength() {
481 return zzMarkedPos-zzStartRead;
482 }
483
484
485 /**
486 * Reports an error that occured while scanning.
487 *
488 * In a wellformed scanner (no or only correct usage of
489 * yypushback(int) and a match-all fallback rule) this method
490 * will only be called with things that "Can't Possibly Happen".
491 * If this method is called, something is seriously wrong
492 * (e.g. a JFlex bug producing a faulty scanner etc.).
493 *
494 * Usual syntax/scanner level error handling should be done
495 * in error fallback rules.
496 *
497 * @param errorCode the code of the errormessage to display
498 */
499 private void zzScanError(int errorCode) {
500 String message;
501 try {
502 message = ZZ_ERROR_MSG[errorCode];
503 }
504 catch (ArrayIndexOutOfBoundsException e) {
505 message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR];
506 }
507
508 throw new Error(message);
509 }
510
511
512 /**
513 * Pushes the specified amount of characters back into the input stream.
514 *
515 * They will be read again by then next call of the scanning method
516 *
517 * @param number the number of characters to be read again.
518 * This number must not be greater than yylength()!
519 */
520 public void yypushback(int number) {
521 if ( number > yylength() )
522 zzScanError(ZZ_PUSHBACK_2BIG);
523
524 zzMarkedPos -= number;
525 }
526
527
528 /**
529 * Resumes scanning until the next regular expression is matched,
530 * the end of input is encountered or an I/O-Error occurs.
531 *
532 * @return the next token
533 * @exception java.io.IOException if any I/O-Error occurs
534 */
535 public org.fife.ui.rsyntaxtextarea.Token yylex() throws java.io.IOException {
536 int zzInput;
537 int zzAction;
538
539 // cached fields:
540 int zzCurrentPosL;
541 int zzMarkedPosL;
542 int zzEndReadL = zzEndRead;
543 char [] zzBufferL = zzBuffer;
544 char [] zzCMapL = ZZ_CMAP;
545
546 int [] zzTransL = ZZ_TRANS;
547 int [] zzRowMapL = ZZ_ROWMAP;
548 int [] zzAttrL = ZZ_ATTRIBUTE;
549
550 while (true) {
551 zzMarkedPosL = zzMarkedPos;
552
553 zzAction = -1;
554
555 zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL;
556
557 zzState = zzLexicalState;
558
559
560 zzForAction: {
561 while (true) {
562
563 if (zzCurrentPosL < zzEndReadL)
564 zzInput = zzBufferL[zzCurrentPosL++];
565 else if (zzAtEOF) {
566 zzInput = YYEOF;
567 break zzForAction;
568 }
569 else {
570 // store back cached positions
571 zzCurrentPos = zzCurrentPosL;
572 zzMarkedPos = zzMarkedPosL;
573 boolean eof = zzRefill();
574 // get translated positions and possibly new buffer
575 zzCurrentPosL = zzCurrentPos;
576 zzMarkedPosL = zzMarkedPos;
577 zzBufferL = zzBuffer;
578 zzEndReadL = zzEndRead;
579 if (eof) {
580 zzInput = YYEOF;
581 break zzForAction;
582 }
583 else {
584 zzInput = zzBufferL[zzCurrentPosL++];
585 }
586 }
587 int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ];
588 if (zzNext == -1) break zzForAction;
589 zzState = zzNext;
590
591 int zzAttributes = zzAttrL[zzState];
592 if ( (zzAttributes & 1) == 1 ) {
593 zzAction = zzState;
594 zzMarkedPosL = zzCurrentPosL;
595 if ( (zzAttributes & 8) == 8 ) break zzForAction;
596 }
597
598 }
599 }
600
601 // store back cached position
602 zzMarkedPos = zzMarkedPosL;
603
604 switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {
605 case 7:
606 { addToken(start, zzEndRead, Token.LITERAL_STRING_DOUBLE_QUOTE); return firstToken;
607 }
608 case 9: break;
609 case 2:
610 { start = zzMarkedPos; addToken(Token.OPERATOR); yybegin(VALUE);
611 }
612 case 10: break;
613 case 8:
614 { int temp=zzStartRead; addToken(start, zzStartRead-1, Token.LITERAL_STRING_DOUBLE_QUOTE); addToken(temp, zzMarkedPos-1, Token.VARIABLE); start = zzMarkedPos;
615 }
616 case 11: break;
617 case 3:
618 { addToken(Token.WHITESPACE);
619 }
620 case 12: break;
621 case 6:
622 { addToken(start, zzMarkedPos-1, Token.LITERAL_STRING_DOUBLE_QUOTE); start = zzMarkedPos;
623 }
624 case 13: break;
625 case 1:
626 { addToken(Token.RESERVED_WORD);
627 }
628 case 14: break;
629 case 5:
630 {
631 }
632 case 15: break;
633 case 4:
634 { addToken(Token.COMMENT_EOL);
635 }
636 case 16: break;
637 default:
638 if (zzInput == YYEOF && zzStartRead == zzCurrentPos) {
639 zzAtEOF = true;
640 switch (zzLexicalState) {
641 case YYINITIAL: {
642 addNullToken(); return firstToken;
643 }
644 case 14: break;
645 case VALUE: {
646 addToken(start,zzStartRead-1, Token.LITERAL_STRING_DOUBLE_QUOTE); addNullToken(); return firstToken;
647 }
648 case 15: break;
649 default:
650 return null;
651 }
652 }
653 else {
654 zzScanError(ZZ_NO_MATCH);
655 }
656 }
657 }
658 }
659
660
661}
Note: See TracBrowser for help on using the repository browser.