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 # 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 fun enter_visit
(n
: ANode)
54 var s
= n
.location
.file
55 htmlize
(s
.first_token
.as(not null), s
.last_token
.as(not null))
58 # Produce HTML between two tokens
59 protected fun htmlize
(first_token
, last_token
: Token)
61 var stack2
= new Array[HTMLTag]
62 var stack
= new Array[Prod]
64 var c
: nullable Token = first_token
65 var hv
= new HighlightVisitor
69 # Handle start of line
70 var cline
= c
.location
.line_start
72 # Handle starting block productions,
73 # Because c could be a detached token, get prods in
75 var c0
= c
.first_token_in_line
77 if c0
!= null then starting
= c0
.starting_prods
78 if starting
!= null then for p
in starting
do
79 if not p
.is_block
then continue
80 var tag
= p
.make_tag
(hv
)
81 if tag
== null then continue
82 tag
.add_class
("foldable")
83 var infobox
= p
.infobox
(hv
)
84 if infobox
!= null then tag
.attach_infobox
(infobox
)
91 # Add a div for the whole line
92 var tag
= new HTMLTag("span")
93 var p
= line_id_prefix
94 if p
!= "" then tag
.attrs
["id"] = "{p}{cline}"
95 tag
.classes
.add
"line"
102 # Add the blank, verbatim
103 html
.add_raw_html c
.blank_before
105 # Handle starting span production
106 starting
= c
.starting_prods
107 if starting
!= null then for p
in starting
do
108 if not p
.is_span
then continue
109 var tag
= p
.make_tag
(hv
)
110 if tag
== null then continue
111 var infobox
= p
.infobox
(hv
)
112 if infobox
!= null then tag
.attach_infobox
(infobox
)
123 var tag
= c
.make_tag
(hv
)
126 if c
isa TId or c
isa TClassid or c
isa TAttrid or c
isa TokenLiteral or c
isa TokenOperator then
128 if pa
!= null then infobox
= pa
.decorate_tag
(hv
, tag
, c
)
129 else if c
isa TComment and pa
isa ADoc then
130 infobox
= pa
.decorate_tag
(hv
, tag
, c
)
132 if infobox
!= null then tag
.attach_infobox
(infobox
)
136 # Handle ending span productions
137 var ending
= c
.ending_prods
138 if ending
!= null then for p
in ending
do
139 if not p
.is_span
then continue
140 if stack
.is_empty
or p
!= stack
.last
then continue
145 # Handle end of line and end of file
147 if c
== last_token
then n
= null
148 if n
== null or n
.location
.line_start
!= line
then
149 # closes the line div
152 # close the block production divs
153 var c0
= c
.last_token_in_line
155 if c0
!= null then ending
= c0
.ending_prods
156 if ending
!= null then for p
in ending
do
157 if not p
.is_block
then continue
158 if stack
.is_empty
or p
!= stack
.last
then continue
166 assert stack
.is_empty
167 assert stack2
.is_empty
170 # Return a default CSS content related to CSS classes used in the `html` tree.
171 # Could be inlined in the `.html` file of saved as a specific `.css` file.
172 fun css_content
: String
175 .nitcode a { color: inherit; cursor:pointer; }
176 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
177 .nitcode .foldable { display: block } /* for block productions*/
178 .nitcode .line{ display: block } /* for lines */
179 .nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
180 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
181 /* lexical raw tokens. independent of usage or semantic: */
182 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
183 .nitcode .nc_d { color: #3D8127; font-style: italic; } /* documentation comments */
184 .nitcode .nc_k { font-weight: bold; } /* keyword */
185 .nitcode .nc_o {} /* operator */
186 .nitcode .nc_i {} /* standard identifier */
187 .nitcode .nc_t { color: #445588; font-weight: bold; } /* type/class identifier */
188 .nitcode .nc_a { color: #445588; font-style: italic; } /* old style attribute identifier */
189 .nitcode .nc_l { color: #009999; } /* char and number literal */
190 .nitcode .nc_s { color: #8F1546; } /* string literal */
191 /* syntactic token usage. added because of their position in the AST */
192 .nitcode .nc_ast { color: blue; } /* assert label */
193 .nitcode .nc_la { color: blue; } /* break/continue label */
194 .nitcode .nc_m { color: #445588; } /* module name */
195 /* syntactic groups */
196 .nitcode .nc_def { font-weight: bold; color: blue; } /* name used in a definition */
197 .nitcode .nc_def.nc_a { color: blue; } /* name used in a attribute definition */
198 .nitcode .nc_def.nc_t { color: blue; } /* name used in a class or vt definition */
199 .nitcode .nc_ss { color: #9E6BEB; } /* superstrings */
200 .nitcode .nc_cdef {} /* A whole class definition */
201 .nitcode .nc_pdef {} /* A whole property definition */
202 /* semantic token usage */
203 .nitcode .nc_v { font-style: italic; } /* local variable or parameter */
204 .nitcode .nc_vt { font-style: italic; } /* virtual type or formal type */
206 .nitcode .nc_error { border: 1px red solid;} /* not used */
207 .popover { max-width: 800px !important; }
211 # Additional content to inject in the <head> tag
212 # Note: does not include `css_content`; handle it yourself.
213 fun head_content
: String
215 return """<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">\n"""
218 # Additional content to inject just before the closing </body> tag
219 fun foot_content
: String
222 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
223 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
224 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
229 # Attach the infobox to the node by using BootStrap popover
230 fun attach_infobox
(infobox
: HInfoBox)
232 classes
.add
("popupable")
233 attrs
["title"] = infobox
.title
234 var href
= infobox
.href
236 attrs
["data-title"] = """<a href="{{{href}}}">{{{infobox.title}}}</a>"""
238 attrs
["data-content"] = infobox
.content
.write_to_string
239 attrs
["data-toggle"] = "popover"
244 # A generic information container that can be used to decorate AST entities
246 # The visitor used for contextualisation, if needed
247 var visitor
: HighlightVisitor
249 # A short title for the AST element
252 # The primary link where the entity points
254 var href
: nullable String = null
256 # The content of the popuped infobox
257 var content
= new HTMLTag("div")
259 # Append a new field in the popuped infobox
260 fun new_field
(title
: String): HTMLTag
262 content
.open
("b").text
(title
)
264 var res
= content
.open
("span")
269 # Append a new dropdown in the popuped content
270 fun new_dropdown
(title
, text
: String): HTMLTag
272 content
.add_raw_html
"""<div class="dropdown"> <a data-toggle="dropdown" href="#"><b>"""
273 content
.append
(title
)
274 content
.add_raw_html
"</b> "
276 content
.add_raw_html
"""<span class="caret"></span></a>"""
277 var res
= content
.open
("ul").add_class
("dropdown-menu").attr
("role", "menu").attr
("aria-labelledby", "dLabel")
278 content
.add_raw_html
"</div>"
285 # Model entity or whatever that can produce an infobox
286 interface HInfoBoxable
287 # An new infobox documenting the entity
288 fun infobox
(v
: HighlightVisitor): HInfoBox is abstract
290 # A human-readable hyper-text for the entity
291 fun linkto
: HTMLTag is abstract
295 # Append an entry for the doc in the given infobox
296 fun fill_infobox
(res
: HInfoBox)
298 if content
.length
< 2 then
299 res
.new_field
("doc").text
(content
.first
)
302 var c
= res
.new_dropdown
("doc", content
.first
)
303 for x
in content
.iterator
.skip_head
(1) do
305 c
.add_raw_html
"<br>"
317 var res
= new HInfoBox(v
, "module {name}")
319 res
.new_field
("module").add
(linkto
)
321 if mdoc
!= null then mdoc
.fill_infobox
(res
)
322 if in_importation
.greaters
.length
> 1 then
323 var c
= res
.new_dropdown
("imports", "{in_importation.greaters.length-1} modules")
324 for x
in in_importation
.greaters
do
325 if x
== self then continue
326 c
.open
("li").add x
.linkto
334 return name
+ ".html"
337 redef fun linkto
do return linkto_text
(name
)
339 # Link to the entitiy with a specific text
340 fun linkto_text
(text
: String): HTMLTag
342 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
346 redef class MClassDef
349 var res
= new HInfoBox(v
, "class {mclass.name}")
352 res
.new_field
("class").text
(mclass
.name
)
354 res
.new_field
("redef class").text
(mclass
.name
)
355 res
.new_field
("intro").add mclass
.intro
.linkto_text
("in {mclass.intro.mmodule.to_s}")
358 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
359 if mdoc
!= null then mdoc
.fill_infobox
(res
)
361 if in_hierarchy
== null then return res
363 if in_hierarchy
.greaters
.length
> 1 then
364 var c
= res
.new_dropdown
("hier", "super-classes")
365 for x
in in_hierarchy
.greaters
do
366 if x
== self then continue
367 if not x
.is_intro
then continue
368 c
.open
("li").add x
.linkto
371 if in_hierarchy
.smallers
.length
> 1 then
372 var c
= res
.new_dropdown
("hier", "sub-classes")
373 for x
in in_hierarchy
.smallers
do
374 if x
== self then continue
375 if not x
.is_intro
then continue
376 c
.open
("li").add x
.linkto
379 if mclass
.mclassdefs
.length
> 1 then
380 var c
= res
.new_dropdown
("redefs", "refinements")
381 for x
in mclass
.mclassdefs
do
382 if x
== self then continue
383 c
.open
("li").add x
.linkto_text
("in {x.mmodule}")
391 return mmodule
.href
+ "#" + to_s
394 redef fun linkto
do return linkto_text
(mclass
.name
)
396 # Link to the entitiy with a specific text
397 fun linkto_text
(text
: String): HTMLTag
399 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
406 var res
= new HInfoBox(v
, to_s
)
408 if self isa MMethodDef then
409 if msignature
!= null then res
.new_field
("fun").append
(mproperty
.name
).add msignature
.linkto
410 else if self isa MAttributeDef then
411 if static_mtype
!= null then res
.new_field
("fun").append
(mproperty
.name
).add static_mtype
.linkto
412 else if self isa MVirtualTypeDef then
413 if bound
!= null then res
.new_field
("add").append
(mproperty
.name
).add bound
.linkto
415 res
.new_field
("wat?").append
(mproperty
.name
)
420 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
423 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
424 if mdoc
!= null then mdoc
.fill_infobox
(res
)
425 if mproperty
.mpropdefs
.length
> 1 then
426 var c
= res
.new_dropdown
("redef", "redefinitions")
427 for x
in mproperty
.mpropdefs
do
428 c
.open
("li").add x
.linkto_text
("in {x.mclassdef}")
437 return self.mclassdef
.mmodule
.href
+ "#" + self.to_s
440 redef fun linkto
do return linkto_text
(mproperty
.name
)
442 # Link to the entitiy with a specific text
443 fun linkto_text
(text
: String): HTMLTag
445 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
449 redef class MClassType
452 var res
= new HInfoBox(v
, to_s
)
453 res
.href
= mclass
.intro
.href
454 res
.new_field
("class").add mclass
.intro
.linkto
455 var mdoc
= mclass
.mdoc
456 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
457 if mdoc
!= null then mdoc
.fill_infobox
(res
)
462 return mclass
.intro
.linkto
465 redef class MVirtualType
468 var res
= new HInfoBox(v
, to_s
)
469 res
.href
= mproperty
.intro
.href
472 res
.new_field
("virtual type").add pd
.linkto
474 if mdoc
!= null then mdoc
.fill_infobox
(res
)
479 return mproperty
.intro
.linkto
482 redef class MParameterType
485 var res
= new HInfoBox(v
, to_s
)
486 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
491 return (new HTMLTag("span")).text
(name
)
495 redef class MNullableType
498 return mtype
.infobox
(v
)
502 var res
= new HTMLTag("span")
503 res
.append
("nullable ").add
(mtype
.linkto
)
508 redef class MSignature
511 var res
= new HTMLTag("span")
513 if not mparameters
.is_empty
then
515 for p
in mparameters
do
523 res
.add p
.mtype
.linkto
527 var ret
= return_mtype
540 var res
= new HInfoBox(v
, "call {mpropdef}")
541 res
.href
= mpropdef
.href
542 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
543 if mpropdef
.is_intro
then
545 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
547 var mdoc
= mpropdef
.mdoc
548 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
549 if mdoc
!= null then mdoc
.fill_infobox
(res
)
555 return mpropdef
.linkto
563 var declared_type
= self.declared_type
564 if declared_type
== null then
565 var res
= new HInfoBox(v
, "{name}")
566 res
.new_field
("local var").append
("{name}")
569 var res
= new HInfoBox(v
, "{name}: {declared_type}")
570 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
575 return (new HTMLTag("span")).text
(name
)
583 # Optionally creates a tag that encapsulate the AST element on HTML rendering
584 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
586 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
587 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
589 #debug("no decoration for {token.inspect}")
590 #res.add_class("nc_error")
594 # Return a optional infobox
595 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
598 redef class AStdClassdef
599 redef fun make_tag
(v
)
601 var res
= new HTMLTag("span")
602 res
.add_class
("nc_cdef")
604 if md
!= null then res
.attr
("id", md
.to_s
)
607 redef fun decorate_tag
(v
, res
, token
)
609 res
.add_class
("nc_def")
612 if md
== null then return null
617 redef fun make_tag
(v
)
619 var res
= new HTMLTag("span")
620 res
.add_class
("nc_pdef")
625 res
.attr
("id", mpd
.to_s
)
627 if self isa AAttrPropdef then
629 if mpd
!= null then res
.add
(tag
(mpd
))
631 if mpd
!= null then res
.add
(tag
(mpd
))
636 private fun tag
(mpd
: MPropDef): HTMLTag
638 var a
= new HTMLTag("a")
639 a
.attr
("id", mpd
.to_s
)
645 # Produce an HTMLTag with the correct contents and CSS classes
646 # Subclasses can redefine it to decorate the tag
647 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
649 var res
= new HTMLTag("span")
655 redef class TokenKeyword
656 redef fun make_tag
(v
)
659 res
.add_class
("nc_k")
663 redef class TokenOperator
664 redef fun make_tag
(v
)
668 if p
!= null then p
.decorate_tag
(v
, res
, self)
669 res
.add_class
("nc_o")
674 redef class AVarFormExpr
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
)
684 redef class AVardeclExpr
685 redef fun decorate_tag
(v
, res
, token
)
687 var variable
= self.variable
688 if variable
== null then return null
689 res
.add_class
("nc_v")
690 return variable
.infobox
(v
)
695 redef fun decorate_tag
(v
, res
, token
)
697 if not token
isa TId then return null
699 if vs
== null then return null
700 res
.add_class
("nc_v")
701 var idx
= n_ids
.index_of
(token
)
702 var variable
= vs
[idx
]
703 return variable
.infobox
(v
)
708 redef fun decorate_tag
(v
, res
, token
)
711 if mp
== null then return null
712 var variable
= self.variable
713 if variable
== null then return null
714 res
.add_class
("nc_v")
715 return variable
.infobox
(v
)
719 redef class AAssertExpr
720 redef fun decorate_tag
(v
, res
, token
)
722 res
.add_class
("nc_ast")
728 redef fun decorate_tag
(v
, res
, token
)
730 res
.add_class
("nc_la")
735 redef class ASendExpr
736 redef fun decorate_tag
(v
, res
, token
)
738 if callsite
== null then return null
739 return callsite
.infobox
(v
)
744 redef fun decorate_tag
(v
, res
, token
)
746 if callsite
== null then return null
747 return callsite
.infobox
(v
)
751 redef class AAssignOp
752 redef fun decorate_tag
(v
, res
, token
)
755 assert p
isa AReassignFormExpr
757 var callsite
= p
.reassign_callsite
758 if callsite
== null then return null
759 return callsite
.infobox
(v
)
763 redef class AModuleName
764 redef fun decorate_tag
(v
, res
, token
)
766 return parent
.decorate_tag
(v
, res
, token
)
770 redef class AModuledecl
771 redef fun decorate_tag
(v
, res
, token
)
773 res
.add_class
("nc_def")
774 res
.add_class
("nc_m")
778 if mm
== null then return null
783 redef class AStdImport
784 redef fun decorate_tag
(v
, res
, token
)
786 res
.add_class
("nc_m")
788 if mm
== null then return null
792 redef class AAttrPropdef
793 redef fun decorate_tag
(v
, res
, token
)
795 res
.add_class
("nc_def")
796 var mpd
: nullable MPropDef
798 if mpd
== null then mpd
= mpropdef
799 if mpd
== null then return null
800 return mpd
.infobox
(v
)
805 redef fun make_tag
(v
)
809 if p
!= null then p
.decorate_tag
(v
, res
, self)
810 res
.add_class
("nc_i")
815 redef fun make_tag
(v
)
817 var res
= new HTMLTag("span")
818 res
.add_class
("nc_def")
821 redef fun decorate_tag
(v
, res
, token
)
824 # nothing to decorate
829 if not p
isa AMethPropdef then return null
831 if mpd
== null then return null
832 return mpd
.infobox
(v
)
836 redef fun make_tag
(v
)
840 if p
!= null then p
.decorate_tag
(v
, res
, self)
841 res
.add_class
("nc_a")
845 redef class AAttrFormExpr
846 redef fun decorate_tag
(v
, res
, token
)
849 if p
== null then return null
850 return p
.intro
.infobox
(v
)
854 redef fun make_tag
(v
)
858 if p
!= null then p
.decorate_tag
(v
, res
, self)
859 res
.add_class
("nc_t")
864 redef fun decorate_tag
(v
, res
, token
)
867 if mt
== null then return null
868 mt
= mt
.as_notnullable
869 if mt
isa MVirtualType or mt
isa MParameterType then
870 res
.add_class
("nc_vt")
875 redef class AFormaldef
876 redef fun decorate_tag
(v
, res
, token
)
878 res
.add_class
("nc_vt")
879 if mtype
== null then return null
880 return mtype
.infobox
(v
)
883 redef class ATypePropdef
884 redef fun decorate_tag
(v
, res
, token
)
886 res
.add_class
("nc_def")
888 if md
== null then return null
893 redef fun make_tag
(v
)
896 if not parent
isa ADoc then
897 res
.add_class
("nc_c")
903 redef fun make_tag
(v
)
905 var res
= new HTMLTag("span")
906 res
.add_class
("nc_d")
910 redef class TokenLiteral
911 redef fun make_tag
(v
)
914 res
.add_class
("nc_l")
916 if p
!= null then p
.decorate_tag
(v
, res
, self)
920 redef class ASuperstringExpr
921 redef fun make_tag
(v
)
923 var res
= new HTMLTag("span")
924 res
.add_class
("nc_ss")
928 redef class AStringFormExpr
929 redef fun decorate_tag
(v
, res
, token
)
931 # Workaround to tag strings
932 res
.classes
.remove
("nc_l")
933 res
.add_class
("nc_s")
938 redef fun decorate_tag
(v
, res
, token
)
941 if t
== null then return null