I translated the SemVer 2 BNF grammar to the following Antlr grammar.
grammar SemVer;
@header {
package com.me.semver;
}
semVer : normal ('-' preRelease)? ('+' build)? ;
normal : major '.' minor '.' patch ;
major : NUM ;
minor : NUM ;
patch : NUM ;
preRelease : PRE_RELEASE ('.' preRelease)* ;
build : BUILD ('.' build)*;
NUM : '0'
| POSITIVE_DIGIT
| POSITIVE_DIGIT DIGITS
;
BUILD : ALPHANUM
| DIGITS
;
PRE_RELEASE : ALPHANUM
| NUM
;
fragment
ALPHANUM : NON_DIGIT
| NON_DIGIT CHARS
| CHARS NON_DIGIT
| CHARS NON_DIGIT CHARS
;
fragment
CHARS : CHAR+ ;
fragment
CHAR : DIGIT
| NON_DIGIT
;
fragment
NON_DIGIT : LETTER
| '-'
;
fragment
DIGITS : DIGIT+ ;
fragment
DIGIT : '0'
| POSITIVE_DIGIT
;
fragment
POSITIVE_DIGIT : [1-9] ;
fragment
LETTER : [a-zA-Z] ;
But parsing 1.0.0-beta+exp.sha.5114f85
gives the following error:
line 1:4 mismatched input '0-beta' expecting NUM
The output from the listener is as follows:
Normal: 1.0.0-beta
Major: 1
Minor: 0
Patch: 0-beta
Build: exp.sha.5114f85
Build: sha.5114f85
Build: 5114f85
Clearly, the patch version is not what it should be. The correct output would have Patch = 0
, Pre release = beta
, and Build = exp.sha.5114f85
.
How can I fix the grammar?