lib/faml/static_hash_parser.rb in faml-0.3.3 vs lib/faml/static_hash_parser.rb in faml-0.3.4
- old
+ new
@@ -56,50 +56,39 @@
else
throw FAILURE_TAG
end
end
- SYMBOL_FIRST_CHARS = [
- ':', # { :'foo' => 'bar' } or { :"foo" => 'bar' }
- "'", # { 'foo': 'bar' }
- '"', # { "foo": 'bar' }
- ].freeze
-
- def eval_symbol(code)
- if SYMBOL_FIRST_CHARS.include?(code[0])
- eval(code).to_sym
- else
- code.to_sym
- end
- end
-
def try_static_key(node)
case node.type
- when :sym
- eval_symbol(node.location.expression.source)
- when :int, :float, :str
- eval(node.location.expression.source)
+ when :sym, :int, :float, :str, :rational, :complex
+ node.children[0]
end
end
def try_static_value(key_static, node)
case node.type
- when :sym
- @static_attributes[key_static] = eval_symbol(node.location.expression.source)
- when :true, :false, :nil, :int, :float, :str
- @static_attributes[key_static] = eval(node.location.expression.source)
+ when :sym, :int, :float, :str, :rational, :complex
+ @static_attributes[key_static] = node.children[0]
+ when :true
+ @static_attributes[key_static] = true
+ when :false
+ @static_attributes[key_static] = false
+ when :nil
+ @static_attributes[key_static] = nil
when :dstr
@dynamic_attributes[key_static] = node.location.expression.source
when :send
if SPECIAL_ATTRIBUTES.include?(key_static.to_s)
throw FAILURE_TAG
else
@dynamic_attributes[key_static] = node.location.expression.source
end
when :hash
try_static_hash_value(key_static, node)
- # TODO: Add array case
+ when :array
+ try_static_array_value(key_static, node)
else
throw FAILURE_TAG
end
end
@@ -122,8 +111,15 @@
expr = parser.dynamic_attributes.map do |k, v|
"#{k.inspect} => #{v}"
end.join(', ')
@dynamic_attributes[key_static] = "{#{expr}}"
end
+ end
+
+ def try_static_array_value(key_static, node)
+ arr = node.children.map do |child|
+ try_static_value(key_static, child)
+ end
+ @static_attributes[key_static] = arr
end
end
end