Method: SlideField::Interpreter#run_string

Defined in:
lib/slidefield/interpreter.rb

#run_string(input, include_path = '.', context = 'input', parent_obj = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/slidefield/interpreter.rb', line 33

def run_string(input, include_path = '.', context = 'input', parent_obj = nil)
  include_path = File.absolute_path(include_path) + File::SEPARATOR

  object = parent_obj || @root
  object.include_path = include_path unless object.include_path
  object.context = context unless object.context

  close = parent_obj.nil?

  begin
    tree = @parser.parse input, reporter: Parslet::ErrorReporter::Deepest.new
  rescue Parslet::ParseFailed => error
    cause = error.cause
    reason = nil

    while cause
      reason = cause.to_s
      cause = cause.children.last
    end

    raise SlideField::ParseError, reason
  end

  interpret_tree tree, object, include_path, context, close
rescue SlideField::Error => error
  message = error.message

  if !message.start_with?('[') && message =~ /line (\d+) char (\d+)/
    line = $1.to_i - 1
    column = $2.to_i - 1

    if line > -1 && source = input.lines[line]
      excerpt = source.strip
      column -= source.index excerpt
      arrow = "#{"\x20" * column}^"

      message += "\n\t#{excerpt}\n\t#{arrow}"
    end
  end

  raise error.class, "[#{context}] #{message}"
end