user1469116
user1469116

Reputation:

Find Comment in ANTLR Grammar

I have the following grammar:

grammar bxk; 
options
{
    language=CSharp3;
}

// start rule
start
    : com=comment root EOF
    ;


root
    : ROOT_ID CT_ID END
    ;

comment returns [string comment]
    :  ((('/*' des=(~'*/')* '*/'))NEWLINE) {$comment=$des.text;}
    ;


CHAR    :  ('a'..'z'|'A'..'Z') ;
ROOT_ID :  'ROOT_'(CHAR | DIGIT | SPECIAL)+ ;
CT_ID   :  'ct_'(CHAR | DIGIT | SPECIAL)+ ;
DIGIT   :  '0'..'9';
SPECIAL :  '_' ;
END :  ';';
ASSIGN : '=';
STRING  :  CHAR (CHAR | DIGIT | SPECIAL)*;
WS      :  (' '|'\t' | '\n' | '\r' | '\u000C')+ {Skip();} ; 

Now I will get the comment betwenn the /* and */. That works fine. But the spaces were cleaned because the WS token with the Skip() call. Can I have anything to skip the white spaces but within the comment structure I want to have the spaces.

I hope anyone have an solution?

Upvotes: 1

Views: 1003

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170288

Don't make comment a parser rule. Make it a lexer rule instead:

COMMENT
    :  '/*' .* '*/' // no need for a NEWLINE at the end
    ;

In your parser rule(s), you can then do:

p returns [string comment]
 : FOO COMMENT BAR {$comment = $COMMENT.text;} // or {$comment = $COMMENT.Text;}
 ;

Upvotes: 2

Related Questions