Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Combining data structure with a markup format
Combining data structure with a markup format [message #699308] Thu, 21 July 2011 10:10 Go to next message
Kiran Subbaraman is currently offline Kiran SubbaramanFriend
Messages: 9
Registered: May 2011
Junior Member
I work with a JSON based document quite often - need to author it by hand. Realized that I could use XText to describe the syntax of this JSON based structure, and use the generated rich text editor as the authoring environment. Expected this to provide some productivity gains.
This is a sample JSON based document

Wrote a Xtext based grammar for it

	'{ '
	(data=Data (',')? &

	'"data":' '[' dataElements+=STRING (',' dataElements+=STRING)* ']'; 

The above grammar definition has the JSON markup structure interspersed with the structure that my application needs.

I also realized that my JSON based structure can be expressed as an XML based structure too. The only change would be the way the interchange format contains the elements (curly braces, quotes, commas, colons in JSON, and arrow brackets in XML). Whereas the actual structure of the grammar would remain the same.

So I was wondering if I could define the structure of JSON using a grammar, and the structure of XML in a grammar. Also, create the grammar for my actual application document independent of the markup. Later I wanted to create grammars that make this possible:
my application grammar + JSON grammar = JSON based application grammar
my application grammar + XML grammar = XML based application grammar

This would ensure that the the core structure of my grammar is not "polluted / hard-coded" with the markup syntax. Also makes it more easily readable, and increases the chances of remixing this with any other structure; maybe Thrift.

Is this possible? I understand that two grammars can be combined (for example the standard Terminals one is used by default), and how to go about it. I am not sure how I can solve the problem that I have stated above though.

P.S: I had asked a similar question earlier, but wasn't sure I had explained the problem properly. So, this is my second attempt at it.

Re: Combining data structure with a markup format [message #699572 is a reply to message #699308] Thu, 21 July 2011 19:56 Go to previous message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 13992
Registered: July 2009
Senior Member

Xtext does not allow to mix grammars but allows a single inheritance hierachy:
so you can have a base Grammar and Grammars a + b that extend from this base grammar,
but you cannot have grammar a and b and create c as a mix of them.

But as alex said before: you can have two grammars that create the same stuff.
you'd e.g. have a "data" metamodel and write two grammar a and b
that create instances of this data metamodel with xml and json ans concrete syntax.

from you application grammar you could reference stuff from the data files,
but you could not inline json or xml in you app models.

So you would have

.jsondsl files that contain data in json format
.xmldsl file that contain data in xml format
.app file that contains you application stuff
within .app you can reference stuff from .jsondsl or .xmldsl
but not write inline xml and json stuff.


Need professional support for Xtext, Xpand, EMF?
Go to:
Twitter : @chrdietrich
Blog :

[Updated on: Thu, 21 July 2011 20:01]

Report message to a moderator

Previous Topic:Import and JvmModel inference
Next Topic:Beginner : Xtext help
Goto Forum:

Current Time: Mon Jun 21 08:29:18 GMT 2021

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

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

Back to the top