de.unkrig.commons.text.parser
Class AbstractParser<TT extends java.lang.Enum<TT>>

java.lang.Object
  extended by de.unkrig.commons.text.parser.AbstractParser<TT>
Type Parameters:
TT - The enumerator representing the scanner's token types
Direct Known Subclasses:
JsonParser, Parser

public class AbstractParser<TT extends java.lang.Enum<TT>>
extends java.lang.Object

The base class for implementing parsers. Typically, you would declare methods named 'parse...()' which invoke each other and the 'peek...()', 'read...()' and 'peekRead...()' methods to parse a document.


Field Summary
protected  ProducerWhichThrows<AbstractScanner.Token<TT>,ScanException> scanner
          The source of tokens that are processed by this parser.
 
Constructor Summary
AbstractParser(ProducerWhichThrows<AbstractScanner.Token<TT>,ScanException> scanner)
           
 
Method Summary
 void eoi()
           
 AbstractScanner.Token<TT> peek()
          Checks the next token, but does not consume it.
 int peek(java.lang.Object... tokenTypeOrText)
          Checks the next token, but does not consume it.
 boolean peek(java.lang.String text)
          Checks the next token, but does not consume it.
 java.lang.String peek(TT tokenType)
          Checks the next token, but does not consume it.
 int peekRead(java.lang.String... texts)
          Checks the next token and consumes it if its text equals any of texts.
 boolean peekRead(java.lang.String text)
          Checks the next token and consumes it if its text equals text.
 AbstractScanner.Token<TT> peekRead(TT... tokenTypes)
          Checks the next token and consumes it if its type is one of the tokenTypes.
 java.lang.String peekRead(TT tokenType)
          Checks the next token and consumes it if its type is tokenType.
<T extends java.lang.Enum<T>>
T
peekReadEnum(T... values)
          Checks the next token and consumes it if its text equals the return value of toString() of one of the values.
 AbstractScanner.Token<TT> read()
          Consumes the next token.
 int read(java.lang.Object... tokenTypeOrText)
          Consumes the next token.
 void read(java.lang.String text)
          Consumes the next token.
 java.lang.String read(TT tokenType)
          Consumes the next token.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

scanner

protected final ProducerWhichThrows<AbstractScanner.Token<TT extends java.lang.Enum<TT>>,ScanException> scanner
The source of tokens that are processed by this parser.

Constructor Detail

AbstractParser

public AbstractParser(ProducerWhichThrows<AbstractScanner.Token<TT>,ScanException> scanner)
Parameters:
scanner - Its toString() method returns a human-readable indication of the scanner location
Method Detail

peek

@Nullable
public AbstractScanner.Token<TT> peek()
                                                          throws ParseException
Checks the next token, but does not consume it.

Returns:
The next token, or null if the scanner is at end-of-input
Throws:
ParseException

peek

@Nullable
public java.lang.String peek(TT tokenType)
                      throws ParseException
Checks the next token, but does not consume it.

Returns:
The next token's text, or null if the next token's type is not tokenType, or if the scanner is at end-of-input
Throws:
ParseException

peek

public boolean peek(java.lang.String text)
             throws ParseException
Checks the next token, but does not consume it.

Returns:
Whether the scanner is not at end-of-input and the next token's text equals text
Throws:
ParseException

peek

public int peek(java.lang.Object... tokenTypeOrText)
         throws ParseException
Checks the next token, but does not consume it.

Returns:
The index of the first of tokenTypeOrText which equals either the next token's type or text, or -1
Throws:
ParseException

peekRead

@Nullable
public java.lang.String peekRead(TT tokenType)
                          throws ParseException
Checks the next token and consumes it if its type is tokenType.

Returns:
The text of the next token, or null if the next token's type is not tokenType, or if the scanner is at end-of-input
Throws:
ParseException

peekRead

public boolean peekRead(java.lang.String text)
                 throws ParseException
Checks the next token and consumes it if its text equals text.

Returns:
Whether the scanner is not at end-of-input and the next token's text equals text
Throws:
ParseException

peekRead

public int peekRead(java.lang.String... texts)
             throws ParseException
Checks the next token and consumes it if its text equals any of texts.

Returns:
The index of the first of texts which equals the next token's text, or -1
Throws:
ParseException

peekReadEnum

@Nullable
public <T extends java.lang.Enum<T>> T peekReadEnum(T... values)
                                         throws ParseException
Checks the next token and consumes it if its text equals the return value of toString() of one of the values.

Returns:
The matched value, or null if none matches
Throws:
ParseException

peekRead

@Nullable
public AbstractScanner.Token<TT> peekRead(TT... tokenTypes)
                                                              throws ParseException
Checks the next token and consumes it if its type is one of the tokenTypes.

Returns:
The matched token, or null iff none matches
Throws:
ParseException

read

public AbstractScanner.Token<TT> read()
                                                          throws ParseException
Consumes the next token.

Returns:
The next token
Throws:
ParseException - The scanner is at end-of-input

read

public java.lang.String read(TT tokenType)
                      throws ParseException
Consumes the next token.

Returns:
The next token's text
Throws:
ParseException - The next token's type is not tokenType
ParseException - The scanner is at end-of-input

read

public void read(java.lang.String text)
          throws ParseException
Consumes the next token.

Throws:
ParseException - The next token's text does not equal text
ParseException - The scanner is at end-of-input
See Also:
eoi()

read

public int read(java.lang.Object... tokenTypeOrText)
         throws ParseException
Consumes the next token.

Returns:
The index of the first of tokenTypeOrText which equals either the next token's type or text
Throws:
ParseException - Neither the next token's type nor its text equals any of tokenTypeOrText
ParseException - The scanner is at end-of-input

eoi

public void eoi()
         throws ParseException
Throws:
ParseException - Iff the scanner is not at end-of-input