Exception: DMark::Lexer::LexerError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/dmark/lexer.rb

Defined Under Namespace

Classes: Coloriser, NullColoriser

Instance Method Summary collapse

Constructor Details

#initialize(message, line, line_nr, col_nr) ⇒ LexerError

Returns a new instance of LexerError.



94
95
96
97
98
99
# File 'lib/dmark/lexer.rb', line 94

def initialize(message, line, line_nr, col_nr)
  @message = message
  @line = line
  @line_nr = line_nr
  @col_nr = col_nr
end

Instance Method Details

#formatted_message(coloriser) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dmark/lexer.rb', line 137

def formatted_message(coloriser)
  line_excerpt_start = [@col_nr - 38, 0].max
  line_excerpt_end = @col_nr + 38
  line_excerpt = @line[line_excerpt_start..line_excerpt_end]

  if line_excerpt_start > 0
    line_excerpt[0] = ''
  end

  if line_excerpt_end < @line.size
    line_excerpt[-1] = ''
  end

  [
    "#{coloriser.red}#{coloriser.bold}ERROR#{coloriser.reset} (line #{@line_nr}, col #{@col_nr}): #{coloriser.red}#{@message}#{coloriser.reset}",
    '',
    line_excerpt,
    coloriser.red + ' ' * (@col_nr - 1 - line_excerpt_start) + '^' + coloriser.reset,
    '',
  ].join("\n")
end

#messageObject



129
130
131
# File 'lib/dmark/lexer.rb', line 129

def message
  formatted_message(NullColoriser.new)
end

#message_for_ttyObject



133
134
135
# File 'lib/dmark/lexer.rb', line 133

def message_for_tty
  formatted_message(Coloriser.new)
end