|
Re: Simple New Grammar [message #1417334 is a reply to message #1416813] |
Fri, 05 September 2014 07:52 |
|
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 |
Daniele P. 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 |
|
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 #1417458 is a reply to message #1417372] |
Fri, 05 September 2014 13:28 |
Daniele P. 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 #1418651 is a reply to message #1417579] |
Sun, 07 September 2014 11:41 |
Daniele P. 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 #1418795 is a reply to message #1418792] |
Sun, 07 September 2014 17:07 |
|
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.04397 seconds