Exception: Emfrp::Parser::ParsingError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/emfrp/parser/parsing_error.rb

Instance Method Summary collapse

Constructor Details

#initialize(src_str, file_name, status) ⇒ ParsingError

Returns a new instance of ParsingError.



6
7
8
9
10
# File 'lib/emfrp/parser/parsing_error.rb', line 6

def initialize(src_str, file_name, status)
  @src_str = src_str
  @file_name = file_name
  @status = status
end

Instance Method Details

#codeObject



12
13
14
# File 'lib/emfrp/parser/parsing_error.rb', line 12

def code
  @status.message[:code] || :noname
end

#column_numberObject



24
25
26
27
28
29
30
# File 'lib/emfrp/parser/parsing_error.rb', line 24

def column_number
  if @status.rest.length > 0
    @status.rest[0].tag[:column_number]
  else
    @src_str.each_line.last.length
  end
end

#lineObject



32
33
34
# File 'lib/emfrp/parser/parsing_error.rb', line 32

def line
  @src_str.each_line.to_a[line_number - 1]
end

#line_numberObject



16
17
18
19
20
21
22
# File 'lib/emfrp/parser/parsing_error.rb', line 16

def line_number
  if @status.rest.length > 0
    @status.rest[0].tag[:line_number]
  else
    @src_str.each_line.count
  end
end


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/emfrp/parser/parsing_error.rb', line 36

def print_error(output_io)
  output_io << "#{@file_name}:#{line_number}: "
  output_io << "SyntaxError, in `#{@status.message[:place]}`: "
  output_io << "#{@status.message[:required]} is expected"
  if @status.rest.length == 0
    output_io << ", but parser reached end-of-file\n"
  else
    output_io << "\n#{line.chomp}\n"
    output_io << "#{" " * (column_number - 1)}#{"^".colorize(:green)}\n"
  end
end