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
272 # Start visit of `self` using a `PrettyPrinterVisitor`
273 private fun accept_pretty_printer
(v
: PrettyPrinterVisitor) is abstract
275 # Collect the length (in `Char`) of the node.
276 private fun collect_length
: Int is abstract
278 # Is `self` printable in one line?
279 private fun is_inlinable
: Bool do return true
281 # Force `self` to be rendered as a block.
282 private var force_block
= false
284 # Does `self` have to be rendered as a block?
285 private fun must_be_block
: Bool do return force_block
287 # Force `self` to be rendered as a line.
288 private var force_inline
= false
290 # Does `self` have be rendered as a line?
291 private fun must_be_inline
: Bool do
292 if parent
!= null and parent
.must_be_inline
then return true
296 # Does `self` was written in one line before transformation?
297 private fun was_inline
: Bool is abstract
301 redef fun accept_pretty_printer
(v
) do
303 v
.current_token
= next_token
306 redef fun collect_length
do return text
.length
307 redef fun is_inlinable
do return true
308 redef fun was_inline
do return true
312 redef fun accept_pretty_printer
(v
) do v
.visit first_token
314 # The token where the production really start (skipping ADoc).
315 private fun start_token
: nullable Token do return first_token
317 # Collect all `TComment` contained in the production
318 # (between `start_token` and `end_token`).
319 private fun collect_comments
: Array[TComment] do
320 var res
= new Array[TComment]
321 if start_token
== null or last_token
== null then return res
322 var token
= start_token
324 while token
!= last_token
do
325 if token
isa TComment then res
.add token
326 token
= token
.next_token
332 redef fun collect_length
do
334 if start_token
== null or last_token
== null then return res
335 var token
= start_token
337 while token
!= last_token
do
338 res
+= token
.text
.length
339 token
= token
.next_token
342 res
+= token
.text
.length
346 redef fun was_inline
do
347 return first_token
.location
.line_start
== last_token
.location
.line_end
354 redef fun accept_pretty_printer
(v
) do
365 if is_last_in_group
then v
.addn
379 if next_token
isa TComment and is_first_in_group
then v
.addn
383 var prev_token
= self.prev_token
384 if prev_token
isa TComment and prev_token
.is_inline
and is_last_in_group
then v
.addn
391 # Is `self` part of an `ADoc`?
392 private fun is_adoc
: Bool do return parent
isa ADoc and parent
.parent
!= null
394 # Is `self` part of a licence?
395 private fun is_licence
: Bool do
396 var prev_token
= self.prev_token
398 if prev_token
== null then
400 else if prev_token
isa TComment then
401 return prev_token
.is_licence
407 # Is `self` starts and ends its line?
408 private fun is_inline
: Bool do
409 return self == first_real_token_in_line
and self == last_real_token_in_line
412 # Is `self` an orphan line (blank before and after)?
413 private fun is_orphan
: Bool do
414 return prev_token
isa TEol and
415 (prev_token
.prev_token
isa TEol or prev_token
.prev_token
isa TComment) and
419 # Is `self` the first comment of a group of comment?
420 private fun is_first_in_group
: Bool do return not prev_token
isa TComment
422 # Is `self` the last comment of a group of comments?
423 private fun is_last_in_group
: Bool do return not next_token
isa TComment
427 redef fun accept_pretty_printer
(v
) do for comment
in n_comment
do v
.visit comment
428 redef fun is_inlinable
do return n_comment
.length
<= 1
433 redef class AAnnotations
434 redef fun accept_pretty_printer
(v
) do
438 if v
.can_inline
(self) then
440 for n_item
in n_items
do
442 if n_item
!= n_items
.last
then
447 else if n_items
.length
> 1 then
451 for n_item
in n_items
do
460 if not was_inline
and v
.current_token
isa TKwend then v
.skip
463 redef fun is_inlinable
do
464 if not super then return false
465 for annot
in n_items
do if not annot
.is_inlinable
then return false
470 redef class AAnnotation
471 redef fun accept_pretty_printer
(v
) do
473 if not n_args
.is_empty
then
474 if n_opar
== null then
485 redef class ATypeExpr
486 redef fun accept_pretty_printer
(v
) do v
.visit n_type
492 redef fun accept_pretty_printer
(v
) do
493 v
.catch_up start_token
496 if not n_imports
.is_empty
then
499 for n_import
in n_imports
do
505 if not n_extern_code_blocks
.is_empty
then
508 for n_extern_block
in n_extern_code_blocks
do
509 v
.catch_up n_extern_block
510 v
.visit n_extern_block
512 if n_extern_block
!= n_extern_code_blocks
.last
then v
.addn
515 if not n_classdefs
.is_empty
then v
.addn
518 if not n_classdefs
.is_empty
then
521 for n_classdef
in n_classdefs
do
522 v
.catch_up n_classdef
524 if n_classdef
!= n_classdefs
.last
then v
.addn
532 redef fun start_token
do
533 if n_moduledecl
!= null then return n_moduledecl
.first_token
534 if not n_imports
.is_empty
then return n_imports
.first
.first_token
535 if not n_classdefs
.is_empty
then return n_classdefs
.first
.first_token
539 redef fun is_inlinable
do return false
542 redef class AModuledecl
543 redef fun accept_pretty_printer
(v
) do
549 if n_annotations
!= null then
550 var annot_inline
= v
.can_inline
(n_annotations
)
551 v
.visit n_annotations
553 if not annot_inline
then
554 if v
.current_token
isa TKwend then
568 redef class AModuleName
569 redef fun accept_pretty_printer
(v
) do
570 for path
in n_path
do
579 redef class ANoImport
580 redef fun accept_pretty_printer
(v
) do
589 redef class AStdImport
590 redef fun accept_pretty_printer
(v
) do
591 if not n_visibility
isa APublicVisibility then
606 redef class AClassdef
607 redef fun accept_pretty_printer
(v
) do
608 for n_propdef
in n_propdefs
do
611 if n_propdef
.n_doc
!= null or not v
.can_inline
(n_propdef
) then
612 if n_propdef
!= n_propdefs
.first
then v
.addn
614 if n_propdef
!= n_propdefs
.last
then v
.addn
622 redef class AStdClassdef
623 redef fun accept_pretty_printer
(v
) do
625 var can_inline
= v
.can_inline
(self)
627 if not n_visibility
isa APublicVisibility then
632 if n_kwredef
!= null then
641 if not n_formaldefs
.is_empty
then
643 v
.visit_list n_formaldefs
647 if n_extern_code_block
!= null then
649 v
.visit n_extern_code_block
655 if not n_superclasses
.is_empty
then
656 for n_superclass
in n_superclasses
do
666 for n_superclass
in n_superclasses
do
667 v
.catch_up n_superclass
674 if not n_superclasses
.is_empty
and not n_propdefs
.is_empty
then
689 redef fun is_inlinable
do
690 if not super then return false
691 if not n_propdefs
.is_empty
then return false
692 if n_superclasses
.length
> 1 then return false
693 if not collect_comments
.is_empty
then return false
697 redef fun start_token
do
698 if not n_visibility
isa APublicVisibility then return n_visibility
.first_token
699 if n_kwredef
!= null then return n_kwredef
700 return n_classkind
.first_token
704 redef class AAbstractClasskind
705 redef fun accept_pretty_printer
(v
) do
712 redef class AExternClasskind
713 redef fun accept_pretty_printer
(v
) do
720 redef class AFormaldef
721 redef fun accept_pretty_printer
(v
) do
724 if n_type
!= null then
733 redef fun accept_pretty_printer
(v
) do
734 if n_kwnullable
!= null then
741 if not n_types
.is_empty
then
749 redef class ASuperclass
750 redef fun accept_pretty_printer
(v
) do
760 redef fun accept_pretty_printer
(v
) do
764 if not n_visibility
isa APublicVisibility then
769 if n_kwredef
!= null then
775 redef fun start_token
do
776 if n_doc
== null then return super
777 return n_doc
.last_token
.next_token
781 redef class AAttrPropdef
782 redef fun accept_pretty_printer
(v
) do
788 if n_type
!= null then
794 if n_expr
!= null then
801 if n_annotations
!= null then v
.visit n_annotations
806 redef fun first_token
do
807 if n_doc
!= null then return n_doc
.first_token
808 if not n_visibility
isa APublicVisibility then return n_visibility
.first_token
809 if n_kwredef
!= null then return n_kwredef
813 redef fun is_inlinable
do return true
816 redef class ATypePropdef
817 redef fun accept_pretty_printer
(v
) do
829 redef fun is_inlinable
do return true
832 redef class AMethPropdef
833 redef fun accept_pretty_printer
(v
) do
834 # TODO: Handle extern annotations
836 var before
= v
.indent
837 var can_inline
= v
.can_inline
(self)
839 if n_kwinit
!= null then v
.visit n_kwinit
840 if n_kwmeth
!= null then v
.visit n_kwmeth
841 if n_kwnew
!= null then v
.visit n_kwnew
843 if not n_methid
== null then
850 if n_annotations
!= null then
851 v
.visit n_annotations
856 if n_extern_calls
!= null or n_extern_code_block
!= null then
857 if n_annotations
!= null then v
.adds
858 if n_extern_calls
!= null then v
.visit n_extern_calls
859 if n_extern_code_block
!= null then v
.visit n_extern_code_block
862 var n_block
= self.n_block
864 if n_block
!= null then
865 while not v
.current_token
isa TKwdo do v
.skip
866 if n_annotations
!= null then
867 if v
.can_inline
(n_annotations
) then
878 if n_block
isa ABlockExpr then
879 if n_block
.n_expr
.is_empty
then
880 v
.visit n_block
.n_kwend
882 v
.visit n_block
.n_expr
.first
883 v
.current_token
= n_block
.n_kwend
888 if v
.current_token
isa TKwend then v
.skip
895 if n_block
isa ABlockExpr then
896 n_block
.force_block
= true
898 v
.catch_up n_block
.n_kwend
907 if n_block
isa ABlockExpr then
908 v
.visit n_block
.n_kwend
917 assert v
.indent
== before
921 # * block is empty or can be inlined
922 # * contains no comments
923 redef fun is_inlinable
do
924 if not super then return false
925 if n_annotations
!= null and not n_annotations
.is_inlinable
then return false
926 if n_block
!= null and not n_block
.is_inlinable
then return false
927 if n_extern_calls
!= null and not n_extern_calls
.is_inlinable
then return false
928 if n_extern_code_block
!= null and not n_extern_code_block
.is_inlinable
then return false
929 if not collect_comments
.is_empty
then return false
934 redef class AMainMethPropdef
935 redef fun accept_pretty_printer
(v
) do
941 redef class ASignature
942 redef fun accept_pretty_printer
(v
) do
943 if not n_params
.is_empty
then
945 v
.visit_list n_params
949 if n_type
!= null then
958 redef fun accept_pretty_printer
(v
) do
961 if n_type
!= null then
967 if n_dotdotdot
!= null then v
.visit n_dotdotdot
973 redef class AExternCalls
974 redef fun accept_pretty_printer
(v
) do
975 var can_inline
= v
.can_inline
(self)
980 v
.visit_list n_extern_calls
985 v
.visit_list n_extern_calls
992 redef class AFullPropExternCall
993 redef fun accept_pretty_printer
(v
) do
1000 redef class ALocalPropExternCall
1001 redef fun accept_pretty_printer
(v
) do v
.visit n_methid
1004 redef class AInitPropExternCall
1005 redef fun accept_pretty_printer
(v
) do v
.visit n_type
1008 redef class ACastAsExternCall
1009 redef fun accept_pretty_printer
(v
) do
1019 redef class AAsNullableExternCall
1020 redef fun accept_pretty_printer
(v
) do
1025 v
.visit n_kwnullable
1029 redef class AAsNotNullableExternCall
1030 redef fun accept_pretty_printer
(v
) do
1037 v
.visit n_kwnullable
1041 redef class AExternCodeBlock
1042 redef fun accept_pretty_printer
(v
) do
1043 if n_in_language
!= null then
1044 v
.visit n_in_language
1048 v
.visit n_extern_code_segment
1051 redef fun is_inlinable
do
1052 if not super then return false
1053 return n_extern_code_segment
.is_inlinable
1057 redef class AInLanguage
1058 redef fun accept_pretty_printer
(v
) do
1065 redef class TExternCodeSegment
1066 redef fun accept_pretty_printer
(v
) do
1067 var can_inline
= v
.can_inline
(self)
1072 var text
= text
.substring
(2, text
.length
- 4)
1073 var lines
= text
.r_trim
.split
("\n")
1075 if text
.is_empty
then
1080 if not lines
.first
.trim
.is_empty
then
1088 for line
in lines
do
1097 v
.current_token
= next_token
1101 redef fun is_inlinable
do
1102 if not super then return false
1103 return location
.line_start
== location
.line_end
1109 redef class ABlockExpr
1110 redef fun accept_pretty_printer
(v
) do
1111 var before
= v
.indent
1112 var can_inline
= v
.can_inline
(self)
1114 if can_inline
and not n_expr
.is_empty
then
1115 v
.visit n_expr
.first
1118 for nexpr
in n_expr
do
1119 var expr_inline
= v
.can_inline
(nexpr
)
1120 if not expr_inline
and nexpr
!= n_expr
.first
then v
.addn
1126 if not expr_inline
and nexpr
!= n_expr
.last
then v
.addn
1130 assert v
.indent
== before
1133 redef fun is_inlinable
do
1134 if not super then return false
1135 if not collect_comments
.is_empty
then return false
1137 if not n_expr
.is_empty
then
1138 if n_expr
.length
> 1 then return false
1139 if not n_expr
.first
.is_inlinable
then return false
1147 redef fun accept_pretty_printer
(v
) do
1148 var before
= v
.indent
1149 var can_inline
= v
.can_inline
(self)
1153 if v
.can_inline
(n_expr
) then
1162 # skip comments before `then` token
1163 while not v
.current_token
isa TKwthen do v
.skip
1165 var n_else
= self.n_else
1169 if n_then
!= null then v
.visit n_then
1172 n_else
.force_inline
= true
1177 else if n_then
== null then
1181 v
.skip_to last_token
.last_real_token_in_line
1187 if n_then
!= null then
1188 if n_then
isa ABlockExpr then
1189 n_then
.force_block
= true
1199 while not v
.current_token
isa TKwelse do
1200 v
.consume v
.current_token
.text
1207 if n_else
isa AIfExpr then
1208 n_else
.force_block
= true
1216 if n_else
isa ABlockExpr then
1217 n_else
.force_block
= true
1225 if last_token
isa TKwend then
1226 v
.catch_up last_token
1237 if last_token
.location
>= v
.current_token
.location
then v
.catch_up last_token
1241 if v
.current_token
isa TKwend then v
.skip
1245 assert v
.indent
== before
1248 redef fun is_inlinable
do
1249 if not super then return false
1250 if n_then
!= null and not n_then
.is_inlinable
then return false
1251 var n_else
= self.n_else
1252 if (n_else
isa ABlockExpr and not n_else
.n_expr
.is_empty
) or
1253 (not n_else
isa ABlockExpr and n_else
!= null) then
1256 if not collect_comments
.is_empty
then return false
1260 # Does this `if` statement contains a `else` part?
1261 private fun has_else
(v
: PrettyPrinterVisitor): Bool do
1263 if n_else
== null then return false
1264 var n_kwelse
= collect_kwelse
1265 if n_kwelse
== null then return false
1267 if n_else
isa ABlockExpr then
1268 var comments
: Array[TComment]
1270 if n_then
== null then
1271 comments
= v
.collect_comments
(n_expr
.last_token
, n_else
.last_token
)
1273 comments
= v
.collect_comments
(n_then
.last_token
, n_else
.last_token
)
1276 if not comments
.is_empty
then return true
1277 return not n_else
.n_expr
.is_empty
1283 # Lookup for `else` token in `self`.
1284 private fun collect_kwelse
: nullable TKwelse do
1285 var token
= first_token
1287 while token
!= last_token
do
1288 if token
isa TKwelse then return token
1289 token
= token
.next_token
1296 # Used to factorize work on loops.
1297 private class ALoopHelper
1300 fun loop_block
: nullable ANode is abstract
1301 fun loop_label
: nullable ANode is abstract
1303 fun visit_loop_block
(v
: PrettyPrinterVisitor) do
1304 var n_block
= loop_block
1309 if n_block
isa ABlockExpr then
1310 n_block
.force_block
= true
1312 v
.catch_up n_block
.n_kwend
1315 v
.visit n_block
.n_kwend
1325 if loop_label
!= null then
1331 fun visit_loop_inline
(v
: PrettyPrinterVisitor) do
1332 var n_block
= loop_block
1335 if n_block
isa ABlockExpr then
1336 if n_block
.n_expr
.is_empty
then
1337 v
.visit n_block
.n_kwend
1339 v
.visit n_block
.n_expr
.first
1340 v
.current_token
= n_block
.n_kwend
1345 if v
.current_token
isa TKwend then v
.skip
1348 if loop_label
!= null then
1354 redef fun is_inlinable
do
1355 var n_block
= loop_block
1356 if not super then return false
1357 if n_block
isa ABlockExpr and not n_block
.is_inlinable
then return false
1358 if not collect_comments
.is_empty
then return false
1363 redef class ALoopExpr
1366 redef fun loop_block
do return n_block
1367 redef fun loop_label
do return n_label
1369 redef fun accept_pretty_printer
(v
) do
1370 var can_inline
= v
.can_inline
(self)
1372 if can_inline
then visit_loop_inline v
else visit_loop_block v
1376 redef class AWhileExpr
1379 redef fun loop_block
do return n_block
1380 redef fun loop_label
do return n_label
1382 redef fun accept_pretty_printer
(v
) do
1383 var can_inline
= v
.can_inline
(self)
1389 if can_inline
then visit_loop_inline v
else visit_loop_block v
1396 redef fun loop_block
do return n_block
1397 redef fun loop_label
do return n_label
1399 redef fun accept_pretty_printer
(v
) do
1400 var can_inline
= v
.can_inline
(self)
1402 if can_inline
then visit_loop_inline v
else visit_loop_block v
1406 redef class AForExpr
1409 redef fun loop_block
do return n_block
1410 redef fun loop_label
do return n_label
1412 redef fun accept_pretty_printer
(v
) do
1413 var can_inline
= v
.can_inline
(self)
1417 for n_id
in n_ids
do
1419 if n_id
!= n_ids
.last
then v
.add
", "
1428 if can_inline
then visit_loop_inline v
else visit_loop_block v
1432 redef class ABreakExpr
1433 redef fun accept_pretty_printer
(v
) do
1436 if n_expr
!= null then
1441 if n_label
!= null then
1447 redef fun is_inlinable
do return true
1450 redef class AContinueExpr
1451 redef fun accept_pretty_printer
(v
) do
1452 v
.visit n_kwcontinue
1454 if n_expr
!= null then
1459 if n_label
!= null then
1465 redef fun is_inlinable
do return true
1470 redef class ASendExpr
1471 redef fun is_inlinable
do return true
1474 redef class ACallExpr
1475 redef fun accept_pretty_printer
(v
) do
1476 var can_inline
= v
.can_inline
(self)
1479 if not n_expr
isa AImplicitSelfExpr and not can_inline
then
1487 if not n_args
.n_exprs
.is_empty
then
1488 if is_stmt
and n_args
.n_exprs
.length
== 1 then
1490 if v
.current_token
isa TOpar then v
.skip
1491 v
.visit n_args
.n_exprs
.first
1492 if v
.current_token
isa TCpar then v
.skip
1494 if v
.current_token
isa TOpar then
1500 v
.visit_list n_args
.n_exprs
1501 if v
.current_token
isa TCpar then v
.consume
")"
1506 # Is the call alone on its line?
1507 fun is_stmt
: Bool do return parent
isa ABlockExpr
1510 redef class ACallAssignExpr
1511 redef fun accept_pretty_printer
(v
) do
1515 if not n_args
.n_exprs
.is_empty
then
1517 v
.visit_list n_args
.n_exprs
1528 redef class ACallReassignExpr
1529 redef fun accept_pretty_printer
(v
) do
1533 if not n_args
.n_exprs
.is_empty
then
1535 v
.visit_list n_args
.n_exprs
1546 redef class ABraExpr
1547 redef fun accept_pretty_printer
(v
) do
1550 if not n_args
.n_exprs
.is_empty
then
1552 v
.visit_list n_args
.n_exprs
1558 redef class ABraAssignExpr
1559 redef fun accept_pretty_printer
(v
) do
1562 if not n_args
.n_exprs
.is_empty
then
1564 v
.visit_list n_args
.n_exprs
1575 redef class ABraReassignExpr
1576 redef fun accept_pretty_printer
(v
) do
1579 if not n_args
.n_exprs
.is_empty
then
1581 v
.visit_list n_args
.n_exprs
1592 redef class AAssignMethid
1593 redef fun accept_pretty_printer
(v
) do
1599 redef class ABraMethid
1600 redef fun accept_pretty_printer
(v
) do
1606 redef class ABraassignMethid
1607 redef fun accept_pretty_printer
(v
) do
1614 redef class AInitExpr
1615 redef fun accept_pretty_printer
(v
) do
1616 if not n_expr
isa AImplicitSelfExpr then
1623 if not n_args
.n_exprs
.is_empty
then
1625 v
.visit_list n_args
.n_exprs
1631 redef class ANewExpr
1632 redef fun accept_pretty_printer
(v
) do
1633 var can_inline
= v
.can_inline
(self)
1638 if n_id
!= null then
1641 if not can_inline
then
1650 if not n_args
.n_exprs
.is_empty
then
1652 v
.visit_list n_args
.n_exprs
1657 redef fun is_inlinable
do return true
1662 redef class AAttrExpr
1663 redef fun accept_pretty_printer
(v
) do
1668 redef fun is_inlinable
do return true
1671 redef class AAttrAssignExpr
1672 redef fun accept_pretty_printer
(v
) do
1682 redef class AAttrReassignExpr
1683 redef fun accept_pretty_printer
(v
) do
1695 redef class AVardeclExpr
1696 redef fun accept_pretty_printer
(v
) do
1701 if n_type
!= null then
1707 if n_expr
!= null then
1715 redef fun is_inlinable
do return true
1718 redef class AVarAssignExpr
1719 redef fun accept_pretty_printer
(v
) do
1728 redef class AAssertExpr
1729 redef fun accept_pretty_printer
(v
) do
1730 var can_inline
= v
.can_inline
(self)
1733 if n_id
!= null then
1741 var n_else
= self.n_else
1743 if n_else
!= null then
1753 if n_else
isa ABlockExpr then
1755 n_else
.force_block
= true
1759 v
.visit n_else
.n_kwend
1773 redef fun is_inlinable
do
1774 if not super then return false
1775 if n_else
!= null and not n_else
.is_inlinable
then return false
1780 redef class AReturnExpr
1781 redef fun accept_pretty_printer
(v
) do
1784 if n_expr
!= null then
1791 redef class ASuperExpr
1792 redef fun accept_pretty_printer
(v
) do
1795 if not n_args
.n_exprs
.is_empty
then
1796 if is_stmt
and n_args
.n_exprs
.length
== 1 then
1798 if v
.current_token
isa TOpar then v
.skip
1799 v
.visit n_args
.n_exprs
.first
1800 if v
.current_token
isa TCpar then v
.skip
1802 if v
.current_token
isa TOpar then
1808 v
.visit_list n_args
.n_exprs
1809 if v
.current_token
isa TCpar then v
.consume
")"
1814 # Is the call alone on its line?
1815 fun is_stmt
: Bool do return self.first_token
.is_starting_line
1817 redef fun is_inlinable
do return true
1820 redef class AOnceExpr
1821 redef fun accept_pretty_printer
(v
) do
1827 redef fun is_inlinable
do return true
1830 redef class AAbortExpr
1831 redef fun accept_pretty_printer
(v
) do v
.visit n_kwabort
1832 redef fun is_inlinable
do return true
1835 redef class ANotExpr
1836 redef fun accept_pretty_printer
(v
) do
1843 redef class AAsCastExpr
1844 redef fun accept_pretty_printer
(v
) do
1854 redef class AAsNotnullExpr
1855 redef fun accept_pretty_printer
(v
) do
1869 # Used to factorize work on Or, And, Implies and Binop expressions.
1870 private class ABinOpHelper
1873 fun bin_expr1
: AExpr is abstract
1874 fun bin_expr2
: AExpr is abstract
1877 fun bin_op
: String is abstract
1879 redef fun accept_pretty_printer
(v
) do
1880 var can_inline
= v
.can_inline
(self)
1882 if not can_inline
then
1883 if (self isa ABinopExpr and bin_expr1
isa ABinopExpr) or
1884 (self isa AAndExpr and (bin_expr1
isa AAndExpr or bin_expr1
isa AOrExpr)) or
1885 (self isa AOrExpr and (bin_expr1
isa AAndExpr or bin_expr1
isa AOrExpr))
1887 bin_expr1
.force_block
= true
1907 redef class AAndExpr
1910 redef fun bin_expr1
do return n_expr
1911 redef fun bin_expr2
do return n_expr2
1912 redef fun bin_op
do return "and"
1918 redef fun bin_expr1
do return n_expr
1919 redef fun bin_expr2
do return n_expr2
1920 redef fun bin_op
do return "or"
1923 redef class AImpliesExpr
1926 redef fun bin_expr1
do return n_expr
1927 redef fun bin_expr2
do return n_expr2
1928 redef fun bin_op
do return "implies"
1931 redef class ABinopExpr
1934 redef fun bin_expr1
do return n_expr
1935 redef fun bin_expr2
do return n_expr2
1939 redef fun bin_op
do return "=="
1943 redef fun bin_op
do return ">="
1947 redef fun bin_op
do return ">>"
1951 redef fun bin_op
do return ">"
1955 redef fun bin_op
do return "<="
1959 redef fun bin_op
do return "<<"
1963 redef fun bin_op
do return "<"
1966 redef class AMinusExpr
1967 redef fun bin_op
do return "-"
1971 redef fun bin_op
do return "!="
1974 redef class APercentExpr
1975 redef fun bin_op
do return "%"
1978 redef class APlusExpr
1979 redef fun bin_op
do return "+"
1982 redef class ASlashExpr
1983 redef fun bin_op
do return "/"
1986 redef class AStarExpr
1987 redef fun bin_op
do return "*"
1990 redef class AStarstarExpr
1991 redef fun bin_op
do return "**"
1994 redef class AStarshipExpr
1995 redef fun bin_op
do return "<=>"
1998 redef class AIsaExpr
1999 redef fun accept_pretty_printer
(v
) do
2008 redef class AOrElseExpr
2009 redef fun accept_pretty_printer
(v
) do
2019 redef fun is_inlinable
do return true
2024 redef class AUminusExpr
2025 redef fun accept_pretty_printer
(v
) do
2031 redef class ANullExpr
2032 redef fun accept_pretty_printer
(v
) do v
.visit n_kwnull
2033 redef fun is_inlinable
do return true
2036 redef class AParExpr
2037 redef fun accept_pretty_printer
(v
) do
2044 redef class AArrayExpr
2045 redef fun accept_pretty_printer
(v
) do
2047 v
.visit_list n_exprs
2052 redef class ACrangeExpr
2053 redef fun accept_pretty_printer
(v
) do
2062 redef class AOrangeExpr
2063 redef fun accept_pretty_printer
(v
) do
2074 redef class AStringFormExpr
2075 redef fun accept_pretty_printer
(v
) do
2076 var can_inline
= v
.can_inline
(self)
2081 var text
= n_string
.text
2084 while i
< text
.length
do
2087 if v
.current_length
>= v
.max_size
and i
<= text
.length
- 3 then
2099 v
.current_token
= n_string
.next_token
2104 redef class ASuperstringExpr
2105 redef fun accept_pretty_printer
(v
) do
2106 for n_expr
in n_exprs
do v
.visit n_expr
2109 redef fun must_be_inline
do
2110 if super then return true
2112 if not n_exprs
.is_empty
then
2113 var first
= n_exprs
.first
2114 return first
isa AStringFormExpr and first
.n_string
.text
.has_prefix
("\"\
"\"")