~ubuntu-branches/ubuntu/trusty/drmips/trusty-backports

« back to all changes in this revision

Viewing changes to src/pc/DrMIPS/src/org/fife/ui/rsyntaxtextarea/modes/PropertiesFileTokenMaker.flex

  • Committer: Package Import Robot
  • Author(s): Bruno Nova
  • Date: 2014-09-27 12:24:17 UTC
  • Revision ID: package-import@ubuntu.com-20140927122417-2gadkwt9k0u7j4zu
Tags: upstream-1.2.3
Import upstream version 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * 03/21/2005
 
3
 *
 
4
 * PropertiesFileTokenMaker.java - Scanner for properties files.
 
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
 * This class splits up text into tokens representing a Java properties file.<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 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>PropertiesFileTokenMaker.java</code> file will
 
39
 *       contain two definitions of both <code>zzRefill</code> and
 
40
 *       <code>yyreset</code>.  You should hand-delete the second of each
 
41
 *       definition (the ones generated by the lexer), as these generated
 
42
 *       methods modify the input buffer, which we'll never have to do.
 
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.
 
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.
 
49
 * </ul>
 
50
 *
 
51
 * @author Robert Futrell
 
52
 * @version 0.4
 
53
 *
 
54
 */
 
55
%%
 
56
 
 
57
%public
 
58
%class PropertiesFileTokenMaker
 
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 PropertiesFileTokenMaker() {
 
72
                super();
 
73
        }
 
74
 
 
75
 
 
76
        /**
 
77
         * Adds the token specified to the current linked list of tokens.
 
78
         *
 
79
         * @param tokenType The token's type.
 
80
         */
 
81
        private void addToken(int tokenType) {
 
82
                addToken(zzStartRead, zzMarkedPos-1, tokenType);
 
83
        }
 
84
 
 
85
 
 
86
        /**
 
87
         * Adds the token specified to the current linked list of tokens.
 
88
         *
 
89
         * @param tokenType The token's type.
 
90
         */
 
91
        private void addToken(int start, int end, int tokenType) {
 
92
                int so = start + offsetShift;
 
93
                addToken(zzBuffer, start,end, tokenType, so);
 
94
        }
 
95
 
 
96
 
 
97
        /**
 
98
         * Adds the token specified to the current linked list of tokens.
 
99
         *
 
100
         * @param array The character array.
 
101
         * @param start The starting offset in the array.
 
102
         * @param end The ending offset in the array.
 
103
         * @param tokenType The token's type.
 
104
         * @param startOffset The offset in the document at which this token
 
105
         *                    occurs.
 
106
         */
 
107
        @Override
 
108
        public void addToken(char[] array, int start, int end, int tokenType, int startOffset) {
 
109
                super.addToken(array, start,end, tokenType, startOffset);
 
110
                zzStartRead = zzMarkedPos;
 
111
        }
 
112
 
 
113
 
 
114
        /**
 
115
         * Returns the text to place at the beginning and end of a
 
116
         * line to "comment" it in a this programming language.
 
117
         *
 
118
         * @return The start and end strings to add to a line to "comment"
 
119
         *         it out.
 
120
         */
 
121
        @Override
 
122
        public String[] getLineCommentStartAndEnd() {
 
123
                return new String[] { "#", null };
 
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
                int state = Token.NULL;
 
146
                switch (initialTokenType) {
 
147
                        case Token.LITERAL_STRING_DOUBLE_QUOTE:
 
148
                                state = VALUE;
 
149
                                start = text.offset;
 
150
                                break;
 
151
                        default:
 
152
                                state = Token.NULL;
 
153
                }
 
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 TokenImpl();
 
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
Equals                          = ([=\:])
 
213
Name                                    = ([^=\: \t\n#!]*)
 
214
Whitespace                      = ([ \t]+)
 
215
Comment                         = ([#!].*)
 
216
SingleQuote                     = (')
 
217
 
 
218
%state VALUE
 
219
 
 
220
%%
 
221
 
 
222
<YYINITIAL> {
 
223
        {Name}                  { addToken(Token.RESERVED_WORD); }
 
224
        {Equals}                        { start = zzMarkedPos; addToken(Token.OPERATOR); yybegin(VALUE); }
 
225
        {Whitespace}            { addToken(Token.WHITESPACE); }
 
226
        {Comment}                       { addToken(Token.COMMENT_EOL); }
 
227
        <<EOF>>                 { addNullToken(); return firstToken; }
 
228
}
 
229
 
 
230
<VALUE> {
 
231
        {SingleQuote}[^']*{SingleQuote}?        { addToken(start, zzMarkedPos-1, Token.LITERAL_STRING_DOUBLE_QUOTE); start = zzMarkedPos; }
 
232
        [^'\{\\]+                                               {}
 
233
        "{"[^\}]*"}"?                                   { int temp=zzStartRead; addToken(start, zzStartRead-1, Token.LITERAL_STRING_DOUBLE_QUOTE); addToken(temp, zzMarkedPos-1, Token.VARIABLE); start = zzMarkedPos; }
 
234
        [\\].                                                   {}
 
235
        [\\]                                                    { addToken(start, zzEndRead, Token.LITERAL_STRING_DOUBLE_QUOTE); return firstToken; }
 
236
        <<EOF>>                                                 { addToken(start,zzStartRead-1, Token.LITERAL_STRING_DOUBLE_QUOTE); addNullToken(); return firstToken; }
 
237
}