Package org.projog.core.parser
Class TokenParser
- java.lang.Object
-
- org.projog.core.parser.TokenParser
-
class TokenParser extends java.lang.ObjectParses an input stream into discrete 'tokens' that are used to represent Prolog queries and rules.- See Also:
SentenceParser
-
-
Field Summary
Fields Modifier and Type Field Description private TokenlastParsedTokenprivate Operandsoperandsprivate CharacterParserparserprivate booleanrewound
-
Constructor Summary
Constructors Constructor Description TokenParser(java.io.Reader reader, Operands operands)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static TokencreateToken(java.lang.StringBuilder value, TokenType type)private static TokencreateToken(java.lang.String value, TokenType type)private intescape(int escape)(package private) booleanhasNext()private static booleanisEndOfStream(int c)private static booleanisEscapeSequencePrefix(int c)(package private) booleanisFollowedByTerm()Does the next value to be parsed represent a term (rather than a delimiter)(package private) booleanisImmediatelyFollowedByBracket(boolean rewind)private static booleanisMultiLineCommentEnd(int c1, int c2)private static booleanisMultiLineCommentStart(int c1, int c2)private static booleanisQuote(int c)private static booleanisSingleLineComment(int c)private static booleanisUnderscore(int c)private static booleanisValidForAtom(int c)private booleanisValidParseableElement(java.lang.String commandName)private static booleanisVariable(int c)private static booleanisZero(int c)(package private) ParserExceptionnewParserException(java.lang.String message)Returns a newParserExceptionwith the specified message.(package private) Tokennext()Parse and return the nextToken.private intparseEscapeSequence()private charparseHex()private TokenparseLeadingZero(int zero)Parses a character code and represents it as an integer.private TokenparseNumber(int startChar)Parses a number, starting with the specified character, read from the parser.private TokenparseQuotedText()Reads aStringconsisting of all characters read from the parser up to the next'.private TokenparseSymbol(int c)private TokenparseText(int c, TokenType t)private TokenparseToken()private intparseUnicode()(package private) voidrewind(Token value)Rewinds the parser (i.e.private voidskipMultiLineComment()private voidskipWhitespace()private voidskipWhitespaceAndComments()
-
-
-
Field Detail
-
parser
private final CharacterParser parser
-
operands
private final Operands operands
-
lastParsedToken
private Token lastParsedToken
-
rewound
private boolean rewound
-
-
Constructor Detail
-
TokenParser
TokenParser(java.io.Reader reader, Operands operands)
-
-
Method Detail
-
hasNext
boolean hasNext()
- Returns:
trueif there are more tokens to be parsed, elsefalse
-
next
Token next()
Parse and return the nextToken.- Returns:
- the token that was parsed as a result of this call
- Throws:
ParserException- if there are no more tokens to parse (i.e. parser has reached the end of the underlying input stream)
-
parseToken
private Token parseToken()
-
rewind
void rewind(Token value)
Rewinds the parser (i.e. "pushes-back" the last parsed token).The last parsed value will remain after the next call to
next()- Parameters:
value- the value to rewind- Throws:
java.lang.IllegalArgumentException- if already in a rewound state (i.e. have already calledTokenParser#rewind(String)since the last call tonext()), orvalueis not equal to#getValue()
-
isImmediatelyFollowedByBracket
boolean isImmediatelyFollowedByBracket(boolean rewind)
-
isFollowedByTerm
boolean isFollowedByTerm()
Does the next value to be parsed represent a term (rather than a delimiter)
-
newParserException
ParserException newParserException(java.lang.String message)
Returns a newParserExceptionwith the specified message.
-
skipWhitespaceAndComments
private void skipWhitespaceAndComments()
-
parseText
private Token parseText(int c, TokenType t)
- Parameters:
c- the first, already parsed, character of the token.
-
parseQuotedText
private Token parseQuotedText()
Reads aStringconsisting of all characters read from the parser up to the next'.If an atom's name is enclosed in quotes (i.e.
') then it may contain any character.
-
parseLeadingZero
private Token parseLeadingZero(int zero)
Parses a character code and represents it as an integer.e.g. the text
0'aresults in a token with the value97(the ascii value fora) being returned.
-
parseEscapeSequence
private int parseEscapeSequence()
-
parseUnicode
private int parseUnicode()
-
parseHex
private char parseHex()
-
parseNumber
private Token parseNumber(int startChar)
Parses a number, starting with the specified character, read from the parser.Deals with numbers of the form
3.4028235E38.
-
escape
private int escape(int escape)
-
parseSymbol
private Token parseSymbol(int c)
-
skipWhitespace
private void skipWhitespace()
-
skipMultiLineComment
private void skipMultiLineComment()
-
isValidParseableElement
private boolean isValidParseableElement(java.lang.String commandName)
-
isEndOfStream
private static boolean isEndOfStream(int c)
-
isSingleLineComment
private static boolean isSingleLineComment(int c)
-
isMultiLineCommentStart
private static boolean isMultiLineCommentStart(int c1, int c2)
-
isMultiLineCommentEnd
private static boolean isMultiLineCommentEnd(int c1, int c2)
-
isValidForAtom
private static boolean isValidForAtom(int c)
-
isVariable
private static boolean isVariable(int c)
-
isUnderscore
private static boolean isUnderscore(int c)
-
isQuote
private static boolean isQuote(int c)
-
isZero
private static boolean isZero(int c)
-
isEscapeSequencePrefix
private static boolean isEscapeSequencePrefix(int c)
-
-