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



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/phlex/compiler/method_compiler.rb', line 244

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



365
366
367
368
369
370
371
372
373
374
375
# File 'lib/phlex/compiler/method_compiler.rb', line 365

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



308
309
310
311
312
# File 'lib/phlex/compiler/method_compiler.rb', line 308

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

	raw("<!doctype html>")
end

#compile_fragment_helper(node) ⇒ Object



324
325
326
327
328
329
330
# File 'lib/phlex/compiler/method_compiler.rb', line 324

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

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

#compile_fragment_helper_block(node) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/phlex/compiler/method_compiler.rb', line 332

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



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/phlex/compiler/method_compiler.rb', line 273

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



187
188
189
190
191
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 187

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



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
# File 'lib/phlex/compiler/method_compiler.rb', line 218

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



314
315
316
317
318
319
320
321
322
# File 'lib/phlex/compiler/method_compiler.rb', line 314

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



377
378
379
380
# File 'lib/phlex/compiler/method_compiler.rb', line 377

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

#compile_standard_element(node, tag) ⇒ Object

Like the element methods, the opening tag is closed even if serialising its attributes raises, and the closing tag is written even if the content raises, so output after a rescued error isn't read as part of an unclosed tag.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/phlex/compiler/method_compiler.rb', line 127

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

	attributes = (compile_phlex_attributes(node.arguments) if node.arguments)
	content = (compile_phlex_block(node.block) if node.block)

	if content
		Refract::StatementsNode.new(
			body: [
				raw("<#{tag}"),
				*opening_tag_attributes(attributes, ">"),
				ensuring([content], [raw("</#{tag}>")]),
			]
		)
	else
		Refract::StatementsNode.new(
			body: [
				raw("<#{tag}"),
				*opening_tag_attributes(attributes, "></#{tag}>"),
			]
		)
	end
end

#compile_void_element(node, tag) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/phlex/compiler/method_compiler.rb', line 151

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

	attributes = (compile_phlex_attributes(node.arguments) if node.arguments)

	Refract::StatementsNode.new(
		body: [
			raw("<#{tag}"),
			*opening_tag_attributes(attributes, ">"),
		]
	)
end

#compile_whitespace_helper(node) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/phlex/compiler/method_compiler.rb', line 292

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