Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » scoping, auto complete and validation (what is the proper way to decide when to use which and how to create recursive references to instances)
scoping, auto complete and validation [message #1312613] Thu, 24 April 2014 11:51 Go to next message
Yoel Pinhas is currently offline Yoel PinhasFriend
Messages: 17
Registered: October 2013
Junior Member
Hi


I have created a simple grammar using xtext called unit. The main concept is that everything is a unit. Units are constructed from units (Struct/Class equivalent). A unit value is pair of a 'unit type reference' and an 'instance name'.

primitives are a special case of units and this allows me to have a unified unit system where everything is a unit Smile

for more details please look at full grammar and full code snippet attached below

so, everything works great... well, almost everything
I'm having trouble with the validation/scoping/autocomplete of the following code:
a = new Point { x = this.line0.a.x, y = this.line0.a.y }, 

specifically my problem is with 'this.line0.a.x'

The grammar rule I used is this:
UnitThisValueReference:
	'this' '.' next=UnitRecursiveValueReference;
UnitRecursiveValueReference:
	prev=[UnitValue] ('.' next=UnitRecursiveValueReference)?;


I have used the following scoping methods to reduce list of unit values in the 'this' rule to values that only belong to this. And a scoping method to reduce unit values in case of recursive ref, and suggest the values for next from the values in prev
class UnitScopeProvider extends AbstractDeclarativeScopeProvider {

	// scope for values inside previous reference
	def IScope scope_UnitRecursiveValueReference_next(UnitRecursiveValueReference reference, EReference erefence) {
		Scopes::scopeFor(reference.prev.type.body.values)
	}
	
	// find the 'this' unit and scope for all values in it
	def IScope scope_UnitThisValueReference_next(UnitThisValueReference reference, EReference erefence) {
		var eContainer = reference.eContainer
		while ((!(eContainer instanceof Unit) && (eContainer != null))) {
			eContainer = eContainer.eContainer			
		}
		if (eContainer != null && (eContainer instanceof Unit)) {
			var Unit thisUnit = eContainer as Unit;
			Scopes::scopeFor(thisUnit.body.values)		
		} else {
			null
		}
	}	

}


When i try to auto complete 'this.' I get: [a, b, line0, line1 and line2, x , y]. x, y belongs to Point. I get the same autocomplete list after every '.'


  1. Is there something wrong with my grammar rules?
  2. Should I use validate and autocomplete to reduce suggestions and make sure everything is valid?


Thanks in advance,
Yoel.


==========


Full example snippet using unit dsl:
package altair.example

import altair.example.Point.*

// primitive unit definitions
int unit Int {
	val Int value
} 

unit Point {
	val Int x
	val Int y
}

unit Line {
	val Point a
	val Point b
}

unit Lines {
	
	val Point a = new Point { x = 0, y = 0 }	
	val Point b = new Point { x = 1, y = 1 }	

	val Line line0 = new Line { a = this.a, b = this.b }

	val Line line1 = new Line {
		a = new Point { x = 0, y = 1 }, 
		b = new Point { x = 2, y = 1 }
	}

	val Line line2 = new Line {
		a = new Point { x = this.line0.a.x, y = this.line0.a.y }, 
		b = new Point { x = new Point {	x = 0, y = 1 }, y = this.b }
	}
		
}


Full grammar for unit dsl
grammar altair.lang.xtext.unit.Unit with org.eclipse.xtext.common.Terminals

import -You can only use links to eclipse.org sites while you have fewer than 5 msgs-
generate unit -You can only use links to eclipse.org sites while you have fewer than 5 msgs-

UnitModel:
	'package' name=QualifiedName
	imports+=Import*
	units+=Unit*;

QualifiedName:
	ID ('.' ID)*;

QualifiedNameWithWildcard: QualifiedName '.' '*';

Import:
	'import' (importedNamespace=QualifiedNameWithWildcard | importedNamespace=QualifiedName);

BOOLEAN returns ecore::EBoolean:
	'true' | 'false';

enum PrimitiveModifier:
	INT='int' | BOOLEAN='boolean' | STRING='string';

UnitModifier:
	{UnitModifier} ((abstract?='abstract'? & static?='static'?) & (literal=PrimitiveModifier)?);

Unit:
	modifier=UnitModifier 'unit' name=ID ('extends' supers+=[Unit] (',' supers+=[Unit])*)? '{' body=UnitBody '}';

UnitBody:
	{UnitBody} values+=UnitValue*;

UnitValue:
	'val' type=[Unit] (array?=('[]'))? name=ID ('=' ctor=UnitCtor)?;

UnitCtor:
	UnitLiteralCtor | UnitNewCtor | UnitCopyCtor | UnitCtorList;

UnitLiteralCtor:
	int=INT | boolean=BOOLEAN | str=STRING; // only for primitive unit types
UnitNewCtor:
	'new' type=[Unit] '{' (assginments+=UnitParameterizedAssignment ','?)* '}';

UnitParameterizedAssignment:
	parameter=[UnitValue] '=' ctor=UnitCtor;

UnitCopyCtor:
	UnitThisValueReference | UnitStaticValueReference;

UnitThisValueReference:
	'this' '.' next=UnitRecursiveValueReference;

// static units will be checked in validation
UnitStaticValueReference:
	static=[Unit] '.' next=UnitRecursiveValueReference;

UnitRecursiveValueReference:
	prev=[UnitValue] ('.' next=UnitRecursiveValueReference)?;

UnitCtorList:
	'new' type=[Unit] '[]' '{' (list+=UnitCtor (',' list+=UnitCtor)*)? '}';
Re: scoping, auto complete and validation [message #1312750 is a reply to message #1312613] Thu, 24 April 2014 13:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

did you have a look at http://christiandietrich.wordpress.com/2013/05/18/xtext-and-dot-expressions/


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: scoping, auto complete and validation [message #1312876 is a reply to message #1312750] Thu, 24 April 2014 15:03 Go to previous messageGo to next message
Yoel Pinhas is currently offline Yoel PinhasFriend
Messages: 17
Registered: October 2013
Junior Member
Hi,

Thank you for the link, it's exactly what i was looking for Smile

DotExpression returns Ref:
	EntityRef ({DotExpression.ref=current} '.' tail=[Feature])*;

Can you kindly point me to a tutorial/document that elaborate about 'current' and about the part in the grammar rule inside the '{' '}'?

Re: scoping, auto complete and validation [message #1312917 is a reply to message #1312876] Thu, 24 April 2014 15:31 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
It is called assigned and unassigned actions and can be found in the
docs that ship with xtext

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: scoping, auto complete and validation [message #1313319 is a reply to message #1312917] Thu, 24 April 2014 20:46 Go to previous message
Yoel Pinhas is currently offline Yoel PinhasFriend
Messages: 17
Registered: October 2013
Junior Member
thanks
Previous Topic:Integrate xml into my dsl ('hidden' transformation)
Next Topic:Horstmann style formatter
Goto Forum:
  


Current Time: Sat Apr 20 02:10:55 GMT 2024

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

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

Back to the top