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
111 infobox
= pa
.decorate_tag
(hv
, tag
, anode
)
114 var messages
= anode
.location
.messages
115 if messages
!= null then
116 tag
.css
("border-bottom", "solid 2px red")
117 if infobox
== null then
118 infobox
= new HInfoBox(hv
, "Messages")
120 var c
= infobox
.new_dropdown
("{messages.length} message(s)", "")
122 c
.open
("li").append
(m
.text
)
125 if infobox
!= null then
126 tag
.attach_infobox
(infobox
)
131 # Produce HTML between two tokens
132 protected fun htmlize
(first_token
, last_token
: Token)
134 var stack2
= new Array[HTMLTag]
135 var stack
= new Array[Prod]
137 var c
: nullable Token = first_token
138 var hv
= new HighlightVisitor
142 # Handle start of line
143 var cline
= c
.location
.line_start
144 if cline
!= line
then
145 # Handle starting block productions,
146 # Because c could be a detached token, get prods in
147 # the first AST token
148 var c0
= c
.first_token_in_line
150 if c0
!= null then starting
= c0
.starting_prods
151 if starting
!= null then for p
in starting
do
152 if not p
.is_block
then continue
153 var tag
= full_tag
(p
, hv
)
154 if tag
== null then continue
155 tag
.add_class
("foldable")
162 # Add a div for the whole line
163 var tag
= new HTMLTag("span")
164 var p
= line_id_prefix
165 if p
!= "" then tag
.attrs
["id"] = "{p}{cline}"
166 tag
.classes
.add
"line"
173 # Add the blank, verbatim
174 html
.add_raw_html c
.blank_before
176 # Handle starting span production
177 starting
= c
.starting_prods
178 if starting
!= null then for p
in starting
do
179 if not p
.is_span
then continue
180 var tag
= full_tag
(p
, hv
)
181 if tag
== null then continue
192 var tag
= full_tag
(c
, hv
)
193 if tag
!= null then html
.add tag
196 # Handle ending span productions
197 var ending
= c
.ending_prods
198 if ending
!= null then for p
in ending
do
199 if not p
.is_span
then continue
200 if stack
.is_empty
or p
!= stack
.last
then continue
205 # Handle end of line and end of file
207 if c
== last_token
then n
= null
208 if n
== null or n
.location
.line_start
!= line
then
209 # closes the line div
212 # close the block production divs
213 var c0
= c
.last_token_in_line
215 if c0
!= null then ending
= c0
.ending_prods
216 if ending
!= null then for p
in ending
do
217 if not p
.is_block
then continue
218 if stack
.is_empty
or p
!= stack
.last
then continue
226 #assert stack.is_empty
227 #assert stack2.is_empty
230 # Return a default CSS content related to CSS classes used in the `html` tree.
231 # Could be inlined in the `.html` file of saved as a specific `.css` file.
232 fun css_content
: String
235 .nitcode a { color: inherit; cursor:pointer; }
236 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
237 .nitcode .foldable { display: block } /* for block productions*/
238 .nitcode .line{ display: block } /* for lines */
239 .nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
240 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
241 /* lexical raw tokens. independent of usage or semantic: */
242 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
243 .nitcode .nc_d { color: #3D8127; font-style: italic; } /* documentation comments */
244 .nitcode .nc_k { font-weight: bold; } /* keyword */
245 .nitcode .nc_o {} /* operator */
246 .nitcode .nc_i {} /* standard identifier */
247 .nitcode .nc_t { color: #445588; font-weight: bold; } /* type/class identifier */
248 .nitcode .nc_a { color: #445588; font-style: italic; } /* old style attribute identifier */
249 .nitcode .nc_l { color: #009999; } /* char and number literal */
250 .nitcode .nc_s { color: #8F1546; } /* string literal */
251 /* syntactic token usage. added because of their position in the AST */
252 .nitcode .nc_ast { color: blue; } /* assert label */
253 .nitcode .nc_la { color: blue; } /* break/continue label */
254 .nitcode .nc_m { color: #445588; } /* module name */
255 /* syntactic groups */
256 .nitcode .nc_def { font-weight: bold; color: blue; } /* name used in a definition */
257 .nitcode .nc_def.nc_a { color: blue; } /* name used in a attribute definition */
258 .nitcode .nc_def.nc_t { color: blue; } /* name used in a class or vt definition */
259 .nitcode .nc_ss { color: #9E6BEB; } /* superstrings */
260 .nitcode .nc_cdef {} /* A whole class definition */
261 .nitcode .nc_pdef {} /* A whole property definition */
262 /* semantic token usage */
263 .nitcode .nc_v { font-style: italic; } /* local variable or parameter */
264 .nitcode .nc_vt { font-style: italic; } /* virtual type or formal type */
266 .nitcode .nc_error { border: 1px red solid;} /* not used */
267 .popover { max-width: 800px !important; }
271 # Additional content to inject in the <head> tag
272 # Note: does not include `css_content`; handle it yourself.
273 fun head_content
: String
275 return """<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">\n"""
278 # Additional content to inject just before the closing </body> tag
279 fun foot_content
: String
282 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
283 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
284 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
289 # Attach the infobox to the node by using BootStrap popover
290 fun attach_infobox
(infobox
: HInfoBox)
292 classes
.add
("popupable")
293 attrs
["title"] = infobox
.title
294 var href
= infobox
.href
296 attrs
["data-title"] = """<a href="{{{href}}}">{{{infobox.title}}}</a>"""
298 attrs
["data-content"] = infobox
.content
.write_to_string
299 attrs
["data-toggle"] = "popover"
304 # A generic information container that can be used to decorate AST entities
306 # The visitor used for contextualisation, if needed
307 var visitor
: HighlightVisitor
309 # A short title for the AST element
312 # The primary link where the entity points
314 var href
: nullable String = null
316 # The content of the popuped infobox
317 var content
= new HTMLTag("div")
319 # Append a new field in the popuped infobox
320 fun new_field
(title
: String): HTMLTag
322 content
.open
("b").text
(title
)
324 var res
= content
.open
("span")
329 # Append a new dropdown in the popuped content
330 fun new_dropdown
(title
, text
: String): HTMLTag
332 content
.add_raw_html
"""<div class="dropdown"> <a data-toggle="dropdown" href="#"><b>"""
333 content
.append
(title
)
334 content
.add_raw_html
"</b> "
336 content
.add_raw_html
"""<span class="caret"></span></a>"""
337 var res
= content
.open
("ul").add_class
("dropdown-menu").attr
("role", "menu").attr
("aria-labelledby", "dLabel")
338 content
.add_raw_html
"</div>"
345 # Model entity or whatever that can produce an infobox
346 interface HInfoBoxable
347 # An new infobox documenting the entity
348 fun infobox
(v
: HighlightVisitor): HInfoBox is abstract
350 # A human-readable hyper-text for the entity
351 fun linkto
: HTMLTag is abstract
355 # Append an entry for the doc in the given infobox
356 fun fill_infobox
(res
: HInfoBox)
358 if content
.length
< 2 then
359 res
.new_field
("doc").text
(content
.first
)
362 var c
= res
.new_dropdown
("doc", content
.first
)
363 for x
in content
.iterator
.skip_head
(1) do
365 c
.add_raw_html
"<br>"
377 var res
= new HInfoBox(v
, "module {name}")
379 res
.new_field
("module").add
(linkto
)
381 if mdoc
!= null then mdoc
.fill_infobox
(res
)
382 if in_importation
.greaters
.length
> 1 then
383 var c
= res
.new_dropdown
("imports", "{in_importation.greaters.length-1} modules")
384 for x
in in_importation
.greaters
do
385 if x
== self then continue
386 c
.open
("li").add x
.linkto
392 # The module HTML page
395 return c_name
+ ".html"
398 redef fun linkto
do return linkto_text
(name
)
400 # Link to the entitiy with a specific text
401 fun linkto_text
(text
: String): HTMLTag
403 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
407 redef class MClassDef
410 var res
= new HInfoBox(v
, "class {mclass.name}")
413 res
.new_field
("class").text
(mclass
.name
)
415 res
.new_field
("redef class").text
(mclass
.name
)
416 res
.new_field
("intro").add mclass
.intro
.linkto_text
("in {mclass.intro_mmodule.to_s}")
419 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
420 if mdoc
!= null then mdoc
.fill_infobox
(res
)
422 if in_hierarchy
== null then return res
424 if in_hierarchy
.greaters
.length
> 1 then
425 var c
= res
.new_dropdown
("hier", "super-classes")
426 for x
in in_hierarchy
.greaters
do
427 if x
== self then continue
428 if not x
.is_intro
then continue
429 c
.open
("li").add x
.linkto
432 if in_hierarchy
.smallers
.length
> 1 then
433 var c
= res
.new_dropdown
("hier", "sub-classes")
434 for x
in in_hierarchy
.smallers
do
435 if x
== self then continue
436 if not x
.is_intro
then continue
437 c
.open
("li").add x
.linkto
440 if mclass
.mclassdefs
.length
> 1 then
441 var c
= res
.new_dropdown
("redefs", "refinements")
442 for x
in mclass
.mclassdefs
do
443 if x
== self then continue
444 c
.open
("li").add x
.linkto_text
("in {x.mmodule}")
450 # The class HTML page (an anchor in the module page)
453 return mmodule
.href
+ "#" + to_s
456 redef fun linkto
do return linkto_text
(mclass
.name
)
458 # Link to the entitiy with a specific text
459 fun linkto_text
(text
: String): HTMLTag
461 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
468 var res
= new HInfoBox(v
, to_s
)
470 if self isa MMethodDef then
471 if msignature
!= null then res
.new_field
("fun").append
(mproperty
.name
).add msignature
.linkto
472 else if self isa MAttributeDef then
473 if static_mtype
!= null then res
.new_field
("fun").append
(mproperty
.name
).add static_mtype
.linkto
474 else if self isa MVirtualTypeDef then
475 if bound
!= null then res
.new_field
("add").append
(mproperty
.name
).add bound
.linkto
477 res
.new_field
("wat?").append
(mproperty
.name
)
482 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
485 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
486 if mdoc
!= null then mdoc
.fill_infobox
(res
)
487 if mproperty
.mpropdefs
.length
> 1 then
488 var c
= res
.new_dropdown
("redef", "redefinitions")
489 for x
in mproperty
.mpropdefs
do
490 c
.open
("li").add x
.linkto_text
("in {x.mclassdef}")
497 # The property HTML page (an anchor in the module page)
500 return self.mclassdef
.mmodule
.href
+ "#" + self.to_s
503 redef fun linkto
do return linkto_text
(mproperty
.name
)
505 # Link to the entitiy with a specific text
506 fun linkto_text
(text
: String): HTMLTag
508 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
512 redef class MClassType
515 var res
= new HInfoBox(v
, to_s
)
516 res
.href
= mclass
.intro
.href
517 res
.new_field
("class").add mclass
.intro
.linkto
518 var mdoc
= mclass
.mdoc
519 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
520 if mdoc
!= null then mdoc
.fill_infobox
(res
)
525 return mclass
.intro
.linkto
528 redef class MVirtualType
531 var res
= new HInfoBox(v
, to_s
)
532 res
.href
= mproperty
.intro
.href
535 res
.new_field
("virtual type").add pd
.linkto
537 if mdoc
!= null then mdoc
.fill_infobox
(res
)
542 return mproperty
.intro
.linkto
545 redef class MParameterType
548 var res
= new HInfoBox(v
, to_s
)
549 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
554 return (new HTMLTag("span")).text
(name
)
558 redef class MNullableType
561 return mtype
.infobox
(v
)
565 var res
= new HTMLTag("span")
566 res
.append
("nullable ").add
(mtype
.linkto
)
571 redef class MNotNullType
574 return mtype
.infobox
(v
)
578 var res
= new HTMLTag("span")
579 res
.append
("not null ").add
(mtype
.linkto
)
584 redef class MNullType
587 var res
= new HInfoBox(v
, to_s
)
592 var res
= new HTMLTag("span")
598 redef class MSignature
601 var res
= new HTMLTag("span")
603 if not mparameters
.is_empty
then
605 for p
in mparameters
do
613 res
.add p
.mtype
.linkto
617 var ret
= return_mtype
629 var res
= new HInfoBox(v
, "call {mpropdef}")
630 res
.href
= mpropdef
.href
631 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
632 if mpropdef
.is_intro
then
634 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
636 var mdoc
= mpropdef
.mdoc
637 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
638 if mdoc
!= null then mdoc
.fill_infobox
(res
)
644 return mpropdef
.linkto
652 var declared_type
= self.declared_type
653 if declared_type
== null then
654 var res
= new HInfoBox(v
, "{name}")
655 res
.new_field
("local var").append
("{name}")
658 var res
= new HInfoBox(v
, "{name}: {declared_type}")
659 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
664 return (new HTMLTag("span")).text
(name
)
672 # Optionally creates a tag that encapsulate the AST element on HTML rendering
673 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
675 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
676 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
678 #debug("no decoration for {token.inspect}")
679 #res.add_class("nc_error")
683 # Return a optional infobox
684 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
687 redef class AStdClassdef
688 redef fun make_tag
(v
)
690 var res
= new HTMLTag("span")
691 res
.add_class
("nc_cdef")
693 if md
!= null then res
.attr
("id", md
.to_s
)
696 redef fun decorate_tag
(v
, res
, token
)
698 if not token
isa TClassid then return null
699 res
.add_class
("nc_def")
702 if md
== null then return null
707 redef fun make_tag
(v
)
709 var res
= new HTMLTag("span")
710 res
.add_class
("nc_pdef")
715 res
.attr
("id", mpd
.to_s
)
717 if self isa AAttrPropdef then
719 if mpd
!= null then res
.add
(tag
(mpd
))
721 if mpd
!= null then res
.add
(tag
(mpd
))
726 private fun tag
(mpd
: MPropDef): HTMLTag
728 var a
= new HTMLTag("a")
729 a
.attr
("id", mpd
.to_s
)
735 # Produce an HTMLTag with the correct contents and CSS classes
736 # Subclasses can redefine it to decorate the tag
737 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
739 var res
= new HTMLTag("span")
745 redef class TokenKeyword
746 redef fun make_tag
(v
)
749 res
.add_class
("nc_k")
753 redef class TokenOperator
754 redef fun make_tag
(v
)
757 res
.add_class
("nc_o")
762 redef class AVarFormExpr
763 redef fun decorate_tag
(v
, res
, token
)
765 if token
!= n_id
then return null
766 var variable
= self.variable
767 if variable
== null then return null
768 res
.add_class
("nc_v")
769 return variable
.infobox
(v
)
773 redef class AVardeclExpr
774 redef fun decorate_tag
(v
, res
, token
)
776 if token
!= n_id
then return null
777 var variable
= self.variable
778 if variable
== null then return null
779 res
.add_class
("nc_v")
780 return variable
.infobox
(v
)
784 redef class AForGroup
785 redef fun decorate_tag
(v
, res
, token
)
787 if not token
isa TId then return null
789 if vs
== null then return null
790 res
.add_class
("nc_v")
791 var idx
= n_ids
.index_of
(token
)
792 var variable
= vs
[idx
]
793 return variable
.infobox
(v
)
798 redef fun decorate_tag
(v
, res
, token
)
800 if token
!= n_id
then return null
802 if mp
== null then return null
803 var variable
= self.variable
804 if variable
== null then return null
805 res
.add_class
("nc_v")
806 return variable
.infobox
(v
)
810 redef class AAssertExpr
811 redef fun decorate_tag
(v
, res
, token
)
813 if not token
isa TId then return null
814 res
.add_class
("nc_ast")
820 redef fun decorate_tag
(v
, res
, token
)
822 if not token
isa TId then return null
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 if not token
isa TId then return null
867 res
.add_class
("nc_def")
868 res
.add_class
("nc_m")
872 if mm
== null then return null
877 redef class AStdImport
878 redef fun decorate_tag
(v
, res
, token
)
880 if not token
isa TId then return null
881 res
.add_class
("nc_m")
883 if mm
== null then return null
887 redef class AAttrPropdef
888 redef fun decorate_tag
(v
, res
, token
)
890 if not token
isa TId then return null
891 res
.add_class
("nc_def")
892 var mpd
: nullable MPropDef
894 if mpd
== null then mpd
= mpropdef
895 if mpd
== null then return null
896 return mpd
.infobox
(v
)
901 redef fun make_tag
(v
)
904 res
.add_class
("nc_i")
909 redef fun make_tag
(v
)
911 var res
= new HTMLTag("span")
912 res
.add_class
("nc_def")
915 redef fun decorate_tag
(v
, res
, token
)
918 # nothing to decorate
923 if not p
isa AMethPropdef then return null
925 if mpd
== null then return null
926 return mpd
.infobox
(v
)
930 redef fun make_tag
(v
)
933 res
.add_class
("nc_a")
937 redef class AAttrFormExpr
938 redef fun decorate_tag
(v
, res
, token
)
940 if not token
isa TAttrid then return null
942 if p
== null then return null
943 return p
.intro
.infobox
(v
)
947 redef fun make_tag
(v
)
950 res
.add_class
("nc_t")
955 redef fun decorate_tag
(v
, res
, token
)
957 if not token
isa TClassid then return null
959 if mt
== null then return null
961 if mt
isa MFormalType then
962 res
.add_class
("nc_vt")
967 redef class AFormaldef
968 redef fun decorate_tag
(v
, res
, token
)
970 if not token
isa TClassid then return null
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 if not token
isa TClassid then return null
980 res
.add_class
("nc_def")
982 if md
== null then return null
987 redef fun make_tag
(v
)
991 res
.add_class
("nc_c")
997 redef fun make_tag
(v
)
999 var res
= new HTMLTag("span")
1000 res
.add_class
("nc_d")
1004 redef class TokenLiteral
1005 redef fun make_tag
(v
)
1008 res
.add_class
("nc_l")
1012 redef class ASuperstringExpr
1013 redef fun make_tag
(v
)
1015 var res
= new HTMLTag("span")
1016 res
.add_class
("nc_ss")
1020 redef class AStringFormExpr
1021 redef fun decorate_tag
(v
, res
, token
)
1023 # Workaround to tag strings
1024 res
.classes
.remove
("nc_l")
1025 res
.add_class
("nc_s")
1030 redef fun decorate_tag
(v
, res
, token
)
1033 if t
== null then return null