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 # Highliting 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
writable = false
32 # The first line to generate, null if start at the first line
33 var first_line
: nullable Int writable = null
35 # The last line to generate, null if finish at the last line
36 var last_line
: nullable Int writable = null
40 html
.add_class
("nitcode")
43 fun enter_visit
(n
: ANode)
46 var s
= n
.location
.file
47 htmlize
(s
.first_token
.as(not null), s
.last_token
.as(not null))
50 # Produce HTML between two tokens
51 protected fun htmlize
(first_token
, last_token
: Token)
53 var stack2
= new Array[HTMLTag]
54 var stack
= new Array[Prod]
55 var closes
= new Array[Prod]
57 var c
: nullable Token = first_token
58 var hv
= new HighlightVisitor
62 # Handle start of line
63 var cline
= c
.location
.line_start
65 # Handle starting block productions,
66 # Because c could be a detached token, get prods in
68 var c0
= c
.first_token_in_line
70 if c0
!= null then starting
= c0
.starting_prods
71 if starting
!= null then for p
in starting
do
72 if not p
.is_block
then continue
73 var tag
= p
.make_tag
(hv
)
74 if tag
== null then continue
75 tag
.add_class
("foldable")
76 var infobox
= p
.infobox
(hv
)
77 if infobox
!= null then tag
.attach_infobox
(infobox
)
84 # Add a div for the whole line
85 var tag
= new HTMLTag("span")
86 tag
.attrs
["id"] = "L{cline}"
87 tag
.classes
.add
"line"
94 # Add the blank, verbatim
95 html
.add_raw_html c
.blank_before
97 # Handle starting span production
98 starting
= c
.starting_prods
99 if starting
!= null then for p
in starting
do
100 if not p
.is_span
then continue
101 var tag
= p
.make_tag
(hv
)
102 if tag
== null then continue
103 var infobox
= p
.infobox
(hv
)
104 if infobox
!= null then tag
.attach_infobox
(infobox
)
115 var tag
= c
.make_tag
(hv
)
118 if c
isa TId or c
isa TClassid or c
isa TAttrid or c
isa TokenLiteral or c
isa TokenOperator then
120 if pa
!= null then infobox
= pa
.decorate_tag
(hv
, tag
, c
)
121 else if c
isa TComment and pa
isa ADoc then
122 infobox
= pa
.decorate_tag
(hv
, tag
, c
)
124 if infobox
!= null then tag
.attach_infobox
(infobox
)
128 # Handle ending span productions
129 var ending
= c
.ending_prods
130 if ending
!= null then for p
in ending
do
131 if not p
.is_span
then continue
132 if stack
.is_empty
or p
!= stack
.last
then continue
137 # Handle end of line and end of file
139 if c
== last_token
then n
= null
140 if n
== null or n
.location
.line_start
!= line
then
141 # closes the line div
144 # close the block production divs
145 var c0
= c
.last_token_in_line
147 if c0
!= null then ending
= c0
.ending_prods
148 if ending
!= null then for p
in ending
do
149 if not p
.is_block
then continue
150 if stack
.is_empty
or p
!= stack
.last
then continue
158 assert stack
.is_empty
159 assert stack2
.is_empty
162 # Return a default CSS content related to CSS classes used in the `html` tree.
163 # Could be inlined in the `.html` file of saved as a specific `.css` file.
164 fun css_content
: String
167 .nitcode a { color: inherit; cursor:pointer; }
168 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
169 pre.nitcode .foldable { display: block } /* for block productions*/
170 pre.nitcode .line{ display: block } /* for lines */
171 pre.nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
172 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
173 /* lexical raw tokens. independent of usage or semantic: */
174 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
175 .nitcode .nc_d { color: #3D8127; font-style: italic; } /* documentation comments */
176 .nitcode .nc_k { font-weight: bold; } /* keyword */
177 .nitcode .nc_o {} /* operator */
178 .nitcode .nc_i {} /* standard identifier */
179 .nitcode .nc_t { color: #445588; font-weight: bold; } /* type/class identifier */
180 .nitcode .nc_a { color: #445588; font-style: italic; } /* old style attribute identifier */
181 .nitcode .nc_l { color: #009999; } /* char and number literal */
182 .nitcode .nc_s { color: #8F1546; } /* string literal */
183 /* syntactic token usage. added because of their position in the AST */
184 .nitcode .nc_ast { color: blue; } /* assert label */
185 .nitcode .nc_la { color: blue; } /* break/continue label */
186 .nitcode .nc_m { color: #445588; } /* module name */
187 /* syntactic groups */
188 .nitcode .nc_def { font-weight: bold; color: blue; } /* name used in a definition */
189 .nitcode .nc_def.nc_a { color: blue; } /* name used in a attribute definition */
190 .nitcode .nc_def.nc_t { color: blue; } /* name used in a class or vt definition */
191 .nitcode .nc_ss { color: #9E6BEB; } /* superstrings */
192 .nitcode .nc_cdef {} /* A whole class definition */
193 .nitcode .nc_pdef {} /* A whole property definition */
194 /* semantic token usage */
195 .nitcode .nc_v { font-style: italic; } /* local variable or parameter */
196 .nitcode .nc_vt { font-style: italic; } /* virtual type or formal type */
198 .nitcode .nc_error { border: 1px red solid;} /* not used */
199 .popover { max-width: 800px !important; }
203 # Additional content to inject in the <head> tag
204 # Note: does not include `css_content`; handle it yourself.
205 fun head_content
: String
207 return """<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">\n"""
210 # Additional content to inject just before the closing </body> tag
211 fun foot_content
: String
214 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
215 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
216 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
221 # Attach the infobox to the node by using BootStrap popover
222 fun attach_infobox
(infobox
: HInfoBox)
224 classes
.add
("popupable")
225 attrs
["title"] = infobox
.title
226 var href
= infobox
.href
228 attrs
["data-title"] = """<a href="{{{href}}}">{{{infobox.title}}}</a>"""
230 attrs
["data-content"] = infobox
.content
.write_to_string
231 attrs
["data-toggle"] = "popover"
236 # A generic information container that can be used to decorate AST entities
238 # The visitor used for contextualisation, if needed
239 var visitor
: HighlightVisitor
241 # A short title for the AST element
244 # The primary link where the entity points
246 var href
: nullable String = null
248 # The content of the popuped infobox
249 var content
= new HTMLTag("div")
251 # Append a new field in the popuped infobox
252 fun new_field
(title
: String): HTMLTag
254 content
.open
("b").text
(title
)
256 var res
= content
.open
("span")
261 # Append a new dropdown in the popuped content
262 fun new_dropdown
(title
, text
: String): HTMLTag
264 content
.add_raw_html
"""<div class="dropdown"> <a data-toggle="dropdown" href="#"><b>"""
265 content
.append
(title
)
266 content
.add_raw_html
"</b> "
268 content
.add_raw_html
"""<span class="caret"></span></a>"""
269 var res
= content
.open
("ul").add_class
("dropdown-menu").attr
("role", "menu").attr
("aria-labelledby", "dLabel")
270 content
.add_raw_html
"</div>"
277 # Model entity or whatever that can produce an infobox
278 interface HInfoBoxable
279 # An new infobox documenting the entity
280 fun infobox
(v
: HighlightVisitor): HInfoBox is abstract
282 # A human-readable hyper-text for the entity
283 fun linkto
: HTMLTag is abstract
287 # Append an entry for the doc in the given infobox
288 fun fill_infobox
(res
: HInfoBox)
290 if content
.length
< 2 then
291 res
.new_field
("doc").text
(content
.first
)
294 var c
= res
.new_dropdown
("doc", content
.first
)
295 for x
in content
.iterator
.skip_head
(1) do
297 c
.add_raw_html
"<br>"
309 var res
= new HInfoBox(v
, "module {name}")
311 res
.new_field
("module").add
(linkto
)
313 if mdoc
!= null then mdoc
.fill_infobox
(res
)
314 if in_importation
.greaters
.length
> 1 then
315 var c
= res
.new_dropdown
("imports", "{in_importation.greaters.length-1} modules")
316 for x
in in_importation
.greaters
do
317 if x
== self then continue
318 c
.open
("li").add x
.linkto
326 return name
+ ".html"
329 redef fun linkto
do return linkto_text
(name
)
331 # Link to the entitiy with a specific text
332 fun linkto_text
(text
: String): HTMLTag
334 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
338 redef class MClassDef
341 var res
= new HInfoBox(v
, "class {mclass.name}")
344 res
.new_field
("class").text
(mclass
.name
)
346 res
.new_field
("redef class").text
(mclass
.name
)
347 res
.new_field
("intro").add mclass
.intro
.linkto_text
("in {mclass.intro.mmodule.to_s}")
350 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
351 if mdoc
!= null then mdoc
.fill_infobox
(res
)
353 if in_hierarchy
.greaters
.length
> 1 then
354 var c
= res
.new_dropdown
("hier", "super-classes")
355 for x
in in_hierarchy
.greaters
do
356 if x
== self then continue
357 if not x
.is_intro
then continue
358 c
.open
("li").add x
.linkto
361 if in_hierarchy
.smallers
.length
> 1 then
362 var c
= res
.new_dropdown
("hier", "sub-classes")
363 for x
in in_hierarchy
.smallers
do
364 if x
== self then continue
365 if not x
.is_intro
then continue
366 c
.open
("li").add x
.linkto
369 if mclass
.mclassdefs
.length
> 1 then
370 var c
= res
.new_dropdown
("redefs", "refinements")
371 for x
in mclass
.mclassdefs
do
372 if x
== self then continue
373 c
.open
("li").add x
.linkto_text
("in {x.mmodule}")
381 return mmodule
.href
+ "#" + to_s
384 redef fun linkto
do return linkto_text
(mclass
.name
)
386 # Link to the entitiy with a specific text
387 fun linkto_text
(text
: String): HTMLTag
389 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
396 var res
= new HInfoBox(v
, to_s
)
398 if self isa MMethodDef then
399 res
.new_field
("fun").append
(mproperty
.name
).add msignature
.linkto
400 else if self isa MAttributeDef then
401 res
.new_field
("fun").append
(mproperty
.name
).add static_mtype
.linkto
402 else if self isa MVirtualTypeDef then
403 res
.new_field
("add").append
(mproperty
.name
).add bound
.linkto
405 res
.new_field
("wat?").append
(mproperty
.name
)
410 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
413 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
414 if mdoc
!= null then mdoc
.fill_infobox
(res
)
415 if mproperty
.mpropdefs
.length
> 1 then
416 var c
= res
.new_dropdown
("redef", "redefinitions")
417 for x
in mproperty
.mpropdefs
do
418 c
.open
("li").add x
.linkto_text
("in {x.mclassdef}")
427 return self.mclassdef
.mmodule
.href
+ "#" + self.to_s
430 redef fun linkto
do return linkto_text
(mproperty
.name
)
432 # Link to the entitiy with a specific text
433 fun linkto_text
(text
: String): HTMLTag
435 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
439 redef class MClassType
442 var res
= new HInfoBox(v
, to_s
)
443 res
.href
= mclass
.intro
.href
444 res
.new_field
("class").add mclass
.intro
.linkto
445 var mdoc
= mclass
.mdoc
446 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
447 if mdoc
!= null then mdoc
.fill_infobox
(res
)
452 return mclass
.intro
.linkto
455 redef class MVirtualType
458 var res
= new HInfoBox(v
, to_s
)
459 res
.href
= mproperty
.intro
.href
462 res
.new_field
("virtual type").add pd
.linkto
464 if mdoc
!= null then mdoc
.fill_infobox
(res
)
469 return mproperty
.intro
.linkto
472 redef class MParameterType
475 var res
= new HInfoBox(v
, to_s
)
476 var name
= mclass
.intro
.parameter_names
[rank
]
477 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
482 var name
= mclass
.intro
.parameter_names
[rank
]
483 return (new HTMLTag("span")).text
(name
)
487 redef class MNullableType
490 return mtype
.infobox
(v
)
494 var res
= new HTMLTag("span")
495 res
.append
("nullable ").add
(mtype
.linkto
)
500 redef class MSignature
503 var res
= new HTMLTag("span")
505 if not mparameters
.is_empty
then
507 for p
in mparameters
do
515 res
.add p
.mtype
.linkto
519 var ret
= return_mtype
532 var res
= new HInfoBox(v
, "call {mpropdef}")
533 res
.href
= mpropdef
.href
534 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
535 if mpropdef
.is_intro
then
537 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
539 var mdoc
= mpropdef
.mdoc
540 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
541 if mdoc
!= null then mdoc
.fill_infobox
(res
)
547 return mpropdef
.linkto
555 var declared_type
= self.declared_type
556 if declared_type
== null then
557 var res
= new HInfoBox(v
, "{name}")
558 res
.new_field
("local var").append
("{name}")
561 var res
= new HInfoBox(v
, "{name}: {declared_type}")
562 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
567 return (new HTMLTag("span")).text
(name
)
575 # Optionally creates a tag that encapsulate the AST element on HTML rendering
576 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
578 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
579 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
581 #debug("no decoration for {token.inspect}")
582 #res.add_class("nc_error")
586 # Return a optional infobox
587 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
590 redef class AStdClassdef
591 redef fun make_tag
(v
)
593 var res
= new HTMLTag("span")
594 res
.add_class
("nc_cdef")
596 if md
!= null then res
.attr
("id", md
.to_s
)
599 redef fun decorate_tag
(v
, res
, token
)
601 res
.add_class
("nc_def")
604 if md
== null then return null
609 redef fun make_tag
(v
)
611 var res
= new HTMLTag("span")
612 res
.add_class
("nc_pdef")
617 res
.attr
("id", mpd
.to_s
)
619 if self isa AAttrPropdef then
621 if mpd
!= null then res
.add
(tag
(mpd
))
623 if mpd
!= null then res
.add
(tag
(mpd
))
628 private fun tag
(mpd
: MPropDef): HTMLTag
630 var a
= new HTMLTag("a")
631 a
.attr
("id", mpd
.to_s
)
637 # Produce an HTMLTag with the correct contents and CSS classes
638 # Subclasses can redefine it to decorate the tag
639 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
641 var res
= new HTMLTag("span")
647 redef class TokenKeyword
648 redef fun make_tag
(v
)
651 res
.add_class
("nc_k")
655 redef class TokenOperator
656 redef fun make_tag
(v
)
660 if p
!= null then p
.decorate_tag
(v
, res
, self)
661 res
.add_class
("nc_o")
666 redef class AVarFormExpr
667 redef fun decorate_tag
(v
, res
, token
)
669 var variable
= self.variable
670 if variable
== null then return null
671 res
.add_class
("nc_v")
672 return variable
.infobox
(v
)
676 redef class AVardeclExpr
677 redef fun decorate_tag
(v
, res
, token
)
679 var variable
= self.variable
680 if variable
== null then return null
681 res
.add_class
("nc_v")
682 return variable
.infobox
(v
)
687 redef fun decorate_tag
(v
, res
, token
)
689 if not token
isa TId then return null
691 if vs
== null then return null
692 res
.add_class
("nc_v")
693 var idx
= n_ids
.index_of
(token
)
694 var variable
= vs
[idx
]
695 return variable
.infobox
(v
)
700 redef fun decorate_tag
(v
, res
, token
)
703 if mp
== null then return null
704 var variable
= self.variable
705 if variable
== null then return null
706 res
.add_class
("nc_v")
707 return variable
.infobox
(v
)
711 redef class AAssertExpr
712 redef fun decorate_tag
(v
, res
, token
)
714 res
.add_class
("nc_ast")
720 redef fun decorate_tag
(v
, res
, token
)
722 res
.add_class
("nc_la")
727 redef class ASendExpr
728 redef fun decorate_tag
(v
, res
, token
)
730 if callsite
== null then return null
731 return callsite
.infobox
(v
)
736 redef fun decorate_tag
(v
, res
, token
)
738 if callsite
== null then return null
739 return callsite
.infobox
(v
)
743 redef class AAssignOp
744 redef fun decorate_tag
(v
, res
, token
)
747 assert p
isa AReassignFormExpr
749 var callsite
= p
.reassign_callsite
750 if callsite
== null then return null
751 return callsite
.infobox
(v
)
755 redef class AModuleName
756 redef fun decorate_tag
(v
, res
, token
)
758 return parent
.decorate_tag
(v
, res
, token
)
762 redef class AModuledecl
763 redef fun decorate_tag
(v
, res
, token
)
765 res
.add_class
("nc_def")
766 res
.add_class
("nc_m")
770 if mm
== null then return null
775 redef class AStdImport
776 redef fun decorate_tag
(v
, res
, token
)
778 res
.add_class
("nc_m")
780 if mm
== null then return null
784 redef class AAttrPropdef
785 redef fun decorate_tag
(v
, res
, token
)
787 res
.add_class
("nc_def")
788 var mpd
: nullable MPropDef
790 if mpd
== null then mpd
= mpropdef
791 if mpd
== null then return null
792 return mpd
.infobox
(v
)
797 redef fun make_tag
(v
)
801 if p
!= null then p
.decorate_tag
(v
, res
, self)
802 res
.add_class
("nc_i")
807 redef fun make_tag
(v
)
809 var res
= new HTMLTag("span")
810 res
.add_class
("nc_def")
813 redef fun decorate_tag
(v
, res
, token
)
816 # nothing to decorate
821 if not p
isa AMethPropdef then return null
823 if mpd
== null then return null
824 return mpd
.infobox
(v
)
828 redef fun make_tag
(v
)
832 if p
!= null then p
.decorate_tag
(v
, res
, self)
833 res
.add_class
("nc_a")
837 redef class AAttrFormExpr
838 redef fun decorate_tag
(v
, res
, token
)
841 if p
== null then return null
842 return p
.intro
.infobox
(v
)
846 redef fun make_tag
(v
)
850 if p
!= null then p
.decorate_tag
(v
, res
, self)
851 res
.add_class
("nc_t")
856 redef fun decorate_tag
(v
, res
, token
)
859 if mt
== null then return null
860 mt
= mt
.as_notnullable
861 if mt
isa MVirtualType or mt
isa MParameterType then
862 res
.add_class
("nc_vt")
867 redef class AFormaldef
868 redef fun decorate_tag
(v
, res
, token
)
870 res
.add_class
("nc_vt")
871 if mtype
== null then return null
872 return mtype
.infobox
(v
)
875 redef class ATypePropdef
876 redef fun decorate_tag
(v
, res
, token
)
878 res
.add_class
("nc_def")
880 if md
== null then return null
885 redef fun make_tag
(v
)
888 if not parent
isa ADoc then
889 res
.add_class
("nc_c")
895 redef fun make_tag
(v
)
897 var res
= new HTMLTag("span")
898 res
.add_class
("nc_d")
902 redef class TokenLiteral
903 redef fun make_tag
(v
)
906 res
.add_class
("nc_l")
908 if p
!= null then p
.decorate_tag
(v
, res
, self)
912 redef class ASuperstringExpr
913 redef fun make_tag
(v
)
915 var res
= new HTMLTag("span")
916 res
.add_class
("nc_ss")
920 redef class AStringFormExpr
921 redef fun decorate_tag
(v
, res
, token
)
923 # Workaround to tag strings
924 res
.classes
.remove
("nc_l")
925 res
.add_class
("nc_s")
930 redef fun decorate_tag
(v
, res
, token
)
933 if t
== null then return null