### Handle YAML Parsing Errors Gracefully (Lisp) Source: https://context7.com/eudoxia0/cl-yaml/llms.txt Demonstrates how to catch and handle `yaml.error:parsing-error` conditions, which are signaled when YAML syntax errors occur. Includes examples of extracting error details and creating a safe parsing wrapper function. ```lisp ;; Handle parsing errors (handler-case (yaml:parse "invalid: [unclosed bracket") (yaml.error:parsing-error (e) (format t "Parse error at line ~A, column ~A: ~A~%" (yaml.error:line e) (yaml.error:column e) (yaml.error:message e)))) ;; Output: Parse error at line 1, column 27: did not find expected ',' or ']'. ;; Safe parsing wrapper (defun safe-parse (yaml-string) (handler-case (values (yaml:parse yaml-string) t) (yaml.error:parsing-error (e) (values nil nil e)))) (multiple-value-bind (result success error) (safe-parse "valid: yaml") (if success (format t "Parsed: ~A~%" result) (format t "Error: ~A~%" error))) ``` -------------------------------- ### Configure IEEE Float Handling Strategy (Lisp) Source: https://context7.com/eudoxia0/cl-yaml/llms.txt Explains how to configure the `yaml.float:*float-strategy*` variable to control the parsing of IEEE special floating-point values (NaN, infinity). Options include using keywords, signaling errors, or attempting best-effort conversion. ```lisp ;; Default strategy: use keywords (let ((yaml.float:*float-strategy* :keyword)) (yaml:parse "values: [.nan, .inf, -.inf]")) ;; => {"values" => (:NAN :+INF :-INF)} ;; Error strategy: signal condition on special values (let ((yaml.float:*float-strategy* :error)) (handler-case (yaml:parse "value: .nan") (yaml.error:unsupported-float-value () (format t "Cannot parse special float value")))) ;; Best-effort strategy: use implementation-specific values when available ;; On SBCL, returns actual NaN and infinity values (let ((yaml.float:*float-strategy* :best-effort)) (yaml:parse "inf: .inf")) ;; => {"inf" => } ``` -------------------------------- ### Low-Level String Emitter with yaml:with-emitter-to-string Source: https://context7.com/eudoxia0/cl-yaml/llms.txt The `yaml:with-emitter-to-string` macro creates a low-level emitter context for constructing YAML output as a string. This provides programmatic control over the YAML structure, suitable for complex or dynamic generation. ```lisp ;; Build complex YAML structure programmatically (yaml:with-emitter-to-string (em) (yaml:emit-stream (em) (yaml:emit-document (em) (yaml:emit-sequence (em) (yaml:emit-scalar em "item1") (yaml:emit-scalar em "item2") (yaml:emit-mapping (em) (yaml:emit-scalar em "nested") (yaml:emit-scalar em "value")))))) ;; => "--- ;; - item1 ;; - item2 ;; - nested: value ;; ..." ``` -------------------------------- ### Low-Level Stream Emitter with yaml:with-emitter-to-stream Source: https://context7.com/eudoxia0/cl-yaml/llms.txt The `yaml:with-emitter-to-stream` macro establishes a low-level emitter context for writing YAML to a stream. It allows for fine-grained control over the YAML output, including explicit document and element boundaries. ```lisp ;; Write YAML with explicit document boundaries (with-open-file (stream "/tmp/config.yaml" :direction :output :if-exists :supersede) (yaml:with-emitter-to-stream (em stream) (yaml:emit-stream (em) (yaml:emit-document (em) (yaml:emit-mapping (em) (yaml:emit-scalar em "server") (yaml:emit-mapping (em) (yaml:emit-scalar em "host") (yaml:emit-scalar em "localhost") (yaml:emit-scalar em "port") (yaml:emit-scalar em 8080))))))) ``` -------------------------------- ### Emit Pretty YAML Document with yaml:emit-pretty-as-document Source: https://context7.com/eudoxia0/cl-yaml/llms.txt The `yaml:emit-pretty-as-document` function formats Lisp data into a human-readable, block-style YAML document. It's typically used within an emitter context provided by `yaml:with-emitter-to-string`. ```lisp ;; Pretty print a hash table as a YAML document (yaml:with-emitter-to-string (em) (yaml:emit-pretty-as-document em (alexandria:plist-hash-table '("name" "MyApp" "version" "1.0" "features" ("auth" "logging" "caching") "database" ("host" "localhost" "port" 5432))))) ;; => "--- ;; name: MyApp ;; version: \"1.0\" ;; features: ;; - auth ;; - logging ;; - caching ;; database: ;; - host ;; - localhost ;; - port ;; - 5432 ;; ..." ``` -------------------------------- ### YAML to Lisp Type Conversions Reference (Lisp) Source: https://context7.com/eudoxia0/cl-yaml/llms.txt Provides a reference for the default type conversions performed by cl-yaml when parsing YAML according to the Core Schema. Covers null, booleans, integers, floats, strings, and collections. ```lisp ;; Null values (yaml:parse "value: null") ;; => {"value" => NIL} (yaml:parse "value: ~") ;; => {"value" => NIL} ;; Booleans (yaml:parse "yes: true") ;; => {"yes" => T} (yaml:parse "no: false") ;; => {"no" => NIL} ;; Integers (decimal, octal, hex) (yaml:parse "decimal: 42") ;; => {"decimal" => 42} (yaml:parse "negative: -17") ;; => {"negative" => -17} (yaml:parse "octal: 0o755") ;; => {"octal" => 493} (yaml:parse "hex: 0xFF") ;; => {"hex" => 255} ;; Floating-point (yaml:parse "pi: 3.14159") ;; => {"pi" => 3.14159d0} (yaml:parse "sci: 1.2e10") ;; => {"sci" => 1.2d10} ;; Strings (quoted strings preserved as-is) (yaml:parse "name: 'true'") ;; => {"name" => "true"} (not boolean) (yaml:parse "num: \"42\"") ;; => {"num" => "42"} (not integer) ;; Collections (yaml:parse "[1, 2, 3]") ;; => (1 2 3) (yaml:parse "{a: 1}") ;; => {"a" => 1} (hash-table) ``` -------------------------------- ### Register Custom Mapping Converter for YAML Tags (Lisp) Source: https://context7.com/eudoxia0/cl-yaml/llms.txt Illustrates registering custom functions to parse YAML mappings (hash tables) with specific tags, converting them into Lisp plists or other structures. This provides a way to map YAML objects to custom Lisp representations. ```lisp ;; Register a converter to create a struct-like plist (yaml:register-mapping-converter "!person" (lambda (hash-table) (list :type :person :name (gethash "name" hash-table) :age (gethash "age" hash-table)))) (yaml:parse "user: !person name: Alice age: 30") ;; => {"user" => (:TYPE :PERSON :NAME "Alice" :AGE 30)} ``` -------------------------------- ### Parse YAML Strings and Paths Source: https://github.com/eudoxia0/cl-yaml/blob/master/README.md Parses a YAML string or a pathname into Common Lisp values. Handles basic data types, lists, and maps. Can optionally parse multi-document YAML. ```lisp CL-USER> (yaml:parse "[1, 2, 3]") (1 2 3) CL-USER> (yaml:parse "{ a: 1, b: 2 }") {"a" => 1, "b" => 2} CL-USER> (yaml:parse "- Mercury - Venus - Earth - Mars") ("Mercury" "Venus" "Earth" "Mars") CL-USER> (yaml:parse "foo --- bar" :multi-document-p t) (:DOCUMENTS "foo" "bar") ``` -------------------------------- ### Emit Lisp Data to Stream with yaml:emit Source: https://context7.com/eudoxia0/cl-yaml/llms.txt The `yaml:emit` function serializes a Common Lisp value into YAML format and writes it to a specified output stream. It uses compact inline notation and supports various Lisp data types. ```lisp ;; Emit a list to standard output (yaml:emit (list 1 2 3) *standard-output*) ;; Prints: [1, 2, 3] ;; Emit with booleans (yaml:emit (list t nil 42 3.14) *standard-output*) ;; Prints: [true, false, 42, 3.14] ;; Emit a hash table (yaml:emit (alexandria:alist-hash-table '(("name" . "Alice") ("age" . 25))) *standard-output*) ;; Prints: { name: "Alice", age: 25 } ;; Emit to a file (with-open-file (stream "/tmp/output.yaml" :direction :output :if-exists :supersede) (yaml:emit (list "config" (alexandria:plist-hash-table '("key" "value"))) stream)) ``` -------------------------------- ### Register Custom Scalar Converter for YAML Tags (Lisp) Source: https://context7.com/eudoxia0/cl-yaml/llms.txt Demonstrates registering custom functions to parse YAML scalars with specific tags like '!date' and '!uuid'. This allows for custom data type transformations during YAML parsing. It requires the cl-ppcre library for string splitting. ```lisp ;; Register a converter for a custom date tag (yaml:register-scalar-converter "!date" (lambda (string) (let ((parts (cl-ppcre:split "-" string))) (list :year (first parts) :month (second parts) :day (third parts))))) ;; Now parsing YAML with the tag uses the custom converter (yaml:parse "created: !date 2024-01-15") ;; => {"created" => (:YEAR "2024" :MONTH "01" :DAY "15")} ;; Register a UUID converter (yaml:register-scalar-converter "!uuid" (lambda (string) (list :uuid string))) (yaml:parse "id: !uuid 550e8400-e29b-41d4-a716-446655440000") ;; => {"id" => (:UUID "550e8400-e29b-41d4-a716-446655440000")} ``` -------------------------------- ### Emit Common Lisp Values to YAML Source: https://github.com/eudoxia0/cl-yaml/blob/master/README.md Emits Common Lisp values into a YAML stream or a string. Supports lists, hash tables, and basic data types. The `emit` function writes to a stream, while `emit-to-string` returns a string representation. ```lisp CL-USER> (yaml:emit-to-string (list 1 2 3)) "[1, 2, 3]" CL-USER> (yaml:emit-to-string (alexandria:alist-hash-table '(("a" . 1) ("b" . 2)))) "{ b: 2, a: 1 }" CL-USER> (yaml:emit (list t 123 3.14) *standard-output*) [true, 123, 3.14] ``` -------------------------------- ### Parse YAML to Lisp Data with yaml:parse Source: https://context7.com/eudoxia0/cl-yaml/llms.txt The `yaml:parse` function converts YAML strings or file paths into native Common Lisp data structures. It supports single and multi-document YAML files and handles various data types including lists, hash tables, and nested structures. ```lisp ;; Parse a simple list (yaml:parse "[1, 2, 3]") ;; => (1 2 3) ;; Parse a hash table (map) (yaml:parse "{ name: John, age: 30 }") ;; => {"name" => "John", "age" => 30} ;; Parse a YAML block-style list (yaml:parse "- Mercury - Venus - Earth - Mars") ;; => ("Mercury" "Venus" "Earth" "Mars") ;; Parse nested structures (yaml:parse "users: - name: Alice role: admin - name: Bob role: user") ;; => {"users" => ({"name" => "Alice", "role" => "admin"} {"name" => "Bob", "role" => "user"})} ;; Parse multi-document YAML (yaml:parse "document1 --- document2 --- document3" :multi-document-p t) ;; => (:DOCUMENTS "document1" "document2" "document3") ;; Parse from a file (yaml:parse #p"/path/to/config.yaml") ;; => ``` -------------------------------- ### Register Custom Sequence Converter for YAML Tags (Lisp) Source: https://context7.com/eudoxia0/cl-yaml/llms.txt Shows how to register custom functions for parsing YAML sequences with specific tags, transforming them into different Lisp data structures like vectors or sets. This enables flexible handling of sequence data. ```lisp ;; Register a converter to turn sequences into vectors (yaml:register-sequence-converter "!vector" (lambda (list) (coerce list 'vector))) (yaml:parse "data: !vector [1, 2, 3, 4, 5]") ;; => {"data" => #(1 2 3 4 5)} ;; Register a set converter (removes duplicates) (yaml:register-sequence-converter "!set" (lambda (list) (remove-duplicates list :test #'equal))) (yaml:parse "items: !set [a, b, a, c, b]") ;; => {"items" => ("a" "b" "c")} ``` -------------------------------- ### Emit Lisp Data to String with yaml:emit-to-string Source: https://context7.com/eudoxia0/cl-yaml/llms.txt The `yaml:emit-to-string` function converts a Common Lisp value into its YAML string representation. This is useful for generating YAML output without writing directly to a stream. ```lisp ;; Convert a list to YAML string (yaml:emit-to-string (list 1 2 3)) ;; => "[1, 2, 3]" ;; Convert a hash table to YAML string (yaml:emit-to-string (alexandria:alist-hash-table '(("a" . 1) ("b" . 2)))) ;; => "{ a: 1, b: 2 }" ;; Convert nested structures (yaml:emit-to-string (alexandria:plist-hash-table (list "name" "MyApp" "ports" (list 8080 8443) "enabled" t))) ;; => "{ name: \"MyApp\", ports: [8080, 8443], enabled: true }" ;; Convert a vector (yaml:emit-to-string #(1 2 3 4 5)) ;; => "[1, 2, 3, 4, 5]" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.