Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Grammar reuse
Grammar reuse [message #54277] Tue, 30 June 2009 21:18 Go to next message
Alexis Marechal is currently offline Alexis MarechalFriend
Messages: 16
Registered: July 2009
Junior Member
Hi,

I'm trying to create a DSL using TMF and I encountered a problem that I
couldn't solve, despite a lot of search (including here in the newsgroup).
The problem concerns grammar reuse, I will try to explain it with a
simplified example.

Let's say I create a grammar A:

---
grammar dsl.ProjectA with org.eclipse.xtext.common.Terminals

generate projectA "http://www.ProjectA.dsl"

Model:
(terms += Term)*;

Term:
"big structure goes here" {Term};
---

And I use that grammar to create a grammar B:

---
grammar dsl.ProjectB with dsl.ProjectA

generate projectB "http://www.ProjectB.dsl"


Model:
(equations+=Equation)*;

Equation:
left=Term "=" right=Term;
---

The ProjectB imports (and reexports) the ProjectA, and there are no
visible errors. I still don't get errors during the code generation for
the ProjectB, but when I try to open the generated metamodel, I get an
UnknownHostException. Apparently, the ProjectB.ecore makes a reference to
"http://www.ProjectA.dsl#//Term", which it cannot resolve.

After reading some posts, I modified the GenerateProjectB.mwe file in
order to register the ProjectA Package, and to use it's genmodel:

---
<workflow>

...

<bean class="org.eclipse.emf.mwe.utils.StandaloneSetup"
platformUri="${runtimeProject}/..">
<registerGeneratedEPackage value="dsl.projectA.ProjectAPackage"/>
</bean>

...

<component class="org.eclipse.xtext.generator.Generator">

...

<language uri="${grammarURI}" fileExtensions="${file.extensions}">
<fragment
class=" org.eclipse.xtext.generator.grammarAccess.GrammarAccessFragm ent "/>

<fragment
class="org.eclipse.xtext.generator.ecore.EcoreGeneratorFragment ">
<genModels
value="platform:/resource/ProjectA/src-gen/dsl/ProjectA.genmodel "/>
</fragment>

...

</language>
</component>
</workflow>
---

The generation still doesn't show any error, but the metamodel is still
wrong.

I also tried to import the ProjectA metamodel in the ProjectB grammar
(using import "platform:/resource/ProjectA/src-gen/dsl/ProjectA.ecore" as
projectAmm). With this I was able to create a rule like the following one:

---
Equation:
left=[projectAmm::Term] "=" right=[projectAmm::Term];
---

With this I generated a correct ProjectB.ecore, and even a correct editor,
but I don't want references, I really want to parse new terms while
creating my equations. And a rule:

---
Equation:
left=projectAmm::Term "=" right=projectAmm::Term;
---

isn't accepted.

Where did I go wrong?

Sorry for the long post, I really don't have any idea, and I hope someone
will be able to help me. Thanks in advance!

Regards,

Alexis Marechal
Re: Grammar reuse [message #54566 is a reply to message #54277] Wed, 01 July 2009 20:37 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Hi,

Alexis Marechal schrieb:
> The ProjectB imports (and reexports) the ProjectA, and there are no
> visible errors. I still don't get errors during the code generation for
> the ProjectB, but when I try to open the generated metamodel, I get an
> UnknownHostException. Apparently, the ProjectB.ecore makes a reference
> to "http://www.ProjectA.dsl#//Term", which it cannot resolve.

That's ok. The generated ecore file references the derived EPackage from
the grammar by its nsURI. If you want to open it, you'll have to make
the ecore model available by its nsURI.

> <bean class="org.eclipse.emf.mwe.utils.StandaloneSetup"
> platformUri="${runtimeProject}/..">
> <registerGeneratedEPackage value="dsl.projectA.ProjectAPackage"/>
> </bean>

This is not needed and should be removed.

> ..
>
> <component class="org.eclipse.xtext.generator.Generator">
>
> ..
>
> <language uri="${grammarURI}" fileExtensions="${file.extensions}">
> <fragment
> class=" org.eclipse.xtext.generator.grammarAccess.GrammarAccessFragm ent "/>
>
> <fragment
> class="org.eclipse.xtext.generator.ecore.EcoreGeneratorFragment ">
> <genModels
> value="platform:/resource/ProjectA/src-gen/dsl/ProjectA.genmodel "/>
> </fragment>

This is correct.
Adding the genmodel is important!

>
> The generation still doesn't show any error, but the metamodel is still
> wrong.

It's not wrong. It is just not openable without the needed context
:-)

>
> I also tried to import the ProjectA metamodel in the ProjectB grammar
> (using import "platform:/resource/ProjectA/src-gen/dsl/ProjectA.ecore"
> as projectAmm). With this I was able to create a rule like the following
> one:

If you want to refer to types from the super grammar you need to import
the metamodel by its nsURI. But you don't reference any types, just
rules. It's sometimes hard to distinguish, especially because they
typically share rthe same name.

>
> ---
> Equation:
> left=[projectAmm::Term] "=" right=[projectAmm::Term];
> ---

Cross references is not what you wanted.

>
> With this I generated a correct ProjectB.ecore, and even a correct
> editor, but I don't want references, I really want to parse new terms
> while creating my equations. And a rule:
>
> ---
> Equation:
> left=projectAmm::Term "=" right=projectAmm::Term;
> ---

You try to refer to types, but types are only referenced from within
cross references (foo=[MyType]) while common assignments, i.e.
containment references point to rules (foo=MyRule).

So left=Term is correct.


>
> isn't accepted.
>
> Where did I go wrong?
>

I'm not sure what the problem is. The generated ecore files can't be
opened with an editor.

Sven
Re: Grammar reuse [message #54701 is a reply to message #54566] Thu, 02 July 2009 11:01 Go to previous messageGo to next message
Alexis Marechal is currently offline Alexis MarechalFriend
Messages: 16
Registered: July 2009
Junior Member
Hi,

Thank you very much for your answer, I really appreciate that you took
some time to check my problem :).

Everything you said is clear for me, exept this line:

"you'll have to make the ecore model available by its nsURI."

How can I do that? I tried some solutions that I found in the TMF or EMF
documentations, but nothing worked.

Regards,

Alexis
Re: Grammar reuse [message #55139 is a reply to message #54701] Fri, 03 July 2009 08:11 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Alexis Marechal schrieb:
> Hi,
>
> Thank you very much for your answer, I really appreciate that you took
> some time to check my problem :).
>
> Everything you said is clear for me, exept this line:
>
> "you'll have to make the ecore model available by its nsURI."
>
> How can I do that? I tried some solutions that I found in the TMF or EMF
> documentations, but nothing worked.

Generally any ecore file, which references another ecore by it's nsURI
is openable using the standard editor, if you have that ecore model
installed in the workbench.
There are also other ways to tell EMF how to find a resource for an
nsURI (e.g. URI maps).

Why do you want to open it?

Sven
Re: Grammar reuse [message #55764 is a reply to message #55139] Sat, 04 July 2009 08:27 Go to previous messageGo to next message
Alexis Marechal is currently offline Alexis MarechalFriend
Messages: 16
Registered: July 2009
Junior Member
I had another problem that was messing with mi generated editor, and I
thought that it was a problem with the metamodel. But now everything's
working fine!

Thanks a lot :)
Re: Grammar reuse [message #56999 is a reply to message #54566] Fri, 10 July 2009 09:43 Go to previous messageGo to next message
Knut Wannheden is currently offline Knut WannhedenFriend
Messages: 298
Registered: July 2009
Senior Member
Hi Sven,

It's only when a grammar inherits from another grammar in the workspace
the generated Ecore or GenModel files cannot be opened in the editor.

If other models are imported using the recommended platform:/resource
URIs, then the Ecore and GenModel can be opened without problems. This
is because the references in the Ecore file use the platform URI. E.g.

<eStructuralFeatures xsi:type="ecore:EReference" name="ref"
eType="ecore:EClass
platform:/resource/org.xtext.example.mydsl/src-gen/org/xtext /example/MyDsl.ecore#//Type "/>

It would be nice if the generated Ecore and GenModel files could also be
opened when using grammar mixins. This is useful when you want to work
out a transformation to apply in the post processor.

Cheers,

--knut

Sven Efftinge wrote:
> Hi,
>
> Alexis Marechal schrieb:
>> The ProjectB imports (and reexports) the ProjectA, and there are no
>> visible errors. I still don't get errors during the code generation
>> for the ProjectB, but when I try to open the generated metamodel, I
>> get an UnknownHostException. Apparently, the ProjectB.ecore makes a
>> reference to "http://www.ProjectA.dsl#//Term", which it cannot resolve.
>
> That's ok. The generated ecore file references the derived EPackage from
> the grammar by its nsURI. If you want to open it, you'll have to make
> the ecore model available by its nsURI.
>
>> <bean class="org.eclipse.emf.mwe.utils.StandaloneSetup"
>> platformUri="${runtimeProject}/..">
>> <registerGeneratedEPackage value="dsl.projectA.ProjectAPackage"/>
>> </bean>
>
> This is not needed and should be removed.
>
>> ..
>>
>> <component class="org.eclipse.xtext.generator.Generator">
>>
>> ..
>>
>> <language uri="${grammarURI}" fileExtensions="${file.extensions}">
>> <fragment
>> class=" org.eclipse.xtext.generator.grammarAccess.GrammarAccessFragm ent "/>
>> <fragment
>> class="org.eclipse.xtext.generator.ecore.EcoreGeneratorFragment ">
>> <genModels
>> value="platform:/resource/ProjectA/src-gen/dsl/ProjectA.genmodel "/>
>> </fragment>
>
> This is correct.
> Adding the genmodel is important!
>
>>
>> The generation still doesn't show any error, but the metamodel is
>> still wrong.
>
> It's not wrong. It is just not openable without the needed context
> :-)
>
>>
>> I also tried to import the ProjectA metamodel in the ProjectB grammar
>> (using import "platform:/resource/ProjectA/src-gen/dsl/ProjectA.ecore"
>> as projectAmm). With this I was able to create a rule like the
>> following one:
>
> If you want to refer to types from the super grammar you need to import
> the metamodel by its nsURI. But you don't reference any types, just
> rules. It's sometimes hard to distinguish, especially because they
> typically share rthe same name.
>
>>
>> ---
>> Equation:
>> left=[projectAmm::Term] "=" right=[projectAmm::Term];
>> ---
>
> Cross references is not what you wanted.
>
>>
>> With this I generated a correct ProjectB.ecore, and even a correct
>> editor, but I don't want references, I really want to parse new terms
>> while creating my equations. And a rule:
>>
>> ---
>> Equation:
>> left=projectAmm::Term "=" right=projectAmm::Term;
>> ---
>
> You try to refer to types, but types are only referenced from within
> cross references (foo=[MyType]) while common assignments, i.e.
> containment references point to rules (foo=MyRule).
>
> So left=Term is correct.
>
>
>>
>> isn't accepted.
>>
>> Where did I go wrong?
>>
>
> I'm not sure what the problem is. The generated ecore files can't be
> opened with an editor.
>
> Sven
Re: Grammar reuse [message #57284 is a reply to message #56999] Mon, 13 July 2009 07:05 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
Hi Knut,

I agree.
Please open a bugzilla for that.

Cheers,
Sven


Knut Wannheden schrieb:
> Hi Sven,
>
> It's only when a grammar inherits from another grammar in the workspace
> the generated Ecore or GenModel files cannot be opened in the editor.
>
> If other models are imported using the recommended platform:/resource
> URIs, then the Ecore and GenModel can be opened without problems. This
> is because the references in the Ecore file use the platform URI. E.g.
>
> <eStructuralFeatures xsi:type="ecore:EReference" name="ref"
> eType="ecore:EClass
> platform:/resource/org.xtext.example.mydsl/src-gen/org/xtext /example/MyDsl.ecore#//Type "/>
>
>
> It would be nice if the generated Ecore and GenModel files could also be
> opened when using grammar mixins. This is useful when you want to work
> out a transformation to apply in the post processor.
>
> Cheers,
>
> --knut
>
> Sven Efftinge wrote:
>> Hi,
>>
>> Alexis Marechal schrieb:
>>> The ProjectB imports (and reexports) the ProjectA, and there are no
>>> visible errors. I still don't get errors during the code generation
>>> for the ProjectB, but when I try to open the generated metamodel, I
>>> get an UnknownHostException. Apparently, the ProjectB.ecore makes a
>>> reference to "http://www.ProjectA.dsl#//Term", which it cannot resolve.
>>
>> That's ok. The generated ecore file references the derived EPackage
>> from the grammar by its nsURI. If you want to open it, you'll have to
>> make the ecore model available by its nsURI.
>>
>>> <bean class="org.eclipse.emf.mwe.utils.StandaloneSetup"
>>> platformUri="${runtimeProject}/..">
>>> <registerGeneratedEPackage value="dsl.projectA.ProjectAPackage"/>
>>> </bean>
>>
>> This is not needed and should be removed.
>>
>>> ..
>>>
>>> <component class="org.eclipse.xtext.generator.Generator">
>>>
>>> ..
>>>
>>> <language uri="${grammarURI}" fileExtensions="${file.extensions}">
>>> <fragment
>>> class=" org.eclipse.xtext.generator.grammarAccess.GrammarAccessFragm ent "/>
>>>
>>> <fragment
>>> class="org.eclipse.xtext.generator.ecore.EcoreGeneratorFragment ">
>>> <genModels
>>> value="platform:/resource/ProjectA/src-gen/dsl/ProjectA.genmodel "/>
>>> </fragment>
>>
>> This is correct.
>> Adding the genmodel is important!
>>
>>>
>>> The generation still doesn't show any error, but the metamodel is
>>> still wrong.
>>
>> It's not wrong. It is just not openable without the needed context
>> :-)
>>
>>>
>>> I also tried to import the ProjectA metamodel in the ProjectB grammar
>>> (using import
>>> "platform:/resource/ProjectA/src-gen/dsl/ProjectA.ecore" as
>>> projectAmm). With this I was able to create a rule like the following
>>> one:
>>
>> If you want to refer to types from the super grammar you need to
>> import the metamodel by its nsURI. But you don't reference any types,
>> just rules. It's sometimes hard to distinguish, especially because
>> they typically share rthe same name.
>>
>>>
>>> ---
>>> Equation:
>>> left=[projectAmm::Term] "=" right=[projectAmm::Term];
>>> ---
>>
>> Cross references is not what you wanted.
>>
>>>
>>> With this I generated a correct ProjectB.ecore, and even a correct
>>> editor, but I don't want references, I really want to parse new terms
>>> while creating my equations. And a rule:
>>>
>>> ---
>>> Equation:
>>> left=projectAmm::Term "=" right=projectAmm::Term;
>>> ---
>>
>> You try to refer to types, but types are only referenced from within
>> cross references (foo=[MyType]) while common assignments, i.e.
>> containment references point to rules (foo=MyRule).
>>
>> So left=Term is correct.
>>
>>
>>>
>>> isn't accepted.
>>>
>>> Where did I go wrong?
>>>
>>
>> I'm not sure what the problem is. The generated ecore files can't be
>> opened with an editor.
>>
>> Sven
Re: Grammar reuse [message #57385 is a reply to message #57284] Mon, 13 July 2009 07:48 Go to previous message
Knut Wannheden is currently offline Knut WannhedenFriend
Messages: 298
Registered: July 2009
Senior Member
Hi Sven,

Reported as https://bugs.eclipse.org/bugs/show_bug.cgi?id=283278.

Cheers,

--knut

Sven Efftinge wrote:
> I agree.
> Please open a bugzilla for that.
>
Previous Topic:[Xtext] A very generic terminal definition
Next Topic:[Xtext] Parsing terminals as ecore::EDouble
Goto Forum:
  


Current Time: Sun Dec 22 06:16:39 GMT 2024

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

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

Back to the top