Class Parser
java.lang.Object
nonapi.io.github.classgraph.types.Parser
- Direct Known Subclasses:
JSONParser
A generic PEG parser.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
The current position.private static final int
How much context to show after the current position.private static final int
How much context to show before the current position.private Object
Extra parsing state.private final String
The string being parsed.private final StringBuilder
The token buffer. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
advance
(int numChars) Advance numChars character positions.void
appendToToken
(char c) Append the given character to the token buffer.void
appendToToken
(String str) Append the given string to the token buffer.Get the current token, and reset the token to empty.void
expect
(char expectedChar) Expect the next character.char
getc()
Get the next character.int
Get the current position.Get the parsing context as a string, for debugging.getState()
Get the "state object" from the parser (can be used to parse state between parser functions).getSubsequence
(int startPosition, int endPosition) Return a subsequence of the input string.getSubstring
(int startPosition, int endPosition) Return a substring of the input string.boolean
hasMore()
Check to see if there are more characters to parse.void
next()
Advance one character without returning the value of the character.char
peek()
Peek at the next character without reading it.void
peekExpect
(char expectedChar) Get the next character, throwing aParseException
if the next character is not the expected character.boolean
peekMatches
(String strMatch) Peek operator that can look ahead several characters.void
setPosition
(int position) Set the position of the parser within the string.Set the "state object" from the parser (can be used to parse state between parser functions).void
Skip whitespace starting at the current position.toString()
-
Field Details
-
string
The string being parsed. -
position
private int positionThe current position. -
token
The token buffer. -
state
Extra parsing state. -
SHOW_BEFORE
private static final int SHOW_BEFOREHow much context to show before the current position.- See Also:
-
SHOW_AFTER
private static final int SHOW_AFTERHow much context to show after the current position.- See Also:
-
-
Constructor Details
-
Parser
Construct a parser.- Parameters:
string
- The string to parse.- Throws:
ParseException
- If the string was null.
-
-
Method Details
-
getPositionInfo
Get the parsing context as a string, for debugging.- Returns:
- A string showing parsing context, for debugging.
-
setState
Set the "state object" from the parser (can be used to parse state between parser functions).- Parameters:
state
- The state object.- Returns:
- The old value of the state object.
-
getState
Get the "state object" from the parser (can be used to parse state between parser functions).- Returns:
- The current value of the state object.
-
getc
Get the next character.- Returns:
- The next character.
- Throws:
ParseException
- If there were no more characters in the string.
-
expect
Expect the next character.- Parameters:
expectedChar
- The expected character.- Throws:
ParseException
- If the next character was not the expected character.
-
peek
public char peek()Peek at the next character without reading it.- Returns:
- The next character, or '\0' if at the end of the string.
-
peekExpect
Get the next character, throwing aParseException
if the next character is not the expected character.- Parameters:
expectedChar
- The expected next character.- Throws:
ParseException
- If the next character is not the expected next character.
-
peekMatches
Peek operator that can look ahead several characters.- Parameters:
strMatch
- The string to compare, starting at the current position, as a "peek" operation.- Returns:
- True if the strMatch matches a substring of the remaining string starting at the current position.
-
next
public void next()Advance one character without returning the value of the character. -
advance
public void advance(int numChars) Advance numChars character positions.- Parameters:
numChars
- The number of character positions to advance.- Throws:
IllegalArgumentException
- If there are insufficient characters remaining in the string.
-
hasMore
public boolean hasMore()Check to see if there are more characters to parse.- Returns:
- true if the input has not all been consumed.
-
getPosition
public int getPosition()Get the current position.- Returns:
- the current position.
-
setPosition
public void setPosition(int position) Set the position of the parser within the string.- Parameters:
position
- The position to move to.- Throws:
IllegalArgumentException
- If the position is out of range.
-
getSubsequence
Return a subsequence of the input string.- Parameters:
startPosition
- The start position.endPosition
- The end position.- Returns:
- The subsequence.
-
getSubstring
Return a substring of the input string.- Parameters:
startPosition
- The start position.endPosition
- The end position.- Returns:
- The substring.
-
appendToToken
Append the given string to the token buffer.- Parameters:
str
- The string to append.
-
appendToToken
public void appendToToken(char c) Append the given character to the token buffer.- Parameters:
c
- The character to append.
-
skipWhitespace
public void skipWhitespace()Skip whitespace starting at the current position. -
currToken
Get the current token, and reset the token to empty.- Returns:
- The current token. Resets the current token to empty.
-
toString
-