Class: Walrat::Node

Inherits:
Object
  • Object
show all
Includes:
LocationTracking
Defined in:
lib/walrat/node.rb

Overview

Make subclasses of this for us in Abstract Syntax Trees (ASTs).

Instance Attribute Summary collapse

Attributes included from LocationTracking

#outer_end, #outer_source_text, #outer_start, #source_text

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LocationTracking

#column_end, #column_end=, #column_start, #column_start=, #end, #end=, #line_end, #line_end=, #line_start, #line_start=, #rightmost?, #start, #start=

Constructor Details

#initialize(lexeme) ⇒ Node

Returns a new instance of Node.



32
33
34
35
# File 'lib/walrat/node.rb', line 32

def initialize lexeme
  @string_value = lexeme.to_s
  @lexeme = lexeme
end

Instance Attribute Details

#lexemeObject (readonly)

Returns the value of attribute lexeme.



30
31
32
# File 'lib/walrat/node.rb', line 30

def lexeme
  @lexeme
end

Class Method Details

.production(*results) ⇒ Object

Overrides the default initialize method to accept the defined attributes and sets up an read accessor for each.

Raises an error if called directly on Node itself rather than a subclass.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/walrat/node.rb', line 46

def self.production *results
  raise 'Node#production called directly on Node' if self == Node

  # set up accessors
  results.each { |result| attr_reader result }

  # set up initializer
  initialize_body = "def initialize #{results.map { |symbol| symbol.to_s}.join(', ')}\n"
  initialize_body << %Q{  @string_value = ""\n}
  results.each do |result|
    initialize_body << "  @#{result} = #{result}\n"
    initialize_body << "  @string_value << #{result}.to_s\n"
  end
  initialize_body << "end\n"
  class_eval initialize_body
end

Instance Method Details

#to_sObject



37
38
39
# File 'lib/walrat/node.rb', line 37

def to_s
  @string_value
end