data formats: comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:51:53 -05:00
1 parent 0bd7b87d2b
commit 0452f26eef
18 files changed
+110 -224

No files matched your search

+6 -17
View File
@@ -1,9 +1,5 @@
--!strict
-- data/yaml/Emit: the YAML writer behind data/yaml. Block style; empty collections as [] / {}; strings plain when
-- they read back as the same string (core schema, and not a YAML 1.1 boolean like "yes"), literal block scalars
-- for multi-line text, double-quoted otherwise; numbers with the shortest round-trip text, .inf / .nan; buffers as
-- !!binary; dates as their RFC 3339 text; tables referenced twice get an anchor and aliases. Never throws: bad
-- values (cycles, functions, collection keys) return nil, message.
-- data/yaml/Emit: YAML block-style writer; plain scalars only when they read back the same (core schema, no 1.1 bools).
local Types = require("@rosetta/Types")
local Base64 = require("@rosetta/core/Base64")
local Parse = require("@rosetta/data/yaml/Parse")
@@ -11,11 +7,11 @@ local Parse = require("@rosetta/data/yaml/Parse")
local Emit = {}
export type Options = {
indent: number?, -- spaces per level (default 2)
all: boolean?, -- the value is an array of documents, written with "---" separators
explicitStart: boolean?, -- start the (first) document with "---" (default false)
sortKeys: boolean?, -- plain tables' keys sorted (default true; Types.object keys keep their order)
null: any?, -- the value written as null besides Types.null
indent: number?, -- default 2
all: boolean?,
explicitStart: boolean?, -- default false
sortKeys: boolean?, -- default true
null: any?,
}
local format, byte, find, gsub = string.format, string.byte, string.find, string.gsub
@@ -96,7 +92,6 @@ local function plainOk(s: string): boolean
return true
end
-- a string as a scalar on a line at indentation ind (for block scalars)
local function str(s: string, ind: string, key: boolean): string
if plainOk(s) then
return s
@@ -126,7 +121,6 @@ local keyLess = Types.keyLess
type St = { out: { string }, step: number, sort: boolean, null: any, refs: { [any]: number }, anchors: { [any]: string }, nextId: number, err: string? }
-- count references to tables (anchors for those seen twice); false on a cycle
local function count(st: St, v: any, onStack: { [any]: boolean }): boolean
if type(v) ~= "table" or v == Types.null or v == st.null or Types.Date.is(v) then
return true
@@ -150,7 +144,6 @@ local function count(st: St, v: any, onStack: { [any]: boolean }): boolean
return true
end
-- scalar text for a non-collection value, or nil
local function scalar(st: St, v: any, ind: string, key: boolean): string?
local tv = type(v)
if v == nil or v == Types.null or (st.null ~= nil and v == st.null) then
@@ -171,7 +164,6 @@ end
local write: (st: St, v: any, depth: number) -> string?
-- the text of v as a node whose first line follows "key:" / "- " (it starts with a space or a newline)
function write(st: St, v: any, depth: number): string?
local ind = string.rep(" ", st.step * (depth + 1))
local s = scalar(st, v, ind, false)
@@ -244,19 +236,16 @@ local function document(st: St, v: any): string?
if not t then
return nil
end
-- the top level: drop the separator write() put before the first line
if string.sub(t, 1, 1) == "\n" then
return string.sub(t, 2)
end
local body = string.sub(t, 2)
if string.sub(body, 1, 1) == "&" then
-- "&id001\n- a": an anchored top-level collection
return body
end
return body
end
-- encode(v, opts) -> text | nil, message
function Emit.encode(v: any, opts: Options?): (string?, string?)
local o: Options = opts or {}
local st: St = {
+13 -44
View File
@@ -1,11 +1,5 @@
--!strict
-- data/yaml/Parse: the YAML 1.2 parser behind data/yaml. Recursive descent over the text (line breaks normalized):
-- block mappings / sequences (compact forms included), flow collections, plain / single / double-quoted scalars with
-- line folding, literal / folded block scalars (indentation and chomping indicators), anchors and aliases (shared
-- references, with an expansion-size limit), tags (core types + !!binary, !!timestamp; others UNSUPPORTED unless
-- opts.tags = "ignore"), %YAML / %TAG directives, multi-document streams, comments. Scalars resolve with the core
-- schema. Not supported: complex keys ("? "), collections as keys, recursive aliases, merge keys "<<" (kept as a
-- normal key). Internally raises { yamlErr } tables caught by run(); never throws past it.
-- data/yaml/Parse: YAML 1.2 recursive-descent parser; raises { yamlErr } tables caught by run(), never throws past it.
local Text = require("@rosetta/codecs/text/Text")
local Types = require("@rosetta/Types")
local Base64 = require("@rosetta/core/Base64")
@@ -15,11 +9,11 @@ local Parse = {}
local byte, sub, find, match, char = string.byte, string.sub, string.find, string.match, string.char
export type Options = {
all: boolean?, -- every document as an array (default false: the first document; none -> Types.null)
tags: ("error" | "ignore")?, -- unknown tags: an UNSUPPORTED error (default) or resolved as if untagged
maxAliasNodes: number?, -- the total size of aliased nodes (default 1e6): the billion laughs guard
maxDepth: number?, -- nesting limit (default 500)
null: any?, -- the value for null (default Types.null)
all: boolean?, -- default false
tags: ("error" | "ignore")?, -- default "error"
maxAliasNodes: number?, -- default 1e6 (billion laughs guard)
maxDepth: number?, -- default 500
null: any?, -- default Types.null
}
type St = {
@@ -54,7 +48,7 @@ local function col(s: string, p: number): number
return p - q
end
local function isWsEnd(c: number?): boolean -- space, tab, newline or the end
local function isWsEnd(c: number?): boolean
return c == nil or c == 32 or c == 9 or c == 10
end
@@ -66,7 +60,6 @@ local function docMarker(s: string, p: number): boolean
return (m == "---" or m == "...") and isWsEnd(byte(s, p + 3))
end
-- skip spaces, tabs, comments and line breaks: the next content position (n + 1 at the end)
local function skip(s: string, p: number): number
local n = #s
while p <= n do
@@ -86,7 +79,6 @@ local function skip(s: string, p: number): number
return p
end
-- the number of leading spaces on p's line, and whether a tab comes before p on it
local function lineIndent(s: string, p: number): (number, boolean)
local q = p
while q > 1 and byte(s, q - 1) ~= 10 do
@@ -105,7 +97,6 @@ local function noTabs(s: string, p: number, ind: number?)
end
end
-- skip spaces / tabs on the line
local function skipLine(s: string, p: number): number
return match(s, "^[ \t]*()", p) :: any
end
@@ -120,7 +111,6 @@ end
local utf8char = Text.utf8char
-- ------------------------------------------------------------------------------------------------ resolution
-- the core schema: a plain scalar's value
function Parse.resolve(t: string, nullv: any): any
if t == "" or t == "~" or t == "null" or t == "Null" or t == "NULL" then
return nullv
@@ -168,7 +158,6 @@ local function base64(t: string): buffer?
return (Base64.decode(t))
end
-- a scalar's value given its tag (nil = untagged, "!" = non-specific)
local function scalarValue(st: St, raw: string, plain: boolean, tag: string?, p: number): any
if tag == nil then
return if plain then Parse.resolve(raw, st.null) else raw
@@ -237,7 +226,6 @@ local function collectionTag(st: St, tag: string?, kind: string, p: number)
end
end
-- expansion size of a value (nodes), memoized
local function size(st: St, v: any): number
if type(v) ~= "table" or v == st.null then
return 1
@@ -257,7 +245,6 @@ end
-- ------------------------------------------------------------------------------------------------ properties
type Props = { anchor: string?, tag: string?, at: number }
-- anchors / tags at p -> props?, the position after them (and the spaces after them)
local function properties(st: St, p: number): (Props?, number)
local s = st.s
local props: Props? = nil
@@ -348,8 +335,7 @@ local function alias(st: St, p: number): (any, number)
end
-- ------------------------------------------------------------------------------------------------ scalars
-- fold the line break(s) at p (p at a '\n'): the text they become and the position of the next line's content.
-- Lines holding only spaces / tabs count as empty.
-- line folding: lines holding only spaces / tabs count as empty
local function fold(s: string, p: number): (string, number)
local empties = 0
local q = p + 1
@@ -385,7 +371,6 @@ local ESC: { [number]: string } = {
[80] = "\226\128\169",
}
-- a quoted scalar at p ('"' or "'") -> text, the position after the closing quote
local function quoted(st: St, p: number, ind: number): (string, number)
local s = st.s
local dq = byte(s, p) == 34
@@ -456,15 +441,13 @@ local function quoted(st: St, p: number, ind: number): (string, number)
end
end
-- a plain scalar at p -> text, the position after it. ind: continuation lines must be indented deeper than ind
-- (block context); flow: the flow-context rules (stops at , [ ] { }).
-- plain scalar: continuation lines indented deeper than ind (block context); flow context stops at , [ ] { }
local function plain(st: St, p: number, ind: number, flow: boolean): (string, number)
local s = st.s
local parts: { string } = {}
local i = p
local lastEnd = p
while true do
-- one line's worth
local j = i
local stopAt = nil
while true do
@@ -492,7 +475,6 @@ local function plain(st: St, p: number, ind: number, flow: boolean): (string, nu
if stopAt or byte(s, j) == nil then
return table.concat(parts), lastEnd
end
-- a continuation line?
local f, q = fold(s, j)
local r = skipLine(s, q)
local c = byte(s, r)
@@ -507,7 +489,6 @@ local function plain(st: St, p: number, ind: number, flow: boolean): (string, nu
end
end
-- a block scalar at p ('|' or '>'); ind = the parent's indentation -> text, the position after it
local function blockScalar(st: St, p: number, ind: number): (string, number)
local s = st.s
local folded = byte(s, p) == 62
@@ -529,7 +510,7 @@ local function blockScalar(st: St, p: number, ind: number): (string, number)
end
local nl = find(s, "\n", q, true)
local lines: { string } = {}
local trailing = 0 -- empty lines after the last content line
local trailing = 0
if not nl then
return "", #s + 1
end
@@ -568,7 +549,6 @@ local function blockScalar(st: St, p: number, ind: number): (string, number)
indent = ind + 1
end
end
-- the lines
local r = ls
local pendingEmpty = 0
local endPos = ls
@@ -625,7 +605,7 @@ local function blockScalar(st: St, p: number, ind: number): (string, number)
empties = 0
end
end
trailing += empties -- empty lines after the last content line (or all of them)
trailing += empties
body = table.concat(out)
end
if not folded then
@@ -666,7 +646,6 @@ local function enter(st: St, p: number)
end
end
-- a flow node at p (after skip) -> value, next, wasEmpty
local function flowNode(st: St, p: number, ind: number): (any, number, boolean)
local s = st.s
local props, q = properties(st, p)
@@ -688,7 +667,6 @@ local function flowNode(st: St, p: number, ind: number): (any, number, boolean)
local t, r = quoted(st, q, ind)
return finish(st, props, scalarValue(st, t, false, props and props.tag, q)), r, false
elseif c == nil or c == 44 or c == 93 or c == 125 or (c == 58 and (isWsEnd(byte(s, q + 1)) or byte(s, q + 1) == 44 or byte(s, q + 1) == 93 or byte(s, q + 1) == 125)) then
-- an empty node
return finish(st, props, scalarValue(st, "", true, props and props.tag, q)), (if props then q else p), true
elseif c == 35 or c == 38 or c == 33 or c == 124 or c == 62 or c == 37 or c == 64 or c == 96 then
corrupt("unexpected character in a flow collection", q)
@@ -819,7 +797,7 @@ end
-- ------------------------------------------------------------------------------------------------ block
local blockNode: (st: St, ind: number, p: number, inline: boolean, mapParent: boolean) -> (any, number)
-- an implicit key at p (on one line, followed by ':' and a space / the end of the line) -> key, position after ':'
-- implicit key: on one line, followed by ':' and a space or the end of the line
local function implicitKey(st: St, p: number): (any, number?)
local s = st.s
local c = byte(s, p)
@@ -847,7 +825,6 @@ local function implicitKey(st: St, p: number): (any, number?)
end
return nil, nil
elseif c == 91 or c == 123 then
-- a flow collection used as a key?
local e = find(s, "\n", p, true) or #s + 1
local line = sub(s, p, e - 1)
if match(line, "[%]}]%s*:%s") or match(line, "[%]}]%s*:$") then
@@ -892,7 +869,7 @@ local function readKey(st: St, p: number, props: Props?): (any, number)
end
k, q = alias(st, p)
q = skipLine(s, q)
q += 1 -- the ':'
q += 1
elseif c == 34 or c == 39 then
local t, r = quoted(st, p, -1)
if find(sub(s, p, r - 1), "\n", 1, true) then
@@ -912,7 +889,6 @@ local function readKey(st: St, p: number, props: Props?): (any, number)
return checkKey(st, k, p), q
end
-- a node on the lines after p's line (content deeper than ind; or a sequence at ind when seqAt) -> value, next | null
local function nextLineNode(st: St, ind: number, p: number, seqAt: boolean, mapParent: boolean): (any, number)
local s = st.s
local r = skip(s, p)
@@ -926,7 +902,6 @@ local function nextLineNode(st: St, ind: number, p: number, seqAt: boolean, mapP
return st.null, p
end
-- the value after a ':' (at p, just past it) of a mapping at column mc
local function mapValue(st: St, mc: number, p: number, explicit: boolean): (any, number)
local s = st.s
local r = skipLine(s, p)
@@ -1044,7 +1019,6 @@ local function blockSeq(st: St, sc: number, p: number): (any, number)
return t, q
end
-- the node at q after its properties (props may be nil); start = where the properties began
local function afterProps(st: St, ind: number, start: number, q: number, props: Props?, inline: boolean, mapParent: boolean): (any, number)
local s = st.s
local tag = props and props.tag
@@ -1111,9 +1085,6 @@ local function afterProps(st: St, ind: number, start: number, q: number, props:
return finish(st, props, scalarValue(st, text, true, tag, q)), e
end
-- a block node at p (content; col(p) > ind, or == ind for a sequence that is a mapping's value). inline: on the
-- same line as its mapping key (no new block collection may start). mapParent: a sequence at the parent's
-- indentation may follow properties that end their line.
function blockNode(st: St, ind: number, p: number, inline: boolean, mapParent: boolean): (any, number)
local s = st.s
local props, q = properties(st, p)
@@ -1253,7 +1224,6 @@ local function stream(st: St): { any }
p = q
else
if explicit and not find(sub(s, p, q), "\n", 1, true) then
-- content on the "---" line
local ok, _, q2 = pcall(properties, st, q)
local c = byte(s, q2)
if ok and c ~= 124 and c ~= 62 and not endOfLine(s, q2) and implicitKey(st, q2) ~= nil then
@@ -1282,7 +1252,6 @@ local function stream(st: St): { any }
return docs
end
-- run(text, opts) -> docs | nil, code, message, offset
function Parse.run(text: string, opts: Options?): (any, any, any, any)
local o: Options = opts or {}
local s = text
+3 -9
View File
@@ -1,11 +1,7 @@
--!strict
-- data/yaml: YAML 1.2 (core schema), new code. Supports: block and flow styles, compact nested collections, plain /
-- quoted / literal / folded scalars (indentation and chomping indicators), comments, anchors and aliases (decoded as
-- shared references; an expansion-size limit guards against alias bombs), tags !!str !!int !!float !!bool !!null
-- !!map !!seq !!binary (-> buffer) !!timestamp (-> Types.Date) and verbatim / %TAG forms, multi-document streams.
-- Maps decode to Types.object (key order kept), null to Types.null. Writes block style (anchors for shared tables).
-- Not supported (Err.UNSUPPORTED): complex keys "? ", collections as keys, other tags unless opts.tags = "ignore";
-- merge keys "<<" stay ordinary keys; YAML 1.1 types (yes / no, sexagesimal, ...) are strings.
-- data/yaml: YAML 1.2 (core schema), new code. Supports: block / flow styles, every scalar style, anchors and aliases
-- (shared, alias-bomb limit), core tags + !!binary / !!timestamp, %TAG, multi-document streams; writes block style.
-- Not supported (Err.UNSUPPORTED): complex keys, collections as keys; merge keys stay keys; YAML 1.1 types are strings.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -47,7 +43,6 @@ function Yaml.sniff(r: Bytes.Reader): number
end
-- ------------------------------------------------------------------------------------------------ decode
-- decode(r, opts?) -> the first document (Types.null when there is none) | every document (opts.all) | nil, Err
function Yaml.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
local docs, code, msg, at = Parse.run(text, opts)
@@ -65,7 +60,6 @@ function Yaml.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
end
-- ------------------------------------------------------------------------------------------------ encode
-- encode(value, opts?) -> buffer | nil, Err (opts.all: value is an array of documents)
function Yaml.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
local s, err = Emit.encode(value, opts)
if not s then