From 2e075ca317211553a19d7c8706a9d66fabcc9d8d Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Thu, 12 Nov 2020 19:21:01 +1100 Subject: Parse_Graph debug strings fixed --- src/packrat-parse_graphs.adb | 75 ++++++++++++++++++++++++++++++-------------- src/packrat-parse_graphs.ads | 3 +- 2 files changed, 54 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/packrat-parse_graphs.adb b/src/packrat-parse_graphs.adb index eed2eee..b0e5554 100644 --- a/src/packrat-parse_graphs.adb +++ b/src/packrat-parse_graphs.adb @@ -2,7 +2,7 @@ with - -- Ada.Strings.Fixed, + Ada.Strings.Fixed, Ada.Characters.Latin_1; @@ -210,46 +210,74 @@ package body Packrat.Parse_Graphs is function Debug_String - (Container : in Parse_Graph) + (Container : in Parse_Graph; + Cutoff : in Positive := 4) return String is - Mapping : Enum_Node_Maps.Map; + function Generate_Map + (Container : in Parse_Graph) + return Enum_Node_Maps.Map + is + Result : Enum_Node_Maps.Map; + Current : Label_Enum; + begin + for Node of Container.Internal_Graph.Nodes loop + Current := Gen_Tokens.Label (Container.Internal_Graph.Label (Node)); + if not Result.Contains (Current) then + Result.Insert (Current, Node_Vectors.Empty_Vector); + end if; + Result.Reference (Current).Append (Node); + end loop; + return Result; + end Generate_Map; + + function Image + (Input : in Label_Enum) + return String + is + Raw : String := Label_Enum'Image (Input); + begin + if Raw'Length <= Cutoff then + return Raw; + else + return Ada.Strings.Fixed.Head (Raw, Cutoff); + end if; + end Image; + + function Image + (Input : in Integer) + return String is + begin + return Ada.Strings.Fixed.Trim (Integer'Image (Input), Ada.Strings.Left); + end Image; + + Mapping : Enum_Node_Maps.Map := Generate_Map (Container); Current : Gen_Tokens.Token; Result : SU.Unbounded_String; begin - for Node of Container.Internal_Graph.Nodes loop - declare - Current : Label_Enum := - Gen_Tokens.Label (Container.Internal_Graph.Label (Node)); - begin - if not Mapping.Contains (Current) then - Mapping.Insert (Current, Node_Vectors.Empty_Vector); - end if; - Mapping.Reference (Current).Append (Node); - end; - end loop; for Iter in Mapping.Iterate loop - SU.Append (Result, Label_Enum'Image (Enum_Node_Maps.Key (Iter)) & Latin.HT); + SU.Append (Result, Image (Enum_Node_Maps.Key (Iter)) & Latin.HT); for Node of Enum_Node_Maps.Element (Iter) loop Current := Container.Internal_Graph.Label (Node); - SU.Append (Result, Positive'Image (Gen_Tokens.Start (Current)) & " ->" & Latin.HT); + SU.Append (Result, Image (Gen_Tokens.Start (Current)) & " ->" & Latin.HT); for Fin of In_Finishes (Container, Node) loop - SU.Append (Result, Finish_Type'Image (Fin) & " ->" & Latin.HT); + SU.Append (Result, Image (Fin) & " ->" & Latin.HT); declare Groupings : Token_Group_Array := Container.Subgroups ((Current, Fin)); begin if Groupings'Length = 0 then - SU.Append (Result, "Leaf"); + SU.Append (Result, "Leaf" & Latin.LF); + SU.Append (Result, SU."*" (3, Latin.HT)); else for Grouping of Groupings loop for Fin_Token of Elements (Grouping) loop SU.Append (Result, "Subnode " & - Label_Enum'Image (Gen_Tokens.Label (Fin_Token.Token)) & - " (" & Positive'Image (Gen_Tokens.Start (Fin_Token.Token)) & - "," & Finish_Type'Image (Fin_Token.Finish) & "), "); + Image (Gen_Tokens.Label (Fin_Token.Token)) & + " (" & Image (Gen_Tokens.Start (Fin_Token.Token)) & + "," & Image (Fin_Token.Finish) & "), "); end loop; SU.Delete (Result, SU.Length (Result) - 1, SU.Length (Result)); - SU.Append (Result, Latin.HT); + SU.Append (Result, Latin.LF); SU.Append (Result, SU."*" (3, Latin.HT)); end loop; end if; @@ -258,9 +286,10 @@ package body Packrat.Parse_Graphs is end loop; SU.Delete (Result, SU.Length (Result), SU.Length (Result)); end loop; - -- what delete goes here? + SU.Delete (Result, SU.Length (Result), SU.Length (Result)); SU.Append (Result, Latin.LF); end loop; + SU.Delete (Result, SU.Length (Result) - 1, SU.Length (Result)); return SU.To_String (Result); end Debug_String; diff --git a/src/packrat-parse_graphs.ads b/src/packrat-parse_graphs.ads index de5e4e9..291a467 100644 --- a/src/packrat-parse_graphs.ads +++ b/src/packrat-parse_graphs.ads @@ -104,7 +104,8 @@ package Packrat.Parse_Graphs is function Debug_String - (Container : in Parse_Graph) + (Container : in Parse_Graph; + Cutoff : in Positive := 4) return String; -- cgit