View Javadoc

    1   package org.codehaus.groovy.syntax.lexer;
    2
    3   import org.codehaus.groovy.syntax.ReadException;
    4
    5   /***
    6    *  A base class for Lexers that process embedded text.
    7    *
    8    *  @author Chris Poirier
    9    */
    10
    11  public class TextLexerBase extends LexerBase implements Delimiter
    12  {
    13
    14      protected boolean delimited = true;   // When true, the lexer can do its delimiting
    15      protected boolean finished  = true;   // When true, the lexer is dry
    16
    17
    18     /***
    19      *  Turns delimiting on or off.  This should affect <code>la()</code>
    20      *  and <code>consume()</code>.  However, once the delimiter has been
    21      *  reached, this routine should have no effect.
    22      */
    23
    24      public void delimit( boolean delimited )
    25      {
    26          this.delimited = delimited;
    27      }
    28
    29
    30
    31     /***
    32      *  Returns true if the lexer is applying its delimiter policy.
    33      */
    34
    35      public boolean isDelimited()
    36      {
    37          return this.delimited;
    38      }
    39
    40
    41
    42     /***
    43      *  Returns true if the lexer stream is dry.
    44      */
    45
    46      public boolean isFinished()
    47      {
    48          return finished;
    49      }
    50
    51
    52
    53     /***
    54      *  Restarts the lexer stream after a <code>finish()</code>
    55      *  and some intevening act (like a new source).
    56      */
    57
    58      protected void restart()
    59      {
    60          finished = false;
    61      }
    62
    63
    64
    65     /***
    66      *  Stops the lexer stream.
    67      */
    68
    69      protected void finish()
    70      {
    71          finished = true;
    72      }
    73
    74
    75
    76
    77
    78    //---------------------------------------------------------------------------
    79    // STREAM ROUTINES
    80
    81
    82     /***
    83      *  Returns the next <code>k</code>th character, without consuming any.
    84      */
    85
    86      public char la(int k) throws LexerException, ReadException
    87      {
    88          if( finished )
    89          {
    90              return CharStream.EOS;
    91          }
    92          else if( source != null )
    93          {
    94              return source.la(k);
    95          }
    96          else
    97          {
    98              return CharStream.EOS;
    99          }
    100     }
    101
    102
    103
    104    /***
    105     *  Eats a character from the input stream.
    106     */
    107
    108     public char consume() throws LexerException, ReadException
    109     {
    110         if( finished )
    111         {
    112             return CharStream.EOS;
    113         }
    114         else if( source != null )
    115         {
    116             return source.consume();
    117         }
    118         else
    119         {
    120             return CharStream.EOS;
    121         }
    122     }
    123
    124 }