Simple New Grammar [message #1416813] |
Thu, 04 September 2014 16:41  |
Eclipse User |
|
|
|
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 #1417365 is a reply to message #1417334] |
Fri, 05 September 2014 05:09   |
Eclipse User |
|
|
|
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 #1417458 is a reply to message #1417372] |
Fri, 05 September 2014 09:28   |
Eclipse User |
|
|
|
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 07:41   |
Eclipse User |
|
|
|
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
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05043 seconds