Class Parser
- java.lang.Object
-
- nonapi.io.github.classgraph.types.Parser
-
- Direct Known Subclasses:
JSONParser
public class Parser extends java.lang.ObjectA generic PEG parser.
-
-
Field Summary
Fields Modifier and Type Field Description private intpositionThe current position.private static intSHOW_AFTERHow much context to show after the current position.private static intSHOW_BEFOREHow much context to show before the current position.private java.lang.ObjectstateExtra parsing state.private java.lang.StringstringThe string being parsed.private java.lang.StringBuildertokenThe 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 voidadvance(int numChars)Advance numChars character positions.voidappendToToken(char c)Append the given character to the token buffer.voidappendToToken(java.lang.String str)Append the given string to the token buffer.java.lang.StringcurrToken()Get the current token, and reset the token to empty.voidexpect(char expectedChar)Expect the next character.chargetc()Get the next character.intgetPosition()Get the current position.java.lang.StringgetPositionInfo()Get the parsing context as a string, for debugging.java.lang.ObjectgetState()Get the "state object" from the parser (can be used to parse state between parser functions).java.lang.CharSequencegetSubsequence(int startPosition, int endPosition)Return a subsequence of the input string.java.lang.StringgetSubstring(int startPosition, int endPosition)Return a substring of the input string.booleanhasMore()Check to see if there are more characters to parse.voidnext()Advance one character without returning the value of the character.charpeek()Peek at the next character without reading it.voidpeekExpect(char expectedChar)Get the next character, throwing aParseExceptionif the next character is not the expected character.booleanpeekMatches(java.lang.String strMatch)Peek operator that can look ahead several characters.voidsetPosition(int position)Set the position of the parser within the string.java.lang.ObjectsetState(java.lang.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.java.lang.StringtoString()
-
-
-
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 ParseExceptionConstruct 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 ParseExceptionGet 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 ParseExceptionExpect 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 ParseExceptionGet the next character, throwing aParseExceptionif 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:
toStringin classjava.lang.Object
-
-