1 # coding: utf-8
2 # frozen_string_literal: true
3
4 require_relative 'assertion'
5
6
7
8 module Umu
9
10 module Location
11
12 class Entry < Abstraction::Record
13 attr_reader :file_name
14 attr_reader :line_num
15
16
17 def self.deconstruct_keys
18 {
19 file_name: ::String,
20 line_num: ::Integer
21 }.freeze
22 end
23
24
25 def initialize(file_name, line_num)
26 ASSERT.kind_of file_name, ::String
27 ASSERT.kind_of line_num, ::Integer
28 ASSERT.assert line_num >= 0
29
30 @file_name = file_name
31 @line_num = line_num
32 end
33
34
35 def ==(other)
36 other.kind_of?(self.class) &&
37 self.file_name == other.file_name &&
38 self.line_num == other.line_num
39 end
40
41
42 def to_s
43 format "#%d in \"%s\"", self.line_num + 1, self.file_name
44 end
45
46
47 def next_line_num(n = 1)
48 ASSERT.kind_of n, ::Integer
49
50 self.update(line_num: line_num + n)
51 end
52 end
53
54
55 INITIAL_LOCATION = Entry.new('', 0).freeze
56
57
58 module_function
59
60 def make_location(file_name, line_num)
61 ASSERT.kind_of file_name, ::String
62 ASSERT.kind_of line_num, ::Integer
63
64 Entry.new(file_name, line_num).freeze
65 end
66
67
68 def make_initial_location
69 INITIAL_LOCATION
70 end
71 end # Umu::Location
72
73 end # Umu