class Take::Unit::Compiler

Attributes

parent[R]

Public Class Methods

new(file, tokens) click to toggle source
# File lib/take/unit/compiler.rb, line 9
def initialize(file, tokens)
  @file   = file
  @tokens = tokens
  @enum   = tokens.each
  @parent = nil
end

Public Instance Methods

compile() click to toggle source
# 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_after() click to toggle source
# 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_before() click to toggle source
# 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_group() click to toggle source
# 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_macro() click to toggle source
# File lib/take/unit/compiler.rb, line 104
def compile_macro

end
compile_part() click to toggle source
# File lib/take/unit/compiler.rb, line 46
def compile_part
  predict :group  => :group,
          :test   => :test,
          :before => :before,
          :after  => :after,
          :macro  => :macro
end
compile_prefix() click to toggle source
# 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_test() click to toggle source
# 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

Private Instance Methods

add_parent_prefix() click to toggle source
# File lib/take/unit/compiler.rb, line 119
      def add_parent_prefix
        prefix = AST::Prefix.new(name: "_generated_prefix")

        prefix.children << AST::Block.new(body: <<-DOC)

#define __FILE \"#{@file}\"
#include \"utest.h\"
_uassert_prefix();
DOC
        @parent.children << prefix
      end
input() click to toggle source
# File lib/take/unit/compiler.rb, line 110
def input
  @enum
end
sourcify(token) click to toggle source
# File lib/take/unit/compiler.rb, line 114
def sourcify(token)
  hash = Hash[[:line, :column].zip(token[-2..-1])]
  hash.merge(file: @file)
end