Class: DParse::Failure
- Inherits:
-
Object
- Object
- DParse::Failure
- Defined in:
- lib/d-parse/failure.rb
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
Instance Method Summary collapse
- #full_message ⇒ Object
-
#initialize(input, pos, origin: nil) ⇒ Failure
constructor
A new instance of Failure.
- #inspect ⇒ Object
- #map ⇒ Object
- #message ⇒ Object
- #pretty_message ⇒ Object
- #success? ⇒ Boolean
- #to_s ⇒ Object
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
#input ⇒ Object (readonly)
Returns the value of attribute input.
3 4 5 |
# File 'lib/d-parse/failure.rb', line 3 def input @input end |
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
5 6 7 |
# File 'lib/d-parse/failure.rb', line 5 def origin @origin end |
#pos ⇒ Object (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_message ⇒ Object
21 22 23 |
# File 'lib/d-parse/failure.rb', line 21 def "#{} at line #{@pos.line + 1}, column #{@pos.column + 1}" end |
#inspect ⇒ Object
42 43 44 |
# File 'lib/d-parse/failure.rb', line 42 def inspect to_s end |
#map ⇒ Object
13 14 15 |
# File 'lib/d-parse/failure.rb', line 13 def map self end |
#message ⇒ Object
17 18 19 |
# File 'lib/d-parse/failure.rb', line 17 def @_message ||= 'expected ' + (@origin ? @origin. : '?') end |
#pretty_message ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/d-parse/failure.rb', line 25 def 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 = [, '', fancy_line, "\e[31m" + ' ' * @pos.column + '↑' + "\e[0m"] lines.join("\n") end |
#success? ⇒ Boolean
38 39 40 |
# File 'lib/d-parse/failure.rb', line 38 def success? false end |
#to_s ⇒ Object
34 35 36 |
# File 'lib/d-parse/failure.rb', line 34 def to_s "Failure(#{@pos}; #{})" end |