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
23 # Visitor used to produce a HTML tree based on a AST on a `Source`
24 class HighlightVisitor
25 # The root of the HTML hierarchy
26 var html
= new HTMLTag("span")
28 # Is the HTML include a nested `<span class"{type_of_node}">` element for each `ANode` of the AST?
29 # Used to have a really huge and verbose HTML (mainly for debug)
30 var with_ast
= false is writable
32 # Prefixes used in generated IDs for line `<span>` elements.
33 # Useful if more than one highlighted code is present in the same HTML document.
35 # If set to the empty string, id for lines are disabled.
37 # Is `"L"` by default.
38 var line_id_prefix
= "L" is writable
40 # The first line to generate, null if start at the first line
41 var first_line
: nullable Int = null is writable
43 # The last line to generate, null if finish at the last line
44 var last_line
: nullable Int = null is writable
48 html
.add_class
("nitcode")
51 # When highlighting a node, also consider the loose tokens around it.
53 # Loose tokens are tokens discarded from the AST but attached before
54 # or after some non-loose tokens. See `Token::is_loose`.
56 # When this flag is set to `true`, the loose tokens that are before the
57 # first token and after the last token are also highlighted.
60 var include_loose_tokens
= false is writable
62 # When highlighting a node, the first and the last lines are fully included.
64 # If the highlighted node starts (or ends) in the middle of a line,
65 # this flags forces the whole line to be highlighted.
68 var include_whole_lines
= false is writable
70 # The entry-point of the highlighting.
71 # Will fill `html` with the generated HTML content.
72 fun enter_visit
(n
: ANode)
85 if f
== null then return
87 if l
== null then return
90 if include_loose_tokens
then
91 if f
.prev_looses
.not_empty
then f
= f
.prev_looses
.first
92 if l
.next_looses
.not_empty
then l
= l
.next_looses
.last
95 if include_whole_lines
then
96 f
= f
.first_real_token_in_line
97 l
= l
.last_real_token_in_line
103 private fun full_tag
(anode
: ANode, hv
: HighlightVisitor): nullable HTMLTag
105 var tag
= anode
.make_tag
(hv
)
106 if tag
== null then return null
107 var infobox
= anode
.infobox
(hv
)
108 if infobox
== null and anode
isa Token then
109 var pa
= anode
.parent
112 if c
isa TId or c
isa TClassid or c
isa TAttrid or c
isa TokenLiteral or c
isa TokenOperator or c
isa TComment and pa
isa ADoc then
113 infobox
= pa
.decorate_tag
(hv
, tag
, anode
)
117 var messages
= anode
.location
.messages
118 if messages
!= null then
119 tag
.css
("border-bottom", "solid 2px red")
120 if infobox
== null then
121 infobox
= new HInfoBox(hv
, "Messages")
123 var c
= infobox
.new_dropdown
("{messages.length} message(s)", "")
125 c
.open
("li").append
(m
.text
)
128 if infobox
!= null then
129 tag
.attach_infobox
(infobox
)
134 # Produce HTML between two tokens
135 protected fun htmlize
(first_token
, last_token
: Token)
137 var stack2
= new Array[HTMLTag]
138 var stack
= new Array[Prod]
140 var c
: nullable Token = first_token
141 var hv
= new HighlightVisitor
145 # Handle start of line
146 var cline
= c
.location
.line_start
147 if cline
!= line
then
148 # Handle starting block productions,
149 # Because c could be a detached token, get prods in
150 # the first AST token
151 var c0
= c
.first_token_in_line
153 if c0
!= null then starting
= c0
.starting_prods
154 if starting
!= null then for p
in starting
do
155 if not p
.is_block
then continue
156 var tag
= full_tag
(p
, hv
)
157 if tag
== null then continue
158 tag
.add_class
("foldable")
165 # Add a div for the whole line
166 var tag
= new HTMLTag("span")
167 var p
= line_id_prefix
168 if p
!= "" then tag
.attrs
["id"] = "{p}{cline}"
169 tag
.classes
.add
"line"
176 # Add the blank, verbatim
177 html
.add_raw_html c
.blank_before
179 # Handle starting span production
180 starting
= c
.starting_prods
181 if starting
!= null then for p
in starting
do
182 if not p
.is_span
then continue
183 var tag
= full_tag
(p
, hv
)
184 if tag
== null then continue
195 var tag
= full_tag
(c
, hv
)
196 if tag
!= null then html
.add tag
199 # Handle ending span productions
200 var ending
= c
.ending_prods
201 if ending
!= null then for p
in ending
do
202 if not p
.is_span
then continue
203 if stack
.is_empty
or p
!= stack
.last
then continue
208 # Handle end of line and end of file
210 if c
== last_token
then n
= null
211 if n
== null or n
.location
.line_start
!= line
then
212 # closes the line div
215 # close the block production divs
216 var c0
= c
.last_token_in_line
218 if c0
!= null then ending
= c0
.ending_prods
219 if ending
!= null then for p
in ending
do
220 if not p
.is_block
then continue
221 if stack
.is_empty
or p
!= stack
.last
then continue
229 #assert stack.is_empty
230 #assert stack2.is_empty
233 # Return a default CSS content related to CSS classes used in the `html` tree.
234 # Could be inlined in the `.html` file of saved as a specific `.css` file.
235 fun css_content
: String
238 .nitcode a { color: inherit; cursor:pointer; }
239 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
240 .nitcode .foldable { display: block } /* for block productions*/
241 .nitcode .line{ display: block } /* for lines */
242 .nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
243 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
244 /* lexical raw tokens. independent of usage or semantic: */
245 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
246 .nitcode .nc_d { color: #3D8127; font-style: italic; } /* documentation comments */
247 .nitcode .nc_k { font-weight: bold; } /* keyword */
248 .nitcode .nc_o {} /* operator */
249 .nitcode .nc_i {} /* standard identifier */
250 .nitcode .nc_t { color: #445588; font-weight: bold; } /* type/class identifier */
251 .nitcode .nc_a { color: #445588; font-style: italic; } /* old style attribute identifier */
252 .nitcode .nc_l { color: #009999; } /* char and number literal */
253 .nitcode .nc_s { color: #8F1546; } /* string literal */
254 /* syntactic token usage. added because of their position in the AST */
255 .nitcode .nc_ast { color: blue; } /* assert label */
256 .nitcode .nc_la { color: blue; } /* break/continue label */
257 .nitcode .nc_m { color: #445588; } /* module name */
258 /* syntactic groups */
259 .nitcode .nc_def { font-weight: bold; color: blue; } /* name used in a definition */
260 .nitcode .nc_def.nc_a { color: blue; } /* name used in a attribute definition */
261 .nitcode .nc_def.nc_t { color: blue; } /* name used in a class or vt definition */
262 .nitcode .nc_ss { color: #9E6BEB; } /* superstrings */
263 .nitcode .nc_cdef {} /* A whole class definition */
264 .nitcode .nc_pdef {} /* A whole property definition */
265 /* semantic token usage */
266 .nitcode .nc_v { font-style: italic; } /* local variable or parameter */
267 .nitcode .nc_vt { font-style: italic; } /* virtual type or formal type */
269 .nitcode .nc_error { border: 1px red solid;} /* not used */
270 .popover { max-width: 800px !important; }
274 # Additional content to inject in the <head> tag
275 # Note: does not include `css_content`; handle it yourself.
276 fun head_content
: String
278 return """<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">\n"""
281 # Additional content to inject just before the closing </body> tag
282 fun foot_content
: String
285 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
286 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
287 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
292 # Attach the infobox to the node by using BootStrap popover
293 fun attach_infobox
(infobox
: HInfoBox)
295 classes
.add
("popupable")
296 attrs
["title"] = infobox
.title
297 var href
= infobox
.href
299 attrs
["data-title"] = """<a href="{{{href}}}">{{{infobox.title}}}</a>"""
301 attrs
["data-content"] = infobox
.content
.write_to_string
302 attrs
["data-toggle"] = "popover"
307 # A generic information container that can be used to decorate AST entities
309 # The visitor used for contextualisation, if needed
310 var visitor
: HighlightVisitor
312 # A short title for the AST element
315 # The primary link where the entity points
317 var href
: nullable String = null
319 # The content of the popuped infobox
320 var content
= new HTMLTag("div")
322 # Append a new field in the popuped infobox
323 fun new_field
(title
: String): HTMLTag
325 content
.open
("b").text
(title
)
327 var res
= content
.open
("span")
332 # Append a new dropdown in the popuped content
333 fun new_dropdown
(title
, text
: String): HTMLTag
335 content
.add_raw_html
"""<div class="dropdown"> <a data-toggle="dropdown" href="#"><b>"""
336 content
.append
(title
)
337 content
.add_raw_html
"</b> "
339 content
.add_raw_html
"""<span class="caret"></span></a>"""
340 var res
= content
.open
("ul").add_class
("dropdown-menu").attr
("role", "menu").attr
("aria-labelledby", "dLabel")
341 content
.add_raw_html
"</div>"
348 # Model entity or whatever that can produce an infobox
349 interface HInfoBoxable
350 # An new infobox documenting the entity
351 fun infobox
(v
: HighlightVisitor): HInfoBox is abstract
353 # A human-readable hyper-text for the entity
354 fun linkto
: HTMLTag is abstract
358 # Append an entry for the doc in the given infobox
359 fun fill_infobox
(res
: HInfoBox)
361 if content
.length
< 2 then
362 res
.new_field
("doc").text
(content
.first
)
365 var c
= res
.new_dropdown
("doc", content
.first
)
366 for x
in content
.iterator
.skip_head
(1) do
368 c
.add_raw_html
"<br>"
380 var res
= new HInfoBox(v
, "module {name}")
382 res
.new_field
("module").add
(linkto
)
384 if mdoc
!= null then mdoc
.fill_infobox
(res
)
385 if in_importation
.greaters
.length
> 1 then
386 var c
= res
.new_dropdown
("imports", "{in_importation.greaters.length-1} modules")
387 for x
in in_importation
.greaters
do
388 if x
== self then continue
389 c
.open
("li").add x
.linkto
395 # The module HTML page
398 return c_name
+ ".html"
401 redef fun linkto
do return linkto_text
(name
)
403 # Link to the entitiy with a specific text
404 fun linkto_text
(text
: String): HTMLTag
406 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
410 redef class MClassDef
413 var res
= new HInfoBox(v
, "class {mclass.name}")
416 res
.new_field
("class").text
(mclass
.name
)
418 res
.new_field
("redef class").text
(mclass
.name
)
419 res
.new_field
("intro").add mclass
.intro
.linkto_text
("in {mclass.intro_mmodule.to_s}")
422 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
423 if mdoc
!= null then mdoc
.fill_infobox
(res
)
425 if in_hierarchy
== null then return res
427 if in_hierarchy
.greaters
.length
> 1 then
428 var c
= res
.new_dropdown
("hier", "super-classes")
429 for x
in in_hierarchy
.greaters
do
430 if x
== self then continue
431 if not x
.is_intro
then continue
432 c
.open
("li").add x
.linkto
435 if in_hierarchy
.smallers
.length
> 1 then
436 var c
= res
.new_dropdown
("hier", "sub-classes")
437 for x
in in_hierarchy
.smallers
do
438 if x
== self then continue
439 if not x
.is_intro
then continue
440 c
.open
("li").add x
.linkto
443 if mclass
.mclassdefs
.length
> 1 then
444 var c
= res
.new_dropdown
("redefs", "refinements")
445 for x
in mclass
.mclassdefs
do
446 if x
== self then continue
447 c
.open
("li").add x
.linkto_text
("in {x.mmodule}")
453 # The class HTML page (an anchor in the module page)
456 return mmodule
.href
+ "#" + to_s
459 redef fun linkto
do return linkto_text
(mclass
.name
)
461 # Link to the entitiy with a specific text
462 fun linkto_text
(text
: String): HTMLTag
464 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
471 var res
= new HInfoBox(v
, to_s
)
473 if self isa MMethodDef then
474 if msignature
!= null then res
.new_field
("fun").append
(mproperty
.name
).add msignature
.linkto
475 else if self isa MAttributeDef then
476 if static_mtype
!= null then res
.new_field
("fun").append
(mproperty
.name
).add static_mtype
.linkto
477 else if self isa MVirtualTypeDef then
478 if bound
!= null then res
.new_field
("add").append
(mproperty
.name
).add bound
.linkto
480 res
.new_field
("wat?").append
(mproperty
.name
)
485 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
488 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
489 if mdoc
!= null then mdoc
.fill_infobox
(res
)
490 if mproperty
.mpropdefs
.length
> 1 then
491 var c
= res
.new_dropdown
("redef", "redefinitions")
492 for x
in mproperty
.mpropdefs
do
493 c
.open
("li").add x
.linkto_text
("in {x.mclassdef}")
500 # The property HTML page (an anchor in the module page)
503 return self.mclassdef
.mmodule
.href
+ "#" + self.to_s
506 redef fun linkto
do return linkto_text
(mproperty
.name
)
508 # Link to the entitiy with a specific text
509 fun linkto_text
(text
: String): HTMLTag
511 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
515 redef class MClassType
518 var res
= new HInfoBox(v
, to_s
)
519 res
.href
= mclass
.intro
.href
520 res
.new_field
("class").add mclass
.intro
.linkto
521 var mdoc
= mclass
.mdoc
522 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
523 if mdoc
!= null then mdoc
.fill_infobox
(res
)
528 return mclass
.intro
.linkto
531 redef class MVirtualType
534 var res
= new HInfoBox(v
, to_s
)
535 res
.href
= mproperty
.intro
.href
538 res
.new_field
("virtual type").add pd
.linkto
540 if mdoc
!= null then mdoc
.fill_infobox
(res
)
545 return mproperty
.intro
.linkto
548 redef class MParameterType
551 var res
= new HInfoBox(v
, to_s
)
552 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
557 return (new HTMLTag("span")).text
(name
)
561 redef class MNullableType
564 return mtype
.infobox
(v
)
568 var res
= new HTMLTag("span")
569 res
.append
("nullable ").add
(mtype
.linkto
)
574 redef class MNotNullType
577 return mtype
.infobox
(v
)
581 var res
= new HTMLTag("span")
582 res
.append
("not null ").add
(mtype
.linkto
)
587 redef class MNullType
590 var res
= new HInfoBox(v
, to_s
)
595 var res
= new HTMLTag("span")
601 redef class MSignature
604 var res
= new HTMLTag("span")
606 if not mparameters
.is_empty
then
608 for p
in mparameters
do
616 res
.add p
.mtype
.linkto
620 var ret
= return_mtype
632 var res
= new HInfoBox(v
, "call {mpropdef}")
633 res
.href
= mpropdef
.href
634 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
635 if mpropdef
.is_intro
then
637 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
639 var mdoc
= mpropdef
.mdoc
640 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
641 if mdoc
!= null then mdoc
.fill_infobox
(res
)
647 return mpropdef
.linkto
655 var declared_type
= self.declared_type
656 if declared_type
== null then
657 var res
= new HInfoBox(v
, "{name}")
658 res
.new_field
("local var").append
("{name}")
661 var res
= new HInfoBox(v
, "{name}: {declared_type}")
662 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
667 return (new HTMLTag("span")).text
(name
)
675 # Optionally creates a tag that encapsulate the AST element on HTML rendering
676 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
678 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
679 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
681 #debug("no decoration for {token.inspect}")
682 #res.add_class("nc_error")
686 # Return a optional infobox
687 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
690 redef class AStdClassdef
691 redef fun make_tag
(v
)
693 var res
= new HTMLTag("span")
694 res
.add_class
("nc_cdef")
696 if md
!= null then res
.attr
("id", md
.to_s
)
699 redef fun decorate_tag
(v
, res
, token
)
701 if not token
isa TClassid then return null
702 res
.add_class
("nc_def")
705 if md
== null then return null
710 redef fun make_tag
(v
)
712 var res
= new HTMLTag("span")
713 res
.add_class
("nc_pdef")
718 res
.attr
("id", mpd
.to_s
)
720 if self isa AAttrPropdef then
722 if mpd
!= null then res
.add
(tag
(mpd
))
724 if mpd
!= null then res
.add
(tag
(mpd
))
729 private fun tag
(mpd
: MPropDef): HTMLTag
731 var a
= new HTMLTag("a")
732 a
.attr
("id", mpd
.to_s
)
738 # Produce an HTMLTag with the correct contents and CSS classes
739 # Subclasses can redefine it to decorate the tag
740 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
742 var res
= new HTMLTag("span")
748 redef class TokenKeyword
749 redef fun make_tag
(v
)
752 res
.add_class
("nc_k")
756 redef class TokenOperator
757 redef fun make_tag
(v
)
761 if p
!= null then p
.decorate_tag
(v
, res
, self)
762 res
.add_class
("nc_o")
767 redef class AVarFormExpr
768 redef fun decorate_tag
(v
, res
, token
)
770 var variable
= self.variable
771 if variable
== null then return null
772 res
.add_class
("nc_v")
773 return variable
.infobox
(v
)
777 redef class AVardeclExpr
778 redef fun decorate_tag
(v
, res
, token
)
780 var variable
= self.variable
781 if variable
== null then return null
782 res
.add_class
("nc_v")
783 return variable
.infobox
(v
)
787 redef class AForGroup
788 redef fun decorate_tag
(v
, res
, token
)
790 if not token
isa TId then return null
792 if vs
== null then return null
793 res
.add_class
("nc_v")
794 var idx
= n_ids
.index_of
(token
)
795 var variable
= vs
[idx
]
796 return variable
.infobox
(v
)
801 redef fun decorate_tag
(v
, res
, token
)
804 if mp
== null then return null
805 var variable
= self.variable
806 if variable
== null then return null
807 res
.add_class
("nc_v")
808 return variable
.infobox
(v
)
812 redef class AAssertExpr
813 redef fun decorate_tag
(v
, res
, token
)
815 res
.add_class
("nc_ast")
821 redef fun decorate_tag
(v
, res
, token
)
823 res
.add_class
("nc_la")
828 redef class ASendExpr
829 redef fun decorate_tag
(v
, res
, token
)
831 if callsite
== null then return null
832 return callsite
.infobox
(v
)
837 redef fun decorate_tag
(v
, res
, token
)
839 if callsite
== null then return null
840 return callsite
.infobox
(v
)
844 redef class AAssignOp
845 redef fun decorate_tag
(v
, res
, token
)
848 assert p
isa AReassignFormExpr
850 var callsite
= p
.reassign_callsite
851 if callsite
== null then return null
852 return callsite
.infobox
(v
)
856 redef class AModuleName
857 redef fun decorate_tag
(v
, res
, token
)
859 return parent
.decorate_tag
(v
, res
, token
)
863 redef class AModuledecl
864 redef fun decorate_tag
(v
, res
, token
)
866 res
.add_class
("nc_def")
867 res
.add_class
("nc_m")
871 if mm
== null then return null
876 redef class AStdImport
877 redef fun decorate_tag
(v
, res
, token
)
879 res
.add_class
("nc_m")
881 if mm
== null then return null
885 redef class AAttrPropdef
886 redef fun decorate_tag
(v
, res
, token
)
888 res
.add_class
("nc_def")
889 var mpd
: nullable MPropDef
891 if mpd
== null then mpd
= mpropdef
892 if mpd
== null then return null
893 return mpd
.infobox
(v
)
898 redef fun make_tag
(v
)
902 if p
!= null then p
.decorate_tag
(v
, res
, self)
903 res
.add_class
("nc_i")
908 redef fun make_tag
(v
)
910 var res
= new HTMLTag("span")
911 res
.add_class
("nc_def")
914 redef fun decorate_tag
(v
, res
, token
)
917 # nothing to decorate
922 if not p
isa AMethPropdef then return null
924 if mpd
== null then return null
925 return mpd
.infobox
(v
)
929 redef fun make_tag
(v
)
933 if p
!= null then p
.decorate_tag
(v
, res
, self)
934 res
.add_class
("nc_a")
938 redef class AAttrFormExpr
939 redef fun decorate_tag
(v
, res
, token
)
942 if p
== null then return null
943 return p
.intro
.infobox
(v
)
947 redef fun make_tag
(v
)
951 if p
!= null then p
.decorate_tag
(v
, res
, self)
952 res
.add_class
("nc_t")
957 redef fun decorate_tag
(v
, res
, token
)
960 if mt
== null then return null
962 if mt
isa MFormalType then
963 res
.add_class
("nc_vt")
968 redef class AFormaldef
969 redef fun decorate_tag
(v
, res
, token
)
971 res
.add_class
("nc_vt")
972 if mtype
== null then return null
973 return mtype
.infobox
(v
)
976 redef class ATypePropdef
977 redef fun decorate_tag
(v
, res
, token
)
979 res
.add_class
("nc_def")
981 if md
== null then return null
986 redef fun make_tag
(v
)
989 if not parent
isa ADoc then
990 res
.add_class
("nc_c")
996 redef fun make_tag
(v
)
998 var res
= new HTMLTag("span")
999 res
.add_class
("nc_d")
1003 redef class TokenLiteral
1004 redef fun make_tag
(v
)
1007 res
.add_class
("nc_l")
1009 if p
!= null then p
.decorate_tag
(v
, res
, self)
1013 redef class ASuperstringExpr
1014 redef fun make_tag
(v
)
1016 var res
= new HTMLTag("span")
1017 res
.add_class
("nc_ss")
1021 redef class AStringFormExpr
1022 redef fun decorate_tag
(v
, res
, token
)
1024 # Workaround to tag strings
1025 res
.classes
.remove
("nc_l")
1026 res
.add_class
("nc_s")
1031 redef fun decorate_tag
(v
, res
, token
)
1034 if t
== null then return null