Skip to content

Commit d5dd8a6

Browse files
Merge pull request project-eutopia#126 from project-eutopia/list_and_hash_across_multiple_lines
Allow hash to be defined across multiple lines
2 parents e25ef4b + 6e15b75 commit d5dd8a6

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

lib/keisan/parsing/hash.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ def initialize(key_value_pairs)
1717
private
1818

1919
def validate_and_extract_key_value_pair(key_value_pair)
20-
key, value = Util.array_split(key_value_pair) {|token| token.is_a?(Tokens::Colon)}
20+
filtered_key_value_pair = key_value_pair.select {|token|
21+
!token.is_a?(Tokens::LineSeparator)
22+
}
23+
key, value = Util.array_split(filtered_key_value_pair) {|token|
24+
token.is_a?(Tokens::Colon)
25+
}
2126
raise Exceptions::ParseError.new("Invalid hash") unless key.size == 1 && value.size >= 1
2227

2328
key = key.first

spec/keisan/ast/hash_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@
4141
expect(calculator.evaluate("[['a', 3], ['b', 7]].to_h").value).to eq({"a" => 3, "b" => 7})
4242
end
4343
end
44+
45+
it "can be defined across multiple lines" do
46+
calculator = Keisan::Calculator.new
47+
calculator.evaluate("h = {\n'a':\n'foo',\n'b': 'bar'}")
48+
expect(calculator.evaluate("h")).to eq({"a" => "foo", "b" => "bar"})
49+
end
4450
end

spec/keisan/ast/list_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@
4141
expect(result.to_s).to eq "[1,2,3,4]"
4242
end
4343
end
44+
45+
it "can be defined across multiple lines" do
46+
calculator = Keisan::Calculator.new
47+
calculator.evaluate("l = [\n'a',\n'foo'\n]")
48+
expect(calculator.evaluate("l")).to eq(["a", "foo"])
49+
end
4450
end

0 commit comments

Comments
 (0)