Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Title: Issue with serializing cross reference with same name
Title: Issue with serializing cross reference with same name [message #1817538] Mon, 25 November 2019 15:14 Go to next message
L R is currently offline L RFriend
Messages: 12
Registered: July 2019
Junior Member
I am seeing a java.lang.RuntimeException when serializing a model that was created using the model factory. In this model, two variables have the same name, but are within different scopes. When I access this model using the parseHelper, I am able to serialize the model. However, if I create the same model using the factory it gives me this exception. Similarly, if I create a similar model with the parseHelper and then modify it by changing the variable name to match it also gives me this exception when serializing.

My best guess is that this is a scoping / qualified naming issue, but I don't understand what makes the parseHelper model serialize, but the other models fail. What is the correct way to resolve this issue?

Error:
java.lang.RuntimeException: No EObjectDescription could be found in Scope NamedReference.id for File.statements[0]->Block.statements[1]->Block.statements[0]->Variable'x'
Semantic Object: File.statements[0]->Block.statements[1]->Block.statements[1]->ExpressionStatement.expr->AssignmentExpression.left->NamedReference
URI: file:/c:/temp/test3.mydsl
EStructuralFeature: myDsl::NamedReference.id
	at org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic$ExceptionThrowingAcceptor.accept(ISerializationDiagnostic.java:131)
	at org.eclipse.xtext.serializer.tokens.CrossReferenceSerializer.getCrossReferenceNameFromScope(CrossReferenceSerializer.java:138)
...


Example Model:
{
	{
		int x=1;
		x = 2;
	}
	{
		int x=3;
		x = 4;
	}
}


Model Creation via Factory:
	def File CreateFile() {
		val file = MyDslFactory.eINSTANCE.createFile();
		val block1 = MyDslFactory.eINSTANCE.createBlock();
		file.getStatements.add(block1)
		val block2 = MyDslFactory.eINSTANCE.createBlock();
		block1.getStatements.add(block2)
		val var1 = MyDslFactory.eINSTANCE.createVariable();
		val type1 = MyDslFactory.eINSTANCE.createType();
		type1.setType("int")
		var1.setType(type1)
		var1.setName("x")
		block2.getStatements.add(var1)
		val literal1 = MyDslFactory.eINSTANCE.createExpression()
		literal1.setValue(1)
		var1.setRight(literal1)
		val literal2 = MyDslFactory.eINSTANCE.createExpression()
		literal2.setValue(2)
		val ae1 = MyDslFactory.eINSTANCE.createAssignmentExpression()
		ae1.setRight(literal2)
		val nr1 = MyDslFactory.eINSTANCE.createNamedReference()
		nr1.setId(var1)
		ae1.setLeft(nr1)
		val es1 = MyDslFactory.eINSTANCE.createExpressionStatement()
		es1.setExpr(ae1)
		block2.getStatements.add(es1)

		val block3 = MyDslFactory.eINSTANCE.createBlock();
		block1.getStatements.add(block3)
		
		val var2 = MyDslFactory.eINSTANCE.createVariable();
		val type2 = MyDslFactory.eINSTANCE.createType();
		type2.setType("int")
		var2.setType(type2)
		var2.setName("x")
		block3.getStatements.add(var2)
		val literal3 = MyDslFactory.eINSTANCE.createExpression()
		literal3.setValue(3)
		var2.setRight(literal3)
		val literal4 = MyDslFactory.eINSTANCE.createExpression()
		literal4.setValue(4)

		val ae2 = MyDslFactory.eINSTANCE.createAssignmentExpression()
		ae2.setRight(literal4)
		val nr2 = MyDslFactory.eINSTANCE.createNamedReference()
		nr2.setId(var2)
		ae2.setLeft(nr2)
		val es2 = MyDslFactory.eINSTANCE.createExpressionStatement()
		es2.setExpr(ae2)
		block3.getStatements.add(es2)

		return file
	}


DSL:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

File:
	statements+=GeneralStatement+;

GeneralStatement:
	ExpressionStatement
	| Block
	| VariableStatement;

Block:
	{Block} "{" (statements+=GeneralStatement)* "}";

ExpressionStatement:
	expr=Expression ';';

VariableStatement:
	Variable ('=' right=Expression)? ';';

Variable:
	type=Type name=ID;

Type:
	type=('int');

Expression:
	AssignmentExpression;

AssignmentExpression returns Expression:
	AtomicExpression ({AssignmentExpression.left=current} '=' right=AtomicExpression)*;

AtomicExpression returns Expression:
	'(' Expression ')'
	| LiteralExpr
	| NamedReference;

LiteralExpr returns Expression:
	value=INT;

NamedReference:
	id=[Element];

Element:
	Variable;


Serialize function:
	def String serialize(EObject obj, String resourceURILocation) {
		val uri = URI.createFileURI(resourceURILocation);
		val setup = new MyDslStandaloneSetupGenerated();
		val injector = setup.createInjectorAndDoEMFRegistration();
		val resourceSet = injector.getInstance(typeof(XtextResourceSet));
		val resource = resourceSet.createResource(uri);
		resource.getContents().add(obj);
		val serializer = injector.getInstance(typeof(ISerializer));
		val result = serializer.serialize(obj);
		return result;
	}
Re: Title: Issue with serializing cross reference with same name [message #1817546 is a reply to message #1817538] Mon, 25 November 2019 16:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,
How does your scoping implementation look like
That ensures the right variable is picked
And not eg the first one


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Title: Issue with serializing cross reference with same name [message #1817558 is a reply to message #1817546] Mon, 25 November 2019 19:21 Go to previous messageGo to next message
L R is currently offline L RFriend
Messages: 12
Registered: July 2019
Junior Member
To date, I am just using the default scoping (i.e. DelegatingScopeProvider). I am predominately using this dsl to translate into and then serialize. So I haven't needed to further implement scoping to date. While I assumed this was related to scoping, I couldn't understand what was different with the parseHelper variation.
Re: Title: Issue with serializing cross reference with same name [message #1817559 is a reply to message #1817558] Mon, 25 November 2019 19:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
if you use default scoping the block thing wont work
did you actually try to crtl clock / f3 and see where the second x=... links to.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Title: Issue with serializing cross reference with same name [message #1817560 is a reply to message #1817559] Mon, 25 November 2019 20:07 Go to previous messageGo to next message
L R is currently offline L RFriend
Messages: 12
Registered: July 2019
Junior Member
The second x=... links to the first variable assignment (illustrating that default scoping the block doesn't work). [I'll be honest, I thought I checked that before, but I missed it.]. Thanks for this insight. I am still surprised that the model produced by the parseHelper is able to be serialized. Maybe there was fragment caching or something that enabled that model to be serialized, but once it was changed it returned to expose this scoping issue. In either case, I will go implement the scoping and am confident that will fix my issue.
Re: Title: Issue with serializing cross reference with same name [message #1817561 is a reply to message #1817560] Mon, 25 November 2019 21:04 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
no

if your change your code tp the second assignment pointing to the first variable (instead of the second) it should seialize


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Issues with terminal STRING which always outputs an error
Next Topic:Getting errors for Gradle Xtext project on Eclipse DSL Tools 2019-06
Goto Forum:
  


Current Time: Thu Apr 25 14:35:36 GMT 2024

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

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

Back to the top