Class Parser
- java.lang.Object
-
- nonapi.io.github.classgraph.types.Parser
-
- Direct Known Subclasses:
JSONParser
public class Parser extends java.lang.Object
A generic PEG parser.
-
-
Field Summary
Fields Modifier and Type Field Description private int
position
The current position.private static int
SHOW_AFTER
How much context to show after the current position.private static int
SHOW_BEFORE
How much context to show before the current position.private java.lang.Object
state
Extra parsing state.private java.lang.String
string
The string being parsed.private java.lang.StringBuilder
token
The token buffer.
-
Constructor Summary
Constructors Constructor Description Parser(java.lang.String string)
Construct a parser.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
advance(int numChars)
Advance numChars character positions.void
appendToToken(char c)
Append the given character to the token buffer.void
appendToToken(java.lang.String str)
Append the given string to the token buffer.java.lang.String
currToken()
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
getPosition()
Get the current position.java.lang.String
getPositionInfo()
Get the parsing context as a string, for debugging.java.lang.Object
getState()
Get the "state object" from the parser (can be used to parse state between parser functions).java.lang.CharSequence
getSubsequence(int startPosition, int endPosition)
Return a subsequence of the input string.java.lang.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(java.lang.String strMatch)
Peek operator that can look ahead several characters.void
setPosition(int position)
Set the position of the parser within the string.java.lang.Object
setState(java.lang.Object state)
Set the "state object" from the parser (can be used to parse state between parser functions).void
skipWhitespace()
Skip whitespace starting at the current position.java.lang.String
toString()
-
-
-
Field Detail
-
string
private final java.lang.String string
The string being parsed.
-
position
private int position
The current position.
-
token
private final java.lang.StringBuilder token
The token buffer.
-
state
private java.lang.Object state
Extra parsing state.
-
SHOW_BEFORE
private static final int SHOW_BEFORE
How much context to show before the current position.- See Also:
- Constant Field Values
-
SHOW_AFTER
private static final int SHOW_AFTER
How much context to show after the current position.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Parser
public Parser(java.lang.String string) throws ParseException
Construct a parser.- Parameters:
string
- The string to parse.- Throws:
ParseException
- If the string was null.
-
-
Method Detail
-
getPositionInfo
public java.lang.String getPositionInfo()
Get the parsing context as a string, for debugging.- Returns:
- A string showing parsing context, for debugging.
-
setState
public java.lang.Object setState(java.lang.Object state)
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
public java.lang.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
public char getc() throws ParseException
Get the next character.- Returns:
- The next character.
- Throws:
ParseException
- If there were no more characters in the string.
-
expect
public void expect(char expectedChar) throws ParseException
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
public void peekExpect(char expectedChar) throws ParseException
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
public boolean peekMatches(java.lang.String strMatch)
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:
java.lang.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:
java.lang.IllegalArgumentException
- If the position is out of range.
-
getSubsequence
public java.lang.CharSequence getSubsequence(int startPosition, int endPosition)
Return a subsequence of the input string.- Parameters:
startPosition
- The start position.endPosition
- The end position.- Returns:
- The subsequence.
-
getSubstring
public java.lang.String getSubstring(int startPosition, int endPosition)
Return a substring of the input string.- Parameters:
startPosition
- The start position.endPosition
- The end position.- Returns:
- The substring.
-
appendToToken
public void appendToToken(java.lang.String str)
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
public java.lang.String currToken()
Get the current token, and reset the token to empty.- Returns:
- The current token. Resets the current token to empty.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-