de.unkrig.commons.text.scanner
Class StatefulScanner<TT extends java.lang.Enum<TT>,S extends java.lang.Enum<S>>

java.lang.Object
  extended by de.unkrig.commons.text.scanner.AbstractScanner<TT>
      extended by de.unkrig.commons.text.scanner.StatefulScanner<TT,S>
Type Parameters:
TT - Enumerates the scanner-specific token types
S - Enumerates the scanner-specific non-default states
All Implemented Interfaces:
ProducerWhichThrows<AbstractScanner.Token<TT>,ScanException>, StringScanner<TT>

public class StatefulScanner<TT extends java.lang.Enum<TT>,S extends java.lang.Enum<S>>
extends AbstractScanner<TT>

A scanner that produces AbstractScanner.Tokens. Before produce() is called, the scanner must be configured by invoking its addRule(String, Enum) methods. These define how character sequences are converted into AbstractScanner.Tokens, and also how the scanner changes state. Initially the scanner is in the default state. The non-default states are defined by the S type parameter.

For an example usage, see JavaScanner.


Nested Class Summary
 
Nested classes/interfaces inherited from class de.unkrig.commons.text.scanner.AbstractScanner
AbstractScanner.Token<TT extends java.lang.Enum<TT>>
 
Field Summary
 
Fields inherited from class de.unkrig.commons.text.scanner.AbstractScanner
cs, offset, previousTokenOffset
 
Constructor Summary
StatefulScanner(java.lang.Class<S> states)
           
 
Method Summary
 void addRule(S state, java.lang.String regex, TT tokenType)
          Adds a rule that applies iff the scanner is in the the given state.
 void addRule(S state, java.lang.String regex, TT tokenType, S nextState)
          Adds a rule that applies iff the scanner is in the the given state.
 void addRule(java.lang.String regex, TT tokenType)
          Adds a rule that applies iff the scanner is in the "default state".
 void addRule(java.lang.String regex, TT tokenType, S nextState)
          Adds a rule that applies iff the scanner is in the "default state".
 AbstractScanner.Token<TT> produce()
          Before AbstractScanner.setInput(CharSequence) is called, this method returns null.
 
Methods inherited from class de.unkrig.commons.text.scanner.AbstractScanner
getOffset, getPreviousTokenOffset, setInput, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StatefulScanner

public StatefulScanner(java.lang.Class<S> states)
Method Detail

addRule

public void addRule(java.lang.String regex,
                    TT tokenType)
Adds a rule that applies iff the scanner is in the "default state". After the rule has matched, the scanner remains in the default state.

See Also:
Pattern

addRule

public void addRule(java.lang.String regex,
                    TT tokenType,
                    S nextState)
Adds a rule that applies iff the scanner is in the "default state".

Parameters:
nextState - The new current state after the rule has matched
See Also:
Pattern

addRule

public void addRule(@Nullable
                    S state,
                    java.lang.String regex,
                    TT tokenType)
Adds a rule that applies iff the scanner is in the the given state. The scanner returns to the default state after the rule has matched.

Parameters:
state - null means "any state"
See Also:
Pattern

addRule

public void addRule(@Nullable
                    S state,
                    java.lang.String regex,
                    TT tokenType,
                    S nextState)
Adds a rule that applies iff the scanner is in the the given state.

Parameters:
state - null means "any state"
nextState - The new current state after the rule has matched
See Also:
Pattern

produce

@Nullable
public AbstractScanner.Token<TT> produce()
                                                             throws ScanException
Description copied from class: AbstractScanner
Before AbstractScanner.setInput(CharSequence) is called, this method returns null. After AbstractScanner.setInput(CharSequence) was called, this method breaks the input char character sequence up into tokens and returns them one by one. When the input char sequence is exhausted, null is returned until AbstractScanner.setInput(CharSequence) is called again.

Specified by:
produce in interface ProducerWhichThrows<AbstractScanner.Token<TT extends java.lang.Enum<TT>>,ScanException>
Specified by:
produce in class AbstractScanner<TT extends java.lang.Enum<TT>>
Returns:
null iff the input string is exhausted
Throws:
ScanException