Skip to content

Instantly share code, notes, and snippets.

@souenzzo
Created September 14, 2018 23:48
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 souenzzo/ac72ca3585206bf436a0d7e8f8636176 to your computer and use it in GitHub Desktop.
Save souenzzo/ac72ca3585206bf436a0d7e8f8636176 to your computer and use it in GitHub Desktop.
minimal parser
(defmulti ast->clj first)
(defmethod ast->clj :S
[[_ _ body]]
(into [] cat (ast->clj body)))
(defmethod ast->clj :AE
[[_ b]]
(ast->clj b))
(defmethod ast->clj :E
[_]
[])
(defmethod ast->clj :A
[[_ & as]]
(prn {:as as})
(let []
[(Integer/parseInt (second (first as)))]))
(defmethod ast->clj :L
[[_ & AES]]
(for [AE AES]
(ast->clj AE)))
(deftest parser-test
(let [parser (insta/parser
"S = '['L']'
L = AE*
AE = A | E
E = ' ' | ','
A = N | N'.'N
N = #\"[0-9]\"")
ast [:S "["
[:L
[:AE [:A [:N "1"] "." [:N "0"]]]
[:AE [:E ","]]
[:AE [:E " "]]
[:AE [:A [:N "2"]]]]
"]"]]
(fact
(parser "[1.0, 2]")
=> ast)
(fact
(ast->clj ast)
=> [1 2])
(fact
(ast->clj (parser "[3 2 3]"))
=> {})
(fact
(ast->clj (parser "[32.1 2 3]"))
=> {})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment