Problems with multidimensional arrays [message #755301] |
Mon, 07 November 2011 20:23  |
Eclipse User |
|
|
|
hi,
I have defined an array which works good.
Array:
'name' name=ID &
'elements [' ( elements += STRING ',')+ elements+=STRING ']'
;
From that point on - I have derrived an multidimensional array, but now I can't access the elements (Array) in the generator.
MultipleArray:
'name' name=ID &
'elements {' ('['( elements += Array ',')+ elements+=Array ']' ',')+ '['( elements += Array ',')+ elements+=Array ']' '}'
;
Could you please help me?
|
|
|
Re: Problems with multidimensional arrays [message #755304 is a reply to message #755301] |
Mon, 07 November 2011 22:04   |
Eclipse User |
|
|
|
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: Mon, 07 November 2011 22:39] by Moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04219 seconds