@@ -181,8 +181,8 @@ defmodule Inspect.Algebra do
181
181
additions, like support for binary nodes and a break mode that
182
182
maximises use of horizontal space.
183
183
184
- iex> Inspect.Algebra.empty ()
185
- :doc_nil
184
+ iex> Inspect.Algebra.line ()
185
+ :doc_line
186
186
187
187
iex> "foo"
188
188
"foo"
@@ -251,19 +251,34 @@ defmodule Inspect.Algebra do
251
251
252
252
@ type t ::
253
253
binary
254
- | :doc_line
255
- | :doc_nil
254
+ | doc_nil
255
+ | doc_cons
256
+ | doc_line
256
257
| doc_break
257
258
| doc_collapse
258
259
| doc_color
259
- | doc_cons
260
260
| doc_fits
261
261
| doc_force
262
262
| doc_group
263
263
| doc_nest
264
264
| doc_string
265
265
| doc_limit
266
266
267
+ @ typep doc_nil :: [ ]
268
+ defmacrop doc_nil do
269
+ [ ]
270
+ end
271
+
272
+ @ typep doc_line :: :doc_line
273
+ defmacrop doc_line do
274
+ :doc_line
275
+ end
276
+
277
+ @ typep doc_cons :: nonempty_improper_list ( t , t )
278
+ defmacrop doc_cons ( left , right ) do
279
+ quote do: [ unquote ( left ) | unquote ( right ) ]
280
+ end
281
+
267
282
@ typep doc_string :: { :doc_string , t , non_neg_integer }
268
283
defmacrop doc_string ( string , length ) do
269
284
quote do: { :doc_string , unquote ( string ) , unquote ( length ) }
@@ -274,11 +289,6 @@ defmodule Inspect.Algebra do
274
289
quote do: { :doc_limit , unquote ( doc ) , unquote ( limit ) }
275
290
end
276
291
277
- @ typep doc_cons :: { :doc_cons , t , t }
278
- defmacrop doc_cons ( left , right ) do
279
- quote do: { :doc_cons , unquote ( left ) , unquote ( right ) }
280
- end
281
-
282
292
@ typep doc_nest :: { :doc_nest , t , :cursor | :reset | non_neg_integer , :always | :break }
283
293
defmacrop doc_nest ( doc , indent , always_or_break ) do
284
294
quote do: { :doc_nest , unquote ( doc ) , unquote ( indent ) , unquote ( always_or_break ) }
@@ -328,7 +338,7 @@ defmodule Inspect.Algebra do
328
338
]
329
339
330
340
defguard is_doc ( doc )
331
- when is_binary ( doc ) or doc in [ :doc_nil , : doc_line] or
341
+ when is_list ( doc ) or is_binary ( doc ) or doc == doc_line ( ) or
332
342
( is_tuple ( doc ) and elem ( doc , 0 ) in @ docs )
333
343
334
344
defguardp is_width ( width ) when width == :infinity or ( is_integer ( width ) and width >= 0 )
@@ -508,8 +518,8 @@ defmodule Inspect.Algebra do
508
518
509
519
group =
510
520
case flex? do
511
- true -> group ( concat ( concat ( left , nest ( docs , 1 ) ) , right ) )
512
- false -> group ( glue ( nest ( glue ( left , "" , docs ) , 2 ) , "" , right ) )
521
+ true -> doc_group ( concat ( concat ( left , nest ( docs , 1 ) ) , right ) , :normal )
522
+ false -> doc_group ( glue ( nest ( glue ( left , "" , docs ) , 2 ) , "" , right ) , :normal )
513
523
end
514
524
515
525
{ group , inspect_opts }
@@ -550,21 +560,21 @@ defmodule Inspect.Algebra do
550
560
551
561
case fun . ( term , changed_opts ) do
552
562
{ doc , % Inspect.Opts { } = opts } -> { doc , opts }
553
- : doc_nil -> { : doc_nil, opts }
563
+ doc_nil ( ) -> { doc_nil ( ) , opts }
554
564
doc -> { doc , changed_opts }
555
565
end
556
566
end
557
567
558
- defp join ( : doc_nil, : doc_nil, _ , _ ) , do: : doc_nil
559
- defp join ( left , : doc_nil, _ , _ ) , do: left
560
- defp join ( : doc_nil, right , _ , _ ) , do: right
568
+ defp join ( doc_nil ( ) , doc_nil ( ) , _ , _ ) , do: doc_nil ( )
569
+ defp join ( left , doc_nil ( ) , _ , _ ) , do: left
570
+ defp join ( doc_nil ( ) , right , _ , _ ) , do: right
561
571
defp join ( left , right , true , sep ) , do: flex_glue ( concat ( left , sep ) , right )
562
572
defp join ( left , right , false , sep ) , do: glue ( concat ( left , sep ) , right )
563
573
564
574
defp simple? ( doc_cons ( left , right ) ) , do: simple? ( left ) and simple? ( right )
565
575
defp simple? ( doc_color ( doc , _ ) ) , do: simple? ( doc )
566
576
defp simple? ( doc_string ( _ , _ ) ) , do: true
567
- defp simple? ( : doc_nil) , do: true
577
+ defp simple? ( doc_nil ( ) ) , do: true
568
578
defp simple? ( other ) , do: is_binary ( other )
569
579
570
580
@ doc false
@@ -615,17 +625,29 @@ defmodule Inspect.Algebra do
615
625
616
626
# Algebra API
617
627
628
+ @ compile { :inline ,
629
+ empty: 0 ,
630
+ concat: 2 ,
631
+ break: 0 ,
632
+ break: 1 ,
633
+ glue: 2 ,
634
+ glue: 3 ,
635
+ flex_break: 0 ,
636
+ flex_break: 1 ,
637
+ flex_glue: 2 ,
638
+ flex_glue: 3 }
639
+
618
640
@ doc """
619
641
Returns a document entity used to represent nothingness.
620
642
621
643
## Examples
622
644
623
645
iex> Inspect.Algebra.empty()
624
- :doc_nil
646
+ []
625
647
626
648
"""
627
- @ spec empty ( ) :: : doc_nil
628
- def empty , do: : doc_nil
649
+ @ spec empty ( ) :: doc_nil ( )
650
+ def empty , do: doc_nil ( )
629
651
630
652
@ doc ~S"""
631
653
Creates a document represented by string.
@@ -1023,7 +1045,7 @@ defmodule Inspect.Algebra do
1023
1045
"""
1024
1046
@ doc since: "1.6.0"
1025
1047
@ spec line ( ) :: t
1026
- def line ( ) , do: : doc_line
1048
+ def line ( ) , do: doc_line ( )
1027
1049
1028
1050
@ doc ~S"""
1029
1051
Inserts a mandatory linebreak between two documents.
@@ -1163,20 +1185,20 @@ defmodule Inspect.Algebra do
1163
1185
do: fits? ( w , k , b? , [ { i , :break_no_flat , x } | t ] )
1164
1186
1165
1187
defp fits? ( _ , _ , _ , [ { _ , :break_no_flat , doc_break ( _ , _ ) } | _ ] ) , do: true
1166
- defp fits? ( _ , _ , _ , [ { _ , :break_no_flat , : doc_line} | _ ] ) , do: true
1188
+ defp fits? ( _ , _ , _ , [ { _ , :break_no_flat , doc_line ( ) } | _ ] ) , do: true
1167
1189
1168
1190
## Breaks
1169
1191
1170
1192
defp fits? ( _ , _ , _ , [ { _ , :break , doc_break ( _ , _ ) } | _ ] ) , do: true
1171
- defp fits? ( _ , _ , _ , [ { _ , :break , : doc_line} | _ ] ) , do: true
1193
+ defp fits? ( _ , _ , _ , [ { _ , :break , doc_line ( ) } | _ ] ) , do: true
1172
1194
1173
1195
defp fits? ( w , k , b? , [ { i , :break , doc_group ( x , _ ) } | t ] ) ,
1174
1196
do: fits? ( w , k , b? , [ { i , :flat , x } | { :tail , b? , t } ] )
1175
1197
1176
1198
## Catch all
1177
1199
1178
- defp fits? ( w , _ , _ , [ { i , _ , : doc_line} | t ] ) , do: fits? ( w , i , false , t )
1179
- defp fits? ( w , k , b? , [ { _ , _ , : doc_nil} | t ] ) , do: fits? ( w , k , b? , t )
1200
+ defp fits? ( w , _ , _ , [ { i , _ , doc_line ( ) } | t ] ) , do: fits? ( w , i , false , t )
1201
+ defp fits? ( w , k , b? , [ { _ , _ , doc_nil ( ) } | t ] ) , do: fits? ( w , k , b? , t )
1180
1202
defp fits? ( w , _ , b? , [ { i , _ , doc_collapse ( _ ) } | t ] ) , do: fits? ( w , i , b? , t )
1181
1203
defp fits? ( w , k , b? , [ { i , m , doc_color ( x , _ ) } | t ] ) , do: fits? ( w , k , b? , [ { i , m , x } | t ] )
1182
1204
defp fits? ( w , k , b? , [ { _ , _ , doc_string ( _ , l ) } | t ] ) , do: fits? ( w , k + l , b? , t )
@@ -1206,8 +1228,8 @@ defmodule Inspect.Algebra do
1206
1228
[ { integer , mode , t } | :group_over ]
1207
1229
) :: [ binary ]
1208
1230
defp format ( _ , _ , [ ] ) , do: [ ]
1209
- defp format ( w , k , [ { _ , _ , : doc_nil} | t ] ) , do: format ( w , k , t )
1210
- defp format ( w , _ , [ { i , _ , : doc_line} | t ] ) , do: [ indent ( i ) | format ( w , i , t ) ]
1231
+ defp format ( w , k , [ { _ , _ , doc_nil ( ) } | t ] ) , do: format ( w , k , t )
1232
+ defp format ( w , _ , [ { i , _ , doc_line ( ) } | t ] ) , do: [ indent ( i ) | format ( w , i , t ) ]
1211
1233
defp format ( w , k , [ { i , m , doc_cons ( x , y ) } | t ] ) , do: format ( w , k , [ { i , m , x } , { i , m , y } | t ] )
1212
1234
defp format ( w , k , [ { i , m , doc_color ( x , c ) } | t ] ) , do: [ c | format ( w , k , [ { i , m , x } | t ] ) ]
1213
1235
defp format ( w , k , [ { _ , _ , doc_string ( s , l ) } | t ] ) , do: [ s | format ( w , k + l , t ) ]
0 commit comments