Class: Lrama::Lexer::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/lrama/lexer/location.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grammar_file:, first_line:, first_column:, last_line:, last_column:) ⇒ Location

Returns a new instance of Location.



14
15
16
17
18
19
20
# File 'lib/lrama/lexer/location.rb', line 14

def initialize(grammar_file:, first_line:, first_column:, last_line:, last_column:)
  @grammar_file = grammar_file
  @first_line = first_line
  @first_column = first_column
  @last_line = last_line
  @last_column = last_column
end

Instance Attribute Details

#first_columnObject (readonly)

: Integer



9
10
11
# File 'lib/lrama/lexer/location.rb', line 9

def first_column
  @first_column
end

#first_lineObject (readonly)

: Integer



8
9
10
# File 'lib/lrama/lexer/location.rb', line 8

def first_line
  @first_line
end

#grammar_fileObject (readonly)

: GrammarFile



7
8
9
# File 'lib/lrama/lexer/location.rb', line 7

def grammar_file
  @grammar_file
end

#last_columnObject (readonly)

: Integer



11
12
13
# File 'lib/lrama/lexer/location.rb', line 11

def last_column
  @last_column
end

#last_lineObject (readonly)

: Integer



10
11
12
# File 'lib/lrama/lexer/location.rb', line 10

def last_line
  @last_line
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/lrama/lexer/location.rb', line 23

def ==(other)
  self.class == other.class &&
  self.grammar_file == other.grammar_file &&
  self.first_line == other.first_line &&
  self.first_column == other.first_column &&
  self.last_line == other.last_line &&
  self.last_column == other.last_column
end

#generate_error_message(error_message) ⇒ Object



69
70
71
72
73
74
# File 'lib/lrama/lexer/location.rb', line 69

def generate_error_message(error_message)
  "    \#{path}:\#{first_line}:\#{first_column}: \#{error_message}\n    \#{line_with_carets}\n  ERROR\nend\n".chomp

#line_with_caretsObject



77
78
79
80
81
82
# File 'lib/lrama/lexer/location.rb', line 77

def line_with_carets
  "    \#{text}\n    \#{carets}\n  TEXT\nend\n"

#partial_location(left, right) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lrama/lexer/location.rb', line 33

def partial_location(left, right)
  offset = -first_column
  new_first_line = -1
  new_first_column = -1
  new_last_line = -1
  new_last_column = -1

  _text.each.with_index do |line, index|
    new_offset = offset + line.length + 1

    if offset <= left && left <= new_offset
      new_first_line = first_line + index
      new_first_column = left - offset
    end

    if offset <= right && right <= new_offset
      new_last_line = first_line + index
      new_last_column = right - offset
    end

    offset = new_offset
  end

  Location.new(
    grammar_file: grammar_file,
    first_line: new_first_line, first_column: new_first_column,
    last_line: new_last_line, last_column: new_last_column
  )
end

#to_sObject



64
65
66
# File 'lib/lrama/lexer/location.rb', line 64

def to_s
  "#{path} (#{first_line},#{first_column})-(#{last_line},#{last_column})"
end