StringYamlParser class final
A production-ready, streaming YAML 1.2 parser that converts a YAML string into a normalized stream of YamlTokens.
StringYamlParser is the core pull-based YAML parser used by JetLeaf.
It implements the full YAML 1.2 grammar including:
- Mappings (flow and block)
- Sequences (flow and block)
- Anchors (
&foo) and aliases (*foo) - Tags (
!!str,!<verbatim:tag>, custom tags) - Block scalars (
|literal and>folded) - Chomping indicators (
|+/|-) - Indentation-sensitive parsing
- Multi-document streams (
---/...)
The parser is designed for:
- High performance (single-pass tokenizer)
- Low memory usage (pull API, no tree construction)
- Spec correctness (YAML 1.2 compliant)
- Robust error handling (precise line/column diagnostics)
Usage Example
final parser = StringYamlParser(source);
while (parser.nextToken()) {
final token = parser.getCurrentToken();
final value = parser.getCurrentValue();
print('$token $value');
}
await parser.close();
Key Features
- Emits one YamlToken at a time (pull model)
- Can skip entire nested collections via skip
- Maintains anchor resolution tables
- Handles all scalar styles (plain, quoted, literal, folded)
- Properly manages indentation and flow contexts
- Produces detailed
YamlExceptionerrors with source positions
Token Stream Example
YAML:
person:
name: Alice
age: 30
Produces:
MAPPING_START
KEY -> "person"
MAPPING_START
KEY -> "name"
SCALAR -> "Alice"
KEY -> "age"
SCALAR -> "30"
MAPPING_END
MAPPING_END
END_DOCUMENT
Lifecycle
- Create parser with input string
- Call nextToken until it returns
false - Inspect tokens + values
- Call close to release buffers
Error Handling
Any structural error throws a YamlException that includes:
- Error message
- Line/column position
- Stack trace for debugging
The implementation closely follows the YAML 1.2 specification and is optimized for correctness over convenience. Higher-level JetLeaf layers (deserializers, builders, AST models) consume this parser.
See also:
YamlScanner— low-level character scanner- YamlToken — token enumeration
BlockScalarChomping— block scalar semanticsYamlStyle— scalar style indicators
- Implemented types
Constructors
- StringYamlParser(String input)
- Creates a new YAML parser for the given input String.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
) → FutureOr< void> - Closes this resource, relinquishing any underlying resources.
-
getAlias(
) → String? -
Returns the alias reference if this token is an alias.
override
-
getAnchor(
) → String? -
Returns the anchor name if this token is anchored.
override
-
getCurrentToken(
) → YamlToken? -
Returns the current token after a successful nextToken call.
override
-
getCurrentValue(
) → String? -
Returns the text or raw value associated with the current token.
override
-
nextToken(
) → bool -
Advances the parser to the next token.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
skip(
) → void -
Skips the entire nested structure at the current cursor position.
override
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited