Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to generate code using Xtext and Xtend?
How to generate code using Xtext and Xtend? [message #717572] Sun, 21 August 2011 12:52 Go to next message
Harshani Nawarathna is currently offline Harshani NawarathnaFriend
Messages: 9
Registered: August 2011
Junior Member
This a grammar I wrote using Xtext to implement a DSL.

grammar org.processing.pde.Pde with org.eclipse.xtext.common.Terminals

generate pde "http://www.processing.org/pde/Pde"

Pde:
    Active | Static;

Active:
    method_1=Setup
    method_2=Draw

;

Static:
    elements+=AbstractElement*
;

AbstractElement:
    Size | Background | Shape | Fill | ShapeMode | Smooth | Stroke | FrameRate | ColorMode

;

terminal LPAREN:
    "("
;

terminal RPAREN:
    ")"
;

terminal MOUSE_X:
    "mouseX"
;

terminal MOUSE_Y:
    "mouseY"
;

terminal P_MOUSE_X:
    "pmouseX"
;

terminal P_MOUSE_Y:
    "pmouseY"
;

terminal NO_VALUE:
    (" ")
;

terminal MODE:
    "CENTER" | "CORNER"
;

terminal OPERATOR:
    "+" | "-" | "/" | "*" | "^"
;

terminal PI_VALUE:
    "PI" |"0" | "HALF_PI" | "QUATER_PI" | "TWO_PI"
;

terminal ROUND_VALUE:
    "ROUND"
;

terminal SQUARE_VALUE:
    "SQUARE"
;

terminal BEVEL_VALUE:
    "BEVEL"
;

terminal CLOSE_VALUE:
    "CLOSE"
;

Corner:
    ROUND_VALUE | BEVEL_VALUE
;

Ending:
    ROUND_VALUE | SQUARE_VALUE
;

PiValuesWithOperations:
    PI_VALUE | PI_VALUE OPERATOR PI_VALUE
;

RadianValue:
    "radian" LPAREN degree_value=INT  RPAREN 
;


GrayValue:
    INT
;

Position_X:
    MOUSE_X| INT | OPERATOR INT | MOUSE_X OPERATOR INT | INT OPERATOR INT | INT OPERATOR MOUSE_X | P_MOUSE_X | P_MOUSE_X OPERATOR MOUSE_X | MOUSE_X OPERATOR P_MOUSE_X | INT OPERATOR P_MOUSE_X | P_MOUSE_X OPERATOR INT
;   

Position_Y:
    MOUSE_Y | INT | OPERATOR INT | MOUSE_Y OPERATOR INT | INT OPERATOR INT | INT OPERATOR MOUSE_Y | P_MOUSE_Y | P_MOUSE_Y OPERATOR MOUSE_Y | MOUSE_Y OPERATOR P_MOUSE_Y | INT OPERATOR P_MOUSE_Y | P_MOUSE_Y OPERATOR INT
;

ColorModeMaxValue:
    INT
;

RedValue:
    INT
;

GreenValue:
    INT
;

BlueValue:
    INT
;

AlphaValue:
    INT
;

HueValue:
    INT
;

SaturationValue:
    INT
;

BrightnessValue:
    INT
;

EndShapeValue:
    CLOSE_VALUE | NO_VALUE
;

Size:
    "size" LPAREN height=INT "," width=INT RPAREN ";"
;

Background:
    "background" LPAREN gray_value=GrayValue RPAREN";"
;

Fill:
    {Fill}
    "fill" LPAREN gray_value=GrayValue RPAREN ";"   

;

ShapeMode:
    "ellipseMode" LPAREN mode_value= MODE RPAREN ";" |
    "rectMode" LPAREN mode_value=MODE RPAREN ";" |
    "beginShape" LPAREN no_begin_shape_value=NO_VALUE RPAREN ";" |
    "endShape" LPAREN no_end_shape_value=EndShapeValue RPAREN ";"
;



Smooth:
    "smooth" LPAREN smooth_value=NO_VALUE RPAREN ";" |
    "noSmooth" LPAREN no_smooth_value=NO_VALUE RPAREN ";"
;

Stroke:
    "stroke" LPAREN stroke_value=INT RPAREN";" |
    "noStroke" LPAREN no_stroke_value=NO_VALUE RPAREN";" |
    "strokeWeight" LPAREN stroke_weight_value=INT RPAREN ";" |
    "strokeJoin" LPAREN stroke_corner=Corner RPAREN";" |
    "strokeCap" LPAREN stroke_ending=Ending RPAREN";"

;

Shape:
    Rectangle | Ellipse | Point | Line | Triangle | Quad | Arc | Vertex
;

Point:
    "point"LPAREN position_x= Position_X"," position_y=Position_Y RPAREN ";"
;

Vertex:
    "vertex" LPAREN position_x=Position_X "," position_y=Position_Y RPAREN ";"
;

Line:
    "line" LPAREN position_1_x=Position_X "," position_1_y=Position_Y "," position_2_x=Position_X "," position_2_y=Position_Y RPAREN ";"
;

Rectangle:
    "rect" LPAREN top_left_x=Position_X "," top_left_y=Position_Y "," bottom_right_x=Position_X"," bottom_right_y=Position_Y RPAREN ";"

;

Ellipse:
    "ellipse" LPAREN center_x=Position_X "," center_y=Position_Y "," width=INT "," height=INT RPAREN ";"
;

Triangle:
    "traingle" LPAREN position_1_x=Position_X "," position_1_y=Position_Y "," position_2_x=Position_X "," position_2_y=Position_Y "," position_3_x=Position_X "," position_3_y=Position_Y RPAREN ";"
;

Quad:
    "quad" LPAREN position_1_x=Position_X "," position_1_y=Position_Y "," position_2_x=Position_X "," position_2_y=Position_Y "," position_3_x=Position_X "," position_3_y=Position_Y "," position_4_x=Position_X "," position_4_y=Position_Y RPAREN ";"
;

Arc:
    "arc" LPAREN position_x=Position_X "," position_y=Position_Y "," width=INT "," height=INT "," start= PiValuesWithOperations "," stop= PiValuesWithOperations RPAREN";" |
    "arc" LPAREN position_x=Position_X "," position_y=Position_Y "," width=INT "," height=INT "," start_r= RadianValue "," stop_r=RadianValue RPAREN";" 

;

FrameRate:
    "frameRate"LPAREN frame_rate_value=INT RPAREN";"
;

ColorMode:
    RGBColorMode | HSBColorMode
;

RGBColorMode:
    "colorMode" LPAREN color_mode="RGB" "," color_mode_max_value=ColorModeMaxValue RPAREN";"
     |"colorMode" LPAREN color_mode="RGB" ","  red_value=RedValue "," green_value=GreenValue "," blue_value=BlueValue ("," alpha_value=AlphaValue)? RPAREN";"

;

HSBColorMode:
    "colorMode" LPAREN color_mode="HSB" "," hue_value=HueValue "," saturation_value=SaturationValue "," brightness_value=BrightnessValue RPAREN";"
;

Setup:
    "void" space=NO_VALUE "setup" LPAREN RPAREN  "{"
        elements+=AbstractElement*
    "}"
;

Draw:
    "void" space=NO_VALUE "draw" LPAREN RPAREN "{"
        shapes+=Shape*
    "}"
;



It can help me to suggest code as in the "test.pde" file mentioned below when I run "org.processing.pde" (my project) in a new eclipse runtime.This is the syntax of programming language "Processing" and I'm currently working on building code suggestion for the language using Xtext.

void setup(){

    size(200,200);

}

void draw(){

    ellipse(50,50,80,80);

}

Now I want to generate a "src-gen/test.java" file according to my "test.pde" file as mentioned below and it will act as the pre-compiler code.

import processing.core.*;


public class Test extends PApplet {

public void setup(){
       size(200,200);
}

public void draw(){
       ellipse(50,50,80,80);
}
   static public void main(String args[]) {
       PApplet.main(new String[] { "--bgcolor=#ECE9D8", "a" });
   }
}



As I found Xtend can generate that code for me. I went through the Xtext team's video's at vimeo, Xtext reference document. I couldn't find any other tutorials for Xtend to proceed with. I'm still clueless how to start coding the Xtend file.

Can someone please help me to start.

Thanks in advance.

[Updated on: Sun, 21 August 2011 12:54]

Report message to a moderator

Re: How to generate code using Xtext and Xtend? [message #717576 is a reply to message #717572] Sun, 21 August 2011 13:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

did you see http://www.eclipse.org/Xtext/documentation/2_0_0/040-first-code-generator.php

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to generate code using Xtext and Xtend? [message #717588 is a reply to message #717576] Sun, 21 August 2011 14:40 Go to previous messageGo to next message
Harshani Nawarathna is currently offline Harshani NawarathnaFriend
Messages: 9
Registered: August 2011
Junior Member
Hi Christian,

Thanks for the quick reply.
I went through that tutorial.
But I tried to map it into my scenario and failed.
What should I used to replace Entity and Feature in this scenario?
Re: How to generate code using Xtext and Xtend? [message #717592 is a reply to message #717588] Sun, 21 August 2011 15:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

since this is your metamodel you should of cource know it much better than me.
it is your decision on how to process the model / what to do.
never the less here a partial solutuin that fits your needs

class MyDslGenerator implements IGenerator {
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		fsa.generateFile("Test.java", '''
		import processing.core.*;


		public class Test extends PApplet {
			
		«FOR active : resource.allContentsIterable.filter(typeof(Active))»
		public void setup(){
		       «FOR e : active.method_1.elements»
		       		«e.compile»
		       «ENDFOR»
		}
		public void draw(){
		       «FOR e : active.method_2.shapes»
		       		«e.compile»
		       «ENDFOR»
		}
		«ENDFOR»
		   static public void main(String args[]) {
		       PApplet.main(new String[] { "--bgcolor=#ECE9D8", "a" });
		   }
		}
		''')
	}
	
	def dispatch compile(AbstractElement e) {
		'''
		//TODO
		'''
	}
	
	def dispatch compile(Size e) {
		'''
			size(«e.width»,«e.height»);
		'''
	}
	
	def dispatch compile(Ellipse e) {
		'''
			ellipse(«e.center_x»,«e.center_y»,«e.width»,«e.height»);
		'''
	}
}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to generate code using Xtext and Xtend? [message #717598 is a reply to message #717592] Sun, 21 August 2011 15:54 Go to previous message
Harshani Nawarathna is currently offline Harshani NawarathnaFriend
Messages: 9
Registered: August 2011
Junior Member
Hi Christian,

It worked. Now I have an idea how to write the Xtend.Many Many Thanks Smile
You are a life saver Smile
Previous Topic:Absolute file path
Next Topic:Mark Keywords within a String terminal
Goto Forum:
  


Current Time: Tue Apr 23 16:14:29 GMT 2024

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

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

Back to the top