tl-rpc.lisp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. (defpackage tl-rpc
  2. (:use :cl :parser-combinators :alexandria))
  3. (in-package :tl-rpc)
  4. (defun not-equal (a b)
  5. (not (equal a b)))
  6. (defun <ml-not-end> ()
  7. (named-seq? (<- char
  8. (choice (sat (curry #'not-equal #\*))
  9. (except? #\* "*/")))
  10. (string char)))
  11. (defun <ml-content> (ml-comment)
  12. (named-seq? (<- content
  13. (many? (choice (named-seq? (<- inner ml-comment)
  14. (concatenate 'string "/*" inner "*/"))
  15. (<ml-not-end>))))
  16. (apply #'concatenate 'string content)))
  17. (defun <ml-comment> ()
  18. (named? ml-comment
  19. (bracket? "/*" (<ml-content> ml-comment) "*/")))
  20. (defun <sl-comment> ()
  21. (bracket? "//"
  22. (named-seq? (<- comment (many? (sat (curry #'not-equal #\Newline))))
  23. (coerce comment 'string))
  24. #\Newline))
  25. (defun <punctuation> ()
  26. (sat (rcurry #'member '(#\; #\( #\) #\{ #\} #\. #\# #\: #\[ #\] #\= #\? #\% #\+ #\< #\> #\, #\* #\!))))
  27. (defun <token> ()
  28. (choice
  29. (between? (sat #'(lambda (i) (or (alphanumericp i)
  30. (member i '(#\_))))) 1 nil 'string)
  31. (quoted? :quote-char #\`)))
  32. (defun tokenize (input)
  33. (remove :whitespace
  34. (parse-string*
  35. (many? (choices (<token>)
  36. (<punctuation>)
  37. (chook? :whitespace
  38. (choices (whitespace?) "---" (<sl-comment>) (<ml-comment>)))))
  39. input)))
  40. ;; Actual parsers
  41. (defun <hex-digit> ()
  42. (choice (digit?) (sat (rcurry #'member '(#\a #\b #\c #\d #\e #\f)))))
  43. (defun <letter> ()
  44. (choice (lower?) (upper?)))
  45. (defun <ident-char> ()
  46. (choices (<letter>) (digit?) #\_))
  47. (defmacro def-string-parser (name arguments &body body)
  48. (with-unique-names (inp)
  49. (multiple-value-bind (forms declarations docstring) (parse-body body :documentation t)
  50. `(defun ,name ,arguments
  51. ,docstring
  52. ,@declarations
  53. (sat (lambda (,inp)
  54. (and (stringp ,inp)
  55. (parse-string* ,@forms ,inp :complete t))))))))
  56. (defun concat (&rest args)
  57. (apply #'concatenate 'string args))
  58. (defun string-concat (args)
  59. (apply #'concatenate 'string (mapcar 'string args)))
  60. (def-string-parser <lc-ident> ()
  61. (named-seq? (<- hd (lower?))
  62. (<- tl (many? (<ident-char>)))
  63. (coerce (cons hd tl) 'string)))
  64. (def-string-parser <uc-ident> ()
  65. (named-seq? (<- hd (upper?))
  66. (<- tl (many? (<ident-char>)))
  67. (coerce (cons hd tl) 'string)))
  68. (defun <namespace-ident> () (<lc-ident>))
  69. (defun <lc-ident-ns> ()
  70. (named-seq? (<- ns (opt? (seq-list? (<namespace-ident>) #\.)))
  71. (<- id (<lc-ident>))
  72. (concat (when ns (string-concat ns)) id)))
  73. (defun <uc-ident-ns> ()
  74. (named-seq? (<- ns (opt? (seq-list? (<namespace-ident>) #\.)))
  75. (<- id (<uc-ident>))
  76. (concat (when ns (string-concat ns)) id)))
  77. (defun <lc-ident-full> ()
  78. (named-seq? (<- id (<lc-ident-ns>))
  79. (<- name (opt? (seq-list? #\# (times? (<hex-digit>) 8))))
  80. (concat id (when name (coerce (apply #'cons name) 'string)))))
  81. (defun <nat-const> ()
  82. (many1? (digit?)))
  83. (defun <boxed-type-ident> () (<uc-ident-ns>))
  84. (defun sep? (&optional accept-empty) (whitespace? :accept-empty accept-empty))
  85. (defun <full-combinator-id> ()
  86. (choice (<lc-ident-full>) "_"))
  87. (defun <combinator-id> ()
  88. (choice (<lc-ident-ns>) "_"))
  89. (defun <type-ident> ()
  90. (choices
  91. (<boxed-type-ident>)
  92. (<lc-ident-ns>)
  93. "#"))
  94. (defun <var-ident> ()
  95. (choice (<lc-ident>) (<uc-ident>)))
  96. (defun <term> (expr)
  97. (named? term
  98. (choices
  99. (seq-list? "(" expr ")")
  100. (<type-ident>)
  101. (<var-ident>)
  102. (<nat-const>)
  103. (seq-list? "%" term)
  104. (seq-list? (<type-ident>) "<" (sepby1? expr ",") ">")
  105. )))
  106. (defun <subexpr> (expr)
  107. (named? subexpr
  108. (choices
  109. (<term> expr)
  110. (seq-list? (<nat-const>) "+" subexpr)
  111. (seq-list? subexpr "+" (<nat-const>)))))
  112. (defun <expr> ()
  113. (named? expr
  114. (many? (<subexpr> expr))))
  115. (defun <type-expr> () (<expr>))
  116. (defun <nat-expr> () (<expr>))
  117. (defun <type-term> () (<term> (<expr>)))
  118. (defun <nat-term> () (<term> (<expr>)))
  119. (defun <opt-args> ()
  120. (named-seq?
  121. "{" (sep? t)
  122. (<- vars (sepby1? (<var-ident>) (sep?))) (sep? t)
  123. ":"
  124. (opt? "!")
  125. (<- type-expr (<type-expr>)) (sep? t)
  126. "}" (sep? t)
  127. (list :opt-args vars type-expr)))
  128. (defun <multiplicity> () (<nat-term>))
  129. (defun <var-ident-opt> () (choices (<var-ident>) "_"))
  130. (defun <conditional-def> ()
  131. (seq-list? (<var-ident>) (opt? (seq-list? "." (<nat-const>))) "?"))
  132. (defun <args> ()
  133. (named? args
  134. (choices
  135. (seq-list? (<var-ident-opt>) ":" (opt? (<conditional-def>)) (opt? "!") (<type-term>))
  136. (seq-list? (opt? (seq-list? (<var-ident-opt>) ":"))
  137. (opt? (seq-list? (<multiplicity>) "*"))
  138. "[" (many? args) "]")
  139. (seq-list? "(" (many1? (<var-ident-opt>)) ":" (opt? "!") (<type-term>) ")")
  140. (seq-list? (opt? "!") (<type-term>)))))
  141. (defun <result-type> ()
  142. (choices
  143. (seq-list? (<boxed-type-ident>) (many? (<subexpr> (<expr>))))
  144. (seq-list? (<boxed-type-ident>) "<" (sepby1? (<subexpr> (<expr>)) ",") ">")))
  145. (defun <combinator-decl> ()
  146. (named-seq?
  147. (<- comb (<full-combinator-id>)) (sep?)
  148. (<- opt-args (many? (<opt-args>))) (sep? t)
  149. (<- args (many? (<args>))) (sep? t)
  150. "=" (sep?)
  151. (<- result-type (<result-type>)) (sep? t)
  152. ";"
  153. (list :comb-decl comb opt-args args result-type)))
  154. (defun <partial-type-app-decl> ()
  155. (choices
  156. (named-seq?
  157. (<- type (<boxed-type-ident>)) (sep?)
  158. (<- ex (many1? (<subexpr> (<expr>)))) (sep?)
  159. ";"
  160. (list :part-type type ex))
  161. (named-seq?
  162. (<- type (<boxed-type-ident>)) (sep?)
  163. "<" (<- ex (sepby1? (<expr>) ",")) ">"
  164. (list :part-type type ex))))
  165. (defun <partial-comb-app-decl> ()
  166. (seq-list? (<combinator-id>) (many1? (<subexpr> (<expr>)))))
  167. (defun <partial-app-decl> ()
  168. (choices
  169. (<partial-type-app-decl>)
  170. (<partial-comb-app-decl>)))
  171. (defun <builtin-combinator-decl> ()
  172. (named-seq?
  173. (<- comb (<full-combinator-id>)) (sep?)
  174. "?" (sep?)
  175. "=" (sep?)
  176. (<- type (<boxed-type-ident>)) (sep? t)
  177. ";"
  178. (list :builtin-decl comb type)))
  179. (defun <final-decl> ()
  180. (named-seq?
  181. (<- type (choices "New" "Final" "Empty"))
  182. (whitespace?)
  183. (<- ind (<boxed-type-ident>)) #\;
  184. (list :final-decl type ind)))
  185. (defun <declaration> ()
  186. (choices (<builtin-combinator-decl>)
  187. (<combinator-decl>)
  188. (<partial-app-decl>)
  189. (<final-decl>)))
  190. (defun <constr-declarations> ()
  191. (many? (named-seq? (<- dec (<declaration>)) #\; dec)))
  192. (defun <fun-declarations> ()
  193. (many? (named-seq? (<- dec (<declaration>)) #\; dec)))
  194. (defun <tl-program> ()
  195. (named-seq? (<- hd (<constr-declarations>))
  196. (whitespace? :accept-empty t)
  197. (<- tl (many?
  198. (choice
  199. (mdo "---" "functions" "---"
  200. (<fun-declarations>))
  201. (mdo "---" "types" "---"
  202. (<constr-declarations>)))))
  203. (cons hd tl)))