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.

@FunctionalInterface public interface Pattern
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 Details

    • toPattern

      static Pattern toPattern(Object object)
      A convenience method for easily creating common patters from single objects. If the given object is null, this method returns the 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 a keyword. Otherwise, this method returns the object as a non-terminal symbol.
      Parameters:
      object - the object to be converted into a pattern
      Returns:
      a pattern
    • first

      static ImmutableList<Token> first(ImmutableList<Token> tokens)
      Returns the first token in 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

      static ImmutableList<Token> last(ImmutableList<Token> tokens)
      Returns the last token in 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

      static ImmutableList<Token> clip(ImmutableList<Token> tokens, int length)
      Returns the first tokens from a list as a new list containing exactly those tokens.
      Parameters:
      tokens - the list of tokens from which the first tokens are desired
      length - 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

      ParseTree match(Parser parser, ImmutableList<Token> tokens) throws ParseException
      If a list of tokens is formatted correctly according to this pattern, this method returns a parse tree that can then be given to a builder to create an object; if the tokens do not match this pattern, a parse exception is thrown. The parser which 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 method
      tokens - the tokens to be checked
      Returns:
      a parse tree if the tokens match this pattern
      Throws:
      ParseException - if the tokens do not match this pattern