update NOTICE and LICENSE
[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 # This module is used to model Nit source-file and locations in source-file
18 package location
19
20 # A raw text Nit source file
21 class SourceFile
22 # The path of the source
23 var filename: String
24
25 # The content of the source
26 var string: String
27
28 # Create a new sourcefile using a filename and a stream
29 init(filename: String, stream: IStream)
30 do
31 self.filename = filename
32 string = stream.read_all
33 line_starts[0] = 0
34 end
35
36 # Position of each line start
37 var line_starts: Array[Int] = new Array[Int]
38 end
39
40 # A location inside a source file
41 class Location
42 super Comparable
43 redef type OTHER: Location
44
45 readable var _file: nullable SourceFile
46 readable var _line_start: Int
47 readable var _line_end: Int
48 readable var _column_start: Int
49 readable var _column_end: Int
50
51 init(f: nullable SourceFile, line_s: Int, line_e: Int, column_s: Int, column_e: Int) do
52 _file = f
53 _line_start = line_s
54 _line_end = line_e
55 _column_start = column_s
56 _column_end = column_e
57 end
58
59 init with_file(f: SourceFile) do init(f,0,0,0,0)
60
61 redef fun ==(other: nullable Object): Bool do
62 if other == null then return false
63 if not other isa Location then return false
64
65 if other.file != file then return false
66 if other.line_start != line_start then return false
67 if other.line_end != line_end then return false
68 if other.column_start != column_start then return false
69 if other.column_end != column_end then return false
70
71 return true
72 end
73
74 fun located_in(loc: nullable Location): Bool do
75 if loc == null then return false
76
77 if line_start < loc.line_start then return false
78 if line_start > loc.line_end then return false
79
80 if line_end > loc.line_end then return false
81
82 if line_start == loc.line_start then
83 if column_start < loc.column_start then return false
84 if column_start > loc.column_end then return false
85 end
86
87 if line_end == loc.line_end and column_end > loc.column_end then return false
88
89 return true
90 end
91
92 redef fun to_s: String do
93 var file_part = ""
94 if file != null then
95 file_part = file.filename
96 if file.filename.length > 0 then file_part += ":"
97 end
98
99 if line_start == line_end then
100 if column_start == column_end then
101 return "{file_part}{line_start},{column_start}"
102 else
103 return "{file_part}{line_start},{column_start}--{column_end}"
104 end
105 else
106 return "{file_part}{line_start},{column_start}--{line_end},{column_end}"
107 end
108 end
109
110 fun relative_to(loc: nullable Location): String do
111 var relative: Location
112 if loc != null and loc.file == self.file then
113 relative = new Location(null, self.line_start, self.line_end, self.column_start, self.column_end)
114 else
115 relative = new Location(self.file, self.line_start, self.line_end, self.column_start, self.column_end)
116 end
117 return relative.to_s
118 end
119
120 redef fun <(other: OTHER): Bool do
121 if self == other then return false
122 if self.located_in(other) then return true
123 if other.located_in(self) then return false
124
125 if line_start != other.line_start then return line_start < other.line_start
126 if column_start != other.column_start then return column_start < other.column_start
127 if line_end != other.line_end then return line_end < other.line_end
128
129 return column_end < other.column_end
130 end
131 end
132