Documents
The example parses the people document in your selected format. It then reads Ada's age through the document nodes. Typed reads give you these values without serializing the document.
Example
Read Ada's age
people_text holds this document:
{:#_ada {:name "Ada"
:role "engineer"
:age 36}
:#_bob {:name "Bob"
:role "engineer"
:age 41
:mentor {:#link "#_ada"}}
:#_cy {:name "Cy"
:role "designer"
:age 29}
:#_tx {:author "smoke"
:reason "seed"}}people := stardust.parse(people_text, .EDN) or_return
defer stardust.close(&people)
root := stardust.root(people) or_return
ada := stardust.member(people, root, "#_ada") or_return
age := stardust.member(people, ada, "age") or_return
result := stardust.i64(people, age) or_return{:value 36}Numbers
A document number is an i64 integer or a finite f64 value. The number type defines both.
- An integer read requires an integer node. A floating-point node can cause the read to fail.
- Choose the typed read that matches the node that you read.
Operations
| Operation | Returns | Summary |
|---|---|---|
| parse | document | parse(text, format) returns an owned document. |
| root | node | document.root() returns the root node ID. |
| count | count | document.count() returns the number of nodes. |
| read | node_view | document.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text. |
| read_many | node_views | document.read_many(start, capacity) reads consecutive node views. |
| member | node | document.member(id, key) returns the first matching object member ID. |
| child_iterator | node_views | document.children(id) iterates the node views of a node's children in order. |
| members | Unavailable | document.members(id) iterates the members of an object node in order, each with its key. |
| i64 | integer | Read an exact signed integer. |
| f64 | real | Read a finite floating-point number. |
| close | none | Release the document handle. |
parse
parse :: proc(
text: string,
format: Source_Format,
allocator := context.allocator,
) -> (
Document,
Error,
)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
text | string | Value | Required | Encoded input bytes in the selected format. TRON is binary. |
format | Source_Format | Value | Required | The codec to use for this operation. |
allocator | mem.Allocator | Value | context.allocator | Allocator for copied return values and diagnostics. The caller releases them with the same allocator. |
Errors
| Status | When |
|---|---|
.Invalid_JSON | JSON text is malformed |
.Invalid_Argument | the text is malformed, the format is unknown or a number is outside i64 or finite f64 |
.Out_Of_Memory | native memory is exhausted |
Examples
Parse the shared DUST expression fixture
expression_text holds these bytes:
// One expression built once and evaluated against several inputs. The
// evaluate examples load this file, build `expression`, evaluate it with each
// of `inputs` in order and compare each result with `expected`.
expression [+ ?x 1]
inputs [{x 1} {x 41}]
expected [2 42]result := stardust.parse(expression_text, .DUST) or_return
defer stardust.close(&result){:value {:$comment "One expression built once and evaluated against several inputs. The\nevaluate examples load this file, build `expression`, evaluate it with each\nof `inputs` in order and compare each result with `expected`."
:expression ["+" ?x 1]
:inputs [{:x 1} {:x 41}]
:expected [2 42]}}Report a malformed byte at offset zero
malformed holds these bytes:
]
failed, result := stardust.parse(malformed, .DUST)
defer stardust.close(&failed)
defer stardust.close(&result){:value {:code "invalid_dust"
:message "Invalid_Syntax at offset 0"
:path nil
:field nil
:entity nil
:offset 0
:status "invalid_argument"}}Report malformed DUST
malformed holds these bytes:
[
failed, result := stardust.parse(malformed, .DUST)
defer stardust.close(&failed)
defer stardust.close(&result){:value {:code "invalid_dust"
:message "Unexpected_End at offset 2"
:path nil
:field nil
:entity nil
:offset 2
:status "invalid_argument"}}C exports: stardust_parse, stardust_document_error
root
root :: proc(document: Document) -> (u32, Error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
Errors
| Status | When |
|---|---|
.Closed | the document is closed |
Examples
document/root
input_text holds this document:
{:x 41}source := stardust.parse(input_text, .EDN) or_return
defer stardust.close(&source)
result := i64(stardust.root(source) or_return){:value 0}C exports: stardust_document_root
count
count :: proc(document: Document) -> (builtin.i64, Error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
Errors
| Status | When |
|---|---|
.Closed | the document is closed |
Examples
document/count
input_text holds this document:
{:x 41}source := stardust.parse(input_text, .EDN) or_return
defer stardust.close(&source)
result := i64(stardust.count(source) or_return){:value 2}C exports: stardust_document_count
read
read :: proc(document: Document, id: u32) -> (Node_View, Error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | u32 | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
.Invalid_Argument | id is not a node of the document |
.Closed | the document is closed |
Notes
Text contains string values. Read numbers with i64 or f64.
Examples
document/read
input_text holds this document:
{:x 41}source := stardust.parse(input_text, .EDN) or_return
defer stardust.close(&source)
root := stardust.root(source) or_return
result := stardust.read(source, root) or_return{:value {:kind "object"
:boolean false
:parent nil
:position 0
:first_child 1
:next_sibling nil
:key ""
:text ""}}C exports: stardust_document_read_many
read_many
read_many :: proc(document: Document, start: u32, output: []Node_View) -> (int, Error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
start | u32 | Value | Required | The first node ID of the consecutive read. |
output | []Node_View | Value | Required | The maximum number of node views to read. For output slices, the call uses the caller's slice length. |
Errors
| Status | When |
|---|---|
.Invalid_Argument | start is past the last node or capacity is negative |
.Closed | the document is closed |
Examples
document/read_many
input_text holds this document:
{:x 41}source := stardust.parse(input_text, .EDN) or_return
defer stardust.close(&source)
root := stardust.root(source) or_return
views: [1]stardust.Node_View
_ = stardust.read_many(source, root, views[:]) or_return
result := views[0]{:value {:kind "object"
:boolean false
:parent nil
:position 0
:first_child 1
:next_sibling nil
:key ""
:text ""}}C exports: stardust_document_read_many
member
member :: proc(document: Document, id: u32, key: string) -> (u32, Error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | u32 | Value | Required | A node ID within the receiver document. |
key | string | Value | Required | The object member name to find. |
Errors
| Status | When |
|---|---|
.Not_Found | the object has no member with the key |
.Invalid_Argument | id is not an object node of the document |
.Closed | the document is closed |
Examples
document/member
input_text holds this document:
{:x 41}source := stardust.parse(input_text, .EDN) or_return
defer stardust.close(&source)
root := stardust.root(source) or_return
node := stardust.member(source, root, "x") or_return
result := i64(node){:value 1}C exports: stardust_document_member
child_iterator
child_iterator :: proc(doc: Document, id: u32) -> Child_IteratorParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
doc | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | u32 | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
.Invalid_Argument | id is not a node of the document |
.Closed | the document is closed |
Notes
Keep the document open during iteration. A scalar has no children. A read failure is reported once and ends iteration.
Examples
Iterate the children of a node
seed_text holds this document:
{:#_ada {:name "Ada"
:role "engineer"
:age 36}
:#_bob {:name "Bob"
:role "engineer"
:age 41
:mentor {:#link "#_ada"}}
:#_cy {:name "Cy"
:role "designer"
:age 29}
:#_tx {:author "smoke"
:reason "seed"}}seed := stardust.parse(seed_text, .EDN) or_return
defer stardust.close(&seed)
root := stardust.root(seed) or_return
it := stardust.child_iterator(seed, root)
result: i64 = 0
for view in stardust.iterate_children(&it) {
if len(view.key) > 0 {result += 1}
}
if it.err != nil {return it.err}{:value 4}C exports: stardust_document_read_many
members
child_iterator views carry each member key. Odin has no separate members iterator.
C exports: stardust_document_read_many
i64
i64 :: proc(document: Document, id: u32) -> (builtin.i64, Error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | u32 | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
.Invalid_Argument | the node is not an integer number |
.Closed | the document is closed |
Examples
document/i64
input_text holds this document:
{:x 41}source := stardust.parse(input_text, .EDN) or_return
defer stardust.close(&source)
root := stardust.root(source) or_return
node := stardust.member(source, root, "x") or_return
result := stardust.i64(source, node) or_return{:value 41}C exports: stardust_document_i64
f64
f64 :: proc(document: Document, id: u32) -> (builtin.f64, Error)Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | u32 | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
.Invalid_Argument | the node is not a number or its value overflows f64 |
.Closed | the document is closed |
Notes
Integer conversion can round values outside the exact f64 integer range.
Examples
document/f64
float_text holds this document:
1.0source := stardust.parse(float_text, .EDN) or_return
defer stardust.close(&source)
result := stardust.f64(source, stardust.root(source) or_return) or_return{:value 1.0}C exports: stardust_document_f64
close
close :: proc(document: ^Document)Consumes the receiver handle.
Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
document | ^Document | Consumed owner | Required | The document owner to release. Do not use it after this call. |
Examples
document/close
expression_text holds this document:
["+" ?x 1]source := stardust.parse(expression_text, .EDN) or_return
defer stardust.close(&source)
result := stardust.serialize(source, .EDN) or_return
defer delete(result)
stardust.close(&source){:value {:text "[\"+\" ?x 1]"}}C exports: stardust_document_release
node_view
This record contains the result of read and read_many. The key and text fields borrow the document until it closes.
| Field | Type | Unit | Absent | Meaning |
|---|---|---|---|---|
kind | Value_Kind | None | — | — |
boolean | bool | None | — | True only for true. |
parent | u32 | None | NO_NODE | — |
position | u32 | Count | — | Index within the parent container. |
first_child | u32 | None | NO_NODE | — |
next_sibling | u32 | None | NO_NODE | — |
key | string | None | — | Member key of an object member, else empty. |
text | string | None | — | Value of a string, else empty. It can contain NUL. |
node_input
This record supplies a node for add and add_many. The number field holds a typed i64 or f64 value. Go uses I64/F64, Rust and Odin use Number, Clojure uses long or double, and Python uses int or float. This value sets the ABI number kind and the integer or real field.
| Field | Type | Absent | Meaning |
|---|---|---|---|
parent | u32 | NO_NODE | — |
kind | Value_Kind | — | — |
boolean | bool | — | Value of a boolean node. |
number | Number | nil | Value of a number node. |
key | string | — | Member key when the parent is an object. |
text | string | — | Value of a string node. |