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 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 trim_string(s: string): string

fn parse_int(s: string): option.Option[int]

0.9.2 C16: None when the string holds no digits at all.

This returned 0 for an empty string and for outright garbage ("abc"), the same answer as the literal "0". yaml_get_int inherited that: a key holding a non-numeric value was reported as 0.

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): option.Option[string]

Parse a key from "key: value" line 0.9.2 C16: None when the line carries no key at all.

This returned "" for a list item, for a line with no colon, AND for a line like : value whose key is genuinely empty -- three different facts with one answer. Callers that want "is there a NON-EMPTY key here" ask that directly (see the two list-scan sites), which is a different question from presence.

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): option.Option[string]

0.9.2 C16: None when the key is absent from the document.

This returned "" for both "no such key" and a key whose value is genuinely empty -- and an empty YAML value is ordinary (key: with nothing after it).

fn yaml_get_int(doc: string, key: string): option.Option[int]

0.9.2 C16: None when the key is absent OR its value is not a number. Both used to be reported as 0, indistinguishable from the literal 0.

fn yaml_get_bool(doc: string, key: string): option.Option[bool]

0.9.2 C16: None when the key is absent. This returned false for a missing key, which is exactly what key: false returns.

A PRESENT key whose value is not a recognised truthy spelling is still Some(false) -- that is YAML's falsy default, a separate question from presence. Use yaml_is_bool() if you need to validate the spelling.

fn yaml_get_string_or(doc: string, key: string, default: string): string

*_or lookups: present value wins, missing uses the caller default. Same shape as sys.env.get_env_or and encoding.toml's twins.

The Option getters stay the source of truth. These exist because after the 0.9.2 C16 conversion (reef-lang#53) a caller who genuinely wants a fallback had to spell option.unwrap_or at every site. NOTE what they must NOT do: Some("") / Some(0) / Some(false) are PRESENT values and are returned as-is. Substituting the default for them would reinstate exactly the sentinel collision C16 removed.

fn yaml_get_int_or(doc: string, key: string, default: int): int

fn yaml_get_bool_or(doc: string, key: string, default: bool): bool

fn yaml_get_list_item_or(doc: string, key: string, index: int, default: string): string

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): option.Option[string]

0.9.2 C16: None when the key is absent or the index is past the end. This returned "" for both, and an empty list element is a legal YAML value.

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