Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Simple New Grammar
Simple New Grammar [message #1416813] Thu, 04 September 2014 20:41 Go to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Hello,
I implemented the grammar of this tutorial " Five simple steps to your JVM language".
With this grammar i have the "package", "entity" and "op".
I would like to define a new grammar without the definition of package and entity, for create something similar to "Scripting Language" but with the "function" definition.
For example:

Quote:

String test;

void function main()
{
//code here
}

String function getVariableText()
{
//code here
}


Please, help me for realise this target.
Thank you
Re: Simple New Grammar [message #1417334 is a reply to message #1416813] Fri, 05 September 2014 07:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

this question is a bit very unspecific or sounds like "please do my work"

the basic idea behinds Xtext's Jvm Languages is to map the DSL concepts to java concepts,

in your case this could look like (just a starting point!)

grammar org.eclipse.xtext.example.domainmodel.Domainmodel with org.eclipse.xtext.xbase.Xbase

generate domainmodel "http://www.xtext.org/example/Domainmodel"

DomainModel:
	importSection=XImportSection?
	features+=Feature*;
	


Feature:
	Property | Operation;

Property:
	name=ValidID ':' type=JvmTypeReference;

Operation:
	'op' name=ValidID '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' (':' type=JvmTypeReference)? 
		body=XBlockExpression;



class DomainmodelJvmModelInferrer extends AbstractModelInferrer {
	
	@Inject extension JvmTypesBuilder
	@Inject extension IQualifiedNameProvider

	def dispatch infer(DomainModel model, IJvmDeclaredTypeAcceptor acceptor, boolean prelinkingPhase) {
		val name = model.eResource.URI.trimFileExtension.lastSegment.toFirstUpper
		acceptor.accept(
			model.toClass( name)
		).initializeLater [
			
			
			val fields = <JvmField>newArrayList()
			for ( f : model.features ) {
				switch f {
			
					Property : {
						val field = f.toField(f.name, f.type)
						field.static = true
						fields += field
						members += field
						
					}
			
					Operation : {
						members += f.toMethod(f.name, f.type ?: inferredType) [
							documentation = f.documentation
							static = true
							for (p : f.params) {
								parameters += p.toParameter(p.name, p.parameterType)
							}
							body = f.body
						]
					}
				}
			}
			
		]
	}
	
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Simple New Grammar [message #1417365 is a reply to message #1417334] Fri, 05 September 2014 09:09 Go to previous messageGo to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Thank you for your reply, and i'm sorry. It's my fault. I didn't explain myself well, at the moment i would like to join the concept of a scripting language with the possibility to make method/function.
With the code outside a method, (in jvm model) i define the method main, otherwise if it is a method/function i would like to transcode in a formal java method.
For example:

Quote:

int i = 5;
int j = 8;

int sum(int val, int val2)
{
return val+val2;
}

int result = somma(i, j);
println(result);


At the moment, i have this grammar:

Quote:


Script returns XBlockExpression:
{Script}
((expressions+=XExpressionOrVarDeclaration | operations+=Operation) ';'?)*;


Operation:
'function' name=ValidID '('
(params+=FullJvmFormalParameter
(',' params+=FullJvmFormalParameter)*)?
')' ':' type=JvmTypeReference
'{'
//Code here, but how?
'}'
;


I would like to insert code inside operation, but i don't know how. Moreover i would like to change the syntax of the grammar for obtain a function declaration without function and with JvmTypeReference at the start (like above).
Thank you
Re: Simple New Grammar [message #1417371 is a reply to message #1417365] Fri, 05 September 2014 09:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

i still do not understand where the problem is. have a look how XBlockExpression is defined.
and i dont understand you you do not imply change the syntax for the op ?!?

e.g.

Operation:
type=JvmTypeReference name=ValidID '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'
body=XBlockExpression;


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Simple New Grammar [message #1417372 is a reply to message #1417371] Fri, 05 September 2014 09:24 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
P.S

by crtl click you get to the definition of xblockexpression

XBlockExpression returns XExpression:
{XBlockExpression}
'{'
(expressions+=XExpressionOrVarDeclaration ';'?)*
'}';



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Simple New Grammar [message #1417458 is a reply to message #1417372] Fri, 05 September 2014 13:28 Go to previous messageGo to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
I'm sorry for my questions, i'm a new user of xtext.
I added the XBlockExpression in Operation, and now its work.
But if i use XExpressionOrVarDeclaration with Operation, i obtain xtext error 211.
I don't understand how to resolve.

To clarify, i want to add in my grammar the following functionality:
1 - Method, with Operation now its work
2 - A sort of scripting language for every part of code outside method
I tryed to implement this part with XExpressionOrVarDeclaration without success, or to be precise its work without Operation.
3 - How can import variabile type like int, string, double, float and so on with Xbase?

Re: Simple New Grammar [message #1417579 is a reply to message #1417458] Fri, 05 September 2014 18:30 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

am still without clue what your actual problem/ error is maybe you want to do something in your grammar as it is done in
https://github.com/cdietrich/mql/blob/master/org.eclipse.xtext.mql/src/org/eclipse/xtext/mqrepl/ModelQueryLanguage.xtext


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Simple New Grammar [message #1418651 is a reply to message #1417579] Sun, 07 September 2014 11:41 Go to previous messageGo to next message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Hi, thanks for your answer. I wanted something like in ModelQueryLanguage.
Now i have the method declaration, variable declaration and a script language in the same environment.
Now, this is my actual grammar:

Grammar


import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase


Model:
	imports = XImportSection?
	elements+=Elementi* 
	body = XBlockExpressionWithoutBraces
;


Elementi:
	Function | Variabili
;

Function:
	type=JvmTypeReference name=ValidID
  '('(params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')'   
   body=XBlockExpression
;	

Variabili:
	type = JvmTypeReference name = ID ';'
;

XBlockExpressionWithoutBraces returns xbase::XExpression:
	{xbase::XBlockExpression}
		(expressions+=XExpressionOrVarDeclaration ';'?)*
;


JvmInferr

def dispatch void infer(Model model, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
   		acceptor.accept(
   			model.toClass("pippo")
   		).initializeLater [
   				
   				//Variabili   				
   				for(elem : model.elements){
   					switch(elem)
   					{
   						Function:{
   							members += elem.toMethod(elem.name, elem.type ?: inferredType) [
								for (p : elem.params) {
									parameters += p.toParameter(p.name, p.parameterType)
								}							
								body = elem.body
							]
   						}
   						
   						Variabili:{
   							members += model.toField(elem.name, elem.type);
		   					members += model.toGetter(elem.name, elem.type);
		   					members += model.toSetter(elem.name, elem.type); 
   						}	
   						
   					}					
   				}
   				
   				members += model.toMethod("main", model.newTypeRef(Void::TYPE)) [   					
        			parameters += model.toParameter("args", model.newTypeRef(typeof(String)).addArrayTypeDimension)
    				setStatic(true)
   					body = model.body   					
   					]
   				]
   	}


I have a specific question, about the order of the elements in the grammar.

Model is my starting point, and at the moment i have:
- Import on top - optional
- Variables, method in arbitrary order

I would like to add Script language in the arbitrary order like Variables and method.
I've tried in this way:

Model:
	imports = XImportSection?
	(elements+=Elementi*) | 
	(body = XBlockExpressionWithoutBraces)
;


Without error, but it doesn't work. Where is the error?
Moreover, before starting with make grammar, some days ago i've had some problem with Warning 200, even with the default grammar. I resolved this warning adding "backtrack = true" on Antlr. But is it normal?
Thanks for your help
Re: Simple New Grammar [message #1418791 is a reply to message #1418651] Sun, 07 September 2014 17:01 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,

this is not possible this way.

this is why i made it easy and put the funcs on the top

if you want to mix it you have to extend xbase itself (the stuff that is behind XExpressionOrVarDeclaration)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Simple New Grammar [message #1418792 is a reply to message #1418791] Sun, 07 September 2014 17:02 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
p.s. and it is anything else than "simple"

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Simple New Grammar [message #1418795 is a reply to message #1418792] Sun, 07 September 2014 17:07 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
p.s. or you dont reuse the toplevel stuff and use your OWN concept for both variable thingy and function.

e.g.

Stuff: Thingy | Func;

Thingy: expression= XExpressionOrVarDeclaration


the problem with that: scoping will be like hell. you wont be able to reuse the xbase get for free scoping then.
why does your scriping language have funcs at all if you can have function literals / closures for free?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Simple New Grammar [message #1419568 is a reply to message #1418795] Mon, 08 September 2014 20:22 Go to previous message
Daniele P. is currently offline Daniele P.Friend
Messages: 18
Registered: August 2014
Junior Member
Thanks for your help. Now it's more clear.
Previous Topic:New Xcore build available?
Next Topic:[Xpand/Xtend] : xtext extensions importation
Goto Forum:
  


Current Time: Fri Mar 29 06:40:58 GMT 2024

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

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

Back to the top