Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Cursive parsing lib example
; 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:
'(defprotocol [[x :variance :covariant]]
"Protocol doc"
([y] a [this a :- x, b :- y] :- y "Method doc"))
=> {: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