Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How do you extract DSL out of Java objects?(How do you extract DSL out of Java objects?)
How do you extract DSL out of Java objects? [message #902480] Fri, 17 August 2012 20:38 Go to next message
Reo Dreams is currently offline Reo DreamsFriend
Messages: 1
Registered: August 2012
Junior Member
Hi,

Let's say I have the following Java class Student with a pair of getter/setter methods:

public class Student
{
int age;

public int getAge()
{
return age;
}

public void setAge(int newAge)
{
age = newAge;
}
}

How do I automatically create a vocabulary for the above class, i.e. a DSL that represents the getter/setter as shown below:

* set the age of Student to "{value}"
* the age of Student

Can this even be done using XTest?

Thanks
Reo.
Re: How do you extract DSL out of Java objects? [message #902483 is a reply to message #902480] Fri, 17 August 2012 21:09 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

yes Xtext has support for Jvm Types. the thing that is problematic
(for code completion) is your synatax that names the getter/setter first and then then Class.

Here is a (syntactically and funtionally)simplified example

grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

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

import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types

Model:
	statements += Statement*;
	
Statement:
	SetStatement | GetStatement
;


GetStatement:
	"with" class=[types::JvmDeclaredType|FQN] "get" "the" op=[types::JvmOperation]
;

SetStatement:
	"with" class=[types::JvmDeclaredType|FQN] "set" "the" op=[types::JvmOperation] "to" value=STRING
;

FQN: ID ("." ID)*;



public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
	
	IScope scope_Statement_op(SetStatement context, EReference ref) {
		Iterable<JvmOperation> setters = Iterables.filter(context.getClass_().getDeclaredOperations(), new Predicate<JvmOperation>() {
			public boolean apply(JvmOperation op) {
				return op.getSimpleName().startsWith("set") && op.getParameters().size() == 1;
			}
		});
		
		Function<JvmOperation, QualifiedName> nameComputation = new Function<JvmOperation, QualifiedName>() {
			public QualifiedName apply(JvmOperation op) {
				return QualifiedName.create(op.getSimpleName().substring(3).toLowerCase());
			}
		};
		return Scopes.scopeFor(setters, nameComputation , IScope.NULLSCOPE);
	}
	
	IScope scope_Statement_op(GetStatement context, EReference ref) {
		Iterable<JvmOperation> setters = Iterables.filter(context.getClass_().getDeclaredOperations(), new Predicate<JvmOperation>() {
			public boolean apply(JvmOperation op) {
				return op.getSimpleName().startsWith("get") && op.getParameters().size() == 0;
			}
		});
		
		Function<JvmOperation, QualifiedName> nameComputation = new Function<JvmOperation, QualifiedName>() {
			public QualifiedName apply(JvmOperation op) {
				return QualifiedName.create(op.getSimpleName().substring(3).toLowerCase());
			}
		};
		return Scopes.scopeFor(setters, nameComputation , IScope.NULLSCOPE);
	}

}


there are already some projects around similar languages (Cucumber/Jnario)

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Shift-reduce conflict between grammar rule and terminal in Xtext
Next Topic:How @Inject ParseHelper<> is supposed to work ?
Goto Forum:
  


Current Time: Fri Apr 26 12:46:15 GMT 2024

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

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

Back to the top