I'm currently developing a parser for an old proprietary markup-like language which has to be converted to a newer standard. I'm using ANTLR 4 for that.
The structure is composed by blocks delimited by a specific starter and its relative terminator (eg. { ... }, < ... >, INPUT ... END). Inside each block, elements are specified in rows, separated by newlines; actually, only somewhere these newlines are needed to understand what code means.
For example:
< ID
SOME_VAR "optional modifier string"
$anEnvironmentVariable
"a constant string"
"another constant" "with its optional modifier"
>
A parser rule like the following
field
: OPEN_ANGLED_BRACKET row_id
((ENVIRONMENT_VAR | DQUOTE_STR | VAR) DQUOTE_STR?)+
CLOSED_ANGLED_BRACKET
;
// [...]
WHITESPACE
: [ \t\r\n] -> skip
;
can easily parse the above example, but because newlines are ignored, it can't actually distinguish if a double-quoted string is a constant (meaning it's at the start of the line) or a modifier string (which follows a previous variable/constant in the same line).
I could actually explicitly handle the newline like this:
field
: OPEN_ANGLED_BRACKET row_id NEWLINE
((ENVIRONMENT_VAR | DQUOTE_STR | VAR) DQUOTE_STR? NEWLINE)+
CLOSED_ANGLED_BRACKET NEWLINE
;
// [...]
WHITESPACE
: [ \t] -> skip
;
NEWLINE
: '\r'? '\n'
| '\r'
;
but then I must explicitly handle newline everywhere in the rest of the grammar, complicating it by a lot!
Is there any way to keep explicit newline confined inside angled brakets, skipping it everywhere else "automatically"?

