Cursor

class Cursor(val input: String)

Class representing an iteration position over a given string, with convenience functions. This is intended for use with the token parsing system, but you can use it for other things too.

Parameters

input

Input string to iterate over. Never modified.

Constructors

Link copied to clipboard
constructor(input: String)

Properties

Link copied to clipboard

Returns true if there are more characters left to iterate over.

Link copied to clipboard

Returns true if there are characters to iterate backwards to.

Link copied to clipboard
var index: Int

Current iteration index, starting at -1.

Link copied to clipboard

Functions

Link copied to clipboard
fun consumeNumber(amount: Int): String

Consume amount characters from this cursor, returning them as a String. Will stop consuming and return when the cursor runs out of characters, instead of throwing.

Link copied to clipboard

Iterate over the rest of the string, returning the result.

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

Iterate over the rest of the string as long as the predicate returns true, returning the result.

Link copied to clipboard
fun next(): Char

Increment the index and return the character found there, throwing if we're at the end of the string.

Link copied to clipboard

Increment the index and return the character found there, or null if we're at the end of the string.

Link copied to clipboard
fun peek(): Char

Return the character at the current index.

Link copied to clipboard
fun peekNext(): Char?

Return the character at the next index, or null if we're at the end of the string.

Link copied to clipboard

Return the character at the previous index, or null if we're at the start of the string.

Link copied to clipboard
fun previous(): Char

Decrement the index and return the character found there, throwing if we're at the start of the string.

Link copied to clipboard

Decrement the index and return the character found there, or null if we're at the start of the string.

Link copied to clipboard

Skip any immediate whitespace, updating the index.