简体   繁体   English

如何使用XText创建方言

[英]How create dialects with 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. 我为其中一种方言使用EMF创建了一个模型,我想重用XText轻松创建专业的文本编辑器,并能够以正确的格式读写模型。

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? 我认为后者是最好的,但是我如何创建描述这两层的语法呢?

Xtext allows extending existing languages: in the head of the grammar you could specify a parent grammar, that gets inherited. Xtext允许扩展现有语言:在语法的开头,您可以指定一个继承的父语法。

For an example, see the Domain model example from Xtext 2.0, that extends the XBase language: 有关示例,请参见Xtext 2.0中的Domain模型示例,该示例扩展了XBase语言:

grammar org.eclipse.xtext.example.domainmodel.Domainmodel with org.eclipse.xtext.xbase.Xbase

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 可以添加新的验证,等等。有关更多建议,请参见以下博客文章: 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. 您可以使用相同的方法:创建基本语言,然后将其扩展为各种方言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM