lib/phlex/compiler/emitter.rb

Lines

98%

59 of 60 covered

Branches

90%

20 of 22 covered

Methods

100%

11 of 11 covered

Not run
45
1# frozen_string_literal: true
2
3# Lowers the output nodes MethodCompiler left in a method's tree to Ruby.
4# Each run of appends becomes one chain of `<<` on the buffer behind the
5# render check, with adjacent literals fused, and each enclosure becomes an
6# `ensure` that appends its closing text if the body exits early.
71class Phlex::Compiler::Emitter < Refract::MutationVisitor
81 include Phlex::Compiler::Builder
9
101 Output = Phlex::Compiler::Output
11
121 def initialize(locals)
13157 super()
14157 @locals = locals
15 end
16
171 visit Refract::StatementsNode do |node|
181682 body = []
19
201682 flatten(node.body).each do |child|
212764 case child
222764 in Output::Append if Output::Append === body.last
23280 body[-1] = Output::Append.new(parts: body.last.parts + child.parts)
24 in Output::Append
25345 body << child
26 in Output::Enclosed
27104 body.concat(enclose(child))
28 in Output::Opening
292 body.concat(appends(child.parts, read(child.buffer)))
30 else
312033 body << visit(child)
32 end
33 end
34
354270 node.copy(body: body.compact.map { |statement| (Output::Append === statement) ? append(statement.parts) : statement })
36 end
37
38 # Splices nested statements and enclosures that can't exit early into the
39 # list. ASCII text at either end of an enclosure's body moves out of it, to
40 # fuse with the text around it, since appending it can't fail or jump. An
41 # enclosure whose body is then just another enclosure closes both at once.
421 private def flatten(statements)
432453 statements.flat_map do |statement|
443695 case statement
450 in nil then []in never taken
46529 in Refract::StatementsNode then flatten(statement.body)
47126 in Output::Enclosed if Output.static?(statement) then flatten(statement.body)
48 in Output::Enclosed
49116 body = flatten(statement.body)
50248 leading = body.take_while { |child| ascii_text?(child) }
51116 rest = body.drop(leading.length)
52250 trailing = rest.reverse.take_while { |child| ascii_text?(child) }.reverse
53116 body = rest.take(rest.length - trailing.length)
54
55116 enclosed = case body
56 in [Output::Enclosed => inner] if statement.closing.ascii_only? && inner.closing.ascii_only?
577 inner.with(closing: inner.closing + statement.closing)
58 else
59109 statement.with(body:)
60 end
61
62116 [*leading, enclosed, *trailing]
632924 else [statement]
64 end
65 end
66 end
67
681 private def ascii_text?(statement)
69320 Output::Append === statement && statement.parts.all? { |part| Output::Literal === part && part.text.ascii_only? }
70 end
71
721 private def enclose(node)
73104 done = @locals.fresh(:done)
74
75 [
76104 write(done, Refract::FalseNode.new),
77 begin_node(
78 [*visit(statements(node.body)).body, write(done, Refract::TrueNode.new)],
79 ensure_clause: ensure_node([unless_node(read(done), [append([Output::Literal.new(node.closing)])])])
80 ),
81 ]
82 end
83
84 # Each part is appended in its own `<<` so that if one raises, everything
85 # before it has already reached the buffer, as at runtime. The whole
86 # statement evaluates to nil, like the element methods it replaces.
871 private def append(parts)
88449 parenthesized([if_node(should_render, appends(parts, buffer)), Refract::NilNode.new])
89 end
90
91 # One chain for each run of parts, and a choice of appends for each Choice.
921 private def appends(parts, receiver)
931104 parts.slice_when { |a, b| Output::Choice === a || Output::Choice === b }.map do |run|
94509 case run
95 in [Output::Choice => choice]
9629 if_node(
97 read(choice.escaped),
98 [chain([Output::Literal.new(choice.prefix), Output::Expression.new(read(choice.escaped)), Output::Literal.new(choice.suffix)], receiver)],
99 else_body: [call(receiver, :<<, read(choice.text))]
100 )
101 else
102480 chain(run, receiver)
103 end
104 end
105 end
106
1071 private def chain(parts, receiver)
1081154 fuse(parts).reduce(receiver) { |chained, part| call(chained, :<<, lower(part, receiver)) }
109 end
110
1111 private def fuse(parts)
112509 parts.each_with_object([]) do |part, fused|
1131162 if Output::Literal === part && Output::Literal === fused.last
114517 fused[-1] = Output::Literal.new(fused.last.text + part.text)
115 else
116645 fused << part
117 end
118 end
119 end
120
1211 private def lower(part, receiver)
122690 case partelse never taken
123581 in Output::Literal then string(part.text)
12464 in Output::Conditional | Output::Expression then part.node
12545 in Output::Guarded then guard(part, receiver)
126 end
127 end
128
129 # The guard is an expression inside the append chain, so the opening tag's
130 # text has already been appended when the closing text is.
1311 private def guard(part, receiver)
13245 done = @locals.fresh(:done)
133
13445 begin_node(
135 [write(done, Refract::FalseNode.new), *part.statements, write(done, Refract::TrueNode.new), lower(part.result, receiver)],
136 ensure_clause: ensure_node([unless_node(read(done), [call(receiver, :<<, string(part.closing))], inline: true)])
137 )
138 end
139end