src: update most tools to new constructors
[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 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: IStream
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 var file: nullable SourceFile
55 var line_start: Int
56 var line_end: Int
57 var column_start: Int
58 var column_end: Int
59
60 # The index in the start character in the source
61 fun pstart: Int do return file.line_starts[line_start-1] + column_start-1
62
63 # The index on the end character in the source
64 fun pend: Int do return file.line_starts[line_end-1] + column_end-1
65
66 # The verbatim associated text in the source-file
67 fun text: String
68 do
69 var res = self.text_cache
70 if res != null then return res
71 var l = self
72 var pstart = self.pstart
73 var pend = self.pend
74 res = l.file.string.substring(pstart, pend-pstart+1)
75 self.text_cache = res
76 return res
77 end
78
79 private var text_cache: nullable String = null
80
81 init with_file(f: SourceFile) do init(f,0,0,0,0)
82
83 redef fun ==(other: nullable Object): Bool do
84 if other == null then return false
85 if not other isa Location then return false
86
87 if other.file != file then return false
88 if other.line_start != line_start then return false
89 if other.line_end != line_end then return false
90 if other.column_start != column_start then return false
91 if other.column_end != column_end then return false
92
93 return true
94 end
95
96 # Is `self` included (or equals) to `loc`?
97 fun located_in(loc: nullable Location): Bool do
98 if loc == null then return false
99
100 if line_start < loc.line_start then return false
101 if line_start > loc.line_end then return false
102
103 if line_end > loc.line_end then return false
104
105 if line_start == loc.line_start then
106 if column_start < loc.column_start then return false
107 if column_start > loc.column_end then return false
108 end
109
110 if line_end == loc.line_end and column_end > loc.column_end then return false
111
112 return true
113 end
114
115 redef fun to_s: String do
116 var file_part = ""
117 if file != null then
118 file_part = file.filename
119 if file.filename.length > 0 then file_part += ":"
120 end
121
122 if line_start == line_end then
123 if column_start == column_end then
124 return "{file_part}{line_start},{column_start}"
125 else
126 return "{file_part}{line_start},{column_start}--{column_end}"
127 end
128 else
129 return "{file_part}{line_start},{column_start}--{line_end},{column_end}"
130 end
131 end
132
133 # Return a location message according to an observer.
134 #
135 # Currently, if both are in the same file, the file information is not present in the result.
136 fun relative_to(loc: nullable Location): String do
137 var relative: Location
138 if loc != null and loc.file == self.file then
139 relative = new Location(null, self.line_start, self.line_end, self.column_start, self.column_end)
140 else
141 relative = new Location(self.file, self.line_start, self.line_end, self.column_start, self.column_end)
142 end
143 return relative.to_s
144 end
145
146 redef fun <(other: OTHER): Bool do
147 if self == other then return false
148 if self.located_in(other) then return true
149 if other.located_in(self) then return false
150
151 if line_start != other.line_start then return line_start < other.line_start
152 if column_start != other.column_start then return column_start < other.column_start
153 if line_end != other.line_end then return line_end < other.line_end
154
155 return column_end < other.column_end
156 end
157
158 # Return the associated line with the location highlighted with color and a caret under the starting position
159 # `color` must be and terminal escape sequence used as `"{escape}[{color}m;"`
160 # * `"0;31"` for red
161 # * `"1;31"` for bright red
162 # * `"0;32"` for green
163 fun colored_line(color: String): String
164 do
165 var esc = 27.ascii
166 var def = "{esc}[0m"
167 var col = "{esc}[{color}m"
168
169 var l = self
170 var i = l.line_start
171 var line_start = l.file.line_starts[i-1]
172 var line_end = line_start
173 var string = l.file.string
174 while line_end+1 < string.length and string.chars[line_end+1] != '\n' and string.chars[line_end+1] != '\r' do
175 line_end += 1
176 end
177 var lstart = string.substring(line_start, l.column_start - 1)
178 var cend
179 if i != l.line_end then
180 cend = line_end - line_start + 1
181 else
182 cend = l.column_end
183 end
184 var lmid
185 var lend
186 if line_start + cend <= string.length then
187 lmid = string.substring(line_start + l.column_start - 1, cend - l.column_start + 1)
188 lend = string.substring(line_start + cend, line_end - line_start - cend + 1)
189 else
190 lmid = ""
191 lend = ""
192 end
193 var indent = new FlatBuffer
194 for j in [line_start..line_start+l.column_start-1[ do
195 if string.chars[j] == '\t' then
196 indent.add '\t'
197 else
198 indent.add ' '
199 end
200 end
201 return "\t{lstart}{col}{lmid}{def}{lend}\n\t{indent}^"
202 end
203 end