Skip to content

Commit 5b0e880

Browse files
committed
initial attempt at protocols and records
1 parent 31a95b6 commit 5b0e880

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

pixie/regex.pxi renamed to pixie/re.pxi

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(ns pixie.regex
1+
(ns pixie.re
22
(:require [pixie.ffi-infer :as i]))
33

44
(i/with-config {:library "cre2"
@@ -62,12 +62,6 @@
6262
(doseq [key opts] ((key optmap) opt))
6363
opt))
6464

65-
(defn regexp
66-
{:doc "Returns internal representation for regular
67-
expression, used in matching functions."
68-
:signatures [[rexegp-str opts]]}
69-
[regexp-str opts]
70-
(cre2_new regexp-str (count regexp-str) (cre2-opts opts)))
7165

7266
(defn match
7367
[pattern text]
@@ -80,3 +74,39 @@
8074
1 ;; anchor 1 - no, 2 - start, 3 - both
8175
(cre2_string_t)
8276
(+ 1 (cre2_num_capturing_groups pattern))))
77+
78+
(defn regex
79+
{:doc "Returns internal representation for regular
80+
expression, used in matching functions."
81+
:signatures [[rexeg-str opts]]}
82+
[regex-str opts]
83+
(re-pattern regex-str opts))
84+
85+
(defprotocol IRegex
86+
(re-matches [r s])
87+
(re-find [r s]))
88+
89+
(defrecord CRE2Regex [pattern opts]
90+
IFinalize
91+
(-finalize! [this]
92+
(println "dropping cre2 obj " this)
93+
(cre2_opt_delete opts)
94+
(cre2_delete pattern))
95+
96+
IRegex)
97+
98+
(def ^:dynamic *default-re-engine* :cre2)
99+
100+
;; an "open" engine registry
101+
(defmulti re-engine (fn [k s o] k))
102+
103+
;; add cre2 to registry
104+
(defmethod re-engine :cre2 [_ regex-str opts]
105+
(let [copts (cre2-opts opts)]
106+
(->CRE2Regex (cre2_new regex-str (count regex-str) copts) copts)))
107+
108+
;; dispatch on the right engine constructor via registry
109+
(defn re-pattern
110+
([s o] (re-pattern s o *default-re-engine*))
111+
([s o kw] (re-engine kw s o)))
112+

pixie/vm/reader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,8 @@ def invoke(self, rdr, ch):
648648
except EOFError:
649649
break
650650

651-
return rt.cons(symbol(u"regexp"), rt.cons(regex_str, rt.cons(regex_opts, nil)))
651+
return rt.cons(symbol(u"pixie.re/regex"),
652+
rt.cons(regex_str, rt.cons(regex_opts, nil)))
652653

653654
dispatch_handlers = {
654655
u"{": SetReader(),

0 commit comments

Comments
 (0)