Exception: Calyx::Errors::UndefinedRule

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/calyx/errors.rb

Overview

Only rules that exist in the registry can be evaluated. When a non-existent rule is referenced, this error is raised.

grammar = Calyx::Grammar.new do
  start :blank
end

grammar.evaluate
# => Calyx::Errors::UndefinedRule: :blank is not defined

Instance Method Summary collapse

Constructor Details

#initialize(rule, symbol) ⇒ UndefinedRule

Returns a new instance of UndefinedRule.



14
15
16
17
18
19
20
21
22
# File 'lib/calyx/errors.rb', line 14

def initialize(rule, symbol)
  @trace = if rule
    rule.trace
  else
    trace_api_boundary(caller_locations)
  end

  super("undefined rule :#{symbol} in #{@trace.path}:#{@trace.lineno}:`#{source_line}`")
end

Instance Method Details

#source_lineObject



32
33
34
35
36
37
# File 'lib/calyx/errors.rb', line 32

def source_line
  File.open(@trace.absolute_path) do |source|
    (@trace.lineno-1).times { source.gets }
    source.gets
  end.strip
end

#trace_api_boundary(trace) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/calyx/errors.rb', line 24

def trace_api_boundary(trace)
  (trace.count - 1).downto(0) do |index|
    if trace[index].to_s.include?('lib/calyx/')
      return trace[index + 1]
    end
  end
end