sexp.nim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Andreas Rumpf, Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## **Note:** Import ``nimsuggest/sexp`` to use this module
  10. import
  11. hashes, strutils, lexbase, streams, unicode, macros
  12. import std/private/decode_helpers
  13. type
  14. SexpEventKind* = enum ## enumeration of all events that may occur when parsing
  15. sexpError, ## an error occurred during parsing
  16. sexpEof, ## end of file reached
  17. sexpString, ## a string literal
  18. sexpSymbol, ## a symbol
  19. sexpInt, ## an integer literal
  20. sexpFloat, ## a float literal
  21. sexpNil, ## the value ``nil``
  22. sexpDot, ## the dot to separate car/cdr
  23. sexpListStart, ## start of a list: the ``(`` token
  24. sexpListEnd, ## end of a list: the ``)`` token
  25. TTokKind = enum # must be synchronized with SexpEventKind!
  26. tkError,
  27. tkEof,
  28. tkString,
  29. tkSymbol,
  30. tkInt,
  31. tkFloat,
  32. tkNil,
  33. tkDot,
  34. tkParensLe,
  35. tkParensRi
  36. tkSpace
  37. SexpError* = enum ## enumeration that lists all errors that can occur
  38. errNone, ## no error
  39. errInvalidToken, ## invalid token
  40. errParensRiExpected, ## ``)`` expected
  41. errQuoteExpected, ## ``"`` expected
  42. errEofExpected, ## EOF expected
  43. SexpParser* = object of BaseLexer ## the parser object.
  44. a: string
  45. tok: TTokKind
  46. kind: SexpEventKind
  47. err: SexpError
  48. const
  49. errorMessages: array[SexpError, string] = [
  50. "no error",
  51. "invalid token",
  52. "')' expected",
  53. "'\"' or \"'\" expected",
  54. "EOF expected",
  55. ]
  56. tokToStr: array[TTokKind, string] = [
  57. "invalid token",
  58. "EOF",
  59. "string literal",
  60. "symbol",
  61. "int literal",
  62. "float literal",
  63. "nil",
  64. ".",
  65. "(", ")", "space"
  66. ]
  67. proc close*(my: var SexpParser) {.inline.} =
  68. ## closes the parser `my` and its associated input stream.
  69. lexbase.close(my)
  70. proc str*(my: SexpParser): string {.inline.} =
  71. ## returns the character data for the events: ``sexpInt``, ``sexpFloat``,
  72. ## ``sexpString``
  73. assert(my.kind in {sexpInt, sexpFloat, sexpString})
  74. result = my.a
  75. proc getInt*(my: SexpParser): BiggestInt {.inline.} =
  76. ## returns the number for the event: ``sexpInt``
  77. assert(my.kind == sexpInt)
  78. result = parseBiggestInt(my.a)
  79. proc getFloat*(my: SexpParser): float {.inline.} =
  80. ## returns the number for the event: ``sexpFloat``
  81. assert(my.kind == sexpFloat)
  82. result = parseFloat(my.a)
  83. proc kind*(my: SexpParser): SexpEventKind {.inline.} =
  84. ## returns the current event type for the SEXP parser
  85. result = my.kind
  86. proc getColumn*(my: SexpParser): int {.inline.} =
  87. ## get the current column the parser has arrived at.
  88. result = getColNumber(my, my.bufpos)
  89. proc getLine*(my: SexpParser): int {.inline.} =
  90. ## get the current line the parser has arrived at.
  91. result = my.lineNumber
  92. proc errorMsg*(my: SexpParser): string =
  93. ## returns a helpful error message for the event ``sexpError``
  94. assert(my.kind == sexpError)
  95. result = "($1, $2) Error: $3" % [$getLine(my), $getColumn(my), errorMessages[my.err]]
  96. proc errorMsgExpected*(my: SexpParser, e: string): string =
  97. ## returns an error message "`e` expected" in the same format as the
  98. ## other error messages
  99. result = "($1, $2) Error: $3" % [$getLine(my), $getColumn(my), e & " expected"]
  100. proc parseString(my: var SexpParser): TTokKind =
  101. result = tkString
  102. var pos = my.bufpos + 1
  103. while true:
  104. case my.buf[pos]
  105. of '\0':
  106. my.err = errQuoteExpected
  107. result = tkError
  108. break
  109. of '"':
  110. inc(pos)
  111. break
  112. of '\\':
  113. case my.buf[pos+1]
  114. of '\\', '"', '\'', '/':
  115. add(my.a, my.buf[pos+1])
  116. inc(pos, 2)
  117. of 'b':
  118. add(my.a, '\b')
  119. inc(pos, 2)
  120. of 'f':
  121. add(my.a, '\f')
  122. inc(pos, 2)
  123. of 'n':
  124. add(my.a, '\L')
  125. inc(pos, 2)
  126. of 'r':
  127. add(my.a, '\C')
  128. inc(pos, 2)
  129. of 't':
  130. add(my.a, '\t')
  131. inc(pos, 2)
  132. of 'u':
  133. inc(pos, 2)
  134. var r: int
  135. if handleHexChar(my.buf[pos], r): inc(pos)
  136. if handleHexChar(my.buf[pos], r): inc(pos)
  137. if handleHexChar(my.buf[pos], r): inc(pos)
  138. if handleHexChar(my.buf[pos], r): inc(pos)
  139. add(my.a, toUTF8(Rune(r)))
  140. else:
  141. # don't bother with the error
  142. add(my.a, my.buf[pos])
  143. inc(pos)
  144. of '\c':
  145. pos = lexbase.handleCR(my, pos)
  146. add(my.a, '\c')
  147. of '\L':
  148. pos = lexbase.handleLF(my, pos)
  149. add(my.a, '\L')
  150. else:
  151. add(my.a, my.buf[pos])
  152. inc(pos)
  153. my.bufpos = pos # store back
  154. proc parseNumber(my: var SexpParser) =
  155. var pos = my.bufpos
  156. if my.buf[pos] == '-':
  157. add(my.a, '-')
  158. inc(pos)
  159. if my.buf[pos] == '.':
  160. add(my.a, "0.")
  161. inc(pos)
  162. else:
  163. while my.buf[pos] in Digits:
  164. add(my.a, my.buf[pos])
  165. inc(pos)
  166. if my.buf[pos] == '.':
  167. add(my.a, '.')
  168. inc(pos)
  169. # digits after the dot:
  170. while my.buf[pos] in Digits:
  171. add(my.a, my.buf[pos])
  172. inc(pos)
  173. if my.buf[pos] in {'E', 'e'}:
  174. add(my.a, my.buf[pos])
  175. inc(pos)
  176. if my.buf[pos] in {'+', '-'}:
  177. add(my.a, my.buf[pos])
  178. inc(pos)
  179. while my.buf[pos] in Digits:
  180. add(my.a, my.buf[pos])
  181. inc(pos)
  182. my.bufpos = pos
  183. proc parseSymbol(my: var SexpParser) =
  184. var pos = my.bufpos
  185. if my.buf[pos] in IdentStartChars:
  186. while my.buf[pos] in IdentChars:
  187. add(my.a, my.buf[pos])
  188. inc(pos)
  189. my.bufpos = pos
  190. proc getTok(my: var SexpParser): TTokKind =
  191. setLen(my.a, 0)
  192. case my.buf[my.bufpos]
  193. of '-', '0'..'9': # numbers that start with a . are not parsed
  194. # correctly.
  195. parseNumber(my)
  196. if {'.', 'e', 'E'} in my.a:
  197. result = tkFloat
  198. else:
  199. result = tkInt
  200. of '"': #" # gotta fix nim-mode
  201. result = parseString(my)
  202. of '(':
  203. inc(my.bufpos)
  204. result = tkParensLe
  205. of ')':
  206. inc(my.bufpos)
  207. result = tkParensRi
  208. of '\0':
  209. result = tkEof
  210. of 'a'..'z', 'A'..'Z', '_':
  211. parseSymbol(my)
  212. if my.a == "nil":
  213. result = tkNil
  214. else:
  215. result = tkSymbol
  216. of ' ':
  217. result = tkSpace
  218. inc(my.bufpos)
  219. of '.':
  220. result = tkDot
  221. inc(my.bufpos)
  222. else:
  223. inc(my.bufpos)
  224. result = tkError
  225. my.tok = result
  226. # ------------- higher level interface ---------------------------------------
  227. type
  228. SexpNodeKind* = enum ## possible SEXP node types
  229. SNil,
  230. SInt,
  231. SFloat,
  232. SString,
  233. SSymbol,
  234. SList,
  235. SCons
  236. SexpNode* = ref SexpNodeObj ## SEXP node
  237. SexpNodeObj* {.acyclic.} = object
  238. case kind*: SexpNodeKind
  239. of SString:
  240. str*: string
  241. of SSymbol:
  242. symbol*: string
  243. of SInt:
  244. num*: BiggestInt
  245. of SFloat:
  246. fnum*: float
  247. of SList:
  248. elems*: seq[SexpNode]
  249. of SCons:
  250. car: SexpNode
  251. cdr: SexpNode
  252. of SNil:
  253. discard
  254. Cons = tuple[car: SexpNode, cdr: SexpNode]
  255. SexpParsingError* = object of ValueError ## is raised for a SEXP error
  256. proc raiseParseErr*(p: SexpParser, msg: string) {.noinline, noreturn.} =
  257. ## raises an `ESexpParsingError` exception.
  258. raise newException(SexpParsingError, errorMsgExpected(p, msg))
  259. proc newSString*(s: string): SexpNode =
  260. ## Creates a new `SString SexpNode`.
  261. result = SexpNode(kind: SString, str: s)
  262. proc newSInt*(n: BiggestInt): SexpNode =
  263. ## Creates a new `SInt SexpNode`.
  264. result = SexpNode(kind: SInt, num: n)
  265. proc newSFloat*(n: float): SexpNode =
  266. ## Creates a new `SFloat SexpNode`.
  267. result = SexpNode(kind: SFloat, fnum: n)
  268. proc newSNil*(): SexpNode =
  269. ## Creates a new `SNil SexpNode`.
  270. result = SexpNode(kind: SNil)
  271. proc newSCons*(car, cdr: SexpNode): SexpNode =
  272. ## Creates a new `SCons SexpNode`
  273. result = SexpNode(kind: SCons, car: car, cdr: cdr)
  274. proc newSList*(): SexpNode =
  275. ## Creates a new `SList SexpNode`
  276. result = SexpNode(kind: SList, elems: @[])
  277. proc newSSymbol*(s: string): SexpNode =
  278. result = SexpNode(kind: SSymbol, symbol: s)
  279. proc getStr*(n: SexpNode, default: string = ""): string =
  280. ## Retrieves the string value of a `SString SexpNode`.
  281. ##
  282. ## Returns ``default`` if ``n`` is not a ``SString``.
  283. if n.kind != SString: return default
  284. else: return n.str
  285. proc getNum*(n: SexpNode, default: BiggestInt = 0): BiggestInt =
  286. ## Retrieves the int value of a `SInt SexpNode`.
  287. ##
  288. ## Returns ``default`` if ``n`` is not a ``SInt``.
  289. if n.kind != SInt: return default
  290. else: return n.num
  291. proc getFNum*(n: SexpNode, default: float = 0.0): float =
  292. ## Retrieves the float value of a `SFloat SexpNode`.
  293. ##
  294. ## Returns ``default`` if ``n`` is not a ``SFloat``.
  295. if n.kind != SFloat: return default
  296. else: return n.fnum
  297. proc getSymbol*(n: SexpNode, default: string = ""): string =
  298. ## Retrieves the int value of a `SList SexpNode`.
  299. ##
  300. ## Returns ``default`` if ``n`` is not a ``SList``.
  301. if n.kind != SSymbol: return default
  302. else: return n.symbol
  303. proc getElems*(n: SexpNode, default: seq[SexpNode] = @[]): seq[SexpNode] =
  304. ## Retrieves the int value of a `SList SexpNode`.
  305. ##
  306. ## Returns ``default`` if ``n`` is not a ``SList``.
  307. if n.kind == SNil: return @[]
  308. elif n.kind != SList: return default
  309. else: return n.elems
  310. proc getCons*(n: SexpNode, defaults: Cons = (newSNil(), newSNil())): Cons =
  311. ## Retrieves the cons value of a `SList SexpNode`.
  312. ##
  313. ## Returns ``default`` if ``n`` is not a ``SList``.
  314. if n.kind == SCons: return (n.car, n.cdr)
  315. elif n.kind == SList: return (n.elems[0], n.elems[1])
  316. else: return defaults
  317. proc sexp*(s: string): SexpNode =
  318. ## Generic constructor for SEXP data. Creates a new `SString SexpNode`.
  319. result = SexpNode(kind: SString, str: s)
  320. proc sexp*(n: BiggestInt): SexpNode =
  321. ## Generic constructor for SEXP data. Creates a new `SInt SexpNode`.
  322. result = SexpNode(kind: SInt, num: n)
  323. proc sexp*(n: float): SexpNode =
  324. ## Generic constructor for SEXP data. Creates a new `SFloat SexpNode`.
  325. result = SexpNode(kind: SFloat, fnum: n)
  326. proc sexp*(b: bool): SexpNode =
  327. ## Generic constructor for SEXP data. Creates a new `SSymbol
  328. ## SexpNode` with value t or `SNil SexpNode`.
  329. if b:
  330. result = SexpNode(kind: SSymbol, symbol: "t")
  331. else:
  332. result = SexpNode(kind: SNil)
  333. proc sexp*(elements: openArray[SexpNode]): SexpNode =
  334. ## Generic constructor for SEXP data. Creates a new `SList SexpNode`
  335. result = SexpNode(kind: SList)
  336. newSeq(result.elems, elements.len)
  337. for i, p in pairs(elements): result.elems[i] = p
  338. proc sexp*(s: SexpNode): SexpNode =
  339. result = s
  340. proc toSexp(x: NimNode): NimNode {.compileTime.} =
  341. case x.kind
  342. of nnkBracket:
  343. result = newNimNode(nnkBracket)
  344. for i in 0 ..< x.len:
  345. result.add(toSexp(x[i]))
  346. else:
  347. result = x
  348. result = prefix(result, "sexp")
  349. macro convertSexp*(x: untyped): untyped =
  350. ## Convert an expression to a SexpNode directly, without having to specify
  351. ## `%` for every element.
  352. result = toSexp(x)
  353. proc `==`* (a, b: SexpNode): bool {.noSideEffect.} =
  354. ## Check two nodes for equality
  355. if a.isNil:
  356. if b.isNil: return true
  357. return false
  358. elif b.isNil or a.kind != b.kind:
  359. return false
  360. else:
  361. return case a.kind
  362. of SString:
  363. a.str == b.str
  364. of SInt:
  365. a.num == b.num
  366. of SFloat:
  367. a.fnum == b.fnum
  368. of SNil:
  369. true
  370. of SList:
  371. a.elems == b.elems
  372. of SSymbol:
  373. a.symbol == b.symbol
  374. of SCons:
  375. a.car == b.car and a.cdr == b.cdr
  376. proc hash* (n:SexpNode): Hash =
  377. ## Compute the hash for a SEXP node
  378. case n.kind
  379. of SList:
  380. result = hash(n.elems)
  381. of SInt:
  382. result = hash(n.num)
  383. of SFloat:
  384. result = hash(n.fnum)
  385. of SString:
  386. result = hash(n.str)
  387. of SNil:
  388. result = hash(0)
  389. of SSymbol:
  390. result = hash(n.symbol)
  391. of SCons:
  392. result = hash(n.car) !& hash(n.cdr)
  393. proc len*(n: SexpNode): int =
  394. ## If `n` is a `SList`, it returns the number of elements.
  395. ## If `n` is a `JObject`, it returns the number of pairs.
  396. ## Else it returns 0.
  397. case n.kind
  398. of SList: result = n.elems.len
  399. else: discard
  400. proc `[]`*(node: SexpNode, index: int): SexpNode =
  401. ## Gets the node at `index` in a List. Result is undefined if `index`
  402. ## is out of bounds
  403. assert(not isNil(node))
  404. assert(node.kind == SList)
  405. return node.elems[index]
  406. proc add*(father, child: SexpNode) =
  407. ## Adds `child` to a SList node `father`.
  408. assert father.kind == SList
  409. father.elems.add(child)
  410. # ------------- pretty printing ----------------------------------------------
  411. proc indent(s: var string, i: int) =
  412. s.add(spaces(i))
  413. proc newIndent(curr, indent: int, ml: bool): int =
  414. if ml: return curr + indent
  415. else: return indent
  416. proc nl(s: var string, ml: bool) =
  417. if ml: s.add("\n")
  418. proc escapeJson*(s: string): string =
  419. ## Converts a string `s` to its JSON representation.
  420. result = newStringOfCap(s.len + s.len shr 3)
  421. result.add("\"")
  422. for x in runes(s):
  423. var r = int(x)
  424. if r >= 32 and r <= 127:
  425. var c = chr(r)
  426. case c
  427. of '"': result.add("\\\"") #" # gotta fix nim-mode
  428. of '\\': result.add("\\\\")
  429. else: result.add(c)
  430. else:
  431. result.add("\\u")
  432. result.add(toHex(r, 4))
  433. result.add("\"")
  434. proc copy*(p: SexpNode): SexpNode =
  435. ## Performs a deep copy of `a`.
  436. case p.kind
  437. of SString:
  438. result = newSString(p.str)
  439. of SInt:
  440. result = newSInt(p.num)
  441. of SFloat:
  442. result = newSFloat(p.fnum)
  443. of SNil:
  444. result = newSNil()
  445. of SSymbol:
  446. result = newSSymbol(p.symbol)
  447. of SList:
  448. result = newSList()
  449. for i in items(p.elems):
  450. result.elems.add(copy(i))
  451. of SCons:
  452. result = newSCons(copy(p.car), copy(p.cdr))
  453. proc toPretty(result: var string, node: SexpNode, indent = 2, ml = true,
  454. lstArr = false, currIndent = 0) =
  455. case node.kind
  456. of SString:
  457. if lstArr: result.indent(currIndent)
  458. result.add(escapeJson(node.str))
  459. of SInt:
  460. if lstArr: result.indent(currIndent)
  461. result.addInt(node.num)
  462. of SFloat:
  463. if lstArr: result.indent(currIndent)
  464. result.addFloat(node.fnum)
  465. of SNil:
  466. if lstArr: result.indent(currIndent)
  467. result.add("nil")
  468. of SSymbol:
  469. if lstArr: result.indent(currIndent)
  470. result.add(node.symbol)
  471. of SList:
  472. if lstArr: result.indent(currIndent)
  473. if len(node.elems) != 0:
  474. result.add("(")
  475. result.nl(ml)
  476. for i in 0..len(node.elems)-1:
  477. if i > 0:
  478. result.add(" ")
  479. result.nl(ml) # New Line
  480. toPretty(result, node.elems[i], indent, ml,
  481. true, newIndent(currIndent, indent, ml))
  482. result.nl(ml)
  483. result.indent(currIndent)
  484. result.add(")")
  485. else: result.add("nil")
  486. of SCons:
  487. if lstArr: result.indent(currIndent)
  488. result.add("(")
  489. toPretty(result, node.car, indent, ml,
  490. true, newIndent(currIndent, indent, ml))
  491. result.add(" . ")
  492. toPretty(result, node.cdr, indent, ml,
  493. true, newIndent(currIndent, indent, ml))
  494. result.add(")")
  495. proc pretty*(node: SexpNode, indent = 2): string =
  496. ## Converts `node` to its Sexp Representation, with indentation and
  497. ## on multiple lines.
  498. result = ""
  499. toPretty(result, node, indent)
  500. proc `$`*(node: SexpNode): string =
  501. ## Converts `node` to its SEXP Representation on one line.
  502. result = ""
  503. toPretty(result, node, 0, false)
  504. iterator items*(node: SexpNode): SexpNode =
  505. ## Iterator for the items of `node`. `node` has to be a SList.
  506. assert node.kind == SList
  507. for i in items(node.elems):
  508. yield i
  509. iterator mitems*(node: var SexpNode): var SexpNode =
  510. ## Iterator for the items of `node`. `node` has to be a SList. Items can be
  511. ## modified.
  512. assert node.kind == SList
  513. for i in mitems(node.elems):
  514. yield i
  515. proc eat(p: var SexpParser, tok: TTokKind) =
  516. if p.tok == tok: discard getTok(p)
  517. else: raiseParseErr(p, tokToStr[tok])
  518. proc parseSexp(p: var SexpParser): SexpNode =
  519. ## Parses SEXP from a SEXP Parser `p`.
  520. case p.tok
  521. of tkString:
  522. # we capture 'p.a' here, so we need to give it a fresh buffer afterwards:
  523. result = SexpNode(kind: SString, str: move p.a)
  524. discard getTok(p)
  525. of tkInt:
  526. result = newSInt(parseBiggestInt(p.a))
  527. discard getTok(p)
  528. of tkFloat:
  529. result = newSFloat(parseFloat(p.a))
  530. discard getTok(p)
  531. of tkNil:
  532. result = newSNil()
  533. discard getTok(p)
  534. of tkSymbol:
  535. result = SexpNode(kind: SSymbol, symbol: move p.a)
  536. discard getTok(p)
  537. of tkParensLe:
  538. result = newSList()
  539. discard getTok(p)
  540. while p.tok notin {tkParensRi, tkDot}:
  541. result.add(parseSexp(p))
  542. if p.tok != tkSpace: break
  543. discard getTok(p)
  544. if p.tok == tkDot:
  545. eat(p, tkDot)
  546. eat(p, tkSpace)
  547. result.add(parseSexp(p))
  548. result = newSCons(result[0], result[1])
  549. eat(p, tkParensRi)
  550. of tkSpace, tkDot, tkError, tkParensRi, tkEof:
  551. raiseParseErr(p, "(")
  552. proc open*(my: var SexpParser, input: Stream) =
  553. ## initializes the parser with an input stream.
  554. lexbase.open(my, input)
  555. my.kind = sexpError
  556. my.a = ""
  557. proc parseSexp*(s: Stream): SexpNode =
  558. ## Parses from a buffer `s` into a `SexpNode`.
  559. var p: SexpParser
  560. p.open(s)
  561. discard getTok(p) # read first token
  562. result = p.parseSexp()
  563. p.close()
  564. proc parseSexp*(buffer: string): SexpNode =
  565. ## Parses Sexp from `buffer`.
  566. result = parseSexp(newStringStream(buffer))
  567. when isMainModule:
  568. let testSexp = parseSexp("""(1 (98 2) nil (2) foobar "foo" 9.234)""")
  569. assert(testSexp[0].getNum == 1)
  570. assert(testSexp[1][0].getNum == 98)
  571. assert(testSexp[2].getElems == @[])
  572. assert(testSexp[4].getSymbol == "foobar")
  573. assert(testSexp[5].getStr == "foo")
  574. let alist = parseSexp("""((1 . 2) (2 . "foo"))""")
  575. assert(alist[0].getCons.car.getNum == 1)
  576. assert(alist[0].getCons.cdr.getNum == 2)
  577. assert(alist[1].getCons.cdr.getStr == "foo")
  578. # Generator:
  579. var j = convertSexp([true, false, "foobar", [1, 2, "baz"]])
  580. assert($j == """(t nil "foobar" (1 2 "baz"))""")