source: other-projects/rsyntax-textarea/src/java/org/fife/ui/rsyntaxtextarea/modes/PlainTextTokenMaker.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: 7.1 KB
Line 
1/*
2 * 11/07/2008
3 *
4 * PlainTextTokenMaker.flex - Scanner for plain text files.
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 plain text files.
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 <code>PlainTextTokenMaker.java</code> file will contain
39 * two 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 PlainTextTokenMaker
59%extends AbstractJFlexTokenMaker
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 PlainTextTokenMaker() {
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 * @param link Whether this token is a hyperlink.
80 */
81 private void addToken(int tokenType, boolean link) {
82 int so = zzStartRead + offsetShift;
83 super.addToken(zzBuffer, zzStartRead,zzMarkedPos-1, tokenType, so, link);
84 zzStartRead = zzMarkedPos;
85 }
86
87
88 /**
89 * Always returns <code>Token.NULL</code>, as there are no multiline
90 * tokens in properties files.
91 *
92 * @param text The line of tokens to examine.
93 * @param initialTokenType The token type to start with (i.e., the value
94 * of <code>getLastTokenTypeOnLine</code> for the line before
95 * <code>text</code>).
96 * @return <code>Token.NULL</code>.
97 */
98 public int getLastTokenTypeOnLine(Segment text, int initialTokenType) {
99 return Token.NULL;
100 }
101
102
103 /**
104 * Returns the text to place at the beginning and end of a
105 * line to "comment" it in a this programming language.
106 *
107 * @return <code>null</code>, as there are no comments in plain text.
108 */
109 public String[] getLineCommentStartAndEnd() {
110 return null;
111 }
112
113
114 /**
115 * Always returns <tt>false</tt>, as you never want "mark occurrences"
116 * working in plain text files.
117 *
118 * @param type The token type.
119 * @return Whether tokens of this type should have "mark occurrences"
120 * enabled.
121 */
122 public boolean getMarkOccurrencesOfTokenType(int type) {
123 return false;
124 }
125
126
127 /**
128 * Returns the first token in the linked list of tokens generated
129 * from <code>text</code>. This method must be implemented by
130 * subclasses so they can correctly implement syntax highlighting.
131 *
132 * @param text The text from which to get tokens.
133 * @param initialTokenType The token type we should start with.
134 * @param startOffset The offset into the document at which
135 * <code>text</code> starts.
136 * @return The first <code>Token</code> in a linked list representing
137 * the syntax highlighted text.
138 */
139 public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
140
141 resetTokenList();
142 this.offsetShift = -text.offset + startOffset;
143
144 // Start off in the proper state.
145 s = text;
146 try {
147 yyreset(zzReader);
148 yybegin(YYINITIAL);
149 return yylex();
150 } catch (IOException ioe) {
151 ioe.printStackTrace();
152 return new DefaultToken();
153 }
154
155 }
156
157
158 /**
159 * Refills the input buffer.
160 *
161 * @return <code>true</code> if EOF was reached, otherwise
162 * <code>false</code>.
163 * @exception IOException if any I/O-Error occurs.
164 */
165 private boolean zzRefill() {
166 return zzCurrentPos>=s.offset+s.count;
167 }
168
169
170 /**
171 * Resets the scanner to read from a new input stream.
172 * Does not close the old reader.
173 *
174 * All internal variables are reset, the old input stream
175 * <b>cannot</b> be reused (internal buffer is discarded and lost).
176 * Lexical state is set to <tt>YY_INITIAL</tt>.
177 *
178 * @param reader the new input stream
179 */
180 public final void yyreset(java.io.Reader reader) {
181 // 's' has been updated.
182 zzBuffer = s.array;
183 /*
184 * We replaced the line below with the two below it because zzRefill
185 * no longer "refills" the buffer (since the way we do it, it's always
186 * "full" the first time through, since it points to the segment's
187 * array). So, we assign zzEndRead here.
188 */
189 //zzStartRead = zzEndRead = s.offset;
190 zzStartRead = s.offset;
191 zzEndRead = zzStartRead + s.count - 1;
192 zzCurrentPos = zzMarkedPos = zzPushbackPos = s.offset;
193 zzLexicalState = YYINITIAL;
194 zzReader = reader;
195 zzAtBOL = true;
196 zzAtEOF = false;
197 }
198
199
200%}
201
202LetterOrDigit = ([a-zA-Z0-9])
203Identifier = ({LetterOrDigit}+)
204Separator = ([^a-zA-Z0-9 \t\n])
205WhiteSpace = ([ \t]+)
206LineTerminator = ([\n])
207
208URLGenDelim = ([:\/\?#\[\]@])
209URLSubDelim = ([\!\$&'\(\)\*\+,;=])
210URLUnreserved = ({LetterOrDigit}|[_\-\.\~])
211URLCharacter = ({URLGenDelim}|{URLSubDelim}|{URLUnreserved}|[%])
212URLCharacters = ({URLCharacter}*)
213URLEndCharacter = ([\/\$]|{LetterOrDigit})
214URL = (((https?|f(tp|ile))"://"|"www.")({URLCharacters}{URLEndCharacter})?)
215
216%%
217
218<YYINITIAL> {
219 {URL} { addToken(Token.IDENTIFIER, true); }
220 {Identifier} { addToken(Token.IDENTIFIER, false); }
221 {Separator} { addToken(Token.IDENTIFIER, false); }
222 {WhiteSpace} { addToken(Token.WHITESPACE, false); }
223 {LineTerminator} { addNullToken(); return firstToken; }
224 <<EOF>> { addNullToken(); return firstToken; }
225 . { /* Never happens */ addToken(Token.IDENTIFIER, false); }
226}
Note: See TracBrowser for help on using the repository browser.