Class: DParse::Failure

Inherits:
Object
  • Object
show all
Defined in:
lib/d-parse/failure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, pos, origin: nil) ⇒ Failure

Returns a new instance of Failure.



7
8
9
10
11
# File 'lib/d-parse/failure.rb', line 7

def initialize(input, pos, origin: nil)
  @input = input
  @pos = pos
  @origin = origin
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



3
4
5
# File 'lib/d-parse/failure.rb', line 3

def input
  @input
end

#originObject (readonly)

Returns the value of attribute origin.



5
6
7
# File 'lib/d-parse/failure.rb', line 5

def origin
  @origin
end

#posObject (readonly)

Returns the value of attribute pos.



4
5
6
# File 'lib/d-parse/failure.rb', line 4

def pos
  @pos
end

Instance Method Details

#full_messageObject



21
22
23
# File 'lib/d-parse/failure.rb', line 21

def full_message
  "#{message} at line #{@pos.line + 1}, column #{@pos.column + 1}"
end

#inspectObject



42
43
44
# File 'lib/d-parse/failure.rb', line 42

def inspect
  to_s
end

#mapObject



13
14
15
# File 'lib/d-parse/failure.rb', line 13

def map
  self
end

#messageObject



17
18
19
# File 'lib/d-parse/failure.rb', line 17

def message
  @_message ||= 'expected ' + (@origin ? @origin.expectation_message : '?')
end

#pretty_messageObject



25
26
27
28
29
30
31
32
# File 'lib/d-parse/failure.rb', line 25

def pretty_message
  line = (input.lines[@pos.line] || '').rstrip
  fancy_line = line.chars.map.with_index { |c, i| i == @pos.column ? "\e[31m" + c + "\e[0m" : c }.join

  lines = [full_message, '', fancy_line, "\e[31m" + ' ' * @pos.column + '' + "\e[0m"]

  lines.join("\n")
end

#success?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/d-parse/failure.rb', line 38

def success?
  false
end

#to_sObject



34
35
36
# File 'lib/d-parse/failure.rb', line 34

def to_s
  "Failure(#{@pos}; #{message})"
end