|
Re: Problems with multidimensional arrays [message #755304 is a reply to message #755301] |
Tue, 08 November 2011 03:04 |
Max Goltzsche Messages: 40 Registered: November 2011 |
Member |
|
|
Hey,
Firstly, I would define the array as
Array:
{Array}
'name' name=ID &
'elements' '[' (elements+=STRING (',' elements+=STRING)*)? ']';
-> use * instead of + else you should only be able to define an array with at least two elements:
* = none or many
+ = one or many
? = one or none
-> Note that every element that is has no {ClassName} description on top will not be instantiated. Only those with that class description will be transformed to java classes. Those without are only grammar rules and are transformed to java interfaces (as far as I know; I am also new to Xtext... it may also be transformed to a class if it contains properties).
If you create a default xtext project with eclipse it generates a generator stub in Xtend into the *.generator package. Inside the void doGenerate(Resource resource, IFileSystemAccess fsa) method you can access every model element and delegate to your specific generator methods. A Resource contains the whole model. You can access your language's root element by accessing resource.contents.head or get all your specific Array elements by typing e.g.:
for (a : resource.contents.filter(typeof(Array))) {
System::out.println("array: " + a.name)
for (e : a.elements)
System::out.println(" element: " + e)
}
To declare nested arrays it may be a better solution to define your grammar recursive. E.g.:
Element:
Array | StringContainer;
StringContainer:
{StringContainer}
content=STRING | ID;
Array:
{Array}
'[' (elements+=Element (',' elements+=Element)*)? ']';
-> Object is an interface.
-> StringContainer and Array are classes and implement the Element interface.
-> An Array can contain more Arrays or StringContainer instances so that you could write sth like [asdf, [asdf, ["asdf "]], "etc and so on"]
As a good starting point I recommend the Xtext domainmodel example which you can create with the new project wizard in eclipse.
regards,
Max
[Updated on: Tue, 08 November 2011 03:39] Report message to a moderator
|
|
|
Re: Problems with multidimensional arrays [message #755427 is a reply to message #755304] |
Tue, 08 November 2011 13:23 |
Jens Missing name Messages: 27 Registered: November 2011 |
Junior Member |
|
|
hi,
thanks for your detailed answer. The grammar works like a charm.
But how can I traverse the elements in the generator?
grammar:
Element:
MultiArray | StringContainer;
StringContainer:
{StringContainer}
content=STRING;
MultiArray:
{MultiArray}
'[' (elements+=Element (',' elements+=Element)*)? ']';
MultipleArray:
'name' name=ID &
'elements {' ('['(( elements += STRING ',')+ elements+=STRING ']' ',')+ '['( elements += STRING ',')+ elements+=STRING ']')+ '}'
;
Table:
'table' name=ID '{' columnNames = Array ':' tableContent = MultiArray '}'
;
generator:
«FOR item:t.tableContent.elements»
«t.tableContent».add("«item»");
«ENDFOR»
How can I realize the second for-loop?
regards,
Jens
[Updated on: Tue, 08 November 2011 20:16] Report message to a moderator
|
|
|
|
Re: Problems with multidimensional arrays [message #756071 is a reply to message #755606] |
Thu, 10 November 2011 19:11 |
Max Goltzsche Messages: 40 Registered: November 2011 |
Member |
|
|
Hey,
What is Array in Table?
MultipleArray and Table are not reachable in your grammar. A Xtext grammar's start element is the first element. For this reason your grammar starts with Element which contains StringContainer and MultiArray but both does not contain Table or MultipleArray. Your grammar may start with Table.
I recommend you to use Xtext 2.1 (not available via eclipse marketplace but here: http://www.eclipse.org/Xtext/download/) and Xtend to generate code and try out the examples.
Those {} are superfluous/redundant... my fault
regards,
Max
[Updated on: Thu, 10 November 2011 19:22] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03545 seconds