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 # When highlighting a node, also consider the loose tokens around it.
53 # Loose tokens are tokens discarded from the AST but attached before
54 # or after some non-loose tokens. See `Token::is_loose`.
56 # When this flag is set to `true`, the loose tokens that are before the
57 # first token and after the last token are also highlighted.
60 var include_loose_tokens
= false is writable
62 # The entry-point of the highlighting.
63 # Will fill `html` with the generated HTML content.
64 fun enter_visit
(n
: ANode)
77 if f
== null then return
79 if l
== null then return
82 if include_loose_tokens
then
83 if f
.prev_looses
.not_empty
then f
= f
.prev_looses
.first
84 if l
.next_looses
.not_empty
then l
= l
.next_looses
.last
90 private fun full_tag
(anode
: ANode, hv
: HighlightVisitor): nullable HTMLTag
92 var tag
= anode
.make_tag
(hv
)
93 if tag
== null then return null
94 var infobox
= anode
.infobox
(hv
)
95 if infobox
== null and anode
isa Token then
99 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
100 infobox
= pa
.decorate_tag
(hv
, tag
, anode
)
104 var messages
= anode
.location
.messages
105 if messages
!= null then
106 tag
.css
("border-bottom", "solid 2px red")
107 if infobox
== null then
108 infobox
= new HInfoBox(hv
, "Messages")
110 var c
= infobox
.new_dropdown
("{messages.length} message(s)", "")
112 c
.open
("li").append
(m
.text
)
115 if infobox
!= null then
116 tag
.attach_infobox
(infobox
)
121 # Produce HTML between two tokens
122 protected fun htmlize
(first_token
, last_token
: Token)
124 var stack2
= new Array[HTMLTag]
125 var stack
= new Array[Prod]
127 var c
: nullable Token = first_token
128 var hv
= new HighlightVisitor
132 # Handle start of line
133 var cline
= c
.location
.line_start
134 if cline
!= line
then
135 # Handle starting block productions,
136 # Because c could be a detached token, get prods in
137 # the first AST token
138 var c0
= c
.first_token_in_line
140 if c0
!= null then starting
= c0
.starting_prods
141 if starting
!= null then for p
in starting
do
142 if not p
.is_block
then continue
143 var tag
= full_tag
(p
, hv
)
144 if tag
== null then continue
145 tag
.add_class
("foldable")
152 # Add a div for the whole line
153 var tag
= new HTMLTag("span")
154 var p
= line_id_prefix
155 if p
!= "" then tag
.attrs
["id"] = "{p}{cline}"
156 tag
.classes
.add
"line"
163 # Add the blank, verbatim
164 html
.add_raw_html c
.blank_before
166 # Handle starting span production
167 starting
= c
.starting_prods
168 if starting
!= null then for p
in starting
do
169 if not p
.is_span
then continue
170 var tag
= full_tag
(p
, hv
)
171 if tag
== null then continue
182 var tag
= full_tag
(c
, hv
)
183 if tag
!= null then html
.add tag
186 # Handle ending span productions
187 var ending
= c
.ending_prods
188 if ending
!= null then for p
in ending
do
189 if not p
.is_span
then continue
190 if stack
.is_empty
or p
!= stack
.last
then continue
195 # Handle end of line and end of file
197 if c
== last_token
then n
= null
198 if n
== null or n
.location
.line_start
!= line
then
199 # closes the line div
202 # close the block production divs
203 var c0
= c
.last_token_in_line
205 if c0
!= null then ending
= c0
.ending_prods
206 if ending
!= null then for p
in ending
do
207 if not p
.is_block
then continue
208 if stack
.is_empty
or p
!= stack
.last
then continue
216 #assert stack.is_empty
217 #assert stack2.is_empty
220 # Return a default CSS content related to CSS classes used in the `html` tree.
221 # Could be inlined in the `.html` file of saved as a specific `.css` file.
222 fun css_content
: String
225 .nitcode a { color: inherit; cursor:pointer; }
226 .nitcode .popupable:hover { text-decoration: underline; cursor:help; } /* underline titles */
227 .nitcode .foldable { display: block } /* for block productions*/
228 .nitcode .line{ display: block } /* for lines */
229 .nitcode .line:hover{ background-color: #FFFFE0; } /* current line */
230 .nitcode :target { background-color: #FFF3C2 } /* target highlight*/
231 /* lexical raw tokens. independent of usage or semantic: */
232 .nitcode .nc_c { color: gray; font-style: italic; } /* comment */
233 .nitcode .nc_d { color: #3D8127; font-style: italic; } /* documentation comments */
234 .nitcode .nc_k { font-weight: bold; } /* keyword */
235 .nitcode .nc_o {} /* operator */
236 .nitcode .nc_i {} /* standard identifier */
237 .nitcode .nc_t { color: #445588; font-weight: bold; } /* type/class identifier */
238 .nitcode .nc_a { color: #445588; font-style: italic; } /* old style attribute identifier */
239 .nitcode .nc_l { color: #009999; } /* char and number literal */
240 .nitcode .nc_s { color: #8F1546; } /* string literal */
241 /* syntactic token usage. added because of their position in the AST */
242 .nitcode .nc_ast { color: blue; } /* assert label */
243 .nitcode .nc_la { color: blue; } /* break/continue label */
244 .nitcode .nc_m { color: #445588; } /* module name */
245 /* syntactic groups */
246 .nitcode .nc_def { font-weight: bold; color: blue; } /* name used in a definition */
247 .nitcode .nc_def.nc_a { color: blue; } /* name used in a attribute definition */
248 .nitcode .nc_def.nc_t { color: blue; } /* name used in a class or vt definition */
249 .nitcode .nc_ss { color: #9E6BEB; } /* superstrings */
250 .nitcode .nc_cdef {} /* A whole class definition */
251 .nitcode .nc_pdef {} /* A whole property definition */
252 /* semantic token usage */
253 .nitcode .nc_v { font-style: italic; } /* local variable or parameter */
254 .nitcode .nc_vt { font-style: italic; } /* virtual type or formal type */
256 .nitcode .nc_error { border: 1px red solid;} /* not used */
257 .popover { max-width: 800px !important; }
261 # Additional content to inject in the <head> tag
262 # Note: does not include `css_content`; handle it yourself.
263 fun head_content
: String
265 return """<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">\n"""
268 # Additional content to inject just before the closing </body> tag
269 fun foot_content
: String
272 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
273 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
274 <script>$(".popupable").popover({html:true, placement:'top'})/*initialize bootstrap popover*/</script>"""
279 # Attach the infobox to the node by using BootStrap popover
280 fun attach_infobox
(infobox
: HInfoBox)
282 classes
.add
("popupable")
283 attrs
["title"] = infobox
.title
284 var href
= infobox
.href
286 attrs
["data-title"] = """<a href="{{{href}}}">{{{infobox.title}}}</a>"""
288 attrs
["data-content"] = infobox
.content
.write_to_string
289 attrs
["data-toggle"] = "popover"
294 # A generic information container that can be used to decorate AST entities
296 # The visitor used for contextualisation, if needed
297 var visitor
: HighlightVisitor
299 # A short title for the AST element
302 # The primary link where the entity points
304 var href
: nullable String = null
306 # The content of the popuped infobox
307 var content
= new HTMLTag("div")
309 # Append a new field in the popuped infobox
310 fun new_field
(title
: String): HTMLTag
312 content
.open
("b").text
(title
)
314 var res
= content
.open
("span")
319 # Append a new dropdown in the popuped content
320 fun new_dropdown
(title
, text
: String): HTMLTag
322 content
.add_raw_html
"""<div class="dropdown"> <a data-toggle="dropdown" href="#"><b>"""
323 content
.append
(title
)
324 content
.add_raw_html
"</b> "
326 content
.add_raw_html
"""<span class="caret"></span></a>"""
327 var res
= content
.open
("ul").add_class
("dropdown-menu").attr
("role", "menu").attr
("aria-labelledby", "dLabel")
328 content
.add_raw_html
"</div>"
335 # Model entity or whatever that can produce an infobox
336 interface HInfoBoxable
337 # An new infobox documenting the entity
338 fun infobox
(v
: HighlightVisitor): HInfoBox is abstract
340 # A human-readable hyper-text for the entity
341 fun linkto
: HTMLTag is abstract
345 # Append an entry for the doc in the given infobox
346 fun fill_infobox
(res
: HInfoBox)
348 if content
.length
< 2 then
349 res
.new_field
("doc").text
(content
.first
)
352 var c
= res
.new_dropdown
("doc", content
.first
)
353 for x
in content
.iterator
.skip_head
(1) do
355 c
.add_raw_html
"<br>"
367 var res
= new HInfoBox(v
, "module {name}")
369 res
.new_field
("module").add
(linkto
)
371 if mdoc
!= null then mdoc
.fill_infobox
(res
)
372 if in_importation
.greaters
.length
> 1 then
373 var c
= res
.new_dropdown
("imports", "{in_importation.greaters.length-1} modules")
374 for x
in in_importation
.greaters
do
375 if x
== self then continue
376 c
.open
("li").add x
.linkto
382 # The module HTML page
385 return c_name
+ ".html"
388 redef fun linkto
do return linkto_text
(name
)
390 # Link to the entitiy with a specific text
391 fun linkto_text
(text
: String): HTMLTag
393 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
397 redef class MClassDef
400 var res
= new HInfoBox(v
, "class {mclass.name}")
403 res
.new_field
("class").text
(mclass
.name
)
405 res
.new_field
("redef class").text
(mclass
.name
)
406 res
.new_field
("intro").add mclass
.intro
.linkto_text
("in {mclass.intro_mmodule.to_s}")
409 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
410 if mdoc
!= null then mdoc
.fill_infobox
(res
)
412 if in_hierarchy
== null then return res
414 if in_hierarchy
.greaters
.length
> 1 then
415 var c
= res
.new_dropdown
("hier", "super-classes")
416 for x
in in_hierarchy
.greaters
do
417 if x
== self then continue
418 if not x
.is_intro
then continue
419 c
.open
("li").add x
.linkto
422 if in_hierarchy
.smallers
.length
> 1 then
423 var c
= res
.new_dropdown
("hier", "sub-classes")
424 for x
in in_hierarchy
.smallers
do
425 if x
== self then continue
426 if not x
.is_intro
then continue
427 c
.open
("li").add x
.linkto
430 if mclass
.mclassdefs
.length
> 1 then
431 var c
= res
.new_dropdown
("redefs", "refinements")
432 for x
in mclass
.mclassdefs
do
433 if x
== self then continue
434 c
.open
("li").add x
.linkto_text
("in {x.mmodule}")
440 # The class HTML page (an anchor in the module page)
443 return mmodule
.href
+ "#" + to_s
446 redef fun linkto
do return linkto_text
(mclass
.name
)
448 # Link to the entitiy with a specific text
449 fun linkto_text
(text
: String): HTMLTag
451 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
458 var res
= new HInfoBox(v
, to_s
)
460 if self isa MMethodDef then
461 if msignature
!= null then res
.new_field
("fun").append
(mproperty
.name
).add msignature
.linkto
462 else if self isa MAttributeDef then
463 if static_mtype
!= null then res
.new_field
("fun").append
(mproperty
.name
).add static_mtype
.linkto
464 else if self isa MVirtualTypeDef then
465 if bound
!= null then res
.new_field
("add").append
(mproperty
.name
).add bound
.linkto
467 res
.new_field
("wat?").append
(mproperty
.name
)
472 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
475 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
476 if mdoc
!= null then mdoc
.fill_infobox
(res
)
477 if mproperty
.mpropdefs
.length
> 1 then
478 var c
= res
.new_dropdown
("redef", "redefinitions")
479 for x
in mproperty
.mpropdefs
do
480 c
.open
("li").add x
.linkto_text
("in {x.mclassdef}")
487 # The property HTML page (an anchor in the module page)
490 return self.mclassdef
.mmodule
.href
+ "#" + self.to_s
493 redef fun linkto
do return linkto_text
(mproperty
.name
)
495 # Link to the entitiy with a specific text
496 fun linkto_text
(text
: String): HTMLTag
498 return (new HTMLTag("a")).attr
("href", href
).text
(text
)
502 redef class MClassType
505 var res
= new HInfoBox(v
, to_s
)
506 res
.href
= mclass
.intro
.href
507 res
.new_field
("class").add mclass
.intro
.linkto
508 var mdoc
= mclass
.mdoc
509 if mdoc
== null then mdoc
= mclass
.intro
.mdoc
510 if mdoc
!= null then mdoc
.fill_infobox
(res
)
515 return mclass
.intro
.linkto
518 redef class MVirtualType
521 var res
= new HInfoBox(v
, to_s
)
522 res
.href
= mproperty
.intro
.href
525 res
.new_field
("virtual type").add pd
.linkto
527 if mdoc
!= null then mdoc
.fill_infobox
(res
)
532 return mproperty
.intro
.linkto
535 redef class MParameterType
538 var res
= new HInfoBox(v
, to_s
)
539 res
.new_field
("parameter type").append
("{name} from class ").add mclass
.intro
.linkto
544 return (new HTMLTag("span")).text
(name
)
548 redef class MNullableType
551 return mtype
.infobox
(v
)
555 var res
= new HTMLTag("span")
556 res
.append
("nullable ").add
(mtype
.linkto
)
561 redef class MNotNullType
564 return mtype
.infobox
(v
)
568 var res
= new HTMLTag("span")
569 res
.append
("not null ").add
(mtype
.linkto
)
574 redef class MNullType
577 var res
= new HInfoBox(v
, to_s
)
582 var res
= new HTMLTag("span")
588 redef class MSignature
591 var res
= new HTMLTag("span")
593 if not mparameters
.is_empty
then
595 for p
in mparameters
do
603 res
.add p
.mtype
.linkto
607 var ret
= return_mtype
619 var res
= new HInfoBox(v
, "call {mpropdef}")
620 res
.href
= mpropdef
.href
621 res
.new_field
("call").add
(mpropdef
.linkto
).add
(msignature
.linkto
)
622 if mpropdef
.is_intro
then
624 res
.new_field
("intro").add mproperty
.intro
.linkto_text
("in {mproperty.intro.mclassdef}")
626 var mdoc
= mpropdef
.mdoc
627 if mdoc
== null then mdoc
= mproperty
.intro
.mdoc
628 if mdoc
!= null then mdoc
.fill_infobox
(res
)
634 return mpropdef
.linkto
642 var declared_type
= self.declared_type
643 if declared_type
== null then
644 var res
= new HInfoBox(v
, "{name}")
645 res
.new_field
("local var").append
("{name}")
648 var res
= new HInfoBox(v
, "{name}: {declared_type}")
649 res
.new_field
("local var").append
("{name}:").add
(declared_type
.linkto
)
654 return (new HTMLTag("span")).text
(name
)
662 # Optionally creates a tag that encapsulate the AST element on HTML rendering
663 protected fun make_tag
(v
: HighlightVisitor): nullable HTMLTag do return null
665 # Add aditionnal information on a child-token and return an additionnal HInfoBox on it
666 protected fun decorate_tag
(v
: HighlightVisitor, res
: HTMLTag, token
: Token): nullable HInfoBox
668 #debug("no decoration for {token.inspect}")
669 #res.add_class("nc_error")
673 # Return a optional infobox
674 fun infobox
(v
: HighlightVisitor): nullable HInfoBox do return null
677 redef class AStdClassdef
678 redef fun make_tag
(v
)
680 var res
= new HTMLTag("span")
681 res
.add_class
("nc_cdef")
683 if md
!= null then res
.attr
("id", md
.to_s
)
686 redef fun decorate_tag
(v
, res
, token
)
688 if not token
isa TClassid then return null
689 res
.add_class
("nc_def")
692 if md
== null then return null
697 redef fun make_tag
(v
)
699 var res
= new HTMLTag("span")
700 res
.add_class
("nc_pdef")
705 res
.attr
("id", mpd
.to_s
)
707 if self isa AAttrPropdef then
709 if mpd
!= null then res
.add
(tag
(mpd
))
711 if mpd
!= null then res
.add
(tag
(mpd
))
716 private fun tag
(mpd
: MPropDef): HTMLTag
718 var a
= new HTMLTag("a")
719 a
.attr
("id", mpd
.to_s
)
725 # Produce an HTMLTag with the correct contents and CSS classes
726 # Subclasses can redefine it to decorate the tag
727 redef fun make_tag
(v
: HighlightVisitor): HTMLTag
729 var res
= new HTMLTag("span")
735 redef class TokenKeyword
736 redef fun make_tag
(v
)
739 res
.add_class
("nc_k")
743 redef class TokenOperator
744 redef fun make_tag
(v
)
748 if p
!= null then p
.decorate_tag
(v
, res
, self)
749 res
.add_class
("nc_o")
754 redef class AVarFormExpr
755 redef fun decorate_tag
(v
, res
, token
)
757 var variable
= self.variable
758 if variable
== null then return null
759 res
.add_class
("nc_v")
760 return variable
.infobox
(v
)
764 redef class AVardeclExpr
765 redef fun decorate_tag
(v
, res
, token
)
767 var variable
= self.variable
768 if variable
== null then return null
769 res
.add_class
("nc_v")
770 return variable
.infobox
(v
)
774 redef class AForGroup
775 redef fun decorate_tag
(v
, res
, token
)
777 if not token
isa TId then return null
779 if vs
== null then return null
780 res
.add_class
("nc_v")
781 var idx
= n_ids
.index_of
(token
)
782 var variable
= vs
[idx
]
783 return variable
.infobox
(v
)
788 redef fun decorate_tag
(v
, res
, token
)
791 if mp
== null then return null
792 var variable
= self.variable
793 if variable
== null then return null
794 res
.add_class
("nc_v")
795 return variable
.infobox
(v
)
799 redef class AAssertExpr
800 redef fun decorate_tag
(v
, res
, token
)
802 res
.add_class
("nc_ast")
808 redef fun decorate_tag
(v
, res
, token
)
810 res
.add_class
("nc_la")
815 redef class ASendExpr
816 redef fun decorate_tag
(v
, res
, token
)
818 if callsite
== null then return null
819 return callsite
.infobox
(v
)
824 redef fun decorate_tag
(v
, res
, token
)
826 if callsite
== null then return null
827 return callsite
.infobox
(v
)
831 redef class AAssignOp
832 redef fun decorate_tag
(v
, res
, token
)
835 assert p
isa AReassignFormExpr
837 var callsite
= p
.reassign_callsite
838 if callsite
== null then return null
839 return callsite
.infobox
(v
)
843 redef class AModuleName
844 redef fun decorate_tag
(v
, res
, token
)
846 return parent
.decorate_tag
(v
, res
, token
)
850 redef class AModuledecl
851 redef fun decorate_tag
(v
, res
, token
)
853 res
.add_class
("nc_def")
854 res
.add_class
("nc_m")
858 if mm
== null then return null
863 redef class AStdImport
864 redef fun decorate_tag
(v
, res
, token
)
866 res
.add_class
("nc_m")
868 if mm
== null then return null
872 redef class AAttrPropdef
873 redef fun decorate_tag
(v
, res
, token
)
875 res
.add_class
("nc_def")
876 var mpd
: nullable MPropDef
878 if mpd
== null then mpd
= mpropdef
879 if mpd
== null then return null
880 return mpd
.infobox
(v
)
885 redef fun make_tag
(v
)
889 if p
!= null then p
.decorate_tag
(v
, res
, self)
890 res
.add_class
("nc_i")
895 redef fun make_tag
(v
)
897 var res
= new HTMLTag("span")
898 res
.add_class
("nc_def")
901 redef fun decorate_tag
(v
, res
, token
)
904 # nothing to decorate
909 if not p
isa AMethPropdef then return null
911 if mpd
== null then return null
912 return mpd
.infobox
(v
)
916 redef fun make_tag
(v
)
920 if p
!= null then p
.decorate_tag
(v
, res
, self)
921 res
.add_class
("nc_a")
925 redef class AAttrFormExpr
926 redef fun decorate_tag
(v
, res
, token
)
929 if p
== null then return null
930 return p
.intro
.infobox
(v
)
934 redef fun make_tag
(v
)
938 if p
!= null then p
.decorate_tag
(v
, res
, self)
939 res
.add_class
("nc_t")
944 redef fun decorate_tag
(v
, res
, token
)
947 if mt
== null then return null
949 if mt
isa MFormalType then
950 res
.add_class
("nc_vt")
955 redef class AFormaldef
956 redef fun decorate_tag
(v
, res
, token
)
958 res
.add_class
("nc_vt")
959 if mtype
== null then return null
960 return mtype
.infobox
(v
)
963 redef class ATypePropdef
964 redef fun decorate_tag
(v
, res
, token
)
966 res
.add_class
("nc_def")
968 if md
== null then return null
973 redef fun make_tag
(v
)
976 if not parent
isa ADoc then
977 res
.add_class
("nc_c")
983 redef fun make_tag
(v
)
985 var res
= new HTMLTag("span")
986 res
.add_class
("nc_d")
990 redef class TokenLiteral
991 redef fun make_tag
(v
)
994 res
.add_class
("nc_l")
996 if p
!= null then p
.decorate_tag
(v
, res
, self)
1000 redef class ASuperstringExpr
1001 redef fun make_tag
(v
)
1003 var res
= new HTMLTag("span")
1004 res
.add_class
("nc_ss")
1008 redef class AStringFormExpr
1009 redef fun decorate_tag
(v
, res
, token
)
1011 # Workaround to tag strings
1012 res
.classes
.remove
("nc_l")
1013 res
.add_class
("nc_s")
1018 redef fun decorate_tag
(v
, res
, token
)
1021 if t
== null then return null