1 | /*
|
---|
2 | * 04/24/2012
|
---|
3 | *
|
---|
4 | * LatexTokenMaker.java - Scanner for LaTeX.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea.modes;
|
---|
10 |
|
---|
11 | import java.io.*;
|
---|
12 | import javax.swing.text.Segment;
|
---|
13 |
|
---|
14 | import org.fife.ui.rsyntaxtextarea.*;
|
---|
15 |
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Scanner for the LaTeX.<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 RSTA 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 LatexTokenMaker.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 LatexTokenMaker
|
---|
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 LatexTokenMaker() {
|
---|
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 | * ${inheritDoc}
|
---|
129 | */
|
---|
130 | public String[] getLineCommentStartAndEnd() {
|
---|
131 | return new String[] { "%", null };
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Returns the first token in the linked list of tokens generated
|
---|
137 | * from <code>text</code>. This method must be implemented by
|
---|
138 | * subclasses so they can correctly implement syntax highlighting.
|
---|
139 | *
|
---|
140 | * @param text The text from which to get tokens.
|
---|
141 | * @param initialTokenType The token type we should start with.
|
---|
142 | * @param startOffset The offset into the document at which
|
---|
143 | * <code>text</code> starts.
|
---|
144 | * @return The first <code>Token</code> in a linked list representing
|
---|
145 | * the syntax highlighted text.
|
---|
146 | */
|
---|
147 | public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
|
---|
148 |
|
---|
149 | resetTokenList();
|
---|
150 | this.offsetShift = -text.offset + startOffset;
|
---|
151 |
|
---|
152 | // Start off in the proper state.
|
---|
153 | int state = Token.NULL;
|
---|
154 |
|
---|
155 | s = text;
|
---|
156 | try {
|
---|
157 | yyreset(zzReader);
|
---|
158 | yybegin(state);
|
---|
159 | return yylex();
|
---|
160 | } catch (IOException ioe) {
|
---|
161 | ioe.printStackTrace();
|
---|
162 | return new DefaultToken();
|
---|
163 | }
|
---|
164 |
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Refills the input buffer.
|
---|
170 | *
|
---|
171 | * @return <code>true</code> if EOF was reached, otherwise
|
---|
172 | * <code>false</code>.
|
---|
173 | * @exception IOException if any I/O-Error occurs.
|
---|
174 | */
|
---|
175 | private boolean zzRefill() {
|
---|
176 | return zzCurrentPos>=s.offset+s.count;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Resets the scanner to read from a new input stream.
|
---|
182 | * Does not close the old reader.
|
---|
183 | *
|
---|
184 | * All internal variables are reset, the old input stream
|
---|
185 | * <b>cannot</b> be reused (internal buffer is discarded and lost).
|
---|
186 | * Lexical state is set to <tt>YY_INITIAL</tt>.
|
---|
187 | *
|
---|
188 | * @param reader the new input stream
|
---|
189 | */
|
---|
190 | public final void yyreset(java.io.Reader reader) {
|
---|
191 | // 's' has been updated.
|
---|
192 | zzBuffer = s.array;
|
---|
193 | /*
|
---|
194 | * We replaced the line below with the two below it because zzRefill
|
---|
195 | * no longer "refills" the buffer (since the way we do it, it's always
|
---|
196 | * "full" the first time through, since it points to the segment's
|
---|
197 | * array). So, we assign zzEndRead here.
|
---|
198 | */
|
---|
199 | //zzStartRead = zzEndRead = s.offset;
|
---|
200 | zzStartRead = s.offset;
|
---|
201 | zzEndRead = zzStartRead + s.count - 1;
|
---|
202 | zzCurrentPos = zzMarkedPos = zzPushbackPos = s.offset;
|
---|
203 | zzLexicalState = YYINITIAL;
|
---|
204 | zzReader = reader;
|
---|
205 | zzAtBOL = true;
|
---|
206 | zzAtEOF = false;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | %}
|
---|
211 |
|
---|
212 | Letter = ([A-Za-z])
|
---|
213 | Digit = ([0-9])
|
---|
214 | LetterOrUnderscore = ({Letter}|[_])
|
---|
215 | AnyChar = ({LetterOrUnderscore}|{Digit}|[\-])
|
---|
216 | Whitespace = ([ \t\f])
|
---|
217 | LineCommentBegin = "%"
|
---|
218 |
|
---|
219 | URLGenDelim = ([:\/\?#\[\]@])
|
---|
220 | URLSubDelim = ([\!\$&'\(\)\*\+,;=])
|
---|
221 | URLUnreserved = ({LetterOrUnderscore}|{Digit}|[\-\.\~])
|
---|
222 | URLCharacter = ({URLGenDelim}|{URLSubDelim}|{URLUnreserved}|[%])
|
---|
223 | URLCharacters = ({URLCharacter}*)
|
---|
224 | URLEndCharacter = ([\/\$]|{Letter}|{Digit})
|
---|
225 | URL = (((https?|f(tp|ile))"://"|"www.")({URLCharacters}{URLEndCharacter})?)
|
---|
226 |
|
---|
227 |
|
---|
228 | %state EOL_COMMENT
|
---|
229 |
|
---|
230 | %%
|
---|
231 |
|
---|
232 | <YYINITIAL> {
|
---|
233 |
|
---|
234 | ([\\]{AnyChar}+) { addToken(Token.FUNCTION); }
|
---|
235 | [\{\}] { addToken(Token.SEPARATOR); }
|
---|
236 | ("\\begin{"{AnyChar}+"}") { int temp = zzStartRead;
|
---|
237 | addToken(temp, temp+5, Token.RESERVED_WORD);
|
---|
238 | addToken(temp+6, temp+6, Token.SEPARATOR);
|
---|
239 | addToken(temp+7, zzMarkedPos-2, Token.RESERVED_WORD);
|
---|
240 | addToken(zzMarkedPos-1, zzMarkedPos-1, Token.SEPARATOR);
|
---|
241 | }
|
---|
242 | ("\\end{"{AnyChar}+"}") { int temp = zzStartRead;
|
---|
243 | addToken(temp, temp+3, Token.RESERVED_WORD);
|
---|
244 | addToken(temp+4, temp+4, Token.SEPARATOR);
|
---|
245 | addToken(temp+5, zzMarkedPos-2, Token.RESERVED_WORD);
|
---|
246 | addToken(zzMarkedPos-1, zzMarkedPos-1, Token.SEPARATOR);
|
---|
247 | }
|
---|
248 | {Whitespace} { addToken(Token.WHITESPACE); }
|
---|
249 |
|
---|
250 | {LineCommentBegin} { start = zzMarkedPos-1; yybegin(EOL_COMMENT); }
|
---|
251 |
|
---|
252 | "\n" |
|
---|
253 | <<EOF>> { addNullToken(); return firstToken; }
|
---|
254 |
|
---|
255 | /* Catch any other (unhandled) characters and flag them as identifiers. */
|
---|
256 | {AnyChar}+ |
|
---|
257 | . { addToken(Token.IDENTIFIER); }
|
---|
258 |
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | <EOL_COMMENT> {
|
---|
263 | [^hwf\n]+ {}
|
---|
264 | {URL} { int temp=zzStartRead; addToken(start,zzStartRead-1, Token.COMMENT_EOL); addHyperlinkToken(temp,zzMarkedPos-1, Token.COMMENT_EOL); start = zzMarkedPos; }
|
---|
265 | [hwf] {}
|
---|
266 | \n |
|
---|
267 | <<EOF>> { addToken(start,zzStartRead-1, Token.COMMENT_EOL); addNullToken(); return firstToken; }
|
---|
268 | }
|
---|