Skip to content

Commit

Permalink
Do not fail relaxed parsing on empty minor/patch numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
mosteo committed Jun 20, 2024
1 parent 0b66141 commit 021a276
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/semantic_versioning-demo.adb
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,9 @@ begin
pragma Assert (not X.Is_In (V ("1"), X.Value ("!(1|3)")));
pragma Assert (not X.Is_In (V ("3"), X.Value ("!(1|3)")));

-- Relaxed parsing, instead of failing we should dump the first strange thing into the build part
pragma Assert (Parse ("1.dfsg-3.1ubuntu2", Relaxed => True) = V ("1+.dfsg-3.1ubuntu2"));
pragma Assert (Parse ("1.3.dfsg-3.1ubuntu2", Relaxed => True) = V ("1.3+.dfsg-3.1ubuntu2"));

Put_Line ("OK");
end Semantic_Versioning.Demo;
6 changes: 4 additions & 2 deletions src/semantic_versioning-parser.adb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Exceptions;
with Ada.Strings.UTF_Encoding.Wide_Wide_Strings;
with Ada.Wide_Wide_Text_IO; use Ada.Wide_Wide_Text_IO;

Expand All @@ -19,7 +20,8 @@ begin

Put_Line (Decode (Image (V)));
exception
when Malformed_Input =>
Put_Line ("Uh oh... that was not a nice version!");
when E : Malformed_Input =>
Put_Line ("Uh oh... that was not a nice version: "
& Decode (Ada.Exceptions.Exception_Message (E)));
end;
end Semantic_Versioning.Parser;
36 changes: 30 additions & 6 deletions src/semantic_versioning.adb
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ package body Semantic_Versioning is
function Eat_Number return Point is
Last : Natural := Next + 1;
begin
if Next <= Description'Last and then Next_Token (Next) /= Number then
raise Malformed_Input with "Empty point number: " & Description (Next);
elsif Next > Description'Last then
raise Malformed_Input with "Empty point number (EOF)";
end if;

while Last <= Description'Last and then Next_Token (Last) = Number loop
Last := Last + 1;
end loop;

if Next > Description'Last or else Last = Next then
raise Malformed_Input with "Empty point number";
end if;

return Number : constant Point := Point'Value (Description (Next .. Last - 1)) do
Next := Last;
end return;
Expand Down Expand Up @@ -138,8 +140,30 @@ package body Semantic_Versioning is
begin
case To_See is
when Major => V.Major := Eat_Number;
when Minor => V.Minor := Eat_Number;
when Patch => V.Patch := Eat_Number;
when Minor =>
begin
V.Minor := Eat_Number;
exception
when Malformed_Input =>
if Relaxed then
Next := Next - 1; -- We need to re-eat the '.'
Accept_Build;
else
raise;
end if;
end;
when Patch =>
begin
V.Patch := Eat_Number;
exception
when Malformed_Input =>
if Relaxed then
Next := Next - 1; -- We need to re-eat the '.'
Accept_Build;
else
raise;
end if;
end;
when others => raise Malformed_Input with "All foreseeable points already seen";
end case;
To_See := Foreseeable'Succ (To_See);
Expand Down

0 comments on commit 021a276

Please sign in to comment.