Class: Herb::Errors::UnexpectedTokenError

Inherits:
Error
  • Object
show all
Includes:
Colors
Defined in:
lib/herb/errors.rb,
ext/herb/error_helpers.c

Constant Summary

Constants included from Colors

Colors::CLEAR_SCREEN, Colors::HIDE_CURSOR, Colors::SHOW_CURSOR

Instance Attribute Summary collapse

Attributes inherited from Error

#location, #message, #type

Instance Method Summary collapse

Methods included from Colors

bold, bright_magenta, cyan, dimmed, enabled?, fg, fg_bg, green, magenta, red, white, yellow

Methods inherited from Error

#class_name, #error_name, #to_json

Constructor Details

#initialize(type, location, message, expected_type, found) ⇒ UnexpectedTokenError

: (String, Location?, String, String, Herb::Token) -> void



116
117
118
119
120
# File 'lib/herb/errors.rb', line 116

def initialize(type, location, message, expected_type, found)
  super(type, location, message)
  @expected_type = expected_type
  @found = found
end

Instance Attribute Details

#expected_typeObject (readonly)

| expected_type: String?, | found: Herb::Token?, | }



112
113
114
# File 'lib/herb/errors.rb', line 112

def expected_type
  @expected_type
end

#foundObject (readonly)

: Herb::Token?



113
114
115
# File 'lib/herb/errors.rb', line 113

def found
  @found
end

Instance Method Details

#inspectObject

: () -> String



123
124
125
# File 'lib/herb/errors.rb', line 123

def inspect
  tree_inspect.rstrip.gsub(/\s+$/, "")
end

#to_hashObject

: () -> serialized_unexpected_token_error



128
129
130
131
132
133
# File 'lib/herb/errors.rb', line 128

def to_hash
  super.merge(
    expected_type: expected_type,
    found: found
  ) #: Herb::serialized_unexpected_token_error
end

#tree_inspect(indent: 0, depth: 0, depth_limit: 25) ⇒ Object

: (?indent: Integer, ?depth: Integer, ?depth_limit: Integer) -> String



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/herb/errors.rb', line 136

def tree_inspect(indent: 0, depth: 0, depth_limit: 25)
  output = +""

  output += white("@ #{bold(red(error_name))} #{dimmed("(location: #{location&.tree_inspect})\n")}")
  output += white("├── message: #{green(message.inspect)}\n")
  output += white("├── expected_type: #{green(expected_type.inspect)}\n")
  output += white("└── found: ")
  output += found ? found.tree_inspect : magenta("")
  output += "\n"
  output += %(\n)

  output.gsub(/^/, "    " * indent)
end