loader: set correct location for README files
[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 # Nit source-file and locations in source-file
18 module 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 is noinit
27
28 # The original stream used to initialize `string`
29 var stream: Reader
30
31 init
32 do
33 string = stream.read_all
34 line_starts[0] = 0
35 end
36
37 # Create a new sourcefile using a dummy filename and a given content
38 init from_string(filename: String, string: String)
39 do
40 self.filename = filename
41 self.string = string
42 line_starts[0] = 0
43 end
44
45 # Offset of each line start in the content `string`.
46 #
47 # Used for fast access to each line when rendering parts of the `string`.
48 var line_starts = new Array[Int]
49 end
50
51 # A location inside a source file
52 class Location
53 super Comparable
54 redef type OTHER: Location
55
56 # The associated source-file
57 var file: nullable SourceFile
58
59 # The starting line number (starting from 1)
60 #
61 # If `line_start==0` then the whole file is considered
62 var line_start: Int
63
64 # The stopping line number (starting from 1)
65 var line_end: Int
66
67 # Start of this location on `line_start`
68 #
69 # A `column_start` of 1 means the first column or character.
70 #
71 # If `column_start == 0` this location concerns the whole line.
72 #
73 # Require: `column_start >= 0`
74 var column_start: Int
75
76 # End of this location on `line_end`
77 var column_end: Int
78
79 # Builds a location instance from its string representation.
80 #
81 # Examples:
82 #
83 # ~~~
84 # var loc = new Location.from_string("location.nit:82,2--105,8")
85 # assert loc.to_s == "location.nit:82,2--105,8"
86 #
87 # loc = new Location.from_string("location.nit")
88 # assert loc.to_s == "location.nit"
89 #
90 # loc = new Location.from_string("location.nit:82,2")
91 # assert loc.to_s == "location.nit:82,2--0,0"
92 #
93 # loc = new Location.from_string("location.nit:82--105")
94 # assert loc.to_s == "location.nit:82,0--105,0"
95 #
96 # loc = new Location.from_string("location.nit:82,2--105")
97 # assert loc.to_s == "location.nit:82,2--105,0"
98 #
99 # loc = new Location.from_string("location.nit:82--105,8")
100 # assert loc.to_s == "location.nit:82,0--105,8"
101 # ~~~
102 init from_string(string: String) do
103 self.line_start = 0
104 self.line_end = 0
105 self.column_start = 0
106 self.column_end = 0
107 # parses the location string and init position vars
108 var parts = string.split_with(":")
109 var filename = parts.shift
110 self.file = new SourceFile(filename, new FileReader.open(filename))
111 # split position
112 if parts.is_empty then return
113 var pos = parts.first.split_with("--")
114 # split start position
115 if pos.first.has(",") then
116 var pos1 = pos.first.split_with(",")
117 self.line_start = pos1[0].to_i
118 if pos1.length > 1 then
119 self.column_start = pos1[1].to_i
120 end
121 else
122 self.line_start = pos.first.to_i
123 end
124 # split end position
125 if pos.length <= 1 then return
126 if pos[1].has(",") then
127 var pos2 = pos[1].split_with(",")
128 if pos2.length > 1 then
129 self.line_end = pos2[0].to_i
130 self.column_end = pos2[1].to_i
131 else
132 self.line_end = self.line_start
133 self.column_end = pos2[0].to_i
134 end
135 else
136 self.line_end = pos[1].to_i
137 end
138 end
139
140 # The index in the start character in the source
141 fun pstart: Int do return file.line_starts[line_start-1] + column_start-1
142
143 # The index on the end character in the source
144 fun pend: Int do return file.line_starts[line_end-1] + column_end-1
145
146 # The verbatim associated text in the source-file
147 fun text: String
148 do
149 var res = self.text_cache
150 if res != null then return res
151 var l = self
152 var pstart = self.pstart
153 var pend = self.pend
154 res = l.file.string.substring(pstart, pend-pstart+1)
155 self.text_cache = res
156 return res
157 end
158
159 private var text_cache: nullable String = null
160
161 redef fun ==(other: nullable Object): Bool do
162 if other == null then return false
163 if not other isa Location then return false
164
165 if other.file != file then return false
166 if other.line_start != line_start then return false
167 if other.line_end != line_end then return false
168 if other.column_start != column_start then return false
169 if other.column_end != column_end then return false
170
171 return true
172 end
173
174 # Is `self` included (or equals) to `loc`?
175 fun located_in(loc: nullable Location): Bool do
176 if loc == null then return false
177
178 if line_start < loc.line_start then return false
179 if line_start > loc.line_end then return false
180
181 if line_end > loc.line_end then return false
182
183 if line_start == loc.line_start then
184 if column_start < loc.column_start then return false
185 if column_start > loc.column_end then return false
186 end
187
188 if line_end == loc.line_end and column_end > loc.column_end then return false
189
190 return true
191 end
192
193 redef fun to_s: String do
194 var file_part = ""
195 if file != null then
196 file_part = file.filename
197 end
198
199 if line_start <= 0 then return file_part
200
201 if file != null and file.filename.length > 0 then file_part += ":"
202
203 if line_start == line_end then
204 if column_start == column_end then
205 return "{file_part}{line_start},{column_start}"
206 else
207 return "{file_part}{line_start},{column_start}--{column_end}"
208 end
209 else
210 return "{file_part}{line_start},{column_start}--{line_end},{column_end}"
211 end
212 end
213
214 # Return a location message according to an observer.
215 #
216 # Currently, if both are in the same file, the file information is not present in the result.
217 fun relative_to(loc: nullable Location): String do
218 var relative: Location
219 if loc != null and loc.file == self.file then
220 relative = new Location(null, self.line_start, self.line_end, self.column_start, self.column_end)
221 else
222 relative = new Location(self.file, self.line_start, self.line_end, self.column_start, self.column_end)
223 end
224 return relative.to_s
225 end
226
227 redef fun <(other: OTHER): Bool do
228 if self == other then return false
229 if self.located_in(other) then return true
230 if other.located_in(self) then return false
231
232 if line_start != other.line_start then return line_start < other.line_start
233 if column_start != other.column_start then return column_start < other.column_start
234 if line_end != other.line_end then return line_end < other.line_end
235
236 return column_end < other.column_end
237 end
238
239 # Return the associated line with the location highlighted with color and a caret under the starting position
240 # `color` must be and terminal escape sequence used as `"{escape}[{color}m;"`
241 # * `"0;31"` for red
242 # * `"1;31"` for bright red
243 # * `"0;32"` for green
244 fun colored_line(color: String): String
245 do
246 var esc = 27.ascii
247 var def = "{esc}[0m"
248 var col = "{esc}[{color}m"
249
250 var l = self
251 var i = l.line_start
252 if i <= 0 then return ""
253
254 var line_start = l.file.line_starts[i-1]
255 var line_end = line_start
256 var string = l.file.string
257 while line_end+1 < string.length and string.chars[line_end+1] != '\n' and string.chars[line_end+1] != '\r' do
258 line_end += 1
259 end
260 var lstart
261 if l.column_start > 0 then
262 lstart = string.substring(line_start, l.column_start - 1)
263 else
264 lstart = ""
265 end
266 var cend
267 if i != l.line_end then
268 cend = line_end - line_start + 1
269 else
270 cend = l.column_end
271 end
272 var lmid
273 var lend
274 if line_start + cend <= string.length then
275 lmid = string.substring(line_start + l.column_start - 1, cend - l.column_start + 1)
276 lend = string.substring(line_start + cend, line_end - line_start - cend + 1)
277 else
278 lmid = ""
279 lend = ""
280 end
281 var indent = new FlatBuffer
282 for j in [line_start..line_start+l.column_start-1[ do
283 if string.chars[j] == '\t' then
284 indent.add '\t'
285 else
286 indent.add ' '
287 end
288 end
289 return "\t{lstart}{col}{lmid}{def}{lend}\n\t{indent}^"
290 end
291 end