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