Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Problems with multidimensional arrays(Problems with multidimensional arrays)
Problems with multidimensional arrays [message #755301] Tue, 08 November 2011 01:23 Go to next message
Jens Missing name is currently offline Jens Missing nameFriend
Messages: 27
Registered: November 2011
Junior Member
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] Tue, 08 November 2011 03:04 Go to previous messageGo to next message
Max Goltzsche is currently offline Max GoltzscheFriend
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 Go to previous messageGo to next message
Jens Missing name is currently offline Jens Missing nameFriend
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 #755606 is a reply to message #755427] Tue, 08 November 2011 22:44 Go to previous messageGo to next message
Jens Missing name is currently offline Jens Missing nameFriend
Messages: 27
Registered: November 2011
Junior Member
Here is how I have defined it in the model:

table myTable { name columnArray elements ['firstname','lastname'] :
["one",["two", "three"]] }}

may be that can help.

[Updated on: Tue, 08 November 2011 22:46]

Report message to a moderator

Re: Problems with multidimensional arrays [message #756071 is a reply to message #755606] Thu, 10 November 2011 19:11 Go to previous message
Max Goltzsche is currently offline Max GoltzscheFriend
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 Rolling Eyes

regards,
Max

[Updated on: Thu, 10 November 2011 19:22]

Report message to a moderator

Previous Topic:Highlighting with Xtext
Next Topic:[xtext2.1] problem implementing custom formatter
Goto Forum:
  


Current Time: Thu Apr 25 02:27:50 GMT 2024

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

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

Back to the top