如何使用 XText 创建方言
我正在处理的项目具有自定义文件格式和预定义的结构。该结构非常简单且通用(我无法更改它):它由(嵌套)命令和类型化属性组成。
利用这种结构,已经创建了几种方言。方言是通用语法的“实例化”,并指定命令的名称和含义以及预期的属性。
我使用 EMF 为这些方言之一创建了一个模型,我想重用 XText 来轻松创建一个专业的文本编辑器,并能够以正确的格式读取和写入我的模型。
现在我有一个选择。一方面,我可以直接针对方言,并在同一语法中混合自定义文件结构中的概念和方言中的概念。另一方面,我可以创建一个描述文件结构的语法,在此之上我可以描述我的方言。
我应该走哪条路?我认为后者是最好的,但是我如何创建一个描述这两层的语法呢?
The project I'm working on has a custom file format, with a pre-defined structure. The structure is really simple and generic (and I cannot change it): it is composed by (nested) commands and typed properties.
Using this structure, several dialects have been created. The dialects are an "instantiation" of the generic grammar, and specify the name and the meaning of commands and the expected properties.
I created a model with EMF for one of these dialects, and I would like to reuse XText to easily create a professional text editor and be able to read and write my model into the correct format.
Now I have a choice. On one side, I can directly target the dialect, and mix in the same grammar the concepts from the custom file structure and those from the dialect. On the other side, I can create a grammar describing the file structure, and on top of this I can describe my dialect.
Which way I should follow? I think that the latter is the best one, but how can I create a grammar describing those two layers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Xtext 允许扩展现有语言:在语法的头部,您可以指定继承的父语法。
例如,请参阅 Xtext 2.0 中的域模型示例,它扩展了 XBase 语言:
每个语法元素都可以被新语法替换;可以添加新的验证等。有关更多想法,请参阅以下博客文章: http: //koehnlein.blogspot.com/2011/07/extending-xbase.html
您可以使用相同的方法:创建一种基本语言,然后将其扩展为您的各种方言。
Xtext allows extending existing languages: in the head of the grammar you could specify a parent grammar, that gets inherited.
For an example, see the Domain model example from Xtext 2.0, that extends the XBase language:
Every grammar element can be replaced by new syntax; new validation can be added, etc. See the following blog posts for further ideas: http://koehnlein.blogspot.com/2011/07/extending-xbase.html
You can use the same approach: create a base language, then extend them for your various dialects.