Skip to content

Commit

Permalink
feat: Support (lightly tested) nested TOML Table Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushalmodi committed Apr 30, 2022
1 parent a33dbd1 commit 10a1994
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
14 changes: 8 additions & 6 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ the Emacs core library [[https://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/j

It will then be gradually refactored so that it meets the
specification defined below.
* Library Completion Status [5/7]
* Library Completion Status [6/7]
- [X] Scalar
- [X] Boolean
- [X] Integer
Expand All @@ -32,9 +32,9 @@ specification defined below.
- [X] Tables
- [X] Basic Tables
- [X] Nested Tables
- [-] Array of Tables
- [X] Array of Tables
- [X] Basic Array of Tables
- [ ] Nested Array of Tables
- [X] Nested Array of Tables
- [ ] Property Lists
* Specification and Conversion Examples
[[https://scripter.co/defining-tomelr/][Companion blog post]]
Expand Down Expand Up @@ -567,7 +567,8 @@ See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D
]
}
#+end_example
*** Nested Array of Tables
*** DONE Nested Array of Tables
CLOSED: [2022-04-30 Sat 01:32]
**** S-expression
#+begin_src emacs-lisp :eval no :noweb-ref nested-table-arrays
'((fruits . (((name . "apple")
Expand All @@ -584,13 +585,14 @@ See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D
<<nested-table-arrays>>)
#+end_src

#+RESULTS:
#+begin_src toml
[[fruits]]
name = "apple"
[fruits.physical] # subtable
[fruits.physical]
color = "red"
shape = "round"
[[fruits.varieties]] # nested array of tables
[[fruits.varieties]]
name = "red delicious"
[[fruits.varieties]]
name = "granny smith"
Expand Down
40 changes: 20 additions & 20 deletions test/ttable-array.el
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D
((fruits . (((name . "apple")
(varieties . (((name . "red delicious"))
((name . "granny smith"))))))))
;; ((fruits . (((name . "apple")
;; (physical . ((color . "red")
;; (shape . "round")))
;; (varieties . (((name . "red delicious"))
;; ((name . "granny smith")))))
;; ((name . "banana")
;; (varieties . (((name . "plantain"))))))))
((fruits . (((name . "apple")
(physical . ((color . "red")
(shape . "round")))
(varieties . (((name . "red delicious"))
((name . "granny smith")))))
((name . "banana")
(varieties . (((name . "plantain"))))))))
))
(ref '(
"[[fruits]]
Expand All @@ -106,19 +106,19 @@ See [org#Drawers](https://www.gnu.org/software/emacs/manual/html_mono/org.html#D
name = \"red delicious\"
[[fruits.varieties]]
name = \"granny smith\""
;; "[[fruits]]
;; name = \"apple\"
;; [fruits.physical]
;; color = \"red\"
;; shape = \"round\"
;; [[fruits.varieties]]
;; name = \"red delicious\"
;; [[fruits.varieties]]
;; name = \"granny smith\"
;; [[fruits]]
;; name = \"banana\"
;; [[fruits.varieties]]
;; name = \"plantain\""
"[[fruits]]
name = \"apple\"
[fruits.physical]
color = \"red\"
shape = \"round\"
[[fruits.varieties]]
name = \"red delicious\"
[[fruits.varieties]]
name = \"granny smith\"
[[fruits]]
name = \"banana\"
[[fruits.varieties]]
name = \"plantain\""
))
out)
(dolist (el inp)
Expand Down
23 changes: 15 additions & 8 deletions tomelr.el
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ Dictates repetitions of `tomelr-encoding-default-indentation'.")
This variable is used for both TOML Tables and Arrays of TOML
Tables.")

(defvar tomelr--print-table-array-key ""
"Internal variable used to save the TOML Table Array name.")

(defvar tomelr--print-keyval-separator " = "
"String used to separate key-value pairs during encoding.")

Expand Down Expand Up @@ -242,9 +239,7 @@ Return nil if OBJECT cannot be encoded as a TOML string."
((equal type 'table)
(princ (format "[%s]" (string-join tomelr--print-table-hierarchy "."))))
((equal type 'table-array)
(let ((tta-name (format "[[%s]]" (string-join tomelr--print-table-hierarchy "."))))
(setq tomelr--print-table-array-key tta-name)
(princ tta-name)))
(princ (format "[[%s]]" (string-join tomelr--print-table-hierarchy "."))))
((stringp object)
;; (message "[tomelr--print-stringlike DBG] %S is string" object)
(tomelr--print-string sym-name))
Expand Down Expand Up @@ -423,6 +418,19 @@ Definition of a TOML Table Array (TTA):
nil))))
ttap))

(defun tomelr--print-tta-key ()
"Print TOML Table Array key."
;; (message "[tomelr--print-array DBG] depth = %d" tomelr--print-indentation-depth)
;; Throw away table keys collected at higher depths, if
;; any, from earlier runs of this function.
(setq tomelr--print-table-hierarchy
(seq-take tomelr--print-table-hierarchy
(1+ tomelr--print-indentation-depth)))

(tomelr--print-indentation)
(insert
(format "[[%s]]" (string-join tomelr--print-table-hierarchy "."))))

(defun tomelr--print-array (array)
"Insert a TOML representation of ARRAY at point.
See `tomelr-encode-array' that returns the same as a string."
Expand All @@ -435,8 +443,7 @@ See `tomelr-encode-array' that returns the same as a string."
(mapc (lambda (elt)
(if first
(setq first nil)
(tomelr--print-indentation)
(insert tomelr--print-table-array-key))
(tomelr--print-tta-key))
(tomelr--print elt))
array))))
(t
Expand Down

0 comments on commit 10a1994

Please sign in to comment.