lib/phlex/compiler/output.rb

Lines

91%

21 of 23 covered

Branches

100%

5 of 5 covered

Methods

50%

2 of 4 covered

Not run
29-30
Never called
1# frozen_string_literal: true
2
3# What a compiled method appends to the buffer, before it's lowered to Ruby.
4# MethodCompiler leaves these in the method's Refract tree where the output
5# goes, and the Emitter replaces each run of them with one guarded append.
61module Phlex::Compiler::Output
7 # Text known at compile time.
81 Literal = Data.define(:text)
9
10 # A conditional choosing between literal texts whose predicate can't raise,
11 # so it can't exit early.
121 Conditional = Data.define(:node)
13
14 # A Ruby expression that evaluates to the text to append, and may raise.
151 Expression = Data.define(:node)
16
17 # An attribute whose value was serialised into locals: a String's escaped
18 # text, appended between the prefix and suffix, or for any other value the
19 # whole text. Appended as statements, so the String needn't be copied.
201 Choice = Data.define(:escaped, :text, :prefix, :suffix)
21
22 # Statements that serialise attributes into locals, then the part to
23 # append, with the closing text appended instead if the statements exit
24 # early. The runtime closes an opening tag however serialisation exits.
251 Guarded = Data.define(:statements, :result, :closing)
26
27 # Stands in for a statement in the method's tree until it's emitted.
281 module Statement
290 def start_line = nil
300 def accept(visitor) = self
31 end
32
33 # Parts appended to the buffer in order.
341 Append = Data.define(:parts) do
351 include Statement
36
37138 def static? = parts.all? { |part| part in Literal | Conditional }
38 end
39
40 # Parts appended to a buffer read into a local, where rendering is already
41 # known, as an element method appends its opening tag.
421 Opening = Data.define(:parts, :buffer) do
431 include Statement
44 end
45
46 # Statements whose output is followed by the closing text however they
47 # exit, unless none of it can exit early.
481 Enclosed = Data.define(:body, :closing) do
491 include Statement
50 end
51
52 # Whether the statements can only append text that's known not to raise
53 # or jump, so they need no guard.
541 def self.static?(node)
55826 case node
56584 in Array then node.all? { |child| static?(child) }
57120 in Append then node.static?
58259 in Enclosed then static?(node.body)
5936 in Refract::StatementsNode then static?(node.body)
60116 else false
61 end
62 end
63end