[#102] Add java.util.regex.Pattern

This commit is contained in:
Michiel Borkent 2019-11-13 21:57:05 +01:00
parent 0bcb568ed8
commit 4960ed769b
6 changed files with 95 additions and 14 deletions

View file

@ -1,3 +1,6 @@
{:lint-as {me.raynes.conch/let-programs clojure.core/let
babashka.impl.File/gen-wrapper-fn clojure.core/def
babashka.impl.File/gen-wrapper-fn-2 clojure.core/def}}
babashka.impl.Pattern/gen-wrapper-fn clojure.core/def
babashka.impl.File/gen-wrapper-fn-2 clojure.core/def
babashka.impl.Pattern/gen-wrapper-fn-2 clojure.core/def
babashka.impl.Pattern/gen-constants clojure.core/declare}}

View file

@ -179,6 +179,7 @@ From Java the following is available:
- static methods: `exit`, `getProperty`, `setProperty`, `getProperties`, `getenv`
- `Thread`:
- static methods: `sleep`
- `java.util.regex.Pattern` (all static and instance methods and constants)
Special vars:

View file

@ -20,7 +20,7 @@
(gen-wrapper-fn canExecute)
(gen-wrapper-fn canRead)
(gen-wrapper-fn canWrite)
(defn ^:bb/export createTempFile
(defn createTempFile
([^String prefix ^String suffix]
(java.io.File/createTempFile prefix suffix))
([^String prefix ^String suffix ^java.io.File dir]
@ -66,13 +66,13 @@
(-> (reduce (fn [acc [k v]]
(if (-> v meta :bb/export)
(assoc acc (symbol (str "." k))
@v)
@v)
acc))
{}
(ns-publics *ns*))
;; static method
(dissoc (symbol ".createTempFile"))
(assoc (symbol "File/createTempFile") createTempFile)))
{}
(ns-publics *ns*))
;; static methods
(assoc (symbol "File/createTempFile") createTempFile)
(assoc (symbol "java.io.File/createTempFile") createTempFile)))
(comment
(canRead (clojure.java.io/file "README.md"))
@ -81,10 +81,7 @@
(renameTo (io/file "/tmp/script2.clj") (io/file "/tmp/script.clj"))
(.setWritable (io/file "/tmp/script.clj") true true)
(meta #'toURI)
(first bindings)
(get bindings '.length)
(get bindings '.canWrite)
(meta #'canWrite)
;; for README.md:
(str/join ", " (map #(format "`%s`" %) (sort (keys bindings))))
(str/join ", " (map #(format "`%s`" %) (sort (keys file-bindings))))
)

View file

@ -0,0 +1,72 @@
(ns babashka.impl.Pattern
{:no-doc true})
(set! *warn-on-reflection* true)
(defmacro gen-wrapper-fn [method-name]
`(defn ~method-name {:bb/export true}
[~(with-meta 'x {:tag 'java.util.regex.Pattern})]
(~(symbol (str "." method-name)) ~'x)))
(defmacro gen-wrapper-fn-2 [method-name]
`(defn ~method-name {:bb/export true}
[~(with-meta 'x {:tag 'java.util.regex.Pattern}) ~'y]
(~(symbol (str "." method-name)) ~'x ~'y)))
(defmacro gen-constants [& constant-names]
(let [defs (for [constant-name constant-names
:let [full-name (symbol (str "java.util.regex.Pattern/" constant-name))
meta {:bb/export-constant true
:bb/full-name (list 'quote full-name)}
constant-name (with-meta constant-name meta)]]
`(def ~constant-name
~full-name))]
`(do ~@defs)))
(gen-constants CANON_EQ CASE_INSENSITIVE COMMENTS DOTALL LITERAL MULTILINE
UNICODE_CASE UNICODE_CHARACTER_CLASS UNIX_LINES)
(gen-wrapper-fn asPredicate)
(defn compile*
([^String s]
(java.util.regex.Pattern/compile s))
([^String s ^long flags]
(java.util.regex.Pattern/compile s flags)))
(gen-wrapper-fn flags)
(gen-wrapper-fn-2 matcher)
(defn matches [^String regex ^CharSequence input]
(java.util.regex.Pattern/matches regex input))
(gen-wrapper-fn pattern)
(defn pattern-quote [s]
(java.util.regex.Pattern/quote s))
(defn ^:bb/export split
([^java.util.regex.Pattern p ^CharSequence input]
(.split p input))
([^java.util.regex.Pattern p ^CharSequence input ^long limit]
(.split p input limit)))
(gen-wrapper-fn-2 splitAsStream)
(def pattern-bindings
(-> (reduce (fn [acc [k v]]
(let [m (meta v)]
(cond (:bb/export m)
(assoc acc (symbol (str "." k))
@v),
(:bb/export-constant m)
(assoc acc (symbol (:bb/full-name m))
@v)
:else acc)))
{}
(ns-publics *ns*))
;; static method
(assoc (symbol "java.util.regex.Pattern/compile") compile*)
(assoc (symbol "java.util.regex.Pattern/quote") pattern-quote)
(assoc (symbol "java.util.regex.Pattern/matches") matches)))

View file

@ -3,6 +3,7 @@
(:require
[babashka.impl.File :refer [file-bindings]]
[babashka.impl.Integer :refer [integer-bindings]]
[babashka.impl.Pattern :refer [pattern-bindings]]
[babashka.impl.System :refer [system-bindings]]
[babashka.impl.Thread :refer [thread-bindings]]
[babashka.impl.async :refer [async-namespace]]
@ -10,11 +11,11 @@
[babashka.impl.clojure.java.io :refer [io-namespace]]
[babashka.impl.clojure.stacktrace :refer [print-stack-trace]]
[babashka.impl.conch :refer [conch-namespace]]
[babashka.impl.csv :as csv]
[babashka.impl.exceptions :refer [exception-bindings]]
[babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]]
[babashka.impl.socket-repl :as socket-repl]
[babashka.impl.tools.cli :refer [tools-cli-namespace]]
[babashka.impl.csv :as csv]
[babashka.wait :as wait]
[clojure.edn :as edn]
[clojure.java.io :as io]
@ -152,7 +153,8 @@ Everything after that is bound to *command-line-args*."))
file-bindings
thread-bindings
integer-bindings
exception-bindings))
exception-bindings
pattern-bindings))
(defn read-edn []
(edn/read {;;:readers *data-readers*

View file

@ -236,3 +236,9 @@
(deftest assert-test
(is (thrown-with-msg? Exception #"should-be-true"
(bb nil "(def should-be-true false) (assert should-be-true)"))))
(deftest Pattern-test
(is (= ["1" "2" "3"]
(bb nil "(vec (.split (java.util.regex.Pattern/compile \"f\") \"1f2f3\"))")))
(is (= java.util.regex.Pattern/CANON_EQ
(bb nil "java.util.regex.Pattern/CANON_EQ"))))