Home » Modeling » TMF (Xtext) » "hidden assignments" in parser rules
"hidden assignments" in parser rules [message #553231] |
Mon, 16 August 2010 17:20 |
Christian Schneider Messages: 23 Registered: July 2009 |
Junior Member |
|
|
Dear all,
recently, I realized that the Xtext editor accepts assignments of
concrete values to an attribute of the object the parser rule/rule call
is in charge of. The code generation is working properly, too.
I tested it with an ID, i.e. EString, attribute (the meta model is
imported) like:
CommentAnnotation returns Annotation:
name="comment" value = STRING;
KeyValueAnnotation returns Annotation:
name=ID value= STRING;
However, in the test environment the xtext validator of the concrete
editor complained about no viable input/assignment to the attribute
(here: during CommentAnnotation concerning the name attribute).
Hence, my question is:
What is the current state of that part of Xtext (or did I do something
wrong, maybe)?
cheers,
Christian
|
|
|
Re: "hidden assignments" in parser rules [message #553248 is a reply to message #553231] |
Mon, 16 August 2010 19:31 |
|
Hello Christian,
can you tell more about the context the rules are used:
i tried following:
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test"
nsURI="http://www.eclipse.org/test" nsPrefix="test">
<eClassifiers xsi:type="ecore:EClass" name="Model">
<eStructuralFeatures xsi:type="ecore:EReference" name="annotations" upperBound="-1"
eType="#//Annotation" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Annotation">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="value" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eClassifiers>
</ecore:EPackage>
grammar org.xtext.example.mydsl2.MyDsl2 with org.eclipse.xtext.common.Terminals
import "platform:/resource/test/model/test.ecore"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
Model:
annotations+=CommentAnnotation*
annotations+=KeyValueAnnotation*
;
CommentAnnotation returns Annotation:
name="comment" value = STRING;
KeyValueAnnotation returns Annotation:
name=ID value= STRING;
comment "a"
comment "b"
x "y"
and it works for me
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: "hidden assignments" in parser rules [message #553372 is a reply to message #553248] |
Tue, 17 August 2010 09:54 |
Christian Schneider Messages: 23 Registered: July 2009 |
Junior Member |
|
|
Hi,
I'm afraid I confused different things!
In this case
> CommentAnnotation returns Annotation:
> name="comment" value = STRING;
"comment" serves as a keyword and is written to name feature
if the rule matches, i.e. "comment" is present (I usually put keywords in single quotes).
What I actually want to achieve is to make the assignment without
mentioning the keyword in the text, like a semantic action in traditional parser generators.
To be in more detail:
In our models we want to declare different sorts of signals:
input, output, inputoutput, local
Our meta model contains a signal class with boolean flags isInput, isOutput.
Thus, I want to write the grammar like
Model:
.
.
'input' signals+=InputSignal (',' signals+=InputSignal)*
|'inputoutput' signals+=InputOutputSignal (',' signals+=InputOutputSignal)*
.
.
.
;
InputSignal returns Signal:
<hidden isInputs=true> name=ID;
InputOutputSignal returns Signal:
<hidden isInputs=true isOutput=true> name=ID;
Would this be possible?
Best,
Christian
On 8/16/10 9:31 PM, Christian Dietrich wrote:
> Hello Christian,
>
> can you tell more about the context the rules are used:
>
> i tried following:
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <ecore:EPackage xmi:version="2.0"
> xmlns:xmi="http://www.omg.org/XMI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test"
> nsURI="http://www.eclipse.org/test" nsPrefix="test">
> <eClassifiers xsi:type="ecore:EClass" name="Model">
> <eStructuralFeatures xsi:type="ecore:EReference" name="annotations"
> upperBound="-1"
> eType="#//Annotation" containment="true"/>
> </eClassifiers>
> <eClassifiers xsi:type="ecore:EClass" name="Annotation">
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> <eStructuralFeatures xsi:type="ecore:EAttribute" name="value"
> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
> </eClassifiers>
> </ecore:EPackage>
>
>
>
> grammar org.xtext.example.mydsl2.MyDsl2 with
> org.eclipse.xtext.common.Terminals
> import "platform:/resource/test/model/test.ecore"
> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
>
> Model:
> annotations+=CommentAnnotation*
> annotations+=KeyValueAnnotation*
> ;
>
> CommentAnnotation returns Annotation:
> name="comment" value = STRING;
>
> KeyValueAnnotation returns Annotation:
> name=ID value= STRING;
>
>
>
> comment "a"
> comment "b"
> x "y"
>
>
> and it works for me
>
> ~Christian
|
|
|
Re: "hidden assignments" in parser rules [message #553692 is a reply to message #553372] |
Wed, 18 August 2010 13:30 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
You can use something like
Rule : aBooleanAttribute =? 'keyword' ;
To set a boolean if the keyword is present.
- henrik
On 8/17/10 11:54 AM, Christian Schneider wrote:
> Hi,
>
> I'm afraid I confused different things!
>
> In this case
>
>> CommentAnnotation returns Annotation:
>> name="comment" value = STRING;
>
> "comment" serves as a keyword and is written to name feature
> if the rule matches, i.e. "comment" is present (I usually put keywords in single quotes).
>
> What I actually want to achieve is to make the assignment without
> mentioning the keyword in the text, like a semantic action in traditional parser generators.
>
> To be in more detail:
>
> In our models we want to declare different sorts of signals:
> input, output, inputoutput, local
>
> Our meta model contains a signal class with boolean flags isInput, isOutput.
>
> Thus, I want to write the grammar like
>
> Model:
> .
> .
> 'input' signals+=InputSignal (',' signals+=InputSignal)*
> |'inputoutput' signals+=InputOutputSignal (',' signals+=InputOutputSignal)*
> .
> .
> .
> ;
>
> InputSignal returns Signal:
> <hidden isInputs=true> name=ID;
>
> InputOutputSignal returns Signal:
> <hidden isInputs=true isOutput=true> name=ID;
>
>
> Would this be possible?
>
> Best,
> Christian
>
> On 8/16/10 9:31 PM, Christian Dietrich wrote:
>> Hello Christian,
>>
>> can you tell more about the context the rules are used:
>>
>> i tried following:
>>
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <ecore:EPackage xmi:version="2.0"
>> xmlns:xmi="http://www.omg.org/XMI"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test"
>> nsURI="http://www.eclipse.org/test" nsPrefix="test">
>> <eClassifiers xsi:type="ecore:EClass" name="Model">
>> <eStructuralFeatures xsi:type="ecore:EReference" name="annotations"
>> upperBound="-1"
>> eType="#//Annotation" containment="true"/>
>> </eClassifiers>
>> <eClassifiers xsi:type="ecore:EClass" name="Annotation">
>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
>> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="value"
>> eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
>> </eClassifiers>
>> </ecore:EPackage>
>>
>>
>>
>> grammar org.xtext.example.mydsl2.MyDsl2 with
>> org.eclipse.xtext.common.Terminals
>> import "platform:/resource/test/model/test.ecore"
>> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
>>
>> Model:
>> annotations+=CommentAnnotation*
>> annotations+=KeyValueAnnotation*
>> ;
>>
>> CommentAnnotation returns Annotation:
>> name="comment" value = STRING;
>>
>> KeyValueAnnotation returns Annotation:
>> name=ID value= STRING;
>>
>>
>>
>> comment "a"
>> comment "b"
>> x "y"
>>
>>
>> and it works for me
>>
>> ~Christian
>
|
|
|
Re: "hidden assignments" in parser rules [message #553946 is a reply to message #553692] |
Thu, 19 August 2010 13:39 |
Christian Schneider Messages: 23 Registered: July 2009 |
Junior Member |
|
|
Of course! :-)
But I want to set a (or more) boolean(s) just by calling a rule, WITHOUT
any keyword consumption!
cheers,
Christian
On 8/18/10 3:30 PM, Henrik Lindberg wrote:
> You can use something like
> Rule : aBooleanAttribute =? 'keyword' ;
>
> To set a boolean if the keyword is present.
>
> - henrik
>
> On 8/17/10 11:54 AM, Christian Schneider wrote:
>> Hi,
>>
>> I'm afraid I confused different things!
>>
>> In this case
>>
>>> CommentAnnotation returns Annotation:
>>> name="comment" value = STRING;
>>
>> "comment" serves as a keyword and is written to name feature
>> if the rule matches, i.e. "comment" is present (I usually put keywords
>> in single quotes).
>>
>> What I actually want to achieve is to make the assignment without
>> mentioning the keyword in the text, like a semantic action in
>> traditional parser generators.
>>
>> To be in more detail:
>>
>> In our models we want to declare different sorts of signals:
>> input, output, inputoutput, local
>>
>> Our meta model contains a signal class with boolean flags isInput,
>> isOutput.
>>
>> Thus, I want to write the grammar like
>>
>> Model:
>> .
>> .
>> 'input' signals+=InputSignal (',' signals+=InputSignal)*
>> |'inputoutput' signals+=InputOutputSignal (','
>> signals+=InputOutputSignal)*
>> .
>> .
>> .
>> ;
>>
>> InputSignal returns Signal:
>> <hidden isInputs=true> name=ID;
>>
>> InputOutputSignal returns Signal:
>> <hidden isInputs=true isOutput=true> name=ID;
>>
>>
>> Would this be possible?
>>
>> Best,
>> Christian
>>
>> On 8/16/10 9:31 PM, Christian Dietrich wrote:
>>> Hello Christian,
>>>
>>> can you tell more about the context the rules are used:
>>>
>>> i tried following:
>>>
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <ecore:EPackage xmi:version="2.0"
>>> xmlns:xmi="http://www.omg.org/XMI"
>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test"
>>> nsURI="http://www.eclipse.org/test" nsPrefix="test">
>>> <eClassifiers xsi:type="ecore:EClass" name="Model">
>>> <eStructuralFeatures xsi:type="ecore:EReference" name="annotations"
>>> upperBound="-1"
>>> eType="#//Annotation" containment="true"/>
>>> </eClassifiers>
>>> <eClassifiers xsi:type="ecore:EClass" name="Annotation">
>>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
>>> eType="ecore:EDataType
>>> http://www.eclipse.org/emf/2002/Ecore#//EString"/>
>>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="value"
>>> eType="ecore:EDataType
>>> http://www.eclipse.org/emf/2002/Ecore#//EString"/>
>>> </eClassifiers>
>>> </ecore:EPackage>
>>>
>>>
>>>
>>> grammar org.xtext.example.mydsl2.MyDsl2 with
>>> org.eclipse.xtext.common.Terminals
>>> import "platform:/resource/test/model/test.ecore"
>>> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
>>>
>>> Model:
>>> annotations+=CommentAnnotation*
>>> annotations+=KeyValueAnnotation*
>>> ;
>>>
>>> CommentAnnotation returns Annotation:
>>> name="comment" value = STRING;
>>>
>>> KeyValueAnnotation returns Annotation:
>>> name=ID value= STRING;
>>>
>>>
>>>
>>> comment "a"
>>> comment "b"
>>> x "y"
>>>
>>>
>>> and it works for me
>>>
>>> ~Christian
>>
>
|
|
| |
Re: "hidden assignments" in parser rules [message #554245 is a reply to message #553946] |
Fri, 20 August 2010 17:23 |
Henrik Lindberg Messages: 2509 Registered: July 2009 |
Senior Member |
|
|
As it is not supported to call arbitrary methods from the grammar the
options I come to think of are:
- assign to feature (boolean or other) and modify assignment to have
side effects (as suggested earlier)
- use subclasses that sets the values in question in their constructors
- change the factory that is used by the parser to create model instances
- post processing
Regards
- henrik
On 8/19/10 3:39 PM, Christian Schneider wrote:
> Of course! :-)
>
> But I want to set a (or more) boolean(s) just by calling a rule, WITHOUT
> any keyword consumption!
>
> cheers,
> Christian
>
> On 8/18/10 3:30 PM, Henrik Lindberg wrote:
>> You can use something like
>> Rule : aBooleanAttribute =? 'keyword' ;
>>
>> To set a boolean if the keyword is present.
>>
>> - henrik
>>
>> On 8/17/10 11:54 AM, Christian Schneider wrote:
>>> Hi,
>>>
>>> I'm afraid I confused different things!
>>>
>>> In this case
>>>
>>>> CommentAnnotation returns Annotation:
>>>> name="comment" value = STRING;
>>>
>>> "comment" serves as a keyword and is written to name feature
>>> if the rule matches, i.e. "comment" is present (I usually put keywords
>>> in single quotes).
>>>
>>> What I actually want to achieve is to make the assignment without
>>> mentioning the keyword in the text, like a semantic action in
>>> traditional parser generators.
>>>
>>> To be in more detail:
>>>
>>> In our models we want to declare different sorts of signals:
>>> input, output, inputoutput, local
>>>
>>> Our meta model contains a signal class with boolean flags isInput,
>>> isOutput.
>>>
>>> Thus, I want to write the grammar like
>>>
>>> Model:
>>> .
>>> .
>>> 'input' signals+=InputSignal (',' signals+=InputSignal)*
>>> |'inputoutput' signals+=InputOutputSignal (','
>>> signals+=InputOutputSignal)*
>>> .
>>> .
>>> .
>>> ;
>>>
>>> InputSignal returns Signal:
>>> <hidden isInputs=true> name=ID;
>>>
>>> InputOutputSignal returns Signal:
>>> <hidden isInputs=true isOutput=true> name=ID;
>>>
>>>
>>> Would this be possible?
>>>
>>> Best,
>>> Christian
>>>
>>> On 8/16/10 9:31 PM, Christian Dietrich wrote:
>>>> Hello Christian,
>>>>
>>>> can you tell more about the context the rules are used:
>>>>
>>>> i tried following:
>>>>
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <ecore:EPackage xmi:version="2.0"
>>>> xmlns:xmi="http://www.omg.org/XMI"
>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test"
>>>> nsURI="http://www.eclipse.org/test" nsPrefix="test">
>>>> <eClassifiers xsi:type="ecore:EClass" name="Model">
>>>> <eStructuralFeatures xsi:type="ecore:EReference" name="annotations"
>>>> upperBound="-1"
>>>> eType="#//Annotation" containment="true"/>
>>>> </eClassifiers>
>>>> <eClassifiers xsi:type="ecore:EClass" name="Annotation">
>>>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
>>>> eType="ecore:EDataType
>>>> http://www.eclipse.org/emf/2002/Ecore#//EString"/>
>>>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="value"
>>>> eType="ecore:EDataType
>>>> http://www.eclipse.org/emf/2002/Ecore#//EString"/>
>>>> </eClassifiers>
>>>> </ecore:EPackage>
>>>>
>>>>
>>>>
>>>> grammar org.xtext.example.mydsl2.MyDsl2 with
>>>> org.eclipse.xtext.common.Terminals
>>>> import "platform:/resource/test/model/test.ecore"
>>>> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
>>>>
>>>> Model:
>>>> annotations+=CommentAnnotation*
>>>> annotations+=KeyValueAnnotation*
>>>> ;
>>>>
>>>> CommentAnnotation returns Annotation:
>>>> name="comment" value = STRING;
>>>>
>>>> KeyValueAnnotation returns Annotation:
>>>> name=ID value= STRING;
>>>>
>>>>
>>>>
>>>> comment "a"
>>>> comment "b"
>>>> x "y"
>>>>
>>>>
>>>> and it works for me
>>>>
>>>> ~Christian
>>>
>>
>
|
|
|
Re: "hidden assignments" in parser rules [message #554601 is a reply to message #554245] |
Mon, 23 August 2010 16:25 |
Christian Schneider Messages: 23 Registered: July 2009 |
Junior Member |
|
|
Hi,
On 8/20/10 7:23 PM, Henrik Lindberg wrote:
> As it is not supported to call arbitrary methods from the grammar the
> options I come to think of are:
> - assign to feature (boolean or other) and modify assignment to have
> side effects (as suggested earlier)
> - use subclasses that sets the values in question in their constructors
That might be a reasonable option on the parsing side.
However, I'm not convinced whether this will work on the serializing
side if the model was constructed outside of Xtext, e.g. programmatically.
For instance: parsing of
input A,B,C
output O
inputoutput P,Q
local L
seems to be easy while serializing such a string seems to be hard to me
if the source is just a list of objects of type 'signal' (with different
boolean attribute settings).
Finally, my question to the Xtext developer is:
Is support of such functionalities on your road map
and, if so, is it for the short or long term?
cheers,
Christian
> - change the factory that is used by the parser to create model instances
> - post processing
>
> Regards
> - henrik
>
> On 8/19/10 3:39 PM, Christian Schneider wrote:
>> Of course! :-)
>>
>> But I want to set a (or more) boolean(s) just by calling a rule, WITHOUT
>> any keyword consumption!
>>
>> cheers,
>> Christian
>>
>> On 8/18/10 3:30 PM, Henrik Lindberg wrote:
>>> You can use something like
>>> Rule : aBooleanAttribute =? 'keyword' ;
>>>
>>> To set a boolean if the keyword is present.
>>>
>>> - henrik
>>>
>>> On 8/17/10 11:54 AM, Christian Schneider wrote:
>>>> Hi,
>>>>
>>>> I'm afraid I confused different things!
>>>>
>>>> In this case
>>>>
>>>>> CommentAnnotation returns Annotation:
>>>>> name="comment" value = STRING;
>>>>
>>>> "comment" serves as a keyword and is written to name feature
>>>> if the rule matches, i.e. "comment" is present (I usually put keywords
>>>> in single quotes).
>>>>
>>>> What I actually want to achieve is to make the assignment without
>>>> mentioning the keyword in the text, like a semantic action in
>>>> traditional parser generators.
>>>>
>>>> To be in more detail:
>>>>
>>>> In our models we want to declare different sorts of signals:
>>>> input, output, inputoutput, local
>>>>
>>>> Our meta model contains a signal class with boolean flags isInput,
>>>> isOutput.
>>>>
>>>> Thus, I want to write the grammar like
>>>>
>>>> Model:
>>>> .
>>>> .
>>>> 'input' signals+=InputSignal (',' signals+=InputSignal)*
>>>> |'inputoutput' signals+=InputOutputSignal (','
>>>> signals+=InputOutputSignal)*
>>>> .
>>>> .
>>>> .
>>>> ;
>>>>
>>>> InputSignal returns Signal:
>>>> <hidden isInputs=true> name=ID;
>>>>
>>>> InputOutputSignal returns Signal:
>>>> <hidden isInputs=true isOutput=true> name=ID;
>>>>
>>>>
>>>> Would this be possible?
>>>>
>>>> Best,
>>>> Christian
>>>>
>>>> On 8/16/10 9:31 PM, Christian Dietrich wrote:
>>>>> Hello Christian,
>>>>>
>>>>> can you tell more about the context the rules are used:
>>>>>
>>>>> i tried following:
>>>>>
>>>>>
>>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>>> <ecore:EPackage xmi:version="2.0"
>>>>> xmlns:xmi="http://www.omg.org/XMI"
>>>>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="test"
>>>>> nsURI="http://www.eclipse.org/test" nsPrefix="test">
>>>>> <eClassifiers xsi:type="ecore:EClass" name="Model">
>>>>> <eStructuralFeatures xsi:type="ecore:EReference"
>>>>> name="annotations"
>>>>> upperBound="-1"
>>>>> eType="#//Annotation" containment="true"/>
>>>>> </eClassifiers>
>>>>> <eClassifiers xsi:type="ecore:EClass" name="Annotation">
>>>>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="name"
>>>>> eType="ecore:EDataType
>>>>> http://www.eclipse.org/emf/2002/Ecore#//EString"/>
>>>>> <eStructuralFeatures xsi:type="ecore:EAttribute" name="value"
>>>>> eType="ecore:EDataType
>>>>> http://www.eclipse.org/emf/2002/Ecore#//EString"/>
>>>>> </eClassifiers>
>>>>> </ecore:EPackage>
>>>>>
>>>>>
>>>>>
>>>>> grammar org.xtext.example.mydsl2.MyDsl2 with
>>>>> org.eclipse.xtext.common.Terminals
>>>>> import "platform:/resource/test/model/test.ecore"
>>>>> import "http://www.eclipse.org/emf/2002/Ecore" as ecore
>>>>>
>>>>> Model:
>>>>> annotations+=CommentAnnotation*
>>>>> annotations+=KeyValueAnnotation*
>>>>> ;
>>>>>
>>>>> CommentAnnotation returns Annotation:
>>>>> name="comment" value = STRING;
>>>>>
>>>>> KeyValueAnnotation returns Annotation:
>>>>> name=ID value= STRING;
>>>>>
>>>>>
>>>>>
>>>>> comment "a"
>>>>> comment "b"
>>>>> x "y"
>>>>>
>>>>>
>>>>> and it works for me
>>>>>
>>>>> ~Christian
>>>>
>>>
>>
>
|
|
| |
Goto Forum:
Current Time: Thu Apr 25 08:44:11 GMT 2024
Powered by FUDForum. Page generated in 0.03253 seconds
|