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 classParser.ParseExceptionA parsing exception.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadvance(int numChars)Advance numChars character positions.voidappendToToken(char c)Append the given character to the token buffer.voidappendToToken(String str)Append the given string to the token buffer.StringcurrToken()Get the current token, and reset the token to empty.voidexpect(char expectedChar)Expect the next character.chargetc()Get the next character.intgetPosition()StringgetPositionInfo()ObjectgetState()Get the "state object" from the parser (can be used to parse state between parser functions).CharSequencegetSubsequence(int startPosition, int endPosition)Return a subsequence of the input string.StringgetSubstring(int startPosition, int endPosition)Return a substring of the input string.booleanhasMore()voidnext()Advance one character without returning the value of the character.charpeek()voidpeekExpect(char expectedChar)Get the next character, throwing aParser.ParseExceptionif the next character is not the expected character.booleanpeekMatches(String strMatch)Peek operator that can look ahead several characters.voidsetPosition(int position)Set the position of the parser within the string.ObjectsetState(Object state)Set the "state object" from the parser (can be used to parse state between parser functions).voidskipWhitespace()Skip whitespace starting at the current position.StringtoString()
-
-
-
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.ParseExceptionGet 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.ParseExceptionExpect 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.ParseExceptionGet the next character, throwing aParser.ParseExceptionif 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.
-
-