@@ -2589,10 +2589,18 @@ defmodule Macro do
2589
2589
2590
2590
header = dbg_format_header ( env )
2591
2591
2592
- quote do
2593
- to_debug = unquote ( dbg_ast_to_debuggable ( code , env ) )
2594
- unquote ( __MODULE__ ) . __dbg__ ( unquote ( header ) , to_debug , unquote ( options ) )
2595
- end
2592
+ prelude =
2593
+ quote do
2594
+ options = unquote ( __MODULE__ ) . __dbg_header__ ( unquote ( header ) , unquote ( options ) )
2595
+ end
2596
+
2597
+ Enum . reduce ( dbg_ast_to_debuggable ( code , env ) , prelude , fn entry , acc ->
2598
+ quote do
2599
+ unquote ( acc )
2600
+ to_debug = unquote ( entry )
2601
+ unquote ( __MODULE__ ) . __dbg__ ( to_debug , options )
2602
+ end
2603
+ end )
2596
2604
end
2597
2605
2598
2606
# Pipelines.
@@ -2619,11 +2627,13 @@ defmodule Macro do
2619
2627
end
2620
2628
end
2621
2629
2622
- quote do
2623
- unquote ( values_ast )
2630
+ [
2631
+ quote do
2632
+ unquote ( values_ast )
2624
2633
2625
- { :pipe , unquote ( escape ( asts ) ) , Enum . reverse ( unquote ( values_acc_var ) ) }
2626
- end
2634
+ { :pipe , unquote ( escape ( asts ) ) , Enum . reverse ( unquote ( values_acc_var ) ) }
2635
+ end
2636
+ ]
2627
2637
end
2628
2638
2629
2639
dbg_decomposed_binary_operators = [ :&& , :|| , :and , :or ]
@@ -2634,22 +2644,26 @@ defmodule Macro do
2634
2644
acc_var = unique_var ( :acc , __MODULE__ )
2635
2645
result_var = unique_var ( :result , __MODULE__ )
2636
2646
2637
- quote do
2638
- unquote ( acc_var ) = [ ]
2639
- unquote ( dbg_boolean_tree ( ast , acc_var , result_var ) )
2640
- { :logic_op , Enum . reverse ( unquote ( acc_var ) ) , unquote ( result_var ) }
2641
- end
2647
+ [
2648
+ quote do
2649
+ unquote ( acc_var ) = [ ]
2650
+ unquote ( dbg_boolean_tree ( ast , acc_var , result_var ) )
2651
+ { :logic_op , Enum . reverse ( unquote ( acc_var ) ) , unquote ( result_var ) }
2652
+ end
2653
+ ]
2642
2654
end
2643
2655
2644
2656
defp dbg_ast_to_debuggable ( { :__block__ , _meta , exprs } = ast , _env ) when exprs != [ ] do
2645
2657
acc_var = unique_var ( :acc , __MODULE__ )
2646
2658
result_var = unique_var ( :result , __MODULE__ )
2647
2659
2648
- quote do
2649
- unquote ( acc_var ) = [ ]
2650
- unquote ( dbg_block ( ast , acc_var , result_var ) )
2651
- { :block , Enum . reverse ( unquote ( acc_var ) ) , unquote ( result_var ) }
2652
- end
2660
+ [
2661
+ quote do
2662
+ unquote ( acc_var ) = [ ]
2663
+ unquote ( dbg_block ( ast , acc_var , result_var ) )
2664
+ { :block , Enum . reverse ( unquote ( acc_var ) ) , unquote ( result_var ) }
2665
+ end
2666
+ ]
2653
2667
end
2654
2668
2655
2669
defp dbg_ast_to_debuggable ( { :case , _meta , [ expr , [ do: clauses ] ] } = ast , _env ) do
@@ -2658,16 +2672,20 @@ defmodule Macro do
2658
2672
{ :-> , meta , [ left , { right , index } ] }
2659
2673
end )
2660
2674
2661
- quote do
2662
- expr = unquote ( expr )
2663
-
2664
- { result , clause_index } =
2665
- case expr do
2666
- unquote ( clauses_returning_index )
2667
- end
2675
+ [
2676
+ quote do
2677
+ expr = unquote ( expr )
2678
+ { :case_argument , unquote ( escape ( expr ) ) , expr }
2679
+ end ,
2680
+ quote do
2681
+ { result , clause_index } =
2682
+ case expr do
2683
+ unquote ( clauses_returning_index )
2684
+ end
2668
2685
2669
- { :case , unquote ( escape ( ast ) ) , expr , clause_index , result }
2670
- end
2686
+ { :case_expression , unquote ( escape ( ast ) ) , clause_index , result }
2687
+ end
2688
+ ]
2671
2689
end
2672
2690
2673
2691
defp dbg_ast_to_debuggable ( { :cond , _meta , [ [ do: clauses ] ] } = ast , _env ) do
@@ -2681,31 +2699,36 @@ defmodule Macro do
2681
2699
)
2682
2700
end )
2683
2701
2684
- quote do
2685
- { clause_ast , clause_value , clause_index , value } =
2686
- cond do
2687
- unquote ( modified_clauses )
2688
- end
2702
+ [
2703
+ quote do
2704
+ { clause_ast , clause_value , clause_index , value } =
2705
+ cond do
2706
+ unquote ( modified_clauses )
2707
+ end
2689
2708
2690
- { :cond , unquote ( escape ( ast ) ) , clause_ast , clause_value , clause_index , value }
2691
- end
2709
+ { :cond , unquote ( escape ( ast ) ) , clause_ast , clause_value , clause_index , value }
2710
+ end
2711
+ ]
2692
2712
end
2693
2713
2694
2714
defp dbg_ast_to_debuggable ( { :if , meta , [ condition_ast , clauses ] } = ast , env ) do
2695
2715
case Macro.Env . lookup_import ( env , { :if , 2 } ) do
2696
2716
[ macro: Kernel ] ->
2697
2717
condition_result_var = unique_var ( :condition_result , __MODULE__ )
2698
2718
2699
- quote do
2700
- unquote ( condition_result_var ) = unquote ( condition_ast )
2701
- result = unquote ( { :if , meta , [ condition_result_var , clauses ] } )
2702
-
2703
- { :if , unquote ( escape ( ast ) ) , unquote ( escape ( condition_ast ) ) ,
2704
- unquote ( condition_result_var ) , result }
2705
- end
2719
+ [
2720
+ quote do
2721
+ unquote ( condition_result_var ) = unquote ( condition_ast )
2722
+ { :if_condition , unquote ( escape ( condition_ast ) ) , unquote ( condition_result_var ) }
2723
+ end ,
2724
+ quote do
2725
+ result = unquote ( { :if , meta , [ condition_result_var , clauses ] } )
2726
+ { :if_result , unquote ( escape ( ast ) ) , result }
2727
+ end
2728
+ ]
2706
2729
2707
2730
_ ->
2708
- quote do: { :value , unquote ( escape ( ast ) ) , unquote ( ast ) }
2731
+ [ quote ( do: { :value , unquote ( escape ( ast ) ) , unquote ( ast ) } ) ]
2709
2732
end
2710
2733
end
2711
2734
@@ -2778,18 +2801,20 @@ defmodule Macro do
2778
2801
2779
2802
modified_with_ast = { :with , meta , modified_clauses ++ [ modified_opts ] }
2780
2803
2781
- quote do
2782
- unquote ( acc_var ) = [ ]
2804
+ [
2805
+ quote do
2806
+ unquote ( acc_var ) = [ ]
2783
2807
2784
- { value , acc } = unquote ( modified_with_ast )
2808
+ { value , acc } = unquote ( modified_with_ast )
2785
2809
2786
- { :with , unquote ( escape ( ast ) ) , Enum . reverse ( acc ) , value }
2787
- end
2810
+ { :with , unquote ( escape ( ast ) ) , Enum . reverse ( acc ) , value }
2811
+ end
2812
+ ]
2788
2813
end
2789
2814
2790
2815
# Any other AST.
2791
2816
defp dbg_ast_to_debuggable ( ast , _env ) do
2792
- quote do: { :value , unquote ( escape ( ast ) ) , unquote ( ast ) }
2817
+ [ quote ( do: { :value , unquote ( escape ( ast ) ) , unquote ( ast ) } ) ]
2793
2818
end
2794
2819
2795
2820
# This is a binary operator. We replace the left side with a recursive call to
@@ -2834,23 +2859,25 @@ defmodule Macro do
2834
2859
# Made public to be called from Macro.dbg/3, so that we generate as little code
2835
2860
# as possible and call out into a function as soon as we can.
2836
2861
@ doc false
2837
- def __dbg__ ( header_string , to_debug , options ) do
2862
+ def __dbg_header__ ( header_string , options ) do
2838
2863
{ print_location? , options } = Keyword . pop ( options , :print_location , true )
2839
- syntax_colors = if IO.ANSI . enabled? ( ) , do: IO.ANSI . syntax_colors ( ) , else: [ ]
2840
- options = Keyword . merge ( [ width: 80 , pretty: true , syntax_colors: syntax_colors ] , options )
2841
2864
2842
- { formatted , result } = dbg_format_ast_to_debug ( to_debug , options )
2865
+ if print_location? do
2866
+ ansi_enabled? = options [ :syntax_colors ] != [ ]
2867
+ formatted = [ :cyan , :italic , header_string , :reset , "\n " ]
2868
+ :ok = IO . write ( IO.ANSI . format ( formatted , ansi_enabled? ) )
2869
+ end
2843
2870
2844
- formatted =
2845
- if print_location? do
2846
- [ :cyan , :italic , header_string , :reset , "\n " , formatted , "\n " ]
2847
- else
2848
- [ formatted , "\n " ]
2849
- end
2871
+ options
2872
+ end
2850
2873
2874
+ @ doc false
2875
+ def __dbg__ ( to_debug , options ) do
2876
+ syntax_colors = if IO.ANSI . enabled? ( ) , do: IO.ANSI . syntax_colors ( ) , else: [ ]
2877
+ options = Keyword . merge ( [ width: 80 , pretty: true , syntax_colors: syntax_colors ] , options )
2878
+ { formatted , result } = dbg_format_ast_to_debug ( to_debug , options )
2851
2879
ansi_enabled? = options [ :syntax_colors ] != [ ]
2852
- :ok = IO . write ( IO.ANSI . format ( formatted , ansi_enabled? ) )
2853
-
2880
+ :ok = IO . write ( IO.ANSI . format ( [ formatted , ?\n ] , ansi_enabled? ) )
2854
2881
result
2855
2882
end
2856
2883
@@ -2891,14 +2918,18 @@ defmodule Macro do
2891
2918
{ formatted , value }
2892
2919
end
2893
2920
2894
- defp dbg_format_ast_to_debug ( { :case , ast , expr_value , clause_index , value } , options ) do
2895
- { :case , _meta , [ expr_ast , _ ] } = ast
2896
-
2921
+ defp dbg_format_ast_to_debug ( { :case_argument , expr_ast , expr_value } , options ) do
2897
2922
formatted = [
2898
2923
dbg_maybe_underline ( "Case argument" , options ) ,
2899
2924
":\n " ,
2900
- dbg_format_ast_with_value ( expr_ast , expr_value , options ) ,
2901
- ?\n ,
2925
+ dbg_format_ast_with_value ( expr_ast , expr_value , options )
2926
+ ]
2927
+
2928
+ { formatted , expr_value }
2929
+ end
2930
+
2931
+ defp dbg_format_ast_to_debug ( { :case_expression , ast , clause_index , value } , options ) do
2932
+ formatted = [
2902
2933
dbg_maybe_underline ( "Case expression" , options ) ,
2903
2934
" (clause ##{ clause_index + 1 } matched):\n " ,
2904
2935
dbg_format_ast_with_value ( ast , value , options )
@@ -2924,15 +2955,18 @@ defmodule Macro do
2924
2955
{ formatted , value }
2925
2956
end
2926
2957
2927
- defp dbg_format_ast_to_debug (
2928
- { :if , ast , condition_ast , condition_result , result } ,
2929
- options
2930
- ) do
2958
+ defp dbg_format_ast_to_debug ( { :if_condition , condition_ast , condition_result } , options ) do
2931
2959
formatted = [
2932
2960
dbg_maybe_underline ( "If condition" , options ) ,
2933
2961
":\n " ,
2934
- dbg_format_ast_with_value ( condition_ast , condition_result , options ) ,
2935
- ?\n ,
2962
+ dbg_format_ast_with_value ( condition_ast , condition_result , options )
2963
+ ]
2964
+
2965
+ { formatted , condition_result }
2966
+ end
2967
+
2968
+ defp dbg_format_ast_to_debug ( { :if_result , ast , result } , options ) do
2969
+ formatted = [
2936
2970
dbg_maybe_underline ( "If expression" , options ) ,
2937
2971
":\n " ,
2938
2972
dbg_format_ast_with_value ( ast , result , options )
0 commit comments