Class StringParser
-
- All Implemented Interfaces:
public class StringParserString 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 Summary
Constructors Constructor Description StringParser(String input)
-
Method Summary
Modifier and Type Method Description final CursorgetCursor()Cursor object, representing the current parsing state. final UnitsetCursor(Cursor cursor)Cursor object, representing the current parsing state. final BooleangetHasNext()StringgetInput()final List<NamedArgumentToken>parseNamed()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. final PositionalArgumentTokenpeekNext()Attempt to parse the next token and reset the cursor's index to what it was before parsing, before returning the result. final PositionalArgumentTokenparseNext()Attempt to parse a single or quoted positional argument token from the cursor, returning it if there was a token, or nullif there wasn't anything left to parse.final StringconsumeRemaining()Consume whatever is left in the cursor, setting it to the end of its input string. final StringconsumeWhile(Function1<Character, Boolean> predicate)Consume characters from the cursor while the predicate returns true, and return those characters joined into a String.final StringpeekRemaining()Return whatever remains in the cursor's string, without consuming it'. final StringpeekWhile(Function1<Character, Boolean> predicate)Collect characters from the cursor while the predicate returns true, and return those characters joined into a String.-
-
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.
-
getHasNext
final Boolean getHasNext()
-
parseNamed
final List<NamedArgumentToken> parseNamed()
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.
Note: This function reassigns the cursor property!
-
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
nullif there wasn't anything left to parse.
-
consumeRemaining
final String consumeRemaining()
Consume whatever is left in the cursor, setting it to the end of its input string.
-
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,nullwill 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.
-
peekRemaining
final String peekRemaining()
Return whatever remains in the cursor's string, without consuming it'.
-
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,nullwill be returned instead. After characters have been collected, the cursor's index is reset, so the characters won't be consumed.
-
-
-
-