Class: Liquor::Compiler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Compiler

Returns a new instance of Compiler.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/liquor/compiler.rb', line 7

def initialize(options={})
  @tags      = {}
  @functions = {}

  @diagnostics = []
  @code        = nil

  options = options.dup
  import_builtins = options.delete(:import_builtins)
  @manager        = options.delete(:manager)

  if options.any?
    raise "Unknown compiler options #{options.keys.join ", "}"
  end

  if import_builtins || import_builtins.nil?
    Builtins.export self
    Partials.export self
  end
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



4
5
6
# File 'lib/liquor/compiler.rb', line 4

def code
  @code
end

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



4
5
6
# File 'lib/liquor/compiler.rb', line 4

def diagnostics
  @diagnostics
end

#functionsObject (readonly)

Returns the value of attribute functions.



5
6
7
# File 'lib/liquor/compiler.rb', line 5

def functions
  @functions
end

#managerObject (readonly)

Returns the value of attribute manager.



3
4
5
# File 'lib/liquor/compiler.rb', line 3

def manager
  @manager
end

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/liquor/compiler.rb', line 4

def source
  @source
end

#tagsObject (readonly)

Returns the value of attribute tags.



5
6
7
# File 'lib/liquor/compiler.rb', line 5

def tags
  @tags
end

Instance Method Details

#add_diagnostic(diagnostic) ⇒ Object



102
103
104
# File 'lib/liquor/compiler.rb', line 102

def add_diagnostic(diagnostic)
  @diagnostics << diagnostic
end

#compile(ast, externals = [], template_name = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/liquor/compiler.rb', line 64

def compile(ast, externals=[], template_name=nil)
  @diagnostics.clear

  externals = externals.map(&:to_sym)
  context = Liquor::Context.new(self, externals)
  source  = context.emitter.compile_toplevel(ast)

  if success?
    if template_name
      source_identity = "(liquor:#{template_name})"
    else
      source_identity = "(liquor)"
    end

    @source = source
    @code   = eval(source, nil, source_identity)
  else
    @source = nil
    @code   = nil
  end

  success?
end

#compile!(ast, externals = [], template_name = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/liquor/compiler.rb', line 88

def compile!(ast, externals=[], template_name=nil)
  compile ast, externals, template_name

  if success?
    @code
  else
    raise diagnostics.first
  end
end

#errorsObject



98
99
100
# File 'lib/liquor/compiler.rb', line 98

def errors
  @diagnostics.select &:error?
end

#function(name) ⇒ Object



60
61
62
# File 'lib/liquor/compiler.rb', line 60

def function(name)
  @functions[name]
end

#has_function?(name) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/liquor/compiler.rb', line 56

def has_function?(name)
  @functions.include? name
end

#has_tag?(name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/liquor/compiler.rb', line 38

def has_tag?(name)
  @tags.include? name
end

#register_function(function) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/liquor/compiler.rb', line 46

def register_function(function)
  if @functions.include? function.name
    raise Exception, "attempt to register function #{function.name} twice"
  end

  @functions[function.name] = function

  self
end

#register_tag(tag) ⇒ Object



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

def register_tag(tag)
  if @tags.include? tag.name
    raise Exception, "attempt to register tag #{tag.name} twict"
  end

  @tags[tag.name] = tag

  self
end

#success?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/liquor/compiler.rb', line 106

def success?
  errors.empty?
end

#tag(name) ⇒ Object



42
43
44
# File 'lib/liquor/compiler.rb', line 42

def tag(name)
  @tags[name]
end