Class: Liquor::Compiler
- Inherits:
-
Object
- Object
- Liquor::Compiler
- Defined in:
- lib/liquor/compiler.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#diagnostics ⇒ Object
readonly
Returns the value of attribute diagnostics.
-
#functions ⇒ Object
readonly
Returns the value of attribute functions.
-
#manager ⇒ Object
readonly
Returns the value of attribute manager.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
- #add_diagnostic(diagnostic) ⇒ Object
- #compile(ast, externals = [], template_name = nil) ⇒ Object
- #compile!(ast, externals = [], template_name = nil) ⇒ Object
- #errors ⇒ Object
- #function(name) ⇒ Object
- #has_function?(name) ⇒ Boolean
- #has_tag?(name) ⇒ Boolean
-
#initialize(options = {}) ⇒ Compiler
constructor
A new instance of Compiler.
- #register_function(function) ⇒ Object
- #register_tag(tag) ⇒ Object
- #success? ⇒ Boolean
- #tag(name) ⇒ Object
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(={}) @tags = {} @functions = {} @diagnostics = [] @code = nil = .dup import_builtins = .delete(:import_builtins) @manager = .delete(:manager) if .any? raise "Unknown compiler options #{.keys.join ", "}" end if import_builtins || import_builtins.nil? Builtins.export self Partials.export self end end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
4 5 6 |
# File 'lib/liquor/compiler.rb', line 4 def code @code end |
#diagnostics ⇒ Object (readonly)
Returns the value of attribute diagnostics.
4 5 6 |
# File 'lib/liquor/compiler.rb', line 4 def diagnostics @diagnostics end |
#functions ⇒ Object (readonly)
Returns the value of attribute functions.
5 6 7 |
# File 'lib/liquor/compiler.rb', line 5 def functions @functions end |
#manager ⇒ Object (readonly)
Returns the value of attribute manager.
3 4 5 |
# File 'lib/liquor/compiler.rb', line 3 def manager @manager end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
4 5 6 |
# File 'lib/liquor/compiler.rb', line 4 def source @source end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
5 6 7 |
# File 'lib/liquor/compiler.rb', line 5 def @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 |
#errors ⇒ Object
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
56 57 58 |
# File 'lib/liquor/compiler.rb', line 56 def has_function?(name) @functions.include? name end |
#has_tag?(name) ⇒ 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
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 |