Package io.github.classgraph.utils
Class Parser
- java.lang.Object
-
- io.github.classgraph.utils.Parser
-
public class Parser extends Object
A generic PEG parser.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Parser.ParseException
A parsing exception.
-
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(String str)
Append the given string to the token buffer.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()
String
getPositionInfo()
Object
getState()
Get the "state object" from the parser (can be used to parse state between parser functions).CharSequence
getSubsequence(int startPosition, int endPosition)
Return a subsequence of the input string.String
getSubstring(int startPosition, int endPosition)
Return a substring of the input string.boolean
hasMore()
void
next()
Advance one character without returning the value of the character.char
peek()
void
peekExpect(char expectedChar)
Get the next character, throwing aParser.ParseException
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.Object
setState(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.String
toString()
-
-
-
Constructor Detail
-
Parser
public Parser(String string) throws Parser.ParseException
Construct a parser.- Parameters:
string
- The string to parse.- Throws:
Parser.ParseException
- If the string was null.
-
-
Method Detail
-
getPositionInfo
public String getPositionInfo()
- Returns:
- A string showing parsing context, for debugging.
-
setState
public Object setState(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 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 Parser.ParseException
Get the next character.- Returns:
- The next character.
- Throws:
Parser.ParseException
- If there were no more characters in the string.
-
expect
public void expect(char expectedChar) throws Parser.ParseException
Expect the next character.- Parameters:
expectedChar
- The expected character.- Throws:
Parser.ParseException
- If the next character was not the expected character.
-
peek
public char peek()
- Returns:
- The next character, or '\0' if at the end of the string.
-
peekExpect
public void peekExpect(char expectedChar) throws Parser.ParseException
Get the next character, throwing aParser.ParseException
if the next character is not the expected character.- Parameters:
expectedChar
- The expected next character.- Throws:
Parser.ParseException
- If the next character is not the expected next character.
-
peekMatches
public boolean peekMatches(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:
IllegalArgumentException
- If there are insufficient characters remaining in the string.
-
hasMore
public boolean hasMore()
- Returns:
- true if the input has not all been consumed.
-
getPosition
public int getPosition()
- 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
public 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 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(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 String currToken()
Get the current token, and reset the token to empty.- Returns:
- The current token. Resets the current token to empty.
-
-