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