4
* CTokenMaker.java - An object that can take a chunk of text and
5
* return a linked list of tokens representing it in the C programming
8
* This library is distributed under a modified BSD license. See the included
9
* RSyntaxTextArea.License.txt file for details.
11
package org.fife.ui.rsyntaxtextarea.modes;
14
import javax.swing.text.Segment;
16
import org.fife.ui.rsyntaxtextarea.*;
20
* Scanner for the C programming language.
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
35
* The actual algorithm generated for scanning has, of course, not been
38
* If you wish to regenerate this file yourself, keep in mind the following:
40
* <li>The generated <code>CTokenMaker.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>
53
* @author Robert Futrell
61
%extends AbstractJFlexCTokenMaker
63
%type org.fife.ui.rsyntaxtextarea.Token
70
* Constructor. This must be here because JFlex does not generate a
71
* no-parameter constructor.
73
public CTokenMaker() {
79
* Adds the token specified to the current linked list of tokens.
81
* @param tokenType The token's type.
82
* @see #addToken(int, int, int)
84
private void addHyperlinkToken(int start, int end, int tokenType) {
85
int so = start + offsetShift;
86
addToken(zzBuffer, start,end, tokenType, so, true);
91
* Adds the token specified to the current linked list of tokens.
93
* @param tokenType The token's type.
95
private void addToken(int tokenType) {
96
addToken(zzStartRead, zzMarkedPos-1, tokenType);
101
* Adds the token specified to the current linked list of tokens.
103
* @param tokenType The token's type.
105
private void addToken(int start, int end, int tokenType) {
106
int so = start + offsetShift;
107
addToken(zzBuffer, start,end, tokenType, so);
112
* Adds the token specified to the current linked list of tokens.
114
* @param array The character array.
115
* @param start The starting offset in the array.
116
* @param end The ending offset in the array.
117
* @param tokenType The token's type.
118
* @param startOffset The offset in the document at which this token
122
public void addToken(char[] array, int start, int end, int tokenType, int startOffset) {
123
super.addToken(array, start,end, tokenType, startOffset);
124
zzStartRead = zzMarkedPos;
129
* Returns the text to place at the beginning and end of a
130
* line to "comment" it in a this programming language.
132
* @return The start and end strings to add to a line to "comment"
136
public String[] getLineCommentStartAndEnd() {
137
return new String[] { "//", null };
142
* Returns the first token in the linked list of tokens generated
143
* from <code>text</code>. This method must be implemented by
144
* subclasses so they can correctly implement syntax highlighting.
146
* @param text The text from which to get tokens.
147
* @param initialTokenType The token type we should start with.
148
* @param startOffset The offset into the document at which
149
* <code>text</code> starts.
150
* @return The first <code>Token</code> in a linked list representing
151
* the syntax highlighted text.
153
public Token getTokenList(Segment text, int initialTokenType, int startOffset) {
156
this.offsetShift = -text.offset + startOffset;
158
// Start off in the proper state.
159
int state = Token.NULL;
160
switch (initialTokenType) {
161
case Token.COMMENT_MULTILINE:
174
} catch (IOException ioe) {
175
ioe.printStackTrace();
176
return new TokenImpl();
183
* Refills the input buffer.
185
* @return <code>true</code> if EOF was reached, otherwise
186
* <code>false</code>.
187
* @exception IOException if any I/O-Error occurs.
189
private boolean zzRefill() throws java.io.IOException {
190
return zzCurrentPos>=s.offset+s.count;
195
* Resets the scanner to read from a new input stream.
196
* Does not close the old reader.
198
* All internal variables are reset, the old input stream
199
* <b>cannot</b> be reused (internal buffer is discarded and lost).
200
* Lexical state is set to <tt>YY_INITIAL</tt>.
202
* @param reader the new input stream
204
public final void yyreset(java.io.Reader reader) throws java.io.IOException {
205
// 's' has been updated.
208
* We replaced the line below with the two below it because zzRefill
209
* no longer "refills" the buffer (since the way we do it, it's always
210
* "full" the first time through, since it points to the segment's
211
* array). So, we assign zzEndRead here.
213
//zzStartRead = zzEndRead = s.offset;
214
zzStartRead = s.offset;
215
zzEndRead = zzStartRead + s.count - 1;
216
zzCurrentPos = zzMarkedPos = zzPushbackPos = s.offset;
217
zzLexicalState = YYINITIAL;
227
LetterOrUnderscore = ({Letter}|[_])
229
HexDigit = {Digit}|[A-Fa-f]
231
Exponent = [eE][+-]?{Digit}+
233
PreprocessorWord = define|elif|else|endif|error|if|ifdef|ifndef|include|line|pragma|undef
235
Trigraph = ("??="|"??("|"??)"|"??/"|"??'"|"??<"|"??>"|"??!"|"??-")
237
OctEscape1 = ([\\]{OctalDigit})
238
OctEscape2 = ([\\]{OctalDigit}{OctalDigit})
239
OctEscape3 = ([\\][0-3]{OctalDigit}{OctalDigit})
240
OctEscape = ({OctEscape1}|{OctEscape2}|{OctEscape3})
241
HexEscape = ([\\][xX]{HexDigit}{HexDigit})
243
AnyChrChr = ([^\'\n\\])
244
Escape = ([\\]([abfnrtv\'\"\?\\0]))
245
UnclosedCharLiteral = ([\']({Escape}|{OctEscape}|{HexEscape}|{Trigraph}|{AnyChrChr}))
246
CharLiteral = ({UnclosedCharLiteral}[\'])
247
ErrorUnclosedCharLiteral = ([\'][^\'\n]*)
248
ErrorCharLiteral = (([\'][\'])|{ErrorUnclosedCharLiteral}[\'])
249
AnyStrChr = ([^\"\n\\])
250
FalseTrigraph = (("?"(("?")*)[^\=\(\)\/\'\<\>\!\-\\\?\"\n])|("?"[\=\(\)\/\'\<\>\!\-]))
251
StringLiteral = ([\"]((((("?")*)({Escape}|{OctEscape}|{HexEscape}|{Trigraph}))|{FalseTrigraph}|{AnyStrChr})*)(("?")*)[\"])
252
UnclosedStringLiteral = ([\"]([\\].|[^\\\"])*[^\"]?)
253
ErrorStringLiteral = ({UnclosedStringLiteral}[\"])
261
LineCommentBegin = "//"
263
NonFloatSuffix = (([uU][lL]?)|([lL][uU]?))
264
IntegerLiteral = ({Digit}+{Exponent}?{NonFloatSuffix}?)
265
HexLiteral = ("0"[xX]{HexDigit}+{NonFloatSuffix}?)
266
FloatLiteral = ((({Digit}*[\.]{Digit}+)|({Digit}+[\.]{Digit}*)){Exponent}?[fFlL]?)
267
ErrorNumberFormat = (({IntegerLiteral}|{HexLiteral}|{FloatLiteral}){NonSeparator}+)
269
NonSeparator = ([^\t\f\r\n\ \(\)\{\}\[\]\;\,\.\=\>\<\!\~\?\:\+\-\*\/\&\|\^\%\"\']|"#")
270
Identifier = ({LetterOrUnderscore}({LetterOrUnderscore}|{Digit}|[$])*)
271
ErrorIdentifier = ({NonSeparator}+)
274
URLGenDelim = ([:\/\?#\[\]@])
275
URLSubDelim = ([\!\$&'\(\)\*\+,;=])
276
URLUnreserved = ({LetterOrUnderscore}|{Digit}|[\-\.\~])
277
URLCharacter = ({URLGenDelim}|{URLSubDelim}|{URLUnreserved}|[%])
278
URLCharacters = ({URLCharacter}*)
279
URLEndCharacter = ([\/\$]|{Letter}|{Digit})
280
URL = (((https?|f(tp|ile))"://"|"www.")({URLCharacters}{URLEndCharacter})?)
312
"while" { addToken(Token.RESERVED_WORD); }
327
"wchar_t" { addToken(Token.DATA_TYPE); }
329
/* Standard functions */
529
"wscanf" { addToken(Token.FUNCTION); }
531
/* Standard-defined macros. */
536
"__STDC__" { addToken(Token.PREPROCESSOR); }
538
{LineTerminator} { addNullToken(); return firstToken; }
540
{Identifier} { addToken(Token.IDENTIFIER); }
542
{WhiteSpace}+ { addToken(Token.WHITESPACE); }
544
/* Preprocessor directives */
545
"#"{WhiteSpace}*{PreprocessorWord} { addToken(Token.PREPROCESSOR); }
547
/* String/Character Literals. */
548
{CharLiteral} { addToken(Token.LITERAL_CHAR); }
549
{UnclosedCharLiteral} { addToken(Token.ERROR_CHAR); /*addNullToken(); return firstToken;*/ }
550
{ErrorUnclosedCharLiteral} { addToken(Token.ERROR_CHAR); addNullToken(); return firstToken; }
551
{ErrorCharLiteral} { addToken(Token.ERROR_CHAR); }
552
{StringLiteral} { addToken(Token.LITERAL_STRING_DOUBLE_QUOTE); }
553
{UnclosedStringLiteral} { addToken(Token.ERROR_STRING_DOUBLE); addNullToken(); return firstToken; }
554
{ErrorStringLiteral} { addToken(Token.ERROR_STRING_DOUBLE); }
556
/* Comment Literals. */
557
{MLCBegin} { start = zzMarkedPos-2; yybegin(MLC); }
558
{LineCommentBegin} { start = zzMarkedPos-2; yybegin(EOL_COMMENT); }
566
"}" { addToken(Token.SEPARATOR); }
601
"," { addToken(Token.OPERATOR); }
604
{IntegerLiteral} { addToken(Token.LITERAL_NUMBER_DECIMAL_INT); }
605
{HexLiteral} { addToken(Token.LITERAL_NUMBER_HEXADECIMAL); }
606
{FloatLiteral} { addToken(Token.LITERAL_NUMBER_FLOAT); }
607
{ErrorNumberFormat} { addToken(Token.ERROR_NUMBER_FORMAT); }
609
/* Some lines will end in '\' to wrap an expression. */
610
"\\" { addToken(Token.IDENTIFIER); }
612
{ErrorIdentifier} { addToken(Token.ERROR_IDENTIFIER); }
614
/* Other punctuation, we'll highlight it as "identifiers." */
615
";" { addToken(Token.IDENTIFIER); }
617
/* Ended with a line not in a string or comment. */
618
<<EOF>> { addNullToken(); return firstToken; }
620
/* Catch any other (unhandled) characters and flag them as bad. */
621
. { addToken(Token.ERROR_IDENTIFIER); }
628
{URL} { int temp=zzStartRead; addToken(start,zzStartRead-1, Token.COMMENT_MULTILINE); addHyperlinkToken(temp,zzMarkedPos-1, Token.COMMENT_MULTILINE); start = zzMarkedPos; }
631
\n { addToken(start,zzStartRead-1, Token.COMMENT_MULTILINE); return firstToken; }
632
{MLCEnd} { yybegin(YYINITIAL); addToken(start,zzStartRead+1, Token.COMMENT_MULTILINE); }
634
<<EOF>> { addToken(start,zzStartRead-1, Token.COMMENT_MULTILINE); return firstToken; }
641
{URL} { int temp=zzStartRead; addToken(start,zzStartRead-1, Token.COMMENT_EOL); addHyperlinkToken(temp,zzMarkedPos-1, Token.COMMENT_EOL); start = zzMarkedPos; }
643
\n { addToken(start,zzStartRead-1, Token.COMMENT_EOL); addNullToken(); return firstToken; }
644
<<EOF>> { addToken(start,zzStartRead-1, Token.COMMENT_EOL); addNullToken(); return firstToken; }