I am trying to write a grammar for parsing single line comments. Comments starts with '--' can appear anywhere in the file.
My basic grammar looks like below.
Grammar (aa.g4):
grammar aa;
statement
: commentStatement* ifStatement
| commentStatement* returnStatement
;
ifStatement
: 'if' '(' expression ')'
returnStatement+
;
returnStatement : 'return' expression ';' ;
commentStatement : '--' (.+?) '\\n'? ;
expression : IDENTIFIER ;
IDENTIFIER : [a-z]([A-Za-z0-9\-\_])* ;
NEWLINE : '\r'? '\n' -> skip ;
WS : [ \t\r\f\n]+ -> skip ;
Test class:
public class aaTest {
static class aaListener extends aaBaseListener {
public void enterCommentStatement(CommentStatementContext ctx) {
System.out.println(ctx.getText());
}
}
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("aa.txt");
CharStream stream = new ANTLRInputStream(is);
aaLexer lexer = new aaLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
aaParser parser = new aaParser(tokenStream);
ParseTree aParseTree = parser.statement();
ParseTreeWalker aWalker = new ParseTreeWalker();
aWalker.walk(new aaListener(), aParseTree);;
}
}
Input:
--comment1
-- if comment
if (x) --mid if comment
--end comment
return result;
Output:
--comment1a
--ifcommentif(x) <<< error output
--midifcomment
--endcomment
Queries:
- What is the issue in parsing error output above. I need only "-- if comment" to be printed.
- How do I get and output actual comment with spaces.