Skip to content

Instantly share code, notes, and snippets.

@eigenhombre
Last active February 27, 2019 18:58
Show Gist options
  • Select an option

  • Save eigenhombre/0c7c5896cf07ba3cd4ca006474c69065 to your computer and use it in GitHub Desktop.

Select an option

Save eigenhombre/0c7c5896cf07ba3cd4ca006474c69065 to your computer and use it in GitHub Desktop.
(ns instaparsexample.core
(:require [instaparse.core :as i]))
(let [p (i/parser (slurp "example.bnf"))]
(for [ex ["1234"
"a"
"aab"
"nil"
"()"
"(())"
"[]"
"{}"
"{1 2}"
"{(+ 1 1) (2 a)}"
"{2 3, 4 5}"
"+"
"(+)"
"(+ 1)"
"(+ 1 1)"
"(+ 1 1 ab)"
"(-)"
"(+ 1 (+ 1 2 3))"
"(+ 1 [1 3 4 (4 5 6 (1))])"
"(for [i (range)] (inc i))"]]
(p ex)))
;;;;;;;
<EXPR> = <SPACE*> (list | num | atom | vec | map) <SPACE*>
list = <LPAREN> EXPR* <RPAREN>
vec = <LBRACKET> EXPR* <RBRACKET>
map = <LBRACE> kvpair* <RBRACE>
key = EXPR
val = EXPR
kvpair = key val
atom = #'[a-z\+\-\*\-]+'
num = #'[0-9]+'
SPACE = ' ' | ','
LPAREN = <'('>
RPAREN = <')'>
LBRACKET = <'['>
RBRACKET = <']'>
LBRACE = <'{'>
RBRACE = <'}'>
;;;;;
(([:num "1234"])
([:atom "a"])
([:atom "aab"])
([:atom "nil"])
([:list])
([:list [:list]])
([:vec])
([:map])
([:map [:kvpair [:key [:num "1"]] [:val [:num "2"]]]])
([:map
[:kvpair
[:key [:list [:atom "+"] [:num "1"] [:num "1"]]]
[:val [:list [:num "2"] [:atom "a"]]]]])
([:map
[:kvpair [:key [:num "2"]] [:val [:num "3"]]]
[:kvpair [:key [:num "4"]] [:val [:num "5"]]]])
([:atom "+"])
([:list [:atom "+"]])
([:list [:atom "+"] [:num "1"]])
([:list [:atom "+"] [:num "1"] [:num "1"]])
([:list [:atom "+"] [:num "1"] [:num "1"] [:atom "ab"]])
([:list [:atom "-"]])
([:list
[:atom "+"]
[:num "1"]
[:list [:atom "+"] [:num "1"] [:num "2"] [:num "3"]]])
([:list
[:atom "+"]
[:num "1"]
[:vec
[:num "1"]
[:num "3"]
[:num "4"]
[:list [:num "4"] [:num "5"] [:num "6"] [:list [:num "1"]]]]])
([:list
[:atom "for"]
[:vec [:atom "i"] [:list [:atom "range"]]]
[:list [:atom "inc"] [:atom "i"]]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment