Class: Koto::Parser::AST::Processor

Inherits:
Parser::AST::Processor
  • Object
show all
Defined in:
lib/koto.rb,
lib/koto/parser/ast/processor.rb,
lib/koto/parser/ast/processor/context.rb,
lib/koto/parser/ast/processor/name_processor.rb

Defined Under Namespace

Classes: Context, NameProcessor

Constant Summary collapse

OBJECT_METHODS =
[
  *(Object.methods - [:class, :hash, :send])
]
KERNEL_METHODS =
[
  *(Kernel.methods - OBJECT_METHODS)
]
ACCESS_METHODS =
[
  :private,
  :protected,
  :public,
]
MODULE_METHODS =
[
  *(Module.methods - OBJECT_METHODS)
]
PREDEFINED_METHODS =
[
  *OBJECT_METHODS,
  *KERNEL_METHODS,
  *ACCESS_METHODS,
  *MODULE_METHODS
]
BUILTIN_TYPES =
[
  *Object.constants
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



39
40
41
# File 'lib/koto/parser/ast/processor.rb', line 39

def initialize
  @context = Context.new
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



37
38
39
# File 'lib/koto/parser/ast/processor.rb', line 37

def context
  @context
end

Instance Method Details

#name_processorObject



47
48
49
# File 'lib/koto/parser/ast/processor.rb', line 47

def name_processor
  @name_processor ||= NameProcessor.new
end

#on_access(node) ⇒ Object Also known as: on_private, on_protected, on_public



203
204
205
206
207
208
# File 'lib/koto/parser/ast/processor.rb', line 203

def on_access(node)
  access          = node.type
  current_context = context.switch_access_to access

  switch_context_to current_context
end

#on_class(node) ⇒ Object

Methods for processing nodes



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/koto/parser/ast/processor.rb', line 57

def on_class(node)
  name, superclass, body = *node

  name       = name_processor.process(name)
  superclass = process superclass

  node =
    node.updated nil,
    [name, superclass, body],
    :name => name

  access          = context.access
  current_context = context.get_in(node)

  switch_context_to current_context

  node =
    node.updated nil,
    [name, superclass, process(body)],
    :name => name

  symbols, current_context = context.get_out

  current_context =
    current_context.switch_access_to access

  node =
    node.updated :casgn,
    [nil, name, node],
    :name => name, :context => current_context, :symbols => symbols

  current_context = current_context.save(node)

  switch_context_to current_context

  node
end

#on_const(node) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/koto/parser/ast/processor.rb', line 169

def on_const(node)
  scope, name = *node

  if scope
    name = name_processor.process(node)
  end

  node.updated nil,
    [nil, name],
    :name => name
end

#on_def(node) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/koto/parser/ast/processor.rb', line 132

def on_def(node)
  name, args, body = *node

  args = process_all(args)

  node =
    node.updated nil,
    [name, *args, body],
    :name => name

  access          = context.access
  current_context = context.get_in(node)

  switch_context_to current_context

  node =
    node.updated nil,
    [name, *args, (body = process(body))],
    :name => name

  symbols, current_context = context.get_out

  current_context =
    current_context.switch_access_to access

  node =
    node.updated nil,
    [name, *args, body],
    :name => name, :context => current_context, :symbols => symbols

  current_context = current_context.save(node)

  switch_context_to current_context

  node
end

#on_module(node) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/koto/parser/ast/processor.rb', line 95

def on_module(node)
  name, body = *node

  name = name_processor.process(name)

  node =
    node.updated nil,
    [name, body],
    :name => name

  access          = context.access
  current_context = context.get_in(node)

  switch_context_to current_context

  node =
    node.updated nil,
    [name, process(body)],
    :name => name

  symbols, current_context = context.get_out

  current_context =
    current_context.switch_access_to access

  node =
    node.updated :casgn,
    [nil, name, node],
    :name => name, :context => current_context, :symbols => symbols

  current_context = current_context.save(node)

  switch_context_to current_context

  node
end

#on_send(node) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/koto/parser/ast/processor.rb', line 181

def on_send(node)
  _, name, _ = *node

  # Invokes specific handlers for predefined methods
  if PREDEFINED_METHODS.include? name
    on_method = :"on_#{name}"

    if respond_to? on_method
      node = node.updated :"#{name}", node.children
      return send on_method, node
    else
      return handler_missing(node)
    end
  end

  node.updated nil,
    [nil, name],
    :name => name

  super
end

#required_filesObject



51
52
53
# File 'lib/koto/parser/ast/processor.rb', line 51

def required_files
  @required_files ||= []
end

#switch_context_to(context) ⇒ Object



43
44
45
# File 'lib/koto/parser/ast/processor.rb', line 43

def switch_context_to(context)
  @context = context
end