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