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
= false is writable
32 # The first line to generate, null if start at the first line
33 var first_line
: nullable Int = null is writable
35 # The last line to generate, null if finish at the last line
36 var last_line
: nullable Int = null is writable
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 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
481 return (new HTMLTag("span")).text
(name
)
485 redef class MNullableType
488 return mtype
.infobox
(v
)
492 var res
= new HTMLTag("span")
493 res
.append
("nullable ").add
(mtype
.linkto
)
498 redef class MSignature
501 var res
= new HTMLTag("span")
503 if not mparameters
.is_empty
then
505 for p
in mparameters
do
513 res
.add p
.mtype
.linkto
517 var ret
= return_mtype
530 var res
= new HInfoBox(v
, "call {mpropdef}")
531 res
.href
= mpropdef
.href
532 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
533 if mpropdef
.is_intro
then
535 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
537 var mdoc
= mpropdef
.mdoc
538 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
539 if mdoc
!= null then mdoc
.fill_infobox
(res
)
545 return mpropdef
.linkto
553 var declared_type
= self.declared_type
554 if declared_type
== null then
555 var res
= new HInfoBox(v
, "{name}")
556 res
.new_field
("local var").append
("{name}")
559 var res
= new HInfoBox(v
, "{name}: {declared_type}")
560 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
565 return (new HTMLTag("span")).text
(name
)
573 # Optionally creates a tag that encapsulate the AST element on HTML rendering
574 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
576 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
577 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
579 #debug("no decoration for {token.inspect}")
580 #res.add_class("nc_error")
584 # Return a optional infobox
585 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
588 redef class AStdClassdef
589 redef fun make_tag
(v
)
591 var res
= new HTMLTag("span")
592 res
.add_class
("nc_cdef")
594 if md
!= null then res
.attr
("id", md
.to_s
)
597 redef fun decorate_tag
(v
, res
, token
)
599 res
.add_class
("nc_def")
602 if md
== null then return null
607 redef fun make_tag
(v
)
609 var res
= new HTMLTag("span")
610 res
.add_class
("nc_pdef")
615 res
.attr
("id", mpd
.to_s
)
617 if self isa AAttrPropdef then
619 if mpd
!= null then res
.add
(tag
(mpd
))
621 if mpd
!= null then res
.add
(tag
(mpd
))
626 private fun tag
(mpd
: MPropDef): HTMLTag
628 var a
= new HTMLTag("a")
629 a
.attr
("id", mpd
.to_s
)
635 # Produce an HTMLTag with the correct contents and CSS classes
636 # Subclasses can redefine it to decorate the tag
637 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
639 var res
= new HTMLTag("span")
645 redef class TokenKeyword
646 redef fun make_tag
(v
)
649 res
.add_class
("nc_k")
653 redef class TokenOperator
654 redef fun make_tag
(v
)
658 if p
!= null then p
.decorate_tag
(v
, res
, self)
659 res
.add_class
("nc_o")
664 redef class AVarFormExpr
665 redef fun decorate_tag
(v
, res
, token
)
667 var variable
= self.variable
668 if variable
== null then return null
669 res
.add_class
("nc_v")
670 return variable
.infobox
(v
)
674 redef class AVardeclExpr
675 redef fun decorate_tag
(v
, res
, token
)
677 var variable
= self.variable
678 if variable
== null then return null
679 res
.add_class
("nc_v")
680 return variable
.infobox
(v
)
685 redef fun decorate_tag
(v
, res
, token
)
687 if not token
isa TId then return null
689 if vs
== null then return null
690 res
.add_class
("nc_v")
691 var idx
= n_ids
.index_of
(token
)
692 var variable
= vs
[idx
]
693 return variable
.infobox
(v
)
698 redef fun decorate_tag
(v
, res
, token
)
701 if mp
== null then return null
702 var variable
= self.variable
703 if variable
== null then return null
704 res
.add_class
("nc_v")
705 return variable
.infobox
(v
)
709 redef class AAssertExpr
710 redef fun decorate_tag
(v
, res
, token
)
712 res
.add_class
("nc_ast")
718 redef fun decorate_tag
(v
, res
, token
)
720 res
.add_class
("nc_la")
725 redef class ASendExpr
726 redef fun decorate_tag
(v
, res
, token
)
728 if callsite
== null then return null
729 return callsite
.infobox
(v
)
734 redef fun decorate_tag
(v
, res
, token
)
736 if callsite
== null then return null
737 return callsite
.infobox
(v
)
741 redef class AAssignOp
742 redef fun decorate_tag
(v
, res
, token
)
745 assert p
isa AReassignFormExpr
747 var callsite
= p
.reassign_callsite
748 if callsite
== null then return null
749 return callsite
.infobox
(v
)
753 redef class AModuleName
754 redef fun decorate_tag
(v
, res
, token
)
756 return parent
.decorate_tag
(v
, res
, token
)
760 redef class AModuledecl
761 redef fun decorate_tag
(v
, res
, token
)
763 res
.add_class
("nc_def")
764 res
.add_class
("nc_m")
768 if mm
== null then return null
773 redef class AStdImport
774 redef fun decorate_tag
(v
, res
, token
)
776 res
.add_class
("nc_m")
778 if mm
== null then return null
782 redef class AAttrPropdef
783 redef fun decorate_tag
(v
, res
, token
)
785 res
.add_class
("nc_def")
786 var mpd
: nullable MPropDef
788 if mpd
== null then mpd
= mpropdef
789 if mpd
== null then return null
790 return mpd
.infobox
(v
)
795 redef fun make_tag
(v
)
799 if p
!= null then p
.decorate_tag
(v
, res
, self)
800 res
.add_class
("nc_i")
805 redef fun make_tag
(v
)
807 var res
= new HTMLTag("span")
808 res
.add_class
("nc_def")
811 redef fun decorate_tag
(v
, res
, token
)
814 # nothing to decorate
819 if not p
isa AMethPropdef then return null
821 if mpd
== null then return null
822 return mpd
.infobox
(v
)
826 redef fun make_tag
(v
)
830 if p
!= null then p
.decorate_tag
(v
, res
, self)
831 res
.add_class
("nc_a")
835 redef class AAttrFormExpr
836 redef fun decorate_tag
(v
, res
, token
)
839 if p
== null then return null
840 return p
.intro
.infobox
(v
)
844 redef fun make_tag
(v
)
848 if p
!= null then p
.decorate_tag
(v
, res
, self)
849 res
.add_class
("nc_t")
854 redef fun decorate_tag
(v
, res
, token
)
857 if mt
== null then return null
858 mt
= mt
.as_notnullable
859 if mt
isa MVirtualType or mt
isa MParameterType then
860 res
.add_class
("nc_vt")
865 redef class AFormaldef
866 redef fun decorate_tag
(v
, res
, token
)
868 res
.add_class
("nc_vt")
869 if mtype
== null then return null
870 return mtype
.infobox
(v
)
873 redef class ATypePropdef
874 redef fun decorate_tag
(v
, res
, token
)
876 res
.add_class
("nc_def")
878 if md
== null then return null
883 redef fun make_tag
(v
)
886 if not parent
isa ADoc then
887 res
.add_class
("nc_c")
893 redef fun make_tag
(v
)
895 var res
= new HTMLTag("span")
896 res
.add_class
("nc_d")
900 redef class TokenLiteral
901 redef fun make_tag
(v
)
904 res
.add_class
("nc_l")
906 if p
!= null then p
.decorate_tag
(v
, res
, self)
910 redef class ASuperstringExpr
911 redef fun make_tag
(v
)
913 var res
= new HTMLTag("span")
914 res
.add_class
("nc_ss")
918 redef class AStringFormExpr
919 redef fun decorate_tag
(v
, res
, token
)
921 # Workaround to tag strings
922 res
.classes
.remove
("nc_l")
923 res
.add_class
("nc_s")
928 redef fun decorate_tag
(v
, res
, token
)
931 if t
== null then return null