Class TextStateParser<M>
- Type Parameters:
M- the type of the model object populated during parsing
- Direct Known Subclasses:
PresentationParser
This parser processes a list of strings (typically lines of a text file) in two sequential phases:
- Headers phase: The parser iterates over lines at the beginning
of the input and passes each non-empty trimmed line to a set of
TextStateParser.HeaderLinematchers. As long as at least one matcher accepts the line, the parser stays in this phase. The first line that is not accepted by any header matcher ends the headers phase, and this same line is immediately handed over to the next phase without being discarded. - States phase: Each line (starting from the one that terminated
the headers phase) is checked against a set of
TextStateParser.NewStateLinematchers to detect state boundaries. If a matcher returns a newTextStateParser.State, the previous state iscommittedand the new state becomes active. Otherwise, the line is dispatched toonLineof the current state.
The generic type parameter <M> represents the model object that
accumulates parsing results. It is passed to all callbacks and is expected
to be mutated during parsing. The same model instance is shared across all
phases, states, and matchers.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceMatches a line as a header entry during the headers phase.static interfaceDetects a state boundary and creates the correspondingTextStateParser.State.static interfaceRepresents a parsing state that processes lines belonging to a specific section of the input. -
Constructor Summary
ConstructorsConstructorDescriptionTextStateParser(M model, TextStateParser.State<M> initialState, List<TextStateParser.HeaderLine<M>> headerLines, List<TextStateParser.NewStateLine<M>> newStateLines) Creates a new parser. -
Method Summary
-
Constructor Details
-
TextStateParser
public TextStateParser(M model, TextStateParser.State<M> initialState, List<TextStateParser.HeaderLine<M>> headerLines, List<TextStateParser.NewStateLine<M>> newStateLines) Creates a new parser.- Parameters:
model- the model to populate during parsing, must not benullinitialState- the initial state to use before any transition occurs, must not benullheaderLines- the list of header matchers; ifnull, an empty list is usednewStateLines- the list of state-boundary matchers; ifnull, an empty list is used- Throws:
NullPointerException- ifmodelorinitialStateisnull
-
-
Method Details
-
parse
Executes the two-phase parsing over the given list of lines.The method first enters the headers phase: non-empty trimmed lines are offered to
TextStateParser.HeaderLinematchers. The first line not recognised as a header terminates the headers phase and is immediately processed in the states phase.During the states phase, each trimmed line is checked against
TextStateParser.NewStateLinematchers. If a matcher returns a newTextStateParser.State, the current state is committed and the new state becomes active. Otherwise, the original (untrimmed) line is dispatched to the current state viaonLine. After all lines are exhausted, the last active state is committed.- Parameters:
lines- the lines to parse
-