1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

Accept normal hash parsing

This commit is contained in:
Takashi Kokubun 2015-11-04 23:31:41 +09:00
parent df14cc4fef
commit 06304a2e8c
2 changed files with 32 additions and 1 deletions

View file

@ -10,7 +10,7 @@ module Hamlit
end
def parse(text)
exp = '{' << text.strip << '}'.freeze
exp = wrap_bracket(text)
return if syntax_error?(exp)
hash = {}
@ -26,6 +26,12 @@ module Hamlit
private
def wrap_bracket(text)
text = text.strip
return text if text[0] == '{'.freeze
'{' << text << '}'.freeze
end
def parse_key!(tokens)
_, type, str = tokens.shift
case type
@ -89,6 +95,8 @@ module Hamlit
paren_open += 1
when :on_rparen
paren_open -= 1
when :on_sp
next if attr_tokens.empty?
end
attr_tokens << token

View file

@ -5,10 +5,13 @@ describe Hamlit::HashParser do
end
it { assert_parse('', {}) }
it { assert_parse('{}', {}) }
describe 'invalid hash' do
it { assert_parse(' hash ', nil) }
it { assert_parse('hash, foo: bar', nil) }
it { assert_parse(' {hash} ', nil) }
it { assert_parse(' { hash, foo: bar } ', nil) }
end
describe 'dynamic key' do
@ -22,12 +25,19 @@ describe Hamlit::HashParser do
it { assert_parse(' foo: bar ', { 'foo' => 'bar' }) }
it { assert_parse('a: b, c: :d', { 'a' => 'b', 'c' => ':d' }) }
it { assert_parse('a: [], c: "d"', { 'a' => '[]', 'c' => '"d"' }) }
it { assert_parse(' { _:1, } ', { '_' => '1' }) }
it { assert_parse(' { foo: bar } ', { 'foo' => 'bar' }) }
it { assert_parse(' { a: b, c: :d } ', { 'a' => 'b', 'c' => ':d' }) }
it { assert_parse(' { a: [], c: "d" } ', { 'a' => '[]', 'c' => '"d"' }) }
end
describe ':foo => bar' do
it { assert_parse(' :foo => :bar ', { 'foo' => ':bar' }) }
it { assert_parse(':_=>"foo"', { '_' => '"foo"' }) }
it { assert_parse(':a => [], c: "", :b => "#{3}"', { 'a' => '[]', 'c' => '""', 'b' => '"#{3}"' }) }
it { assert_parse(' { :foo => :bar } ', { 'foo' => ':bar' }) }
it { assert_parse(' { :_=>"foo" } ', { '_' => '"foo"' }) }
it { assert_parse(' { :a => [], c: "", :b => "#{3}" } ', { 'a' => '[]', 'c' => '""', 'b' => '"#{3}"' }) }
it { assert_parse(':"f#{o}o" => bar', nil) }
it { assert_parse(':"#{f}oo" => bar', nil) }
it { assert_parse(':"#{foo}" => bar', nil) }
@ -37,10 +47,14 @@ describe Hamlit::HashParser do
it { assert_parse('"foo"=>[1]', { 'foo' => '[1]' }) }
it { assert_parse(" 'foo' => nya ", { 'foo' => 'nya' }) }
it { assert_parse('%q[foo] => bar ', { 'foo' => 'bar' }) }
it { assert_parse(' { "foo"=>[1] } ', { 'foo' => '[1]' }) }
it { assert_parse(" { 'foo' => nya } ", { 'foo' => 'nya' }) }
it { assert_parse(' { %q[foo] => bar } ', { 'foo' => 'bar' }) }
it { assert_parse('"f#{o}o" => bar', nil) }
it { assert_parse('"#{f}oo" => bar', nil) }
it { assert_parse('"#{foo}" => bar', nil) }
it { assert_parse('%q[f#{o}o] => bar ', { 'f#{o}o' => 'bar' }) }
it { assert_parse(' { %q[f#{o}o] => bar, } ', { 'f#{o}o' => 'bar' }) }
it { assert_parse('%Q[f#{o}o] => bar ', nil) }
end
@ -48,6 +62,8 @@ describe Hamlit::HashParser do
describe '"foo" => bar' do
it { assert_parse('"foo":()', { 'foo' => '()' }) }
it { assert_parse(" 'foo': nya ", { 'foo' => 'nya' }) }
it { assert_parse(' { "foo":() , }', { 'foo' => '()' }) }
it { assert_parse(" { 'foo': nya , }", { 'foo' => 'nya' }) }
it { assert_parse('"f#{o}o": bar', nil) }
it { assert_parse('"#{f}oo": bar', nil) }
it { assert_parse('"#{foo}": bar', nil) }
@ -58,16 +74,23 @@ describe Hamlit::HashParser do
it { assert_parse('foo: [1,2,],', { 'foo' => '[1,2,]' }) }
it { assert_parse('foo: [1,2,[3,4],5],', { 'foo' => '[1,2,[3,4],5]' }) }
it { assert_parse('foo: [1,2,[3,4],5],bar: [[1,2],],', { 'foo' => '[1,2,[3,4],5]', 'bar' => '[[1,2],]'}) }
it { assert_parse(' { foo: [1,2,], } ', { 'foo' => '[1,2,]' }) }
it { assert_parse(' { foo: [1,2,[3,4],5], } ', { 'foo' => '[1,2,[3,4],5]' }) }
it { assert_parse(' { foo: [1,2,[3,4],5],bar: [[1,2],], } ', { 'foo' => '[1,2,[3,4],5]', 'bar' => '[[1,2],]'}) }
end
describe 'nested hash' do
it { assert_parse('foo: { }, bar: {}', { 'foo' => '{ }', 'bar' => '{}' }) }
it { assert_parse('foo: { bar: baz, hoge: fuga, }, ', { 'foo' => '{ bar: baz, hoge: fuga, }' }) }
it { assert_parse('data: { confirm: true, disable: false }, :hello => { world: foo, },', { 'data' => '{ confirm: true, disable: false }', 'hello' => '{ world: foo, }' }) }
it { assert_parse(' { foo: { }, bar: {} } ', { 'foo' => '{ }', 'bar' => '{}' }) }
it { assert_parse(' { foo: { bar: baz, hoge: fuga, }, } ', { 'foo' => '{ bar: baz, hoge: fuga, }' }) }
it { assert_parse(' { data: { confirm: true, disable: false }, :hello => { world: foo, }, } ', { 'data' => '{ confirm: true, disable: false }', 'hello' => '{ world: foo, }' }) }
end
describe 'nested method' do
it { assert_parse('foo: bar(a, b), hoge: piyo(a, b,),', { 'foo' => 'bar(a, b)', 'hoge' => 'piyo(a, b,)' }) }
it { assert_parse(' { foo: bar(a, b), hoge: piyo(a, b,), } ', { 'foo' => 'bar(a, b)', 'hoge' => 'piyo(a, b,)' }) }
end
end
end