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
22 # Visitor used to produce a HTML tree based on a AST on a `Source`
23 class AbstractHighlightVisitor
24 # The first line to generate, null if start at the first line
25 var first_line
: nullable Int = null is writable
27 # The last line to generate, null if finish at the last line
28 var last_line
: nullable Int = null is writable
30 # When highlighting a node, show its messages (errors, warnings), if any.
33 var show_messages
= true is writable
35 # When highlighting a node, also consider the loose tokens around it.
37 # Loose tokens are tokens discarded from the AST but attached before
38 # or after some non-loose tokens. See `Token::is_loose`.
40 # When this flag is set to `true`, the loose tokens that are before the
41 # first token and after the last token are also highlighted.
44 var include_loose_tokens
= false is writable
46 # When highlighting a node, the first and the last lines are fully included.
48 # If the highlighted node starts (or ends) in the middle of a line,
49 # this flags forces the whole line to be highlighted.
52 var include_whole_lines
= false is writable
54 # Highlight a AST element.
55 fun highlight_node
(n
: ANode)
68 if f
== null then return
70 if l
== null then return
73 if include_loose_tokens
then
74 if f
.prev_looses
.not_empty
then f
= f
.prev_looses
.first
75 if l
.next_looses
.not_empty
then l
= l
.next_looses
.last
80 while f
.location
.line_start
< line
do
82 if f
== null then return
88 while l
.location
.line_end
> line
do
90 if l
== null then return
94 if include_whole_lines
then
95 f
= f
.first_real_token_in_line
96 l
= l
.last_real_token_in_line
102 # Highlight a full lexed source file.
104 # REQUIRE `source.first_token != null`
105 fun highlight_source
(source
: SourceFile)
107 do_highlight
(source
.first_token
.as(not null), null)
110 # Low-level highlighting between 2 tokens
111 protected fun do_highlight
(first_token
: Token, last_token
: nullable Token) is abstract
114 # Text-based highlighter that use ANSI escape sequence for colors
115 class AnsiHighlightVisitor
116 super AbstractHighlightVisitor
118 # The produced highlighting
119 var result
= new Template
121 redef fun do_highlight
(f
, l
)
126 if c
!= f
then result
.add
(c
.blank_before
)
127 result
.add c
.ansi_colored
139 # Return the ANSI colored text
140 fun ansi_colored
: String do return text
144 redef fun ansi_colored
do return super.blue
147 redef class TokenKeyword
148 redef fun ansi_colored
do return super.yellow
152 redef fun ansi_colored
do return super.green
155 redef class TokenLiteral
156 redef fun ansi_colored
do return super.red