Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to Write Grammer For Function Defination(Grammer Writing Help)
How to Write Grammer For Function Defination [message #754745] Fri, 04 November 2011 07:52 Go to next message
Zeeshan Safder is currently offline Zeeshan SafderFriend
Messages: 27
Registered: November 2011
Junior Member
Hi,

I like to write a grammer that accept a function with any no of parameters. Where I have stuck is how to specify the length of parameter? Dont want parameter names to be repeat.

Function:
'$'?name = ValidIdentifier ':' 'function''('
( ValidIdentifier (','ValidIdentifier)* )?
')''{'
(feature += FunctionFeatures)*
'}'
;

this grammer will accept function with parameters, but it does not restricts the duplicate parameter name.

Kindly help me writing the right grammer

Thanks
Zeeshan Safder
Re: How to Write Grammer For Function Defination [message #754749 is a reply to message #754745] Fri, 04 November 2011 07:58 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

this is nothing to put into the grammar. Ensure these semantic constraints using validation. Also, you should probably store the parameter names in the model.

Function:
'$'?name = ValidIdentifier ':' 'function''('
( parameters+=ValidIdentifier (',' parameters+=ValidIdentifier)* )?
')''{'
(feature += FunctionFeatures)*
'}'
;

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: How to Write Grammer For Function Defination [message #758440 is a reply to message #754749] Wed, 23 November 2011 10:16 Go to previous messageGo to next message
Zeeshan Safder is currently offline Zeeshan SafderFriend
Messages: 27
Registered: November 2011
Junior Member
hi,
This is my langauge

Domainmodel :
(elements += Type
| variables+=VariableDeclaration
| actions+=ActionStatement)*
;

Type:
ClassDeclaration
;


ClassDeclaration returns classs:
name = ID '=' '{'
(feature += Feature)*
'}'
;
Feature:
Function
;

Function returns function:
name = ID ':' 'function''(' ( parameters+=ID (','parameters+=ID)* )? ')''{'

'}'
;

VariableDeclaration:
'var' name=ID '=' 'new' resource=[classs] '('')'
;

ResourcePathHead:
variable=[VariableDeclaration]
;

ActionStatement:
path=ResourcePathHead '.' action=[function]
;

And this is the simple 'AbstractDeclarativeScopeProvider' extended class method

public IScope scope_ActionStatement_action(final ActionStatement context, EReference ref) {
//get the tail end of the resource path
classs tailResource = context.getPath().getVariable().getResource();
return Scopes.scopeFor (tailResource.getFeature());
}


Suppose the follwing language code

a = {
abc: function(t1, t2){

}
xyz: function(){

}
zst: function(){

}
}

var ab = new a()

Now when I do
'ab.' it will list all functions defined. But I want more information. It must show how many parameter it receives.
Currenlt 'ab.' will assist 'abc', 'xyz', 'zst' it should assist like 'abs(t1,t2)', 'xyz()' etc

For that point I just come to know that I have to implement a custom IScope Providor, but have got the idea how to implement it. After almost 5 hrz of google, I have decided to post here and hope will got the best possible help...
Re: How to Write Grammer For Function Defination [message #758460 is a reply to message #758440] Wed, 23 November 2011 11:14 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
You should have spent those 5 hours reading and understanding the Xtext User Guide and the (simpler) examples shipping with Xtext! In which case, you probably would have come across code templates.

Re: How to Write Grammer For Function Defination [message #758474 is a reply to message #758460] Wed, 23 November 2011 12:06 Go to previous messageGo to next message
Zeeshan Safder is currently offline Zeeshan SafderFriend
Messages: 27
Registered: November 2011
Junior Member
I was expecting help but Meinte Boersma, your reply totaly discourage my efforts! If every1 can easy understood all documentation, then why there is need of such help communities. In documentation they talk in abstract manner. Can you please give some better help...
Re: How to Write Grammer For Function Defination [message #758478 is a reply to message #758474] Wed, 23 November 2011 12:20 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Well, then I can only conclude that you are easily discouraged... Obviously, a lot of stuff is not explicitly covered by the User's Guide which is indeed where the community could help. However, about 90% of your question material is covered by the User Guide. At the same time, your knowledge of Xtext makes it quite hard to help you as we have to explain quite a lot of stuff from scratch (which, again, is covered by the User Guide).

Communities exist to help you with your work, not do the work for you (in spite of Christian's efforts!). I could do the work for you too, by the way: http://www.dslconsultancy.com/ You see, it's my living and there's no economical point in doing your work for you for free.

Strange: googling for 5 hours is OK, but apparently spending the same amount of time really trying to understand something to some depth is not OK?


Re: How to Write Grammer For Function Defination [message #758489 is a reply to message #758478] Wed, 23 November 2011 12:49 Go to previous messageGo to next message
Zeeshan Safder is currently offline Zeeshan SafderFriend
Messages: 27
Registered: November 2011
Junior Member
OK I have spended or wasted so much time on google, but now is there some easy way to accomplish this task and basics will cover later some time.
Re: How to Write Grammer For Function Defination [message #758496 is a reply to message #758489] Wed, 23 November 2011 13:09 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Your very generic question "I just come to know that I have to implement a custom IScope Providor, but have got the [-no?] idea how to implement it" can only be sensibly answered by pointing you to the User Guide and the tutorials. You need to understand those basics before we can even explain any "easy way". Likewise, we can point to an earlier solution along the same lines but you'd be hard-pressed to even recognize it as such, given your lack of basic understanding of Xtext.

There's no such thing as a free lunch: to receive help you've got to show that you're willing to receive it and be prepared to pour effort in it as well.


Re: How to Write Grammer For Function Defination [message #758505 is a reply to message #758496] Wed, 23 November 2011 13:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

did you try Meintes solution?

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to Write Grammer For Function Defination [message #758510 is a reply to message #758505] Wed, 23 November 2011 14:04 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Btw, dou you want to change the display string in the proposal?
or the text that will be pasted too?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to Write Grammer For Function Defination [message #758644 is a reply to message #758510] Thu, 24 November 2011 04:09 Go to previous messageGo to next message
Zeeshan Safder is currently offline Zeeshan SafderFriend
Messages: 27
Registered: November 2011
Junior Member
just want to change the display string
Re: How to Write Grammer For Function Defination [message #758656 is a reply to message #758644] Thu, 24 November 2011 06:48 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

if you would have (as i told you) had a look at YourDslProposalProvider + read the docs you would have found

(1) in AbstractMyDslProposalProvider there is a completeActionStatement_Action method
(2) this method calls lookupCrossReference
(3) following some refs you would have found that the actual work is done in DefaultProposalCreator .
(4) this calls getStyledDisplayString(IEObjectDescription description)

first you have to care that the IEObjectDescriptions contain the infos you need
this could look like

public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
	
	public IScope scope_ActionStatement_action(final ActionStatement context, EReference ref) {
		//get the tail end of the resource path
		classs tailResource = context.getPath().getVariable().getResource();
		List<IEObjectDescription> scope = new ArrayList<IEObjectDescription>();
		for (Feature o : tailResource.getFeature()) {
			if (o instanceof function) {
				function f = (function) o;
				Map<String, String> data = new HashMap<String, String>();
				String sig = "(" + StringUtils.join(f.getParameters(), ",")+ ")";
				data.put("sig", sig);
				IEObjectDescription d = EObjectDescription.create(QualifiedName.create(f.getName()), f, data);
				scope.add(d);
			}
		}
		return new SimpleScope(scope);
		}

}


and then make use of it

public class MyDslProposalProvider extends AbstractMyDslProposalProvider {
	
	@Override
	protected StyledString getStyledDisplayString(
			IEObjectDescription description) {
		if (description.getEClass().equals(MyDslPackage.Literals.FUNCTION)) {
			return new StyledString(description.getName().toString() + (description.getUserData("sig") == null ? "" : description.getUserData("sig")));
		}
		return super.getStyledDisplayString(description);
	}

}


in a quick and dirty solution you may have overwritten getStyledDisplayString(Eobject element ...
this is easy even for a beginner that trys to get the xtext black belt at his first day


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to Write Grammer For Function Defination [message #758668 is a reply to message #758656] Thu, 24 November 2011 07:49 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Christian, I'm always completely amazed by the ludicrously detailed answers you give, the speed with which you give them and the amount of effort and time you have to put into them. I hope it's not pearls before swine, in this case.

Re: How to Write Grammer For Function Defination [message #758670 is a reply to message #758656] Thu, 24 November 2011 07:57 Go to previous message
Zeeshan Safder is currently offline Zeeshan SafderFriend
Messages: 27
Registered: November 2011
Junior Member
thnx alot Christian Dietrich Smile
Previous Topic:Error after creating a new eclipse instance
Next Topic:ClassNotFoundException: org.eclipse.xtext.ui.refactoring.ui.RenameElementHandler
Goto Forum:
  


Current Time: Sat Apr 20 02:34:20 GMT 2024

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

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

Back to the top