com.hdcookbook.grin.io.text
Class Lexer

java.lang.Object
  extended by com.hdcookbook.grin.io.text.Lexer

public class Lexer
extends java.lang.Object

Read tokens from a file. This is used by the various parsers.

It reads various kinds of tokens on demand, and throws an IOException with a plausible error message on a lexing error. It makes no attempt to recover from errors, so parsers will need to stop on the first error. Methods:

Author:
Bill Foote (http://jovial.com)

Constructor Summary
Lexer(java.io.Reader input, java.lang.String fileName, ShowParser parser)
           
 
Method Summary
 double convertToDouble(java.lang.String tok)
           
 int convertToInt(java.lang.String tok)
           
 void expectString(java.lang.String expected, java.lang.String tok)
          Expect to see a certain string.
 boolean getBoolean()
           
 double getDouble()
           
 java.lang.String getFileName()
          Get the name of the main show file being read.
 int getInt()
           
 int getIntOrInfinite()
           
 int getIntOrOffscreen()
           
 int getLineNumber()
          Get the current line number within the main show file.
 ShowParser getParser()
          Get the main ShowParser we're lexing for.
 java.lang.String getString()
           
 java.lang.String getStringExact()
          Gets the next string that's the exact contents of the next bit of the input stream.
 void parseExpected(java.lang.String expected)
          Parses a token that we expect to see.
 void reportError(java.lang.String msg)
          Report a nice error message with the current line number.
 void reportWarning(java.lang.String msg)
          Give a warning with the current line number
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Lexer

public Lexer(java.io.Reader input,
             java.lang.String fileName,
             ShowParser parser)
Method Detail

getParser

public ShowParser getParser()
Get the main ShowParser we're lexing for. This is useful for extension parsers that want to call back to the main parser, e.g. to parse more complex things, like a color or a forward reference to another feature.


reportError

public void reportError(java.lang.String msg)
                 throws java.io.IOException
Report a nice error message with the current line number.

Throws:
java.io.IOException

reportWarning

public void reportWarning(java.lang.String msg)
Give a warning with the current line number


getLineNumber

public int getLineNumber()
Get the current line number within the main show file. If a file is currently being included, this will be the line number of the include directive.


getFileName

public java.lang.String getFileName()
Get the name of the main show file being read. If a file is currently being included, this won't be that file name.


getInt

public int getInt()
           throws java.io.IOException
Returns:
an int. It's an error to see EOF.
Throws:
java.io.IOException

getDouble

public double getDouble()
                 throws java.io.IOException
Returns:
a double. It's an error to see EOF.
Throws:
java.io.IOException

getIntOrInfinite

public int getIntOrInfinite()
                     throws java.io.IOException
Returns:
an int. Integer.MAX_VALUE for "infinite".
Throws:
java.io.IOException

getIntOrOffscreen

public int getIntOrOffscreen()
                      throws java.io.IOException
Returns:
an int. Translator.OFFSCREEN for "offscreen".
Throws:
java.io.IOException

convertToInt

public int convertToInt(java.lang.String tok)
                 throws java.io.IOException
Throws:
java.io.IOException

convertToDouble

public double convertToDouble(java.lang.String tok)
                       throws java.io.IOException
Throws:
java.io.IOException

getBoolean

public boolean getBoolean()
                   throws java.io.IOException
Returns:
a boolean. It's an error to see EOF.
Throws:
java.io.IOException

getString

public java.lang.String getString()
                           throws java.io.IOException
Returns:
next word, or null on EOF
Throws:
java.io.IOException - on any unexpected characters

getStringExact

public java.lang.String getStringExact()
                                throws java.io.IOException
Gets the next string that's the exact contents of the next bit of the input stream. This will either be a token (like you'd get from getString()), or it's whitespace. Quoted strings aren't recognized.

This method was added to the lexer in order to read the java code between a "[[" and a "]]".

Returns:
next word or next section of whitespace, or null on eof
Throws:
java.io.IOException

parseExpected

public void parseExpected(java.lang.String expected)
                   throws java.io.IOException
Parses a token that we expect to see. A token is read, and if it's not the expected token, an IOException is generated. This can be useful for things like parsing the ";" at the end of various constructs.

Throws:
java.io.IOException

expectString

public void expectString(java.lang.String expected,
                         java.lang.String tok)
                  throws java.io.IOException
Expect to see a certain string. The token passed in is checked, and if it's not the expected token, an IOException is generated. This can be useful for things like parsing the ";" at the end of various constructs.

Throws:
java.io.IOException