Skip to content

Commit 41240e9

Browse files
committed
Allow use of do syntax with matchers
1 parent c06be37 commit 41240e9

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/matchers.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ end
125125
# 3. Callback: takes arguments Dictionary × Number of elements matched
126126
#
127127
function matcher(val::Any)
128-
function literal_matcher(data, bindings, ctx, next)
128+
function literal_matcher(next, data, bindings, ctx)
129129
!isempty(data) && isequal(car(data), val) ? next(bindings, 1) : nothing
130130
end
131131
end
132132

133133
function matcher(slot::Slot)
134-
function slot_matcher(data, bindings, ctx, next)
134+
function slot_matcher(next, data, bindings, ctx)
135135
isempty(data) && return
136136
val = get(bindings, slot.name, nothing)
137137
if val !== nothing
@@ -175,7 +175,7 @@ function trymatchexpr(data, value, n)
175175
end
176176

177177
function matcher(segment::Segment)
178-
function segment_matcher(data, bindings, ctx, success)
178+
function segment_matcher(success, data, bindings, ctx)
179179
val = get(bindings, segment.name, nothing)
180180

181181
if val !== nothing
@@ -204,7 +204,7 @@ end
204204

205205
function matcher(term::Term)
206206
matchers = (matcher(operation(term)), map(matcher, arguments(term))...,)
207-
function term_matcher(data, bindings, ctx, success)
207+
function term_matcher(success, data, bindings, ctx)
208208

209209
isempty(data) && return nothing
210210
!(car(data) isa Term) && return nothing
@@ -216,8 +216,9 @@ function matcher(term::Term)
216216
end
217217
return nothing
218218
end
219-
res = car(matchers′)(term, bindings′, ctx,
220-
(b, n) -> loop(drop_n(term, n), b, cdr(matchers′)))
219+
car(matchers′)(term, bindings′, ctx) do b, n
220+
loop(drop_n(term, n), b, cdr(matchers′))
221+
end
221222
end
222223

223224
loop(car(data), bindings, matchers) # Try to eat exactly one term

src/rule_dsl.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ const EMPTY_DICT = ImmutableDict{Symbol, Any}(:____, nothing)
3131
struct EmptyCtx end
3232

3333
function (r::Rule)(term, ctx=EmptyCtx())
34-
match_function = r.matcher
3534
rhs = r.rhs
3635

37-
match_function((term,),
38-
EMPTY_DICT,
39-
ctx,
40-
(d, n) -> n === 1 ? (@timer "RHS" rhs(d, ctx)) : nothing)
36+
r.matcher((term,), EMPTY_DICT, ctx) do bindings, n
37+
# n == 1 means that exactly one term of the input (term,) was matched
38+
n === 1 ? (@timer "RHS" rhs(bindings, ctx)) : nothing
39+
end
4140
end
4241

4342
"""

0 commit comments

Comments
 (0)