Class Parser

java.lang.Object
nonapi.io.github.classgraph.types.Parser
Direct Known Subclasses:
JSONParser

public class Parser extends Object
A generic PEG parser.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private 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
    Constructor
    Description
    Parser(String string)
    Construct a parser.
  • Method Summary

    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
    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
    Get the next character.
    int
    Get the current position.
    Get the parsing context as a string, for debugging.
    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
    Check to see if there are more characters to parse.
    void
    Advance one character without returning the value of the character.
    char
    Peek at the next character without reading it.
    void
    peekExpect(char expectedChar)
    Get the next character, throwing a 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.
    Set the "state object" from the parser (can be used to parse state between parser functions).
    void
    Skip whitespace starting at the current position.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • string

      private final String string
      The string being parsed.
    • position

      private int position
      The current position.
    • token

      private final StringBuilder token
      The token buffer.
    • state

      private Object state
      Extra parsing state.
    • SHOW_BEFORE

      private static final int SHOW_BEFORE
      How much context to show before the current position.
      See Also:
    • SHOW_AFTER

      private static final int SHOW_AFTER
      How much context to show after the current position.
      See Also:
  • Constructor Details

    • Parser

      public Parser(String string) throws ParseException
      Construct a parser.
      Parameters:
      string - The string to parse.
      Throws:
      ParseException - If the string was null.
  • Method Details

    • getPositionInfo

      public String getPositionInfo()
      Get the parsing context as a string, for debugging.
      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 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 a ParseException 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(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()
      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

      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.
    • toString

      public String toString()
      Overrides:
      toString in class Object