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)
56 var s
= n
.location
.file
57 htmlize
(s
.first_token
.as(not null), s
.last_token
.as(not null))
60 private fun full_tag
(anode
: ANode, hv
: HighlightVisitor): nullable HTMLTag
62 var tag
= anode
.make_tag
(hv
)
63 if tag
== null then return null
64 var infobox
= anode
.infobox
(hv
)
65 if infobox
== null and anode
isa Token then
69 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
70 infobox
= pa
.decorate_tag
(hv
, tag
, anode
)
74 if infobox
!= null then
75 tag
.attach_infobox
(infobox
)
80 # Produce HTML between two tokens
81 protected fun htmlize
(first_token
, last_token
: Token)
83 var stack2
= new Array[HTMLTag]
84 var stack
= new Array[Prod]
86 var c
: nullable Token = first_token
87 var hv
= new HighlightVisitor
91 # Handle start of line
92 var cline
= c
.location
.line_start
94 # Handle starting block productions,
95 # Because c could be a detached token, get prods in
97 var c0
= c
.first_token_in_line
99 if c0
!= null then starting
= c0
.starting_prods
100 if starting
!= null then for p
in starting
do
101 if not p
.is_block
then continue
102 var tag
= full_tag
(p
, hv
)
103 if tag
== null then continue
104 tag
.add_class
("foldable")
111 # Add a div for the whole line
112 var tag
= new HTMLTag("span")
113 var p
= line_id_prefix
114 if p
!= "" then tag
.attrs
["id"] = "{p}{cline}"
115 tag
.classes
.add
"line"
122 # Add the blank, verbatim
123 html
.add_raw_html c
.blank_before
125 # Handle starting span production
126 starting
= c
.starting_prods
127 if starting
!= null then for p
in starting
do
128 if not p
.is_span
then continue
129 var tag
= full_tag
(p
, hv
)
130 if tag
== null then continue
141 var tag
= full_tag
(c
, hv
)
142 if tag
!= null then html
.add tag
145 # Handle ending span productions
146 var ending
= c
.ending_prods
147 if ending
!= null then for p
in ending
do
148 if not p
.is_span
then continue
149 if stack
.is_empty
or p
!= stack
.last
then continue
154 # Handle end of line and end of file
156 if c
== last_token
then n
= null
157 if n
== null or n
.location
.line_start
!= line
then
158 # closes the line div
161 # close the block production divs
162 var c0
= c
.last_token_in_line
164 if c0
!= null then ending
= c0
.ending_prods
165 if ending
!= null then for p
in ending
do
166 if not p
.is_block
then continue
167 if stack
.is_empty
or p
!= stack
.last
then continue
175 assert stack
.is_empty
176 assert stack2
.is_empty
179 # Return a default CSS content related to CSS classes used in the `html` tree.
180 # Could be inlined in the `.html` file of saved as a specific `.css` file.
181 fun css_content
: String
184 .nitcode a { color: inherit; cursor:pointer; }
185 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
186 .nitcode .foldable { display: block } /* for block productions*/
187 .nitcode .line{ display: block } /* for lines */
188 .nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
189 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
190 /* lexical raw tokens. independent of usage or semantic: */
191 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
192 .nitcode .nc_d { color: #3D8127; font-style: italic; } /* documentation comments */
193 .nitcode .nc_k { font-weight: bold; } /* keyword */
194 .nitcode .nc_o {} /* operator */
195 .nitcode .nc_i {} /* standard identifier */
196 .nitcode .nc_t { color: #445588; font-weight: bold; } /* type/class identifier */
197 .nitcode .nc_a { color: #445588; font-style: italic; } /* old style attribute identifier */
198 .nitcode .nc_l { color: #009999; } /* char and number literal */
199 .nitcode .nc_s { color: #8F1546; } /* string literal */
200 /* syntactic token usage. added because of their position in the AST */
201 .nitcode .nc_ast { color: blue; } /* assert label */
202 .nitcode .nc_la { color: blue; } /* break/continue label */
203 .nitcode .nc_m { color: #445588; } /* module name */
204 /* syntactic groups */
205 .nitcode .nc_def { font-weight: bold; color: blue; } /* name used in a definition */
206 .nitcode .nc_def.nc_a { color: blue; } /* name used in a attribute definition */
207 .nitcode .nc_def.nc_t { color: blue; } /* name used in a class or vt definition */
208 .nitcode .nc_ss { color: #9E6BEB; } /* superstrings */
209 .nitcode .nc_cdef {} /* A whole class definition */
210 .nitcode .nc_pdef {} /* A whole property definition */
211 /* semantic token usage */
212 .nitcode .nc_v { font-style: italic; } /* local variable or parameter */
213 .nitcode .nc_vt { font-style: italic; } /* virtual type or formal type */
215 .nitcode .nc_error { border: 1px red solid;} /* not used */
216 .popover { max-width: 800px !important; }
220 # Additional content to inject in the <head> tag
221 # Note: does not include `css_content`; handle it yourself.
222 fun head_content
: String
224 return """<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">\n"""
227 # Additional content to inject just before the closing </body> tag
228 fun foot_content
: String
231 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
232 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
233 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
238 # Attach the infobox to the node by using BootStrap popover
239 fun attach_infobox
(infobox
: HInfoBox)
241 classes
.add
("popupable")
242 attrs
["title"] = infobox
.title
243 var href
= infobox
.href
245 attrs
["data-title"] = """<a href="{{{href}}}">{{{infobox.title}}}</a>"""
247 attrs
["data-content"] = infobox
.content
.write_to_string
248 attrs
["data-toggle"] = "popover"
253 # A generic information container that can be used to decorate AST entities
255 # The visitor used for contextualisation, if needed
256 var visitor
: HighlightVisitor
258 # A short title for the AST element
261 # The primary link where the entity points
263 var href
: nullable String = null
265 # The content of the popuped infobox
266 var content
= new HTMLTag("div")
268 # Append a new field in the popuped infobox
269 fun new_field
(title
: String): HTMLTag
271 content
.open
("b").text
(title
)
273 var res
= content
.open
("span")
278 # Append a new dropdown in the popuped content
279 fun new_dropdown
(title
, text
: String): HTMLTag
281 content
.add_raw_html
"""<div class="dropdown"> <a data-toggle="dropdown" href="#"><b>"""
282 content
.append
(title
)
283 content
.add_raw_html
"</b> "
285 content
.add_raw_html
"""<span class="caret"></span></a>"""
286 var res
= content
.open
("ul").add_class
("dropdown-menu").attr
("role", "menu").attr
("aria-labelledby", "dLabel")
287 content
.add_raw_html
"</div>"
294 # Model entity or whatever that can produce an infobox
295 interface HInfoBoxable
296 # An new infobox documenting the entity
297 fun infobox
(v
: HighlightVisitor): HInfoBox is abstract
299 # A human-readable hyper-text for the entity
300 fun linkto
: HTMLTag is abstract
304 # Append an entry for the doc in the given infobox
305 fun fill_infobox
(res
: HInfoBox)
307 if content
.length
< 2 then
308 res
.new_field
("doc").text
(content
.first
)
311 var c
= res
.new_dropdown
("doc", content
.first
)
312 for x
in content
.iterator
.skip_head
(1) do
314 c
.add_raw_html
"<br>"
326 var res
= new HInfoBox(v
, "module {name}")
328 res
.new_field
("module").add
(linkto
)
330 if mdoc
!= null then mdoc
.fill_infobox
(res
)
331 if in_importation
.greaters
.length
> 1 then
332 var c
= res
.new_dropdown
("imports", "{in_importation.greaters.length-1} modules")
333 for x
in in_importation
.greaters
do
334 if x
== self then continue
335 c
.open
("li").add x
.linkto
341 # The module HTML page
344 return name
+ ".html"
347 redef fun linkto
do return linkto_text
(name
)
349 # Link to the entitiy with a specific text
350 fun linkto_text
(text
: String): HTMLTag
352 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
356 redef class MClassDef
359 var res
= new HInfoBox(v
, "class {mclass.name}")
362 res
.new_field
("class").text
(mclass
.name
)
364 res
.new_field
("redef class").text
(mclass
.name
)
365 res
.new_field
("intro").add mclass
.intro
.linkto_text
("in {mclass.intro.mmodule.to_s}")
368 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
369 if mdoc
!= null then mdoc
.fill_infobox
(res
)
371 if in_hierarchy
== null then return res
373 if in_hierarchy
.greaters
.length
> 1 then
374 var c
= res
.new_dropdown
("hier", "super-classes")
375 for x
in in_hierarchy
.greaters
do
376 if x
== self then continue
377 if not x
.is_intro
then continue
378 c
.open
("li").add x
.linkto
381 if in_hierarchy
.smallers
.length
> 1 then
382 var c
= res
.new_dropdown
("hier", "sub-classes")
383 for x
in in_hierarchy
.smallers
do
384 if x
== self then continue
385 if not x
.is_intro
then continue
386 c
.open
("li").add x
.linkto
389 if mclass
.mclassdefs
.length
> 1 then
390 var c
= res
.new_dropdown
("redefs", "refinements")
391 for x
in mclass
.mclassdefs
do
392 if x
== self then continue
393 c
.open
("li").add x
.linkto_text
("in {x.mmodule}")
399 # The class HTML page (an anchor in the module page)
402 return mmodule
.href
+ "#" + to_s
405 redef fun linkto
do return linkto_text
(mclass
.name
)
407 # Link to the entitiy with a specific text
408 fun linkto_text
(text
: String): HTMLTag
410 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
417 var res
= new HInfoBox(v
, to_s
)
419 if self isa MMethodDef then
420 if msignature
!= null then res
.new_field
("fun").append
(mproperty
.name
).add msignature
.linkto
421 else if self isa MAttributeDef then
422 if static_mtype
!= null then res
.new_field
("fun").append
(mproperty
.name
).add static_mtype
.linkto
423 else if self isa MVirtualTypeDef then
424 if bound
!= null then res
.new_field
("add").append
(mproperty
.name
).add bound
.linkto
426 res
.new_field
("wat?").append
(mproperty
.name
)
431 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
434 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
435 if mdoc
!= null then mdoc
.fill_infobox
(res
)
436 if mproperty
.mpropdefs
.length
> 1 then
437 var c
= res
.new_dropdown
("redef", "redefinitions")
438 for x
in mproperty
.mpropdefs
do
439 c
.open
("li").add x
.linkto_text
("in {x.mclassdef}")
446 # The property HTML page (an anchor in the module page)
449 return self.mclassdef
.mmodule
.href
+ "#" + self.to_s
452 redef fun linkto
do return linkto_text
(mproperty
.name
)
454 # Link to the entitiy with a specific text
455 fun linkto_text
(text
: String): HTMLTag
457 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
461 redef class MClassType
464 var res
= new HInfoBox(v
, to_s
)
465 res
.href
= mclass
.intro
.href
466 res
.new_field
("class").add mclass
.intro
.linkto
467 var mdoc
= mclass
.mdoc
468 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
469 if mdoc
!= null then mdoc
.fill_infobox
(res
)
474 return mclass
.intro
.linkto
477 redef class MVirtualType
480 var res
= new HInfoBox(v
, to_s
)
481 res
.href
= mproperty
.intro
.href
484 res
.new_field
("virtual type").add pd
.linkto
486 if mdoc
!= null then mdoc
.fill_infobox
(res
)
491 return mproperty
.intro
.linkto
494 redef class MParameterType
497 var res
= new HInfoBox(v
, to_s
)
498 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
503 return (new HTMLTag("span")).text
(name
)
507 redef class MNullableType
510 return mtype
.infobox
(v
)
514 var res
= new HTMLTag("span")
515 res
.append
("nullable ").add
(mtype
.linkto
)
520 redef class MNotNullType
523 return mtype
.infobox
(v
)
527 var res
= new HTMLTag("span")
528 res
.append
("not null ").add
(mtype
.linkto
)
533 redef class MNullType
536 var res
= new HInfoBox(v
, to_s
)
541 var res
= new HTMLTag("span")
547 redef class MSignature
550 var res
= new HTMLTag("span")
552 if not mparameters
.is_empty
then
554 for p
in mparameters
do
562 res
.add p
.mtype
.linkto
566 var ret
= return_mtype
579 var res
= new HInfoBox(v
, "call {mpropdef}")
580 res
.href
= mpropdef
.href
581 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
582 if mpropdef
.is_intro
then
584 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
586 var mdoc
= mpropdef
.mdoc
587 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
588 if mdoc
!= null then mdoc
.fill_infobox
(res
)
594 return mpropdef
.linkto
602 var declared_type
= self.declared_type
603 if declared_type
== null then
604 var res
= new HInfoBox(v
, "{name}")
605 res
.new_field
("local var").append
("{name}")
608 var res
= new HInfoBox(v
, "{name}: {declared_type}")
609 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
614 return (new HTMLTag("span")).text
(name
)
622 # Optionally creates a tag that encapsulate the AST element on HTML rendering
623 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
625 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
626 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
628 #debug("no decoration for {token.inspect}")
629 #res.add_class("nc_error")
633 # Return a optional infobox
634 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
637 redef class AStdClassdef
638 redef fun make_tag
(v
)
640 var res
= new HTMLTag("span")
641 res
.add_class
("nc_cdef")
643 if md
!= null then res
.attr
("id", md
.to_s
)
646 redef fun decorate_tag
(v
, res
, token
)
648 res
.add_class
("nc_def")
651 if md
== null then return null
656 redef fun make_tag
(v
)
658 var res
= new HTMLTag("span")
659 res
.add_class
("nc_pdef")
664 res
.attr
("id", mpd
.to_s
)
666 if self isa AAttrPropdef then
668 if mpd
!= null then res
.add
(tag
(mpd
))
670 if mpd
!= null then res
.add
(tag
(mpd
))
675 private fun tag
(mpd
: MPropDef): HTMLTag
677 var a
= new HTMLTag("a")
678 a
.attr
("id", mpd
.to_s
)
684 # Produce an HTMLTag with the correct contents and CSS classes
685 # Subclasses can redefine it to decorate the tag
686 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
688 var res
= new HTMLTag("span")
694 redef class TokenKeyword
695 redef fun make_tag
(v
)
698 res
.add_class
("nc_k")
702 redef class TokenOperator
703 redef fun make_tag
(v
)
707 if p
!= null then p
.decorate_tag
(v
, res
, self)
708 res
.add_class
("nc_o")
713 redef class AVarFormExpr
714 redef fun decorate_tag
(v
, res
, token
)
716 var variable
= self.variable
717 if variable
== null then return null
718 res
.add_class
("nc_v")
719 return variable
.infobox
(v
)
723 redef class AVardeclExpr
724 redef fun decorate_tag
(v
, res
, token
)
726 var variable
= self.variable
727 if variable
== null then return null
728 res
.add_class
("nc_v")
729 return variable
.infobox
(v
)
734 redef fun decorate_tag
(v
, res
, token
)
736 if not token
isa TId then return null
738 if vs
== null then return null
739 res
.add_class
("nc_v")
740 var idx
= n_ids
.index_of
(token
)
741 var variable
= vs
[idx
]
742 return variable
.infobox
(v
)
747 redef fun decorate_tag
(v
, res
, token
)
750 if mp
== null then return null
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 AAssertExpr
759 redef fun decorate_tag
(v
, res
, token
)
761 res
.add_class
("nc_ast")
767 redef fun decorate_tag
(v
, res
, token
)
769 res
.add_class
("nc_la")
774 redef class ASendExpr
775 redef fun decorate_tag
(v
, res
, token
)
777 if callsite
== null then return null
778 return callsite
.infobox
(v
)
783 redef fun decorate_tag
(v
, res
, token
)
785 if callsite
== null then return null
786 return callsite
.infobox
(v
)
790 redef class AAssignOp
791 redef fun decorate_tag
(v
, res
, token
)
794 assert p
isa AReassignFormExpr
796 var callsite
= p
.reassign_callsite
797 if callsite
== null then return null
798 return callsite
.infobox
(v
)
802 redef class AModuleName
803 redef fun decorate_tag
(v
, res
, token
)
805 return parent
.decorate_tag
(v
, res
, token
)
809 redef class AModuledecl
810 redef fun decorate_tag
(v
, res
, token
)
812 res
.add_class
("nc_def")
813 res
.add_class
("nc_m")
817 if mm
== null then return null
822 redef class AStdImport
823 redef fun decorate_tag
(v
, res
, token
)
825 res
.add_class
("nc_m")
827 if mm
== null then return null
831 redef class AAttrPropdef
832 redef fun decorate_tag
(v
, res
, token
)
834 res
.add_class
("nc_def")
835 var mpd
: nullable MPropDef
837 if mpd
== null then mpd
= mpropdef
838 if mpd
== null then return null
839 return mpd
.infobox
(v
)
844 redef fun make_tag
(v
)
848 if p
!= null then p
.decorate_tag
(v
, res
, self)
849 res
.add_class
("nc_i")
854 redef fun make_tag
(v
)
856 var res
= new HTMLTag("span")
857 res
.add_class
("nc_def")
860 redef fun decorate_tag
(v
, res
, token
)
863 # nothing to decorate
868 if not p
isa AMethPropdef then return null
870 if mpd
== null then return null
871 return mpd
.infobox
(v
)
875 redef fun make_tag
(v
)
879 if p
!= null then p
.decorate_tag
(v
, res
, self)
880 res
.add_class
("nc_a")
884 redef class AAttrFormExpr
885 redef fun decorate_tag
(v
, res
, token
)
888 if p
== null then return null
889 return p
.intro
.infobox
(v
)
893 redef fun make_tag
(v
)
897 if p
!= null then p
.decorate_tag
(v
, res
, self)
898 res
.add_class
("nc_t")
903 redef fun decorate_tag
(v
, res
, token
)
906 if mt
== null then return null
908 if mt
isa MFormalType then
909 res
.add_class
("nc_vt")
914 redef class AFormaldef
915 redef fun decorate_tag
(v
, res
, token
)
917 res
.add_class
("nc_vt")
918 if mtype
== null then return null
919 return mtype
.infobox
(v
)
922 redef class ATypePropdef
923 redef fun decorate_tag
(v
, res
, token
)
925 res
.add_class
("nc_def")
927 if md
== null then return null
932 redef fun make_tag
(v
)
935 if not parent
isa ADoc then
936 res
.add_class
("nc_c")
942 redef fun make_tag
(v
)
944 var res
= new HTMLTag("span")
945 res
.add_class
("nc_d")
949 redef class TokenLiteral
950 redef fun make_tag
(v
)
953 res
.add_class
("nc_l")
955 if p
!= null then p
.decorate_tag
(v
, res
, self)
959 redef class ASuperstringExpr
960 redef fun make_tag
(v
)
962 var res
= new HTMLTag("span")
963 res
.add_class
("nc_ss")
967 redef class AStringFormExpr
968 redef fun decorate_tag
(v
, res
, token
)
970 # Workaround to tag strings
971 res
.classes
.remove
("nc_l")
972 res
.add_class
("nc_s")
977 redef fun decorate_tag
(v
, res
, token
)
980 if t
== null then return null