Skip to content

Commit

Permalink
Code review nits from peggyjs#300
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Jun 20, 2022
1 parent 4b98f92 commit b570309
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ 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
Expand Down Expand Up @@ -37,6 +34,8 @@ Released: TBD
(can be useful for plugin writers), from @Mingun
- [#294](https://github.com/peggyjs/peggy/pull/294) Website: show errors in the
editors, from @Mingun
- [#299](https://github.com/peggyjs/peggy/issues/299) Add example grammar for a
[SemVer.org](https://semver.org) semantic version string, from @dselman

### Bug Fixes

Expand Down
30 changes: 14 additions & 16 deletions examples/semver.peggy
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,27 @@
* 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 };
return { ...versionCore, pre, build };
}

versionCore
= major:$numericIdentifier '.' minor:$numericIdentifier '.' patch:$numericIdentifier
{
return {
major: parseInt(major, 10),
minor: parseInt(minor, 10),
patch: parseInt(patch, 10),
};
= major:numericIdentifier '.' minor:numericIdentifier '.' patch:numericIdentifier {
return { major, minor, patch };
}

preRelease
= head:$preReleaseIdentifier tail:('.' @$preReleaseIdentifier)*
{
= head:preReleaseIdentifier tail:('.' @preReleaseIdentifier)* {
return [head, ...tail];
}

build
= head:$buildIdentifier tail:('.' @$buildIdentifier)*
{
= head:buildIdentifier tail:('.' @buildIdentifier)* {
return [head, ...tail];
}

Expand All @@ -39,13 +33,17 @@ preReleaseIdentifier

buildIdentifier
= alphanumericIdentifier
/ digit+
/ $digit+ // Not a number, buildIdentifiers aren't semantically significant.

// If there is a non-digit anywhere, this label is alphanumeric, and
// is compared lexically. Return a string.
alphanumericIdentifier
= digit* nonDigit identifierChar*
= $(digit* nonDigit identifierChar*)

// Any semantically significant numbers are turned into integers for
// later numeric comparison.
numericIdentifier
= '0' / (positiveDigit digit*)
= n:('0' / $(positiveDigit digit*)) { return parseInt(n, 10); }

identifierChar
= [a-z0-9-]i
Expand All @@ -57,4 +55,4 @@ digit
= [0-9]

positiveDigit
= [1-9]
= [1-9]

0 comments on commit b570309

Please sign in to comment.