Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Inheritance in XText
Inheritance in XText [message #676842] Mon, 06 June 2011 17:34 Go to next message
Simon Stratmann is currently offline Simon StratmannFriend
Messages: 27
Registered: February 2011
Junior Member
Hi,

in my language I have a type declaration that looks a bit like:
type myType is
a: integer;
b: integer;
end type;

signal x: myType;

use x.a; //this is the tricky part




I looked in the documentation but didn't find any clues on how to achieve this. a and b aren't seen as exported child objects of x.
Any help would be appreciated.

Thanks,

Simon
Re: Inheritance in XText [message #676856 is a reply to message #676842] Mon, 06 June 2011 18:09 Go to previous messageGo to next message
Andreas   is currently offline Andreas Friend
Messages: 8
Registered: April 2011
Junior Member
Hi,

checkout the part about Local Scoping in the Documentation:

http://www.eclipse.org/Xtext/documentation/1_0_1/xtext.html#scoping

Best Regards

Andreas
Re: Inheritance in XText [message #676862 is a reply to message #676856] Mon, 06 June 2011 18:47 Go to previous messageGo to next message
Simon Stratmann is currently offline Simon StratmannFriend
Messages: 27
Registered: February 2011
Junior Member
I did (repeatedly), but I am not sure there's anything that might help me.
Re: Inheritance in XText [message #676873 is a reply to message #676862] Mon, 06 June 2011 19:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

without having your grammar it is hard to give you precise advice. if you want to refernce "things" in Xtext these things must have an attribute "name". If this is not the case you have to fix this using a qualified name provider. you often may want to restrict the things visible by some criteria. in your case you want only to see the attributes of myType and not these of other type.

lets asume you have following grammar
Model:
	typs+=Type*
	signals+=Signal*
	usages+=Usage*
;

enum Datatype : string | integer | boolean;

Type:
	"type" name=ID "is"
		attributes+=Attribute*
	"end" "type" ";";

	Attribute: id=ID ":" type=Datatype ";";
	
Signal: "signal" name=ID ":" type=[Type] ";";

Usage: "use" signal=[Signal] "." attr=[Attribute] ";";


then a QualifiedNameProvider (bound in the Runtime Module)
public class MyQNP extends DefaultDeclarativeQualifiedNameProvider {

	public String qualifiedName(Attribute a) {
		return a.getId();
	}
	
}


and a scopeprovider
public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {

	public IScope scope_Usage_attr(Usage u, Attribute a) {
		return Scopes.scopeFor(u.getSignal().getType().getAttributes());
	}
	
}


might solve your problem Wink

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance in XText [message #677100 is a reply to message #676873] Tue, 07 June 2011 15:14 Go to previous messageGo to next message
Simon Stratmann is currently offline Simon StratmannFriend
Messages: 27
Registered: February 2011
Junior Member
Christian,

thanks for your help! I realize not posting the grammar might make the problem somewhat abstract, but having a pretty complex grammar with more than 1200 lines I thought posting it or even parts of it might confuse more than help. The real grammar is pretty much what you deducted from my example, but I refer to the variable by a FQN. SO I tried your approach with something like this:

Signal:
"signal" name=IDENTIFIER;
QualifiedName:
IDENTIFIER("."IDENTIFIER);
Usage: "use" signal=[Signal|QualifiedName] ("." attr=[Attribute|IDENTIFIER])? ";";

At the same place there can either be a referrance like "use work.a" or "a" (with an ImportScope) but also like "work.a.attribute1".
Because of the QualifiedName-Rule when looking for candidates for the Usage target it will look for something with the QualifiedName "work.a.attribute1" instead of "work.a" and then something with the simple name "attribute1".

Part of my grammar:
refToSimpleName:
	refersTo=[Referrable|QualifiedName]("." rt=record_type)?;

QualifiedName:
	IDENTIFIER ("." IDENTIFIER)*;

record_type:
	name=IDENTIFIER;	
	
record_type_definition:
	"record" ED+=record_type_list+ "end" "record" (ends_record=refToSimpleName)?;
	
record_type_list :
records+=record_type ("," records+=record_type)*	":" ESD=element_subtype_definition ";";

variable_declaration:
	("shared")? "variable" variables+=variable_name ("," variables+=variable_name)* ":"
	variable_subtype=subtype_indication (":=" E=expression)? ";";

variable_name:
	name=IDENTIFIER;

Referrable:
variable_name;

[Updated on: Tue, 07 June 2011 15:16]

Report message to a moderator

Re: Inheritance in XText [message #677124 is a reply to message #677100] Tue, 07 June 2011 16:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

can't you simply remove the ambiguity from your grammar?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Inheritance in XText [message #677135 is a reply to message #677124] Tue, 07 June 2011 16:44 Go to previous messageGo to next message
Simon Stratmann is currently offline Simon StratmannFriend
Messages: 27
Registered: February 2011
Junior Member
Not sure what you mean. If you mean changing the language, nope, it's an already existing language which I need to write an editor for. I've already made a lot of compromises (like not differentiating between between constants and variables and functions, hence the type Referrable).
I'd even go with something like
refersTo=[Referrable|QualifiedName]("." foobar=STRING)?;
or so, but the ? in QualifiedName consumes the whole string and doesn't "leave" anything for the foobar part.
Re: Inheritance in XText [message #677156 is a reply to message #677135] Tue, 07 June 2011 18:12 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

what i mean is that the grammar (modified to Usage: "use" signal=[Signal|FQN] ("." attr=[Attribute])? ";"; is ambigous. so what you have here is i want to refernce to a signal or to an attribute (qualified by the signal)
so my grammar works when changed to (similar as your refferable)
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"

Model:
	typs+=Type*
	signals+=Signal*
	usages+=Usage*
;

FQN: ID ("." ID)*;

enum Datatype : string | integer | boolean;

Type:
	"type" name=ID "is"
		attributes+=Attribute*
	"end" "type" ";";

	Attribute: id=ID ":" type=Datatype ";";
	
Signal: "signal" name=FQN ":" type=[Type] ";";

SignalOrAttribute : Signal | Attribute;

Usage: "use" {Usage} ref=[SignalOrAttribute|FQN] ";";


and fixing the local scoping

public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
	
	@Inject
	IQualifiedNameProvider nameProvider;

	public IScope scope_Usage_ref(Usage u, EReference r) {
		List<IEObjectDescription> objects = new ArrayList<IEObjectDescription>();
		Model m = EcoreUtil2.getContainerOfType(u, Model.class);
		for (Signal s : m.getSignals()) {
			objects.add(EObjectDescription.create(nameProvider.getQualifiedName(s), s));
			for (Attribute a : s.getType().getAttributes()) {
				objects.add(EObjectDescription.create(nameProvider.getQualifiedName(s) + "." + a.getId(), a));
			}
		}
		return new SimpleScope(IScope.NULLSCOPE, objects);
	}
	
}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:EClass from Ecore model in xtext runtime editor
Next Topic:navigating through an importURI statement
Goto Forum:
  


Current Time: Thu Apr 25 05:14:41 GMT 2024

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

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

Back to the top