summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packrat_parser_lib_notes.txt19
-rw-r--r--src/packrat-errors.adb105
-rw-r--r--src/packrat.adb11
-rw-r--r--src/packrat.ads85
-rw-r--r--test/ratnest-tests.adb144
-rw-r--r--test/ratnest-tests.ads22
-rw-r--r--test/test_main.adb4
7 files changed, 386 insertions, 4 deletions
diff --git a/packrat_parser_lib_notes.txt b/packrat_parser_lib_notes.txt
index a09babf..8bcac69 100644
--- a/packrat_parser_lib_notes.txt
+++ b/packrat_parser_lib_notes.txt
@@ -220,18 +220,26 @@ Pretty_Print
Packrat.Error
(actually a nested package, as this functionality is important to parsers/lexers)
- functions to handle and process exception messages
- - exception messages take the form of one or more "s<SYMBOLNAME>p<POSITION>" separated by a space
+ - exception messages take the form of one or more "s<SYMBOLNAME>p<POSITION>" substrings joined together,
+ with each symbol name being in all capitals and each position being a positive integer
- this message represents the list of expected symbols at particular positions that would have resulted in a
more complete parse
+ - one of these messages will be raised with an exception of no valid parse is possible with a given input
List of datatypes:
-Error_Info (containing an enum of the symbol expected, and a natural of the position)
+Error_Message
+Error_Info (containing a string of the symbol name expected, and a natural of the position)
+Error_Info_Array
List of funcs:
-Newcode
+Valid_Message
+Valid_Identifier
+Valid_Identifier_Array
+Join
Encode
+Encode_Array
Decode
-Join
+(the message format ensures that using "&" to join messages will still result in a valid message)
@@ -249,6 +257,9 @@ Run_Tests
Ratnest.Tests
List of funcs:
+In_Set_Check
+Not_In_Set_Check
+
Is_Digit_Check
Is_Hex_Check
Is_Letter_Check
diff --git a/src/packrat-errors.adb b/src/packrat-errors.adb
new file mode 100644
index 0000000..44b9202
--- /dev/null
+++ b/src/packrat-errors.adb
@@ -0,0 +1,105 @@
+
+
+separate (Packrat)
+package body Errors is
+
+
+ package SU renames Ada.Strings.Unbounded;
+
+
+
+
+
+ function Valid_Identifier
+ (Check : in String)
+ return Boolean is
+ begin
+ return True;
+ end Valid_Identifier;
+
+
+ function Valid_Identifier
+ (Check : in SU.Unbounded_String)
+ return Boolean is
+ begin
+ return True;
+ end Valid_Identifier;
+
+
+ function Valid_Identifier_Array
+ (Check : in Error_Info_Array)
+ return Boolean is
+ begin
+ return True;
+ end Valid_Identifier_Array;
+
+
+ function Valid_Message
+ (Check : in String)
+ return Boolean is
+ begin
+ return True;
+ end Valid_Message;
+
+
+
+
+
+ function Join
+ (Left, Right : in Error_Message)
+ return Error_Message is
+ begin
+ return "";
+ end Join;
+
+
+
+
+
+ function Encode
+ (Name : in String;
+ Pos : in Natural)
+ return Error_Message is
+ begin
+ return "";
+ end Encode;
+
+
+ function Encode
+ (Name : in SU.Unbounded_String;
+ Pos : in Natural)
+ return Error_Message is
+ begin
+ return "";
+ end Encode;
+
+
+ function Encode
+ (Info : in Error_Info)
+ return Error_Message is
+ begin
+ return "";
+ end Encode;
+
+
+ function Encode_Array
+ (Info : in Error_Info_Array)
+ return Error_Message is
+ begin
+ return "";
+ end Encode_Array;
+
+
+ function Decode
+ (Msg : in Error_Message)
+ return Error_Info_Array
+ is
+ Result : Error_Info_Array (1 .. 0);
+ begin
+ return Result;
+ end Decode;
+
+
+end Errors;
+
+
diff --git a/src/packrat.adb b/src/packrat.adb
new file mode 100644
index 0000000..e78d0ae
--- /dev/null
+++ b/src/packrat.adb
@@ -0,0 +1,11 @@
+
+
+package body Packrat is
+
+
+ package body Errors is separate;
+
+
+end Packrat;
+
+
diff --git a/src/packrat.ads b/src/packrat.ads
index 36410d5..628923f 100644
--- a/src/packrat.ads
+++ b/src/packrat.ads
@@ -1,8 +1,93 @@
+with
+
+ Ada.Strings.Unbounded;
+
+
package Packrat is
+ Parse_Error : exception;
+
+
+
+
+ package Errors is
+
+
+ subtype Error_Message is String
+ with Dynamic_Predicate => Valid_Message (Error_Message);
+
+ type Error_Info is record
+ Symbol : Ada.Strings.Unbounded.Unbounded_String;
+ Position : Natural;
+ end record;
+
+ type Error_Info_Array is array (Positive range <>) of Error_Info;
+
+
+ -- Note: No consideration is given to ordering of Error_Info items
+ -- encoded into an Error_Message string.
+
+ -- Note: Using "&" to join two Valid Error_Messages together
+ -- will result in an Error_Message that is also Valid,
+ -- but for best results Join should be used instead to
+ -- prevent duplication of Error_Info in the message.
+
+
+ function Valid_Identifier
+ (Check : in String)
+ return Boolean;
+
+ function Valid_Identifier
+ (Check : in Ada.Strings.Unbounded.Unbounded_String)
+ return Boolean;
+
+ function Valid_Identifier_Array
+ (Check : in Error_Info_Array)
+ return Boolean;
+
+ function Valid_Message
+ (Check : in String)
+ return Boolean;
+
+
+ function Join
+ (Left, Right : in Error_Message)
+ return Error_Message;
+
+
+ function Encode
+ (Name : in String;
+ Pos : in Natural)
+ return Error_Message
+ with Pre => Valid_Identifier (Name);
+
+ function Encode
+ (Name : in Ada.Strings.Unbounded.Unbounded_String;
+ Pos : in Natural)
+ return Error_Message
+ with Pre => Valid_Identifier (Name);
+
+ function Encode
+ (Info : in Error_Info)
+ return Error_Message
+ with Pre => Valid_Identifier (Info.Symbol);
+
+ function Encode_Array
+ (Info : in Error_Info_Array)
+ return Error_Message
+ with Pre => Valid_Identifier_Array (Info);
+
+ function Decode
+ (Msg : in Error_Message)
+ return Error_Info_Array;
+
+
+ end Errors;
+
+
private
diff --git a/test/ratnest-tests.adb b/test/ratnest-tests.adb
index 3309b2a..2e3478a 100644
--- a/test/ratnest-tests.adb
+++ b/test/ratnest-tests.adb
@@ -12,12 +12,156 @@ package body Ratnest.Tests is
package Latin renames Ada.Characters.Latin_1;
package Strmaps renames Ada.Strings.Maps;
+ package PE renames Packrat.Errors;
package PU renames Packrat.Util;
+ function Valid_Message_Check
+ return Test_Result is
+ begin
+ if PE.Valid_Message ("abcde") or PE.Valid_Message ("sSYM") or
+ PE.Valid_Message ("sSYMp") or PE.Valid_Message ("p345") or
+ PE.Valid_Message ("pOne") or PE.Valid_Message ("sSYMp1sNEXT") or
+ PE.Valid_Message ("sSYMp1.2") or PE.Valid_Message ("sABcDp12") or
+ not PE.Valid_Message ("sSYMp0") or not PE.Valid_Message ("sAp12") or
+ not PE.Valid_Message ("sNAMEp34sSYMp02") or not PE.Valid_Message ("") or
+ not PE.Valid_Message ("sA_Bp3")
+ then
+ return Failure;
+ end if;
+ return Success;
+ end Valid_Message_Check;
+
+
+ function Valid_Identifier_Check
+ return Test_Result
+ is
+ Pass_Array : PE.Error_Info_Array :=
+ ((+"A", 1), (+"ABC", 2), (+"AB_CD", 3), (+"A_B_CD", 4));
+ Fail_Array : PE.Error_Info_Array :=
+ ((+"_", 1), (+"_A", 2), (+"A_", 3), (+"A__B", 4));
+ begin
+ for EI of Pass_Array loop
+ if not PE.Valid_Identifier (EI.Symbol) or
+ not PE.Valid_Identifier (-EI.Symbol)
+ then
+ return Failure;
+ end if;
+ end loop;
+ for EI of Fail_Array loop
+ if PE.Valid_Identifier (EI.Symbol) or
+ PE.Valid_Identifier (-EI.Symbol)
+ then
+ return Failure;
+ end if;
+ end loop;
+ if not PE.Valid_Identifier_Array (Pass_Array) or
+ PE.Valid_Identifier_Array (Fail_Array)
+ then
+ return Failure;
+ end if;
+ return Success;
+ end Valid_Identifier_Check;
+
+
+ function Join_Check
+ return Test_Result
+ is
+ Array_1 : PE.Error_Info_Array := ((+"A", 1), (+"B", 2));
+ Msg_1 : PE.Error_Message := PE.Encode_Array (Array_1);
+
+ Array_2 : PE.Error_Info_Array := ((+"C", 3), (+"D", 4));
+ Msg_2 : PE.Error_Message := PE.Encode_Array (Array_2);
+
+ Msg_3 : PE.Error_Message := PE.Join (Msg_1, Msg_2);
+
+ Array_4 : PE.Error_Info_Array := ((+"A", 1), (+"B", 2), (+"C", 3), (+"D", 4));
+ Msg_4 : PE.Error_Message := PE.Encode_Array (Array_4);
+
+ Array_5 : PE.Error_Info_Array := ((+"A", 1), (+"B", 4));
+ Msg_5 : PE.Error_Message := PE.Encode_Array (Array_5);
+
+ Array_6 : PE.Error_Info_Array := ((+"A", 1), (+"C", 3));
+ Msg_6 : PE.Error_Message := PE.Encode_Array (Array_6);
+
+ Msg_7 : PE.Error_Message := PE.Join (Msg_5, Msg_6);
+
+ Array_8 : PE.Error_Info_Array := ((+"A", 1), (+"B", 4), (+"C", 3));
+ Msg_8 : PE.Error_Message := PE.Encode_Array (Array_8);
+ begin
+ if Msg_3 /= Msg_4 or Msg_7 /= Msg_8 then
+ return Failure;
+ end if;
+ return Success;
+ end Join_Check;
+
+
+ function Encode_1_Check
+ return Test_Result is
+ begin
+ -- Encode with a String and a Natural
+ if PE.Encode ("ABC", 15) /= "sABCp15" then
+ return Failure;
+ end if;
+ return Success;
+ end Encode_1_Check;
+
+
+ function Encode_2_Check
+ return Test_Result is
+ begin
+ -- Encode with an Unbounded_String and a Natural
+ if PE.Encode (+"ABC", 15) /= "sABCp15" then
+ return Failure;
+ end if;
+ return Success;
+ end Encode_2_Check;
+
+
+ function Encode_3_Check
+ return Test_Result is
+ begin
+ -- Encode with an Error_Info
+ if PE.Encode ((+"ABC", 15)) /= "sABCp15" then
+ return Failure;
+ end if;
+ return Success;
+ end Encode_3_Check;
+
+
+ function Encode_4_Check
+ return Test_Result is
+ begin
+ -- Encode with an Error_Info_Array
+ if PE.Encode_Array (((+"A", 3), (+"BC", 2), (+"ABC", 1), (+"B", 4))) /=
+ "sAp3sBCp2sABCp1sBp4"
+ then
+ return Failure;
+ end if;
+ return Success;
+ end Encode_4_Check;
+
+
+ function Decode_Check
+ return Test_Result
+ is
+ use type PE.Error_Info_Array;
+ begin
+ if PE.Decode ("sAp1sBp3sCp10sDEFp456") /=
+ ((+"A", 1), (+"B", 3), (+"C", 10), (+"DEF", 456))
+ then
+ return Failure;
+ end if;
+ return Success;
+ end Decode_Check;
+
+
+
+
+
function In_Set_Check
return Test_Result
is
diff --git a/test/ratnest-tests.ads b/test/ratnest-tests.ads
index fdcbe71..db20313 100644
--- a/test/ratnest-tests.ads
+++ b/test/ratnest-tests.ads
@@ -3,6 +3,28 @@
package Ratnest.Tests is
+ function Valid_Message_Check return Test_Result;
+ function Valid_Identifier_Check return Test_Result;
+ function Join_Check return Test_Result;
+ function Encode_1_Check return Test_Result;
+ function Encode_2_Check return Test_Result;
+ function Encode_3_Check return Test_Result;
+ function Encode_4_Check return Test_Result;
+ function Decode_Check return Test_Result;
+
+ Error_Tests : Test_Array :=
+ ((+"Valid_Message", Valid_Message_Check'Access),
+ (+"Valid_Identifier", Valid_Identifier_Check'Access),
+ (+"Join", Join_Check'Access),
+ (+"Encode_1", Encode_1_Check'Access),
+ (+"Encode_2", Encode_2_Check'Access),
+ (+"Encode_3", Encode_3_Check'Access),
+ (+"Encode_4", Encode_4_Check'Access),
+ (+"Decode", Decode_Check'Access));
+
+
+
+
function In_Set_Check return Test_Result;
function Not_In_Set_Check return Test_Result;
diff --git a/test/test_main.adb b/test/test_main.adb
index 3ab5269..0ce72b1 100644
--- a/test/test_main.adb
+++ b/test/test_main.adb
@@ -14,6 +14,10 @@ use
procedure Test_Main is
begin
+ Put_Line ("Running tests for Packrat.Errors...");
+ Run_Tests (Error_Tests);
+ New_Line;
+
Put_Line ("Running tests for Packrat.Util...");
Put_Line ("Testing set predicates...");
Run_Tests (Set_Predicate_Tests);