|Integrate ocl in xtext grammar or grab all the characters until any language rule appears [message #1841343]
||Wed, 12 May 2021 16:29
| Julia Robles
Registered: April 2021
I am creating the grammar of an existing language in xtext to be able to generate an XMI. I have managed to do it all but there is a part that I don't know how to describe in xtext.
The language contains OCL expressions, but I don't need to validate it. It is enough for me to take all the text that represents the expression until another rule of my language appears.
I will explain it better with an example.
The language looks like the following:
att1 : Integer
att2 : Integer
list : Sequence(A)
operationname1() = self.att1 + self.att2 < 3
pre namepre: self.att1 <= self.att2
post namepost: self.list-> notEmpty()
I have represented the operations part as follows:
OperationComplex | OperationQuery;
operationDeclaration=OperationDeclaration '=' operationbody=OCLExpression (conditions+=ConditionType)*;
Precondition | Postcondition;
'pre' (name=ID)? ':' oclexpression=OCLExpression;
'post' (name=ID)? ':' oclexpression=OCLExpression;
I have tested it using STRING instead of OCLExpression (and putting single quotes at the beginning and end of ocl expressions manually) and everything works correctly, my problem is that I don't know how to do the OCLExpression rule.
I have also thought that I could add an OCL library and use the call to that library, but I can't find much information on how to integrate OCL into an xtext grammar. I've been looking at this documentation (https://download.eclipse.org/ocl/doc/6.14.0/ocl.pdf) but I don't know if it has any application to what I want.
Thanks in advance!
|Re: Integrate ocl in xtext grammar or grab all the characters until any language rule appears [message #1841955 is a reply to message #1841936]
||Fri, 04 June 2021 09:10
| Ed Willink
Registered: July 2009
Congratulations on coming up with a lightish weight solution that works for you. (I'm not sure why you don't need syntax checking / validation ...)
Your backtracking issue might be resolved by emulating the RetokenizingEssentialOCLParser that installs a RetokenizingTokenSource
* RetokenizingTokenSource accomodates backtracking limitations in the ANTRL lexer when used from Xtext.
* The problem is the three overlapping syntaxes
* INT.INT leading to a Floating Point lteral
* INT..INT leading to a Collection range
* INT.ID leading to a numeric navigation
* ANTLR proceeds to INT. but won't backup when the character after the . is bad.
* The code here allows the basic lexer to be ignorant of floating point syntax so that it correctly parses
* INT.INT as INT DOT INT
* INT..INT as INT DOTDOT INT
* INT.INTe+INT as INT DOT INT ID PLUS INT
* so the code here recognises the floating point literal and reconsttructs. As an additional benefit 'e' and 'E' are not keywords.
Powered by FUDForum
. Page generated in 0.02180 seconds