Overview
The state module provides types and utilities for managing parser state throughout the parsing process. It includes types for representing source positions, parser state, and parser output, as well as utility functions for state manipulation.Types
ParserState
Represents the complete state of a parser at any point during parsing. Contains the input being parsed, current position, and optional debugging/context information.The complete original input string
Current byte offset from start of input (0-indexed)
Current line number (1-indexed)
Current column number (1-indexed)
Whether debug mode is enabled for detailed error reporting
Stack of parsing context labels for error reporting
Whether the parser has committed to this parse path
ParserOutput
Represents the output of a parser operation, containing both the updated state and the parsing result (either success or error).The parser state after the operation
Either a successful result of type T or a ParseErrorBundle
ParserOutput Factory
Factory function for creating ParserOutput objects.The parser state after the operation
Either a successful result or error bundle
SourcePosition
Represents a position within source code with line, column, and byte offset. All values are 1-indexed for human readability.Line number (1-indexed)
Column number (1-indexed)
Byte offset from start of input (0-indexed)
Spanned
A tuple containing a parsed value and its span information.State Utilities
TheState object provides static methods for creating and manipulating parser state.
State.fromInput
Creates a new parser state from an input string.The input string to parse
A new parser state initialized at the start of the input
State.remaining
Gets the remaining unparsed portion of the input.The current parser state
The remaining input string from current offset
State.charAt
Gets the character at the current offset without allocating.The current parser state
The character at current offset, or empty string if at end
State.startsWith
Checks if remaining input starts with the given string, without allocating.The current parser state
The string to check for
True if remaining input starts with str
State.consume
Creates a new state by consuming n characters from the current state.The current parser state
Number of characters to consume
A new state with n characters consumed and position updated
Throws an error if attempting to consume more characters than remaining.
State.consumeString
Creates a new state by consuming a specific string from the current state.The current parser state
The string to consume
A new state with the string consumed and position updated
Throws an error if the input doesn’t start with the specified string.
State.consumeWhile
Creates a new state by consuming characters while a predicate is true.The current parser state
Function that tests each character
A new state with matching characters consumed
State.peek
Gets the next n characters from the input without consuming them.The current parser state
Number of characters to peek (default: 1)
The next n characters as a string
State.isAtEnd
Checks if the parser has reached the end of input.The current parser state
True if at end of input, false otherwise
State.move
Creates a new state by moving to a specific offset position in the source.The current parser state
Number of characters to move forward from current position
A new state at the target position
State.computePosition
Computes the actual line and column for a given offset.The current parser state
Updated state with correct line/column
State.printPosition
Creates a human-readable string representation of the current parser position.The current parser state
A formatted string showing line, column, and offset
State.toPosition
Creates a SourcePosition from the current parser state.The current parser state
A SourcePosition object
How State Flows Through Parsing
Parser state flows through the parsing process in the following way:- Initial State: Created from input using
State.fromInput(input) - Parser Execution: Each parser receives the current state and returns a new state
- State Updates: Successful parsers advance the offset, failed parsers may preserve the original state
- Sequencing: State flows from one parser to the next in sequential operations
- Backtracking: Failed parsers in choice operations reset to the original state
- Commit Points: Once committed, backtracking is prevented