Package org.projog.core.parser
Class SentenceParser
- java.lang.Object
-
- org.projog.core.parser.SentenceParser
-
public class SentenceParser extends java.lang.ObjectParses Prolog syntax representing rules including operators.Note: not thread safe.
- See Also:
Operands
-
-
Field Summary
Fields Modifier and Type Field Description private static intCOMMA_PRIORITYprivate static TokenEMPTY_LIST_TOKENprivate Operandsoperandsprivate TokenParserparserprivate java.util.HashMap<java.lang.String,Variable>variablesA collection ofVariables this parser currently knows about (key = the variable name).
-
Constructor Summary
Constructors Modifier Constructor Description privateSentenceParser(java.io.Reader reader, Operands operands)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SentenceParsergetInstance(java.io.Reader reader, Operands operands)Returns a newSentenceParserthat will parse Prolog syntax read from the specifiedReaderusing the specifiedOperands.static SentenceParsergetInstance(java.lang.String prologSyntax, Operands operands)Returns a newSentenceParserwill parse the specifiedStringusing the specifiedOperands.private VariablegetOrCreateVariable(java.lang.String id)java.util.Map<java.lang.String,Variable>getParsedTermVariables()Returns collection ofVariableinstances created by thisSentenceParser.private ParserExceptionnewParserException(java.lang.String message)Returns a newParserExceptionwith the specified message.private TokenparseList()private TokenparsePredicate(Token next)TermparseSentence()private TokenparseToken(boolean rewindOnTermination, java.util.function.Function<Token,java.lang.Boolean> terminationCriteria)private java.util.List<Token>parseTokens(boolean rewindOnTermination, java.util.function.Function<Token,java.lang.Boolean> terminationCriteria)private DecimalFractiontoDecimalFraction(java.lang.String value)private IntegerNumbertoIntegerNumber(java.lang.String value)private TermtoList(Token token)private TokentoSingleToken(Token[] tokens, int startIdx, int endIdx, int previousPriority, boolean faiIfPriorityEqual)private TermtoStructureFromNamedBracket(Token token)private TermtoStructureFromOperandAndArguments(Token token)private TermtoTerm(Token token)
-
-
-
Field Detail
-
COMMA_PRIORITY
private static final int COMMA_PRIORITY
- See Also:
- Constant Field Values
-
EMPTY_LIST_TOKEN
private static final Token EMPTY_LIST_TOKEN
-
parser
private final TokenParser parser
-
operands
private final Operands operands
-
variables
private final java.util.HashMap<java.lang.String,Variable> variables
A collection ofVariables 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
Variableinstance.e.g. During the parsing of the sentence
Y is 2, X is Y * 2.twoVariableinstances need to be created - one for the variable nameXand one that is shared between both references to the variable nameY.
-
-
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 newSentenceParserwill parse the specifiedStringusing the specifiedOperands.- Parameters:
prologSyntax- the prolog syntax to be parsedoperands- 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 newSentenceParserthat will parse Prolog syntax read from the specifiedReaderusing the specifiedOperands.- Parameters:
reader- the source of the prolog syntax to be parsedoperands- details of the operands to use during parsing- Returns:
- a new
SentenceParser
-
getParsedTermVariables
public java.util.Map<java.lang.String,Variable> getParsedTermVariables()
Returns collection ofVariableinstances created by thisSentenceParser.Returns all
Variables created by thisSentenceParsereither since it was created or since the last execution ofparseSentence().- Returns:
- collection of
Variableinstances created by thisSentenceParser
-
parseSentence
public Term parseSentence()
- Returns:
- a
Termcreated from Prolog syntax read from this object'sCharacterParserornullif 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()
-
toSingleToken
private Token toSingleToken(Token[] tokens, int startIdx, int endIdx, int previousPriority, boolean faiIfPriorityEqual)
-
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 newParserExceptionwith the specified message.
-
-