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 # This module is used to model 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
28 # Create a new sourcefile using a filename and a stream
29 init(filename
: String, stream
: IStream)
31 self.filename
= filename
32 string
= stream
.read_all
36 # A location inside a source file
39 redef type OTHER: Location
41 readable var _file
: nullable SourceFile
42 readable var _line_start
: Int
43 readable var _line_end
: Int
44 readable var _column_start
: Int
45 readable var _column_end
: Int
47 init(f
: nullable SourceFile, line_s
: Int, line_e
: Int, column_s
: Int, column_e
: Int) do
51 _column_start
= column_s
52 _column_end
= column_e
55 init with_file
(f
: SourceFile) do init(f
,0,0,0,0)
57 redef fun ==(other
: nullable Object): Bool do
58 if other
== null then return false
59 if not other
isa Location then return false
61 if other
.file
!= file
then return false
62 if other
.line_start
!= line_start
then return false
63 if other
.line_end
!= line_end
then return false
64 if other
.column_start
!= column_start
then return false
65 if other
.column_end
!= column_end
then return false
70 fun located_in
(loc
: nullable Location): Bool do
71 if loc
== null then return false
73 if line_start
< loc
.line_start
then return false
74 if line_start
> loc
.line_end
then return false
76 if line_end
> loc
.line_end
then return false
78 if line_start
== loc
.line_start
then
79 if column_start
< loc
.column_start
then return false
80 if column_start
> loc
.column_end
then return false
83 if line_end
== loc
.line_end
and column_end
> loc
.column_end
then return false
88 redef fun to_s
: String do
91 file_part
= file
.filename
92 if file
.filename
.length
> 0 then file_part
+= ":"
95 if line_start
== line_end
then
96 if column_start
== column_end
then
97 return "{file_part}{line_start},{column_start}"
99 return "{file_part}{line_start},{column_start}--{column_end}"
102 return "{file_part}{line_start},{column_start}--{line_end},{column_end}"
106 fun relative_to
(loc
: nullable Location): String do
107 var relative
: Location
108 if loc
!= null and loc
.file
== self.file
then
109 relative
= new Location(null, self.line_start
, self.line_end
, self.column_start
, self.column_end
)
111 relative
= new Location(self.file
, self.line_start
, self.line_end
, self.column_start
, self.column_end
)
116 redef fun <(other
: OTHER): Bool do
117 if self == other
then return false
118 if self.located_in
(other
) then return true
119 if other
.located_in
(self) then return false
121 if line_start
!= other
.line_start
then return line_start
< other
.line_start
122 if column_start
!= other
.column_start
then return column_start
< other
.column_start
123 if line_end
!= other
.line_end
then return line_end
< other
.line_end
125 return column_end
< other
.column_end