Class: Phlex::Compiler::MethodCompiler

Inherits:
Refract::MutationVisitor
  • Object
show all
Defined in:
lib/phlex/compiler/method_compiler.rb

Constant Summary collapse

ALLOWED_OWNERS =

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ MethodCompiler

Returns a new instance of MethodCompiler.



7
8
9
10
11
12
# File 'lib/phlex/compiler/method_compiler.rb', line 7

def initialize(component)
	super()
	@component = component
	@preamble = []
	@optimized = false
end

Instance Method Details

#compile(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/phlex/compiler/method_compiler.rb', line 14

def compile(node)
	tree = visit(node)

	if @optimized
		Compactor.new.visit(
			tree
		)
	else
		nil
	end
end

#compile_block_body_node(node) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/phlex/compiler/method_compiler.rb', line 218

def compile_block_body_node(node)
	node => Refract::StatementsNode | Refract::BeginNode

	Refract::StatementsNode.new(
		body: [
			Refract::IfNode.new(
				inline: false,
				predicate: Refract::CallNode.new(
					receiver: Refract::SelfNode.new,
					name: :==,
					arguments: Refract::ArgumentsNode.new(
						arguments: [
							Refract::LocalVariableReadNode.new(
								name: self_local
							),
						]
					)
				),
				statements: Refract::StatementsNode.new(
					body: node.body.map { |n| visit(n) }
				),
				subsequent: Refract::ElseNode.new(
					statements: node
				)
			),
		]
	)
end

#compile_comment_helper(node) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
# File 'lib/phlex/compiler/method_compiler.rb', line 339

def compile_comment_helper(node)
	node => Refract::CallNode

	Refract::StatementsNode.new(
		body: [
			raw("<!-- "),
			compile_phlex_block(node.block),
			raw(" -->"),
		]
	)
end

#compile_doctype_helper(node) ⇒ Object



282
283
284
285
286
# File 'lib/phlex/compiler/method_compiler.rb', line 282

def compile_doctype_helper(node)
	node => Refract::CallNode

	raw("<!doctype html>")
end

#compile_fragment_helper(node) ⇒ Object



298
299
300
301
302
303
304
# File 'lib/phlex/compiler/method_compiler.rb', line 298

def compile_fragment_helper(node)
	node => Refract::CallNode

	node.copy(
		block: compile_fragment_helper_block(node.block)
	)
end

#compile_fragment_helper_block(node) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/phlex/compiler/method_compiler.rb', line 306

def compile_fragment_helper_block(node)
	node => Refract::BlockNode

	node.copy(
		body: Refract::StatementsNode.new(
			body: [
				Refract::LocalVariableWriteNode.new(
					name: :__phlex_original_should_render__,
					value: Refract::LocalVariableReadNode.new(
						name: should_render_local
					)
				),
				Refract::LocalVariableWriteNode.new(
					name: should_render_local,
					value: Refract::CallNode.new(
						receiver: Refract::LocalVariableReadNode.new(
							name: state_local
						),
						name: :should_render?
					)
				),
				visit(node.body),
				Refract::LocalVariableWriteNode.new(
					name: should_render_local,
					value: Refract::LocalVariableReadNode.new(
						name: :__phlex_original_should_render__
					)
				),
			]
		)
	)
end

#compile_interpolated_string_node(node) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/phlex/compiler/method_compiler.rb', line 247

def compile_interpolated_string_node(node)
	node => Refract::InterpolatedStringNode

	Refract::StatementsNode.new(
		body: node.parts.map do |part|
			case part
			when Refract::StringNode
				plain(part.unescaped)
			when Refract::EmbeddedVariableNode
				interpolate(part.variable)
			when Refract::EmbeddedStatementsNode
				interpolate(part.statements)
			else
				raise Phlex::Compiler::Error, "Unexpected node type in InterpolatedStringNode: #{part.class}"
			end
		end
	)
end

#compile_phlex_attributes(node) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/phlex/compiler/method_compiler.rb', line 161

def compile_phlex_attributes(node)
	arguments = node.arguments

	if arguments.size == 1 && Refract::KeywordHashNode === (first_argument = arguments[0])
		attributes = first_argument.elements
		literal_attributes = attributes.all? do |attribute|
			Refract::AssocNode === attribute && static_attribute_value_literal?(attribute)
		end

		if literal_attributes
			return raw(
				Phlex::SGML::Attributes.generate_attributes(
					eval(
						"{#{Refract::Formatter.new.format_node(node).source}}",
						TOPLEVEL_BINDING
					)
				)
			)
		end

		Refract::CallNode.new(
			name: :__render_attributes__,
			arguments: Refract::ArgumentsNode.new(
				arguments: [
					node,
				]
			)
		)
	end
end

#compile_phlex_block(node) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/phlex/compiler/method_compiler.rb', line 192

def compile_phlex_block(node)
	case node
	when Refract::BlockNode
		if output_block?(node)
			return visit(node.body)
		elsif static_content_block?(node)
			content = node.body.body.first
			case content
			when Refract::StringNode, Refract::SymbolNode
				return plain(content.unescaped)
			when Refract::InterpolatedStringNode
				return compile_interpolated_string_node(content)
			when Refract::NilNode
				return nil
			else
				raise
			end
		end
	end

	Refract::CallNode.new(
		name: :__yield_content__,
		block: node
	)
end

#compile_plain_helper(node) ⇒ Object



288
289
290
291
292
293
294
295
296
# File 'lib/phlex/compiler/method_compiler.rb', line 288

def compile_plain_helper(node)
	node => Refract::CallNode

	if node.arguments.arguments in [Refract::StringNode]
		raw(node.arguments.arguments.first.unescaped)
	else
		node
	end
end

#compile_raw_helper(node) ⇒ Object



351
352
353
354
# File 'lib/phlex/compiler/method_compiler.rb', line 351

def compile_raw_helper(node)
	node => Refract::CallNode
	node
end

#compile_standard_element(node, tag) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/phlex/compiler/method_compiler.rb', line 123

def compile_standard_element(node, tag)
	node => Refract::CallNode

	Refract::StatementsNode.new(
		body: [
			raw("<#{tag}"),
			*(
				if node.arguments
					compile_phlex_attributes(node.arguments)
				end
			),
			raw(">"),
			*(
				if node.block
					compile_phlex_block(node.block)
				end
			),
			raw("</#{tag}>"),
		]
	)
end

#compile_void_element(node, tag) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/phlex/compiler/method_compiler.rb', line 145

def compile_void_element(node, tag)
	node => Refract::CallNode

	Refract::StatementsNode.new(
		body: [
			raw("<#{tag}"),
			*(
				if node.arguments
					compile_phlex_attributes(node.arguments)
				end
			),
			raw(">"),
		]
	)
end

#compile_whitespace_helper(node) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/phlex/compiler/method_compiler.rb', line 266

def compile_whitespace_helper(node)
	node => Refract::CallNode

	if node.block
		Refract::StatementsNode.new(
			body: [
				raw(" "),
				compile_phlex_block(node.block),
				raw(" "),
			]
		)
	else
		raw(" ")
	end
end