TUPLE: entry key value ;
+: check-no-key ( key assoc -- key assoc )
+ 2dup at* nip [ over duplicate-key ] when ;
+
+: entries-at ( table keys -- key entries )
+ unclip-last -rot [
+ over ?at [ nip ] [
+ H{ } clone [ spin check-no-key set-at ] keep
+ ] if
+ ] each ;
+
+GENERIC: update-toml ( root table entry -- root table' )
+
+M: entry update-toml
+ dupd [ key>> entries-at ] [ value>> ] bi
+ -rot check-no-key set-at ;
+
+M: table update-toml
+ nip dupd [ name>> entries-at ] [ array?>> ] bi
+ H{ } clone [
+ swap [ -rot push-at ] [ -rot check-no-key set-at ] if
+ ] keep ;
+
: ws ( -- parser )
[ " \t" member? ] satisfy repeat0 ;
: string-parser ( -- parser )
multi-string single-string 2choice ;
+: non-ascii ( -- parser )
+ 0x80 0xd7ff range 0xe000 0x10ffff range 2choice ;
+
+: comment-char ( -- parser )
+ 0x01 0x09 range 0x0e 0x7f range non-ascii 3choice ;
+
+: comment ( -- parser )
+ "#" token comment-char repeat0 2seq hide ;
+
+: ws-comment-newline ( -- parser )
+ ws comment optional 2seq newline list-of ;
+
: date-parser ( -- parser )
[
decdigit 4 exactly-n ,
timezone-parser optional ,
] seq* [ "" concat-as ] action ;
-DEFER: comment
-
: separator ( -- parser )
"," token comment optional 2seq ;
-DEFER: ws-comment-newline
-
DEFER: value-parser
: array-value-parser ( -- parser )
DEFER: key-value-parser
-DEFER: update-toml
-
: inline-table-key-value ( -- parser )
ws-comment-newline hide
key-value-parser
: quoted-key ( -- parser )
multi-string single-string 2choice ;
-: non-ascii ( -- parser )
- 0x80 0xd7ff range 0xe000 0x10ffff range 2choice ;
-
-: comment-char ( -- parser )
- 0x01 0x09 range 0x0e 0x7f range non-ascii 3choice ;
-
-: comment ( -- parser )
- "#" token comment-char repeat0 2seq hide ;
-
-: ws-comment-newline ( -- parser )
- ws comment optional 2seq newline list-of ;
-
: simple-key ( -- parser )
unquoted-key quoted-key 2choice ;
value-parser ,
] seq* [ first2 entry boa ] action ;
-ALIAS: name-parser unquoted-key
-
:: table-name-parser ( begin end array? -- parser )
[
begin token hide ,
: table-parser ( -- parser )
array-table std-table 2choice ;
-: toml-parser ( -- parser )
+PEG: parse-toml ( string -- ast )
ws hide key-value-parser ws hide comment optional hide 4seq
ws hide table-parser ws hide comment optional hide 4seq
ws hide comment optional hide 2seq
3choice newline list-of [ { } concat-as ] action ;
-: check-no-key ( key assoc -- key assoc )
- 2dup at* nip [ over duplicate-key ] when ;
-
-: entries-at ( table keys -- key entries )
- unclip-last -rot [
- over ?at [ nip ] [
- H{ } clone [ spin check-no-key set-at ] keep
- ] if
- ] each ;
-
-GENERIC: update-toml ( root table entry -- root table' )
-
-M: entry update-toml
- dupd [ key>> entries-at ] [ value>> ] bi
- -rot check-no-key set-at ;
-
-M: table update-toml
- nip dupd [ name>> entries-at ] [ array?>> ] bi
- H{ } clone [
- swap [ -rot push-at ] [ -rot check-no-key set-at ] if
- ] keep ;
-
-PEG: parse-toml ( string -- ast ) toml-parser ;
-
PRIVATE>
: toml> ( string -- assoc )