我当前正在使用帮助OP XTEXT进行新的DSL。我希望能够在我的语法中定义规则,在该语法中可以操纵某些值并使用this
参考当前对象。但是,我无法正确地使用语法。
我从Xtext表达式示例中取了一些代码,并将其修改为也能够对卡值进行交叉引用。如何使用此关键字?我从其他一些问题中了解了我可以使用自己的范围提供商的问题,但不知道从哪里开始。
请参阅一些代码:
//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 ????
}
}
}
澄清:如果我有一个与上面的代码示例中具有属性的卡模型,我希望能够在规则部分中说:
this.properties.propertyName + 2
我该如何实现?
在Xtext交叉引用中看起来像
ref=[SomeType]
这是
的简短ref=[SomeType|ID]
这意味着ID
将被解析为参考SomeType
因此,通过用另一个规则替换ID
,例如
ref=[SomeType|IDORTHIS]
...
IDORTHIS: ID | "this";
您可以实现解析方面
对于范围,您需要实现范围提供商
范围提供商内部
- 覆盖getscope
- 使用上下文和参考参数确定范围范围的参考
- 使用上下文收集要放入范围的元素
- 创建一个新的范围,例如使用
Scopes
类中的方法或手动/eobjectDescription.greate或.....(您的要求非常模糊)