Class: Bison::Action
- Inherits:
-
Object
- Object
- Bison::Action
- Defined in:
- lib/bison/action.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#location ⇒ Object
Returns the value of attribute location.
-
#predecessors ⇒ Object
Returns the value of attribute predecessors.
-
#sequence ⇒ Object
Returns the value of attribute sequence.
Instance Method Summary collapse
- #errors ⇒ Object
-
#funcall(receiver) ⇒ Object
What to do about default $1/Qnil?.
-
#initialize(code) ⇒ Action
constructor
A new instance of Action.
- #name ⇒ Object
- #predecessor_tags ⇒ Object
- #to_bison ⇒ Object
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
#code ⇒ Object
Returns the value of attribute code.
7 8 9 |
# File 'lib/bison/action.rb', line 7 def code @code end |
#location ⇒ Object
Returns the value of attribute location.
8 9 10 |
# File 'lib/bison/action.rb', line 8 def location @location end |
#predecessors ⇒ Object
Returns the value of attribute predecessors.
9 10 11 |
# File 'lib/bison/action.rb', line 9 def predecessors @predecessors end |
#sequence ⇒ Object
Returns the value of attribute sequence.
10 11 12 |
# File 'lib/bison/action.rb', line 10 def sequence @sequence end |
Instance Method Details
#errors ⇒ Object
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 = .keys.map{ |i| "$#{i}" }.join(', ') args = args.empty? ? '0' : "#{.size}, #{args}" "rb_funcall(#{receiver}, #{method}, #{args})" end |
#name ⇒ Object
49 50 51 52 |
# File 'lib/bison/action.rb', line 49 def name base = "_#{sequence.index}_#{sequence.rule.name}" base << "_#{Digest::MD5.hexdigest(.inspect)}" end |
#predecessor_tags ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/bison/action.rb', line 26 def = predecessors.each_with_index.map do |e, i| [i+1, e.tag] if (Bison::Nonterminal === e) && e.tag end.compact Hash[] end |
#to_bison ⇒ Object
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) .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 |