Xtext grammar for custom hash comment [message #1733847] |
Wed, 01 June 2016 13:08  |
Eclipse User |
|
|
|
In my language '##' is concatenation operator and also have hash comment which should not preceded or followed by anything except spaces.
But when a hash comment starts with two hashes ## it is taking it as concatenation operator specified in
Concatenation returns Expression:
Primary ({Concatenation.left=current} '##' right=Primary)*
;
and throwing error not showing as comment.
I want something like this
##This is a comment
a = "String1" ## "String2" -------------> this should be concatenation
I have tried something like this :
Model: {Model} NL* statements+=Statement*;
Statement: (OpenAssignment|HASH_COMMENT) NL*;
OpenAssignment: 'math' exp+=Expression(','? exp+=Expression*);
Expression:
LogicalOr
;
LogicalOr returns Expression:
BitwiseOr ({LogicalOr.left=current} '||' right=BitwiseOr)*
;
BitwiseOr returns Expression:
LogicalAnd ({BitwiseOr.left=current} '|' right=LogicalAnd)*
;
LogicalAnd returns Expression:
BitwiseAnd ({LogicalAnd.left=current} '&&' right=BitwiseAnd)*
;
BitwiseAnd returns Expression:
Equivalency ({BitwiseAnd.left=current} '&' right=Equivalency)*
;
Equivalency returns Expression:
Relational ({Equivalency.left=current} ('=='|'!='|'=') right=Relational)*
;
Relational returns Expression:
PlusMinus ({Relational.left=current} ('>='|'<='|'>'|'<') right=PlusMinus)*
;
PlusMinus returns Expression:
DivMult ({PlusMinus.left=current} ('+'|'-') right=DivMult)*
;
DivMult returns Expression:
LogicalNot ({DivMult.left=current} ('/'|'*') right=LogicalNot)*
;
LogicalNot returns Expression:
OnesComplement ({LogicalNot.left=current} '!' right=OnesComplement)*
;
OnesComplement returns Expression:
Exponentiation ({OnesComplement.left=current} ('~') right=Exponentiation)*
;
Exponentiation returns Expression:
Concatenation ({Exponentialtion.left=current} '^' right=Concatenation)*
;
Concatenation returns Expression:
Primary ({Concatenation.left=current} CONCAT right=Primary)*
;
Primary returns Expression: {Expression}
jplId=ID | number=NUMBER | str=STRING
;
terminal ID : (':'('+')?)? ('a'..'z'|'A'..'Z'|'_'|'$'|'.'|'@')('a'..'z'|'A'..'Z'|'_'|'$'|'.'|'0'..'9'|'@')*;
terminal STRING :
'"' ( '\\'('"'|('\r'? '\n')) | !('\n'|'"') )* '"' |
"'" ( '\\'("'"|('\r'? '\n')) | !('\n'|"'") )* "'";
terminal SL_COMMENT: '//' !('\n' | '\r')*;
terminal CONCAT : (('\\'('\r'? '\n')) | !('\n'|'\r')) '##';
terminal HASH_COMMENT : ('#'('\r'? '\n')?) | ('#' !('#'|'\n'|'\r')+ ('\r'? '\n')?) | (('#''#''#') !('\n'|'\r')* ('\r'? '\n')?);
terminal WS : (' '|'\t'| ('\\'(' '* '\r'? '\n')))+;
terminal NL : ('\r'? '\n')+;
terminal NUMBER: ('-'|'+')? (OCTAL|FLOAT|INT|BINARY|HEX);
terminal OCTAL returns ecore::EInt: ('0') ('0'..'7')+;
terminal FLOAT returns ecore::EFloat: ('0'..'9' ('0'..'9')*) '.' ('0'..'9')*;
terminal INT returns ecore::EInt: '0'..'9' ('0'..'9')*;
terminal RANGE: '0'..'9' '..' ('0'..'9')*;
terminal BINARY returns ecore::EInt: ('0b'|'0B') ('0'|'1')+;
terminal HEX returns ecore::EInt:('0x'|'0X')('0'..'9'|'a'..'f'|'A'..'F')+;
|
|
|
|
|
Re: Xtext grammar for custom hash comment [message #1734262 is a reply to message #1734261] |
Mon, 06 June 2016 15:46   |
Eclipse User |
|
|
|
Hi,
I tried it but it is failing in case if a line starts with double # like
##value.
Actual if double hash (##) comes in between two variables then it should work like concatenation operator else if it comes at the starting of the line then it should be comment. I write terminal CONCAT and HASH_COMMENT seperately but I didn't success. I tried in all ways but that I understand but no luck.
|
|
|
|
|
|
Re: Xtext grammar for custom hash comment [message #1734266 is a reply to message #1734265] |
Mon, 06 June 2016 15:57   |
Eclipse User |
|
|
|
It is similar to c language code. The minimal grammar I can share with you is this:
Model: {Model} NL* statements+=Statement*;
Statement: (NonRecursingStatement) ';'? NL+ | HASH_COMMENT NL*;
NonRecursingStatement returns Statement:
( Variables|DBMS )
;
Variables: name=('vars'|'global') ((exp+=Expression) (','? ('vars'|'global')? (exp+=Expression))*) ','?;
Expression:
LogicalOr
;
LogicalOr returns Expression:
BitwiseOr ({LogicalOr.left=current} '||' right=BitwiseOr)*
;
BitwiseOr returns Expression:
LogicalAnd ({BitwiseOr.left=current} '|' right=LogicalAnd)*
;
LogicalAnd returns Expression:
BitwiseAnd ({LogicalAnd.left=current} '&&' right=BitwiseAnd)*
;
BitwiseAnd returns Expression:
Equivalency ({BitwiseAnd.left=current} '&' right=Equivalency)*
;
Equivalency returns Expression:
Relational ({Equivalency.left=current} ('=='|'!='|'=') right=Relational)*
;
Relational returns Expression:
PlusMinus ({Relational.left=current} ('>='|'<='|'>'|'<') right=PlusMinus)*
;
PlusMinus returns Expression:
DivMult ({PlusMinus.left=current} ('+'|'-') right=DivMult)*
;
DivMult returns Expression:
LogicalNot ({DivMult.left=current} ('/'|'*') right=LogicalNot)*
;
LogicalNot returns Expression:
OnesComplement ({LogicalNot.left=current} '!' right=OnesComplement)*
;
OnesComplement returns Expression:
Exponentiation ({OnesComplement.left=current} ('~') right=Exponentiation)*
;
Exponentiation returns Expression:
Concatenation ({Exponentialtion.left=current} '^' right=Concatenation)*
;
Concatenation returns Expression:
Primary ({Concatenation.left=current} CONCAT right=Primary)*
;
Primary returns Expression: {Expression}
JPLID=ID | NUMBER=NUMBER | STRING=STRING | RANGE=RANGE
;
terminal DBMS: ('DBMS'|'Dbms'|'dbms') (('\\'((' '|'\t')* '\r'? '\n')) | !('\n' | '\r'))*;
terminal ID : (':'('+')?)? ('a'..'z'|'A'..'Z'|'_'|'$'|'.'|'@')('a'..'z'|'A'..'Z'|'_'|'$'|'.'|'0'..'9'|'@')*;
terminal STRING : '"' ( '\\'('"'|('\r'? '\n')) | !('\n'|'"') )* '"' |
"'" ( '\\'("'"|('\r'? '\n')) | !('\n'|"'") )* "'";
terminal SL_COMMENT: '//' !('\n' | '\r')*;
terminal CONCAT : (('\\'('\r'? '\n')) | !('\n'|'\r')) '##';
terminal HASH_COMMENT : (('#') !('\n'|'\r')* ('\r'? '\n')?);
terminal WS : (' '|'\t'| ('\\'((' '|'\t')* '\r'? '\n')))+;
//terminal HASH_COMMENT : ('#'('\r'? '\n')?) | ('#' !('#'|'\n'|'\r')+ ('\r'? '\n')?) | (('#''#''#') !('\n'|'\r')* ('\r'? '\n')?);
terminal NL : ('\r'? '\n')+;
terminal NUMBER: ('-'|'+')? (OCTAL|FLOAT|INT|BINARY|HEX);
terminal OCTAL returns ecore::EInt: ('0') ('0'..'7')+;
terminal FLOAT returns ecore::EFloat: ('0'..'9' ('0'..'9')*) '.' ('0'..'9')*;
terminal INT returns ecore::EInt: '0'..'9' ('0'..'9')*;
terminal RANGE: '0'..'9' '..' ('0'..'9')*;
terminal BINARY returns ecore::EInt: ('0b'|'0B') ('0'|'1')+;
terminal HEX returns ecore::EInt:('0x'|'0X')('0'..'9'|'a'..'f'|'A'..'F')+;
|
|
|
Re: Xtext grammar for custom hash comment [message #1734267 is a reply to message #1734266] |
Mon, 06 June 2016 16:09   |
Eclipse User |
|
|
|
this is still 1000km from beeing minimal ((((((
this makes me fight 10000 times before i get in mood tryting to solve
your strange requirements.
btw how is the strange grammar parsed before using xtext.
i have doubts this is solvable without having some magic handcrafted custom lexer
i have no idea sry
|
|
|
|
Powered by
FUDForum. Page generated in 0.06052 seconds