1 package org.codehaus.groovy.syntax.lexer; 2 3 import org.codehaus.groovy.syntax.ReadException; 4 import org.codehaus.groovy.syntax.Types; 5 import org.codehaus.groovy.syntax.Token; 6 import org.codehaus.groovy.GroovyBugError; 7 8 9 17 18 public class LexerBase implements Lexer 19 { 20 21 protected int startLine; protected int startColumn; 24 protected Lexer delegate = null; protected Lexer source = null; 27 28 29 32 33 public LexerBase( ) 34 { 35 } 36 37 38 39 43 44 public Lexer getDelegate() 45 { 46 return delegate; 47 } 48 49 50 51 54 55 public Lexer getSource() 56 { 57 return source; 58 } 59 60 61 62 68 69 public Token nextToken() throws ReadException, LexerException 70 { 71 74 if( delegate != null ) 75 { 76 Token next = delegate.nextToken(); 77 78 if( next == null ) 79 { 80 undelegate(); 81 } 82 else 83 { 84 return next; 85 } 86 87 } 88 89 mark(); 90 return undelegatedNextToken(); 91 } 92 93 94 95 99 100 protected Token undelegatedNextToken() throws ReadException, LexerException 101 { 102 return null; 103 } 104 105 106 107 108 111 112 116 117 protected Token tokenizeEOL() throws LexerException, ReadException 118 { 119 Token token = null; 120 121 char c = la(); 122 switch( c ) 123 { 124 case '\r': 125 case '\n': 126 token = symbol( Types.NEWLINE ); 127 128 consume(); 129 if (c == '\r' && la() == '\n') 130 { 131 consume(); 132 } 133 } 134 135 return token; 136 } 137 138 139 140 144 145 protected boolean readEOL( StringBuffer destination ) throws LexerException, ReadException 146 { 147 boolean read = false; 148 149 char c = la(); 150 switch( c ) 151 { 152 case '\r': 153 case '\n': 154 if( destination == null ) 155 { 156 consume(); 157 if (c == '\r' && la() == '\n') 158 { 159 consume(); 160 } 161 } 162 else 163 { 164 destination.append( consume() ); 165 if (c == '\r' && la() == '\n') 166 { 167 destination.append( consume() ); 168 } 169 } 170 171 read = true; 172 } 173 174 return read; 175 } 176 177 178 179 182 183 protected void readEOL() throws LexerException, ReadException 184 { 185 readEOL( null ); 186 } 187 188 189 190 191 192 195 196 199 200 public void reset() 201 { 202 delegate = null; 203 source = null; 204 } 205 206 207 208 211 212 public void delegate( Lexer to ) 213 { 214 this.delegate = to; 215 to.setSource( this ); 216 } 217 218 219 220 223 224 public void undelegate() 225 { 226 if( delegate != null ) 227 { 228 delegate.unsetSource( ); 229 delegate = null; 230 } 231 } 232 233 234 235 238 239 public void setSource( Lexer source ) 240 { 241 if( source == null ) 242 { 243 throw new GroovyBugError( "use unsetSource() to remove a source from a lexer" ); 244 } 245 this.source = source; 246 } 247 248 249 250 253 254 public void unsetSource() 255 { 256 this.source = null; 257 } 258 259 260 261 264 265 public boolean isDelegated() 266 { 267 return delegate != null; 268 } 269 270 271 272 276 277 public boolean isExternallySourced() 278 { 279 return source != null; 280 } 281 282 283 284 285 288 289 292 293 protected void unexpected( char c, int offset, String message ) throws UnexpectedCharacterException 294 { 295 throw new UnexpectedCharacterException( getStartLine(), getStartColumn() + offset, c, message ); 296 } 297 298 299 300 303 304 protected void unexpected( char c, char[] expected, int offset ) throws UnexpectedCharacterException 305 { 306 throw new UnexpectedCharacterException( getStartLine(), getStartColumn() + offset, c, expected ); 307 } 308 309 310 311 314 315 protected void unexpected( char c, int offset ) throws UnexpectedCharacterException 316 { 317 unexpected( c, null, offset ); 318 } 319 320 321 322 323 326 327 331 332 protected Token symbol( int type, int columnOffset ) 333 { 334 return Token.newSymbol( type, getStartLine(), getStartColumn() - columnOffset ); 335 } 336 337 338 339 342 343 protected Token symbol( int type ) 344 { 345 return Token.newSymbol( type, getStartLine(), getStartColumn() ); 346 } 347 348 349 350 351 354 355 358 359 public int getLine() 360 { 361 if( source != null ) 362 { 363 return source.getLine(); 364 } 365 366 return -1; 367 } 368 369 370 371 374 375 public int getColumn() 376 { 377 if( source != null ) 378 { 379 return source.getColumn(); 380 } 381 382 return -1; 383 } 384 385 386 387 390 391 protected void mark() 392 { 393 startLine = getLine(); 394 startColumn = getColumn(); 395 } 396 397 398 399 402 403 protected int getStartLine() 404 { 405 return this.startLine; 406 } 407 408 409 410 413 414 protected int getStartColumn() 415 { 416 return this.startColumn; 417 } 418 419 420 421 424 425 public char la() throws LexerException, ReadException 426 { 427 return la(1); 428 } 429 430 431 432 435 436 public char la(int k) throws LexerException, ReadException 437 { 438 if( source != null ) 439 { 440 return source.la(k); 441 } 442 else 443 { 444 return CharStream.EOS; 445 } 446 } 447 448 449 450 453 454 public char consume() throws LexerException, ReadException 455 { 456 if( source != null ) 457 { 458 return source.consume(); 459 } 460 else 461 { 462 return CharStream.EOS; 463 } 464 } 465 466 467 } 468 | Popular Tags |