Class: Twig::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment) ⇒ Compiler

Returns a new instance of Compiler.

Parameters:



10
11
12
13
# File 'lib/twig/compiler.rb', line 10

def initialize(environment)
  @environment = environment
  @var_name_salt = 0
end

Instance Attribute Details

#environmentEnvironment (readonly)

Returns:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/twig/compiler.rb', line 6

class Compiler
  attr_reader :source, :environment

  # @param [Environment] environment
  def initialize(environment)
    @environment = environment
    @var_name_salt = 0
  end

  # @param [Node::Base] node
  # @param [Integer] indentation
  # @return [Compiler]
  def compile(node, indentation = 0)
    reset(indentation)
    node.compile(self)

    self
  end

  # @param [Node::Base] node
  # @param [Boolean] raw
  # @return [Compiler]
  def subcompile(node, raw: true)
    indent_source unless raw

    node.compile(self)

    self
  end

  # @param [Array<String>] strings
  # @return [Compiler]
  def write(*strings)
    strings.each do |string|
      indent_source
      @source << string
    end

    self
  end

  # @param [String] value
  # @return [Compiler]
  def string(value)
    @source << "%q[#{value}]"

    self
  end

  # @param [String] string
  # @return [Compiler]
  def raw(string)
    @source << string

    self
  end

  def repr(value)
    case value
    when Integer, Float
      raw(value.to_s)
    when TrueClass, FalseClass
      raw(value ? 'true' : 'false')
    when NilClass
      raw('nil')
    when Array, Hash
      raw('Marshal.load(').
        raw(Marshal.dump(value).inspect).
        raw(')')
    when Symbol
      raw(":#{value}")
    else
      string(value)
    end
  end

  # @param [Integer] step
  # @return [Compiler]
  def indent(step = 1)
    @indentation += step

    self
  end

  # @param [Integer] step
  # @return [Compiler]
  def outdent(step = 1)
    @indentation -= step

    self
  end

  # @return [String]
  def var_name
    "_v#{@var_name_salt}"
  end

  private

  def indent_source
    @source << ('  ' * @indentation)
  end

  # @return [Compiler]
  def reset(indentation = 0)
    @last_line = nil
    @source = +''
    @debug_info = []
    @source_offset = 0
    # source code starts at 1 (as we then increment it when we encounter new lines)
    @source_line = 1
    @indentation = indentation
    @var_name_salt = 0

    self
  end
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/twig/compiler.rb', line 7

def source
  @source
end

Instance Method Details

#compile(node, indentation = 0) ⇒ Compiler

Parameters:

  • node (Node::Base)
  • indentation (Integer) (defaults to: 0)

Returns:



18
19
20
21
22
23
# File 'lib/twig/compiler.rb', line 18

def compile(node, indentation = 0)
  reset(indentation)
  node.compile(self)

  self
end

#indent(step = 1) ⇒ Compiler

Parameters:

  • step (Integer) (defaults to: 1)

Returns:



84
85
86
87
88
# File 'lib/twig/compiler.rb', line 84

def indent(step = 1)
  @indentation += step

  self
end

#outdent(step = 1) ⇒ Compiler

Parameters:

  • step (Integer) (defaults to: 1)

Returns:



92
93
94
95
96
# File 'lib/twig/compiler.rb', line 92

def outdent(step = 1)
  @indentation -= step

  self
end

#raw(string) ⇒ Compiler

Parameters:

  • string (String)

Returns:



57
58
59
60
61
# File 'lib/twig/compiler.rb', line 57

def raw(string)
  @source << string

  self
end

#repr(value) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/twig/compiler.rb', line 63

def repr(value)
  case value
  when Integer, Float
    raw(value.to_s)
  when TrueClass, FalseClass
    raw(value ? 'true' : 'false')
  when NilClass
    raw('nil')
  when Array, Hash
    raw('Marshal.load(').
      raw(Marshal.dump(value).inspect).
      raw(')')
  when Symbol
    raw(":#{value}")
  else
    string(value)
  end
end

#string(value) ⇒ Compiler

Parameters:

  • value (String)

Returns:



49
50
51
52
53
# File 'lib/twig/compiler.rb', line 49

def string(value)
  @source << "%q[#{value}]"

  self
end

#subcompile(node, raw: true) ⇒ Compiler

Parameters:

  • node (Node::Base)
  • raw (Boolean) (defaults to: true)

Returns:



28
29
30
31
32
33
34
# File 'lib/twig/compiler.rb', line 28

def subcompile(node, raw: true)
  indent_source unless raw

  node.compile(self)

  self
end

#var_nameString

Returns:

  • (String)


99
100
101
# File 'lib/twig/compiler.rb', line 99

def var_name
  "_v#{@var_name_salt}"
end

#write(*strings) ⇒ Compiler

Parameters:

  • strings (Array<String>)

Returns:



38
39
40
41
42
43
44
45
# File 'lib/twig/compiler.rb', line 38

def write(*strings)
  strings.each do |string|
    indent_source
    @source << string
  end

  self
end