| [news.eclipse.modeling.tmf] Re: [XText] serialize parsed tree to String |
omfg, you are right. Input was "-1" and '-' was not in the definition of INT. I've overwrite it with
terminal INT returns ecore::EInt: '-'?('0'..'9')+;and everything was fine. Argh, sorry. The INT in XText is just NAT ;-)
That was a bug in Xtext in the 0.7.2-release.
You are my best friend when you'll deploy the current version of XText to the Maven2 repository [http://openarchitectureware.org/m2/].
Thank you for your help.
Ciao, Micha
--- Original-Nachricht --- Absender: Moritz Eysholdt Datum: 16.09.2009 22:18
Hi Michael,you are right, was not the correct line, but a very similar. The source is long and the corresponding exception print out is much longer.Generally speaking, this happens when your model contains values that are not written during serialization or when your model does *not* contain values that are required for serialization. Your grammar specifies which values these are. But since your are serializing the model right after you've parsed it, I guess that you haven't added any model elements in the meantime, right? One could do things like that in the Linker, for example. In case there is multiple inheritance in your Ecore model, this is likely to be the reason. That was a bug in Xtext in the 0.7.2-release. Updating to a newer release can help.
But, back to topic.
If I try this [1], I got an exception in line 11.
Exception: org.eclipse.xtext.parsetree.reconstr.XtextSerializationException: Serialization failed
But why?
Model checked and OK.OK.
Parsing OK.To be sure parsing is *really* OK, please check whether resource.getWarnings() and resource.getErrors() are empty after calling resource.load().
Am I right, that Resource.load() is for parsing and Resource.save() for serialization?
Yes.
If my answers don't tackle your problem, please post your grammar and the textual model you've been parsing.
cheers, Moritz
[1] - java code
---------------
line 1: Injector injector = new DslStandaloneSetup().createInjectorAndDoEMFRegistration();
line 2: XtextResourceSet xtextResourceSet = injector.getInstance(XtextResourceSet.class);
line 3: xtextResourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
line 4: Map<?, ?> options = xtextResourceSet.getLoadOptions();
line 5: URI dslResource = org.eclipse.emf.common.util.URI.createURI("platform:/resource/dummy.dsl");
line 6: Resource resource = xtextResourceSet.createResource(dslResource);
line 7: InputStream in = new ByteArrayInputStream(dslCode.getBytes());
line 8: resource.load(in, xtextResourceSet.getLoadOptions());
line 9: ret = (Model) resource.getContents().get(0);
line 10: ByteArrayOutputStream bos = new ByteArrayOutputStream();
line 11: resource.save(bos, options);
==== end ====
--- Original-Nachricht --- Absender: Moritz Eysholdt Datum: 14.09.2009 16:15Hi Michael,
what you are listing below is actually an error message and *not* your serialized model. However, this error message contains fragments of your serialized model.
Are you sure the message resulted from serializing the model from parsing "$varname = 5;"?
The message contains the serialized part of your model: "] = $current ; $message = "square root found" ; } }" And this doesn't look to me like it has originated from "$varname = 5;".
regards, Moritz
Hi Sebastian,
ok, I've got it running, but not in a way I want.
Parsing and serialization are divided into two classes in my implementation. Thats the reason why XtextResourceSet.getResource() reports: "resource was not registered".
If I merge the methods, I got a different serialization as I hoped for. I put into the parser a line like this:
$varname = 5;
after parsing and serializing I got something like that:
======
scripts[0] = Assign {
variable = Variable(name='de.btopia.ghandara.charme.impl.VariableNameImpl@55eb1db2 (name: $result)') {
69: "] = $current ; $message = "square root found" ; } }...":
-> Variable_IndexAssignment_1_1: Variable.index is not set.
68: "= $current ; $message = "square root found" ; } } }...":
-> Variable_PropertyAssignment_2_1: Variable.property is not set.
}
value = Expression {
headTerm = IntegerConst(...
====
OK, I found no easy-to-read code snipplet. Sorry. I can give you much more complex examples.
But first, the Injector needs an inject with the registered resources. The use cases (parse and serialize) can be made independent from each other. The parsed model has all information about the original DSL code. There is no need to store resource URIs _OR_ the authority for registration resources is unique in the whole system.
But in my case:
How can I reach my simple DSL code line "$varname = 5;" from serialization?
Ciao, Micha
--- Original-Nachricht --- Absender: Sebastian Zarnekow Datum: 11.09.2009 18:17Hi Michael,
your resource is emtpy and has not been loaded.
Please try something like:
Resource res = resourceSet.getResource(URI.createURI(..existing uri..));
res.save(..);
resourceSet.createResource will create a new resource but won't load it.
res = resourceSet.createResource(..uri..); res.load(new StringInputStream("valid model")); res.save(..)
should do the trick as well.
Hope that helps, Sebastian