metamodel: rename 'universal' to 'enum'
[nit.git] / src / location.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 package location
18
19 class Location
20 super Comparable
21 redef type OTHER: Location
22
23 readable var _file: String
24 readable var _line_start: Int
25 readable var _line_end: Int
26 readable var _column_start: Int
27 readable var _column_end: Int
28
29 init(f: String, line_s: Int, line_e: Int, column_s: Int, column_e: Int) do
30 _file = f
31 _line_start = line_s
32 _line_end = line_e
33 _column_start = column_s
34 _column_end = column_e
35 end
36
37 init with_file(f: String) do init(f,0,0,0,0)
38
39 redef fun ==(other: nullable Object): Bool do
40 if other == null then return false
41 if not other isa Location then return false
42
43 if other.file != file then return false
44 if other.line_start != line_start then return false
45 if other.line_end != line_end then return false
46 if other.column_start != column_start then return false
47 if other.column_end != column_end then return false
48
49 return true
50 end
51
52 fun located_in(loc: nullable Location): Bool do
53 if loc == null then return false
54
55 if line_start < loc.line_start then return false
56 if line_start > loc.line_end then return false
57
58 if line_end > loc.line_end then return false
59
60 if line_start == loc.line_start then
61 if column_start < loc.column_start then return false
62 if column_start > loc.column_end then return false
63 end
64
65 if line_end == loc.line_end and column_end > loc.column_end then return false
66
67 return true
68 end
69
70 redef fun to_s: String do
71 var file_part = file
72 if file_part.length > 0 then file_part += ":"
73
74 if line_start == line_end then
75 if column_start == column_end then
76 return "{file_part}{line_start},{column_start}"
77 else
78 return "{file_part}{line_start},{column_start}--{column_end}"
79 end
80 else
81 return "{file_part}{line_start},{column_start}--{line_end},{column_end}"
82 end
83 end
84
85 fun relative_to(loc: nullable Location): String do
86 var relative: Location
87 if loc != null and loc.file == self.file then
88 relative = new Location("", self.line_start, self.line_end, self.column_start, self.column_end)
89 else
90 relative = new Location(self.file, self.line_start, self.line_end, self.column_start, self.column_end)
91 end
92 return relative.to_s
93 end
94
95 redef fun <(other: OTHER): Bool do
96 if self == other then return false
97 if self.located_in(other) then return true
98 if other.located_in(self) then return false
99
100 if line_start != other.line_start then return line_start < other.line_start
101 if column_start != other.column_start then return column_start < other.column_start
102 if line_end != other.line_end then return line_end < other.line_end
103
104 return column_end < other.column_end
105 end
106 end
107