1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Nit source-file and locations in source-file
20 # A raw text Nit source file
22 # The path of the source
25 # The content of the source
26 var string
: String is noinit
28 # The original stream used to initialize `string`
33 string
= stream
.read_all
37 # Create a new sourcefile using a dummy filename and a given content
38 init from_string
(filename
: String, string
: String)
40 self.filename
= filename
45 # Position of each line start
46 var line_starts
= new Array[Int]
49 # A location inside a source file
52 redef type OTHER: Location
54 # The associated source-file
55 var file
: nullable SourceFile
57 # The starting line number (starting from 1)
59 # If `line_start==0` then the whole file is considered
62 # The stopping line number (starting from 1)
65 # Start of this location on `line_start`
67 # A `column_start` of 1 means the first column or character.
69 # If `column_start == 0` this location concerns the whole line.
71 # Require: `column_start >= 0`
74 # End of this location on `line_end`
77 # The index in the start character in the source
78 fun pstart
: Int do return file
.line_starts
[line_start-1
] + column_start-1
80 # The index on the end character in the source
81 fun pend
: Int do return file
.line_starts
[line_end-1
] + column_end-1
83 # The verbatim associated text in the source-file
86 var res
= self.text_cache
87 if res
!= null then return res
89 var pstart
= self.pstart
91 res
= l
.file
.string
.substring
(pstart
, pend-pstart
+1)
96 private var text_cache
: nullable String = null
98 redef fun ==(other
: nullable Object): Bool do
99 if other
== null then return false
100 if not other
isa Location then return false
102 if other
.file
!= file
then return false
103 if other
.line_start
!= line_start
then return false
104 if other
.line_end
!= line_end
then return false
105 if other
.column_start
!= column_start
then return false
106 if other
.column_end
!= column_end
then return false
111 # Is `self` included (or equals) to `loc`?
112 fun located_in
(loc
: nullable Location): Bool do
113 if loc
== null then return false
115 if line_start
< loc
.line_start
then return false
116 if line_start
> loc
.line_end
then return false
118 if line_end
> loc
.line_end
then return false
120 if line_start
== loc
.line_start
then
121 if column_start
< loc
.column_start
then return false
122 if column_start
> loc
.column_end
then return false
125 if line_end
== loc
.line_end
and column_end
> loc
.column_end
then return false
130 redef fun to_s
: String do
133 file_part
= file
.filename
136 if line_start
<= 0 then return file_part
138 if file
!= null and file
.filename
.length
> 0 then file_part
+= ":"
140 if line_start
== line_end
then
141 if column_start
== column_end
then
142 return "{file_part}{line_start},{column_start}"
144 return "{file_part}{line_start},{column_start}--{column_end}"
147 return "{file_part}{line_start},{column_start}--{line_end},{column_end}"
151 # Return a location message according to an observer.
153 # Currently, if both are in the same file, the file information is not present in the result.
154 fun relative_to
(loc
: nullable Location): String do
155 var relative
: Location
156 if loc
!= null and loc
.file
== self.file
then
157 relative
= new Location(null, self.line_start
, self.line_end
, self.column_start
, self.column_end
)
159 relative
= new Location(self.file
, self.line_start
, self.line_end
, self.column_start
, self.column_end
)
164 redef fun <(other
: OTHER): Bool do
165 if self == other
then return false
166 if self.located_in
(other
) then return true
167 if other
.located_in
(self) then return false
169 if line_start
!= other
.line_start
then return line_start
< other
.line_start
170 if column_start
!= other
.column_start
then return column_start
< other
.column_start
171 if line_end
!= other
.line_end
then return line_end
< other
.line_end
173 return column_end
< other
.column_end
176 # Return the associated line with the location highlighted with color and a caret under the starting position
177 # `color` must be and terminal escape sequence used as `"{escape}[{color}m;"`
179 # * `"1;31"` for bright red
180 # * `"0;32"` for green
181 fun colored_line
(color
: String): String
185 var col
= "{esc}[{color}m"
189 if i
<= 0 then return ""
191 var line_start
= l
.file
.line_starts
[i-1
]
192 var line_end
= line_start
193 var string
= l
.file
.string
194 while line_end
+1 < string
.length
and string
.chars
[line_end
+1] != '\n' and string
.chars
[line_end
+1] != '\r' do
198 if l
.column_start
> 0 then
199 lstart
= string
.substring
(line_start
, l
.column_start
- 1)
204 if i
!= l
.line_end
then
205 cend
= line_end
- line_start
+ 1
211 if line_start
+ cend
<= string
.length
then
212 lmid
= string
.substring
(line_start
+ l
.column_start
- 1, cend
- l
.column_start
+ 1)
213 lend
= string
.substring
(line_start
+ cend
, line_end
- line_start
- cend
+ 1)
218 var indent
= new FlatBuffer
219 for j
in [line_start
..line_start
+l
.column_start-1
[ do
220 if string
.chars
[j
] == '\t' then
226 return "\t{lstart}{col}{lmid}{def}{lend}\n\t{indent}^"