src: new program `test_highlight`
[nit.git] / src / test_highlight.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Program used to test the Nit highlighter
16 module test_highlight
17
18 import highlight
19 import test_phase
20
21 redef fun do_work(mainmodule, mmodules, mb)
22 do
23 var v = new HighlightVisitor
24 print """<head>
25 <meta charset="utf-8">
26 <style type="text/css">
27 {{{v.css_content}}}
28 </style>
29 {{{v.head_content}}}
30 </head><body>
31 """
32
33 for mm in mmodules do for cd in mm.mclassdefs do for pd in cd.mpropdefs do
34 var n = mb.mpropdef2node(pd)
35 if not n isa APropdef then continue
36 var hl = new HighlightVisitor
37 hl.enter_visit(n)
38 print "<h1>{pd.full_name}</h1>"
39 printn "<pre><code>"
40 hl.html.write_to(stdout)
41 print "</code></pre>"
42 end
43
44 # Some random nodes
45 var thlv = new THLVisitor
46 for mm in mmodules do
47 var n = mb.mmodule2node(mm)
48 thlv.enter_visit(n)
49 end
50
51 print v.foot_content
52 print "</body></html>"
53 end
54
55 class THLVisitor
56 super Visitor
57 var seen = new HashSet[String]
58 redef fun visit(n)
59 do
60 var cn = n.class_name
61 if not seen.has(cn) then
62 seen.add cn
63
64 var hl = new HighlightVisitor
65 hl.enter_visit(n)
66 print "<h2>AST node: {cn} at {n.location}</h2>"
67 printn "<pre><code>"
68 hl.html.write_to(stdout)
69 print "</code></pre>"
70 end
71
72 n.visit_all(self)
73 end
74 end