这是我用Xtext编写的一种语法来实现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*
"}"
;
当我在一个新的eclipse运行时运行“org.processing.pde”(我的项目)时,它可以帮助我建议下面提到的“test.pde”文件中的代码。这是编程语言“Processing”的语法“我目前正致力于使用Xtext为语言构建代码建议。
void setup(){
size(200,200);
}
void draw(){
ellipse(50,50,80,80);
}
现在我想根据我的“test.pde”文件生成一个“src-gen / test.java”文件,如下所述,它将作为预编译器代码。
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" });
}
}
我发现Xtend可以为我生成该代码。我在vimeo,Xtext参考文档中浏览了Xtext团队的视频。我找不到任何其他Xtend继续教程。我仍然不知道如何开始编码Xtend文件。
有人可以帮我开始。
提前致谢。
答案 0 :(得分:5)
请查看作为Xtext SDK一部分的Statemachine示例(新项目 - >示例 - > Xtext ..)。它包含StatemachineGenerator.xtend
类,它几乎说明了如何使用Xtend进行简单的代码生成。
答案 1 :(得分:2)
您可以创建一个生成器,填充xtext项目中“generator”包内的xtend文件。请观看此视频,以福勒的书为例。 http://vimeo.com/24584729
答案 2 :(得分:1)
只是文件 - >新 - >其他并创建Xtext示例项目,看看如何使用Xtend生成代码(2?我认为是因为旧的Xtend不打算用于代码生成)。