Class: Ruby::Program

Inherits:
Statements show all
Defined in:
lib/ruby/statements.rb

Instance Attribute Summary collapse

Attributes included from Node::Composite

#parent

Instance Method Summary collapse

Methods included from Conversions::Statements

#to_block, #to_chained_block, #to_named_block, #to_program

Methods inherited from List

#method_missing

Methods included from Alternation::List

#<<, #[]=, #pop

Methods included from Conversions::List

#to_array

Methods inherited from Aggregate

#position, #position=, #prolog, #prolog=

Methods inherited from Node

#<=>, #all_nodes, #column, #length, #row

Methods included from Conversions

included, #to_node

Methods included from Node::Traversal

#has_token?, #has_type?, #has_value?, #is_instance_of?, #left_of?, #matches?, #position?, #right_of?, #select

Methods included from Node::Source

#context, #line, #lines, #src

Methods included from Node::Composite

included, #root, #root?

Constructor Details

#initialize(src, filename, statements = [], end_data = nil) ⇒ Program

Returns a new instance of Program.



13
14
15
16
17
18
# File 'lib/ruby/statements.rb', line 13

def initialize(src, filename, statements = [], end_data = nil)
  self.src = src
  self.filename = filename if filename
  self.end_data = end_data if end_data
  super(statements)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ruby::List

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



10
11
12
# File 'lib/ruby/statements.rb', line 10

def filename
  @filename
end

#src=(value) ⇒ Object (writeonly)

Sets the attribute src

Parameters:

  • value

    the value to set the attribute src to.



11
12
13
# File 'lib/ruby/statements.rb', line 11

def src=(value)
  @src = value
end

Instance Method Details

#line_pos(row) ⇒ Object

TODO replace this with Clip?



31
32
33
# File 'lib/ruby/statements.rb', line 31

def line_pos(row)
  (row > 0 ? src.split("\n")[0..(row - 1)].inject(0) { |pos, line| pos + line.length + 1 } : 0)
end

#nodesObject



26
27
28
# File 'lib/ruby/statements.rb', line 26

def nodes
  [super, end_data].flatten.compact
end

#replace_src(row, column, length, src) ⇒ Object



35
36
37
38
39
# File 'lib/ruby/statements.rb', line 35

def replace_src(row, column, length, src)
  self.src[line_pos(row) + column, length] = src 
  offset_column = src.length - length
  update_positions(row, column + length, offset_column)
end

#save_srcObject



41
42
43
# File 'lib/ruby/statements.rb', line 41

def save_src
  File.open(filename, 'w+') { |f| f.write(src) }
end

#to_ruby(prolog = false) ⇒ Object



22
23
24
# File 'lib/ruby/statements.rb', line 22

def to_ruby(prolog = false)
  super(true)
end