|
|||||||||||||||||||
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 | |||||||||||||||
GroovyExpressionLexer.java | 0% | 0% | 0% | 0% |
|
1 |
package org.codehaus.groovy.syntax.lexer;
|
|
2 |
|
|
3 |
import org.codehaus.groovy.syntax.ReadException;
|
|
4 |
import org.codehaus.groovy.syntax.Token;
|
|
5 |
|
|
6 |
/**
|
|
7 |
* Lexes Groovy, counting braces. Considers itself at end of stream
|
|
8 |
* when the } count exceeds the { count.
|
|
9 |
*
|
|
10 |
* @author Chris Poirier
|
|
11 |
*/
|
|
12 |
|
|
13 |
public class GroovyExpressionLexer extends GroovyLexerBase implements Delimiter |
|
14 |
{ |
|
15 |
|
|
16 |
protected boolean delimited = true; // When true, the lexer can do its delimiting |
|
17 |
protected boolean finished = false; // Set when we reach the delimiter |
|
18 |
protected int balance = 0; // The current number of unmatched open-braces |
|
19 |
|
|
20 |
|
|
21 |
/**
|
|
22 |
* Finds and returns (and consumes) the next token from the underlying stream.
|
|
23 |
* Returns null when out of tokens. We let the GroovyLexerBase version deal
|
|
24 |
* with delegation stuff.
|
|
25 |
*/
|
|
26 |
|
|
27 | 0 |
public Token nextToken() throws ReadException, LexerException |
28 |
{ |
|
29 | 0 |
if( finished )
|
30 |
{ |
|
31 | 0 |
return null; |
32 |
} |
|
33 |
else
|
|
34 |
{ |
|
35 | 0 |
return super.nextToken(); |
36 |
} |
|
37 |
|
|
38 |
} |
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
//---------------------------------------------------------------------------
|
|
43 |
// DELIMITER ROUTINES
|
|
44 |
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Turns delimiting on or off. This should affect <code>la()</code>
|
|
48 |
* and <code>consume()</code>. However, once the delimiter has been
|
|
49 |
* reached, this routine should have no effect.
|
|
50 |
*/
|
|
51 |
|
|
52 | 0 |
public void delimit( boolean delimited ) |
53 |
{ |
|
54 | 0 |
this.delimited = delimited;
|
55 |
} |
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Returns true if the lexer is applying its delimiter policy.
|
|
61 |
*/
|
|
62 |
|
|
63 | 0 |
public boolean isDelimited() |
64 |
{ |
|
65 | 0 |
return this.delimited; |
66 |
} |
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Returns true if the lexer stream is dry.
|
|
72 |
*/
|
|
73 |
|
|
74 | 0 |
public boolean isFinished() |
75 |
{ |
|
76 | 0 |
return finished;
|
77 |
} |
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Restarts the lexer stream after a <code>finish()</code>
|
|
83 |
* and some intevening act (like a new source).
|
|
84 |
*/
|
|
85 |
|
|
86 | 0 |
protected void restart() |
87 |
{ |
|
88 | 0 |
finished = false;
|
89 |
} |
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Stops the lexer stream.
|
|
95 |
*/
|
|
96 |
|
|
97 | 0 |
protected void finish() |
98 |
{ |
|
99 | 0 |
finished = true;
|
100 |
} |
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
//---------------------------------------------------------------------------
|
|
106 |
// DELEGATION
|
|
107 |
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Delegates our duties to another Lexer.
|
|
111 |
*/
|
|
112 |
|
|
113 | 0 |
public void delegate( Lexer to ) |
114 |
{ |
|
115 | 0 |
this.delegate = to;
|
116 | 0 |
delimit( false );
|
117 | 0 |
to.setSource( this );
|
118 |
} |
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Retakes responsibility for our duties.
|
|
124 |
*/
|
|
125 |
|
|
126 | 0 |
public void undelegate() |
127 |
{ |
|
128 | 0 |
if( delegate != null ) |
129 |
{ |
|
130 | 0 |
delegate.unsetSource( ); |
131 | 0 |
delegate = null;
|
132 | 0 |
delimit( true );
|
133 |
} |
|
134 |
} |
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
//---------------------------------------------------------------------------
|
|
139 |
// STREAM ROUTINES
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
/**
|
|
144 |
* Returns the next <code>k</code>th character, without consuming any.
|
|
145 |
*/
|
|
146 |
|
|
147 | 0 |
public char la(int k) throws LexerException, ReadException |
148 |
{ |
|
149 | 0 |
if( source != null ) |
150 |
{ |
|
151 | 0 |
if( delimited )
|
152 |
{ |
|
153 | 0 |
char c = ' ';
|
154 | 0 |
int balance = this.balance; |
155 | 0 |
for( int i = 1; i <= k && balance >= 0; i++ ) |
156 |
{ |
|
157 | 0 |
c = source.la(k); |
158 | 0 |
switch( c )
|
159 |
{ |
|
160 |
case '{':
|
|
161 | 0 |
balance++; |
162 | 0 |
break;
|
163 |
case '}':
|
|
164 | 0 |
balance--; |
165 | 0 |
break;
|
166 |
} |
|
167 |
} |
|
168 |
|
|
169 | 0 |
if( balance >= 0 )
|
170 |
{ |
|
171 | 0 |
return c;
|
172 |
} |
|
173 |
else
|
|
174 |
{ |
|
175 | 0 |
return CharStream.EOS;
|
176 |
} |
|
177 |
|
|
178 |
} |
|
179 |
else
|
|
180 |
{ |
|
181 | 0 |
return source.la(k);
|
182 |
} |
|
183 |
|
|
184 |
} |
|
185 |
else
|
|
186 |
{ |
|
187 | 0 |
return CharStream.EOS;
|
188 |
} |
|
189 |
} |
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
/**
|
|
194 |
* Eats a character from the input stream.
|
|
195 |
*/
|
|
196 |
|
|
197 | 0 |
public char consume() throws LexerException, ReadException |
198 |
{ |
|
199 | 0 |
if( source != null ) |
200 |
{ |
|
201 | 0 |
if( delimited )
|
202 |
{ |
|
203 | 0 |
char c = source.la(1);
|
204 | 0 |
switch( c )
|
205 |
{ |
|
206 |
case '{':
|
|
207 | 0 |
balance++; |
208 | 0 |
break;
|
209 |
case '}':
|
|
210 | 0 |
balance--; |
211 | 0 |
break;
|
212 |
} |
|
213 |
|
|
214 | 0 |
if( balance >= 0 )
|
215 |
{ |
|
216 | 0 |
return source.consume();
|
217 |
} |
|
218 |
else
|
|
219 |
{ |
|
220 | 0 |
finish(); |
221 |
} |
|
222 |
} |
|
223 |
else
|
|
224 |
{ |
|
225 | 0 |
return source.consume();
|
226 |
} |
|
227 |
} |
|
228 |
|
|
229 | 0 |
return CharStream.EOS;
|
230 |
} |
|
231 |
|
|
232 |
} |
|
233 |
|
|