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 # Library used to pretty print Nit code.
20 # var tc = new ToolContext
21 # var nmodule = tc.parse_something("class A\nfun toto : Int do return 5\nend")
22 # var ppv = new PrettyPrinterVisitor
23 # var pmodule = ppv.pretty(nmodule)
24 # assert pmodule.write_to_string == """
26 # \tfun toto: Int do return 5
29 # See `nitpretty` tool for more documentation.
37 # The `PrettyPrinterVisitor` is used to visit a node and pretty print it.
39 # The main method here is `visit` that performs the pretty printing of a `ANode`.
41 # Because some tokens like `TComment` are not stored in the AST,
42 # we visit the AST like traditionnal visitor and also maintain a
43 # pointer to the `current_token` (actually the next one to be printed).
45 # Visited productions are in charges to move the token pointer using methods such as:
47 # * `consume`: print `current_token` and move to the next one
48 # * `skip`: move to the next token without printing the current one
49 # * `skip_to`: move to a specified token skipping all the tokens before
50 # * `catch_up`: consume all the tokens between `current_token` and a specified token
51 # * `finish_line`: consume all the tokens between `current_token` and the end of line
52 class PrettyPrinterVisitor
54 fun pretty
(n
: ANode): Template do
59 current_token
= n
.first_token
61 else if n
isa Token then
64 while p
!= null and not p
isa Prod do
68 current_token
= p
.first_token
72 return tpl
.as(not null)
75 # Pretty print the whole `nmodule` with comments before and after.
76 fun pretty_nmodule
(nmodule
: AModule): Template do
78 nmodule
.parentize_tokens
79 current_token
= nmodule
.location
.file
.first_token
81 catch_up nmodule
.location
.file
.last_token
83 return tpl
.as(not null)
86 # Prepare `self` for a new visit.
96 # Visit `n` if not null.
97 fun visit
(n
: nullable ANode) do
98 if n
== null then return
99 n
.accept_pretty_printer
self
102 # Visit a list of `Anode`.
103 fun visit_list
(n
: nullable ANodes[ANode]) do
104 if n
== null then return
105 n
.accept_pretty_printer
self
108 # Is the node inlinable and can fit on the line.
109 fun can_inline
(n
: nullable ANode): Bool do
110 if n
== null then return true
111 if n
.must_be_inline
then return true
112 if n
.must_be_block
then return false
114 if n
.collect_length
+ current_length
> max_size
then return false
115 # check block is inlinable
116 return n
.is_inlinable
119 # Collect all `TComment` between `from` and `to`.
120 fun collect_comments
(from
: nullable ANode, to
: nullable ANode): Array[TComment] do
121 var res
= new Array[TComment]
122 if from
isa Prod then from
= from
.first_token
123 if to
isa Prod then to
= to
.first_token
124 if from
== null or to
== null then return res
127 if from
isa TComment then res
.add from
128 from
= from
.as(Token).next_token
134 # Token under cursor.
136 # This is the next token to visit.
137 var current_token
: nullable Token = null
139 # Skip the `current_token`.
140 fun skip
do current_token
= current_token
.next_token
142 # Skip `current_token` until the end of line.
143 fun skip_line
do current_token
= current_token
.last_real_token_in_line
145 # Skip `current_token` until `target` is reached.
146 fun skip_to
(target
: nullable Token) do
147 if target
== null then return
148 while current_token
!= target
do skip
151 # Visit `current_token`.
152 fun consume
(token
: String) do
153 assert current_token
.text
== token
157 # Is there token to visit between `current_token` and `target`?
158 fun need_catch_up
(target
: nullable Token): Bool do
159 if target
== null then return false
160 return current_token
!= target
163 # Visit all tokens between `current_token` and `target`.
164 fun catch_up
(target
: nullable ANode) do
165 if target
== null then return
166 if current_token
== null then return
168 if target
isa Token then
170 else if target
isa Prod then
171 token
= target
.first_token
.as(not null)
175 assert current_token
.location
<= token
.location
176 while current_token
!= token
do visit current_token
179 # Visit all tokens between `current_token` and the end of line.
181 if current_token
isa TComment then
186 while current_token
isa TEol do skip
189 # The template under construction.
190 private var tpl
: nullable Template = null
192 # Current indent level.
195 # Size of a tabulation in spaces.
201 # Length of the current line.
202 var current_length
= 0
204 # Length of the previous line.
205 var previous_length
= 0
207 # Is the last line a blank line?
208 fun last_line_is_blank
: Bool do return previous_length
== 0
210 # Add `t` to current template.
211 fun add
(t
: String) do
212 if t
.is_empty
then return
213 while wait_addn
> 0 do
218 current_length
+= t
.length
223 if current_length
== 0 and last_line_is_blank
then return
224 previous_length
= current_length
229 # End of line chars are stored until another char is added.
230 # This avoid empty files with only a '`\n`'.
231 private var wait_addn
= 0
233 # Add `'\t' * indent`.
234 fun addt
do add
"\t" * indent
239 # Visit explicit receiver, implicit self will be ignored.
240 fun visit_recv
(n_expr
: AExpr) do
241 if not n_expr
isa AImplicitSelfExpr then
248 # Base framework redefs
250 redef class ANodes[E
]
251 private fun accept_pretty_printer
(v
: PrettyPrinterVisitor) do
253 var e_can_inline
= v
.can_inline
(e
)
256 if not e_can_inline
then
273 # Start visit of `self` using a `PrettyPrinterVisitor`
274 private fun accept_pretty_printer
(v
: PrettyPrinterVisitor) is abstract
276 # Collect the length (in `Char`) of the node.
277 private fun collect_length
: Int is abstract
279 # Is `self` printable in one line?
280 private fun is_inlinable
: Bool do return true
282 # Force `self` to be rendered as a block.
283 private var force_block
= false
285 # Does `self` have to be rendered as a block?
286 private fun must_be_block
: Bool do return force_block
288 # Force `self` to be rendered as a line.
289 private var force_inline
= false
291 # Does `self` have be rendered as a line?
292 private fun must_be_inline
: Bool do
293 if parent
!= null and parent
.must_be_inline
then return true
297 # Does `self` was written in one line before transformation?
298 private fun was_inline
: Bool is abstract
302 redef fun accept_pretty_printer
(v
) do
304 v
.current_token
= next_token
307 redef fun collect_length
do return text
.length
308 redef fun is_inlinable
do return true
309 redef fun was_inline
do return true
313 redef fun accept_pretty_printer
(v
) do v
.visit first_token
315 # The token where the production really start (skipping ADoc).
316 private fun start_token
: nullable Token do return first_token
318 # Collect all `TComment` contained in the production
319 # (between `start_token` and `end_token`).
320 private fun collect_comments
: Array[TComment] do
321 var res
= new Array[TComment]
322 if start_token
== null or last_token
== null then return res
323 var token
= start_token
325 while token
!= last_token
do
326 if token
isa TComment then res
.add token
327 token
= token
.next_token
333 redef fun collect_length
do
335 if start_token
== null or last_token
== null then return res
336 var token
= start_token
338 while token
!= last_token
do
339 res
+= token
.text
.length
340 token
= token
.next_token
343 res
+= token
.text
.length
347 redef fun was_inline
do
348 return first_token
.location
.line_start
== last_token
.location
.line_end
355 redef fun accept_pretty_printer
(v
) do
366 if is_last_in_group
then v
.addn
380 if next_token
isa TComment and is_first_in_group
then v
.addn
384 var prev_token
= self.prev_token
385 if prev_token
isa TComment and prev_token
.is_inline
and is_last_in_group
then v
.addn
392 # Is `self` part of an `ADoc`?
393 private fun is_adoc
: Bool do return parent
isa ADoc and parent
.parent
!= null
395 # Is `self` part of a licence?
396 private fun is_licence
: Bool do
397 var prev_token
= self.prev_token
399 if prev_token
== null then
401 else if prev_token
isa TComment then
402 return prev_token
.is_licence
408 # Is `self` starts and ends its line?
409 private fun is_inline
: Bool do
410 return self == first_real_token_in_line
and self == last_real_token_in_line
413 # Is `self` an orphan line (blank before and after)?
414 private fun is_orphan
: Bool do
415 return prev_token
isa TEol and
416 (prev_token
.prev_token
isa TEol or prev_token
.prev_token
isa TComment) and
420 # Is `self` the first comment of a group of comment?
421 private fun is_first_in_group
: Bool do return not prev_token
isa TComment
423 # Is `self` the last comment of a group of comments?
424 private fun is_last_in_group
: Bool do return not next_token
isa TComment
428 redef fun accept_pretty_printer
(v
) do for comment
in n_comment
do v
.visit comment
429 redef fun is_inlinable
do return n_comment
.length
<= 1
434 redef class AAnnotations
435 redef fun accept_pretty_printer
(v
) do
439 if v
.can_inline
(self) then
441 for n_item
in n_items
do
443 if n_item
!= n_items
.last
then
448 else if n_items
.length
> 1 then
452 for n_item
in n_items
do
461 if not was_inline
and v
.current_token
isa TKwend then v
.skip
464 redef fun is_inlinable
do
465 if not super then return false
466 for annot
in n_items
do if not annot
.is_inlinable
then return false
471 redef class AAnnotation
472 redef fun accept_pretty_printer
(v
) do
474 if not n_args
.is_empty
then
475 if n_opar
== null then
486 redef class ATypeExpr
487 redef fun accept_pretty_printer
(v
) do v
.visit n_type
493 redef fun accept_pretty_printer
(v
) do
494 v
.catch_up start_token
497 if not n_imports
.is_empty
then
500 for n_import
in n_imports
do
506 if not n_extern_code_blocks
.is_empty
then
509 for n_extern_block
in n_extern_code_blocks
do
510 v
.catch_up n_extern_block
511 v
.visit n_extern_block
513 if n_extern_block
!= n_extern_code_blocks
.last
then v
.addn
516 if not n_classdefs
.is_empty
then v
.addn
519 if not n_classdefs
.is_empty
then
522 for n_classdef
in n_classdefs
do
523 v
.catch_up n_classdef
525 if n_classdef
!= n_classdefs
.last
then v
.addn
533 redef fun start_token
do
534 if n_moduledecl
!= null then return n_moduledecl
.first_token
535 if not n_imports
.is_empty
then return n_imports
.first
.first_token
536 if not n_classdefs
.is_empty
then return n_classdefs
.first
.first_token
540 redef fun is_inlinable
do return false
543 redef class AModuledecl
544 redef fun accept_pretty_printer
(v
) do
550 if n_annotations
!= null then
551 var annot_inline
= v
.can_inline
(n_annotations
)
552 v
.visit n_annotations
554 if not annot_inline
then
555 if v
.current_token
isa TKwend then
569 redef class AModuleName
570 redef fun accept_pretty_printer
(v
) do
571 for path
in n_path
do
580 redef class ANoImport
581 redef fun accept_pretty_printer
(v
) do
590 redef class AStdImport
591 redef fun accept_pretty_printer
(v
) do
592 if not n_visibility
isa APublicVisibility then
607 redef class AClassdef
608 redef fun accept_pretty_printer
(v
) do
609 for n_propdef
in n_propdefs
do
612 if n_propdef
.n_doc
!= null or not v
.can_inline
(n_propdef
) then
613 if n_propdef
!= n_propdefs
.first
then v
.addn
615 if n_propdef
!= n_propdefs
.last
then v
.addn
623 redef class AStdClassdef
624 redef fun accept_pretty_printer
(v
) do
626 var can_inline
= v
.can_inline
(self)
628 if not n_visibility
isa APublicVisibility then
633 if n_kwredef
!= null then
642 if not n_formaldefs
.is_empty
then
644 v
.visit_list n_formaldefs
648 if n_extern_code_block
!= null then
650 v
.visit n_extern_code_block
656 if not n_superclasses
.is_empty
then
657 for n_superclass
in n_superclasses
do
667 for n_superclass
in n_superclasses
do
668 v
.catch_up n_superclass
675 if not n_superclasses
.is_empty
and not n_propdefs
.is_empty
then
690 redef fun is_inlinable
do
691 if not super then return false
692 if not n_propdefs
.is_empty
then return false
693 if n_superclasses
.length
> 1 then return false
694 if not collect_comments
.is_empty
then return false
698 redef fun start_token
do
699 if not n_visibility
isa APublicVisibility then return n_visibility
.first_token
700 if n_kwredef
!= null then return n_kwredef
701 return n_classkind
.first_token
705 redef class AAbstractClasskind
706 redef fun accept_pretty_printer
(v
) do
713 redef class AExternClasskind
714 redef fun accept_pretty_printer
(v
) do
721 redef class AFormaldef
722 redef fun accept_pretty_printer
(v
) do
725 if n_type
!= null then
734 redef fun accept_pretty_printer
(v
) do
735 if n_kwnullable
!= null then
742 if not n_types
.is_empty
then
750 redef class ASuperclass
751 redef fun accept_pretty_printer
(v
) do
761 redef fun accept_pretty_printer
(v
) do
765 if not n_visibility
isa APublicVisibility then
770 if n_kwredef
!= null then
776 # Factorize annotations visit for all APropdef.
778 # Return true if annotations were inlined.
779 fun visit_annotations
(v
: PrettyPrinterVisitor, n_annotations
: nullable AAnnotations): Bool do
780 var res
= v
.can_inline
(n_annotations
)
781 if n_annotations
!= null then v
.visit n_annotations
785 # Factorize block visit for APropdefs.
787 # Were annotations printed inline? If so, we need to print the block differently.
788 fun visit_block
(v
: PrettyPrinterVisitor, n_block
: nullable AExpr, annot_inline
: Bool) do
789 if n_block
== null then return
790 while not v
.current_token
isa TKwdo do v
.skip
791 if n_annotations
!= null and not annot_inline
then
798 if v
.can_inline
(n_block
) then
801 if n_block
isa ABlockExpr then
802 if n_block
.n_expr
.is_empty
then
803 v
.visit n_block
.n_kwend
805 v
.visit n_block
.n_expr
.first
806 v
.current_token
= n_block
.n_kwend
811 if v
.current_token
isa TKwend then v
.skip
818 if n_block
isa ABlockExpr then
819 n_block
.force_block
= true
821 v
.catch_up n_block
.n_kwend
830 if n_block
isa ABlockExpr then
831 v
.visit n_block
.n_kwend
839 redef fun start_token
do
840 if n_doc
== null then return super
841 return n_doc
.last_token
.next_token
845 redef class AAttrPropdef
846 redef fun accept_pretty_printer
(v
) do
852 if n_type
!= null then
858 if n_expr
!= null then
865 var annot_inline
= visit_annotations
(v
, n_annotations
)
866 visit_block
(v
, n_block
, annot_inline
)
871 redef fun first_token
do
872 if n_doc
!= null then return n_doc
.first_token
873 if not n_visibility
isa APublicVisibility then return n_visibility
.first_token
874 if n_kwredef
!= null then return n_kwredef
878 redef fun is_inlinable
do return true
881 redef class ATypePropdef
882 redef fun accept_pretty_printer
(v
) do
890 visit_annotations
(v
, n_annotations
)
895 redef fun is_inlinable
do return true
898 redef class AMethPropdef
899 redef fun accept_pretty_printer
(v
) do
900 # TODO: Handle extern annotations
902 var before
= v
.indent
904 if n_kwinit
!= null then v
.visit n_kwinit
905 if n_kwmeth
!= null then v
.visit n_kwmeth
906 if n_kwnew
!= null then v
.visit n_kwnew
908 if not n_methid
== null then
915 var annot_inline
= visit_annotations
(v
, n_annotations
)
917 if n_extern_calls
!= null or n_extern_code_block
!= null then
919 if n_extern_calls
!= null then v
.visit n_extern_calls
920 if n_extern_code_block
!= null then v
.visit n_extern_code_block
923 visit_block
(v
, n_block
, annot_inline
)
925 assert v
.indent
== before
929 # * block is empty or can be inlined
930 # * contains no comments
931 redef fun is_inlinable
do
932 if not super then return false
933 if n_annotations
!= null and not n_annotations
.is_inlinable
then return false
934 if n_block
!= null and not n_block
.is_inlinable
then return false
935 if n_extern_calls
!= null and not n_extern_calls
.is_inlinable
then return false
936 if n_extern_code_block
!= null and not n_extern_code_block
.is_inlinable
then return false
937 if not collect_comments
.is_empty
then return false
942 redef class AMainMethPropdef
943 redef fun accept_pretty_printer
(v
) do
949 redef class ASignature
950 redef fun accept_pretty_printer
(v
) do
951 if not n_params
.is_empty
then
953 v
.visit_list n_params
957 if n_type
!= null then
966 redef fun accept_pretty_printer
(v
) do
969 if n_type
!= null then
975 if n_dotdotdot
!= null then v
.visit n_dotdotdot
981 redef class AExternCalls
982 redef fun accept_pretty_printer
(v
) do
983 var can_inline
= v
.can_inline
(self)
988 v
.visit_list n_extern_calls
994 v
.visit_list n_extern_calls
1001 redef class AFullPropExternCall
1002 redef fun accept_pretty_printer
(v
) do
1009 redef class ALocalPropExternCall
1010 redef fun accept_pretty_printer
(v
) do v
.visit n_methid
1013 redef class AInitPropExternCall
1014 redef fun accept_pretty_printer
(v
) do v
.visit n_type
1017 redef class ACastAsExternCall
1018 redef fun accept_pretty_printer
(v
) do
1028 redef class AAsNullableExternCall
1029 redef fun accept_pretty_printer
(v
) do
1034 v
.visit n_kwnullable
1038 redef class AAsNotNullableExternCall
1039 redef fun accept_pretty_printer
(v
) do
1046 v
.visit n_kwnullable
1050 redef class AExternCodeBlock
1051 redef fun accept_pretty_printer
(v
) do
1052 if n_in_language
!= null then
1053 v
.visit n_in_language
1057 v
.visit n_extern_code_segment
1060 redef fun is_inlinable
do
1061 if not super then return false
1062 return n_extern_code_segment
.is_inlinable
1066 redef class AInLanguage
1067 redef fun accept_pretty_printer
(v
) do
1074 redef class TExternCodeSegment
1075 redef fun accept_pretty_printer
(v
) do
1076 var can_inline
= v
.can_inline
(self)
1081 var text
= text
.substring
(2, text
.length
- 4)
1082 var lines
= text
.r_trim
.split
("\n")
1084 if text
.is_empty
then
1089 if not lines
.first
.trim
.is_empty
then
1097 for line
in lines
do
1106 v
.current_token
= next_token
1110 redef fun is_inlinable
do
1111 if not super then return false
1112 return location
.line_start
== location
.line_end
1118 redef class ABlockExpr
1119 redef fun accept_pretty_printer
(v
) do
1120 var before
= v
.indent
1121 var can_inline
= v
.can_inline
(self)
1123 if can_inline
and not n_expr
.is_empty
then
1124 v
.visit n_expr
.first
1127 for nexpr
in n_expr
do
1128 var expr_inline
= v
.can_inline
(nexpr
)
1129 if not expr_inline
and nexpr
!= n_expr
.first
then v
.addn
1135 if not expr_inline
and nexpr
!= n_expr
.last
then v
.addn
1139 assert v
.indent
== before
1142 redef fun is_inlinable
do
1143 if not super then return false
1144 if not collect_comments
.is_empty
then return false
1146 if not n_expr
.is_empty
then
1147 if n_expr
.length
> 1 then return false
1148 if not n_expr
.first
.is_inlinable
then return false
1156 redef fun accept_pretty_printer
(v
) do
1157 var before
= v
.indent
1158 var can_inline
= v
.can_inline
(self)
1162 if v
.can_inline
(n_expr
) then
1171 # skip comments before `then` token
1172 while not v
.current_token
isa TKwthen do v
.skip
1174 var n_else
= self.n_else
1178 if n_then
!= null then v
.visit n_then
1181 n_else
.force_inline
= true
1186 else if n_then
== null then
1190 v
.skip_to last_token
.last_real_token_in_line
1196 if n_then
!= null then
1197 if n_then
isa ABlockExpr then
1198 n_then
.force_block
= true
1208 while not v
.current_token
isa TKwelse do
1209 v
.consume v
.current_token
.text
1216 if n_else
isa AIfExpr then
1217 n_else
.force_block
= true
1225 if n_else
isa ABlockExpr then
1226 n_else
.force_block
= true
1234 if last_token
isa TKwend then
1235 v
.catch_up last_token
1246 if last_token
.location
>= v
.current_token
.location
then v
.catch_up last_token
1250 if v
.current_token
isa TKwend then v
.skip
1254 assert v
.indent
== before
1257 redef fun is_inlinable
do
1258 if not super then return false
1259 if n_then
!= null and not n_then
.is_inlinable
then return false
1260 var n_else
= self.n_else
1261 if (n_else
isa ABlockExpr and not n_else
.n_expr
.is_empty
) or
1262 (not n_else
isa ABlockExpr and n_else
!= null) then
1265 if not collect_comments
.is_empty
then return false
1269 # Does this `if` statement contains a `else` part?
1270 private fun has_else
(v
: PrettyPrinterVisitor): Bool do
1272 if n_else
== null then return false
1273 var n_kwelse
= collect_kwelse
1274 if n_kwelse
== null then return false
1276 if n_else
isa ABlockExpr then
1277 var comments
: Array[TComment]
1279 if n_then
== null then
1280 comments
= v
.collect_comments
(n_expr
.last_token
, n_else
.last_token
)
1282 comments
= v
.collect_comments
(n_then
.last_token
, n_else
.last_token
)
1285 if not comments
.is_empty
then return true
1286 return not n_else
.n_expr
.is_empty
1292 # Lookup for `else` token in `self`.
1293 private fun collect_kwelse
: nullable TKwelse do
1294 var token
= first_token
1296 while token
!= last_token
do
1297 if token
isa TKwelse then return token
1298 token
= token
.next_token
1305 # Used to factorize work on loops.
1306 private class ALoopHelper
1309 fun loop_block
: nullable ANode is abstract
1310 fun loop_label
: nullable ANode is abstract
1312 fun visit_loop_block
(v
: PrettyPrinterVisitor) do
1313 var n_block
= loop_block
1318 if n_block
isa ABlockExpr then
1319 n_block
.force_block
= true
1321 v
.catch_up n_block
.n_kwend
1324 v
.visit n_block
.n_kwend
1334 if loop_label
!= null then
1340 fun visit_loop_inline
(v
: PrettyPrinterVisitor) do
1341 var n_block
= loop_block
1344 if n_block
isa ABlockExpr then
1345 if n_block
.n_expr
.is_empty
then
1346 v
.visit n_block
.n_kwend
1348 v
.visit n_block
.n_expr
.first
1349 v
.current_token
= n_block
.n_kwend
1354 if v
.current_token
isa TKwend then v
.skip
1357 if loop_label
!= null then
1363 redef fun is_inlinable
do
1364 var n_block
= loop_block
1365 if not super then return false
1366 if n_block
isa ABlockExpr and not n_block
.is_inlinable
then return false
1367 if not collect_comments
.is_empty
then return false
1372 redef class ALoopExpr
1375 redef fun loop_block
do return n_block
1376 redef fun loop_label
do return n_label
1378 redef fun accept_pretty_printer
(v
) do
1379 var can_inline
= v
.can_inline
(self)
1381 if can_inline
then visit_loop_inline v
else visit_loop_block v
1385 redef class AWhileExpr
1388 redef fun loop_block
do return n_block
1389 redef fun loop_label
do return n_label
1391 redef fun accept_pretty_printer
(v
) do
1392 var can_inline
= v
.can_inline
(self)
1398 if can_inline
then visit_loop_inline v
else visit_loop_block v
1405 redef fun loop_block
do return n_block
1406 redef fun loop_label
do return n_label
1408 redef fun accept_pretty_printer
(v
) do
1409 var can_inline
= v
.can_inline
(self)
1411 if can_inline
then visit_loop_inline v
else visit_loop_block v
1415 redef class AForExpr
1418 redef fun loop_block
do return n_block
1419 redef fun loop_label
do return n_label
1421 redef fun accept_pretty_printer
(v
) do
1422 var can_inline
= v
.can_inline
(self)
1426 for n_id
in n_ids
do
1428 if n_id
!= n_ids
.last
then v
.add
", "
1437 if can_inline
then visit_loop_inline v
else visit_loop_block v
1441 redef class ABreakExpr
1442 redef fun accept_pretty_printer
(v
) do
1445 if n_expr
!= null then
1450 if n_label
!= null then
1456 redef fun is_inlinable
do return true
1459 redef class AContinueExpr
1460 redef fun accept_pretty_printer
(v
) do
1461 v
.visit n_kwcontinue
1463 if n_expr
!= null then
1468 if n_label
!= null then
1474 redef fun is_inlinable
do return true
1479 redef class ASendExpr
1480 redef fun is_inlinable
do return true
1483 redef class ACallExpr
1484 redef fun accept_pretty_printer
(v
) do
1485 var can_inline
= v
.can_inline
(self)
1488 if not n_expr
isa AImplicitSelfExpr and not can_inline
then
1495 if not n_args
.n_exprs
.is_empty
then
1496 if is_stmt
and n_args
.n_exprs
.length
== 1 then
1498 if v
.current_token
isa TOpar then v
.skip
1499 v
.visit n_args
.n_exprs
.first
1500 if v
.current_token
isa TCpar then v
.skip
1502 if v
.current_token
isa TOpar then
1508 v
.visit_list n_args
.n_exprs
1509 if v
.current_token
isa TCpar then v
.consume
")"
1514 # Is the call alone on its line?
1515 fun is_stmt
: Bool do return parent
isa ABlockExpr
1518 redef class ACallAssignExpr
1519 redef fun accept_pretty_printer
(v
) do
1523 if not n_args
.n_exprs
.is_empty
then
1525 v
.visit_list n_args
.n_exprs
1536 redef class ACallReassignExpr
1537 redef fun accept_pretty_printer
(v
) do
1541 if not n_args
.n_exprs
.is_empty
then
1543 v
.visit_list n_args
.n_exprs
1554 redef class ABraExpr
1555 redef fun accept_pretty_printer
(v
) do
1558 if not n_args
.n_exprs
.is_empty
then
1560 v
.visit_list n_args
.n_exprs
1566 redef class ABraAssignExpr
1567 redef fun accept_pretty_printer
(v
) do
1570 if not n_args
.n_exprs
.is_empty
then
1572 v
.visit_list n_args
.n_exprs
1583 redef class ABraReassignExpr
1584 redef fun accept_pretty_printer
(v
) do
1587 if not n_args
.n_exprs
.is_empty
then
1589 v
.visit_list n_args
.n_exprs
1600 redef class AAssignMethid
1601 redef fun accept_pretty_printer
(v
) do
1607 redef class ABraMethid
1608 redef fun accept_pretty_printer
(v
) do
1614 redef class ABraassignMethid
1615 redef fun accept_pretty_printer
(v
) do
1622 redef class AInitExpr
1623 redef fun accept_pretty_printer
(v
) do
1624 if not n_expr
isa AImplicitSelfExpr then
1631 if not n_args
.n_exprs
.is_empty
then
1633 v
.visit_list n_args
.n_exprs
1639 redef class ANewExpr
1640 redef fun accept_pretty_printer
(v
) do
1641 var can_inline
= v
.can_inline
(self)
1646 if n_id
!= null then
1649 if not can_inline
then
1659 if not n_args
.n_exprs
.is_empty
then
1661 v
.visit_list n_args
.n_exprs
1666 redef fun is_inlinable
do return true
1671 redef class AAttrExpr
1672 redef fun accept_pretty_printer
(v
) do
1677 redef fun is_inlinable
do return true
1680 redef class AAttrAssignExpr
1681 redef fun accept_pretty_printer
(v
) do
1691 redef class AAttrReassignExpr
1692 redef fun accept_pretty_printer
(v
) do
1704 redef class AVardeclExpr
1705 redef fun accept_pretty_printer
(v
) do
1710 if n_type
!= null then
1716 if n_expr
!= null then
1724 redef fun is_inlinable
do return true
1727 redef class AVarAssignExpr
1728 redef fun accept_pretty_printer
(v
) do
1737 redef class AAssertExpr
1738 redef fun accept_pretty_printer
(v
) do
1739 var can_inline
= v
.can_inline
(self)
1742 if n_id
!= null then
1750 var n_else
= self.n_else
1752 if n_else
!= null then
1763 if n_else
isa ABlockExpr then
1764 n_else
.force_block
= true
1768 v
.visit n_else
.n_kwend
1781 redef fun is_inlinable
do
1782 if not super then return false
1783 if n_else
!= null and not n_else
.is_inlinable
then return false
1788 redef class AReturnExpr
1789 redef fun accept_pretty_printer
(v
) do
1792 if n_expr
!= null then
1799 redef class ASuperExpr
1800 redef fun accept_pretty_printer
(v
) do
1803 if not n_args
.n_exprs
.is_empty
then
1804 if is_stmt
and n_args
.n_exprs
.length
== 1 then
1806 if v
.current_token
isa TOpar then v
.skip
1807 v
.visit n_args
.n_exprs
.first
1808 if v
.current_token
isa TCpar then v
.skip
1810 if v
.current_token
isa TOpar then
1816 v
.visit_list n_args
.n_exprs
1817 if v
.current_token
isa TCpar then v
.consume
")"
1822 # Is the call alone on its line?
1823 fun is_stmt
: Bool do return self.first_token
.is_starting_line
1825 redef fun is_inlinable
do return true
1828 redef class AOnceExpr
1829 redef fun accept_pretty_printer
(v
) do
1835 redef fun is_inlinable
do return true
1838 redef class AAbortExpr
1839 redef fun accept_pretty_printer
(v
) do v
.visit n_kwabort
1840 redef fun is_inlinable
do return true
1843 redef class ANotExpr
1844 redef fun accept_pretty_printer
(v
) do
1851 redef class AAsCastExpr
1852 redef fun accept_pretty_printer
(v
) do
1862 redef class AAsNotnullExpr
1863 redef fun accept_pretty_printer
(v
) do
1877 # Used to factorize work on Or, And, Implies and Binop expressions.
1878 private class ABinOpHelper
1881 fun bin_expr1
: AExpr is abstract
1882 fun bin_expr2
: AExpr is abstract
1885 fun bin_op
: String is abstract
1887 redef fun accept_pretty_printer
(v
) do
1888 var can_inline
= v
.can_inline
(self)
1890 if not can_inline
then
1891 if (self isa ABinopExpr and bin_expr1
isa ABinopExpr) or
1892 (self isa AAndExpr and (bin_expr1
isa AAndExpr or bin_expr1
isa AOrExpr)) or
1893 (self isa AOrExpr and (bin_expr1
isa AAndExpr or bin_expr1
isa AOrExpr))
1895 bin_expr1
.force_block
= true
1916 redef class AAndExpr
1919 redef fun bin_expr1
do return n_expr
1920 redef fun bin_expr2
do return n_expr2
1921 redef fun bin_op
do return "and"
1927 redef fun bin_expr1
do return n_expr
1928 redef fun bin_expr2
do return n_expr2
1929 redef fun bin_op
do return "or"
1932 redef class AImpliesExpr
1935 redef fun bin_expr1
do return n_expr
1936 redef fun bin_expr2
do return n_expr2
1937 redef fun bin_op
do return "implies"
1940 redef class ABinopExpr
1943 redef fun bin_expr1
do return n_expr
1944 redef fun bin_expr2
do return n_expr2
1948 redef fun bin_op
do return "=="
1952 redef fun bin_op
do return ">="
1956 redef fun bin_op
do return ">>"
1960 redef fun bin_op
do return ">"
1964 redef fun bin_op
do return "<="
1968 redef fun bin_op
do return "<<"
1972 redef fun bin_op
do return "<"
1975 redef class AMinusExpr
1976 redef fun bin_op
do return "-"
1980 redef fun bin_op
do return "!="
1983 redef class APercentExpr
1984 redef fun bin_op
do return "%"
1987 redef class APlusExpr
1988 redef fun bin_op
do return "+"
1991 redef class ASlashExpr
1992 redef fun bin_op
do return "/"
1995 redef class AStarExpr
1996 redef fun bin_op
do return "*"
1999 redef class AStarstarExpr
2000 redef fun bin_op
do return "**"
2003 redef class AStarshipExpr
2004 redef fun bin_op
do return "<=>"
2007 redef class AIsaExpr
2008 redef fun accept_pretty_printer
(v
) do
2017 redef class AOrElseExpr
2018 redef fun accept_pretty_printer
(v
) do
2028 redef fun is_inlinable
do return true
2033 redef class AUminusExpr
2034 redef fun accept_pretty_printer
(v
) do
2040 redef class ANullExpr
2041 redef fun accept_pretty_printer
(v
) do v
.visit n_kwnull
2042 redef fun is_inlinable
do return true
2045 redef class AParExpr
2046 redef fun accept_pretty_printer
(v
) do
2053 redef class AArrayExpr
2054 redef fun accept_pretty_printer
(v
) do
2056 v
.visit_list n_exprs
2061 redef class ACrangeExpr
2062 redef fun accept_pretty_printer
(v
) do
2071 redef class AOrangeExpr
2072 redef fun accept_pretty_printer
(v
) do
2083 redef class AStringFormExpr
2084 redef fun accept_pretty_printer
(v
) do
2085 var can_inline
= v
.can_inline
(self)
2090 var text
= n_string
.text
2093 while i
< text
.length
do
2096 if v
.current_length
>= v
.max_size
and i
<= text
.length
- 3 then
2108 v
.current_token
= n_string
.next_token
2113 redef class ASuperstringExpr
2114 redef fun accept_pretty_printer
(v
) do
2115 for n_expr
in n_exprs
do v
.visit n_expr
2118 redef fun must_be_inline
do
2119 if super then return true
2121 if not n_exprs
.is_empty
then
2122 var first
= n_exprs
.first
2123 return first
isa AStringFormExpr and first
.n_string
.text
.has_prefix
("\"\
"\"")