Skip to content

Commit

Permalink
Added parses & ready for 0.0.3 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
judepayne authored Apr 24, 2023
1 parent dc2deb6 commit 0599cca
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
24 changes: 11 additions & 13 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ jobs:
no_output_timeout: 30m
- run:
name: Run tests
command: |
script/test
command: ./test.clj
# - run:
# name: Performance report
# command: |
Expand Down Expand Up @@ -153,16 +152,16 @@ jobs:
name: Build binary
command: script/compile
no_output_timeout: 30m
- run:
name: Run tests
command: script/test
- run:
name: Release
command: |
curl -sLO https://raw.githubusercontent.com/babashka/babashka/master/install
chmod +x install
sudo ./install
.circleci/script/release
- run:
name: Run tests
command: ./test.clj
- save_cache:
paths:
- ~/.m2
Expand Down Expand Up @@ -210,21 +209,20 @@ jobs:
command: |
script/compile
no_output_timeout: 30m
- run:
name: Run tests
command: |
script/test
# - run:
# name: Performance report
# command: |
# .circleci/script/performance
- run:
name: Release
command: |
curl -sLO https://raw.githubusercontent.com/babashka/babashka/master/install
chmod +x install
sudo ./install
.circleci/script/release
- run:
name: Run tests
command: ./test.clj
# - run:
# name: Performance report
# command: |
# .circleci/script/performance
- save_cache:
paths:
- ~/.m2
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ Only a subset of instaparse is exposed. If you are missing functionality, please

- `parser`
- `parse`
- `parses`
- `failure?`

## Differences with instaparse

- Parser only works on a string grammar input
- The result of `parser` must be used with `parse`, it cannot be called as a function directly
- The result of `parser` must be used with `parse` or `parses`, it cannot be called as a function directly

## Example

``` clojure
(require '[babashka.pods :as pods])

(pods/load-pod 'org.babashka/instaparse "0.0.1")
(pods/load-pod 'org.babashka/instaparse "0.0.3")

;; loading the pod creates the instaparse.core namespace

Expand Down
2 changes: 1 addition & 1 deletion resources/POD_BABASHKA_INSTAPARSE_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.2
0.0.3
16 changes: 15 additions & 1 deletion src/pod/babashka/instaparse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@
(-> (apply insta/parse p opts)
mark-failure)))

(defn parses [ref & opts]
(let [id (::id ref)
p (get @parsers id)]
(-> (apply insta/parses p opts)
mark-failure)))

(def lookup*
{'pod.babashka.instaparse
{#_#_'-parser -parser
'parse parse
'parses parses
'parser -parser
#_#_'-call-parser -call-parser}})

Expand All @@ -86,6 +93,7 @@
#_{"name" "-call-parser"}
{"name" "parser" #_#_"code" parser-wrapper}
{"name" "parse"}
{"name" "parses"}
{"name" "failure?" "code" "(defn failure? [x] (boolean (:pod.babashka.instaparse/failure x)))"}]}]}))

(defn read-transit [^String v]
Expand All @@ -94,11 +102,17 @@
(java.io.ByteArrayInputStream. (.getBytes v "utf-8"))
:json)))

(defn serialize [x]
(defn auto-seq? [x]
(instance? instaparse.auto_flatten_seq.AutoFlattenSeq x))

(defn serialize- [x]
(if (instance? instaparse.auto_flatten_seq.AutoFlattenSeq x)
(seq x)
x))

(defn serialize [x]
(clojure.walk/prewalk serialize- x))

(defn write-transit [v]
(let [baos (java.io.ByteArrayOutputStream.)]
(transit/write (transit/writer baos :json) v)
Expand Down
23 changes: 20 additions & 3 deletions test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,25 @@

(def commit-msg-parser (insta/parser commit-msg-grammar))

(assert (seq? (insta/parse commit-msg-parser "feat: adding a new awesome feature")))
(assert (= '([:TYPE "feat"] [:SUBJECT [:TEXT "adding a new awesome feature"]])
(insta/parse commit-msg-parser "feat: adding a new awesome feature")))

(def commit-msg-parser-enlive (insta/parser commit-msg-grammar :output-format :enlive))

;; test nested AutoFlattenSeqs are handled by serialize
(assert (= '({:tag :TYPE, :content ("feat")}
{:tag :SUBJECT,
:content ({:tag :TEXT, :content ("adding a new awesome feature")})})
(insta/parse commit-msg-parser-enlive "feat: adding a new awesome feature")))
(assert (seq? (insta/parse commit-msg-parser-enlive "feat: adding a new awesome feature")))
(assert (= '(({:tag :TYPE, :content ("feat")}
{:tag :SUBJECT,
:content
({:tag :TEXT, :content ("adding a new awesome feature")})}))
(insta/parses commit-msg-parser-enlive "feat: adding a new awesome feature") ))
(when-not (= "executable" (System/getProperty "org.graalvm.nativeimage.kind"))
(shutdown-agents)
(System/exit 0))
(shutdown-agents)
(System/exit 0))

0 comments on commit 0599cca

Please sign in to comment.