Skip to content

BNF Grammar for Version #110

Closed
Closed

Description

@petermichaux

Recently there has been confusion around what is allowed as a version. The standard is ambiguous. I propose including a BNF grammar for what is allowed. It is short and completely unambiguous so why not include it?

<version> ::= <major> "." <minor> "." <patch>
            | <major> "." <minor> "." <patch> "-" <pre-release>
            | <major> "." <minor> "." <patch> "+" <build>
            | <major> "." <minor> "." <patch> "-" <pre-release> "+" <build>

<major> ::= <non-negative integer>

<minor> ::= <non-negative integer>

<patch> ::= <non-negative integer>

<pre-release> ::= <dot-separated identifiers>

<build> ::= <dot-separated identifiers>

<dot-separated identifiers> ::= <identifier>
                              | <identifier> "." <dot-separated identifiers>

<identifier> ::= <identifier character>
               | <identifier character> <identifier>

<identifier character> ::= <letter> | <digit> | "-"

<non-negative integer> ::= "0"
                         | <positive integer>

<positive integer> ::= <positive digit>
                     | <positive digit> <digits>

<digits> ::= <digit>
           | <digit> <digits>

<digit> ::= "0" | <positive digit>

<positive digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

<letter> ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J"
           | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T"
           | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d"
           | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n"
           | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x"
           | "y" | "z"

Activity

  1. petermichaux commented on Jun 11, 2013

    @petermichaux
    Author

    Note that the grammar I have proposed does not allow major, minor, or patch to have unnecessary leading zeros. So major, minor, and patch can be 0, 1, 3200, etc but cannot be 00, 05, etc.

  2. tbull commented on Jun 11, 2013

    @tbull

    Note that the grammar I have proposed does not allow [..] unnecessary leading zeros.

    That could be overcome with the following, provided we want that.

    <non-negative integer> ::= <digits>
    
    <digits> ::= <digit>
               | <digit> <digits>
    
    <digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
    
  3. petermichaux commented on Jun 11, 2013

    @petermichaux
    Author

    I think leading zeros are not desirable. They are not semantic. They are for manipulating automatic file listing order.

  4. tbull commented on Jun 11, 2013

    @tbull

    I have no opinion on that detail. My implementation allows leading zeroes.

    One point against leading zeroes could be that there are some languages inspired by C that parse numbers starting with 0 as octal notation. This goes well so long as there are only digits 0 through 7 used, but fails if digits 8 or 9 show up. This is one common source of surprise for noobs to a language. On the other hand, I think, a spec should not take such language peculiarities into account.

    One point for leading zeroes is that, if they are disallowed in major/minor/patch, they should be disallowed in numeric id parts of pre-release strings, too, and that would make matters even more complicated.

  5. isaacs commented on Jun 11, 2013

    @isaacs
    Contributor

    YES. THIS. 👍

  6. petermichaux commented on Jun 12, 2013

    @petermichaux
    Author

    A !JavaScript regular expression for this grammar is the following and could be included as non-normative bonus content since getting regular expressions correct is error prone. (This one should definitely be checked.)

    /^([1-9]\d*)\.([1-9]\d*)\.([1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/
    

    The first capturing group is "major".

    The second capturing group is "minor".

    The third capturing group is "patch".

    The forth capturing group is "pre-release" (not including the preceding dash delimiter between patch and pre-release).

    The "build" is non-capturing because it is not to be used in any comparisons.

  7. Tieske commented on Jun 12, 2013

    @Tieske
    Contributor

    imo leading zero's should be allowed, they don't do any harm (comparison is numerically), but might help out in sorting stuff easily.

    I have little regex experience, but it seems to me that the regex fails on 10.10.10 as a version as zeros are not allowed at all. Besides that, I don't think the spec should include any regexes.

  8. petermichaux commented on Jun 12, 2013

    @petermichaux
    Author
    > /^([1-9]\d*)\.([1-9]\d*)\.([1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/.test('10.10.10')
    true
    

    In my experience, most developers are nervous and diffident when it comes to regular expressions. Including one as non-normative content could help ensure poor implementations do not appear. This will help the perceived integrity of the standard. It is optional, of course, but I think would be very helpful to have it in an easy to find location: right in the standard.

  9. haacked commented on Jun 12, 2013

    @haacked
    Contributor

    @Tieske The /d* matches 0-9. The * means "0 or more". So this would match 10.10.10.

  10. isaacs commented on Jun 12, 2013

    @isaacs
    Contributor

    Leading zeroes should not be allowed. It'd be confusing if 1.1.1 was the "same version" as 01.01.000001. Is this ambiguous in the language of the spec?

  11. petermichaux commented on Jun 12, 2013

    @petermichaux
    Author

    Leading zeroes should not be allowed. It'd be confusing if 1.1.1 was the "same version" as 01.01.000001. Is this ambiguous in the language of the spec?

    I agree. That is a good example.

  12. tbull commented on Jun 12, 2013

    @tbull

    It'd be confusing if 1.1.1 was the "same version" as 01.01.000001.

    I don't see the point. What's so bad about that?

    As to example rexgexp, certainly would hurt to include one as a non-normative example. There have several examples already been published somewhere in all the issues here.

  13. isaacs commented on Jun 12, 2013

    @isaacs
    Contributor

    @tbull The versions look dramatically different. From the spec's point of view, they'd have the same precedence. However, it's strange and confusing to me that they're the same, when they apparently differ quite a bit.

    Regardless, this definitely needs to be clarified in the specification.

  14. isaacs commented on Jun 12, 2013

    @isaacs
    Contributor

    @petermichaux That regexp doesn't handle zeros in the main version portions.

    Fixed to match the BNF you presented (which I believe is correct):

    /^([1-9]\d*|0)\.([1-9]\d*|0)\.([1-9]\d*|0)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/
    
  15. petermichaux commented on Jun 12, 2013

    @petermichaux
    Author

    Thanks, @isaacs. I did miss the zeros.

  16. 1 remaining item

  17. petermichaux commented on Jun 13, 2013

    @petermichaux
    Author

    The regular expression could be appropriately included in "An implementers FAQ with normative and non-normative examples" mentioned in #113

  18. isaacs commented on Jun 14, 2013

    @isaacs
    Contributor

    That BNF would allow leading zeroes in numeric prerelease identifiers, because "0009" is a valid set of "identifier characters".

    Working on a more restrictive grammar now.

  19. isaacs commented on Jun 14, 2013

    @isaacs
    Contributor

    Disallowing leading zeroes on numeric identifiers.

    Regexp:

    /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*))*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/
    

    BNF:

    <version> ::= <major> "." <minor> "." <patch>
                | <major> "." <minor> "." <patch> "-" <pre-release>
                | <major> "." <minor> "." <patch> "+" <build>
                | <major> "." <minor> "." <patch> "-" <pre-release> "+" <build>
    
    <major> ::= <non-negative integer>
    
    <minor> ::= <non-negative integer>
    
    <patch> ::= <non-negative integer>
    
    <pre-release> ::= <dot-separated identifiers>
    
    <build> ::= <dot-separated identifiers>
    
    <dot-separated identifiers> ::= <identifier>
                                  | <identifier> "." <dot-separated identifiers>
    
    <identifier> ::= <non-negative integer>
                   | <non-numeric identifier>
    
    <non-numeric identifier> ::= <letter>
                               | "-"
                               | <non-numeric identifier> <digits>
                               | <digits> <non-numeric identifier>
    
    <identifier character> ::= <letter> | <digit> | "-"
    
    <non-negative integer> ::= "0"
                             | <positive integer>
    
    <positive integer> ::= <positive digit>
                         | <positive digit> <digits>
    
    <digits> ::= <digit>
               | <digit> <digits>
    
    <digit> ::= "0" | <positive digit>
    
    <positive digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
    
    <letter> ::= "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J"
               | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T"
               | "U" | "V" | "W" | "X" | "Y" | "Z" | "a" | "b" | "c" | "d"
               | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n"
               | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x"
               | "y" | "z"
    
  20. isaacs commented on Jun 14, 2013

    @isaacs
    Contributor

    Regular expression with comments:

    var r = new RegExp(
      '^' +
      '(0|[1-9]\\d*)' + // major
      '\\.(0|[1-9]\\d*)' + // minor
      '\\.(0|[1-9]\\d*)' + // patch
      '(?:-' + // start prerelease
        '(' + // capture
          '(?:' + // first identifier
            '0|' + // 0, or
            '[1-9]\\d*|' + // numeric identifier, or
            '\\d*[a-zA-Z-][a-zA-Z0-9-]*' + // id with at least one non-number
          ')' + // end first identifier
          '(?:\\.' + // dot-separated
            '(?:0|[1-9]\\d*|\\d*[a-zA-Z-][a-zA-Z0-9-]*)' + // identifier
          ')*' + // zero or more of those
        ')' + // end prerelease capture
      ')?' + // prerelease is optional
      '(?:' + // build tag (non-capturing)
        '\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*' + // pretty much anything goes
      ')?' + // build tag is optional
      '$'
    )
  21. mojombo commented on Jun 14, 2013

    @mojombo
    Contributor

    I'm in favor of a BNF grammar in the spec. As SemVer gains adoption, and especially as tools rely on well-formed versions, it will be critical that no ambiguity exists. Seems fine to put it in the main spec rather than an appendix. One page to rule them all.

  22. petermichaux commented on Jun 14, 2013

    @petermichaux
    Author

    @isaacs I believe the following part of your BNF is incorrect. You don't allow aa, --, or a1a, for example. You don't use <identifier character> at all.

    <non-numeric identifier> ::= <letter>
                               | "-"
                               | <non-numeric identifier> <digits>
                               | <digits> <non-numeric identifier>
    
    <identifier character> ::= <letter> | <digit> | "-"
    

    I think it should be

    <non-numeric identifier> ::= <non-digit character>
                               | <non-digit character> <identifier>
                               | <identifier> <non-digit character>
                               | <identifier> <non-digit character> <identifier>
    
    <identifier> ::= <identifier character>
                   | <identifier character> <identifier>
    
    <non-digit character> ::= <letter> | "-"
    
    <identifier character> ::= <non-digit character> | <digit>
    
  23. isaacs commented on Jun 14, 2013

    @isaacs
    Contributor

    Yes, that is correct, thanks. My regexp-fu greatly exceeds my bnf-fu :)

  24. added a commit that references this issue on Jun 14, 2013
    f31c422
  25. isaacs commented on Jun 14, 2013

    @isaacs
    Contributor

    @mojombo Great, thanks for the direction. Patch submitted in #116.

    Seems fine to put it in the main spec rather than an appendix. One page to rule them all.

    I absolutely agree about the BNF grammar, since that's part of the "specification" bit. How would you feel about adding example pseudocode and/or authoritative test cases as separate pages, or even just separate files in this repo that don't show up on the website at all? I'd like them to be included for clarity, but the concern is that the page could get too unwieldy.

  26. added 2 commits that reference this issue on Jun 14, 2013
    96dce56
    74ca033
  27. EddieGarmon commented on Jun 18, 2013

    @EddieGarmon
    Contributor

    A proposed simplified and descriptive update to the BNF.
    isaacs#1

  28. added a commit that references this issue on Jun 19, 2013
    375b063
  29. added a commit that references this issue on Jul 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions