Interface Parser<E>

  • Type Parameters:
    E - the type of parse result.
    All Superinterfaces:
    Function<FList<String>,​Result<E>>
    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 Parser<E>
    extends Function<FList<String>,​Result<E>>
    Functional interface for parsers.
    Author:
    Herbert Praehofer
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Parser<String> anyToken
      A parser parsing any token.
    • Method Summary

      All Methods Static Methods Instance Methods Default Methods 
      Modifier and Type Method Description
      static <R> Parser<R> alt​(Parser<? extends R>... alternatives)
      Creates a parser which tries to apply one of the given alternative parsers.
      static <R> Parser<R> alt​(Supplier<Parser<? extends R>>... pSuppliers)
      Creates a parser which tries to apply one of the alternative parsers.
      default Parser<E> filter​(Predicate<? super E> pred)
      Creates a new parser from this parser which is only successful if the parse result fulfills the given predicate.
      default <R> Parser<R> map​(Function<? super E,​? extends R> f)
      Creates a new parser from this parser where the parse result is mapped by the given function.
      static <R> Parser<FList<R>> mult​(Parser<R> parser)
      Creates a parser which applies the given parser as long as it is successful.
      static <R> Parser<Optional<R>> opt​(Parser<R> parser)
      Creates a parser which optionally applies the given parser.
      default <S> Parser<Pair<E,​S>> seq​(Parser<S> second)
      Creates a new parser which applies this parser and the second parser and combined both results in a Pair.
      static Parser<String> token​(String tkn)
      A parser parsing a token if it is equal to the given string.
    • Field Detail

      • anyToken

        static final Parser<String> anyToken
        A parser parsing any token.
    • Method Detail

      • filter

        default Parser<E> filter​(Predicate<? super E> pred)
        Creates a new parser from this parser which is only successful if the parse result fulfills the given predicate.
        Parameters:
        pred - the predicate to test the parse result
        Returns:
        the new parser
      • map

        default <R> Parser<R> map​(Function<? super E,​? extends R> f)
        Creates a new parser from this parser where the parse result is mapped by the given function.
        Type Parameters:
        R - the type of the parse result of the new parser
        Parameters:
        f - the mapping function
        Returns:
        the new parser
      • seq

        default <S> Parser<Pair<E,​S>> seq​(Parser<S> second)
        Creates a new parser which applies this parser and the second parser and combined both results in a Pair.
        Type Parameters:
        S - the type of the parse result of the second parser
        Parameters:
        second - the second parser
        Returns:
        the combined parser
      • alt

        @SafeVarargs
        static <R> Parser<R> alt​(Parser<? extends R>... alternatives)
        Creates a parser which tries to apply one of the given alternative parsers.
        Type Parameters:
        R - the common type of the parse results of the alternative parsers.
        Parameters:
        alternatives - the alternative parsers
        Returns:
        the new parser
      • alt

        @SafeVarargs
        static <R> Parser<R> alt​(Supplier<Parser<? extends R>>... pSuppliers)
        Creates a parser which tries to apply one of the alternative parsers. The alternative parsers are created by the given supplier functions.
        Type Parameters:
        R - the common type of the parse results of the alternative parsers.
        Parameters:
        pSuppliers - the supplier functions for the alternative parsers
        Returns:
        the new parser
      • mult

        static <R> Parser<FList<R>> mult​(Parser<R> parser)
        Creates a parser which applies the given parser as long as it is successful. The parse result is a list of the parse results of the multiple applications.
        Type Parameters:
        R - the type of the parse results of the given parser
        Parameters:
        parser - the parser to apply multiple times
        Returns:
        the parser applying the given parser multiple times
      • opt

        static <R> Parser<Optional<R>> opt​(Parser<R> parser)
        Creates a parser which optionally applies the given parser. The result of the created parser is an optional. If the parser is successful, then the parse result is returned in an optional; otherwise the optional is empty and no token is consumed. The parser is always successful but possibly with an empty result.
        Type Parameters:
        R - the type of the parse result
        Parameters:
        parser - the parser to optionally apply
        Returns:
        the optional parser
      • token

        static Parser<String> token​(String tkn)
        A parser parsing a token if it is equal to the given string.
        Parameters:
        tkn - the token to parse
        Returns:
        the parser parsing a token which is equal to tkn.