Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Allow cross-references to "built-in" types
Allow cross-references to "built-in" types [message #1851662] Tue, 12 April 2022 06:59 Go to next message
Cédric Moonen is currently offline Cédric MoonenFriend
Messages: 14
Registered: December 2014
Junior Member
Hello,

I'm currently implementing a language which generate some boilerplate C++ code. This language has the concept of classes (like in C++/Java) and these classes should be cross-referencable in other parts of the language (like for instance as a parameter to a function, like in Java and C++).

Now the tricky part is that the language should come with some predefined objects (those are not basic types, like int, double, but real existing classes). I was thinking that a way to solve this issue would be to distribute a model file containing the declaration of these classes inside my grammar jar file and make the scope provider somehow aware of this file. However, I have no clue how to implement this properly (or even if that is the proper way of doing this).

Any tips on how this could be achieved?

Thanks
Re: Allow cross-references to "built-in" types [message #1851664 is a reply to message #1851662] Tue, 12 April 2022 07:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
hi, is this about c++ source code only?
if yes you could write a resourceserviceprovider for c++ files and do some (lightweigt) parsing


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Allow cross-references to "built-in" types [message #1851665 is a reply to message #1851664] Tue, 12 April 2022 07:06 Go to previous messageGo to next message
Rubén Porras Campo is currently offline Rubén Porras CampoFriend
Messages: 67
Registered: July 2009
Member
if you want to create a model file containing built-ins, you might want to look at https://www.eclipse.org/forums/index.php/m/1850460/#msg_1850460 for how we do built-ins in the DDK
Re: Allow cross-references to "built-in" types [message #1851666 is a reply to message #1851664] Tue, 12 April 2022 07:16 Go to previous messageGo to next message
Cédric Moonen is currently offline Cédric MoonenFriend
Messages: 14
Registered: December 2014
Junior Member
Christian Dietrich wrote on Tue, 12 April 2022 07:04
hi, is this about c++ source code only?
if yes you could write a resourceserviceprovider for c++ files and do some (lightweigt) parsing


Thanks for your reply. Sorry, I think my question was maybe not that clear. I do not want to reference C++ classes from my grammar, I simply wish to have some default types to be available for the end user to be able to cross-reference them.

Suppose a simple grammar (reducing it to the bar minimum to illustrate what I want to achieve):
Class:
	'class' name=ID
	'{'
		('superclass' superclass=[Class])? 
	'}'
;


So, the user will be able to declare classes and optionally derive them from other classes. I would like to provide some built-in types to the user that he can derive his classes from. So, he should be able to cross-reference objects which are somehow provided by myself (e.g. in the jar file or through any other means).
Re: Allow cross-references to "built-in" types [message #1851667 is a reply to message #1851665] Tue, 12 April 2022 07:31 Go to previous messageGo to next message
Cédric Moonen is currently offline Cédric MoonenFriend
Messages: 14
Registered: December 2014
Junior Member
Rubén Porras Campo wrote on Tue, 12 April 2022 07:06
if you want to create a model file containing built-ins, you might want to look at https://www.eclipse.org/forums/index.php/m/1850460/#msg_1850460 for how we do built-ins in the DDK


Thanks, I had a look at the links you shared but I'm not too sure about this. I'll need to go into a bit more details before I can understand what you did there. It seems pretty complex for what I want to achieve though...
Re: Allow cross-references to "built-in" types [message #1851678 is a reply to message #1851667] Tue, 12 April 2022 11:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
if the list is hardcoded, why no either hardcode them in grammar or
add a classpath library to the projects
(dont know if your model projects are java or plguin projects


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Allow cross-references to "built-in" types [message #1851680 is a reply to message #1851678] Tue, 12 April 2022 11:29 Go to previous messageGo to next message
Rubén Porras Campo is currently offline Rubén Porras CampoFriend
Messages: 67
Registered: July 2009
Member
if you hardcode them in the grammar, then they will become keywords and this have unwanted side-effects (more complex parser, also you cannot use them as identifiers at other places,...). Depending on other restrictions the project might have, that could be an issue.

For our DSLs, that is the reason why do not model build-ins hardcoded in the grammar.
Re: Allow cross-references to "built-in" types [message #1851687 is a reply to message #1851680] Tue, 12 April 2022 12:36 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

For some languages such as C all built-in types such as "int" are reserved words so hardcoding them in the grammar is not too awful, perhaps even good.

Arguably this is true for Java too, but obviously you might like to make e.g. "String" accessible in which case you should recognize that the underlying structure is such that you have a possibly free import of the library definition of java.lang.String.

If you need more than hardwired names, I recommend extending any import mechanism to allow an import of your built-in library.

Regards

Ed Willink
Re: Allow cross-references to "built-in" types [message #1851689 is a reply to message #1851687] Tue, 12 April 2022 13:21 Go to previous messageGo to next message
Cédric Moonen is currently offline Cédric MoonenFriend
Messages: 14
Registered: December 2014
Junior Member
Ed Willink wrote on Tue, 12 April 2022 12:36

If you need more than hardwired names, I recommend extending any import mechanism to allow an import of your built-in library.


This is the case in which I am: I want to provide some exiting types which are not C++ basic types (like int, double, ...) but objects of type "Class" (which is a type defined in my grammar).
I'm able to include a model file in my jar file which defines those types and to load this file at runtime (I get the proper IResource and I have access to the Classes within the resource). However, the link which I'm missing is to somehow make the scope provider aware of this resource. There's some documentation here (https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping) but I'm not understanding everything.

So, this is probably something very simple but there's something I am missing here...
Re: Allow cross-references to "built-in" types [message #1851691 is a reply to message #1851689] Tue, 12 April 2022 14:00 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
It seems to me that if it doesn't cause ambiguity, you can explicitly allow a keyword to be an identifier too:

ValidID:
ID | 'keyword'


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Allow cross-references to "built-in" types [message #1851692 is a reply to message #1851691] Tue, 12 April 2022 14:08 Go to previous messageGo to next message
Rubén Porras Campo is currently offline Rubén Porras CampoFriend
Messages: 67
Registered: July 2009
Member
Yes, you could do that, but in complex grammars it can, as you say cause ambiguities. It can also create parses which are too big to handle in the limits of java for methods. We run into both of them several times.

Of course, for simple grammars with not many built-ins, using keywords is the easiest solution.
Re: Allow cross-references to "built-in" types [message #1851698 is a reply to message #1851692] Tue, 12 April 2022 17:09 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Since you probably have a relatively stable language, your built-in types are quite stable so firing up the Xtext parser at run-time imposes an unnecessary additiional overhead for users.

For a small number of types you could therefore manually code the construction of a parsed built-ins model.

For OCL and QVTd, I auto-generate the construction of the parsed built-in library model so that the parsing occurs at build time. Users get to use a much faster load. See for instance [1] and associated files. (NB OCL has an additional complexity of distinct Concrete and Abstract Syntax models so don't use the example for more than inspiration.)

Regards

Ed Willink

[1] https://git.eclipse.org/r/plugins/gitiles/ocl/org.eclipse.ocl/+/refs/heads/master/examples/org.eclipse.ocl.examples.build/src/org/eclipse/ocl/examples/build/xtend/GenerateOCLstdlibXtend.xtend
Re: Allow cross-references to "built-in" types [message #1851713 is a reply to message #1851698] Wed, 13 April 2022 13:08 Go to previous messageGo to next message
Cédric Moonen is currently offline Cédric MoonenFriend
Messages: 14
Registered: December 2014
Junior Member
Hi, thanks for your reply, but it still not clear to me how these objects would be available in the index (or whatever you call it so that they can be referenced by the user and appear in the auto-completion list).

I can also build these objects at runtime myself if performance is an issue, but like I said, parsing or creating those objects is not my issue. Once again, my real problem is how can I populate the indexer with resources/objects which are created programmatically?
Re: Allow cross-references to "built-in" types [message #1851714 is a reply to message #1851713] Wed, 13 April 2022 13:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
again the question: how do your project look like. what kind of project are these

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Allow cross-references to "built-in" types [message #1851715 is a reply to message #1851714] Wed, 13 April 2022 13:29 Go to previous messageGo to next message
Cédric Moonen is currently offline Cédric MoonenFriend
Messages: 14
Registered: December 2014
Junior Member
So far, it is the default Xtext projects which are generated by the Eclipse Wizard with the default options. I'm currently testing my code using an Eclipse Application as runtime configuration.
The final goal is to deploy my plug-ins into an existing RCP application and for that, I will have to somehow populate the index myself (the model files will be stored somewhere on disk and I will have to feed the indexer with these files). However, I'm not very familiar with this part of Xtext and I'm trying to understand things one step at a time.
Re: Allow cross-references to "built-in" types [message #1851718 is a reply to message #1851715] Wed, 13 April 2022 14:08 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
i mean the project where you put the
.mydsl files to


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Allow cross-references to "built-in" types [message #1851719 is a reply to message #1851718] Wed, 13 April 2022 14:32 Go to previous messageGo to next message
Cédric Moonen is currently offline Cédric MoonenFriend
Messages: 14
Registered: December 2014
Junior Member
You mean in my runtime Eclipse instance? If yes, for now I created a Java project in which I store my language files. Does this make a difference actually? (sorry if this is a dumb question, but like I said I'm not familiar with that at all)
Re: Allow cross-references to "built-in" types [message #1851721 is a reply to message #1851719] Wed, 13 April 2022 15:07 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14664
Registered: July 2009
Senior Member
Then put you lib mydsl files in a jar and the jar on the classpath of that project

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Xtext 2.27.0.M1 is out
Next Topic:Packages does not exist from Xtext Maven Example
Goto Forum:
  


Current Time: Tue Apr 16 10:49:26 GMT 2024

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

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

Back to the top