From 0599ccaf6ddbf291337659936ca298c24925df8a Mon Sep 17 00:00:00 2001 From: Jude Payne Date: Mon, 24 Apr 2023 19:03:40 +0100 Subject: [PATCH] Added `parses` & ready for 0.0.3 (#3) --- .circleci/config.yml | 24 +++++++++++------------ README.md | 5 +++-- resources/POD_BABASHKA_INSTAPARSE_VERSION | 2 +- src/pod/babashka/instaparse.clj | 16 ++++++++++++++- test.clj | 23 +++++++++++++++++++--- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b9f0d5a..1165fdd 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -87,8 +87,7 @@ jobs: no_output_timeout: 30m - run: name: Run tests - command: | - script/test + command: ./test.clj # - run: # name: Performance report # command: | @@ -153,9 +152,6 @@ jobs: name: Build binary command: script/compile no_output_timeout: 30m - - run: - name: Run tests - command: script/test - run: name: Release command: | @@ -163,6 +159,9 @@ jobs: chmod +x install sudo ./install .circleci/script/release + - run: + name: Run tests + command: ./test.clj - save_cache: paths: - ~/.m2 @@ -210,14 +209,6 @@ 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: | @@ -225,6 +216,13 @@ jobs: 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 diff --git a/README.md b/README.md index 1d79b4b..a7c3101 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/resources/POD_BABASHKA_INSTAPARSE_VERSION b/resources/POD_BABASHKA_INSTAPARSE_VERSION index 4e379d2..bcab45a 100644 --- a/resources/POD_BABASHKA_INSTAPARSE_VERSION +++ b/resources/POD_BABASHKA_INSTAPARSE_VERSION @@ -1 +1 @@ -0.0.2 +0.0.3 diff --git a/src/pod/babashka/instaparse.clj b/src/pod/babashka/instaparse.clj index 0a3cb9f..83d2254 100644 --- a/src/pod/babashka/instaparse.clj +++ b/src/pod/babashka/instaparse.clj @@ -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}}) @@ -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] @@ -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) diff --git a/test.clj b/test.clj index ebc2037..ec4431a 100755 --- a/test.clj +++ b/test.clj @@ -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))