-
Notifications
You must be signed in to change notification settings - Fork 66
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
(feat) add semver.org example #300
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think that hiding most important information into a deeper object with non-descript name is a good idea. At least |
||
} | ||
|
||
versionCore | ||
= major:$numericIdentifier '.' minor:$numericIdentifier '.' patch:$numericIdentifier | ||
{ | ||
return { | ||
major: parseInt(major, 10), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor parseInt into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving parseInt to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then an intermediate rule that does the parsing, I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regardless, the |
||
minor: parseInt(minor, 10), | ||
patch: parseInt(patch, 10), | ||
}; | ||
} | ||
|
||
preRelease | ||
= head:$preReleaseIdentifier tail:('.' @$preReleaseIdentifier)* | ||
{ | ||
return [head, ...tail]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for this. I automatically kept using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's in the docs now, look for "Parsing Lists". |
||
} | ||
|
||
build | ||
= head:$buildIdentifier tail:('.' @$buildIdentifier)* | ||
{ | ||
return [head, ...tail]; | ||
} | ||
|
||
preReleaseIdentifier | ||
= alphanumericIdentifier | ||
/ numericIdentifier | ||
|
||
buildIdentifier | ||
= alphanumericIdentifier | ||
/ digit+ | ||
|
||
alphanumericIdentifier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to spec, it should be If you substitute everything into In PEG parsers there is no parse conflicts, it can be greatly simplified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is all you need to comply to the spec:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
rule semver = major:numericId "." minor:numericId "." patch:numericId prerelease:("-" @prerelease)? build:("+" @build)? {
return {major, minor, patch, prerelease, build};
}
prerelease = head:prereleaseId tail:("." @prereleaseId)* { return [head, ...tail]; }
build = head:alnumId tail:("." @alnumId)* { return [head, ...tail]; }
prereleaseId = alnumId / numericId
alnumId = $[0-9a-z-]i+
numericId = $("0" / [1-9][0-9]*) However, while this will successfully parse all valid examples it will also successfully parse invalid examples ( |
||
= digit* nonDigit identifierChar* | ||
|
||
numericIdentifier | ||
= '0' / (positiveDigit digit*) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this? numericIdentifier
= num:$(positiveDigit digit*) { return parseInt(num) }
/ '0' ![0-9] { return 0 } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH I don't understand how this works right now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest a bit different code, though:
IMO you don't have to write a lot of code to make it more readable. "Positive digit" doesn't add any more information to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trying to keep it closer to https://semver.org/spec/v2.0.0.html I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The magic in the
Agreed, especially since this rule is used only once There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I missed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anyway, I didn't find a place in the spec that would limit numbers by any limit, so I suspect |
||
|
||
identifierChar | ||
= [a-z0-9-]i | ||
|
||
nonDigit | ||
= [a-z-]i | ||
|
||
digit | ||
= [0-9] | ||
|
||
positiveDigit | ||
= [1-9] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newline at end, please There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please create an issue to set up There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't have a linter for .peggy files yet, as far as I know. There should be one. We lint all of the JS on each GHA run. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix this up after merge. Apparently I didn't leave a template in the changelog after 2.0.1, because that was done i n such a rush.