Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext grammar for custom hash comment(Xtext grammar for custom hash comment)
Xtext grammar for custom hash comment [message #1733847] Wed, 01 June 2016 13:08 Go to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
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 #1734260 is a reply to message #1733847] Mon, 06 June 2016 15:29 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Hi,

Anyone any idea about this?
Re: Xtext grammar for custom hash comment [message #1734261 is a reply to message #1734260] Mon, 06 June 2016 15:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
Same answer as to https://www.eclipse.org/forums/index.php/t/1078111/

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Xtext grammar for custom hash comment [message #1734262 is a reply to message #1734261] Mon, 06 June 2016 15:46 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
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 #1734263 is a reply to message #1734262] Mon, 06 June 2016 15:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
Please share a minimal grammar to work on

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Xtext grammar for custom hash comment [message #1734264 is a reply to message #1734262] Mon, 06 June 2016 15:51 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
At present I am using

terminal CONCAT : (('\\'('\r'? '\n')) | !('\n'|'\r')) '##';

terminal HASH_COMMENT : (('#') !('\n'|'\r')* ('\r'? '\n')?);
Re: Xtext grammar for custom hash comment [message #1734265 is a reply to message #1734264] Mon, 06 June 2016 15:55 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
is your grammar line based?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Xtext grammar for custom hash comment [message #1734266 is a reply to message #1734265] Mon, 06 June 2016 15:57 Go to previous messageGo to next message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
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 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14729
Registered: July 2009
Senior Member
this is still 1000km from beeing minimal Sad((((((
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
Re: Xtext grammar for custom hash comment [message #1734269 is a reply to message #1734267] Mon, 06 June 2016 16:14 Go to previous message
Sachin Samaram is currently offline Sachin SamaramFriend
Messages: 271
Registered: April 2016
Senior Member
Nope. Thank you for your time
Previous Topic:xtext terminal rule
Next Topic:Unit Test for XAnnotation validation shows unresolved reference
Goto Forum:
  


Current Time: Sat Nov 09 02:05:50 GMT 2024

Powered by FUDForum. Page generated in 0.03705 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top