Module: Wcl

Defined in:
lib/wcl.rb,
lib/wcl/types.rb,
lib/wcl/convert.rb,
lib/wcl/version.rb,
lib/wcl/callback.rb,
lib/wcl/document.rb,
lib/wcl/wasm_runtime.rb

Defined Under Namespace

Modules: Callback, Convert Classes: BlockRef, Decorator, Diagnostic, Document, ValueError, WasmRuntime

Constant Summary collapse

VERSION =
"0.8.0.alpha1"

Class Method Summary collapse

Class Method Details

.parse(source, root_dir: nil, allow_imports: nil, max_import_depth: nil, max_macro_depth: nil, max_loop_depth: nil, max_iterations: nil, functions: nil, variables: nil) ⇒ Object

Parse a WCL source string and return a Document.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wcl.rb', line 13

def parse(source, root_dir: nil, allow_imports: nil, max_import_depth: nil,
          max_macro_depth: nil, max_loop_depth: nil, max_iterations: nil,
          functions: nil, variables: nil)
  options = {}
  options["rootDir"] = root_dir.to_s if root_dir
  options["allowImports"] = allow_imports unless allow_imports.nil?
  options["maxImportDepth"] = max_import_depth if max_import_depth
  options["maxMacroDepth"] = max_macro_depth if max_macro_depth
  options["maxLoopDepth"] = max_loop_depth if max_loop_depth
  options["maxIterations"] = max_iterations if max_iterations
  options["variables"] = variables if variables

  options_json = options.empty? ? nil : JSON.generate(options)
  runtime = WasmRuntime.get

  if functions && !functions.empty?
    Callback.set_functions(functions)
    begin
      func_names_json = JSON.generate(functions.keys)
      handle = runtime.parse_with_functions(source, options_json, func_names_json)
    ensure
      Callback.clear_functions
    end
  else
    handle = runtime.parse(source, options_json)
  end

  Document.new(handle)
end

.parse_file(path, **kwargs) ⇒ Object

Parse a WCL file and return a Document.



44
45
46
47
48
49
50
51
52
# File 'lib/wcl.rb', line 44

def parse_file(path, **kwargs)
  path = path.to_s
  source = File.read(path)
rescue Errno::ENOENT, Errno::EACCES => e
  raise IOError, "#{path}: #{e.message}"
else
  kwargs[:root_dir] ||= File.dirname(File.expand_path(path))
  parse(source, **kwargs)
end