Interface ParsePattern<T extends Sentence>
- Type Parameters:
T- Sentence type, matchingParser.
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Pattern is a function which consumes Expression Pieces and converts them into a Sentence list.
Patterns either belong to the parser or just exist outside to be used by other patterns. Patterns in the first case will be gone through when trying to parse the whole text. They have IDs by which they can be received from the parser. A parser should have at least one of such a pattern.
Pattern takes some amount of pieces, gives this amount and sentence as result (or null on failure), and leaves the rest of pieces to the code that used the pattern. Hence, that code should check whether all pieces were used when required.
Patterns in parser-related documents should be defined using following pattern notation:
[_] - piece 0 or 1 times <_> - piece once [_]... - piece 0 or more times <_>... - piece 1 or more times _(_) - group of other pattern (used inside brackets)> a|b - either a or b (used inside brackets) ... - anything / et cetera (used inside brackets) -> _ - possible result (used at the end of a pattern)
The written text (denoted as an underscore) can be anything, but should usually display the piece name (as code variable name or part of the piece structure name), their character content (if it is explicit), or other pattern notation. The patterns can be separated by spaces (reflects how the spaces work in the parsing process).
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Sentence>
LinkedList<T>match(List<ExpressionPiece> expressions, ParsePattern<T>... patterns) Match all the given patterns or fail.static <T extends Sentence>
PatternResult<T>matchOne(List<ExpressionPiece> expressions, ParsePattern<T>... patterns) Try to match at least one pattern from the given patterns.parse(List<ExpressionPiece> e) Apply this pattern piece conversion from a given piece list.
-
Method Details
-
parse
Apply this pattern piece conversion from a given piece list.- Parameters:
e- Pieces to parse.- Returns:
PatternResult, or null if pieces do not match the pattern.
-
matchOne
@SafeVarargs static <T extends Sentence> PatternResult<T> matchOne(List<ExpressionPiece> expressions, ParsePattern<T>... patterns) Try to match at least one pattern from the given patterns.- Type Parameters:
T- Sentence type, matchingParser.- Parameters:
expressions- Pieces to parse.patterns- Patterns to match.- Returns:
parse(List), or null if no patterns match.
-
match
@SafeVarargs static <T extends Sentence> LinkedList<T> match(List<ExpressionPiece> expressions, ParsePattern<T>... patterns) throws ParserException Match all the given patterns or fail.- Type Parameters:
T- Sentence type, matchingParser.- Parameters:
expressions- Pieces to parse.patterns- Patterns to match.- Returns:
- List of
matchOne(List, ParsePattern[]). - Throws:
ParserException- If no patterns match at a given position.
-