XText:使用自定义终端定义 [英] XText: use custom terminals definitions
问题描述
当您使用XText定义语法时,您可以指定第二个语法,并使用它声明的定义,因为它是< a href =http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.xtext.doc/help/getting-started.html =nofollow> here :
grammar org.eclipse.xtext.example.Domainmodel with org.eclipse.xtext.common.Terminals
在Xtext中,每个语法都有一个唯一的
名称,它像公共Java类
需要反映Java类路径中
文件的位置。在
的情况下,语法文件位于
/org/eclipse/xtext/example/Domainmodel.xtext
中,因此语法的名称为
org.eclipse.xtext。 example.Domainmodel。
该语句的第二部分(
和
org.eclipse.xtext.common.Terminals)
声明,该语法重用,
将覆盖指定的规则
语法。
org.eclipse.xtext.common.Terminals是
a图书馆语法与Xtext
一起发送,并预先定义最常见的
终端规则
我正在开发一个XText插件,我想在一个单独的文件中定义我自己的终端符号。可能吗?我如何做到这一点?
我尝试创建一个新的Xtext文件,并在org.eclipse.xtext.common.Terminals之后添加,并添加只是我的两个解决方案都不编译。
谢谢。
编辑 >
如果我在同一个项目中使用两个xtext文件,一个用于语法,另一个用于语法的终端,我得到以下异常,启动mwe2文件:
java.lang.IllegalStateException:
问题解析
'classpath:/org/xvr/language/sh/ShaderDsl.xtext':[XtextLinkingDiagnostic :
null:1无法解析引用
Grammar
'org.xvr.language.sh.ShTerminal'。,
XtextLinkingDiagnostic:null:9
可以不能创建数据类型INVARIANT
(ErrorCode:NoSuchTypeAvailable),
TransformationDiagnostic:null:不能创建数据类型
AbstractRule'ID'。
TransformationDiagnostic:null: 17
无法创建数据类型PRECISION
(ErrorCode:NoSuchTypeAvailable),
TransformationDiagnostic:null:19
无法创建数据类型HIGH_PRECISION
(ErrorCode:NoSuchTypeAvailable),
TransformationDiagnostic:null:20
无法创建数据类型
MEDIUM_PRECISION(ErrorCode:
NoSuchTypeAvailable),
TransformationDiagnostic:null:21
无法创建数据类型LOW_PRECISION
(ErrorCode:NoSuchTypeAvailable)]
两个xtext文件是:
语法
grammar org.xvr.language.sh.ShaderDsl with org.xvr.language.sh.ShTerminal //org.eclipse.xtext.common.Terminals
生成shaderDslhttp://www.xvr.org/language/sh/ShaderDsl
....
和语法终端
语法org.xvr.language.sh 。使用org.eclipse.xtext.common.Terminals的终端
生成shTerminalshttp://www.xvr.org/languag e / sh / ShTerminals
终端测试:'test';
你有很多选择在线帮助):
- 在您正在使用的语法中定义您的终端规则。
- 为终端符号创建一个新的专用语法,类似于我们用common.Terminals做的。使用一个而不是通用的终端。
- 为终端符号创建一个新的专用语法,重用该语法中的common.Terminals,并以您的实际语言使用您自己的终端语法。 li>
我建议只要用您的语言(选项1)覆盖您想要更改的终端,或者您想要定义多个使用与我使用的一组终端相同的语言(选项3)或组合两个选项,例如
语法org.mycompany。 MyTerminals with org.eclipse.xtext.common.Terminals
终端ID:('a'..'z'|'A'..'Z'|'_')('a'。 .'z '|' A .. 'Z' | '_' | '0' .. '9')*;
terminal SL_COMMENT:' - '!('\\\
'|'\r')*('\r'?'\\\
')?
== b
$ b grammar org.mycompany.MyLanguage with org.mycompany.MyTerminals
MyModel:name = ID other = ANOTHER;
终端ANOTHER:'/#' - > '#/'
I'm totally new to XText.
When you define a grammar using XText you could specify a second grammar and use the definitions it declares as it is said here:
grammar org.eclipse.xtext.example.Domainmodel with org.eclipse.xtext.common.Terminals
In Xtext each grammar has a unique name, which like public Java classes needs to reflect the location of the file within the Java classpath. In our case the grammar file is located in /org/eclipse/xtext/example/Domainmodel.xtext therefore the name of the grammar is org.eclipse.xtext.example.Domainmodel. The second part of that statement ( with org.eclipse.xtext.common.Terminals) states, that this grammar reuses and overrides rules from the specified grammar. The org.eclipse.xtext.common.Terminals is a library grammar shipped with Xtext and predefines the most common terminal rules
I'm developing an XText plugin and i would like to define my own terminal symbols in a separated file. Is it possible? How can i do that?
I tried both to create a new Xtext file and append it after org.eclipse.xtext.common.Terminals and to add just the mine but both solutions don't compile.
Thanks.
EDIT
If i use two xtext files in the same project, one for the grammar and one for the grammar's terminals i get the following exception launching the mwe2 file:
java.lang.IllegalStateException: Problem parsing 'classpath:/org/xvr/language/sh/ShaderDsl.xtext':[XtextLinkingDiagnostic: null:1 Couldn't resolve reference to Grammar 'org.xvr.language.sh.ShTerminal'., XtextLinkingDiagnostic: null:9 Couldn't resolve reference to AbstractRule 'ID'., TransformationDiagnostic: null:14 Cannot create datatype INVARIANT (ErrorCode: NoSuchTypeAvailable), TransformationDiagnostic: null:17 Cannot create datatype PRECISION (ErrorCode: NoSuchTypeAvailable), TransformationDiagnostic: null:19 Cannot create datatype HIGH_PRECISION (ErrorCode: NoSuchTypeAvailable), TransformationDiagnostic: null:20 Cannot create datatype MEDIUM_PRECISION (ErrorCode: NoSuchTypeAvailable), TransformationDiagnostic: null:21 Cannot create datatype LOW_PRECISION (ErrorCode: NoSuchTypeAvailable)]
the two xtext files are:
the grammar
grammar org.xvr.language.sh.ShaderDsl with org.xvr.language.sh.ShTerminal //org.eclipse.xtext.common.Terminals
generate shaderDsl "http://www.xvr.org/language/sh/ShaderDsl"
....
and the grammar's terminals
grammar org.xvr.language.sh.ShTerminals with org.eclipse.xtext.common.Terminals
generate shTerminals "http://www.xvr.org/language/sh/ShTerminals"
terminal Test : 'test';
You have plenty of options (all of them are documented in the online help):
- Define your terminal rules right in the grammar that your are currently working with.
- Create a new dedicated grammar for terminal symbols similar to what we did with the common.Terminals. Use that one instead of the common Terminals.
- Create a new dedicated grammar for terminal symbols, reuse the common.Terminals in that grammar and use your own terminal grammar in your actual language.
I'd recommend to just override the terminals that you want to change right in your language (option 1) or if your want to define multiple languages with the same set of terminals I'd use (option 3) or combine both options, e.g.
grammar org.mycompany.MyTerminals with org.eclipse.xtext.common.Terminals
terminal ID: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal SL_COMMENT: '--' !('\n'|'\r')* ('\r'? '\n')?;
==
grammar org.mycompany.MyLanguage with org.mycompany.MyTerminals
MyModel: name=ID other=ANOTHER;
terminal ANOTHER: '/#' -> '#/'
这篇关于XText:使用自定义终端定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!