Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Serializing expressions results in an exception
Serializing expressions results in an exception [message #1756242] Tue, 14 March 2017 12:51 Go to next message
Niek Hulsman is currently offline Niek HulsmanFriend
Messages: 3
Registered: March 2017
Junior Member
When serializing the following EMF object structure:

BinaryExpression {
    cref Expression left BinaryExpression {
        cref Expression left LiteralNumber {
            attr EDouble value '10.0'
        }
        attr BinaryOperator operator '+'
        cref Expression right LiteralNumber {
            attr EDouble value '5.0'
        }
    }
    cref Expression right LiteralNumber {
        attr EDouble value '5.0'
    }
}


I get the exception shown below:

at org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic$ExceptionThrowingAcceptor.accept(ISerializationDiagnostic.java:131)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:480)
	at expression.serializer.AbstractExpressionSemanticSequencer.sequence_AdditiveExpression_MultiplicativeExpression_PowerExpression(AbstractExpressionSemanticSequencer.java:245)
	at expression.serializer.AbstractExpressionSemanticSequencer.sequence(AbstractExpressionSemanticSequencer.java:82)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:67)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:118)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:142)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:192)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:58)
	at expression.SerializerTest.testSerializer(SerializerTest.java:49)


Below is part of the grammar:

enum AdditiveOperator returns BinaryOperator: 
	PLUS='+' | MINUS='-' | CONCAT='&'
;

AdditiveExpression returns Expression:
	MultiplicativeExpression (
		=>({BinaryExpression.left=current}
		operator=AdditiveOperator)
		right=MultiplicativeExpression
	)*
;

enum MultiplicativeOperator returns BinaryOperator: 
	MULTIPLY='*' | DIVIDE='/' | MODULO='mod'
;

MultiplicativeExpression returns Expression:
	PowerExpression (
		=>({BinaryExpression.left=current}
		operator=MultiplicativeOperator)
		right=PowerExpression
	)*
;


Can anyone point me in the right direction how to solve this issue?
Re: Serializing expressions results in an exception [message #1756271 is a reply to message #1756242] Tue, 14 March 2017 18:42 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11206
Registered: July 2009
Senior Member
can you please share a complete sample grammar and a reproducing unit test

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Serializing expressions results in an exception [message #1756307 is a reply to message #1756271] Wed, 15 March 2017 09:20 Go to previous messageGo to next message
Niek Hulsman is currently offline Niek HulsmanFriend
Messages: 3
Registered: March 2017
Junior Member
Thnx for looking at this issue.

I can reproduce this issue with the following grammar with a standard xtext project, using Xtext 2.10.0

I've added TestNg on the build path.

Grammar:

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

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

Model:
	greetings+=Greeting*;
	
Greeting:
	'Hello' expression=AdditiveExpression '!';
		
enum AdditiveOperator returns BinaryOperator: 
	PLUS='+' | MINUS='-' | CONCAT='&'
;

AdditiveExpression returns Expression:
	MultiplicativeExpression (
		=>({BinaryExpression.left=current}
		operator=AdditiveOperator)
		right=MultiplicativeExpression
	)*
;

enum MultiplicativeOperator returns BinaryOperator: 
	MULTIPLY='*' | DIVIDE='/' | MODULO='mod'
;

MultiplicativeExpression returns Expression:
	LiteralNumber (
		=>({BinaryExpression.left=current}
		operator=MultiplicativeOperator)
		right=LiteralNumber
	)*
;

LiteralNumber:
	value=INT
;


Using this test:

package org.xtext.example.mydsl;

import org.eclipse.xtext.serializer.ISerializer;
import org.eclipse.xtext.util.EmfFormatter;
import org.testng.annotations.Test;
import org.xtext.example.mydsl.myDsl.BinaryExpression;
import org.xtext.example.mydsl.myDsl.BinaryOperator;
import org.xtext.example.mydsl.myDsl.Expression;
import org.xtext.example.mydsl.myDsl.LiteralNumber;
import org.xtext.example.mydsl.myDsl.MyDslFactory;

import com.google.inject.Inject;
import com.google.inject.Injector;

public class SerializerTest
{
	@Inject
	ISerializer serializer;
	
	@Test
	public void testSerializer()
	{
		Expression result = MyDslFactory.eINSTANCE.createLiteralNumber();
		((LiteralNumber)result).setValue(10);

		for (int i = 1; i < 3; i++)
		{			
			LiteralNumber literalNumber  = MyDslFactory.eINSTANCE.createLiteralNumber();
			literalNumber.setValue(5);

			BinaryExpression binaryExpression = MyDslFactory.eINSTANCE.createBinaryExpression();
			binaryExpression.setLeft(result);
			binaryExpression.setRight(literalNumber);
			binaryExpression.setOperator(BinaryOperator.PLUS);
			
			result = binaryExpression;
		}
		
        System.out.println(EmfFormatter.objToStr(result));
		
		MyDslStandaloneSetup standaloneSetup = new MyDslStandaloneSetup();
		Injector injector = standaloneSetup.createInjectorAndDoEMFRegistration();
		injector.injectMembers(this);
		
		String serializedExpression = serializer.serialize(result);		
		System.out.println(serializedExpression);
	}
}

Re: Serializing expressions results in an exception [message #1756314 is a reply to message #1756307] Wed, 15 March 2017 10:13 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 11206
Registered: July 2009
Senior Member
the problem is that the serializer is not able to determine the context

can you please file a bug at github eclipse/xtext-core

workaround
Expression result = MyDslFactory.eINSTANCE.createLiteralNumber();
		((LiteralNumber)result).setValue(10);

		Greeting g = MyDslFactory.eINSTANCE.createGreeting();
	
		LiteralNumber literalNumber  = MyDslFactory.eINSTANCE.createLiteralNumber();
		literalNumber.setValue(5);

		BinaryExpression binaryExpression = MyDslFactory.eINSTANCE.createBinaryExpression();
		g.setExpression(binaryExpression);
		
		binaryExpression.setLeft(result);
		binaryExpression.setRight(literalNumber);
		binaryExpression.setOperator(BinaryOperator.PLUS);
		
		result = binaryExpression;
		
		
        System.out.println(EmfFormatter.objToStr(result));
		
		MyDslStandaloneSetup standaloneSetup = new MyDslStandaloneSetup();
		Injector injector = standaloneSetup.createInjectorAndDoEMFRegistration();
		injector.injectMembers(this);
		
		
		String serializedExpression = serializer.serialize(result);		
		System.out.println(serializedExpression);


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: Serializing expressions results in an exception [message #1756326 is a reply to message #1756314] Wed, 15 March 2017 12:07 Go to previous message
Niek Hulsman is currently offline Niek HulsmanFriend
Messages: 3
Registered: March 2017
Junior Member
Thanx Christian for the workaround, that works perfectly.

I've made an issue: https://github.com/eclipse/xtext-core/issues/302
Previous Topic:Method not found in enclosing type
Next Topic:XText grammar with lookbehind
Goto Forum:
  


Current Time: Sat Sep 23 16:38:35 GMT 2017

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

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