1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # print AST in an human form
22 private import literal
24 private class ASTPrinterVisitor
28 node
.accept_printer
(self)
31 var out
= new List[String]
38 if has_eol
then return
40 for x
in [0..indent_level
[ do out
.add
("\t")
44 var last_current
: nullable ANode = null
48 if last_current
!= current_node
then
49 last_current
= current_node
50 var l
= current_node
._location
54 out
.add
("\t# {l.colored_line("0;32").split_with('\n').first}")
64 fun indent
do indent_level
+= 1
65 fun unindent
do indent_level
-= 1
69 # print the tree (using the semantic information) on screen
70 # This method is used to debug AST transformations
73 var v
= new ASTPrinterVisitor
81 private fun accept_printer
(v
: ASTPrinterVisitor)
92 redef class ABlockExpr
93 redef fun accept_printer
(v
)
103 redef fun accept_printer
(v
)
110 redef fun accept_printer
(v
)
112 v
.write
("new {mtype.as(not null)}.{callsite.mproperty}")
113 if not n_args
.n_exprs
.is_empty
then
117 for a
in n_args
.n_exprs
do
118 if is_first
then is_first
= false else v
.write
(",")
127 redef class ASendExpr
128 redef fun accept_printer
(v
)
130 v
.enter_visit
(n_expr
)
131 v
.write
(".{callsite.mproperty.name}")
132 if not raw_arguments
.is_empty
then
136 for a
in raw_arguments
do
137 if is_first
then is_first
= false else v
.write
(",")
147 redef fun accept_printer
(v
)
149 var name
= variable
.name
150 if name
== "" then name
= "t{variable.object_id}"
155 redef class AVarAssignExpr
156 redef fun accept_printer
(v
)
158 var name
= variable
.name
159 if name
== "" then name
= "t{variable.object_id}"
162 v
.enter_visit
(n_value
)