edu.cornell.cs.sam.io
Class SamTokenizer

java.lang.Object
  extended by edu.cornell.cs.sam.io.SamTokenizer
All Implemented Interfaces:
Tokenizer

public class SamTokenizer
extends java.lang.Object
implements Tokenizer

This is a SamTokenizer implementation that has full backwards/forwards mobility and loads everything into memory as soon as it is created. By default, quote processing is disabled and comment ignoring is enabled. You may use one of the constructors with options to enabled/disable this options


Nested Class Summary
static class SamTokenizer.TokenizerOptions
           
 
Nested classes/interfaces inherited from interface edu.cornell.cs.sam.io.Tokenizer
Tokenizer.TokenType
 
Constructor Summary
SamTokenizer(java.io.Reader r, SamTokenizer.TokenizerOptions... opt)
          Creates a new SamTokenizer with a Reader as a source
SamTokenizer(SamTokenizer.TokenizerOptions... opt)
          Creates a new SamTokenizer with System.in as a source
SamTokenizer(java.lang.String FileName, SamTokenizer.TokenizerOptions... opt)
          Creates a new SamTokenizer with a file for a source
 
Method Summary
 boolean canPushBack()
          Checks if the stream can be pushed back
 boolean check(char c)
          Removes the next token if it is an operator, and matches the character c.
 boolean check(java.lang.String s)
          Removes the next token if it is a word, and matches the string s.
 void close()
          Closes the input stream
 char getCharacter()
          Returns the next token (unless it is not a character)
 java.lang.String getComment()
          Returns the next token (unless it is not a comment)
 float getFloat()
          Returns the next token (unless it is not a float)
 int getInt()
          Returns the next token (unless it is not an integer)
 char getOp()
          Returns the next token (unless it is not an operator)
 java.lang.String getString()
          Returns the next token (unless it is not a string)
 java.lang.String getWhitespaceBeforeToken()
          Returns the whitespace in the file before the current token.
 java.lang.String getWord()
          Returns the next token (unless it is not a word)
 int lineNo()
          Returns the line number of the last token requested
 void match(char c)
          Removes the next token if it is an operator, and matches the given character.
 void match(java.lang.String s)
          Removes the next token if it is a word, and matches the given string.
 int nextLineNo()
          Returns the line number of the next token to be read
 Tokenizer.TokenType peekAtKind()
          Returns the type of the next token
 void pushBack()
          Pushes the last token requested back.
 void skipToken()
          Skips the next token
 boolean test(char c)
          Checks if the next token is an operator and matches the character c.
 boolean test(java.lang.String s)
          Checks if the next token is a word and matches the string s.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SamTokenizer

public SamTokenizer(java.lang.String FileName,
                    SamTokenizer.TokenizerOptions... opt)
             throws java.io.IOException,
                    java.io.FileNotFoundException,
                    TokenParseException
Creates a new SamTokenizer with a file for a source

Parameters:
FileName - The file name of the file to read
opt - Options for parsing
Throws:
java.io.IOException - If there is a file error
java.io.FileNotFoundException - If the file could not be found
TokenParseException

SamTokenizer

public SamTokenizer(java.io.Reader r,
                    SamTokenizer.TokenizerOptions... opt)
             throws java.io.IOException,
                    TokenParseException
Creates a new SamTokenizer with a Reader as a source

Parameters:
r - The source
opt - Options for parsing
Throws:
java.io.IOException - if there is a stream error
TokenParseException

SamTokenizer

public SamTokenizer(SamTokenizer.TokenizerOptions... opt)
             throws java.io.IOException,
                    TokenParseException
Creates a new SamTokenizer with System.in as a source

Parameters:
opt - Options for parsing
Throws:
java.io.IOException - if there is a stream error
TokenParseException
Method Detail

peekAtKind

public Tokenizer.TokenType peekAtKind()
Description copied from interface: Tokenizer
Returns the type of the next token

Specified by:
peekAtKind in interface Tokenizer
Returns:
the type of the next token

getInt

public int getInt()
           throws TokenizerException
Description copied from interface: Tokenizer
Returns the next token (unless it is not an integer)

Specified by:
getInt in interface Tokenizer
Returns:
the integer expected
Throws:
TokenizerException - if the next token is not an integer

getFloat

public float getFloat()
               throws TokenizerException
Description copied from interface: Tokenizer
Returns the next token (unless it is not a float)

Specified by:
getFloat in interface Tokenizer
Returns:
the floating point number expected
Throws:
TokenizerException - if the next token is not a float

getWord

public java.lang.String getWord()
                         throws TokenizerException
Description copied from interface: Tokenizer
Returns the next token (unless it is not a word)

Specified by:
getWord in interface Tokenizer
Returns:
the word expected
Throws:
TokenizerException - if the next token is not a word

getString

public java.lang.String getString()
                           throws TokenizerException
Description copied from interface: Tokenizer
Returns the next token (unless it is not a string)

Specified by:
getString in interface Tokenizer
Returns:
the string expected
Throws:
TokenizerException - if the next token is not a string

getCharacter

public char getCharacter()
                  throws TokenizerException
Description copied from interface: Tokenizer
Returns the next token (unless it is not a character)

Specified by:
getCharacter in interface Tokenizer
Returns:
the character expected
Throws:
TokenizerException - if the next token is not a character

getOp

public char getOp()
           throws TokenizerException
Description copied from interface: Tokenizer
Returns the next token (unless it is not an operator)

Specified by:
getOp in interface Tokenizer
Returns:
the operator expected
Throws:
TokenizerException - if the next token is not an operator

getComment

public java.lang.String getComment()
                            throws TokenizerException
Description copied from interface: Tokenizer
Returns the next token (unless it is not a comment)

Specified by:
getComment in interface Tokenizer
Returns:
the comment expected
Throws:
TokenizerException - if the next token is not a comment

match

public void match(char c)
           throws TokenizerException
Description copied from interface: Tokenizer
Removes the next token if it is an operator, and matches the given character. Otherwise, throws TokenizerException.

Specified by:
match in interface Tokenizer
Parameters:
c - the character to match
Throws:
TokenizerException - if the next token is not c, or is not an operator token.

match

public void match(java.lang.String s)
           throws TokenizerException
Description copied from interface: Tokenizer
Removes the next token if it is a word, and matches the given string. Otherwise, throws TokenizerException.

Specified by:
match in interface Tokenizer
Parameters:
s - the string to match
Throws:
TokenizerException - if the next token is not s, or is not a word token

check

public boolean check(char c)
Description copied from interface: Tokenizer
Removes the next token if it is an operator, and matches the character c. Otherwise, returns false.

Specified by:
check in interface Tokenizer
Parameters:
c - the character to match
Returns:
true if the next token is an operator and matches c, false otherwise

check

public boolean check(java.lang.String s)
Description copied from interface: Tokenizer
Removes the next token if it is a word, and matches the string s. Otherwise, returns false.

Specified by:
check in interface Tokenizer
Parameters:
s - the string to match
Returns:
true if the next token is a word, and matches s, false otherwise

test

public boolean test(char c)
Description copied from interface: Tokenizer
Checks if the next token is an operator and matches the character c. Otherwise, returns false. The token is not removed.

Specified by:
test in interface Tokenizer
Parameters:
c - the character to match
Returns:
true if the next token is an operator, and matches c, false otherwise

test

public boolean test(java.lang.String s)
Description copied from interface: Tokenizer
Checks if the next token is a word and matches the string s. The token is not removed.

Specified by:
test in interface Tokenizer
Parameters:
s - the string to match
Returns:
true if the next token is a word, and matches s, false otherwise

pushBack

public void pushBack()
Description copied from interface: Tokenizer
Pushes the last token requested back. Implementations are required to be able to push back to the start of the file.

Specified by:
pushBack in interface Tokenizer

canPushBack

public boolean canPushBack()
Description copied from interface: Tokenizer
Checks if the stream can be pushed back

Specified by:
canPushBack in interface Tokenizer
Returns:
true if it can be pushed back, false if at the beginning of the file

getWhitespaceBeforeToken

public java.lang.String getWhitespaceBeforeToken()
Description copied from interface: Tokenizer
Returns the whitespace in the file before the current token. This may be null

Specified by:
getWhitespaceBeforeToken in interface Tokenizer
Returns:
the whitespace string

lineNo

public int lineNo()
Description copied from interface: Tokenizer
Returns the line number of the last token requested

Specified by:
lineNo in interface Tokenizer
Returns:
the line number of the last token

nextLineNo

public int nextLineNo()
Description copied from interface: Tokenizer
Returns the line number of the next token to be read

Specified by:
nextLineNo in interface Tokenizer
Returns:
the line number of the next token

close

public void close()
Description copied from interface: Tokenizer
Closes the input stream

Specified by:
close in interface Tokenizer

skipToken

public void skipToken()
Description copied from interface: Tokenizer
Skips the next token

Specified by:
skipToken in interface Tokenizer