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 # The entry-point of the highlighting.
52 # Will fill `html` with the generated HTML content.
53 fun enter_visit
(n
: ANode)
66 if f
== null then return
68 if l
== null then return
74 private fun full_tag
(anode
: ANode, hv
: HighlightVisitor): nullable HTMLTag
76 var tag
= anode
.make_tag
(hv
)
77 if tag
== null then return null
78 var infobox
= anode
.infobox
(hv
)
79 if infobox
== null and anode
isa Token then
83 if c
isa TId or c
isa TClassid or c
isa TAttrid or c
isa TokenLiteral or c
isa TokenOperator or c
isa TComment and pa
isa ADoc then
84 infobox
= pa
.decorate_tag
(hv
, tag
, anode
)
88 var messages
= anode
.location
.messages
89 if messages
!= null then
90 tag
.css
("border-bottom", "solid 2px red")
91 if infobox
== null then
92 infobox
= new HInfoBox(hv
, "Messages")
94 var c
= infobox
.new_dropdown
("{messages.length} message(s)", "")
96 c
.open
("li").append
(m
.text
)
99 if infobox
!= null then
100 tag
.attach_infobox
(infobox
)
105 # Produce HTML between two tokens
106 protected fun htmlize
(first_token
, last_token
: Token)
108 var stack2
= new Array[HTMLTag]
109 var stack
= new Array[Prod]
111 var c
: nullable Token = first_token
112 var hv
= new HighlightVisitor
116 # Handle start of line
117 var cline
= c
.location
.line_start
118 if cline
!= line
then
119 # Handle starting block productions,
120 # Because c could be a detached token, get prods in
121 # the first AST token
122 var c0
= c
.first_token_in_line
124 if c0
!= null then starting
= c0
.starting_prods
125 if starting
!= null then for p
in starting
do
126 if not p
.is_block
then continue
127 var tag
= full_tag
(p
, hv
)
128 if tag
== null then continue
129 tag
.add_class
("foldable")
136 # Add a div for the whole line
137 var tag
= new HTMLTag("span")
138 var p
= line_id_prefix
139 if p
!= "" then tag
.attrs
["id"] = "{p}{cline}"
140 tag
.classes
.add
"line"
147 # Add the blank, verbatim
148 html
.add_raw_html c
.blank_before
150 # Handle starting span production
151 starting
= c
.starting_prods
152 if starting
!= null then for p
in starting
do
153 if not p
.is_span
then continue
154 var tag
= full_tag
(p
, hv
)
155 if tag
== null then continue
166 var tag
= full_tag
(c
, hv
)
167 if tag
!= null then html
.add tag
170 # Handle ending span productions
171 var ending
= c
.ending_prods
172 if ending
!= null then for p
in ending
do
173 if not p
.is_span
then continue
174 if stack
.is_empty
or p
!= stack
.last
then continue
179 # Handle end of line and end of file
181 if c
== last_token
then n
= null
182 if n
== null or n
.location
.line_start
!= line
then
183 # closes the line div
186 # close the block production divs
187 var c0
= c
.last_token_in_line
189 if c0
!= null then ending
= c0
.ending_prods
190 if ending
!= null then for p
in ending
do
191 if not p
.is_block
then continue
192 if stack
.is_empty
or p
!= stack
.last
then continue
200 #assert stack.is_empty
201 #assert stack2.is_empty
204 # Return a default CSS content related to CSS classes used in the `html` tree.
205 # Could be inlined in the `.html` file of saved as a specific `.css` file.
206 fun css_content
: String
209 .nitcode a { color: inherit; cursor:pointer; }
210 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
211 .nitcode .foldable { display: block } /* for block productions*/
212 .nitcode .line{ display: block } /* for lines */
213 .nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
214 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
215 /* lexical raw tokens. independent of usage or semantic: */
216 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
217 .nitcode .nc_d { color: #3D8127; font-style: italic; } /* documentation comments */
218 .nitcode .nc_k { font-weight: bold; } /* keyword */
219 .nitcode .nc_o {} /* operator */
220 .nitcode .nc_i {} /* standard identifier */
221 .nitcode .nc_t { color: #445588; font-weight: bold; } /* type/class identifier */
222 .nitcode .nc_a { color: #445588; font-style: italic; } /* old style attribute identifier */
223 .nitcode .nc_l { color: #009999; } /* char and number literal */
224 .nitcode .nc_s { color: #8F1546; } /* string literal */
225 /* syntactic token usage. added because of their position in the AST */
226 .nitcode .nc_ast { color: blue; } /* assert label */
227 .nitcode .nc_la { color: blue; } /* break/continue label */
228 .nitcode .nc_m { color: #445588; } /* module name */
229 /* syntactic groups */
230 .nitcode .nc_def { font-weight: bold; color: blue; } /* name used in a definition */
231 .nitcode .nc_def.nc_a { color: blue; } /* name used in a attribute definition */
232 .nitcode .nc_def.nc_t { color: blue; } /* name used in a class or vt definition */
233 .nitcode .nc_ss { color: #9E6BEB; } /* superstrings */
234 .nitcode .nc_cdef {} /* A whole class definition */
235 .nitcode .nc_pdef {} /* A whole property definition */
236 /* semantic token usage */
237 .nitcode .nc_v { font-style: italic; } /* local variable or parameter */
238 .nitcode .nc_vt { font-style: italic; } /* virtual type or formal type */
240 .nitcode .nc_error { border: 1px red solid;} /* not used */
241 .popover { max-width: 800px !important; }
245 # Additional content to inject in the <head> tag
246 # Note: does not include `css_content`; handle it yourself.
247 fun head_content
: String
249 return """<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">\n"""
252 # Additional content to inject just before the closing </body> tag
253 fun foot_content
: String
256 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
257 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
258 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
263 # Attach the infobox to the node by using BootStrap popover
264 fun attach_infobox
(infobox
: HInfoBox)
266 classes
.add
("popupable")
267 attrs
["title"] = infobox
.title
268 var href
= infobox
.href
270 attrs
["data-title"] = """<a href="{{{href}}}">{{{infobox.title}}}</a>"""
272 attrs
["data-content"] = infobox
.content
.write_to_string
273 attrs
["data-toggle"] = "popover"
278 # A generic information container that can be used to decorate AST entities
280 # The visitor used for contextualisation, if needed
281 var visitor
: HighlightVisitor
283 # A short title for the AST element
286 # The primary link where the entity points
288 var href
: nullable String = null
290 # The content of the popuped infobox
291 var content
= new HTMLTag("div")
293 # Append a new field in the popuped infobox
294 fun new_field
(title
: String): HTMLTag
296 content
.open
("b").text
(title
)
298 var res
= content
.open
("span")
303 # Append a new dropdown in the popuped content
304 fun new_dropdown
(title
, text
: String): HTMLTag
306 content
.add_raw_html
"""<div class="dropdown"> <a data-toggle="dropdown" href="#"><b>"""
307 content
.append
(title
)
308 content
.add_raw_html
"</b> "
310 content
.add_raw_html
"""<span class="caret"></span></a>"""
311 var res
= content
.open
("ul").add_class
("dropdown-menu").attr
("role", "menu").attr
("aria-labelledby", "dLabel")
312 content
.add_raw_html
"</div>"
319 # Model entity or whatever that can produce an infobox
320 interface HInfoBoxable
321 # An new infobox documenting the entity
322 fun infobox
(v
: HighlightVisitor): HInfoBox is abstract
324 # A human-readable hyper-text for the entity
325 fun linkto
: HTMLTag is abstract
329 # Append an entry for the doc in the given infobox
330 fun fill_infobox
(res
: HInfoBox)
332 if content
.length
< 2 then
333 res
.new_field
("doc").text
(content
.first
)
336 var c
= res
.new_dropdown
("doc", content
.first
)
337 for x
in content
.iterator
.skip_head
(1) do
339 c
.add_raw_html
"<br>"
351 var res
= new HInfoBox(v
, "module {name}")
353 res
.new_field
("module").add
(linkto
)
355 if mdoc
!= null then mdoc
.fill_infobox
(res
)
356 if in_importation
.greaters
.length
> 1 then
357 var c
= res
.new_dropdown
("imports", "{in_importation.greaters.length-1} modules")
358 for x
in in_importation
.greaters
do
359 if x
== self then continue
360 c
.open
("li").add x
.linkto
366 # The module HTML page
369 return c_name
+ ".html"
372 redef fun linkto
do return linkto_text
(name
)
374 # Link to the entitiy with a specific text
375 fun linkto_text
(text
: String): HTMLTag
377 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
381 redef class MClassDef
384 var res
= new HInfoBox(v
, "class {mclass.name}")
387 res
.new_field
("class").text
(mclass
.name
)
389 res
.new_field
("redef class").text
(mclass
.name
)
390 res
.new_field
("intro").add mclass
.intro
.linkto_text
("in {mclass.intro_mmodule.to_s}")
393 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
394 if mdoc
!= null then mdoc
.fill_infobox
(res
)
396 if in_hierarchy
== null then return res
398 if in_hierarchy
.greaters
.length
> 1 then
399 var c
= res
.new_dropdown
("hier", "super-classes")
400 for x
in in_hierarchy
.greaters
do
401 if x
== self then continue
402 if not x
.is_intro
then continue
403 c
.open
("li").add x
.linkto
406 if in_hierarchy
.smallers
.length
> 1 then
407 var c
= res
.new_dropdown
("hier", "sub-classes")
408 for x
in in_hierarchy
.smallers
do
409 if x
== self then continue
410 if not x
.is_intro
then continue
411 c
.open
("li").add x
.linkto
414 if mclass
.mclassdefs
.length
> 1 then
415 var c
= res
.new_dropdown
("redefs", "refinements")
416 for x
in mclass
.mclassdefs
do
417 if x
== self then continue
418 c
.open
("li").add x
.linkto_text
("in {x.mmodule}")
424 # The class HTML page (an anchor in the module page)
427 return mmodule
.href
+ "#" + to_s
430 redef fun linkto
do return linkto_text
(mclass
.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
)
442 var res
= new HInfoBox(v
, to_s
)
444 if self isa MMethodDef then
445 if msignature
!= null then res
.new_field
("fun").append
(mproperty
.name
).add msignature
.linkto
446 else if self isa MAttributeDef then
447 if static_mtype
!= null then res
.new_field
("fun").append
(mproperty
.name
).add static_mtype
.linkto
448 else if self isa MVirtualTypeDef then
449 if bound
!= null then res
.new_field
("add").append
(mproperty
.name
).add bound
.linkto
451 res
.new_field
("wat?").append
(mproperty
.name
)
456 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
459 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
460 if mdoc
!= null then mdoc
.fill_infobox
(res
)
461 if mproperty
.mpropdefs
.length
> 1 then
462 var c
= res
.new_dropdown
("redef", "redefinitions")
463 for x
in mproperty
.mpropdefs
do
464 c
.open
("li").add x
.linkto_text
("in {x.mclassdef}")
471 # The property HTML page (an anchor in the module page)
474 return self.mclassdef
.mmodule
.href
+ "#" + self.to_s
477 redef fun linkto
do return linkto_text
(mproperty
.name
)
479 # Link to the entitiy with a specific text
480 fun linkto_text
(text
: String): HTMLTag
482 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
486 redef class MClassType
489 var res
= new HInfoBox(v
, to_s
)
490 res
.href
= mclass
.intro
.href
491 res
.new_field
("class").add mclass
.intro
.linkto
492 var mdoc
= mclass
.mdoc
493 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
494 if mdoc
!= null then mdoc
.fill_infobox
(res
)
499 return mclass
.intro
.linkto
502 redef class MVirtualType
505 var res
= new HInfoBox(v
, to_s
)
506 res
.href
= mproperty
.intro
.href
509 res
.new_field
("virtual type").add pd
.linkto
511 if mdoc
!= null then mdoc
.fill_infobox
(res
)
516 return mproperty
.intro
.linkto
519 redef class MParameterType
522 var res
= new HInfoBox(v
, to_s
)
523 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
528 return (new HTMLTag("span")).text
(name
)
532 redef class MNullableType
535 return mtype
.infobox
(v
)
539 var res
= new HTMLTag("span")
540 res
.append
("nullable ").add
(mtype
.linkto
)
545 redef class MNotNullType
548 return mtype
.infobox
(v
)
552 var res
= new HTMLTag("span")
553 res
.append
("not null ").add
(mtype
.linkto
)
558 redef class MNullType
561 var res
= new HInfoBox(v
, to_s
)
566 var res
= new HTMLTag("span")
572 redef class MSignature
575 var res
= new HTMLTag("span")
577 if not mparameters
.is_empty
then
579 for p
in mparameters
do
587 res
.add p
.mtype
.linkto
591 var ret
= return_mtype
603 var res
= new HInfoBox(v
, "call {mpropdef}")
604 res
.href
= mpropdef
.href
605 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
606 if mpropdef
.is_intro
then
608 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
610 var mdoc
= mpropdef
.mdoc
611 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
612 if mdoc
!= null then mdoc
.fill_infobox
(res
)
618 return mpropdef
.linkto
626 var declared_type
= self.declared_type
627 if declared_type
== null then
628 var res
= new HInfoBox(v
, "{name}")
629 res
.new_field
("local var").append
("{name}")
632 var res
= new HInfoBox(v
, "{name}: {declared_type}")
633 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
638 return (new HTMLTag("span")).text
(name
)
646 # Optionally creates a tag that encapsulate the AST element on HTML rendering
647 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
649 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
650 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
652 #debug("no decoration for {token.inspect}")
653 #res.add_class("nc_error")
657 # Return a optional infobox
658 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
661 redef class AStdClassdef
662 redef fun make_tag
(v
)
664 var res
= new HTMLTag("span")
665 res
.add_class
("nc_cdef")
667 if md
!= null then res
.attr
("id", md
.to_s
)
670 redef fun decorate_tag
(v
, res
, token
)
672 if not token
isa TClassid then return null
673 res
.add_class
("nc_def")
676 if md
== null then return null
681 redef fun make_tag
(v
)
683 var res
= new HTMLTag("span")
684 res
.add_class
("nc_pdef")
689 res
.attr
("id", mpd
.to_s
)
691 if self isa AAttrPropdef then
693 if mpd
!= null then res
.add
(tag
(mpd
))
695 if mpd
!= null then res
.add
(tag
(mpd
))
700 private fun tag
(mpd
: MPropDef): HTMLTag
702 var a
= new HTMLTag("a")
703 a
.attr
("id", mpd
.to_s
)
709 # Produce an HTMLTag with the correct contents and CSS classes
710 # Subclasses can redefine it to decorate the tag
711 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
713 var res
= new HTMLTag("span")
719 redef class TokenKeyword
720 redef fun make_tag
(v
)
723 res
.add_class
("nc_k")
727 redef class TokenOperator
728 redef fun make_tag
(v
)
732 if p
!= null then p
.decorate_tag
(v
, res
, self)
733 res
.add_class
("nc_o")
738 redef class AVarFormExpr
739 redef fun decorate_tag
(v
, res
, token
)
741 var variable
= self.variable
742 if variable
== null then return null
743 res
.add_class
("nc_v")
744 return variable
.infobox
(v
)
748 redef class AVardeclExpr
749 redef fun decorate_tag
(v
, res
, token
)
751 var variable
= self.variable
752 if variable
== null then return null
753 res
.add_class
("nc_v")
754 return variable
.infobox
(v
)
758 redef class AForGroup
759 redef fun decorate_tag
(v
, res
, token
)
761 if not token
isa TId then return null
763 if vs
== null then return null
764 res
.add_class
("nc_v")
765 var idx
= n_ids
.index_of
(token
)
766 var variable
= vs
[idx
]
767 return variable
.infobox
(v
)
772 redef fun decorate_tag
(v
, res
, token
)
775 if mp
== null then return null
776 var variable
= self.variable
777 if variable
== null then return null
778 res
.add_class
("nc_v")
779 return variable
.infobox
(v
)
783 redef class AAssertExpr
784 redef fun decorate_tag
(v
, res
, token
)
786 res
.add_class
("nc_ast")
792 redef fun decorate_tag
(v
, res
, token
)
794 res
.add_class
("nc_la")
799 redef class ASendExpr
800 redef fun decorate_tag
(v
, res
, token
)
802 if callsite
== null then return null
803 return callsite
.infobox
(v
)
808 redef fun decorate_tag
(v
, res
, token
)
810 if callsite
== null then return null
811 return callsite
.infobox
(v
)
815 redef class AAssignOp
816 redef fun decorate_tag
(v
, res
, token
)
819 assert p
isa AReassignFormExpr
821 var callsite
= p
.reassign_callsite
822 if callsite
== null then return null
823 return callsite
.infobox
(v
)
827 redef class AModuleName
828 redef fun decorate_tag
(v
, res
, token
)
830 return parent
.decorate_tag
(v
, res
, token
)
834 redef class AModuledecl
835 redef fun decorate_tag
(v
, res
, token
)
837 res
.add_class
("nc_def")
838 res
.add_class
("nc_m")
842 if mm
== null then return null
847 redef class AStdImport
848 redef fun decorate_tag
(v
, res
, token
)
850 res
.add_class
("nc_m")
852 if mm
== null then return null
856 redef class AAttrPropdef
857 redef fun decorate_tag
(v
, res
, token
)
859 res
.add_class
("nc_def")
860 var mpd
: nullable MPropDef
862 if mpd
== null then mpd
= mpropdef
863 if mpd
== null then return null
864 return mpd
.infobox
(v
)
869 redef fun make_tag
(v
)
873 if p
!= null then p
.decorate_tag
(v
, res
, self)
874 res
.add_class
("nc_i")
879 redef fun make_tag
(v
)
881 var res
= new HTMLTag("span")
882 res
.add_class
("nc_def")
885 redef fun decorate_tag
(v
, res
, token
)
888 # nothing to decorate
893 if not p
isa AMethPropdef then return null
895 if mpd
== null then return null
896 return mpd
.infobox
(v
)
900 redef fun make_tag
(v
)
904 if p
!= null then p
.decorate_tag
(v
, res
, self)
905 res
.add_class
("nc_a")
909 redef class AAttrFormExpr
910 redef fun decorate_tag
(v
, res
, token
)
913 if p
== null then return null
914 return p
.intro
.infobox
(v
)
918 redef fun make_tag
(v
)
922 if p
!= null then p
.decorate_tag
(v
, res
, self)
923 res
.add_class
("nc_t")
928 redef fun decorate_tag
(v
, res
, token
)
931 if mt
== null then return null
933 if mt
isa MFormalType then
934 res
.add_class
("nc_vt")
939 redef class AFormaldef
940 redef fun decorate_tag
(v
, res
, token
)
942 res
.add_class
("nc_vt")
943 if mtype
== null then return null
944 return mtype
.infobox
(v
)
947 redef class ATypePropdef
948 redef fun decorate_tag
(v
, res
, token
)
950 res
.add_class
("nc_def")
952 if md
== null then return null
957 redef fun make_tag
(v
)
960 if not parent
isa ADoc then
961 res
.add_class
("nc_c")
967 redef fun make_tag
(v
)
969 var res
= new HTMLTag("span")
970 res
.add_class
("nc_d")
974 redef class TokenLiteral
975 redef fun make_tag
(v
)
978 res
.add_class
("nc_l")
980 if p
!= null then p
.decorate_tag
(v
, res
, self)
984 redef class ASuperstringExpr
985 redef fun make_tag
(v
)
987 var res
= new HTMLTag("span")
988 res
.add_class
("nc_ss")
992 redef class AStringFormExpr
993 redef fun decorate_tag
(v
, res
, token
)
995 # Workaround to tag strings
996 res
.classes
.remove
("nc_l")
997 res
.add_class
("nc_s")
1002 redef fun decorate_tag
(v
, res
, token
)
1005 if t
== null then return null