tl-rpc.lisp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. (substitute #\_ "_"
  35. (parse-string*
  36. (many? (choices (<token>)
  37. (<punctuation>)
  38. (chook? :whitespace
  39. (choices (whitespace?) "---"
  40. (<sl-comment>) (<ml-comment>)))))
  41. input)
  42. :test #'equal)))
  43. ;; Actual parsers
  44. (defun <hex-digit> ()
  45. (choice (digit?) (sat (rcurry #'member '(#\a #\b #\c #\d #\e #\f)))))
  46. (defun <letter> ()
  47. (choice (lower?) (upper?)))
  48. (defun <ident-char> ()
  49. (choices (<letter>) (digit?) #\_))
  50. (defmacro def-string-parser (name arguments &body body)
  51. (with-unique-names (inp res peek)
  52. (multiple-value-bind (forms declarations docstring) (parse-body body :documentation t)
  53. `(defun ,name ,arguments
  54. ,docstring
  55. ,@declarations
  56. #'(lambda (,inp)
  57. (typecase ,inp
  58. (parser-combinators::end-context (constantly nil))
  59. (parser-combinators::context
  60. (let* ((,peek (parser-combinators::context-peek ,inp))
  61. (,res (and (stringp ,peek)
  62. (parse-string* ,@forms ,peek :complete t))))
  63. (if ,res
  64. (let ((closure-value
  65. (make-instance 'parser-possibility
  66. :tree ,res :suffix (parser-combinators::context-next ,inp))))
  67. #'(lambda ()
  68. (when closure-value
  69. (prog1
  70. closure-value
  71. (setf closure-value nil)))))
  72. (constantly nil))))))))))
  73. (defstruct ident ns id name)
  74. (def-string-parser <lc-ident> ()
  75. (named-seq? (<- hd (lower?))
  76. (<- tl (many? (<ident-char>)))
  77. (make-ident :id (coerce (cons hd tl) 'string))))
  78. (def-string-parser <uc-ident> ()
  79. (named-seq? (<- hd (upper?))
  80. (<- tl (many? (<ident-char>)))
  81. (make-ident :id (coerce (cons hd tl) 'string))))
  82. (def-string-parser <hex-string> (&optional len)
  83. (between? (<hex-digit>) len len))
  84. (defun <namespace-ident> ()
  85. (named-seq? (<- ns (<lc-ident>))
  86. (make-ident :ns (ident-id ns))))
  87. (defun <lc-ident-ns> ()
  88. (named-seq? (<- ns (opt? (seq-list? (<namespace-ident>) #\.)))
  89. (<- id (<lc-ident>))
  90. (let ((ident (if ns (car ns) id)))
  91. (when ns (setf (ident-id ident) (ident-id id)))
  92. ident)))
  93. (defun <uc-ident-ns> ()
  94. (named-seq? (<- ns (opt? (seq-list? (<namespace-ident>) #\.)))
  95. (<- id (<uc-ident>))
  96. (let ((ident (if ns (car ns) id)))
  97. (when ns (setf (ident-id ident) (ident-id id)))
  98. ident)))
  99. (defun <lc-ident-full> ()
  100. (named-seq? (<- id (<lc-ident-ns>))
  101. (<- name (opt? (seq-list? #\# (<hex-string> 8))))
  102. (progn (when name (setf (ident-name id) (second name)))
  103. id)))
  104. (defun <boxed-type-ident> () (<uc-ident-ns>))
  105. (defun <type-ident> ()
  106. (choices
  107. (<boxed-type-ident>)
  108. (<lc-ident-ns>)
  109. (named-seq? #\# (make-ident :id "nat"))))
  110. (defun <var-ident> ()
  111. (choice (<lc-ident>) (<uc-ident>)))
  112. (def-string-parser <nat-const> ()
  113. (named-seq? (<- digits (many1? (digit?)))
  114. (parse-integer (coerce digits 'string))))
  115. (defun <term> (expr)
  116. (named? term
  117. (choices
  118. (seq-list? #\( expr #\))
  119. (seq-list? (<type-ident>) #\< (sepby1? expr #\,) #\>)
  120. (<type-ident>)
  121. (<var-ident>)
  122. (<nat-const>)
  123. (seq-list? #\% term))))
  124. (defun <subexpr1> ()
  125. (named? subexpr1
  126. (opt? (named-seq? #\+
  127. (<- hd (<nat-const>))
  128. (<- tl subexpr1)
  129. (append (list #\+ hd) (cdr tl))))))
  130. (defun <subexpr> (expr)
  131. (named? subexpr
  132. (choices
  133. (named-seq? (<- hd (<nat-const>))
  134. #\+
  135. (<- tl subexpr)
  136. (<- sub (<subexpr1>))
  137. (append (list #\+ hd)
  138. (list tl)
  139. (cdr sub)))
  140. (named-seq? (<- term (<term> expr))
  141. (<- sub (<subexpr1>))
  142. (if sub (append (list #\+ term) (cdr sub))
  143. term)))))
  144. (defun <expr> ()
  145. (named? expr
  146. (many? (<subexpr> expr))))
  147. (defun <type-expr> () (<expr>))
  148. (defun <nat-expr> () (<expr>))
  149. (defun <type-term> () (<term> (<expr>)))
  150. (defun <nat-term> () (<term> (<expr>)))
  151. (defun <full-combinator-id> ()
  152. (choice (<lc-ident-full>) #\_))
  153. (defun <combinator-id> ()
  154. (choice (<lc-ident-ns>) #\_))
  155. (defun <opt-args> ()
  156. (named-seq?
  157. #\{
  158. (<- vars (many1? (<var-ident>)))
  159. #\:
  160. (<- excl (opt? #\!))
  161. (<- type-expr (<type-expr>))
  162. #\}
  163. (list :opt-args vars excl type-expr)))
  164. (defun <multiplicity> () (<nat-term>))
  165. (defun <var-ident-opt> () (choices (<var-ident>) #\_))
  166. (defun <conditional-def> ()
  167. (seq-list? (<var-ident>) (opt? (seq-list? #\. (<nat-const>))) #\?))
  168. (defun <args> ()
  169. (named? args
  170. (choices
  171. (seq-list? (<var-ident-opt>) #\: (opt? (<conditional-def>)) (opt? #\!) (<type-term>))
  172. (seq-list? (opt? (seq-list? (<var-ident-opt>) #\:))
  173. (opt? (seq-list? (<multiplicity>) #\*))
  174. #\[ (many? args) #\])
  175. (seq-list? #\( (many1? (<var-ident-opt>)) #\: (opt? #\!) (<type-term>) #\))
  176. (seq-list? (opt? #\!) (<type-term>)))))
  177. (defun <result-type> ()
  178. (choices
  179. (seq-list? (<boxed-type-ident>) (many? (<subexpr> (<expr>))))
  180. (seq-list? (<boxed-type-ident>) #\< (sepby1? (<subexpr> (<expr>)) #\,) #\>)))
  181. (defun <combinator-decl> ()
  182. (named-seq?
  183. (<- comb (<full-combinator-id>))
  184. (<- opt-args (many? (<opt-args>)))
  185. (<- args (many? (<args>)))
  186. #\=
  187. (<- result-type (<result-type>))
  188. #\;
  189. (list :comb-decl comb opt-args args result-type)))
  190. (defun <partial-type-app-decl> ()
  191. (choices
  192. (named-seq?
  193. (<- type (<boxed-type-ident>))
  194. (<- ex (many1? (<subexpr> (<expr>))))
  195. #\;
  196. (list :part-type type ex))
  197. (named-seq?
  198. (<- type (<boxed-type-ident>))
  199. #\< (<- ex (sepby1? (<expr>) #\,)) #\>
  200. (list :part-type type ex))))
  201. (defun <partial-comb-app-decl> ()
  202. (seq-list? (<combinator-id>) (many1? (<subexpr> (<expr>)))))
  203. (defun <partial-app-decl> ()
  204. (choices
  205. (<partial-type-app-decl>)
  206. (<partial-comb-app-decl>)))
  207. (defun <builtin-combinator-decl> ()
  208. (named-seq?
  209. (<- comb (<full-combinator-id>))
  210. #\? #\=
  211. (<- type (<boxed-type-ident>))
  212. #\;
  213. (list :builtin-decl comb type)))
  214. (defun <final-decl> ()
  215. (named-seq?
  216. (<- type (choices "New" "Final" "Empty"))
  217. (<- ind (<boxed-type-ident>))
  218. #\;
  219. (list :final-decl type ind)))
  220. (defun <declaration> ()
  221. (choices (<builtin-combinator-decl>)
  222. (<combinator-decl>)
  223. (<partial-app-decl>)
  224. (<final-decl>)))
  225. (defun <constr-declarations> ()
  226. (many? (<declaration>)))
  227. (defun <fun-declarations> ()
  228. (many? (<declaration>)))
  229. (defstruct tl-program constructors functions)
  230. (defun <tl-program> ()
  231. (named-seq? (<- hd (<constr-declarations>))
  232. (<- tl (many?
  233. (choice
  234. (named-seq? "---" "functions" "---"
  235. (<- decs (<fun-declarations>))
  236. (cons :funcs decs))
  237. (named-seq? "---" "types" "---"
  238. (<- decs (<constr-declarations>))
  239. (cons :constrs decs)))))
  240. (make-tl-program
  241. :constructors (append hd (mapcar #'cdr (remove :funcs tl :key #'car)))
  242. :functions (mapcar #'cdr (remove :constrs tl :key #'car)))))