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

+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