Created
August 29, 2014 23:35
-
-
Save cursive-ide/f0d8c53d25363082ca06 to your computer and use it in GitHub Desktop.
Cursive parsing lib example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Parsers are defined like this - this this for the core.typed defprotocol | |
(defn binder [] | |
(vector (alt (symbol :into :bindings) | |
(vector (symbol :into :bindings))))) | |
(defn type-decl [] | |
(all (keyword :matching :-) | |
(any :into :types) | |
(opt (alt (symbol :matching '* | |
:into :no-resolve) | |
(all (symbol :matching '... | |
:into :no-resolve) | |
(symbol :into :no-resolve)))))) | |
(defn defprotocol [] | |
(all (symbol) | |
(opt (binder)) | |
(symbol :as :name-symbol) | |
(opt (string :as :docstring)) | |
(repeat (item :into :methods | |
(list :as :method | |
(opt (binder)) | |
(symbol :as :name-symbol) | |
(repeat (vector :into :arglists | |
(repeat (all (any :into :parameters) | |
(opt (type-decl))))) | |
(opt (type-decl))) | |
(opt (string :as :docstring))))))) | |
; And I can parse them like this: | |
(parse | |
'(defprotocol [[x :variance :covariant]] | |
MyProtocol | |
"Protocol doc" | |
([y] a [this a :- x, b :- y] :- y "Method doc")) | |
(patterns/defprotocol)) | |
=> {:methods [{:docstring "Method doc", | |
:types [x y y], | |
:parameters [this a b], | |
:arglists [[this a :- x b :- y]], | |
:name-symbol a, | |
:bindings [y], | |
:method ([y] a [this a :- x b :- y] :- y "Method doc"), | |
:remaining ()}], | |
:docstring "Protocol doc", | |
:name-symbol MyProtocol, | |
:bindings [x], | |
:remaining ()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment