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
46 # When highlighting a node, show its messages (errors, warnings), if any.
49 var show_messages
= true is writable
51 # When highlighting a node, attach a full popupable infobox, if any.
53 # If `false`, only a simple `title` tooltip is used.
56 var show_infobox
= true is writable
60 html
.add_class
("nitcode")
63 # When highlighting a node, also consider the loose tokens around it.
65 # Loose tokens are tokens discarded from the AST but attached before
66 # or after some non-loose tokens. See `Token::is_loose`.
68 # When this flag is set to `true`, the loose tokens that are before the
69 # first token and after the last token are also highlighted.
72 var include_loose_tokens
= false is writable
74 # When highlighting a node, the first and the last lines are fully included.
76 # If the highlighted node starts (or ends) in the middle of a line,
77 # this flags forces the whole line to be highlighted.
80 var include_whole_lines
= false is writable
82 # The entry-point of the highlighting.
83 # Will fill `html` with the generated HTML content.
84 fun enter_visit
(n
: ANode)
97 if f
== null then return
99 if l
== null then return
102 if include_loose_tokens
then
103 if f
.prev_looses
.not_empty
then f
= f
.prev_looses
.first
104 if l
.next_looses
.not_empty
then l
= l
.next_looses
.last
107 if include_whole_lines
then
108 f
= f
.first_real_token_in_line
109 l
= l
.last_real_token_in_line
115 private fun full_tag
(anode
: ANode, hv
: HighlightVisitor): nullable HTMLTag
117 var tag
= anode
.make_tag
(hv
)
118 if tag
== null then return null
119 var infobox
= anode
.infobox
(hv
)
120 if infobox
== null and anode
isa Token then
121 var pa
= anode
.parent
123 infobox
= pa
.decorate_tag
(hv
, tag
, anode
)
126 if infobox
!= null and not show_infobox
then
127 tag
.attr
("title", infobox
.title
)
128 tag
.classes
.add
"titled"
131 var messages
= anode
.location
.messages
132 if messages
!= null and show_messages
then
133 tag
.css
("border-bottom", "solid 2px red")
134 if infobox
== null then
135 infobox
= new HInfoBox(hv
, "Messages")
137 var c
= infobox
.new_dropdown
("{messages.length} message(s)", "")
139 c
.open
("li").append
(m
.text
)
142 if infobox
!= null then
143 tag
.attach_infobox
(infobox
)
148 # Produce HTML between two tokens
149 protected fun htmlize
(first_token
, last_token
: Token)
151 var stack2
= new Array[HTMLTag]
152 var stack
= new Array[Prod]
154 var c
: nullable Token = first_token
155 var hv
= new HighlightVisitor
159 # Handle start of line
160 var cline
= c
.location
.line_start
161 if cline
!= line
then
162 # Handle starting block productions,
163 # Because c could be a detached token, get prods in
164 # the first AST token
165 var c0
= c
.first_token_in_line
167 if c0
!= null then starting
= c0
.starting_prods
168 if starting
!= null then for p
in starting
do
169 if not p
.is_block
then continue
170 var tag
= full_tag
(p
, hv
)
171 if tag
== null then continue
172 tag
.add_class
("foldable")
179 # Add a div for the whole line
180 var tag
= new HTMLTag("span")
181 var p
= line_id_prefix
182 if p
!= "" then tag
.attrs
["id"] = "{p}{cline}"
183 tag
.classes
.add
"line"
190 # Add the blank, verbatim
191 html
.add_raw_html c
.blank_before
193 # Handle starting span production
194 starting
= c
.starting_prods
195 if starting
!= null then for p
in starting
do
196 if not p
.is_span
then continue
197 var tag
= full_tag
(p
, hv
)
198 if tag
== null then continue
209 var tag
= full_tag
(c
, hv
)
210 if tag
!= null then html
.add tag
213 # Handle ending span productions
214 var ending
= c
.ending_prods
215 if ending
!= null then for p
in ending
do
216 if not p
.is_span
then continue
217 if stack
.is_empty
or p
!= stack
.last
then continue
222 # Handle end of line and end of file
224 if c
== last_token
then n
= null
225 if n
== null or n
.location
.line_start
!= line
then
226 # closes the line div
229 # close the block production divs
230 var c0
= c
.last_token_in_line
232 if c0
!= null then ending
= c0
.ending_prods
233 if ending
!= null then for p
in ending
do
234 if not p
.is_block
then continue
235 if stack
.is_empty
or p
!= stack
.last
then continue
243 #assert stack.is_empty
244 #assert stack2.is_empty
247 # Return a default CSS content related to CSS classes used in the `html` tree.
248 # Could be inlined in the `.html` file of saved as a specific `.css` file.
249 fun css_content
: String
252 .nitcode a { color: inherit; cursor:pointer; }
253 .nitcode .titled:hover { text-decoration: underline; } /* underline titles */
254 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
255 .nitcode .foldable { display: block } /* for block productions*/
256 .nitcode .line{ display: block } /* for lines */
257 .nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
258 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
259 /* lexical raw tokens. independent of usage or semantic: */
260 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
261 .nitcode .nc_d { color: #3D8127; font-style: italic; } /* documentation comments */
262 .nitcode .nc_k { font-weight: bold; } /* keyword */
263 .nitcode .nc_o {} /* operator */
264 .nitcode .nc_i {} /* standard identifier */
265 .nitcode .nc_t { color: #445588; font-weight: bold; } /* type/class identifier */
266 .nitcode .nc_a { color: #445588; font-style: italic; } /* old style attribute identifier */
267 .nitcode .nc_l { color: #009999; } /* char and number literal */
268 .nitcode .nc_s { color: #8F1546; } /* string literal */
269 /* syntactic token usage. added because of their position in the AST */
270 .nitcode .nc_ast { color: blue; } /* assert label */
271 .nitcode .nc_la { color: blue; } /* break/continue label */
272 .nitcode .nc_m { color: #445588; } /* module name */
273 /* syntactic groups */
274 .nitcode .nc_def { font-weight: bold; color: blue; } /* name used in a definition */
275 .nitcode .nc_def.nc_a { color: blue; } /* name used in a attribute definition */
276 .nitcode .nc_def.nc_t { color: blue; } /* name used in a class or vt definition */
277 .nitcode .nc_ss { color: #9E6BEB; } /* superstrings */
278 .nitcode .nc_cdef {} /* A whole class definition */
279 .nitcode .nc_pdef {} /* A whole property definition */
280 /* semantic token usage */
281 .nitcode .nc_v { font-style: italic; } /* local variable or parameter */
282 .nitcode .nc_vt { font-style: italic; } /* virtual type or formal type */
284 .nitcode .nc_error { border: 1px red solid;} /* not used */
285 .popover { max-width: 800px !important; }
289 # Additional content to inject in the <head> tag
290 # Note: does not include `css_content`; handle it yourself.
291 fun head_content
: String
293 return """<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">\n"""
296 # Additional content to inject just before the closing </body> tag
297 fun foot_content
: String
300 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
301 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
302 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
307 # Attach the infobox to the node by using BootStrap popover
308 fun attach_infobox
(infobox
: HInfoBox)
310 classes
.add
("popupable")
311 attrs
["title"] = infobox
.title
312 var href
= infobox
.href
314 attrs
["data-title"] = """<a href="{{{href}}}">{{{infobox.title}}}</a>"""
316 attrs
["data-content"] = infobox
.content
.write_to_string
317 attrs
["data-toggle"] = "popover"
322 # A generic information container that can be used to decorate AST entities
324 # The visitor used for contextualisation, if needed
325 var visitor
: HighlightVisitor
327 # A short title for the AST element
330 # The primary link where the entity points
332 var href
: nullable String = null
334 # The content of the popuped infobox
335 var content
= new HTMLTag("div")
337 # Append a new field in the popuped infobox
338 fun new_field
(title
: String): HTMLTag
340 content
.open
("b").text
(title
)
342 var res
= content
.open
("span")
347 # Append a new dropdown in the popuped content
348 fun new_dropdown
(title
, text
: String): HTMLTag
350 content
.add_raw_html
"""<div class="dropdown"> <a data-toggle="dropdown" href="#"><b>"""
351 content
.append
(title
)
352 content
.add_raw_html
"</b> "
354 content
.add_raw_html
"""<span class="caret"></span></a>"""
355 var res
= content
.open
("ul").add_class
("dropdown-menu").attr
("role", "menu").attr
("aria-labelledby", "dLabel")
356 content
.add_raw_html
"</div>"
363 # Model entity or whatever that can produce an infobox
364 interface HInfoBoxable
365 # An new infobox documenting the entity
366 fun infobox
(v
: HighlightVisitor): HInfoBox is abstract
368 # A human-readable hyper-text for the entity
369 fun linkto
: HTMLTag is abstract
373 # Append an entry for the doc in the given infobox
374 fun fill_infobox
(res
: HInfoBox)
376 if content
.length
< 2 then
377 res
.new_field
("doc").text
(content
.first
)
380 var c
= res
.new_dropdown
("doc", content
.first
)
381 for x
in content
.iterator
.skip_head
(1) do
383 c
.add_raw_html
"<br>"
395 var res
= new HInfoBox(v
, "module {name}")
397 res
.new_field
("module").add
(linkto
)
399 if mdoc
!= null then mdoc
.fill_infobox
(res
)
400 if in_importation
.greaters
.length
> 1 then
401 var c
= res
.new_dropdown
("imports", "{in_importation.greaters.length-1} modules")
402 for x
in in_importation
.greaters
do
403 if x
== self then continue
404 c
.open
("li").add x
.linkto
410 # The module HTML page
413 return c_name
+ ".html"
416 redef fun linkto
do return linkto_text
(name
)
418 # Link to the entitiy with a specific text
419 fun linkto_text
(text
: String): HTMLTag
421 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
425 redef class MClassDef
428 var res
= new HInfoBox(v
, "class {mclass.name}")
431 res
.new_field
("class").text
(mclass
.name
)
433 res
.new_field
("redef class").text
(mclass
.name
)
434 res
.new_field
("intro").add mclass
.intro
.linkto_text
("in {mclass.intro_mmodule.to_s}")
437 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
438 if mdoc
!= null then mdoc
.fill_infobox
(res
)
440 if in_hierarchy
== null then return res
442 if in_hierarchy
.greaters
.length
> 1 then
443 var c
= res
.new_dropdown
("hier", "super-classes")
444 for x
in in_hierarchy
.greaters
do
445 if x
== self then continue
446 if not x
.is_intro
then continue
447 c
.open
("li").add x
.linkto
450 if in_hierarchy
.smallers
.length
> 1 then
451 var c
= res
.new_dropdown
("hier", "sub-classes")
452 for x
in in_hierarchy
.smallers
do
453 if x
== self then continue
454 if not x
.is_intro
then continue
455 c
.open
("li").add x
.linkto
458 if mclass
.mclassdefs
.length
> 1 then
459 var c
= res
.new_dropdown
("redefs", "refinements")
460 for x
in mclass
.mclassdefs
do
461 if x
== self then continue
462 c
.open
("li").add x
.linkto_text
("in {x.mmodule}")
468 # The class HTML page (an anchor in the module page)
471 return mmodule
.href
+ "#" + to_s
474 redef fun linkto
do return linkto_text
(mclass
.name
)
476 # Link to the entitiy with a specific text
477 fun linkto_text
(text
: String): HTMLTag
479 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
486 var res
= new HInfoBox(v
, to_s
)
488 if self isa MMethodDef then
489 if msignature
!= null then res
.new_field
("fun").append
(mproperty
.name
).add msignature
.linkto
490 else if self isa MAttributeDef then
491 if static_mtype
!= null then res
.new_field
("fun").append
(mproperty
.name
).add static_mtype
.linkto
492 else if self isa MVirtualTypeDef then
493 if bound
!= null then res
.new_field
("add").append
(mproperty
.name
).add bound
.linkto
495 res
.new_field
("wat?").append
(mproperty
.name
)
500 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
503 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
504 if mdoc
!= null then mdoc
.fill_infobox
(res
)
505 if mproperty
.mpropdefs
.length
> 1 then
506 var c
= res
.new_dropdown
("redef", "redefinitions")
507 for x
in mproperty
.mpropdefs
do
508 c
.open
("li").add x
.linkto_text
("in {x.mclassdef}")
515 # The property HTML page (an anchor in the module page)
518 return self.mclassdef
.mmodule
.href
+ "#" + self.to_s
521 redef fun linkto
do return linkto_text
(mproperty
.name
)
523 # Link to the entitiy with a specific text
524 fun linkto_text
(text
: String): HTMLTag
526 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
530 redef class MClassType
533 var res
= new HInfoBox(v
, to_s
)
534 res
.href
= mclass
.intro
.href
535 res
.new_field
("class").add mclass
.intro
.linkto
536 var mdoc
= mclass
.mdoc
537 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
538 if mdoc
!= null then mdoc
.fill_infobox
(res
)
543 return mclass
.intro
.linkto
546 redef class MVirtualType
549 var res
= new HInfoBox(v
, to_s
)
550 res
.href
= mproperty
.intro
.href
553 res
.new_field
("virtual type").add pd
.linkto
555 if mdoc
!= null then mdoc
.fill_infobox
(res
)
560 return mproperty
.intro
.linkto
563 redef class MParameterType
566 var res
= new HInfoBox(v
, to_s
)
567 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
572 return (new HTMLTag("span")).text
(name
)
576 redef class MNullableType
579 return mtype
.infobox
(v
)
583 var res
= new HTMLTag("span")
584 res
.append
("nullable ").add
(mtype
.linkto
)
589 redef class MNotNullType
592 return mtype
.infobox
(v
)
596 var res
= new HTMLTag("span")
597 res
.append
("not null ").add
(mtype
.linkto
)
602 redef class MNullType
605 var res
= new HInfoBox(v
, to_s
)
610 var res
= new HTMLTag("span")
616 redef class MSignature
619 var res
= new HTMLTag("span")
621 if not mparameters
.is_empty
then
623 for p
in mparameters
do
631 res
.add p
.mtype
.linkto
635 var ret
= return_mtype
647 var res
= new HInfoBox(v
, "call {mpropdef}")
648 res
.href
= mpropdef
.href
649 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
650 if mpropdef
.is_intro
then
652 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
654 var mdoc
= mpropdef
.mdoc
655 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
656 if mdoc
!= null then mdoc
.fill_infobox
(res
)
662 return mpropdef
.linkto
670 var declared_type
= self.declared_type
671 if declared_type
== null then
672 var res
= new HInfoBox(v
, "{name}")
673 res
.new_field
("local var").append
("{name}")
676 var res
= new HInfoBox(v
, "{name}: {declared_type}")
677 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
682 return (new HTMLTag("span")).text
(name
)
690 # Optionally creates a tag that encapsulate the AST element on HTML rendering
691 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
693 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
694 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
696 #debug("no decoration for {token.inspect}")
697 #res.add_class("nc_error")
701 # Return a optional infobox
702 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
705 redef class AStdClassdef
706 redef fun make_tag
(v
)
708 var res
= new HTMLTag("span")
709 res
.add_class
("nc_cdef")
711 if md
!= null then res
.attr
("id", md
.to_s
)
714 redef fun decorate_tag
(v
, res
, token
)
716 if not token
isa TClassid then return null
717 res
.add_class
("nc_def")
720 if md
== null then return null
725 redef fun make_tag
(v
)
727 var res
= new HTMLTag("span")
728 res
.add_class
("nc_pdef")
733 res
.attr
("id", mpd
.to_s
)
735 if self isa AAttrPropdef then
737 if mpd
!= null then res
.add
(tag
(mpd
))
739 if mpd
!= null then res
.add
(tag
(mpd
))
744 private fun tag
(mpd
: MPropDef): HTMLTag
746 var a
= new HTMLTag("a")
747 a
.attr
("id", mpd
.to_s
)
753 # Produce an HTMLTag with the correct contents and CSS classes
754 # Subclasses can redefine it to decorate the tag
755 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
757 var res
= new HTMLTag("span")
763 redef class TokenKeyword
764 redef fun make_tag
(v
)
767 res
.add_class
("nc_k")
771 redef class TokenOperator
772 redef fun make_tag
(v
)
775 res
.add_class
("nc_o")
780 redef class AVarFormExpr
781 redef fun decorate_tag
(v
, res
, token
)
783 if token
!= n_id
then return null
784 var variable
= self.variable
785 if variable
== null then return null
786 res
.add_class
("nc_v")
787 return variable
.infobox
(v
)
791 redef class AVardeclExpr
792 redef fun decorate_tag
(v
, res
, token
)
794 if token
!= n_id
then return null
795 var variable
= self.variable
796 if variable
== null then return null
797 res
.add_class
("nc_v")
798 return variable
.infobox
(v
)
802 redef class AForGroup
803 redef fun decorate_tag
(v
, res
, token
)
805 if not token
isa TId then return null
807 if vs
== null then return null
808 res
.add_class
("nc_v")
809 var idx
= n_ids
.index_of
(token
)
810 var variable
= vs
[idx
]
811 return variable
.infobox
(v
)
816 redef fun decorate_tag
(v
, res
, token
)
818 if token
!= n_id
then return null
820 if mp
== null then return null
821 var variable
= self.variable
822 if variable
== null then return null
823 res
.add_class
("nc_v")
824 return variable
.infobox
(v
)
828 redef class AAssertExpr
829 redef fun decorate_tag
(v
, res
, token
)
831 if not token
isa TId then return null
832 res
.add_class
("nc_ast")
838 redef fun decorate_tag
(v
, res
, token
)
840 if not token
isa TId then return null
841 res
.add_class
("nc_la")
846 redef class ASendExpr
847 redef fun decorate_tag
(v
, res
, token
)
849 if callsite
== null then return null
850 return callsite
.infobox
(v
)
855 redef fun decorate_tag
(v
, res
, token
)
857 if callsite
== null then return null
858 return callsite
.infobox
(v
)
862 redef class AAssignOp
863 redef fun decorate_tag
(v
, res
, token
)
866 assert p
isa AReassignFormExpr
868 var callsite
= p
.reassign_callsite
869 if callsite
== null then return null
870 return callsite
.infobox
(v
)
874 redef class AModuleName
875 redef fun decorate_tag
(v
, res
, token
)
877 return parent
.decorate_tag
(v
, res
, token
)
881 redef class AModuledecl
882 redef fun decorate_tag
(v
, res
, token
)
884 if not token
isa TId then return null
885 res
.add_class
("nc_def")
886 res
.add_class
("nc_m")
890 if mm
== null then return null
895 redef class AStdImport
896 redef fun decorate_tag
(v
, res
, token
)
898 if not token
isa TId then return null
899 res
.add_class
("nc_m")
901 if mm
== null then return null
905 redef class AAttrPropdef
906 redef fun decorate_tag
(v
, res
, token
)
908 if not token
isa TId then return null
909 res
.add_class
("nc_def")
910 var mpd
: nullable MPropDef
912 if mpd
== null then mpd
= mpropdef
913 if mpd
== null then return null
914 return mpd
.infobox
(v
)
919 redef fun make_tag
(v
)
922 res
.add_class
("nc_i")
927 redef fun make_tag
(v
)
929 var res
= new HTMLTag("span")
930 res
.add_class
("nc_def")
933 redef fun decorate_tag
(v
, res
, token
)
936 # nothing to decorate
941 if not p
isa AMethPropdef then return null
943 if mpd
== null then return null
944 return mpd
.infobox
(v
)
948 redef fun make_tag
(v
)
951 res
.add_class
("nc_a")
955 redef class AAttrFormExpr
956 redef fun decorate_tag
(v
, res
, token
)
958 if not token
isa TAttrid then return null
960 if p
== null then return null
961 return p
.intro
.infobox
(v
)
965 redef fun make_tag
(v
)
968 res
.add_class
("nc_t")
973 redef fun decorate_tag
(v
, res
, token
)
975 if not token
isa TClassid then return null
977 if mt
== null then return null
979 if mt
isa MFormalType then
980 res
.add_class
("nc_vt")
985 redef class AFormaldef
986 redef fun decorate_tag
(v
, res
, token
)
988 if not token
isa TClassid then return null
989 res
.add_class
("nc_vt")
990 if mtype
== null then return null
991 return mtype
.infobox
(v
)
994 redef class ATypePropdef
995 redef fun decorate_tag
(v
, res
, token
)
997 if not token
isa TClassid then return null
998 res
.add_class
("nc_def")
1000 if md
== null then return null
1001 return md
.infobox
(v
)
1004 redef class TComment
1005 redef fun make_tag
(v
)
1009 res
.add_class
("nc_c")
1015 redef fun make_tag
(v
)
1017 var res
= new HTMLTag("span")
1018 res
.add_class
("nc_d")
1022 redef class TokenLiteral
1023 redef fun make_tag
(v
)
1026 res
.add_class
("nc_l")
1030 redef class ASuperstringExpr
1031 redef fun make_tag
(v
)
1033 var res
= new HTMLTag("span")
1034 res
.add_class
("nc_ss")
1038 redef class AStringFormExpr
1039 redef fun decorate_tag
(v
, res
, token
)
1041 # Workaround to tag strings
1042 res
.classes
.remove
("nc_l")
1043 res
.add_class
("nc_s")
1048 redef fun decorate_tag
(v
, res
, token
)
1051 if t
== null then return null