Skip to content

Commit 2af6eb3

Browse files
committed
✨ feat: add functions to stringify and parse TOON strings
1 parent fc3923b commit 2af6eb3

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

crates/mq-lang/modules/toon.mq

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,15 @@ def _toon_stringify_recursive(data, level, delim):
140140
else: to_string(data)
141141
end
142142

143+
# To convert a data structure into a TOON string
143144
def toon_stringify(data): _toon_stringify_recursive(data, 0, ",");
144145

145146
# --- Parse ---
146147

147148
def _toon_parse_primitive(s):
148149
let ss = trim(s)
149150
| if (starts_with(ss, "\"") && ends_with(ss, "\"")):
150-
replace(replace(replace(replace(replace(slice(ss, 1, -1), "\\\"", "\""), "\\n", "\n"), "\\r", "\r"), "\\t", "\t"), "\\\\", "\\")
151+
_toon_escape_string(slice(ss, 1, -1))
151152
elif (ss == "true"): true
152153
elif (ss == "false"): false
153154
elif (ss == "null"): None
@@ -294,9 +295,9 @@ def _toon_parse_recursive(lines, start_i, target_indent, active_delim):
294295
| if (is_none(loop_res)): [result, i] else: loop_res
295296
end
296297

298+
# To parse a TOON string into a data structure
297299
def toon_parse(input):
298300
let lines = split(to_string(input), "\n")
299301
| let res = _toon_parse_recursive(lines, 0, 0, ",")
300302
| res[0]
301303
end
302-

0 commit comments

Comments
 (0)