Class: RedParse::HashLiteralNode

Inherits:
ValueNode show all
Defined in:
lib/redparse/node.rb,
lib/redparse/ripper.rb,
lib/redparse/ReduceWithsFor_RedParse_1_9.rb,
lib/redparse/ReduceWithsFor_RedParse_1_8.rb

Constant Summary

Constants included from FlattenedIvars

FlattenedIvars::EXCLUDED_IVARS

Instance Attribute Summary collapse

Attributes inherited from Node

#endline, #errors, #offset, #parent, #startline

Attributes included from Stackable::Meta

#boolean_identity_params, #identity_params

Instance Method Summary collapse

Methods inherited from ValueNode

#lvalue, #reducer_method

Methods inherited from Node

#+, #+@, #==, [], #[]=, #add_parent_links!, #args_rip, #begin_parsetree, #classic_inspect, create, #data, #deep_copy, #delete_extraneous_ivars!, #delete_linenums!, #depthwalk, #depthwalk_nodes, #evalable_inspect, #fixup_multiple_assignments!, #fixup_rescue_assignments!, #force_stmt_list_rip, #initialize_ivars, inline_symbols, #inspect, #lhs_unparse, #linerange, #lvalue, #lvars_defined_in, #merge_replacement_session, namelist, #negate, #original_brackets_assign, param_names, #parsetrees, #pretty_print, #prohibit_fixup, #replace_ivars_and_self, #replace_value, #rescue_parsetree, #rfind, #rfind_all, #rgrep, #rip_and_rescues, #rip_explode!, #short_inspect, #stmts_rip, #to_parsetree, #to_parsetree_and_warnings, #to_ruby, #to_s, #unary, #walk, #xform_tree!

Methods included from Stackable::Meta

#build_exemplars, #enumerate_exemplars, #identity_param

Methods included from FlattenedIvars

#flattened_ivars, #flattened_ivars_equal?

Methods included from Stackable

#identity_name

Constructor Details

#initialize(open, contents, close) ⇒ HashLiteralNode

Returns a new instance of HashLiteralNode.



4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
# File 'lib/redparse/node.rb', line 4655

def initialize(open,contents,close)
  @offset=open.offset rescue contents.first.offset
  case contents
  when nil; super()
  when ArrowOpNode; super(contents.first,contents.last)
  when CommaOpNode,Array
    if ArrowOpNode===contents.first
      data=[]
      contents.each{|pair|
        ArrowOpNode===pair or fail
        data.push pair.first,pair.last
      }
    else
      @no_arrows=true
      data=Array[*contents]
    end
    super(*data)
  else fail
  end
  @no_braces=true unless open
end

Instance Attribute Details

#no_arrows ⇒ Object (readonly)

Returns the value of attribute no_arrows.



4677
4678
4679
# File 'lib/redparse/node.rb', line 4677

def no_arrows
  @no_arrows
end

#no_braces ⇒ Object

Returns the value of attribute no_braces.



4678
4679
4680
# File 'lib/redparse/node.rb', line 4678

def no_braces
  @no_braces
end

#offset=(value) ⇒ Object (writeonly)

Sets the attribute offset

Parameters:

  • value —

    the value to set the attribute offset to.



4679
4680
4681
# File 'lib/redparse/node.rb', line 4679

def offset=(value)
  @offset = value
end

Instance Method Details

#as_h ⇒ Object



4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
# File 'lib/redparse/node.rb', line 4709

def as_h
  return @h if defined? @h
  @h={}
  (0...size).step(2){|i|
    k=self[i].dup
    k.delete_extraneous_ivars!
    k.delete_linenums!
    @h[k]=self[i+1]
  }
  return @h
end

#blame ⇒ Object



4730
# File 'lib/redparse/node.rb', line 4730

def blame; self end

#error?(rubyversion = 1.8) ⇒ Boolean

Returns:

  • (Boolean)


4725
4726
4727
4728
# File 'lib/redparse/node.rb', line 4725

def error? rubyversion=1.8
  return true if defined?(@no_arrows) and rubyversion>=1.9
  return super
end

#get(k) ⇒ Object



4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
# File 'lib/redparse/node.rb', line 4697

def get k
  case k
  when Node; 
    k.delete_extraneous_ivars!
    k.delete_linenums!
  when Symbol, Numeric; k=LiteralNode[k]
  when true,false,nil; k=VarLikeNode[k.inspect]
  else raise ArgumentError
  end
  return as_h[k]
end

#image ⇒ Object



4681
# File 'lib/redparse/node.rb', line 4681

def image; "({})" end

#msg ⇒ Object



4731
# File 'lib/redparse/node.rb', line 4731

def msg; "comma not allowed within pairs in hashes anymore" end

#parsetree(o) ⇒ Object



4721
4722
4723
# File 'lib/redparse/node.rb', line 4721

def parsetree(o)
  map{|elem| elem.rescue_parsetree(o)}.unshift :hash
end

#reducer_ident ⇒ Object



17375
17376
17377
# File 'lib/redparse/ReduceWithsFor_RedParse_1_9.rb', line 17375

def reducer_ident
  :HashLiteralNode
end

#rip(p) ⇒ Object



386
387
388
389
390
# File 'lib/redparse/ripper.rb', line 386

def rip p
  list=[]
  (0...size).step(2){|i| list.push p.on_assoc_new(self[i].rip(p),self[i+1].rip(p)) }
  p.on_hash((p.on_assoclist_from_args(list) unless empty?))
end

#unparse(o = default_unparse_options) ⇒ Object



4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
# File 'lib/redparse/node.rb', line 4683

def unparse o=default_unparse_options
  result=''
  result << "{" unless defined? @no_braces and @no_braces
  arrow= defined?(@no_arrows) ? " , " : " => "
  (0...size).step(2){|i| 
    result<< unparse_nl(self[i],o,'')+
      self[i].unparse_maybe_parens(o)+arrow+
      self[i+1].unparse_maybe_parens(o)+', '
  }
  result.chomp! ', '
  result << "}" unless defined? @no_braces and @no_braces
  return result
end