From 0a200beb4a6bbc1d2a8db7a630341d67ddd2810b Mon Sep 17 00:00:00 2001 From: Dan Selman Date: Sat, 18 Jun 2022 12:35:34 +0100 Subject: [PATCH] (feat) add semver.org example Signed-off-by: Dan Selman --- AUTHORS | 1 + CHANGELOG.md | 3 +++ examples/semver.peggy | 60 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 examples/semver.peggy diff --git a/AUTHORS b/AUTHORS index 06715be7..20c5aab8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -14,6 +14,7 @@ Balázs Kutil (https://github.com/bkutil/) Caleb Hearon (https://github.com/chearon/) Charles Pick (https://github.com/phpnode/) Christian Flach (https://github.com/cmfcmf/) +Dan Selman (https://github.com/dselman) David Berneda Futago-za Ryuu (https://github.com/futagoza/) Jakub Vrana (https://github.com/vrana/) diff --git a/CHANGELOG.md b/CHANGELOG.md index 116b732e..e717585d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ This file documents all notable changes to Peggy. Unreleased ---------- +- [#299](https://github.com/peggyjs/peggy/issues/299) Add example grammar for a + [SemVer.org](https://semver.org) semantic version string + Released: TBD ### Major Changes diff --git a/examples/semver.peggy b/examples/semver.peggy new file mode 100644 index 00000000..8107c4b8 --- /dev/null +++ b/examples/semver.peggy @@ -0,0 +1,60 @@ +/** + * SemVer.org v2 + * https://semver.org/spec/v2.0.0.html + * For unit tests see: https://github.com/dselman/peggy-semver + */ +semver + = versionCore:versionCore + pre:('-' @preRelease)? + build:('+' @build)? + { + return { versionCore, pre, build }; + } + +versionCore + = major:$numericIdentifier '.' minor:$numericIdentifier '.' patch:$numericIdentifier + { + return { + major: parseInt(major, 10), + minor: parseInt(minor, 10), + patch: parseInt(patch, 10), + }; + } + +preRelease + = head:$preReleaseIdentifier tail:('.' @$preReleaseIdentifier)* + { + return [head, ...tail]; + } + +build + = head:$buildIdentifier tail:('.' @$buildIdentifier)* + { + return [head, ...tail]; + } + +preReleaseIdentifier + = alphanumericIdentifier + / numericIdentifier + +buildIdentifier + = alphanumericIdentifier + / digit+ + +alphanumericIdentifier + = digit* nonDigit identifierChar* + +numericIdentifier + = '0' / (positiveDigit digit*) + +identifierChar + = [a-z0-9-]i + +nonDigit + = [a-z-]i + +digit + = [0-9] + +positiveDigit + = [1-9] \ No newline at end of file