简体   繁体   中英

ANTLR not matching empty comments

I am using ANTLR to parse a language which uses the colon for both a comment indicator and as part of a 'becomes equal to' assignment. So for example in the line

Index := 2    :Set Index

I need to recognize the first part as an assignment statement and the text after the second colon as a comment. Currently I do this using the rule:

COMMENT                 : ':'+ ~[:='\r\n']*;

This seems to work OK apart from when the colon is immediately followed by a new line. eg in the line

Index := 2    :

the newline occurs immediately after the second colon. In this case the comment is not recognized and the rest of the code is not parsed in the correct context. If there is a single space after the second colon the line is parsed correctly.

I expected the '\\r'\\n' to cope with this but it only seems to work if there is at least one character after the comment symbol - have I missed something from the command?

The braces denote a collection of characters without any quotes. Hence your '\\r\\n' literal doesn't work there (you should have got a warning that the apostrophe is included more than once in the char range.

Define the comment like this instead:

COMMENT: ':'+ ~[:=\n\r]*;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM