Skip to content

Commit 99c8e1f

Browse files
committed
Parse interpolated strings
Implemented node types: dstr
1 parent d7c729e commit 99c8e1f

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

lib/code_breaker/parsable/data_types.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ def parse_xstr_node(node)
2929
{ node.type => parse_children(node).first }
3030
end
3131

32+
# interpolated string
33+
def parse_dstr_node(node)
34+
values = parse_as_hash(node)[node.type].map do |value|
35+
if value.kind_of?(Array)
36+
value.flatten(1)
37+
else
38+
value
39+
end
40+
end
41+
42+
{ node.type => values }
43+
end
44+
3245
def parse_sym_node(node)
3346
Symbol
3447
end

spec/code_breaker/parser/data_types_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,20 @@
4343
end
4444
end
4545

46-
context 'for a root node representing a interpolated executed string' do
46+
context 'for a root node representing an interpolated executed string' do
4747
it 'returns a Hash with key :xstr and value String' do
4848
input = "%x{string}"
4949
output = { xstr: String }
5050
expect(input).to be_parsed_as output
5151
end
5252
end
53+
54+
context 'for a root node representing an interpolated string' do
55+
it 'returns a Hash with key :dstr and an Array of interpolation values' do
56+
input = '"#{1 + 2} interpolated string #{\'here\'}"'
57+
output = { dstr: [[Fixnum, :+, Fixnum], String, [String]] }
58+
expect(input).to be_parsed_as output
59+
end
60+
end
5361
end
5462
end

0 commit comments

Comments
 (0)