Class SentenceParser

java.lang.Object
org.projog.core.parser.SentenceParser

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

Note: not thread safe.

See Also:
  • Field Details

    • COMMA_PRIORITY

      private static final int COMMA_PRIORITY
      See Also:
    • EMPTY_LIST_TOKEN

      private static final Token EMPTY_LIST_TOKEN
    • parser

      private final TokenParser parser
    • operands

      private final Operands operands
    • variables

      private final HashMap<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 Details

    • SentenceParser

      private SentenceParser(Reader reader, Operands operands)
  • Method Details

    • getInstance

      public static SentenceParser getInstance(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(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 Map<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, Function<Token,Boolean> terminationCriteria)
    • parseTokens

      private List<Token> parseTokens(boolean rewindOnTermination, Function<Token,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(String value)
    • toDecimalFraction

      private DecimalFraction toDecimalFraction(String value)
    • getOrCreateVariable

      private Variable getOrCreateVariable(String id)
    • newParserException

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