Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Providing type for keywords(The topic is in general concerned with typing and scoping mechanism of Xtext 2.9.)
Providing type for keywords [message #1731047] Mon, 02 May 2016 14:38 Go to next message
Marina Andric is currently offline Marina AndricFriend
Messages: 7
Registered: March 2016
Junior Member
I am learning/trying to implement scoping for my dsl, which is small java-like one. I have two keywords I need to provide a type and consecutively scope for, one is this and the other is here.

In the TypeProvider, type of this is computed as the containing class, and the scope is members of the class. The problem is to compute the type of here, which should be a class contained in the small library I wrote for my dsl. I don't know how to 'link' the two.

Can someone please help? Thanks a lot
Re: Providing type for keywords [message #1731049 is a reply to message #1731047] Mon, 02 May 2016 14:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
do you use xbase or not? how does your grammar look like?
this the keyword itself a cross reference or not? .....

which type system do you use?
....


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Mon, 02 May 2016 14:44]

Report message to a moderator

Re: Providing type for keywords [message #1731050 is a reply to message #1731049] Mon, 02 May 2016 14:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
p.s:

simply asking the delegating scope provider for the "here" thing does not work?


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Providing type for keywords [message #1731064 is a reply to message #1731049] Mon, 02 May 2016 15:28 Go to previous messageGo to next message
Marina Andric is currently offline Marina AndricFriend
Messages: 7
Registered: March 2016
Junior Member
Hi Christian,
Thanks a lot for your responsiveness.

I am not using xbase.
My grammar is similar to this one
https://github.com/LorenzoBettini/packtpub-xtext-book-examples/blob/master/org.example.smalljava/src/org/example/smalljava/SmallJava.xtext
with only a few additions.
I was basically copying the approach applied in that book example, until I ran into the problem of providing the type of 'here'.

I've just googled, are you suggesting I should in some way use IDelegatingScopeProvider ?
Re: Providing type for keywords [message #1731065 is a reply to message #1731064] Mon, 02 May 2016 16:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
i mean something like

package org.example.smalljava.typing

import com.google.common.base.Predicates
import com.google.inject.Inject
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EcoreFactory
import org.eclipse.xtext.naming.QualifiedName
import org.eclipse.xtext.scoping.IGlobalScopeProvider
import org.example.smalljava.lib.SmallJavaLib
import org.example.smalljava.smallJava.SJAssignment
import org.example.smalljava.smallJava.SJBoolConstant
import org.example.smalljava.smallJava.SJClass
import org.example.smalljava.smallJava.SJExpression
import org.example.smalljava.smallJava.SJHere
import org.example.smalljava.smallJava.SJIntConstant
import org.example.smalljava.smallJava.SJMemberSelection
import org.example.smalljava.smallJava.SJMethod
import org.example.smalljava.smallJava.SJNew
import org.example.smalljava.smallJava.SJNull
import org.example.smalljava.smallJava.SJReturn
import org.example.smalljava.smallJava.SJStringConstant
import org.example.smalljava.smallJava.SJSuper
import org.example.smalljava.smallJava.SJSymbolRef
import org.example.smalljava.smallJava.SJThis
import org.example.smalljava.smallJava.SJVariableDeclaration
import org.example.smalljava.smallJava.SmallJavaFactory
import org.example.smalljava.smallJava.SmallJavaPackage

import static extension org.example.smalljava.util.SmallJavaModelUtil.*
import org.eclipse.emf.ecore.util.EcoreUtil

class SmallJavaTypeProvider {
	
	@Inject extension SmallJavaLib
	
	@Inject IGlobalScopeProvider globalScopeProvider
	
	val ep = SmallJavaPackage::eINSTANCE
	
	public static val stringType = 
		SmallJavaFactory::eINSTANCE.createSJClass => [name = 'stringType']
	public static val intType = 
		SmallJavaFactory::eINSTANCE.createSJClass => [name = 'intType']
	public static val booleanType = 
		SmallJavaFactory::eINSTANCE.createSJClass => [name = 'booleanType']

	public static val nullType = 
		SmallJavaFactory::eINSTANCE.createSJClass => [name = 'nullType']
	
	def typeFor(SJExpression e) {
		switch (e) {
			SJThis : e.containingClass
			SJHere : e.here
			SJSuper : e.containingClass.getSuperclassOrObject
			SJSymbolRef: e.symbol?.type
			SJNew: e.type
			SJMemberSelection: e.member?.type
			SJNull: nullType
			SJStringConstant: stringType
			SJIntConstant: intType
			SJBoolConstant: booleanType
		}
	}
	
	def SJClass getHere(EObject e) {
		var result = globalScopeProvider.getScope(e.eResource, EcoreFactory.eINSTANCE.createEReference => [EType = SmallJavaPackage.Literals.SJ_CLASS] , Predicates.alwaysTrue).getSingleElement(QualifiedName.create("Here")).EObjectOrProxy as SJClass
		return EcoreUtil.resolve(result, e.eResource.resourceSet) as SJClass
	}

	def expectedType(SJExpression e) {
		val c = e.eContainer
		val f = e.eContainingFeature
		switch (c) {
			SJVariableDeclaration case f == ep.SJVariableDeclaration_Expression : 
				c.type
			SJAssignment case f == ep.SJAssignment_Right : 
				c.left.typeFor
			SJReturn case f == ep.SJReturn_Expression :
				c.containingMethod.type
			SJMemberSelection case f == ep.SJMemberSelection_Args : {
				// assume that it refers to a method and that there
				// is a parameter corresponding to the argument
				try {
					(c.member as SJMethod).params.get(c.args.indexOf(e)).type
				} catch (Throwable t) {
					null // otherwise there is no specific expected type
				}
			}
			case f == ep.SJIfStatement_Expression: booleanType
		}
	}

	def argsTypesAsStrings(SJMemberSelection sel) {
		"(" + sel.args.map[typeFor?.name].join(", ") + ")"
	}

	def isPrimitive(SJClass c) {
		c.eResource == null
	}
}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Providing type for keywords [message #1731067 is a reply to message #1731065] Mon, 02 May 2016 16:33 Go to previous messageGo to next message
Marina Andric is currently offline Marina AndricFriend
Messages: 7
Registered: March 2016
Junior Member
No Message Body
Re: Providing type for keywords [message #1731071 is a reply to message #1731067] Mon, 02 May 2016 16:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
?

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Providing type for keywords [message #1731075 is a reply to message #1731067] Mon, 02 May 2016 17:12 Go to previous messageGo to next message
Marina Andric is currently offline Marina AndricFriend
Messages: 7
Registered: March 2016
Junior Member

I tried this solution and it works nicely when the class 'Here' is in the same source folder, like other user-defined classes.
In my case, the class 'Here' is defined in another package. Only if I type 'here' explicitly in the test program, say, write 'val here:Here;' then I'm able to select members define in 'Here'.

(When I created an eclipse project for my dsl I added as dependency the project where my package is defined).

Should there be some code to suggest to look for the class definition in the package? Perhaps in the ScopeProvider..

Thanks a lot for your wisdom and time Smile
Re: Providing type for keywords [message #1731076 is a reply to message #1731075] Mon, 02 May 2016 17:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
sry please share all needed to reproduce

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Providing type for keywords [message #1731100 is a reply to message #1731076] Mon, 02 May 2016 18:27 Go to previous messageGo to next message
Marina Andric is currently offline Marina AndricFriend
Messages: 7
Registered: March 2016
Junior Member
I uploaded my code here:
https://github.com/marinamygithub/xtext_project/tree/master/org.xtext.example.ADSL

In my case 'here' should be of type Place, which is a class defined in main.adsl
Re: Providing type for keywords [message #1731101 is a reply to message #1731100] Mon, 02 May 2016 18:32 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
Yes but I don't see why you cannot adopt this from my example. I ask the scope for a sjclass called here you can ask it for a sjclass called place

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Providing type for keywords [message #1731102 is a reply to message #1731101] Mon, 02 May 2016 18:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
p.s.

did you test the "Qualified name" of Here? maybe you have to ask the scope for

full.qualified.name.Here


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Providing type for keywords [message #1731103 is a reply to message #1731102] Mon, 02 May 2016 18:43 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13186
Registered: July 2009
Senior Member
Thus QualifiedName.create("full","qualified","name","Here")

Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Providing type for keywords [message #1731108 is a reply to message #1731103] Mon, 02 May 2016 20:44 Go to previous message
Marina Andric is currently offline Marina AndricFriend
Messages: 7
Registered: March 2016
Junior Member
yes, that was it Smile

Thanks so much again.. I learned a few new things thanks to you.


Previous Topic:eliminating left recursion
Next Topic:Project Wizard for xtext Project
Goto Forum:
  


Current Time: Thu Nov 14 08:42:18 GMT 2019

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

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

Back to the top