Interface Pattern
- All Known Implementing Classes:
Keyword, List, Lookahead, NonTerminal, Selection, Sentinel, Single, Symbol, Terminal
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
A pattern determines whether a sequence of
tokens matches the
expected format to be parsed as an object. A pattern's primary method is
match(Parser, ImmutableList), which returns a parse tree if the tokens are formatted correctly or throws a parse exception if they are not.- Author:
- Stephen G. Ware
-
Method Summary
Modifier and TypeMethodDescriptionstatic ImmutableList<Token> clip(ImmutableList<Token> tokens, int length) Returns the firsttokensfrom a list as a new list containing exactly those tokens.static ImmutableList<Token> first(ImmutableList<Token> tokens) Returns the firsttokenin a list as a new list containing exactly that one token.static ImmutableList<Token> last(ImmutableList<Token> tokens) Returns the lasttokenin a list as a new list containing exactly that one token.match(Parser parser, ImmutableList<Token> tokens) If a list oftokensis formatted correctly according to this pattern, this method returns aparse treethat can then be given to abuilderto create an object; if the tokens do not match this pattern, aparse exceptionis thrown.static PatternA convenience method for easily creating common patters from single objects.
-
Method Details
-
toPattern
A convenience method for easily creating common patters from single objects. If the given object is null, this method returnsthe nothing terminal symbol. If the object is already a pattern, this method casts and returns it. If the object is a String, this method returns akeyword. Otherwise, this method returns the object as anon-terminal symbol.- Parameters:
object- the object to be converted into a pattern- Returns:
- a pattern
-
first
Returns the firsttokenin a list as a new list containing exactly that one token.- Parameters:
tokens- the list of tokens from which the first token is desired- Returns:
- a new list containing only the first token from the list, or null if the list was empty
-
last
Returns the lasttokenin a list as a new list containing exactly that one token.- Parameters:
tokens- the list of tokens from which the last token is desired- Returns:
- a new list containing only the last token from the list, or null if the list was empty
-
clip
Returns the firsttokensfrom a list as a new list containing exactly those tokens.- Parameters:
tokens- the list of tokens from which the first tokens are desiredlength- the number of first tokens desired- Returns:
- a new list containing only the given number of first tokens, or as many first tokens as were available, or an empty list if the given list was empty
-
match
If a list oftokensis formatted correctly according to this pattern, this method returns aparse treethat can then be given to abuilderto create an object; if the tokens do not match this pattern, aparse exceptionis thrown. Theparserwhich called this method is given as an argument so that it can be used to parse other patterns inside this pattern.- Parameters:
parser- the parse which called this methodtokens- the tokens to be checked- Returns:
- a
parse treeif the tokens match this pattern - Throws:
ParseException- if the tokens do not match this pattern
-