Class: Liquor::Tag

Inherits:
Object
  • Object
show all
Includes:
ASTTools
Defined in:
lib/liquor/tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Tag

Returns a new instance of Tag


8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/liquor/tag.rb', line 8

def initialize(name, options={}, &block)
  @name = name.to_s
  @body = block

  options = options.dup
  @continuations = (options.delete(:continuations) || []).map(&:to_s)

  if @body.nil?
    raise "Cannot define a tag without body"
  elsif options.any?
    raise "Unknown tag options: #{options.keys.join ", "}"
  end
end

Instance Attribute Details

#continuationsObject (readonly)

Returns the value of attribute continuations


6
7
8
# File 'lib/liquor/tag.rb', line 6

def continuations
  @continuations
end

#nameObject (readonly)

Returns the value of attribute name


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

def name
  @name
end

Instance Method Details

#check_arg_type(node, type) ⇒ Object


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
# File 'lib/liquor/tag.rb', line 57

def check_arg_type(node, type)
  expected = nil

  case ntype(node)
  when :kwarg
    actual = ntype(kwvalue(node))
  when :blockarg
    actual = :block
  else
    actual = ntype(node)
  end

  type = type.compact.first if type.is_a?(Array)

  case type
  when :ident, :string
    expected = type
    failed = (actual != expected)

  when :expr
    expected = :expression
    failed = (actual == :block)

  when :block
    expected = :block
    failed = (actual != expected)

  else
    raise "Unknown node type #{type}"
  end

  if failed
    actual   = Parser::TOKEN_NAME_MAP[actual]   || actual
    expected = Parser::TOKEN_NAME_MAP[expected] || expected
    raise SyntaxError.new("unexpected `#{actual}', expecting `#{expected}'", nloc(node))
  end
end

#check_args(node, arg_type, kwarg_types = {}) ⇒ Object


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
# File 'lib/liquor/tag.rb', line 26

def check_args(node, arg_type, kwarg_types={})
  name, arg, *kwargs = nvalue(node)
  if arg_type.nil? && !arg.nil?
    raise SyntaxError.new("extraneous argument", nloc(arg))
  elsif arg.nil?
    if !arg_type.nil? && !(arg_type.is_a?(Array) && arg_type.include?(nil))
      raise SyntaxError.new("missing unnamed argument", nloc(node))
    end
  end

  if !arg_type.nil? && !arg.nil?
    check_arg_type arg, arg_type
  end

  kwarg_hash = {}

  kwarg_types.zip(kwargs).each do |(name, type), kwarg|
    if kwarg.nil?
      raise SyntaxError.new("unexpected `%}', expecting `#{name}:'", nloc(node))
    elsif kwname(kwarg) != name.to_s
      raise SyntaxError.new("unexpected `#{kwname(kwarg)}:', expecting `#{name}:'", nloc(kwarg))
    end

    check_arg_type(kwarg, type)

    kwarg_hash[name] = kwvalue(kwarg)
  end

  [ arg, kwarg_hash ]
end

#compile(emit, node) ⇒ Object


22
23
24
# File 'lib/liquor/tag.rb', line 22

def compile(emit, node)
  instance_exec emit, emit.context, node, &@body
end