Skip to content
This repository was archived by the owner on Feb 15, 2026. It is now read-only.

Commit cee676d

Browse files
HertzDevilPapierkorb
authored andcommitted
Template::Seq -> Sequence
1 parent adcc9eb commit cee676d

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

spec/bindgen/template_spec.cr

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe "Template" do
2626
it "returns false for any other templates" do
2727
conversion1 = Bindgen::Template::Basic.new("%x")
2828
conversion2 = Bindgen::Template::Basic.new("%x", simple: true)
29-
conversion3 = Bindgen::Template::Seq.new(conversion1, conversion2)
29+
conversion3 = Bindgen::Template::Sequence.new(conversion1, conversion2)
3030

3131
conversion1.no_op?.should be_false
3232
conversion2.no_op?.should be_false
@@ -38,7 +38,7 @@ describe "Template" do
3838
it "composes two templates" do
3939
conversion1 = Bindgen::Template::Basic.new("%x")
4040
conversion2 = Bindgen::Template::Basic.new("%y")
41-
conversion3 = Bindgen::Template::Seq.new(conversion1, conversion2)
41+
conversion3 = Bindgen::Template::Sequence.new(conversion1, conversion2)
4242

4343
conversion1.followed_by(conversion2).should eq(conversion3)
4444
end
@@ -75,12 +75,16 @@ describe "Template" do
7575
end
7676
end
7777

78-
describe "Seq#template" do
79-
it "composes two templates" do
80-
first = Bindgen::Template::Basic.new("%_a")
81-
second = Bindgen::Template::Basic.new("%_b")
82-
conversion = Bindgen::Template::Seq.new(first: first, second: second)
78+
describe "Sequence#template" do
79+
it "composes multiple templates" do
80+
a_conv = Bindgen::Template::Basic.new("%_a")
81+
b_conv = Bindgen::Template::Basic.new("%_b")
82+
83+
conversion = Bindgen::Template::Sequence.new(a_conv, b_conv)
8384
conversion.template("c").should eq("c_a_b")
85+
86+
conversion = Bindgen::Template::Sequence.new(a_conv, a_conv, a_conv, b_conv, b_conv, a_conv)
87+
conversion.template("c").should eq("c_a_a_a_b_b_a")
8488
end
8589
end
8690
end

src/bindgen/template.cr

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module Bindgen
2828
def followed_by(other : Base) : Base
2929
return other if no_op?
3030
return self if other.no_op?
31-
Seq.new(first: self, second: other)
31+
Sequence.new(self, other)
3232
end
3333
end
3434

@@ -68,19 +68,31 @@ module Bindgen
6868
def_equals @pattern, @simple
6969
end
7070

71-
# Compound template which runs *code* through two child templaters.
72-
class Seq < Base
73-
@first : Base
74-
@second : Base
75-
76-
def initialize(@first, @second)
71+
# Compound template which runs *code* through multiple child templater, in
72+
# the order they are given in the constructor.
73+
class Sequence < Base
74+
getter children = Array(Base).new
75+
76+
def initialize(*conversions : Base)
77+
conversions.each do |conversion|
78+
# Flatten `Sequence` templates automatically.
79+
if conversion.is_a?(Sequence)
80+
@children.concat(conversion.children)
81+
else
82+
@children << conversion
83+
end
84+
end
7785
end
7886

7987
def template(code) : String
80-
@second.template(@first.template(code))
88+
@children.each do |conversion|
89+
code = conversion.template(code)
90+
end
91+
92+
code
8193
end
8294

83-
def_equals @first, @second
95+
def_equals @children
8496
end
8597

8698
# Template to transform `Proc`s for Crystal wrapper types into `Proc`

0 commit comments

Comments
 (0)