Module: Kumi::Frontends::Text

Defined in:
lib/kumi/frontends/text.rb

Class Method Summary collapse

Class Method Details

.load(path: nil, src: nil, inputs: {}) ⇒ Object

Load from a file path or a raw string. Usage:

Text.load(path: "schema.kumi", inputs: {...})
Text.load(src: "...raw text...", inputs: {...})

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kumi/frontends/text.rb', line 14

def load(path: nil, src: nil, inputs: {})
  raise ArgumentError, "provide either :path or :src" if (path.nil? && src.nil?) || (path && src)

  src ||= File.read(path)
  file_label = path || "schema"

  begin
    require "kumi-parser"
    ast = Kumi::Parser::TextParser.parse(src, source_file: file_label)
    Core::Analyzer::Debug.info(:parse, kind: :text, file: file_label, ok: true) if Core::Analyzer::Debug.enabled?
    [ast, inputs]
  rescue LoadError
    raise "kumi-parser gem not available. Install: gem install kumi-parser"
  rescue StandardError => e
    raise StandardError, SourceFrame.render(e, src: src, file_label: file_label)
  end
end