【发布时间】:2017-12-22 16:21:26
【问题描述】:
我目前正在借助 op Xtext 制作新的 DSL。我希望能够在我的语法中定义规则,其中可以操作某些值并使用this 引用当前对象。但是,我无法正确使用语法。
我从Xtext Expressions example 中提取了一些代码并对其进行了修改,以便能够对卡片值进行交叉引用。如何使用 this 关键字?我从其他一些 SO 问题中了解到,我可以为此使用自己的范围提供程序,但不知道从哪里开始。
查看一些代码:
// MyDSL.xtext
Game returns Game:
'Game'
name=STRING
...
('Cardpropertytypes' '{' cardpropertytypes+=CardPropertyType ( "," cardpropertytypes+=CardPropertyType)* '}' )?
cards += Card*
;
Card returns Card:
'Card'
name=ID
'{'
'type' type=[CardType]
('cost' '{' cost+=Cost ( "," cost+=Cost)* '}' )?
('properties' '{' properties+=CardProperty ( "," properties+=CardProperty)* '}' )?
('rules' '{' rules+=CardRule ( "," rules+=CardRule)* '}' )?
('actions' '{' actions+=CardAction ( "," actions+=CardAction)* '}' )?
'}';
CardRule returns CardRule:
{CardRule}
'{'
('description' description=STRING)?
('requirements' requirements=Addition)?
('action' action=Addition)?
('duration' duration=Duration)?
'}';
CardProperty returns CardProperty:
type=[CardPropertyType] (':' value=INT)?;
Addition returns Expression:
Multiplication ({Addition.left=current} '+' right=Multiplication)*;
Multiplication returns Expression:
Primary ({Multiplication.left=current} '*' right=Primary)*;
Primary returns Expression:
Literal |
'(' Addition ')';
Literal returns Expression:
{Expression}
QualifiedName |
NumberLiteral;
QualifiedName:
ID ('.' ID)*;
NumberLiteral:
value=INT;
// Card.mydsl
Cardpropertytypes {
Toughness,
Power,
Flying,
Indestructible
}
Card AdantoVanguard {
...
properties {
Toughness: 1,
Power: 1,
Flying
}
rules {
{
//action this.properties.Toughness + 2
action ????
}
}
}
澄清: 如果我有一个 Card 模型,它具有上面代码示例中的属性,我希望能够在规则部分中说:
this.properties.propertyName + 2
我怎样才能做到这一点?
【问题讨论】:
-
您说的是哪个交叉引用?在这方面你的语法似乎不完整