In my grammar it should normally be allowed to freely insert whitespaces between tokens. However, this shouldn't be the case for semantic versions: 1 . 0 . 0 feels wrong.
In the grammar below, whitespaces are sent to the hidden channel and should be allowed between a version comparator and the version itself, but not the version components.
How can I check for whitespaces between them and report as unwanted tokens? In my case adding predicates is fine as this is only used in Java.
versionPredicate
: (stringComparator | semanticComparator)? semanticVersion
;
semanticComparator
: REPL_MARK
| COMP_MAJOR
;
stringComparator
: COMP_EQUAL
| COMP_NEQUAL
| COMP_MORE
| COMP_GMORE
| COMP_LESS
| COMP_GLESS
;
semanticVersion
: versionCore (DASH preRelease)? (PLUS buildMetadata)?
;
versionCore: NUMERIC (DOT NUMERIC)*;
preRelease: metadata (DOT metadata)*;
buildMetadata: metadata (DOT metadata)*;
metadata: NUMERIC | IDENTIFIER;
fragment NUMBER: '0'|[1-9][0-9]*;
fragment IDENTIFIER_START: [_a-zA-Z];
fragment IDENTIFIER_PART: [_\-a-zA-Z0-9];
REPL_MARK: '~';
DOT: '.';
DASH: '-';
PLUS: '+';
COMP_MAJOR: '^';
COMP_EQUAL: '=';
COMP_NEQUAL: '!=';
COMP_MORE: '>';
COMP_GMORE: '>=';
COMP_LESS: '<';
COMP_GLESS: '<=';
NUMERIC: NUMBER;
IDENTIFIER: IDENTIFIER_START IDENTIFIER_PART*;
WHITESPACE: [ \t]+ -> channel(HIDDEN);
(as a part of https://codeberg.org/stonecutter/stonecutter/src/branch/0.9/stitcher/src/main/antlr/dev/kikugie/stitcher/antlr/Stitcher.g4)