[英]Create an expression to get a sub element from entity, using a Xtext DSL
I'd like to know, how can I create a DSL, using Xtext. 我想知道,如何使用Xtext创建DSL。 This is my code, that I created:
这是我创建的代码:
Model:
(entities += Entity)*
(access += Acessing)*
;
Entity:
'entity' name = ID '{'
( variables += Variable )*
'}'
;
Variable:
'var' name=ID
;
Acessing:
'use' (entity = [Entity])'.'(variable = [Variable])
;
The code is a little bit incomplete, but in this way I'd like perform this operation as follows: 代码有点不完整,但是通过这种方式,我想执行以下操作:
entity user {
var name
var phone
var address
}
use user.phone
I understand that I can use this tag [Entity]
as a identifier from a specific element, but I don't know how can I get those sub elements from it. 我知道我可以将此标签
[Entity]
用作特定元素的标识符,但是我不知道如何从中获取那些子元素。
How can I procede? 我该如何进行?
You are using the name
attribute for Entity
and Variable
. 您正在为
Entity
和Variable
使用name
属性。 This is a special attribute which Xtext automatically uses to create namespaces and delivers a working scope provider for free. 这是Xtext自动用于创建名称空间并免费提供工作范围提供者的特殊属性。 Elements are identified by their qualified name.
元素由其限定名称标识。 You need only a single reference to access them.
您只需要一个引用即可访问它们。
To solve your problem, you only have to modify your Use
grammar rule and introduce a rule which describes a qualified name. 要解决您的问题,只需修改“
Use
语法”规则并引入描述合格名称的规则。 Your grammar then could look like this: 这样您的语法可能如下所示:
Model:
(entities+=Entity)*
(access+=Acessing)*;
Entity:
'entity' name=ID '{'
(variables+=Variable)*
'}';
Variable:
'var' name=ID;
Acessing:
'use' var=[Variable|QualifiedName];
QualifiedName:
ID ('.' ID)*;
As you can see, it now uses the QualifiedName
name to identify a variable. 如您所见,它现在使用
QualifiedName
名称来标识变量。 I have just tried it and it works out of the box. 我刚刚尝试过,它开箱即用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.