Class Reading<D,T,A>

java.lang.Object
eu.bandm.tools.ramus.ogma.AbstractExpression<D,T,A>
eu.bandm.tools.ramus.ogma.Reading<D,T,A>
Type Parameters:
D - the type of document identifiers
T - the type of token types
A - the type of parsing results

public class Reading<D,T,A> extends AbstractExpression<D,T,A>
Constructive pairs of syntax and semantic interpretation.
  • Method Details

    • getFactory

      public final ReadingFactory getFactory()
      Returns the factory that has created this reading.
      Returns:
      the factory that has created this reading
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • cast

      public static <D, T, A> Reading<D,T,A> cast(Reading<D,T,? extends A> reading)
      Returns a reference to the given reading that specifies a widened result type.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      reading - the reading to cast
      Returns:
      a reference to reading with result type A
      Throws:
      NullPointerException - if reading is null
    • content

      @Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D, T> Reading<D,T,Token<D,T>> content(T... types)
      Deprecated.
      Returns a reading that matches a single token of any of the given token types and interprets it as a token.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      types - an array of acceptable token types
      Returns:
      a reading that matches a single token whose type is contained in types, with the semantic result being the matched token
      Throws:
      NullPointerException - if types is null
    • content

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Reading<D,T,Token<D,T>> content(Collection<? extends T> types)
      Deprecated.
      Returns a reading that matches a single token of any of the given token types and interprets it as a token.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      types - a collection of acceptable token types
      Returns:
      a reading that matches a single token whose type is contained in types, with the semantic result being the matched token
      Throws:
      NullPointerException - if types is null
    • type

      @Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D, T> Reading<D,T,T> type(T... types)
      Deprecated.
      Returns a reading that matches a single token of any of the given token types and interprets it as a token.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      types - an array of acceptable token types
      Returns:
      a reading that matches a single token whose type is contained in types, with the semantic result being the matched token
      Throws:
      NullPointerException - if types is null
    • type

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Reading<D,T,T> type(Collection<? extends T> types)
      Deprecated.
      Returns a reading that matches a single token of any of the given token types and interprets it as content.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      types - a collection of acceptable token types
      Returns:
      a reading that matches a single token whose type is contained in types, with the semantic result being the matched token
      Throws:
      NullPointerException - if types is null
    • text

      @Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D, T> Reading<D,T,String> text(T... types)
      Deprecated.
      Returns a reading that matches a single token of any of the given token types and interprets it as a text string.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      types - an array of acceptable token types
      Returns:
      a reading that matches a single token whose type is contained in types, with the semantic result being the text of the matched token
      Throws:
      NullPointerException - if types is null
    • text

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Reading<D,T,String> text(Collection<? extends T> types)
      Deprecated.
      Returns a reading that matches a single token of any of the given token types and interprets it as a text string.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      types - a collection of acceptable token types
      Returns:
      a reading that matches a single token whose type is contained in types, with the semantic result being the text of the matched token
      Throws:
      NullPointerException - if types is null
    • map

      public final <B> Reading<D,T,B> map(Function<? super A,? extends B> fun)
      Returns a reading that matches the same syntax as this reading, and applies a function to the result of semantic interpretation.
      Type Parameters:
      B - the transformed type of parsing results
      Parameters:
      fun - the function to transform the results with
      Returns:
      a reading that matches the same syntax as this, and applies fun to the result
      Throws:
      NullPointerException - if fun is null
    • prepend

      public final Reading<D,T,A> prepend(Parser<D,T> prefix)
      Returns a reading that matches the syntax of this reading with a prefix, with the same semantic interpretation. The prefix must be present, but does not contribute to interpretation.
      Parameters:
      prefix - the parser that specifies the prefix
      Returns:
      a reading that matches the syntax of this with prefix prepended
      Throws:
      NullPointerException - if prefix is null
    • append

      public final Reading<D,T,A> append(Parser<D,T> suffix)
      Returns a reading that matches the syntax of this reading with a suffix, with the same semantic interpretation. The suffix must be present, but does not contribute to interpretation.
      Parameters:
      suffix - the parser that specifies the suffix
      Returns:
      a reading that matches the syntax of this with suffix appended
      Throws:
      NullPointerException - if suffix is null
    • prepend

      @SafeVarargs public final Reading<D,T,A> prepend(T... prefix)
      Returns a reading that matches the syntax of this reading with a prefix, with the same semantic interpretation. The prefix is specified by zero or more token types, which are to be matched sequentially. The prefix must be present, but does not contribute to interpretation.

      Specifying zero token types has no effect.

      Parameters:
      prefix - the token types that specify the prefix
      Returns:
      a reading that matches the syntax of this with a sequence of tokens of types prefix prepended
      Throws:
      NullPointerException - if prefix is or contains null
    • append

      @SafeVarargs public final Reading<D,T,A> append(T... suffix)
      Returns a reading that matches the syntax of this reading with a suffix, with the same semantic interpretation. The suffix is specified by zero or more token types, which are to be matched sequentially. The suffix must be present, but does not contribute to interpretation.

      Specifying zero token types has no effect.

      Parameters:
      suffix - the token types that specify the suffix
      Returns:
      a reading that matches the syntax of this with a sequence of tokens of types suffix appended
      Throws:
      NullPointerException - if suffix is or contains null
    • wrap

      public final Reading<D,T,A> wrap(Parser<D,T> prefix, Parser<D,T> suffix)
      Returns a reading that matches the syntax of this reading with a prefix and suffix, with the same semantic interpretation. The prefix and suffix must be present, but do not contribute to interpretation.

      This operation is equivalent to, but may be more convenient than, a combination of prepend(Parser) and append(Parser).

      Parameters:
      prefix - the parser that specifies the prefix
      suffix - the parser that specifies the suffix
      Returns:
      a reading that matches the syntax of this with prefix prepended and suffix appended
      Throws:
      NullPointerException - if prefix or suffix is null
    • wrap

      public final Reading<D,T,A> wrap(T prefix, T suffix)
      Returns a reading that matches the syntax of this reading with a prefix and suffix, with the same semantic interpretation. The prefix and suffix are specified by a single token type each. The prefix and suffix must be present, but do not contribute to interpretation.

      This operation is equivalent to, but may be more convenient than, a combination of prepend(Object[]) and append(Object[]).

      Parameters:
      prefix - the parser that specifies the prefix
      suffix - the parser that specifies the suffix
      Returns:
      a reading that matches the syntax of this with a token of type prefix prepended and a token of type suffix appended
      Throws:
      NullPointerException - if prefix or suffix is null
    • orElse

      public final Reading<D,T,A> orElse(A defaultValue, Parser.Pragma... pragmas)
      Returns a reading that matches the syntax of this reading with the same interpretation, or otherwise matches no input with the given default result value.
      Parameters:
      defaultValue - the default result value for the alternative that matches no input
      pragmas - the pragmas to apply
      Returns:
      a reading that matches the syntax of this with the same result, or otherwise no input with the result defaultValue
    • orElseGet

      public final Reading<D,T,A> orElseGet(Supplier<? extends A> defaultValue, Parser.Pragma... pragmas)
      Returns a reading that matches the syntax of this reading with the same interpretation, or otherwise matches no input with the given default result value.
      Parameters:
      defaultValue - a supplier that can provide the default result value for the alternative that matches no input
      pragmas - the pragmas to apply
      Returns:
      a reading that matches the syntax of this with the same result, or otherwise no input with the result defaultValue.get()
      Throws:
      NullPointerException - if defaultValue is null
    • optionalOrElse

      public final Reading<D,T,Optional<A>> optionalOrElse(T defaultType, Parser.Pragma... pragmas)
      Returns a reading that matches the syntax of this reading with optional interpretation, or otherwise match a single token.

      This method supports the following pragmas:

      • Greedy – always perform the behavior of the parser if the next token is of an acceptable type. Note that the empty alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.
      • Productive – throw an exception if the parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      defaultType - the type of tokens to match alternatively
      pragmas - the pragmas to apply
      Returns:
      a reading that wraps successful interpretations of this reading in Optional.of(), or otherwise consumes a single token of type defaultType and has the result Optional.empty()
      See Also:
    • optionalOrElse

      public final Reading<D,T,Optional<A>> optionalOrElse(Parser<D,T> defaultSyntax, Parser.Pragma... pragmas)
      Returns a reading that matches the syntax of this reading with optional interpretation, or otherwise match some alternative syntax.

      This method supports the following pragmas:

      • Greedy – always perform the behavior of the parser if the next token is of an acceptable type. Note that the empty alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.
      • Productive – throw an exception if the parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      defaultSyntax - the type of tokens to match alternatively
      pragmas - the pragmas to apply
      Returns:
      a reading that wraps successful interpretations of this reading in Optional.of(), or otherwise consumes input as specified by defaultSyntax and has the result Optional.empty()
      See Also:
    • optional

      public final Reading<D,T,Optional<A>> optional(Parser.Pragma... pragmas)
      Returns a reading that matches the syntax of this reading with optional interpretation, or otherwise matches no input.

      This method supports the following pragmas:

      • Greedy – always perform the behavior of the parser if the next token is of an acceptable type. Note that the empty alternative is discarded in this case; use only if the grammar context is such that no possible solution is lost.
      • Productive – throw an exception if the parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.

      Whether input has been matched or not is reflected in the Optional interpretation result.

      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a reading that wraps successful interpretations of this reading in Optional.of(), or otherwise consumes no input and has the result Optional.empty()
      See Also:
    • star

      public final Reading<D,T,List<A>> star(Parser.Pragma... pragmas)
      Returns a reading that matches zero or more repetitions of this reading.

      The interpretation results are collected in a List.

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a reading that behaves equivalently to zero or more repetitions of this reading, and collects the respective results
    • star

      public final Reading<D,T,List<A>> star(Parser<D,T> sep, Parser.Pragma... pragmas)
      Returns a reading that matches zero or more repetitions of this reading with intervening separator syntax.

      The interpretation results are collected in a List. Separators are not interpreted.

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      sep - the parser that specifies the separator syntax
      pragmas - the pragmas to apply
      Returns:
      a reading that behaves equivalently to zero or more repetitions of this reading separated by matches of sep, and collects the respective results
      Throws:
      NullPointerException - if sep is null
    • star

      public final Reading<D,T,List<A>> star(T sep, Parser.Pragma... pragmas)
      Returns a reading that matches zero or more repetitions of this reading with intervening simple separator syntax.

      The interpretation results are collected in a List. Separators must be single tokens of a fixed type, and are not interpreted.

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      sep - the token type of separators
      pragmas - the pragmas to apply
      Returns:
      a reading that behaves equivalently to zero or more repetitions of this reading separated by matches of sep, and collects the respective results
      Throws:
      NullPointerException - if sep is null
    • plus

      public final Reading<D,T,List<A>> plus(Parser.Pragma... pragmas)
      Returns a reading that matches one or more repetitions of this reading.

      The interpretation results are collected in a non-empty List.

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      pragmas - the pragmas to apply
      Returns:
      a reading that behaves equivalently to one or more repetitions of this reading, and collects the respective results
    • plus

      public final Reading<D,T,List<A>> plus(Parser<D,T> sep, Parser.Pragma... pragmas)
      Returns a reading that matches one or more repetitions of this reading with intervening separator syntax.

      The interpretation results are collected in a non-empty List. Separators are not interpreted.

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      sep - the parser that specifies the separator syntax
      pragmas - the pragmas to apply
      Returns:
      a reading that behaves equivalently to one or more repetitions of this reading separated by matches of sep, and collects the respective results
      Throws:
      NullPointerException - if sep is null
    • plus

      public final Reading<D,T,List<A>> plus(T sep, Parser.Pragma... pragmas)
      Returns a reading that matches one or more repetitions of this reading with intervening simple separator syntax.

      The interpretation results are collected in a non-empty List. Separators must be single tokens of a fixed type, and are not interpreted.

      This method supports the following pragmas:

      • Greedy – always perform another iteration of the behavior of this parser if the next token is of an acceptable type.
      • Productive – throw an exception if this parser may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      Parameters:
      sep - the token type of separators
      pragmas - the pragmas to apply
      Returns:
      a reading that behaves equivalently to one or more repetitions of this reading separated by matches of sep, and collects the respective results
      Throws:
      NullPointerException - if sep is null
    • constant

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> constant(Parser<D,T> syntax, A value)
      Deprecated.
      Returns a reading with the given syntax and a constant semantic interpretation.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      syntax - the parser that specifies the syntax
      value - the constant value of the semantic interpretation
      Returns:
      a reading that matches syntax, with the semantic result being always value
      Throws:
      NullPointerException - if syntax or value is null
    • constant

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> constant(T type, A value)
      Deprecated.
      Returns a reading that matches a single token of the given type with a constant semantic interpretation.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      type - the acceptable token type
      value - the constant value of the semantic interpretation
      Returns:
      a reading that matches a single token of type equal to type, with the semantic result being always value
      Throws:
      NullPointerException - if type or value is null
    • constant

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> constant(T type, String text, A value)
      Returns a reading that matches a single token of the given type and text with a constant semantic interpretation.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      type - the acceptable token type
      text - the acceptable token text
      value - the constant value of the semantic interpretation
      Returns:
      a reading that matches a single token of type type and text equal to text, with the semantic result being always value
      Throws:
      NullPointerException - if type, text or value is null
    • constantEpsilon

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> constantEpsilon(A value)
      Deprecated.
      Returns a reading that matches an empty fragment of input with a constant semantic interpretation.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      value - the constant value of the semantic interpretation
      Returns:
      a reading that matches without consuming any input, with the semantic result being always value
      Throws:
      NullPointerException - if value is null
    • choice

      @Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D, T, A> Reading<D,T,A> choice(Reading<D,T,? extends A>... alts)
      Deprecated.
      Returns a reading that matches any of the given readings, with the respective semantic interpretation. The alternatives are tried in order.

      Note that the information which of the alternatives has matched is not recorded.

      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      alts - the alternative readings to combine
      Returns:
      a reading that matches any of alts with the respective semantic interpretation
      Throws:
      NullPointerException - if alts is or contains null
    • choice

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> choice(List<? extends Reading<D,T,? extends A>> alts)
      Deprecated.
      Returns a reading that matches any of the given readings, with the respective semantic interpretation. The alternatives are tried in order.

      Note that the information which of the alternatives has matched is not recorded.

      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      alts - the alternative readings to combine
      Returns:
      a reading that matches any of alts with the respective semantic interpretation
      Throws:
      NullPointerException - if alts is or contains null
    • orElse

      public final Reading<D,T,A> orElse(Reading<D,T,A> other, Parser.Pragma... pragmas)
      Returns a reading that matches alternatively the syntax of this or another reading, with the respective interpretation result.

      This method supports the following pragmas:

      • Productive – throw an exception if both alternatives may succeed without consuming a token; thus creating an ambiguity that cannot be resolved by syntax.
      • Disjoint – return a result only if the choice between the two alternatives can be made deterministically by lookahead; throw an exception otherwise.
      Parameters:
      other - the other reading to attempt
      pragmas - the pragmas to apply
      Returns:
      a reading that behaves as the alternative choice between this and other
      Throws:
      NullPointerException - if other is null
    • lookup

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> lookup(Class<A> type, Map<T,? extends A> model)
      Deprecated.
      Returns a reading that maps the given token types to constant semantic interpretations.

      FIXME: type checking

      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      type - the checked type of semantic values
      model - a map of token types to semantic values
      Returns:
      a reading that matches any token whose type is a key in model, with the semantic result being the corresponding value
      Throws:
      NullPointerException - if type or model is null
    • lookup

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> lookup(Map<T,? extends A> model)
      Deprecated.
      Returns a reading that maps the given token types to constant semantic interpretations.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      model - a map of token types to semantic values
      Returns:
      a reading that matches any token whose type is a key in model, with the semantic result being the corresponding value
      Throws:
      NullPointerException - if model is null
    • lookahead

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> lookahead(Map<T,Reading<D,T,? extends A>> map, Parser.Pragma... pragmas)
      Returns a reading that chooses from the given readings according to the type of the next token.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      map - a map of token types to readings to choose from
      pragmas - the pragmas to apply
      Returns:
      a reading that delegates to a reading chosen from map according to the type of the next token, or fails if there is no matching entry
      Throws:
      NullPointerException - if map is null or contains a null value
    • andThenCombining

      public final <B, C> Reading<D,T,C> andThenCombining(Reading<D,T,B> other, BiFunction<? super A,? super B,? extends C> pair)
      Returns a reading that matches the syntax of this reading followed by that of another, combining the semantic interpretations with a function.
      Type Parameters:
      B - the type of parsing results of the other reading
      C - the type of parsing results of the combined reading
      Parameters:
      other - the other reading to append
      pair - the pairing function to apply to results
      Returns:
      a reading that matches the syntax of this followed by that of other, combining the semantic interpretations by applying pair
      Throws:
      NullPointerException - if other or pair is null
    • mapsTo

      public final <B> Reading<D,T,Map.Entry<A,B>> mapsTo(Reading<D,T,B> other)
      Returns a reading that matches the syntax of this reading followed by that of another, combining the semantic interpretations as a map entry.
      Type Parameters:
      B - the type of parsing results of the other reading
      Parameters:
      other - the other reading to append
      Returns:
      a reading that matches the syntax of this followed by that of other, combining the semantic interpretations as a map entry
      Throws:
      NullPointerException - if other is null
    • andThenDependent

      public final <B> Reading<D,T,B> andThenDependent(Reading<D,T,Function<? super A,? extends B>> other)
      Returns a reading that matches the syntax of this reading followed by that of another, combining the semantic interpretations.

      The other reading must yield a result that is a function, to be applied to the result of this reading.

      Type Parameters:
      B - the type of parsing results of the combined reading
      Parameters:
      other - the other reading to append
      Returns:
      a reading that matches the syntax of this followed by that of other, combining the semantic interpretations by applying the latter result to the former
      Throws:
      NullPointerException - if other is null
    • fix

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> fix(Function<Reading<D,T,A>,Reading<D,T,A>> loop)
      Deprecated.
      Returns a recursive reading.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      loop - a reading construction function to apply recursively
      Returns:
      a reading r that behaves equivalently to loop.apply(r)
      Throws:
      NullPointerException - if loop is null
    • unit

      @Deprecated(forRemoval=false, since="1.0") public static <D, T> Reading<D,T,Unit> unit(Parser<D,T> syntax)
      Deprecated.
      Returns a reading that matches the given syntax with zero-information semantic interpretation.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      syntax - the parser that specifies the syntax
      Returns:
      a reading that matches syntax, with the semantic result being always Unit.unit
      Throws:
      NullPointerException - if syntax is null
    • unit

      @Deprecated(forRemoval=false, since="1.0") @SafeVarargs public static <D, T> Reading<D,T,Unit> unit(T... types)
      Deprecated.
      Returns a reading that matches the given sequence of token types with zero-information semantic interpretation.

      The sequence types may be empty; then no input is consumed.

      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      types - an array of token types to match sequentially
      Returns:
      a reading that matches a sequence of tokens whose types are elementwise equal to types, with the semantic result being always Unit.unit
      Throws:
      NullPointerException - if types is null
    • ifThenElse

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> ifThenElse(T type, Reading<D,T,? extends A> thenBranch, Reading<D,T,? extends A> elseBranch)
      Returns a reading that chooses deterministically between two alternatives depending on the type of the next token.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      type - the expected token type
      thenBranch - the alternative to choose if the next token is of the expected type
      elseBranch - the alternative to choose if the next token is of an unexpected type
      Returns:
      a reading that delegates to thenBranch if the type of the next token is equal to type, or to elseBranch otherwise
      Throws:
      NullPointerException - if thenBranch or elseBranch is null
    • access

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> access(Reading.Key<A> key)
      Deprecated.
      Returns a reading that retrieves the value assigned to a given key as the semantic interpretation.

      No input is consumed.

      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      key - the key
      Returns:
      a reading that consumes no input, with the semantic result being the value assigned to key, or FIXME if unassigned
      Throws:
      NullPointerException - if key is null
    • assigning

      public final Reading<D,T,A> assigning(Reading.Key<? super A> key)
      Returns a reading that assigns the interpretation result of this reading to the given key.

      No input is consumed.

      Parameters:
      key - the key
      Returns:
      a reading that behaves equivalently to this reading, and then assigns the interpretation result to key
      Throws:
      NullPointerException - if key is null
    • withScope

      public final Reading<D,T,A> withScope(Reading.Initializer init)
      Returns a reading that matches the same syntax as this reading, but performs semantic interpretation in a local scope.

      All assignments to keys performed by this reading are undone afterwards.

      Parameters:
      init - a map of initial local values for some keys
      Returns:
      a reading that behaves equivalently to this reading, but performs semantic interpretation in a local scope that is then popped
      Throws:
      NullPointerException - if init is null
      See Also:
    • guard

      public final Reading<D,T,A> guard(Predicate<? super A> condition, Function<? super A,List<SimpleMessage<D>>> error)
      Returns a reading that extends the behavior of this reading with a mandatory check and conditional error diagnostics.
      Parameters:
      condition - the condition to check on the interpretation result
      error - a function to provide diagnostics for the result if the condition fails
      Returns:
      a reading that behaves equivalently to this reading, but then only succeeds if condition returns true for the result value; otherwise diagnostics created by applying error to the value are attached, and the parsing attempt is rejected
      Throws:
      NullPointerException - if condition or error is null
    • diagnose

      @SafeVarargs public final Reading<D,T,A> diagnose(SimpleMessage<D>... msgs)
    • process

      public Reading.Record<D,T,A> process(LookaheadTokenSource<D,T,?> input)
      Parses the given input with this reading and returns a complete summary of interpretations.
      Parameters:
      input - the input to parse
      Returns:
      a complete summary of interpretations
      Throws:
      NullPointerException - if input is null
      See Also:
    • process

      public Reading.Record<D,T,A> process(Input<D,T> input)
      Parses the given input with this reading and returns a complete summary of interpretations.
      Parameters:
      input - the input to parse
      Returns:
      a complete summary of interpretations
      Throws:
      NullPointerException - if input is null
    • leftAssoc

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> leftAssoc(ReadingFactory.OperatorTemplate<D,T,A> template)
      Tactic for left-associative operators.

      The resulting expression will match inputs of the form "e1 op1 ... opn-1 en", and interpret them as an AST of the form opn-1(... op1(e1, e2) ..., en).

      It is assumed that all operands ei conform to the same syntax and interpretation, and that operators are distinguished by token type.

      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      template - the operand template to apply
      Returns:
      a reading that uses template left-associatively
      See Also:
    • rightAssoc

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> rightAssoc(ReadingFactory.OperatorTemplate<D,T,A> template)
      Tactic for right-associative operators.

      The resulting expression will match inputs of the form "e1 op1 ... opn-1 en", and interpret them as an AST of the form op1(e1, ... opn-1(en-1, en)...).

      It is assumed that all operands ei conform to the same syntax and interpretation, and that operators are distinguished by token type.

      Unmixable right-associative operators are currently not supported.

      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      template - the operator template to apply
      Returns:
      a reading that uses template right-associatively
      Throws:
      NullPointerException - if template is null
      UnsupportedOperationException - if template.isMixable() is false
      See Also:
    • nonAssoc

      @Deprecated(forRemoval=false, since="1.0") public static <D, T, A> Reading<D,T,A> nonAssoc(ReadingFactory.OperatorTemplate<D,T,A> template)
      Tactic for non-associative operators.

      The resulting expression will match inputs of the form "e1 op e2", and interpret them as an AST of the form op(e1, e2).

      It is assumed that all operands ei conform to the same syntax and interpretation, and that operators are distinguished by token type.

      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      A - the type of parsing results
      Parameters:
      template - the operator template to apply
      Returns:
      a reading that uses template non-associatively
      See Also:
    • withOtherTokenSource

      public final <K> Reading<D,T,A> withOtherTokenSource(LookaheadTokenMultiplexer<K,D,T,?> multi, K newKey)
      Extends the behavior of this reading by switching input token sources.

      Note that token source switching is only supported in situations where no lookahead tokens have been acquired. IllegalStateException will be thrown if lookahead tokens are present when the switch is performed.

      Type Parameters:
      K - the type of token multiplexer keys
      Parameters:
      multi - the token multiplexer to use for switching
      newKey - the key to switch the token source to during parsing with the extended parser
      Returns:
      a reading that behaves equivalently to this reading, but switches to the token source associated with newKey before parsing, and switches back afterwards, on both success and failure
      Throws:
      NullPointerException - if multi or newKey is null
    • closeAndTerminate

      public final Reading<D,T,A> closeAndTerminate(T endType, Parser.Pragma... pragmas)
    • closeAndRepeat

      public final Reading<D,T,A> closeAndRepeat(Parser.Pragma... pragmas)