Skip to content

Commit a72845e

Browse files
authored
Merge pull request #776 from interscript/feat/kwargs-directives
feat: kwargs directives through ISC — title_case word_separator, separate separator
2 parents 9a380ae + 84c2d4c commit a72845e

5 files changed

Lines changed: 53 additions & 12 deletions

File tree

‎lib/interscript/isc/document_builder.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ def extract_stage_items(n)
263263
elsif n[:compose]
264264
[{kind: :compose}]
265265
elsif n[:case]
266-
[{kind: :string_case, op: n[:case].to_s}]
266+
item = {kind: :string_case, op: n[:case].to_s}
267+
item[:kwargs] = extract_kwargs(n[:case_kwargs]) if n[:case_kwargs]
268+
[item]
267269
elsif n[:funcall_name]
268270
[{kind: :funcall, name: n[:funcall_name].to_s, kwargs: extract_kwargs(n[:funcall_kwargs])}]
269271
elsif n[:dep]

‎lib/interscript/isc/generator.rb‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,25 @@ def run_target(run)
9696
end
9797

9898
def funcall(rule, pad)
99-
unless rule.kwargs.empty?
100-
raise UnsupportedConstruct,
101-
"cannot generate #{rule.name}(...) with kwargs as ISC"
102-
end
99+
case rule.name
100+
when :separate
101+
separator = rule.kwargs[:separator]
102+
return "#{pad}separate\n" if separator.nil?
103+
104+
"#{pad}separate separator #{item(separator)}\n"
105+
when :title_case
106+
word_separator = rule.kwargs[:word_separator]
107+
return "#{pad}title_case\n" if word_separator.nil?
103108

104-
"#{pad}#{rule.name}\n"
109+
"#{pad}title_case word_separator: #{quote(word_separator)}\n"
110+
else
111+
unless rule.kwargs.empty?
112+
raise UnsupportedConstruct,
113+
"cannot generate #{rule.name}(...) with kwargs as ISC"
114+
end
115+
116+
"#{pad}#{rule.name}\n"
117+
end
105118
end
106119

107120
# -- items --

‎lib/interscript/isc/grammar/concerns/stages.rb‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ module Stages
5454
end
5555

5656
# `downcase`, `upcase`, `title_case` — string-case directives.
57+
# title_case takes an optional word_separator kwarg.
5758
rule(:string_case_directive) do
58-
(str("downcase") | str("upcase") | str("title_case")).as(:case)
59+
(str("downcase") | str("upcase")).as(:case) |
60+
(str("title_case").as(:case) >>
61+
(whitespace >> kwarg_list.as(:case_kwargs)).maybe)
5962
end
6063

6164
# `compose` / `decompose` — Unicode normalization directives.

‎lib/interscript/isc/node_adapter.rb‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ def build_stage(stage_def)
111111
when :run
112112
stage.children << build_run_rule(item)
113113
when :separate
114-
stage.children << Interscript::Node::Rule::Sub.new(
115-
Interscript::Node::Item::String.new(" "),
116-
Interscript::Node::Item::String.new(item[:separator]&.value || "-")
117-
)
114+
kwargs = {}
115+
kwargs[:separator] = item[:separator].value if item[:separator]
116+
stage.children << Interscript::Node::Rule::Funcall.new(:separate, **kwargs)
118117
when :string_case
119-
stage.children << Interscript::Node::Rule::Funcall.new(item[:op].to_sym)
118+
stage.children << Interscript::Node::Rule::Funcall.new(item[:op].to_sym,
119+
**(item[:kwargs] || {}).transform_keys(&:to_sym))
120120
when :compose
121121
stage.children << Interscript::Node::Rule::Funcall.new(:compose)
122122
when :decompose

‎spec/interscript/isc/generator_spec.rb‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ def round_trip_stage(&block)
8686
expect(reparsed.call("ava eve")).to eq(original.call("ava eve"))
8787
end
8888

89+
it "round-trips separate and title_case directives" do
90+
original = document {
91+
stage {
92+
title_case
93+
separate
94+
}
95+
}
96+
reparsed = round_trip(original)
97+
expect(reparsed.call("hello world")).to eq(original.call("hello world"))
98+
99+
kwargs_doc = document {
100+
stage {
101+
title_case word_separator: ""
102+
separate separator: "|"
103+
}
104+
}
105+
kwargs_reparsed = round_trip(kwargs_doc)
106+
expect(kwargs_reparsed.call("hello world hello hello")).to eq(
107+
kwargs_doc.call("hello world hello hello")
108+
)
109+
expect(kwargs_reparsed.call("こんいちは")).to eq(kwargs_doc.call("こんいちは"))
110+
end
111+
89112
it "round-trips multiple stages with a stage run" do
90113
original = document {
91114
stage :first do

0 commit comments

Comments
 (0)