tl-rpc.lisp 8.7 KB

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