AtlatestRepositorysigil-yaml
sigil-yaml / tree / docsyaml.md
1
# (sigil yaml)3
YAML parsing and serialization for Sigil applications.5
## Type Mapping7
| YAML | Scheme |8
|-----------|------------------------------------|9
| mapping | dict: `#{ name: "Alice" }` |10
| sequence | list: `("apple" "banana")` |11
| string | string: `"hello"` |12
| integer | integer: `42` |13
| float | float: `3.14` |14
| true/false| `#t` / `#f` |15
| null | `'null` symbol |17
## Quick Start19
```scheme20
(import (sigil yaml))22
;; Decode YAML strings23
(yaml-decode "name: Alice\nage: 30")24
; => #{ name: "Alice" age: 30 }26
(yaml-decode "- apple\n- banana\n- cherry")27
; => ("apple" "banana" "cherry")29
;; Encode Scheme values to YAML30
(yaml-encode #{ name: "Alice" age: 30 })31
; => "name: Alice\nage: 30\n"33
(yaml-encode '(1 2 3))34
; => "- 1\n- 2\n- 3\n"35
```37
## Reading and Writing39
### String Convenience Functions41
```scheme42
;; Decode a YAML string43
(yaml-decode "key: value") ; => #{ key: "value" }45
;; Encode to YAML string46
(yaml-encode #{ key: "value" }) ; => "key: value\n"48
;; Flow (inline) style49
(yaml-encode #{ a: 1 b: 2 } flow: #t) ; => "{a: 1, b: 2}"50
```52
### Port-Based I/O54
```scheme55
;; Write YAML to a file56
(call-with-output-file "config.yaml"57
(lambda (port)58
(yaml-write #{ name: "Alice" age: 30 } port)))60
;; Read YAML from a file61
(call-with-input-file "config.yaml" yaml-read)62
; => #{ name: "Alice" age: 30 }63
```65
## Scalars67
YAML scalars are automatically resolved to Scheme types:69
```scheme70
(yaml-decode "42") ; => 42 (integer)71
(yaml-decode "3.14") ; => 3.14 (float)72
(yaml-decode "0xFF") ; => 255 (hex integer)73
(yaml-decode "0o10") ; => 8 (octal integer)74
(yaml-decode "true") ; => #t75
(yaml-decode "false") ; => #f76
(yaml-decode "null") ; => 'null77
(yaml-decode "~") ; => 'null78
(yaml-decode ".inf") ; => +infinity79
(yaml-decode ".nan") ; => NaN80
(yaml-decode "hello") ; => "hello" (string)81
```83
### Quoted Strings85
```scheme86
;; Double-quoted (supports escape sequences)87
(yaml-decode "\"line1\\nline2\"") ; => "line1\nline2"89
;; Single-quoted (no escapes except '')90
(yaml-decode "'it''s here'") ; => "it's here"92
;; Quoted strings preserve literal values93
(yaml-decode "\"true\"") ; => "true" (string, not boolean)94
(yaml-decode "'null'") ; => "null" (string, not null)95
```97
## Collections99
### Block Mappings101
```scheme102
(yaml-decode "name: Alice\nage: 30")103
; => #{ name: "Alice" age: 30 }105
;; Nested106
(yaml-decode "server:\n host: localhost\n port: 8080")107
; => #{ server: #{ host: "localhost" port: 8080 } }108
```110
### Block Sequences112
```scheme113
(yaml-decode "- apple\n- banana\n- cherry")114
; => ("apple" "banana" "cherry")116
;; Sequence of mappings117
(yaml-decode "- name: Alice\n age: 30\n- name: Bob\n age: 25")118
; => (#{ name: "Alice" age: 30 } #{ name: "Bob" age: 25 })119
```121
### Flow Collections123
```scheme124
;; Inline sequence125
(yaml-decode "[1, 2, 3]")126
; => (1 2 3)128
;; Inline mapping129
(yaml-decode "{name: Alice, age: 30}")130
; => #{ name: "Alice" age: 30 }132
;; Mixed with block style133
(yaml-decode "items: [1, 2, 3]\npoint: {x: 10, y: 20}")134
; => #{ items: (1 2 3) point: #{ x: 10 y: 20 } }135
```137
## Block Scalars139
### Literal (`|`) - preserves newlines141
```scheme142
(yaml-decode "text: |\n line 1\n line 2\n line 3")143
; text => "line 1\nline 2\nline 3\n"144
```146
### Folded (`>`) - joins lines with spaces148
```scheme149
(yaml-decode "text: >\n long line\n continues here")150
; text => "long line continues here\n"151
```153
### Chomping Indicators155
```scheme156
;; Strip (-) removes all trailing newlines157
(yaml-decode "text: |-\n hello") ; text => "hello"159
;; Keep (+) preserves trailing newlines160
(yaml-decode "text: |+\n hello\n\nnext: val") ; text => "hello\n\n"162
;; Clip (default) keeps one trailing newline163
(yaml-decode "text: |\n hello") ; text => "hello\n"164
```166
## Multi-Document168
```scheme169
;; Read all documents170
(yaml-decode-all "---\na: 1\n---\nb: 2")171
; => (#{ a: 1 } #{ b: 2 })173
;; yaml-read / yaml-decode only reads the first document174
(yaml-decode "---\na: 1\n---\nb: 2")175
; => #{ a: 1 }176
```178
## Anchors and Aliases180
```scheme181
(yaml-decode "defaults: &def\n color: red\n size: large\nref: *def")182
; => #{ defaults: #{ color: "red" size: "large" }183
; ref: #{ color: "red" size: "large" } }184
```186
## Null Handling188
Use `yaml-null?` to distinguish null from `#f`:190
```scheme191
(yaml-null? (yaml-decode "null")) ; => #t192
(yaml-null? (yaml-decode "~")) ; => #t193
(yaml-null? (yaml-decode "false")) ; => #f194
(yaml-null? #f) ; => #f195
```197
## API Reference199
| Procedure | Signature | Description |200
|-----------|-----------|-------------|201
| `yaml-read` | `(port) -> any` | Read one YAML document from a port |202
| `yaml-write` | `(value port [indent: N] [flow: bool]) -> void` | Write YAML to a port |203
| `yaml-decode` | `(string) -> any` | Decode a YAML string |204
| `yaml-encode` | `(value [indent: N] [flow: bool]) -> string` | Encode a value as YAML |205
| `yaml-read-all` | `(port) -> list` | Read all documents from a port |206
| `yaml-decode-all` | `(string) -> list` | Decode all documents from a string |207
| `yaml-null?` | `(value) -> boolean` | Check if value is YAML null |