StringParser

open class StringParser(val input: String)

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:

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

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

  3. Use the other functions to parse in more specific ways

Parameters

input

Input string to parse.

Constructors

Link copied to clipboard
constructor(input: String)

Properties

Link copied to clipboard

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.

Link copied to clipboard

Returns true if the cursor has more parsing to do.

Link copied to clipboard
open val input: String

Functions

Link copied to clipboard

Consume whatever is left in the cursor, setting it to the end of its input string.

Link copied to clipboard
fun consumeWhile(predicate: (Char) -> Boolean): String?

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.

Link copied to clipboard

Parse all of the flag and keyword arguments out of the cursor, creating a new Cursor containing only the positional arguments and returning a list of parsed NamedArgumentTokens.

Link copied to clipboard

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.

Link copied to clipboard

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

Link copied to clipboard

Return whatever remains in the cursor's string, without consuming it'.

Link copied to clipboard
fun peekWhile(predicate: (Char) -> Boolean): String?

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.