Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept empty minor/patch points in relaxed parsing #20

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: build

on:
push:
branches:
- main
- master
pull_request:
paths-ignore:
- 'doc/**'
Expand All @@ -10,17 +13,18 @@ on:
jobs:

build:
name: ${{ matrix.os }}
name: ${{ matrix.os }} / GNAT ${{ matrix.gnat }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- macos-latest
- macos-12
- ubuntu-latest
- windows-latest

gnat: [10, 11, 12, 13, 14]

steps:
- name: Check out repository
uses: actions/checkout@v1
Expand All @@ -29,22 +33,9 @@ jobs:
run: git submodule update --init --recursive

- name: Set up GNAT toolchain (FSF)
if: matrix.os == 'ubuntu-latest'
uses: ada-actions/toolchain@ce2021
with:
distrib: fsf

- name: Set up GNAT toolchain (Community 2021)
if: matrix.os == 'windows-latest'
uses: ada-actions/toolchain@ce2021
with:
distrib: community

- name: Set up GNAT toolchain (Community 2020)
if: matrix.os == 'macos-latest'
uses: ada-actions/toolchain@ce2020
uses: alire-project/alr-install@v1
with:
distrib: community
crates: gnat_native^${{matrix.gnat}} gprbuild

- name: Build on demand
run: gprbuild -j0 -p -XSEMVER_BUILD_MODE=On_Demand
Expand Down
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
3 changes: 3 additions & 0 deletions src/semver.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
with Semantic_Versioning;

package Semver renames Semantic_Versioning;
Loading