Module: LibBEL

Defined in:
lib/bel/libbel.rb,
lib/bel/libbel/bel_token.rb,
lib/bel/libbel/node_test.rb,
lib/bel/libbel/bel_token_list.rb,
lib/bel/libbel/node_traversal.rb,
lib/bel/libbel/bel_ast_structs.rb,
lib/bel/libbel/bel_token_iterator.rb,
lib/bel/libbel/node_transformation.rb

Defined Under Namespace

Modules: NodeTest, NodeTransformation, NodeTraversal Classes: AstNodeTypeUnion, BelAst, BelAstNode, BelAstNodeToken, BelAstNodeTypeInfo, BelAstNodeValue, BelToken, BelTokenIterator, BelTokenList

Constant Summary collapse

FFI =

Constant holding the FFI module for this ruby engine.

LibBEL::load_ffi

Class Method Summary collapse

Class Method Details

.copy_ast(bel_ast) ⇒ Object



177
178
179
180
181
182
# File 'lib/bel/libbel.rb', line 177

def self.copy_ast(bel_ast)
  copy_root = self.copy_ast_node(bel_ast.root)
  new_ast   = LibBEL::BelAst.new(LibBEL::bel_new_ast())
  new_ast.root = copy_root
  new_ast
end

.copy_ast_node(bel_ast_node) ⇒ Object



184
185
186
# File 'lib/bel/libbel.rb', line 184

def self.copy_ast_node(bel_ast_node)
  LibBEL::BelAstNode.new(LibBEL::bel_copy_ast_node(bel_ast_node))
end

.extension_path(ruby_version = RUBY_VERSION) ⇒ Object

Determines the correct extension path for your ruby version



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bel/libbel.rb', line 11

def extension_path(ruby_version = RUBY_VERSION)
  version =
    case ruby_version
    when /^2\.0/
      '2.0'
    when /^2\.1/
      '2.1'
    when /^2\.2/
      '2.2'
    else
      fail RuntimeError.new("Do not support Ruby #{RUBY_VERSION}.")
    end

  File.join(File.expand_path(File.dirname(__FILE__)), version)
end

.find_ffiObject

Determine FFI constant for this ruby engine.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bel/libbel.rb', line 29

def find_ffi
  if rubinius?
    # Use ffi gem instead of rubinius-bundled FFI
    # Rubinius 2.5.2 does not seem to support FFI::ManagedStruct
    require "ffi"
    ::FFI
  else # mri, jruby, etc
    require "ffi"
    ::FFI
  end
end

.load_ffiObject

Extend with the correct ffi implementation.



43
44
45
46
47
# File 'lib/bel/libbel.rb', line 43

def load_ffi
  ffi_module = LibBEL::find_ffi
  extend ffi_module::Library
  ffi_module
end

.load_libBELObject

Loads the libbel shared library.



51
52
53
54
55
56
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
# File 'lib/bel/libbel.rb', line 51

def load_libBEL
  ffi_module = find_ffi
  extend ffi_module::Library

  # libbel.so:     Linux and MinGW
  # libbel.bundle: Mac OSX
  messages = []
  library_loaded = ['libbel.so', 'libbel.bundle'].map { |library_file|
    [
      library_file,
      File.join(
        File.expand_path(File.dirname(__FILE__)),
        library_file
      )
    ]
  }.any? do |library_file, library_path|
    # Try loading top-level shared library.
    begin
      ffi_lib library_path
      true
    rescue LoadError => exception1
      # Try loading version-specific shared library.
      begin
        ffi_lib File.join(extension_path, library_file)
        true
      rescue LoadError => exception2
        messages << exception1.to_s
        messages << exception2.to_s
        false
      end
    end
  end

  if !library_loaded
    msg = messages.map { |msg| "  #{msg}" }.join("\n")
    fail LoadError.new("Failed to load libbel library. Details:\n#{msg}")
  end
end

.parse_statement(bel_string) ⇒ Object



165
166
167
# File 'lib/bel/libbel.rb', line 165

def self.parse_statement(bel_string)
  LibBEL::BelAst.new(LibBEL::bel_parse_statement(bel_string))
end

.parse_term(bel_string) ⇒ Object



169
170
171
# File 'lib/bel/libbel.rb', line 169

def self.parse_term(bel_string)
  LibBEL::BelAst.new(LibBEL::bel_parse_term(bel_string))
end


188
189
190
# File 'lib/bel/libbel.rb', line 188

def self.print_ast(bel_ast)
  LibBEL::bel_print_ast(bel_ast)
end


192
193
194
# File 'lib/bel/libbel.rb', line 192

def self.print_token(token)
  self.bel_print_token(token.pointer)
end


196
197
198
# File 'lib/bel/libbel.rb', line 196

def self.print_token_list(list)
  self.bel_print_token_list(list.pointer)
end

.rubinius?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/bel/libbel.rb', line 5

def rubinius?
  defined?(RUBY_ENGINE) && ("rbx" == RUBY_ENGINE)
end

.tokenize_term(bel_string) ⇒ Object



173
174
175
# File 'lib/bel/libbel.rb', line 173

def self.tokenize_term(bel_string)
  LibBEL::BelTokenList.new(self.bel_tokenize_term(bel_string))
end