string patterns should now be working
This commit is contained in:
parent
014da297d0
commit
5deab18356
|
@ -47,7 +47,7 @@
|
||||||
(def splat? (= :splat ((last members) :type)))
|
(def splat? (= :splat ((last members) :type)))
|
||||||
(when splat?
|
(when splat?
|
||||||
(when (< val-len patt-len)
|
(when (< val-len patt-len)
|
||||||
(print "mismatched splatted tuple lenghts")
|
(print "mismatched splatted tuple lengths")
|
||||||
(break {:success false :miss [pattern value]}))
|
(break {:success false :miss [pattern value]}))
|
||||||
(print "splat!")
|
(print "splat!")
|
||||||
(set splat (last members))
|
(set splat (last members))
|
||||||
|
@ -75,7 +75,6 @@
|
||||||
{:success true :ctx ctx}
|
{:success true :ctx ctx}
|
||||||
{:success false :miss [pattern value]}))
|
{:success false :miss [pattern value]}))
|
||||||
|
|
||||||
|
|
||||||
(defn- match-list [pattern value ctx]
|
(defn- match-list [pattern value ctx]
|
||||||
(when (not (array? value))
|
(when (not (array? value))
|
||||||
(break {:success false :miss [pattern value]}))
|
(break {:success false :miss [pattern value]}))
|
||||||
|
@ -86,13 +85,13 @@
|
||||||
(def splat? (= :splat ((last members) :type)))
|
(def splat? (= :splat ((last members) :type)))
|
||||||
(when splat?
|
(when splat?
|
||||||
(when (< val-len patt-len)
|
(when (< val-len patt-len)
|
||||||
(print "mismatched splatted tuple lenghts")
|
(print "mismatched splatted list lengths")
|
||||||
(break {:success false :miss [pattern value]}))
|
(break {:success false :miss [pattern value]}))
|
||||||
(print "splat!")
|
(print "splat!")
|
||||||
(set splat (last members))
|
(set splat (last members))
|
||||||
(set members (slice members 0 (dec patt-len))))
|
(set members (slice members 0 (dec patt-len))))
|
||||||
(when (and (not splat?) (not= val-len patt-len))
|
(when (and (not splat?) (not= val-len patt-len))
|
||||||
(print "mismatched tuple lengths")
|
(print "mismatched list lengths")
|
||||||
(break {:success false :miss [pattern value]}))
|
(break {:success false :miss [pattern value]}))
|
||||||
(var curr-mem :^nothing)
|
(var curr-mem :^nothing)
|
||||||
(var curr-val :^nothing)
|
(var curr-val :^nothing)
|
||||||
|
@ -100,7 +99,7 @@
|
||||||
(for i 0 (length members)
|
(for i 0 (length members)
|
||||||
(set curr-mem (get members i))
|
(set curr-mem (get members i))
|
||||||
(set curr-val (get value i))
|
(set curr-val (get value i))
|
||||||
(print "in tuple, matching " curr-val " with ")
|
(print "in list, matching " curr-val " with ")
|
||||||
(pp curr-mem)
|
(pp curr-mem)
|
||||||
(def match? (match-pattern curr-mem curr-val ctx))
|
(def match? (match-pattern curr-mem curr-val ctx))
|
||||||
(pp match?)
|
(pp match?)
|
||||||
|
@ -114,6 +113,21 @@
|
||||||
{:success true :ctx ctx}
|
{:success true :ctx ctx}
|
||||||
{:success false :miss [pattern value]}))
|
{:success false :miss [pattern value]}))
|
||||||
|
|
||||||
|
(defn- match-string [pattern value ctx]
|
||||||
|
(when (not (string? value))
|
||||||
|
(break {:success false :miss [pattern value]}))
|
||||||
|
(def {:compiled compiled :bindings bindings} pattern)
|
||||||
|
(print "matching " value " with")
|
||||||
|
(pp (pattern :grammar))
|
||||||
|
(def matches (peg/match compiled value))
|
||||||
|
(when (not matches)
|
||||||
|
(break {:success false :miss [pattern value]}))
|
||||||
|
(when (not= (length matches) (length bindings))
|
||||||
|
(error "oops: different number of matches and bindings"))
|
||||||
|
(for i 0 (length matches)
|
||||||
|
(set (ctx (bindings i)) (matches i)))
|
||||||
|
{:success true :ctx ctx})
|
||||||
|
|
||||||
(defn- match-pattern* [pattern value &opt ctx]
|
(defn- match-pattern* [pattern value &opt ctx]
|
||||||
(print "in match-pattern, matching " value " with:")
|
(print "in match-pattern, matching " value " with:")
|
||||||
(pp pattern)
|
(pp pattern)
|
||||||
|
@ -136,6 +150,7 @@
|
||||||
:tuple (match-tuple pattern value ctx)
|
:tuple (match-tuple pattern value ctx)
|
||||||
:list (match-list pattern value ctx)
|
:list (match-list pattern value ctx)
|
||||||
# TODO: string-patterns
|
# TODO: string-patterns
|
||||||
|
:interpolated (match-string pattern value ctx)
|
||||||
|
|
||||||
:typed (typed pattern value ctx)
|
:typed (typed pattern value ctx)
|
||||||
))
|
))
|
||||||
|
@ -386,10 +401,20 @@
|
||||||
|
|
||||||
(do
|
(do
|
||||||
(set source `
|
(set source `
|
||||||
match "foo" with {
|
match "foooooo" with {
|
||||||
"f{ooo}o" -> ooo
|
"f{foo}{bar}{baz}" -> (foo, bar, baz)
|
||||||
_ -> :nope
|
_ -> :nope
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
(run)
|
(run)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(def g @{:0 "f" :1 '(<- (to :2)) :2 "o" :3 -1
|
||||||
|
:main '(* :0 :1 :2 :3)})
|
||||||
|
|
||||||
|
(def f '{:0 "b" :1 (<- (to :2)) :2 (* "" -1)
|
||||||
|
:main (* :0 :1 :2)})
|
||||||
|
|
||||||
|
(def h ~(* (<- (to "a")) "a" -1))
|
||||||
|
|
||||||
|
(peg/match f "barrr")
|
||||||
|
|
|
@ -187,7 +187,7 @@ Imports are for a later iteration of Ludus:
|
||||||
(defn- str-pattern [validator]
|
(defn- str-pattern [validator]
|
||||||
(def ast (validator :ast))
|
(def ast (validator :ast))
|
||||||
(def data (ast :data))
|
(def data (ast :data))
|
||||||
(array/pop data)
|
(def last-term (-> data array/pop string))
|
||||||
(def grammar @{})
|
(def grammar @{})
|
||||||
(def bindings @[])
|
(def bindings @[])
|
||||||
(var current 0)
|
(var current 0)
|
||||||
|
@ -202,9 +202,9 @@ Imports are for a later iteration of Ludus:
|
||||||
~(<- (to ,(keyword (inc current)))))
|
~(<- (to ,(keyword (inc current)))))
|
||||||
(array/push bindings (node :data))))
|
(array/push bindings (node :data))))
|
||||||
(set current (inc current)))
|
(set current (inc current)))
|
||||||
|
(set (grammar (keyword current)) ~(* ,last-term -1))
|
||||||
(def rules (map keyword (range (length grammar))))
|
(def rules (map keyword (range (length grammar))))
|
||||||
(set (grammar (keyword current)) -1)
|
(set (grammar :main) ~(* ,;rules))
|
||||||
(set (grammar :main) ~(sequence ,;rules))
|
|
||||||
(set (ast :grammar) grammar)
|
(set (ast :grammar) grammar)
|
||||||
(set (ast :compiled) (peg/compile grammar))
|
(set (ast :compiled) (peg/compile grammar))
|
||||||
(set (ast :bindings) bindings))
|
(set (ast :bindings) bindings))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user