Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert Object to Xtext DSL
    primarykey
    data
    text
    <p>I've defined a simple Xtext grammar which looks like this (simplified):</p> <pre><code>grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals generate myDsl "http://www.xtext.org/example/mydsl/MyDsl" import "http://www.eclipse.org/emf/2002/Ecore" as ecore System: 'Define System' ( 'Define Components' '{' components+=Component+ '}' ) 'End' ; Component: 'Component' name=ID 'Value' value=Double ';' ; Double returns ecore::EDouble: '-'? INT? '.' INT ; </code></pre> <p><strong>The problem I like to solve is - how can I convert a simple Java Object to a valid xtext file?</strong></p> <p>To simplify my problem, lets say we create a list of components in Java:</p> <pre><code>List&lt;Component&gt; components = new ArrayList&lt;Component&gt;(); components.add(new Component("FirstComponent", 1.0)); components.add(new Component("SecondComponent", 2.0)); components.add(new Component("ThirdComponent", 3.0)); </code></pre> <p>The output-file I like to create should look like this:</p> <pre><code>Define System Define Components { Component FirstComponent Value 1.0; Component SecondComponent Value 2.0; Component ThirdComponent Value 3.0; } End </code></pre> <p>It is important that this file is checked by the xtext grammar, so that it's valid. I Hope you have any ideas for me. Here are some of mine, but so far I don't know how to implement them:</p> <p>Idea #1: I know how to read and write a file. In my head one solution could look like this: I have the list in my Java code, now I like to write a file which looks like the output-file above. Afterwards I like to read this file and check for errors by the grammar. How can I do this?</p> <p>Idea #2: If I imagine I would create a xml file out of Java code using JDOM, I wish I could do the same in xtext. Just define a parent "Define System" which ends with "End" (see my output-file) and then add a child "Define Components {" which ends with "}" and then add the children to this, e.g. "Component FirstComponent Value 1.0;". I hope this isn't confusing :-)</p> <p>Idea #3: I could use a template like the following and add children between the braces "{" ... "}":</p> <pre><code>Define System Define Components { ... } End </code></pre> <p>Btw: I already tried <a href="https://stackoverflow.com/questions/10917386/linking-xtext-with-stringtemplate-code-generator">Linking Xtext with StringTemplate code generator</a>, but it is kind of another problem. Hope you have any ideas.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload