| [XText] Use serializer in Standalone mode [message #536824] |
Mon, 31 May 2010 03:54  |
Alex Lagarde Messages: 124 Registered: May 2010 |
Senior Member |

|
|
Hi everyone !
I'm currently trying to use XText in Standalone Mode.
I managed to correctly parse a file and get its AST, but when serializing this AST I get the following error :
java.lang.NegativeArraySizeException
at java.lang.AbstractStringBuilder.<init>(Unknown Source)
at java.lang.StringBuilder.<init>(Unknown Source)
at org.eclipse.xtext.parsetree.reconstr.impl.DefaultValueSerial izer.serialize(DefaultValueSerializer.java:50)
at org.eclipse.xtext.parsetree.reconstr.impl.DefaultValueSerial izer.equalsOrReplacesNode(DefaultValueSerializer.java:42)
at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor$AssignmentToken.equalsOrReplacesNode(AbstractPars eTreeConstructor.java:228)
at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.assignTokenByMatcher(AbstractParseTreeConstructor .java:586)
at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.assignTokenByMatcher(AbstractParseTreeConstructor .java:600)
at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.assignNodesByMatching(AbstractParseTreeConstructo r.java:573)
at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC onstructor.serializeRecursive(AbstractParseTreeConstructor.j ava:739)
at org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:87)
at org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:103)
at org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ e(SerializerUtil.java:97)
Here is the code I used :
// Step 1 : we register the EPackages in use
registerEPackages();
// Step 2 : we get the XText parser and serializer in standalone mode
Injector guiceInjector = MyDslStandaloneSetup.doSetupAndReturnInjector();
parser = guiceInjector.getInstance(IAntlrParser.class);
serializer = guiceInjector.getInstance(SerializerUtil.class);
// Step 3 : we parse the given String
IParseResult result = parser.parse(new StringReader(stringToParse));
// Step 4 : we get the AST from the parse result
EObject parsedAST = result.getRootASTElement();
// Step 5 we serialize this AST
SerializationOptions options = new SerializationOptions();
options.setFormat(true);
options.setValidateConcreteSyntax(true);
String serializedForm= serializer.serialize(parsedAST, options);
Can anyone tell me what I do wrong ?
Thanks by advance !
[Updated on: Mon, 31 May 2010 03:56] Report message to a moderator
|
|
|
| Re: [XText] Use serializer in Standalone mode [message #536856 is a reply to message #536824] |
Mon, 31 May 2010 05:29   |
Moritz Eysholdt Messages: 131 Registered: July 2009 Location: Kiel, Germany |
Senior Member |
|
|
Hi Alex,
it looks like node.getLength() returned a negative value for some node
from your node model. This usually means there have been parse errors.
Could you please check if result.getParseErrors() holds any message
after parsing?
Usually it is easier to use EMF's ResourceSet directly like this:
ResourceSet rs = new XtextResourceSet();
Resoure resource = rs.getResource(someURI, true);
EObject myAST = resource.getContents.get(0);
myAST.setSomeValue("FooBar");
resource.save(null);
Since Xtexts implements an EMF resource, this delegates to Xtext's
parser and serializer.
Calling parser and serializer as you did it works as well, but is more
complicated, so I'd only recommend it if the resource based approach
doesn't work for you.... btw, your code misses to call the Linker to
resolve cross references after parsing ;)
regards,
Moritz
alex.lagarde@obeo.fr wrote:
> Hi everyone !
> I'm currently trying to use XText in Standalone Mode. I managed to
> correctly parse a file and get its AST, but when serializing this AST I
> get the following error :
> java.lang.NegativeArraySizeException
> at java.lang.AbstractStringBuilder.<init>(Unknown Source)
> at java.lang.StringBuilder.<init>(Unknown Source)
> at org.eclipse.xtext.parsetree.reconstr.impl.DefaultValueSerial
> izer.serialize(DefaultValueSerializer.java:50)
> at org.eclipse.xtext.parsetree.reconstr.impl.DefaultValueSerial
> izer.equalsOrReplacesNode(DefaultValueSerializer.java:42)
> at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC
> onstructor$AssignmentToken.equalsOrReplacesNode(AbstractPars
> eTreeConstructor.java:228)
> at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC
> onstructor.assignTokenByMatcher(AbstractParseTreeConstructor .java:586)
> at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC
> onstructor.assignTokenByMatcher(AbstractParseTreeConstructor .java:600)
> at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC
> onstructor.assignNodesByMatching(AbstractParseTreeConstructo r.java:573)
> at org.eclipse.xtext.parsetree.reconstr.impl.AbstractParseTreeC
> onstructor.serializeRecursive(AbstractParseTreeConstructor.j ava:739)
> at org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ
> e(SerializerUtil.java:87)
> at org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ
> e(SerializerUtil.java:103)
> at org.eclipse.xtext.parsetree.reconstr.SerializerUtil.serializ
> e(SerializerUtil.java:97)
>
> Here is the code I used : MyDslStandaloneSetup.doSetup();
>
> // Step 1 : we register the EPackages in use
> registerEPackages();
>
> // Step 2 : we get the XText parser and serializer in standalone mode
> Injector guiceInjector =
> MyDslStandaloneSetup.doSetupAndReturnInjector();
> parser = guiceInjector.getInstance(IAntlrParser.class);
> serializer = guiceInjector.getInstance(SerializerUtil.class);
>
> // Step 3 : we parse the given String
> IParseResult result = parser.parse(new StringReader(stringToParse));
>
> // Step 4 : we get the AST from the parse result
> EObject parsedAST = result.getRootASTElement();
>
> // Step 5 we serialize this AST
> SerializationOptions options = new SerializationOptions();
> options.setFormat(true);
> options.setValidateConcreteSyntax(true);
> String serializedForm= serializer.serialize(parsedAST,
> options);
>
> Can anyone tell me what I do wrong ?
> Thanks by advance !
>
>
>
>
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
|
|
|
|
| Re: [XText] Use serializer in Standalone mode [message #536877 is a reply to message #536862] |
Mon, 31 May 2010 07:18   |
Moritz Eysholdt Messages: 131 Registered: July 2009 Location: Kiel, Germany |
Senior Member |
|
|
hi Alex,
> Thanks for your reply.
>
> I always check for the parseErrors before serializing :
ok, if you say you're getting the exception even though there haven't
been any parse errors, could you please provide a small example (an
example project, or the grammar + the document you're parsing)? This
would help to reproduce the problem and ensure it's not a bug in Xtext.
> I know it's easier to use XtextResources, but in fine I would like to
> create my own Resource : I want my AST to be stored in a CDO Repository,
> so if it is possible I would not like to use XtextRessources.
Sure, you can do that. Another approach would be to use the
XtextResource to load/save the model and to move the model into a
different resource implementation if you need it:
someResource.getContents().addAll(xtextResource.getContents( ))
hth,
Moritz
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
|
|
|
|
|
| Re: [XText] Use serializer in Standalone mode [message #536904 is a reply to message #536878] |
Mon, 31 May 2010 09:02   |
Moritz Eysholdt Messages: 131 Registered: July 2009 Location: Kiel, Germany |
Senior Member |
|
|
> Thanks anyway, I think I will have to make my own serializer, except if
> someone understands where does this NegativeArraySizeException come
> from, knowing that my parsed AST is correct ?!
Your AST is *not* correct since a negative value is not allowed for
node.getLength().
As I said, I'd need an example that I can use to reproduce the problem.
Then, it would probably be easy to understand what went wrong.
What you found is not a problem in Xtext's serializer but a problem in
the node model. Since the node model is also used in other places like
outline, hyperlinking/navigation, content assist, etc. you might run
into errors with that as well.
cheers,
Moritz
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01859 seconds