XText : ML_COMMENT overrides own written comment

544 Views Asked by At

I am new to xtext.

I want to add the following comments.

  1. ML_COMMENT (from org.eclipse.xtext.common.Terminals)
  2. own comment

    /* @var var1 @property property1 */

The following is my attempt code.

grammar org.xtext.example.mydemo.MyDemo with org.eclipse.xtext.common.Terminals

generate myDemo "http://www.xtext.org/example/mydemo/MyDemo"

Model:
   (elements+=DocComments)*
;

DocComments hidden(WS, OTHER):
   start=StartComment name=VarComment? property=PropertyComment* end=EndComment
;

StartComment: '/**';
EndComment:'*/';
VarComment: key='@var' name=ID;
PropertyComment: key='@property' name=ID;

terminal OTHER: '*';

It seems that ML_COMMENT works and own comment does not work.

How to resolve this problem? Please help.

1

There are 1 best solutions below

5
On

you can override ML_COMMENT to something "else"

e.g.

ML_COMMENT: '%'->'%';

or you dont inherit from terminals but copy the rules (except ML_COMMENT)

grammar org.xtext.example.demodsl.DemoDsl hidden(WS, SL_COMMENT)

generate demoDsl "http://www.xtext.org/example/demodsl/DemoDsl"

import "http://www.eclipse.org/emf/2002/Ecore" as ecore
Model:
    greetings+=Greeting*;

Greeting:
    'Hello' name=ID '!';


terminal ID         : '^'?('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal INT returns ecore::EInt: ('0'..'9')+;
terminal STRING : 
            '"' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'"') )* '"' |
            "'" ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|"'") )* "'"
        ; 
terminal SL_COMMENT     : '//' !('\n'|'\r')* ('\r'? '\n')?;

terminal WS         : (' '|'\t'|'\r'|'\n')+;

terminal ANY_OTHER: .;