4

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.

Harshani
  • 649
  • 2
  • 9
  • 22

4 Answers4

7

Please have a look at the Statemachine example that is part of the Xtext SDK (New Project -> Examples -> Xtext ..). It contains the StatemachineGenerator.xtend class that pretty much illustrates how plain code generation with Xtend works.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Sebastian Zarnekow
  • 6,609
  • 20
  • 23
3

You can create a generator filling the xtend file inside the "generator" package that is inside your xtext project. Please this video can explain that with an example from Fowler's book. http://vimeo.com/24584729

robermorales
  • 3,293
  • 2
  • 27
  • 36
2

Just File -> New -> Other and create Xtext example projects and see how code generated in them using Xtend (2? I think so because old Xtend not intended to be used for code generation).

Nikolay Kasyanov
  • 897
  • 5
  • 14
1

In your first eclipse instance, open the file having the name 'yourproject'Generator.xtend in the first xtext project inside the generator package. This file contains a method called "doGenerate", which contains a commented section. It is there where you should generate your code and the file that will be created in your project in the second eclipse instance.