Class ReadingFactory

java.lang.Object
eu.bandm.tools.ramus.ogma.Factory
eu.bandm.tools.ramus.ogma.ReadingFactory

public class ReadingFactory extends Factory
Factory with configurable operations for constructing readings.

The construction of parsers is delegated to an encapsulated object of class ParserFactory, with which the configuration is shared.

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Functional interface for constructing binary AST nodes for operators.
    static class 
    Configurable template for binary operator syntax.

    Nested classes/interfaces inherited from class eu.bandm.tools.ramus.ogma.Factory

    Factory.Configuration
  • Field Summary

    Fields inherited from class eu.bandm.tools.ramus.ogma.Factory

    configuration, MODULE_NAME, PACKAGE_PREFIX
  • Method Summary

    Modifier and Type
    Method
    Description
    final <D, T, A> Reading<D,T,A>
    Returns a reading that retrieves the value assigned to a given key as the semantic interpretation.
    final <D, T, A> Reading<D,T,Unit>
    assign(Reading.Key<A> key, A value)
    Returns a reading that assigns the given value to the given key.
    final <D, T, A> Reading<D,T,A>
    choice(Reading<D,T,? extends A>... alts)
    Returns a reading that matches any of the given readings, with the respective semantic interpretation.
    final <D, T, A> Reading<D,T,A>
    choice(List<? extends Reading<D,T,? extends A>> alts)
    Returns a reading that matches any of the given readings, with the respective semantic interpretation.
    final <D, T, A> Reading<D,T,A>
    constant(Parser<D,T> syntax, A value)
    Returns a reading with the given syntax and a constant semantic interpretation.
    final <D, T, A> Reading<D,T,A>
    constant(T type, A value)
    Returns a reading that matches a single token of the given type with a constant semantic interpretation.
    final <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.
    final <D, T, A> Reading<D,T,A>
    constantEpsilon(A value)
    Returns a reading that matches an empty fragment of input with a constant semantic interpretation.
    Creates a new variant of this factory with a modified configuration.
    final <D, T, A> Reading<D,T,A>
    fix(Function<Reading<D,T,A>,Reading<D,T,A>> loop)
    Returns a recursive reading.
    Returns a factory with default configuration (factory settings).
    Returns the parser factory used by this factory.
    final <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.
    final <D, T, A> Reading<D,T,A>
    Returns a reading that uses the given operator template left-associatively.
    final <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.
    final <D, T, A> Reading<D,T,A>
    lookup(Class<A> type, Map<T,? extends A> model)
    Returns a reading that maps the given token types to constant semantic interpretations.
    final <D, T, A> Reading<D,T,A>
    lookup(Map<T,? extends A> model)
    Returns a reading that maps the given token types to constant semantic interpretations.
    final <D, T, A> Reading<D,T,A>
    Returns a reading that uses the given operator template non-associatively.
    final <D, T, A> Reading<D,T,A>
    Returns a reading that uses the given operator template right-associatively.
    final <D, T> Reading<D,T,String>
    text(Collection<? extends T> types)
    Returns a reading that matches a single token of any of the given token types and interprets it as a text string.
    final <D, T> Reading<D,T,String>
    text(T... types)
    Returns a reading that matches a single token of any of the given token types and interprets it as a text string.
    final <D, T> Reading<D,T,Token<D,T>>
    token(Collection<? extends T> types)
    Returns a reading that matches a single token of any of the given token types and interprets it as content.
    final <D, T> Reading<D,T,Token<D,T>>
    token(T... types)
    Returns a reading that matches a single token of any of the given token types and interprets it as a token.
    final <D, T> Reading<D,T,T>
    type(Collection<? extends T> types)
    Returns a reading that matches a single token of any of the given token types and interprets it as a token type.
    final <D, T> Reading<D,T,T>
    type(T... types)
    Returns a reading that matches a single token of any of the given token types and interprets it as a token type.
    final <D, T> Reading<D,T,Unit>
    unit(Parser<D,T> syntax)
    Returns a reading that matches the given syntax with zero-information semantic interpretation.
    final <D, T> Reading<D,T,Unit>
    unit(T... types)
    Returns a reading that matches the given sequence of token types with zero-information semantic interpretation.

    Methods inherited from class eu.bandm.tools.ramus.ogma.Factory

    construct, defaultConfiguration, getConfiguration, serviceFilterByModule, serviceFilterByPackage

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • derive

      Creates a new variant of this factory with a modified configuration.
      Parameters:
      evolution - the modification to apply to the configuration
      Returns:
      a new factory with a modified configuration
      Throws:
      NullPointerException - if evolution is null
    • getParserFactory

      public final ParserFactory getParserFactory()
      Returns the parser factory used by this factory.
      Returns:
      the parser factory used by this factory
    • getDefault

      public static ReadingFactory getDefault()
      Returns a factory with default configuration (factory settings).
      Returns:
      a factory with default configuration
      See Also:
    • token

      @SafeVarargs public final <D, T> Reading<D,T,Token<D,T>> token(T... types)
      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
    • token

      public final <D, T> Reading<D,T,Token<D,T>> token(Collection<? extends T> types)
      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
    • type

      @SafeVarargs public final <D, T> Reading<D,T,T> type(T... types)
      Returns a reading that matches a single token of any of the given token types and interprets it as a token type.
      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 type of the matched token
      Throws:
      NullPointerException - if types is null
    • type

      public final <D, T> Reading<D,T,T> type(Collection<? extends T> types)
      Returns a reading that matches a single token of any of the given token types and interprets it as a token type.
      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 type of the matched token
      Throws:
      NullPointerException - if types is null
    • text

      @SafeVarargs public final <D, T> Reading<D,T,String> text(T... types)
      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

      public final <D, T> Reading<D,T,String> text(Collection<? extends T> types)
      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
    • constant

      public final <D, T, A> Reading<D,T,A> constant(Parser<D,T> syntax, A value)
      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

      public final <D, T, A> Reading<D,T,A> constant(T type, A value)
      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

      public final <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

      public final <D, T, A> Reading<D,T,A> constantEpsilon(A value)
      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

      @SafeVarargs public final <D, T, A> Reading<D,T,A> choice(Reading<D,T,? extends A>... alts)
      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

      public final <D, T, A> Reading<D,T,A> choice(List<? extends Reading<D,T,? extends A>> alts)
      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
    • lookup

      public final <D, T, A> Reading<D,T,A> lookup(Class<A> type, Map<T,? extends A> model)
      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

      public final <D, T, A> Reading<D,T,A> lookup(Map<T,? extends A> model)
      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

      public final <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
    • fix

      public final <D, T, A> Reading<D,T,A> fix(Function<Reading<D,T,A>,Reading<D,T,A>> loop)
      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

      public final <D, T> Reading<D,T,Unit> unit(Parser<D,T> syntax)
      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

      @SafeVarargs public final <D, T> Reading<D,T,Unit> unit(T... types)
      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
      See Also:
    • ifThenElse

      public final <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

      public final <D, T, A> Reading<D,T,A> access(Reading.Key<A> key)
      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
      See Also:
    • assign

      public final <D, T, A> Reading<D,T,Unit> assign(Reading.Key<A> key, A value)
      Returns a reading that assigns the given value to the given key.

      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
      value - the value
      Returns:
      a reading that consumes no input and assigns value to key as a side effect, with the semantic result begin always Unit.unit
      See Also:
    • leftAssoc

      public final <D, T, A> Reading<D,T,A> leftAssoc(ReadingFactory.OperatorTemplate<D,T,A> template)
      Returns a reading that uses the given operator template left-associatively.

      FIXME:

      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 operator template to apply
      Returns:
      a reading that uses template left-associatively
      Throws:
      NullPointerException - if template is null
      See Also:
    • rightAssoc

      public final <D, T, A> Reading<D,T,A> rightAssoc(ReadingFactory.OperatorTemplate<D,T,A> template)
      Returns a reading that uses the given operator template right-associatively.

      FIXME:

      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

      public final <D, T, A> Reading<D,T,A> nonAssoc(ReadingFactory.OperatorTemplate<D,T,A> template)
      Returns a reading that uses the given operator template non-associatively.

      FIXME:

      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
      Throws:
      NullPointerException - if template is null
      See Also: