src/toml/toml.bend checks
raw source on the hub · import ezx@1.15.0.0/src/toml/toml.bend as Toml
toml/toml: ez's view of a TOML document: sections of string pairs. ez's
ledger and lock are documents of tables of string (and one boolean) pairs,
and every command reads them as a list of sections, each a header's
segments and the pairs under it. Reading and writing TOML are eztoml's:
parse walks the document eztoml's parse reads into that list, and
normal writes a document through eztoml's render. Only eztoml's
interface, main.bend, is imported; that a document it renders reads back
as itself is trusted (below).
The rest is ez's own and pure: finding a key's value, a header's segments,
and the text of a section in the form ez assembles a document in before
eztoml writes it (render). That form is the TOML ez wrote before eztoml
0.4, so an old ledger or lock and a new one read to the same sections.
Trusted, not proved: a document eztoml renders reads back as itself, and a
text it reads without error renders back as the same document: eztoml's
laws/RenderThenParse.bend, laws/ParseRenderParse.bend and
laws/ParsedIsWellFormed.bend, proved in eztoml v0.9.0, the rev ez.toml
pins. ez imports only its interface, main.bend, and its gate does not
re-check them. ez relies on them for the lock reading back (trusted in
lock/lock.bend) and for laws/WrittenLedgerReadsBack.bend. ez's step
between its sections and eztoml's document (this file) is ez code with no
law: tests/toml.bend checks it on an old and a new ledger and lock.
2 imports
import Base import emerging-eztoml@0.9.0.0/main.bend as E
Types
type Kv source · line 28 · raw
Data
a key and the string written for it
Kv@key:String -> @val:String -> Kv
type Sect source · line 32 · raw
Data
a section header and the pairs under it, in the order they were written
Sect@name:String -> @pairs:List<&2, Kv> -> Sect
type Toml source · line 38 · raw
Data
a document read: the first error, "" when there is none, a name kept for the shape callers match on (always ""), the pairs above the first header, and every section that holds a pair, or holds no table, in document order
Toml@bad:String -> @name:String -> @pairs:List<&2, Kv> -> @done:List<&2, Sect> -> Toml
type Sym source · line 66 · raw
Data
what a header's next character is: the quoting toggles, an unquoted dot cuts, anything else is kept
SQuoteSym
SDotSym
SKeep@ch:Char -> Sym
type Split source · line 78 · raw
Data
a header being cut apart: quoted is true inside quotes, cur is the segment
being gathered and out the finished ones, both reversed
Split@quoted:Bool -> @cur:List<&2, Char> -> @out:List<&2, String> -> Split
Definitions
def value.at source · line 43 · raw
@kv:Kv -> @rest:String -> @key:String -> String
this pair's value when the key matches, otherwise whatever the rest of the table gave
def value source · line 49 · raw
@pairs:List<&2, Kv> -> @+key:String -> String
the value written for a key in a table, or "" when it is absent. The scan recurses first and picks after, so no recursive call hides inside a branch.
def sects source · line 57 · raw
@doc:Toml -> List<&2, Sect>
the sections a parse found, in the order they were written
def seg.sym source · line 72 · raw
@+ch:Char -> Sym
a header char classified. +ch because it is both asked about and kept.
def seg.chars source · line 82 · raw
@buf:List<&2, Char> -> @acc:String -> String
the chars gathered so far, last read first, spelled in the order they arrived
def seg.done source · line 90 · raw
@cur:List<&2, Char> -> String
the segment gathered so far, in order
def seg.dot source · line 94 · raw
@quoted:Bool -> @cur:List<&2, Char> -> @out:List<&2, String> -> Split
a dot inside quotes is part of the segment; outside, it ends one
def seg.go source · line 102 · raw
@st:Split -> @sym:Sym -> Split
the state once a char has been classified
def seg.step source · line 113 · raw
@st:Split -> @ch:Char -> Split
one char of a header against the state
def seg.fin source · line 117 · raw
@st:Split -> List<&2, String>
the segment still being gathered, closed, and the rest put back in order
def seg.walk source · line 122 · raw
@cs:List<&2, Char> -> @st:Split -> Split
the walk one char at a time, written out rather than folded
def segments source · line 131 · raw
@head:String -> List<&2, String>
a header's segments, unquoted. packages."0x0a".files is three of them, and a
quoted segment may hold the dots a bare one may not.
def quote source · line 136 · raw
@+text:String -> String
a string in quotes, which is how a segment or a key that is not bare is written, and how every value is
def bare.at source · line 141 · raw
@+ch:Char -> Bool
a char a bare key may hold. +ch because it is asked four questions; a
predicate handed to a fold may not take one, so it is split out here.
def bare.all.step source · line 148 · raw
@here:Bool -> @rest:(@_:Unit -> Bool) -> Bool
one step of the walk, with the head's answer in hand. Bool.and runs both
of its sides, so the first char that a bare key may not hold still cost a
walk of the rest of the key.
def bare.all source · line 158 · raw
@cs:List<&2, Char> -> Bool
whether every char of a key is one a bare key may hold. Base's List.all
applies an erased function parameter and so falls outside the termination
check; this walk is structural.
def bare source · line 166 · raw
@+text:String -> Bool
whether a key can be written without quotes
def key source · line 170 · raw
@+text:String -> String
a key as it is written: bare when it can be, quoted when it must be
def render.pair source · line 177 · raw
@kv:Kv -> String
one key = "value" line
def render.pairs source · line 182 · raw
@ps:List<&2, Kv> -> String
every pair of a section, in the order it holds them
def render.sect source · line 191 · raw
@sect:Sect -> String
one table: its header, then its pairs. The header is written as it is given, so a caller that needs a quoted segment quotes it.
def render.all source · line 196 · raw
@ss:List<&2, Sect> -> List<&2, String>
every table of a document
def render source · line 205 · raw
@ss:List<&2, Sect> -> String
a document of tables, one blank line between them, as ez assembles it;
normal is what is written
def normal source · line 209 · raw
@text:String -> String
a document written as eztoml writes it: read by eztoml, and rendered back
def flag.text source · line 216 · raw
@bit:Bool -> String
a truth value as TOML writes it
def pair.of source · line 226 · raw
@+name:String -> @val:0xc5d9d3e28b79ea98be22eb42c7e37802/main.Val -> List<&2, Kv>
a pair whose value is written as text: a string or a boolean, as ez writes them; an integer as its digits. A table, an array or an inline table is not a pair of the section.
def pairs.of source · line 240 · raw
@rows:List<&2, 0xc5d9d3e28b79ea98be22eb42c7e37802/main.Val> -> List<&2, Kv>
the pairs among a table's rows, in order
def row.table source · line 250 · raw
@row:0xc5d9d3e28b79ea98be22eb42c7e37802/main.Val -> Bool
whether a row is a table
def has.table.step source · line 258 · raw
@here:Bool -> @rest:(@_:Unit -> Bool) -> Bool
the rest of the walk, when this row was not a table
def has.table source · line 266 · raw
@rows:List<&2, 0xc5d9d3e28b79ea98be22eb42c7e37802/main.Val> -> Bool
whether a table's rows hold a table, stopping at the first
def head.of source · line 274 · raw
@segs:List<&2, String> -> String
a header as ez names it: its segments, each bare or quoted, joined by dots
def sect.put source · line 288 · raw
@skip:Bool -> @+ps:List<&2, Kv> -> @+segs:List<&2, String> -> List<&2, Sect>
a table's section, when it holds a pair or no table. A table that holds
only tables is the parent a dotted header implies, which ez never writes a
pair into; old ledgers and locks name no such table, so it is no section.
(segs arrive innermost first, as tables builds them; the header reads
them outermost first)
def sect.keep source · line 295 · raw
@+ps:List<&2, Kv> -> @parent:Bool -> @+segs:List<&2, String> -> List<&2, Sect>
def table.sects source · line 299 · raw
@+segs:List<&2, String> -> @+sub:List<&2, 0xc5d9d3e28b79ea98be22eb42c7e37802/main.Val> -> @inner:List<&2, Sect> -> List<&2, Sect>
a table's section, then the sections of the tables in it
def tables source · line 305 · raw
@rows:List<&2, 0xc5d9d3e28b79ea98be22eb42c7e37802/main.Val> -> @+segs:List<&2, String> -> List<&2, Sect>
the sections of every table among a table's rows, in document order: each table's own section, then the tables in it, then the tables after it. The path to the table is kept innermost first, so a level prepends its name.
def parse.of source · line 316 · raw
@+doc:0xc5d9d3e28b79ea98be22eb42c7e37802/main.Doc -> Toml
a document eztoml read, as sections
def parse source · line 321 · raw
@text:String -> Toml
a document read from TOML text: eztoml's reading of it, as sections
def show.kv source · line 328 · raw
@kv:Kv -> String
a pair as key=value
def show.kvs source · line 333 · raw
@ps:List<&2, Kv> -> List<&2, String>
every pair of a section, shown
def show.sect source · line 341 · raw
@sect:Sect -> String
a section as name key=value ..
def show.sects source · line 346 · raw
@ss:List<&2, Sect> -> List<&2, String>
every section of a document, shown