Class: Bison::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/bison/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ Action

Returns a new instance of Action.



12
13
14
# File 'lib/bison/action.rb', line 12

def initialize(code)
  self.code = code
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



7
8
9
# File 'lib/bison/action.rb', line 7

def code
  @code
end

#locationObject

Returns the value of attribute location.



8
9
10
# File 'lib/bison/action.rb', line 8

def location
  @location
end

#predecessorsObject

Returns the value of attribute predecessors.



9
10
11
# File 'lib/bison/action.rb', line 9

def predecessors
  @predecessors
end

#sequenceObject

Returns the value of attribute sequence.



10
11
12
# File 'lib/bison/action.rb', line 10

def sequence
  @sequence
end

Instance Method Details

#errorsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bison/action.rb', line 34

def errors
  tmp = Tempfile.new('action-src.rb').tap do |tmp|
    location[0].times{ tmp.puts }
    tmp.puts(code)
    tmp.close
  end

  errors = `ruby -c "#{tmp.path}" 2>&1`
  if $?.success?
    return nil
  else
    return errors.gsub(tmp.path, '-')
  end
end

#funcall(receiver) ⇒ Object

What to do about default $1/Qnil?



55
56
57
58
59
60
# File 'lib/bison/action.rb', line 55

def funcall(receiver)
  method = "rb_intern(#{name.inspect})"
  args = predecessor_tags.keys.map{ |i| "$#{i}" }.join(', ')
  args = args.empty? ? '0' : "#{predecessor_tags.size}, #{args}"
  "rb_funcall(#{receiver}, #{method}, #{args})"
end

#nameObject



49
50
51
52
# File 'lib/bison/action.rb', line 49

def name
  base = "_#{sequence.index}_#{sequence.rule.name}"
  base << "_#{Digest::MD5.hexdigest(predecessor_tags.inspect)}"
end

#predecessor_tagsObject



26
27
28
29
30
31
32
# File 'lib/bison/action.rb', line 26

def predecessor_tags
  tags = predecessors.each_with_index.map do |e, i|
    [i+1, e.tag] if (Bison::Nonterminal === e) && e.tag
  end.compact
  
  Hash[tags]
end

#to_bisonObject



16
17
18
19
20
21
22
23
24
# File 'lib/bison/action.rb', line 16

def to_bison
  code = "\n  {\n"
  code << %(    rb_ivar_set(__actions, rb_intern("@_"), rb_ary_new3(2, INT2FIX(@$.first_line), INT2FIX(@$.first_column)));\n)
  predecessor_tags.each do |i, name|
    code << %(    rb_ivar_set(__actions, rb_intern("@#{name}"), rb_ary_new3(2, INT2FIX(@#{i}.first_line), INT2FIX(@#{i}.first_column)));\n)
  end
  code << %(    $$ = #{funcall('__actions')};\n)
  code << "  }\n"
end