[GH-ISSUE #667] Semver 2.0.0 string parser/composer using Prolog's DCG (including unit tests) #4702

Open
opened 2026-06-13 13:02:35 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @dtonhofer on GitHub (Feb 24, 2021).
Original GitHub issue: https://github.com/semver/semver/issues/667

An SWI-Prolog module to

  • parse a "semver 2.0.0 string" into its components, and to
  • assemble components back into a semver string

can be found here:

Code and Unit Tests

(Note that github can't syntax-highlight the unit tests file .plt properly, thinking it is gnuplot code. This is why it looks messy.)

This is a simplified version from an overly complex earlier version, somewhat adapted to SWI-Prolog's "codes" approach.

Additionally, for alphanum_id:

  • also excurse into EBNF instead of just BNF to avoid copying rules
  • change the grammar slightly to increase determinism: an alphanum_id is simply "possibly zero digits followed by a nondigit followed by any number of id_chars"

And so:

$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.19-30-gdba93e8ba)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- [semver].
true.

?- load_test_files([]).
true.

23 test cases to make sure we stay on track

?- run_tests.
% PL-Unit: semver ....................... done
% All 23 tests passed
true.

Assemble semver string from elements:

?- semantic_version(Text,1,2,3,['00x67'],[alpha,'foo-bar','0099']),!.
Text = '1.2.3-00x67+alpha.foo-bar.0099'.

Disassemble semver string into elements:

?- semantic_version('1.2.3-00x67+alpha.foo-bar.0099',Maj,Min,Pat,Pre,Build),!.
Maj = 1,
Min = 2,
Pat = 3,
Pre = ['00x67'],
Build = [alpha,'foo-bar','0099'].

Bad string doesn't pass the grammar

?- semantic_version('1.x.x',Maj,Min,Pat,Pre,Build),!.
false.

Also added tests that:

Originally created by @dtonhofer on GitHub (Feb 24, 2021). Original GitHub issue: https://github.com/semver/semver/issues/667 An SWI-Prolog module to - parse a "semver 2.0.0 string" into its components, and to - assemble components back into a semver string can be found here: [Code and Unit Tests](https://github.com/dtonhofer/prolog_code/blob/main/unpacked/onepointfour_semver/) (Note that github can't syntax-highlight the unit tests file .plt properly, thinking it is gnuplot code. This is why it looks messy.) This is a simplified version from an overly complex earlier version, somewhat adapted to SWI-Prolog's "codes" approach. Additionally, for `alphanum_id`: - also excurse into EBNF instead of just BNF to avoid copying rules - change the grammar slightly to increase determinism: an alphanum_id is simply _"possibly zero digits followed by a nondigit followed by any number of id_chars"_ And so: ``` $ swipl Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.19-30-gdba93e8ba) SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software. Please run ?- license. for legal details. For online help and background, visit https://www.swi-prolog.org For built-in help, use ?- help(Topic). or ?- apropos(Word). ?- [semver]. true. ?- load_test_files([]). true. ``` 23 test cases to make sure we stay on track ``` ?- run_tests. % PL-Unit: semver ....................... done % All 23 tests passed true. ``` Assemble semver string from elements: ``` ?- semantic_version(Text,1,2,3,['00x67'],[alpha,'foo-bar','0099']),!. Text = '1.2.3-00x67+alpha.foo-bar.0099'. ``` Disassemble semver string into elements: ``` ?- semantic_version('1.2.3-00x67+alpha.foo-bar.0099',Maj,Min,Pat,Pre,Build),!. Maj = 1, Min = 2, Pat = 3, Pre = ['00x67'], Build = [alpha,'foo-bar','0099']. ``` Bad string doesn't pass the grammar ``` ?- semantic_version('1.x.x',Maj,Min,Pat,Pre,Build),!. false. ``` Also added tests that: - exercise the regular expression given on https://regex101.com/r/Ly7O1x/3/ - run the examples given on https://regex101.com/r/Ly7O1x/3/ against the DCG
GiteaMirror added the question label 2026-06-13 13:02:35 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/semver#4702