|Combining data structure with a markup format [message #699308]
||Thu, 21 July 2011 10:10
| Kiran Subbaraman
Registered: May 2011
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
|| Christian Dietrich
Registered: July 2009
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: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Thu, 21 July 2011 20:01]
Report message to a moderator
Powered by FUDForum
. Page generated in 0.01834 seconds