Class SentenceParser


  • public class SentenceParser
    extends java.lang.Object
    Parses Prolog syntax representing rules including operators.

    Note: not thread safe.

    See Also:
    Operands
    • Field Detail

      • EMPTY_LIST_TOKEN

        private static final Token EMPTY_LIST_TOKEN
      • operands

        private final Operands operands
      • variables

        private final java.util.HashMap<java.lang.String,​Variable> variables
        A collection of Variables this parser currently knows about (key = the variable name).

        The reason this information needs to be stored is so that each instance of the same variable name, in a single sentence, refers to the same Variable instance.

        e.g. During the parsing of the sentence Y is 2, X is Y * 2. two Variable instances need to be created - one for the variable name X and one that is shared between both references to the variable name Y.

    • Constructor Detail

      • SentenceParser

        private SentenceParser​(java.io.Reader reader,
                               Operands operands)
    • Method Detail

      • getInstance

        public static SentenceParser getInstance​(java.lang.String prologSyntax,
                                                 Operands operands)
        Returns a new SentenceParser will parse the specified String using the specified Operands.
        Parameters:
        prologSyntax - the prolog syntax to be parsed
        operands - details of the operands to use during parsing
        Returns:
        a new SentenceParser
      • getInstance

        public static SentenceParser getInstance​(java.io.Reader reader,
                                                 Operands operands)
        Returns a new SentenceParser that will parse Prolog syntax read from the specified Reader using the specified Operands.
        Parameters:
        reader - the source of the prolog syntax to be parsed
        operands - details of the operands to use during parsing
        Returns:
        a new SentenceParser
      • getParsedTermVariables

        public java.util.Map<java.lang.String,​Variable> getParsedTermVariables()
        Returns collection of Variable instances created by this SentenceParser.

        Returns all Variables created by this SentenceParser either since it was created or since the last execution of parseSentence().

        Returns:
        collection of Variable instances created by this SentenceParser
      • parseSentence

        public Term parseSentence()
        Creates a Term from Prolog syntax, terminated by a ., read from this object's CharacterParser.
        Returns:
        a Term created from Prolog syntax read from this object's CharacterParser or null if the end of the underlying stream being parsed has been reached
        Throws:
        ParserException - if an error parsing the Prolog syntax occurs
      • parseToken

        private Token parseToken​(boolean rewindOnTermination,
                                 java.util.function.Function<Token,​java.lang.Boolean> terminationCriteria)
      • parseTokens

        private java.util.List<Token> parseTokens​(boolean rewindOnTermination,
                                                  java.util.function.Function<Token,​java.lang.Boolean> terminationCriteria)
      • parseList

        private Token parseList()
      • parsePredicate

        private Token parsePredicate​(Token next)
      • toSingleToken

        private Token toSingleToken​(Token[] tokens,
                                    int startIdx,
                                    int endIdx,
                                    int previousPriority,
                                    boolean faiIfPriorityEqual)
      • toTerm

        private Term toTerm​(Token token)
      • toStructureFromNamedBracket

        private Term toStructureFromNamedBracket​(Token token)
      • toStructureFromOperandAndArguments

        private Term toStructureFromOperandAndArguments​(Token token)
      • toList

        private Term toList​(Token token)
      • toIntegerNumber

        private IntegerNumber toIntegerNumber​(java.lang.String value)
      • toDecimalFraction

        private DecimalFraction toDecimalFraction​(java.lang.String value)
      • getOrCreateVariable

        private Variable getOrCreateVariable​(java.lang.String id)
      • newParserException

        private ParserException newParserException​(java.lang.String message)
        Returns a new ParserException with the specified message.