Module: encoding.yaml
Source: ./encoding/yaml.reef
Overview
encoding/yaml - Simple YAML parsing
This module provides basic YAML parsing support for simple documents. Supports:
- Key-value pairs (scalars)
- Simple lists (- item)
- Nested mappings (indentation-based)
- String, integer, and boolean values
- Comments (# comment)
Limitations:
- No multi-line strings (folded/literal)
- No anchors/aliases
- No complex types (dates, binary)
- Maximum nesting depth of 10
Usage: import encoding.yaml let doc: string = "name: test\nversion: 1" let val: string = yaml_get_string(doc, "name") // "test" let ver: int = yaml_get_int(doc, "version") // 1
Types
YamlBuilder
Fields:
| Name | Type |
|---|---|
sb |
sb.StringBuilder |
depth |
int |
pending_list_item |
bool |
Functions
fn char_to_int(c: char): int
Helper to convert char to int for arithmetic
fn str_append_char(s: string, c: char): string
fn substring(s: string, start: int, len: int): string
fn str_equals(s1: string, s2: string): bool
fn str_starts_with(s: string, prefix: string): bool
fn is_whitespace(c: char): bool
fn is_digit(c: char): bool
fn trim_string(s: string): string
fn parse_int(s: string): int
fn get_line(doc: string, offset: int, out_end: [int]): string
Get a line from document starting at offset, returns line and updates offset
fn count_indent(line: string): int
Count leading spaces in a line
fn strip_comment(line: string): string
Remove comment from line
fn parse_key(line: string): string
Parse a key from "key: value" line
fn parse_value(line: string): string
Parse value from "key: value" line
fn parse_list_item(line: string): string
Parse list item from "- item" line
fn yaml_get_string(doc: string, key: string): string
fn yaml_get_int(doc: string, key: string): int
fn yaml_get_bool(doc: string, key: string): bool
fn yaml_has_key(doc: string, key: string): bool
fn yaml_get_list_len(doc: string, key: string): int
fn yaml_get_list_item(doc: string, key: string, index: int): string
fn yaml_is_string(value: string): bool
fn yaml_is_int(value: string): bool
fn yaml_is_bool(value: string): bool
fn yaml_is_null(value: string): bool
fn yaml_encode_string(key: string, value: string): string
Quoting decisions and escaping are delegated to the same helpers the YamlBuilder uses, so both API surfaces produce consistent, valid YAML.
fn yaml_encode_int(key: string, value: int): string
fn yaml_encode_bool(key: string, value: bool): string
fn yaml_builder(): YamlBuilder
fn yaml_serialize(b: YamlBuilder): string
fn yaml_value_needs_quoting(value: string): bool
True when the scalar must be wrapped in double quotes to round-trip through the parser unambiguously.
fn yaml_escape_quoted(value: string): string
Escape \ and " inside a double-quoted scalar. Newlines are blocked upstream by yaml_value_needs_quoting (we never emit a multi-line scalar).
Procedures
proc yaml_emit_indent(b: YamlBuilder)
Emit indent spaces for the current depth. If a list item marker is pending, emit "- " in place of the last two spaces of indent so the next key (or value) hangs off the dash.
proc yaml_emit_scalar_value(b: YamlBuilder, value: string)
proc yaml_emit_key(b: YamlBuilder, key: string)
Sprint 2 review followup: keys must use the same quoting logic as values, otherwise a key like "&foo", "a: b", "42", or "true" would emit bare and either parse as something other than the intended string key, or produce invalid YAML.
proc yaml_set_string(b: YamlBuilder, key: string, value: string)
proc yaml_set_int(b: YamlBuilder, key: string, value: int)
proc yaml_set_bool(b: YamlBuilder, key: string, value: bool)
proc yaml_begin_mapping(b: YamlBuilder, key: string)
proc yaml_end_mapping(b: YamlBuilder)
proc yaml_begin_list(b: YamlBuilder, key: string)
proc yaml_end_list(b: YamlBuilder)
proc yaml_list_string(b: YamlBuilder, value: string)
proc yaml_list_int(b: YamlBuilder, value: int)
proc yaml_list_bool(b: YamlBuilder, value: bool)
proc yaml_list_begin_item_mapping(b: YamlBuilder)
Begin a mapping that is itself an item of the enclosing list. The next emitted key prints with the "- " marker; subsequent keys at the same depth align underneath (YAML's inline mapping-as-list-item style).
proc yaml_list_end_item_mapping(b: YamlBuilder)
Generated by reefc doc