Skip to content

Instantly share code, notes, and snippets.

@cursive-ide
Created August 29, 2014 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cursive-ide/f0d8c53d25363082ca06 to your computer and use it in GitHub Desktop.
Save cursive-ide/f0d8c53d25363082ca06 to your computer and use it in GitHub Desktop.
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:
(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