1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Highlighting of Nit AST
21 # Visitor used to produce a HTML tree based on a AST on a `Source`
22 class AbstractHighlightVisitor
23 # The first line to generate, null if start at the first line
24 var first_line
: nullable Int = null is writable
26 # The last line to generate, null if finish at the last line
27 var last_line
: nullable Int = null is writable
29 # When highlighting a node, show its messages (errors, warnings), if any.
32 var show_messages
= true is writable
34 # When highlighting a node, also consider the loose tokens around it.
36 # Loose tokens are tokens discarded from the AST but attached before
37 # or after some non-loose tokens. See `Token::is_loose`.
39 # When this flag is set to `true`, the loose tokens that are before the
40 # first token and after the last token are also highlighted.
43 var include_loose_tokens
= false is writable
45 # When highlighting a node, the first and the last lines are fully included.
47 # If the highlighted node starts (or ends) in the middle of a line,
48 # this flags forces the whole line to be highlighted.
51 var include_whole_lines
= false is writable
53 # Highlight a AST element.
54 fun highlight_node
(n
: ANode)
67 if f
== null then return
69 if l
== null then return
72 if include_loose_tokens
then
73 if f
.prev_looses
.not_empty
then f
= f
.prev_looses
.first
74 if l
.next_looses
.not_empty
then l
= l
.next_looses
.last
79 while f
.location
.line_start
< line
do
81 if f
== null then return
87 while l
.location
.line_end
> line
do
89 if l
== null then return
93 if include_whole_lines
then
94 f
= f
.first_real_token_in_line
95 l
= l
.last_real_token_in_line
101 # Highlight a full lexed source file.
103 # REQUIRE `source.first_token != null`
104 fun highlight_source
(source
: SourceFile)
106 do_highlight
(source
.first_token
.as(not null), null)
109 # Low-level highlighting between 2 tokens
110 protected fun do_highlight
(first_token
: Token, last_token
: nullable Token) is abstract