parser.lisp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. (import test ())
  2. (import urn/parser parser)
  3. (import urn/logger/void (void))
  4. (defun lex (str)
  5. "Lex STR, removing the EOF token"
  6. (with (res (parser/lex void str "<stdin>"))
  7. (pop-last! res)
  8. res))
  9. (defun parse (str)
  10. "Lex and parse STR"
  11. (let* [(lexed (parser/lex void str "<stdin>"))
  12. (parsed (parser/parse void lexed))]
  13. parsed))
  14. (defun teq? (x y)
  15. "Check the two values X and Y are equal, unwrapping them using [[const-val]]."
  16. (cond
  17. [(/= (type x) (type y)) false]
  18. [(list? x) (and (= (n x) (n y)) (all id (map teq? x y)))]
  19. [true (eq? x y)]))
  20. (defmethod (pretty interpolate) (x)
  21. (.. "$" (string/quoted (.> x :value))))
  22. (defmethod (eq? interpolate interpolate) (x y)
  23. (= (.> x :value) (.> y :value)))
  24. (defmethod (pretty rational) (x)
  25. (.. (pretty (.> x :num)) "/" (pretty (.> x :dom))))
  26. (defmethod (eq? rational rational) (x y)
  27. (and (eq? (.> x :num) (.> y :num)) (eq? (.> x :dom) (.> y :dom))))
  28. (defun string->key (key) { :tag "key" :value key })
  29. (defmacro try (expr)
  30. `(list (pcall (lambda () ,expr))))
  31. (defun failed? (msg res)
  32. (and (not (car res))
  33. (string/ends-with? (tostring (cadr res)) msg)))
  34. (describe "The parser"
  35. (it "lexes whitespace"
  36. (affirm (teq? '() (lex " \n \t \f \v"))))
  37. (it "lexes numbers"
  38. (affirm (teq? '( 23) (lex "23"))
  39. (teq? '(-23) (lex "-23"))
  40. (teq? '( 23) (lex "#x17"))
  41. (teq? '(-23) (lex "-#x17"))
  42. (teq? '( 23) (lex "#b10111"))
  43. (teq? '(-23) (lex "-#b10111"))
  44. (teq? '( 23) (lex "0.23e2"))
  45. (teq? '(-23) (lex "-0.23e2"))
  46. (teq? '(23) (lex "#rXXIII"))
  47. (teq? '(-23) (lex "-#rXXIII"))
  48. (teq? '(1666) (lex "#rMDCLXVI"))
  49. (teq? (list { :tag "rational" :num 1 :dom 2 }) (lex "1/2"))
  50. (teq? (list { :tag "rational" :num 1 :dom 2 }) (lex "1'/'2'"))
  51. (failed? "Expected hexadecimal (#x), binary (#b), or Roman (#r) digit specifier." (try (lex "#)")))
  52. (failed? "Expected hexadecimal (#x), binary (#b), or Roman (#r) digit specifier." (try (lex "#a")))
  53. (failed? "Expected binary digit, got \"2\"" (try (lex "#b2")))
  54. (failed? "Expected hexadecimal digit, got \"h\"" (try (lex "#xh")))
  55. (failed? "Expected digit, got \"a\"" (try (lex "2a")))
  56. (failed? "Expected digit, got eof" (try (lex ".2e")))
  57. (failed? "Expected digit, got \"-\"" (try (lex "2-")))
  58. (failed? "Invalid denominator in rational literal" (try (lex "2/''")))))
  59. (it "lexes strings"
  60. (affirm (teq? '("foo") (lex "\"foo\""))
  61. (teq? '("\"foo\"") (lex "\"\\\"foo\\\"\""))
  62. (teq? '("A") (lex "\"\\65\""))
  63. (teq? '("A") (lex "\"\\x41\""))
  64. (teq? '("foo\nbar") (lex "\"foo\n bar\""))
  65. (teq? '("foo\nbar") (lex " \"foo\n bar\""))
  66. (teq? '("foo\n bar") (lex " \"foo\n bar\""))
  67. (teq? '("foo\nbar\nbaz") (lex " \"foo\n bar\n baz\""))
  68. (teq? (list { :tag "interpolate" :value "foo"}) (lex "$\"foo\""))
  69. (failed? "Expected '\"', got eof" (try (lex "\"foo")))
  70. (failed? "Expected hexadecimal digit, got \"g\"" (try (lex "\"\\xg\"")))
  71. (failed? "Invalid escape code" (try (lex "\"\\333\"")))
  72. (failed? "Illegal escape character" (try (lex "\"\\l\"")))
  73. (failed? "Expected escape code, got eof" (try (lex "\"\\")))))
  74. (it "lexes symbols"
  75. (affirm (teq? (list (string->symbol "foo")) (lex "foo"))
  76. (teq? (list (string->symbol "foo-bar")) (lex "foo-bar"))
  77. (teq? (list (string->symbol "foo-bar!")) (lex "foo-bar!"))
  78. (teq? (list (string->symbol "foo-\"bar")) (lex "foo-\"bar"))
  79. (teq? (list (string->symbol "-")) (lex "-"))
  80. (teq? (list (string->symbol "-.e")) (lex "-.e"))
  81. (teq? (list (string->symbol "//\\//")) (lex "//\\//"))))
  82. (it "lexes keys"
  83. (affirm (teq? (list (string->key "foo")) (lex ":foo"))
  84. (teq? (list (string->key "foo-bar")) (lex ":foo-bar"))
  85. (teq? (list (string->key "foo-bar!")) (lex ":foo-bar!"))
  86. (teq? (list (string->key "foo-\"bar")) (lex ":foo-\"bar"))))
  87. (it "lexes tokens"
  88. (affirm (eq? '("unquote") (map type (lex ",")))
  89. (eq? '("unquote-splice") (map type (lex ",@")))
  90. (eq? '("quote") (map type (lex "'")))
  91. (eq? '("syntax-quote") (map type (lex "`")))
  92. (eq? '("quasiquote") (map type (lex "~")))
  93. (eq? '("quote" "symbol") (map type (lex "'@")))
  94. (eq? '("splice" "open") (map type (lex "@(")))
  95. (eq? '("symbol" "close") (map type (lex "@)")))))
  96. (it "lexes lists"
  97. (affirm (eq? '("open" "open" "open-struct") (map type (lex "( [ {")))
  98. (eq? '("close" "close" "close") (map type (lex ") ] }")))))
  99. (it "stops symbols"
  100. (affirm (teq? 'foo-bar (car (lex "foo-bar)")))
  101. (teq? 'foo-bar (car (lex "foo-bar]")))
  102. (teq? 'foo-bar (car (lex "foo-bar}")))
  103. (teq? 'foo-bar (car (lex "foo-bar ")))
  104. (teq? 'foo-bar (car (lex "foo-bar\n")))
  105. (teq? 'foo-bar (car (lex "foo-bar\t")))
  106. (teq? 'foo-bar (car (lex "foo-bar\f")))
  107. (teq? 'foo-bar (car (lex "foo-bar\v")))
  108. (teq? 'foo-bar (car (lex "foo-bar;")))))
  109. (it "lexes handles comments"
  110. (affirm (teq? '() (lex "; foo bar"))
  111. (teq? '(foo) (lex "; foo bar\nfoo"))))
  112. (it "parses constants"
  113. (affirm (teq? '(23) (parse "23"))
  114. (teq? '("foo") (parse "\"foo\""))
  115. (teq? '(23 foo "foo" 23) (parse "23 foo \"foo\" 23"))
  116. (teq? '((23)) (parse "(23)"))))
  117. (it "parses rationals"
  118. (affirm (teq? '((rational 1 2)) (parse "1/2"))))
  119. (it "parses string interpolation"
  120. (affirm (teq? '(($ "foo")) (parse "$\"foo\""))))
  121. (it "parses lists"
  122. (affirm (teq? '((((23)))) (parse "(((23)))"))
  123. (teq? '((foo bar) foo (((foo)))) (parse "(foo bar) foo (((foo)))"))
  124. (teq? '((foo (bar)) (((foo)))) (parse "[foo (bar)] [[(foo)]]"))))
  125. (it "parses struct"
  126. (affirm (teq? '((struct-literal)) (parse "{}"))
  127. (teq? '((struct-literal :x 2)) (parse "{ :x 2 }"))
  128. (teq? '(((struct-literal :x 2))) (parse "[{ :x 2 }]"))
  129. (teq? '((struct-literal :x 2 (y) (z))) (parse "{ :x 2 (y) [z] }"))))
  130. (it "parses quotes"
  131. (affirm (teq? '((quote foo)) (parse "'foo"))
  132. (teq? '((quote (foo))) (parse "'(foo)"))
  133. (teq? '((syntax-quote foo)) (parse "`foo"))
  134. (teq? '((syntax-quote (foo))) (parse "`(foo)"))
  135. (teq? '((quasiquote foo)) (parse "~foo"))
  136. (teq? '((quasiquote (foo))) (parse "~(foo)"))
  137. (teq? '((splice foo)) (parse "@foo"))
  138. (teq? '((splice (foo))) (parse "@(foo)"))
  139. (failed? "Expected expression quote, got eof" (try (parse "'")))
  140. (failed? "')' without matching '(' inside quote" (try (parse "')")))))
  141. (it "parses unquotes"
  142. (affirm (teq? '((unquote foo)) (parse ",foo"))
  143. (teq? '((unquote (foo))) (parse ",(foo)"))
  144. (teq? '((unquote-splice foo)) (parse ",@foo"))
  145. (teq? '((unquote-splice (foo))) (parse ",@(foo)"))))
  146. (it "fails on mismatched parens"
  147. (affirm (failed? "')' without matching '('" (try (parse ")")))
  148. (failed? "Expected ')', got ']'" (try (parse "(]")))
  149. (failed? "Expected ')', got eof" (try (parse "(")))))
  150. )