Class: Take::Unit::Compiler

Inherits:
Object
  • Object
show all
Includes:
PredictMatch
Defined in:
lib/take/unit/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PredictMatch

#match, #peek, #predict

Constructor Details

#initialize(file, tokens) ⇒ Compiler

Returns a new instance of Compiler.



9
10
11
12
13
14
# File 'lib/take/unit/compiler.rb', line 9

def initialize(file, tokens)
  @file   = file
  @tokens = tokens
  @enum   = tokens.each
  @parent = nil
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#compileObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/take/unit/compiler.rb', line 16

def compile
  @parent ||= begin
    @parent = AST::Parent.new(name: File.basename(@file).
      gsub(/\..*\z/, ""))
    while peek do
      part = predict(:group => :group, :prefix => :prefix)
      @parent.children << part if part
    end

    add_parent_prefix

    @parent.source = sourcify([0, 0])
    @parent
  end
end

#compile_afterObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/take/unit/compiler.rb', line 77

def compile_after
  after = match(:after)
  body = match(:block)

  AST::After.new(name: after[1],
                 children: [AST::Block.new({
                   body: body[1],
                   source: sourcify(body)
                   })],
                 source: sourcify(after))
end

#compile_beforeObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/take/unit/compiler.rb', line 65

def compile_before
  before = match(:before)
  body = match(:block)

  AST::Before.new(name: before[1],
                  children: [AST::Block.new({
                    body: body[1],
                    source: sourcify(body)
                    })],
                  source: sourcify(before))
end

#compile_groupObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/take/unit/compiler.rb', line 32

def compile_group
  group = match(:group)
  node = AST::Group.new(name: group[1], source: sourcify(group))

  until peek == :group_end
    part = compile_part
    node.children << part if part
  end

  match(:group_end)

  node
end

#compile_macroObject



104
105
106
# File 'lib/take/unit/compiler.rb', line 104

def compile_macro

end

#compile_partObject



46
47
48
49
50
51
52
# File 'lib/take/unit/compiler.rb', line 46

def compile_part
  predict :group  => :group,
          :test   => :test,
          :before => :before,
          :after  => :after,
          :macro  => :macro
end

#compile_prefixObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/take/unit/compiler.rb', line 89

def compile_prefix
  prefix = match(:prefix)
  body = match(:block)

  @parent.children << AST::Prefix.new(
    name: prefix[1],
    children: [AST::Block.new({
      body: body[1],
      source: sourcify(body)
    })],
    source: sourcify(prefix))

  nil
end

#compile_testObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/take/unit/compiler.rb', line 54

def compile_test
  test = match(:test)
  body = match(:block)
  AST::Test.new(name: test[1],
                children: [AST::Block.new({
                  body: body[1],
                  source: sourcify(body)
                })],
                source: sourcify(test))
end