|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
GStringLexer.java | 0% | 0% | 0% | 0% |
|
1 |
package org.codehaus.groovy.syntax.lexer;
|
|
2 |
|
|
3 |
//{{{ imports
|
|
4 |
import org.codehaus.groovy.syntax.ReadException;
|
|
5 |
import org.codehaus.groovy.syntax.Types;
|
|
6 |
import org.codehaus.groovy.syntax.Token;
|
|
7 |
//}}}
|
|
8 |
|
|
9 |
/**
|
|
10 |
* A lexer for GStrings, usually run on a LexerFilter base.
|
|
11 |
*
|
|
12 |
* @author Chris Poirier
|
|
13 |
*/
|
|
14 |
|
|
15 |
public class GStringLexer extends LexerBase |
|
16 |
{ |
|
17 |
|
|
18 |
protected boolean sentStartToken = false; |
|
19 |
protected boolean sentEndToken = false; |
|
20 |
|
|
21 |
protected StringBuffer fullText = new StringBuffer(); |
|
22 |
protected int fullTextStartLine = 0; |
|
23 |
protected int fullTextStartColumn = 0; |
|
24 |
|
|
25 |
protected GroovyExpressionLexer child = null; |
|
26 |
protected boolean inExpression = false; |
|
27 |
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Finds and returns (consuming) the next token from the underlying stream.
|
|
31 |
* Returns null when out of tokens.
|
|
32 |
*/
|
|
33 |
|
|
34 | 0 |
protected Token undelegatedNextToken() throws ReadException, LexerException |
35 |
{ |
|
36 |
// System.out.println( "" + this + "undelegatedNextToken()" );
|
|
37 | 0 |
Token token = null;
|
38 |
|
|
39 |
|
|
40 |
//
|
|
41 |
// Handle bracketing tokens and EOS
|
|
42 |
|
|
43 | 0 |
if( !sentStartToken )
|
44 |
{ |
|
45 | 0 |
mark(); |
46 | 0 |
fullTextStartLine = getStartLine(); |
47 | 0 |
fullTextStartColumn = getStartColumn(); |
48 | 0 |
sentStartToken = true;
|
49 |
|
|
50 |
// System.out.println( "" + this + "returning GSTRING_START" );
|
|
51 | 0 |
return symbol( Types.GSTRING_START );
|
52 |
} |
|
53 | 0 |
else if( la(1) == CharStream.EOS ) |
54 |
{ |
|
55 | 0 |
if( !sentEndToken )
|
56 |
{ |
|
57 | 0 |
sentEndToken = true;
|
58 | 0 |
token = Token.newSymbol( Types.GSTRING_END, fullTextStartLine, fullTextStartColumn ); |
59 | 0 |
token.setText( fullText.toString() ); |
60 |
} |
|
61 |
|
|
62 |
// System.out.println( "" + this + "returning " + token );
|
|
63 | 0 |
return token;
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
//
|
|
68 |
// If we get this far, we are no longer delegated. If
|
|
69 |
// we just processed an expression, the next character
|
|
70 |
// had better be a '}'...
|
|
71 |
|
|
72 | 0 |
if( inExpression && la(1) != '}' )
|
73 |
{ |
|
74 | 0 |
mark(); |
75 | 0 |
unexpected( la(1), 0 ); |
76 |
} |
|
77 |
|
|
78 |
|
|
79 |
//
|
|
80 |
// Otherwise, it's a lex...
|
|
81 |
|
|
82 | 0 |
mark(); |
83 | 0 |
StringBuffer segment = new StringBuffer();
|
84 |
|
|
85 | 0 |
char c;
|
86 | 0 |
MAIN_LOOP: while( true ) |
87 |
{ |
|
88 | 0 |
c = la(1); |
89 |
|
|
90 | 0 |
ROOT_SWITCH: switch( c )
|
91 |
{ |
|
92 |
case CharStream.EOS:
|
|
93 |
{ |
|
94 | 0 |
break MAIN_LOOP;
|
95 |
} |
|
96 |
|
|
97 |
case '\r':
|
|
98 |
case '\n':
|
|
99 |
{ |
|
100 | 0 |
readEOL( segment ); |
101 | 0 |
break ROOT_SWITCH;
|
102 |
} |
|
103 |
|
|
104 |
case '\\':
|
|
105 |
{ |
|
106 | 0 |
ESCAPE_SWITCH: switch( la(2) )
|
107 |
{ |
|
108 |
case '\\':
|
|
109 |
case '$':
|
|
110 |
{ |
|
111 | 0 |
consume(); |
112 | 0 |
segment.append( consume() ); |
113 | 0 |
break ESCAPE_SWITCH;
|
114 |
} |
|
115 |
|
|
116 |
default:
|
|
117 |
{ |
|
118 | 0 |
segment.append( consume() ); |
119 | 0 |
break ESCAPE_SWITCH;
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 | 0 |
break ROOT_SWITCH;
|
124 |
} |
|
125 |
|
|
126 |
case '$':
|
|
127 |
{ |
|
128 | 0 |
if( la(2) == '{' )
|
129 |
{ |
|
130 | 0 |
if( segment.length() == 0 )
|
131 |
{ |
|
132 | 0 |
sourceDelimiting( false ); // ensures ${"..."} works |
133 |
|
|
134 | 0 |
mark(); |
135 | 0 |
consume(); |
136 | 0 |
consume(); |
137 |
|
|
138 | 0 |
token = symbol( Types.GSTRING_EXPRESSION_START ); |
139 | 0 |
inExpression = true;
|
140 |
|
|
141 | 0 |
if( child == null ) |
142 |
{ |
|
143 | 0 |
child = new GroovyExpressionLexer();
|
144 |
} |
|
145 |
else
|
|
146 |
{ |
|
147 | 0 |
child.reset(); |
148 |
} |
|
149 |
|
|
150 | 0 |
delegate( child ); |
151 |
|
|
152 | 0 |
break MAIN_LOOP;
|
153 |
} |
|
154 |
else
|
|
155 |
{ |
|
156 | 0 |
break MAIN_LOOP;
|
157 |
} |
|
158 |
} |
|
159 |
else
|
|
160 |
{ |
|
161 | 0 |
segment.append( consume() ); |
162 |
} |
|
163 |
|
|
164 | 0 |
break ROOT_SWITCH;
|
165 |
} |
|
166 |
|
|
167 |
case '}':
|
|
168 |
{ |
|
169 | 0 |
if( inExpression )
|
170 |
{ |
|
171 | 0 |
mark(); |
172 | 0 |
consume(); |
173 | 0 |
token = symbol( Types.GSTRING_EXPRESSION_END ); |
174 |
|
|
175 | 0 |
inExpression = false;
|
176 |
|
|
177 | 0 |
break MAIN_LOOP;
|
178 |
} |
|
179 |
else
|
|
180 |
{ |
|
181 | 0 |
segment.append( consume() ); |
182 | 0 |
break ROOT_SWITCH;
|
183 |
} |
|
184 |
} |
|
185 |
|
|
186 |
default:
|
|
187 |
{ |
|
188 | 0 |
segment.append( consume() ); |
189 | 0 |
break ROOT_SWITCH;
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
|
|
195 | 0 |
if( token != null ) |
196 |
{ |
|
197 |
// System.out.println( "" + this + "returning " + token );
|
|
198 | 0 |
return token;
|
199 |
} |
|
200 |
else
|
|
201 |
{ |
|
202 |
// System.out.println( "" + this + "returning string of " + segment );
|
|
203 | 0 |
return Token.newString( segment.toString(), getStartLine(), getStartColumn() );
|
204 |
} |
|
205 |
|
|
206 |
} |
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
//---------------------------------------------------------------------------
|
|
212 |
// DELEGATION
|
|
213 |
|
|
214 |
|
|
215 |
/**
|
|
216 |
* Coordinates with our source about delimiting. When
|
|
217 |
* entering or processing sub-expressions, source delimiting
|
|
218 |
* should be off.
|
|
219 |
*/
|
|
220 |
|
|
221 | 0 |
protected void sourceDelimiting( boolean delimit ) |
222 |
{ |
|
223 | 0 |
if( source instanceof Delimiter ) |
224 |
{ |
|
225 | 0 |
((Delimiter)source).delimit( delimit ); |
226 |
} |
|
227 |
} |
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
/**
|
|
232 |
* Delegates our duties to another Lexer.
|
|
233 |
*/
|
|
234 |
|
|
235 | 0 |
public void delegate( Lexer to ) |
236 |
{ |
|
237 | 0 |
this.delegate = to;
|
238 | 0 |
sourceDelimiting( false );
|
239 | 0 |
to.setSource( this );
|
240 |
} |
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
/**
|
|
245 |
* Retakes responsibility for our duties.
|
|
246 |
*/
|
|
247 |
|
|
248 | 0 |
public void undelegate() |
249 |
{ |
|
250 | 0 |
if( delegate != null ) |
251 |
{ |
|
252 | 0 |
delegate.unsetSource( ); |
253 | 0 |
delegate = null;
|
254 | 0 |
sourceDelimiting( true );
|
255 |
} |
|
256 |
} |
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
/**
|
|
261 |
* Sets the source lexer.
|
|
262 |
*/
|
|
263 |
|
|
264 | 0 |
public void setSource( Lexer source ) |
265 |
{ |
|
266 | 0 |
super.setSource( source );
|
267 |
|
|
268 | 0 |
sentStartToken = false;
|
269 | 0 |
sentEndToken = false;
|
270 |
|
|
271 | 0 |
fullTextStartLine = getStartLine(); |
272 | 0 |
fullTextStartColumn = getStartColumn(); |
273 | 0 |
fullText = new StringBuffer();
|
274 |
|
|
275 | 0 |
inExpression = false;
|
276 |
} |
|
277 |
|
|
278 |
|
|
279 |
|
|
280 |
/**
|
|
281 |
* Unsets the source lexer.
|
|
282 |
*/
|
|
283 |
|
|
284 | 0 |
public void unsetSource() |
285 |
{ |
|
286 | 0 |
super.unsetSource();
|
287 |
|
|
288 | 0 |
sentStartToken = false;
|
289 | 0 |
sentEndToken = false;
|
290 | 0 |
fullText = null;
|
291 | 0 |
inExpression = false;
|
292 |
} |
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
//---------------------------------------------------------------------------
|
|
298 |
// STREAM ROUTINES
|
|
299 |
|
|
300 |
|
|
301 |
/**
|
|
302 |
* Eats a character from the input stream.
|
|
303 |
*/
|
|
304 |
|
|
305 | 0 |
public char consume() throws LexerException, ReadException |
306 |
{ |
|
307 | 0 |
char c = super.consume(); |
308 |
|
|
309 | 0 |
if( c != CharStream.EOS )
|
310 |
{ |
|
311 | 0 |
fullText.append(c); |
312 |
} |
|
313 |
|
|
314 | 0 |
return c;
|
315 |
} |
|
316 |
|
|
317 |
|
|
318 |
} |
|
319 |
|
|
320 |
|
|