Home » Modeling » TMF (Xtext) » Multiple EObjects from a single rule.
Multiple EObjects from a single rule. [message #903157] |
Wed, 22 August 2012 09:39 |
Alexis Marechal Messages: 16 Registered: July 2009 |
Junior Member |
|
|
Hello,
I'm creating a textual syntax with XText from an existing metamodel that I cannot modify. In this metamodel I have some classes looking a bit like this (using the EMFatic syntax):
Class Module {
val Type[*] types;
val VariableDeclaration[*] variables;
val Element[*] elements;
val Connection[*] connexions;
}
Class Type {
attr String[1] name;
}
Class VariableDeclaration {
attr String[1] name;
ref Type[1] type;
}
Class Element {
attr String[1] name;
}
Class Connection {
ref Element[1] from;
ref Element[1] to;
}
Now I would like to make a textual language for this metamodel following the following pattern:
Module:
Types type1, type2;
Variables
x, y : type1;
z : type2;
Elements
element1 connectsTo element2, element3;
element2 connectsTo element3;
element3;
Thus, I would like a rule that parses the line "x, y : type1;" and creates two distinct variables, each with the type "type1" assigned. Similarly, the line "element1 connectsTo element2, element3;" should generate three EObjects, one element and two connections.
Is all this even possible with XText without touching the underlying metamodel?
Thank you very much for any advise!
Alexis
|
|
|
Re: Multiple EObjects from a single rule. [message #903293 is a reply to message #903157] |
Wed, 22 August 2012 22:24 |
Alexis Marechal Messages: 16 Registered: July 2009 |
Junior Member |
|
|
I tried to mess up with the parser, but it did not work (and it seemed a very dirty solution). Then I tried to modify the underlying semantic model (as seen in Jan Köhnlein's blog), but still nothing:
First, I created these rules in my XText source:
Module returns Module:
'Types' '{'
(types+=Type)+
'}'
'Variables' '{'
((variables+=UnSortedVariableDeclaration ',')* variables+=SortedVariableDeclaration ';')+
'}';
Type returns Type:
name=ID;
SortedVariableDeclaration returns VariableDeclaration:
name=ID ':' type=[Type];
UnSortedVariableDeclaration returns VariableDeclaration:
name=ID;
Then I created my own implementation of XTextEditor so that, at each file save, I look for variables with a null type, and I manually add the correct type.
Unfortunately the result is not what I'm looking for: it does modify the semantic model, but at the same time it modifies the textual representation in the XText editor, and produces code that is not recognized by the editor. Is there a way to manually modify my semantic model without touching the textual representation? Or maybe there is a better way to achieve what I'm looking for?
Thanks for any help!
|
|
| |
Re: Multiple EObjects from a single rule. [message #903380 is a reply to message #903294] |
Thu, 23 August 2012 12:39 |
Alexis Marechal Messages: 16 Registered: July 2009 |
Junior Member |
|
|
Thanks for your answer Henrik.
The central part of my work is the meta-model itself. It is meant to serve as a platform to define the semantics of other languages by transformation, e.g., with ATL. Instances of my meta-model are then translated to a programming language for execution. I want to provide also a textual language for those clients who don't use Eclipse in their own tools. Moreover, I want people to be able to interact directly with the language, mainly for "proof of concept" purposes. The latter is why I do not want to have a specific meta-model for the xtext editor language nor modify the existing meta-model: I want to show specifically my meta-model through the editor.
You proposed me a solution using serialization, but losing the comments and indentation is too harsh, I still prefer to have an ugly language.
You also talked about an underlying model, the "original" one. That would work if:
1) Both the "original" and "textual" models are instances of the same meta-model.
2) This "original" model is completely invisible for the users, no editor is offered for this model and there is no way to modify it directly.
3) All checks are performed in the "original" model, thus the "textual" model does not show any error that is solved in the "original" one, e.g., untyped variables in my example.
Is it possible to obtain this? I am able to work with an ugly language, what I'm looking for here is simply some syntactic sugar.
Thanks for any further help .
|
|
|
Re: Multiple EObjects from a single rule. [message #903496 is a reply to message #903380] |
Thu, 23 August 2012 23:25 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
Not sure if it is clear, but with Xtext the model instance is expressed
in textual form. It is not saved to XMI unless you explicitly do so (and
then you loose comments, whitespace, formatting, etc.).
You can add it to a resource (i.e. the URL to the source text), have
other models link to the parsed result etc.
So, depending on what you want as the result; an XMI file, or the
ability for others to open/link use the model the solutions are different.
So, when you say
> I want to show specifically
> my meta-model through the editor.
This can mean several different things.
A simple way if you want linking is to use different extensions. Say
that the textual representation is ".mydsl". If someone wants the
textual model in "foo.mydsl", they would simply load this resource - if
you instead load say "foo.xxx" where ".xxx" is your unified/existing
language model, you could have a loader that checks if there is a
"foo.mydsl", load that and transform it on the fly.
If you then in your "foo.mydsl" resource remember this transformation,
you can keep it updated as your .mydsl resource is changed.
....sprinkle with a cache for taste :)
If you are instead going the other way; you have instances of your model
and want to view them as text, you need to transform, and then serialize
the model (supported by Xtext), now you have a real model instance (the
XMI you loaded) to keep in sync if you allow editing in textual form (or
make editor read-only).
If you want to export an XMI copy of a .mydsl, just add such a command
(export as... or something).
Hope that helps.
- henrik
On 2012-23-08 14:39, Alexis Marechal wrote:
> Thanks for your answer Henrik.
>
> The central part of my work is the meta-model itself. It is meant to
> serve as a platform to define the semantics of other languages by
> transformation, e.g., with ATL. Instances of my meta-model are then
> translated to a programming language for execution. I want to provide
> also a textual language for those clients who don't use Eclipse in their
> own tools. Moreover, I want people to be able to interact directly with
> the language, mainly for "proof of concept" purposes. The latter is why
> I do not want to have a specific meta-model for the xtext editor
> language nor modify the existing meta-model: I want to show specifically
> my meta-model through the editor.
>
> You proposed me a solution using serialization, but losing the comments
> and indentation is too harsh, I still prefer to have an ugly language.
>
> You also talked about an underlying model, the "original" one. That
> would work if:
> 1) Both the "original" and "textual" models are instances of the same
> meta-model.
> 2) This "original" model is completely invisible for the users, no
> editor is offered for this model and there is no way to modify it directly.
> 3) All checks are performed in the "original" model, thus the "textual"
> model does not show any error that is solved in the "original" one,
> e.g., untyped variables in my example.
>
> Is it possible to obtain this? I am able to work with an ugly language,
> what I'm looking for here is simply some syntactic sugar.
>
> Thanks for any further help :).
|
|
| |
Re: Multiple EObjects from a single rule. [message #903588 is a reply to message #903498] |
Fri, 24 August 2012 12:48 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
On 2012-24-08 1:45, Alexis Marechal wrote:
> Ok, it means it is not possible to do what I wanted to do, i.e., tweak
> the model inference from the textual representation, since apparently
> the textual representation IS the model itself. Your solutions imply an
> existence of two separate files,
Not really, I am saying that a model instance can be "virtual" and
appear in memory on demand. Model instance existence is not the same as
a physical manifestation/serialization of it in some format. It all
depends on how you want to use it.
> which I do not want. Anyway, I'll look
> for some workaround, or stick to a verbose dsl, no big deal :).
>
> Thank you very much!
np.
- henrik
|
|
|
Goto Forum:
Current Time: Mon Sep 23 13:49:01 GMT 2024
Powered by FUDForum. Page generated in 0.03517 seconds
|