Class: P2::TagTranslator

Inherits:
Prism::MutationCompiler
  • Object
show all
Includes:
Prism::DSL
Defined in:
lib/p2/compiler/tag_translator.rb

Overview

Translates a normal proc AST into an AST containing custom nodes used for generating HTML. This translation is the first step in compiling templates into procs that generate HTML.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ TagTranslator

Returns a new instance of TagTranslator.



13
14
15
16
# File 'lib/p2/compiler/tag_translator.rb', line 13

def initialize(root)
  @root = root
  super()
end

Class Method Details

.transform(ast, root) ⇒ Object



18
19
20
# File 'lib/p2/compiler/tag_translator.rb', line 18

def self.transform(ast, root)
  ast.accept(new(root))
end

Instance Method Details

#match_block_call(node) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/p2/compiler/tag_translator.rb', line 72

def match_block_call(node)
  return if !node.receiver
  return if node.name != :call

  receiver = node.receiver
  return if !receiver.is_a?(Prism::LocalVariableReadNode)
  return if @root.parameters&.parameters.block&.name != receiver.name

  if node.block
    raise P2::Error, 'No support for proc invocation with block'
  end

  BlockInvocationNode.new(node, self)
end

#match_builtin(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/p2/compiler/tag_translator.rb', line 33

def match_builtin(node)
  return if node.receiver

  case node.name
  when :render_yield
    RenderYieldNode.new(node, self)
  when :render_children
    RenderChildrenNode.new(node, self)
  when :raise
    visit_call_node(node, dont_translate: true)
  when :render
    RenderNode.new(node, self)
  when :raw
    RawNode.new(node, self)
  when :text
    TextNode.new(node, self)
  when :defer
    DeferNode.new(node, self)
  when :html5, :markdown
    BuiltinNode.new(node, self)
  else
    nil
  end
end

#match_const_tag(node) ⇒ Object



65
66
67
68
69
70
# File 'lib/p2/compiler/tag_translator.rb', line 65

def match_const_tag(node)
  return if node.receiver
  return if node.name !~ /^[A-Z]/

  ConstTagNode.new(node, self)
end

#match_extension(node) ⇒ Object



58
59
60
61
62
63
# File 'lib/p2/compiler/tag_translator.rb', line 58

def match_extension(node)
  return if node.receiver
  return if !P2::Extensions[node.name]

  ExtensionTagNode.new(node, self)
end

#match_tag(node) ⇒ Object



87
88
89
90
91
# File 'lib/p2/compiler/tag_translator.rb', line 87

def match_tag(node)
  return if node.receiver

  TagNode.new(node, self)
end

#visit_call_node(node, dont_translate: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/p2/compiler/tag_translator.rb', line 22

def visit_call_node(node, dont_translate: false)
  return super(node) if dont_translate

  match_builtin(node) ||
  match_extension(node) ||
  match_const_tag(node) ||
  match_block_call(node) ||
  match_tag(node) ||
  super(node)
end