Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XText cross-reference to rule containing a cross-reference
XText cross-reference to rule containing a cross-reference [message #1377158] Fri, 23 May 2014 19:09 Go to next message
dan jatnieks is currently offline dan jatnieksFriend
Messages: 8
Registered: July 2009
Junior Member
The following is a simple representation of an xtext grammar with cross references.

There are two entities - a container and an object - and operations on each. I would like to be able to refer to an object either by it's own name, or qualified with the container name. I would like cross-references to be available in either case.

The grammar I have is:

Model:
    operations+=Operation*;

ContainerEntity:
    name=ID;

ObjectEntity:
    (first=[ContainerEntity] '.')? name=ID
;

Operation:
    CreateContainer | CreateObject | ContainerOp | ObjectOp
;

CreateContainer:
    'Container' container=ContainerEntity ';'
;

CreateObject:
    'Object' object=ObjectEntity ';'
;

ContainerOp:
    'ContainerOp' name=[ContainerEntity] ';'
;

ObjectOp:
    'ObjectOp' name=[ObjectEntity] ';'
;


And the editor statements are:

// Define a container and object
Container c;
Object o;

// Can define an object using container name qualifier
Object c.o2;

// Operations
ContainerOp c;
ObjectOp o;

// Cannot use object operation with container name 
ObjectOp c.o;            // ERROR: Couldn't resolve reference to ObjectEntity 'c'.

Note that it does not recognize "c" as a ContainerEntity. What can I do to make this work the way I described?
Re: XText cross-reference to rule containing a cross-reference [message #1379258 is a reply to message #1377158] Sat, 24 May 2014 16:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi you have to come up with a own Customization of
DefaultDeclarativeQualifiedNameProvider and leverage NodeModelUtils
findnodesforfeature to read the cross references texr


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText cross-reference to rule containing a cross-reference [message #1383995 is a reply to message #1379258] Tue, 27 May 2014 18:11 Go to previous messageGo to next message
dan jatnieks is currently offline dan jatnieksFriend
Messages: 8
Registered: July 2009
Junior Member
Thanks for that tip Christian.

I have since noticed that hovering on the red 'X' in the left side rather than the red underlined "c", give me this message:

Quote:

Multiple markers at this line
- Couldn't resolve reference to ObjectEntity 'c'.
- mismatched input '.' expecting ';'


I'm a little concerned the 'mismatched input' message may indicate a problem in the grammar?

I've added the following to my MyDslQualifiedNameProvider
	QualifiedName qualifiedName(ObjectEntity obj) {
		List<String> names = new ArrayList<String>();

		final EClass eClass = obj.eClass();
		final EStructuralFeature result = eClass.getEStructuralFeature("first");
		List<INode> nodes = NodeModelUtils.findNodesForFeature(obj, result);
		for (INode n : nodes) {
			System.out.println(n.getText());
                        // TODO: add ContainerEntity name to qualified name ??
		}

		names.add(obj.getName());

		QualifiedName qn = QualifiedName.create(names);
		System.out.println("QN= " + qn);
		return qn;
	}


But 'findNodesForFeature' just returns an empty list for the line 'ObjectOp c.o'. Could it be because of the 'mismatched input' message?

When called for ObjectEntity 'o2' from the line 'Object c.o2;', then 'findNodesForFeature' does return a node and node.getText() is 'c' from the ContainerEntity as expected.

So I'm still having a problem with 'ObjectOp c.o;' and getting the ContainerEntity.

[Updated on: Tue, 27 May 2014 18:23]

Report message to a moderator

Re: XText cross-reference to rule containing a cross-reference [message #1383998 is a reply to message #1383995] Tue, 27 May 2014 18:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i dont understand your impl. here is basically what i intended you to do

ObjectOp:
    'ObjectOp' name=[ObjectEntity|FQN] ';'
;

FQN: ID ("." ID)*;


a second problem is that xx=[YY] is short for xx=[YY|ID] which means reference an YY and parse an ID. an ID does not allow a .

=>

ObjectOp:
    'ObjectOp' name=[ObjectEntity|FQN] ';'
;

FQN: ID ("." ID)*;


then i do not understand your example model: c.o the both are not related to each other.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText cross-reference to rule containing a cross-reference [message #1384014 is a reply to message #1383998] Tue, 27 May 2014 20:07 Go to previous messageGo to next message
dan jatnieks is currently offline dan jatnieksFriend
Messages: 8
Registered: July 2009
Junior Member
Hi Christian,

Both code blocks in your last reply have identical content - was that supposed to be the case or did you mean to have something else in the first block?

You mentioned customizing DefaultDeclarativeQualifiedNameProvider and that's what I tried to do in MyDslQualifiedNameProvider.qualifiedName() using the NodeModelUtils.findNodesForFeature as suggested.

I don't quite follow the suggestion about FQN. If I use an FQN in the cross reference, e.g. name=[ObjectEntity|FQN], then I guess that the ObjectEntity name will contain both the ContainerEntity name and the ObjectEntity name, e.g "c.o". Then I need a way to pull this apart so that the "c" becomes a ContainerEntity name and the "o" becomes the ObjectEntity name? How would I do that?

Thanks again,

dan
Re: XText cross-reference to rule containing a cross-reference [message #1384015 is a reply to message #1384014] Tue, 27 May 2014 20:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hmmm i was missing that one

public class MyDslQualifiedNameProvider extends
		DefaultDeclarativeQualifiedNameProvider {
	
	QualifiedName qualifiedName(ObjectEntity obj) {
		List<INode> nodes = NodeModelUtils.findNodesForFeature(obj, MyDslPackage.Literals.OBJECT_ENTITY__FIRST);
		if (nodes.size()>0) {
			return QualifiedName.create(nodes.get(0).getText().trim(), obj.getName());
		}
		return null;
	}


}


then the container should become the parent of the child and the childs name should be parentname.childname


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: XText cross-reference to rule containing a cross-reference [message #1384017 is a reply to message #1384015] Tue, 27 May 2014 20:13 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
CreateContainer:
    'Container' container=ContainerEntity ';' children+= CreateObject*
;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Saving/Loading to/from BinaryResourceImpl
Next Topic:Customization of NamesAreUniqueValidator
Goto Forum:
  


Current Time: Tue Apr 23 06:35:06 GMT 2024

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

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

Back to the top