Class StringParser

  • All Implemented Interfaces:

    
    public class StringParser
    
                        

    String parser, tokenizing the input as requested by the function calls. Intended for command argument parsing, but can be used for other things too if needed.

    This parser supports:

    • Keyword arguments at any position (key=value)

    • Flag arguments at any position (--key value)

    • Quoted arguments with spaces ("value value value")

    • Unquoted arguments without spaces (value)

    The recommended workflow is as follows:

    • Call parseNamed to parse out the keyword/flag arguments, removing them from the cursor

    • Call parseNext as required to parse out single or quoted tokens sequentially

    • Use the other functions to parse in more specific ways

    • Constructor Detail

      • StringParser

        StringParser(String input)
        Parameters:
        input - Input string to parse.
    • Method Detail

      • getCursor

         final Cursor getCursor()

        Cursor object, representing the current parsing state. Initially, this will contain a Cursor that iterates over the entire input, but functions (eg parseNamed) may reassign it to change the iteration target.

      • setCursor

         final Unit setCursor(Cursor cursor)

        Cursor object, representing the current parsing state. Initially, this will contain a Cursor that iterates over the entire input, but functions (eg parseNamed) may reassign it to change the iteration target.

      • peekNext

         final PositionalArgumentToken peekNext()

        Attempt to parse the next token and reset the cursor's index to what it was before parsing, before returning the result.

      • parseNext

         final PositionalArgumentToken parseNext()

        Attempt to parse a single or quoted positional argument token from the cursor, returning it if there was a token, or null if there wasn't anything left to parse.

      • consumeWhile

         final String consumeWhile(Function1<Character, Boolean> predicate)

        Consume characters from the cursor while the predicate returns true, and return those characters joined into a String. If the predicate fails on the first character, null will be returned instead.

        Note: Once this function has finished consuming characters, it will instruct the cursor to skip any immediate whitespace, which prepares it for normal token parsing - assuming there's anything left to parse.

      • peekWhile

         final String peekWhile(Function1<Character, Boolean> predicate)

        Collect characters from the cursor while the predicate returns true, and return those characters joined into a String. If the predicate fails on the first character, null will be returned instead. After characters have been collected, the cursor's index is reset, so the characters won't be consumed.