Class Grammar

  • All Implemented Interfaces:
    Serializable

    public class Grammar
    extends Object
    implements Serializable

    A grammar representing the rules for parsing. Rules are defined using ParserHandlers, which are associated with individual Tags. There are two main types of handlers, prefix and postfix. The PrefixHandler takes care of prefix operators and the PostfixHandler handles infix and postfix operators.

    By default a grammar will throw a ParseException when it encounters a token type that it does not know how to handle. Grammars can be set to instead ignore these tokens.

    Author:
    David B. Bracewell
    See Also:
    Serialized Form
    • Constructor Detail

      • Grammar

        public Grammar()
    • Method Detail

      • getPostfixHandler

        public Optional<PostfixHandler> getPostfixHandler​(ParserToken token)
        Gets the postfix handler associated with the Tag of the given ParserToken
        Parameters:
        token - the token to use to determine the correct PostfixHandler to retrieve
        Returns:
        An Optional containing the postfix handler if available or empty if none are available
      • getPostfixHandler

        public Optional<PostfixHandler> getPostfixHandler​(Tag tag)
        Gets the postfix handler associated with the given Tag.
        Parameters:
        tag - the tag representing the operator / value for which we want to retrieve a PostfixHandler
        Returns:
        An Optional containing the postfix handler if available or empty if none are available
      • getPrefixHandler

        public Optional<PrefixHandler> getPrefixHandler​(ParserToken token)
        Gets the prefix handler associated with the Tag of the given ParserToken
        Parameters:
        token - the token to use to determine the correct PrefixHandler to retrieve
        Returns:
        An Optional containing the prefix handler if available or empty if none are available
      • getPrefixHandler

        public Optional<PrefixHandler> getPrefixHandler​(Tag tag)
        Gets the prefix handler associated with the given Tag.
        Parameters:
        tag - the tag representing the operator / value for which we want to retrieve a PrefixHandler
        Returns:
        An Optional containing the postfix handler if available or empty if none are available
      • isIgnored

        public boolean isIgnored​(ParserToken token)
        Checks if the given token should be ignored during parsing
        Parameters:
        token - the token
        Returns:
        True - if should be ignored, False otherwise
      • isIgnored

        public boolean isIgnored​(Tag tag)
        Checks if the given tag should be ignored during parsing
        Parameters:
        tag - the tag
        Returns:
        True - if should be ignored, False otherwise
      • postfix

        public Grammar postfix​(Tag tag,
                               PostfixHandler handler)
        Registers a PostfixHandler with a precedence of 1 for the given Tag
        Parameters:
        tag - the tag for which the handler will be registered
        handler - the handler to associate with the given tag
        Returns:
        the grammar
      • postfix

        public Grammar postfix​(Tag tag,
                               PostfixHandler handler,
                               int precedence)
        Registers a PostfixHandler with the given precedence for the given Tag
        Parameters:
        tag - the tag for which the handler will be registered
        handler - the handler to associate with the given tag
        precedence - the precedence of the tag (operator). Note that precedence will be the given value or 1 if the given value is less than 1.
        Returns:
        the grammar
      • postfix

        public <E extends ExpressionGrammar postfix​(Tag tag,
                                                      PostfixHandler handler,
                                                      int precedence,
                                                      SerializablePredicate<E> validator)
        Registers a PostfixHandler with the given precedence for the given Tag
        Type Parameters:
        E - the type parameter
        Parameters:
        tag - the tag for which the handler will be registered
        handler - the handler to associate with the given tag
        precedence - the precedence of the tag (operator). Note that precedence will be the given value or 1 if the given value is less than 1.
        validator - the validator to use validate the generated expression
        Returns:
        the grammar
      • precedenceOf

        public int precedenceOf​(Tag tag)
        Determines the precedence of the given tag
        Parameters:
        tag - the tag
        Returns:
        the precedence
      • precedenceOf

        public int precedenceOf​(ParserToken token)
        Determines the precedence of the Tag of the given ParserToken
        Parameters:
        token - the token for which the precedence will bec calculated.
        Returns:
        the precedence
      • prefix

        public Grammar prefix​(Tag tag,
                              PrefixHandler handler)
        Registers a PrefixHandler for the given Tag
        Parameters:
        tag - the tag for which the handler will be registered
        handler - the handler to associate with the given tag
        Returns:
        the grammar
      • prefix

        public <E extends ExpressionGrammar prefix​(Tag tag,
                                                     PrefixHandler handler,
                                                     SerializablePredicate<E> validator)
        Registers a PrefixHandler for the given Tag
        Parameters:
        tag - the tag for which the handler will be registered
        handler - the handler to associate with the given tag
        validator - the validator to use validate the generated expression
        Returns:
        the grammar
      • skip

        public Grammar skip​(Tag tag)
        Registers the given Tag as one which should be skipped during parsing.
        Parameters:
        tag - the tag which will be skipped.
        Returns:
        the grammar
      • validatePostfix

        public void validatePostfix​(Expression expression)
                             throws ParseException
        Validates the given expression as if it were generated from a postfix handler.
        Parameters:
        expression - the expression to validate
        Throws:
        ParseException - the exception if the expression is invalid
      • validatePrefix

        public void validatePrefix​(Expression expression)
                            throws ParseException
        Validates the given expression as if it were generated from a prefix handler.
        Parameters:
        expression - the expression to validate
        Throws:
        ParseException - the exception if the expression is invalid