eclipse - XText:使用自定义终端定义

标签 eclipse eclipse-plugin grammar xtext

我对 XText 完全陌生。

当您使用 XText 定义语法时,您可以指定第二个语法并使用它声明的定义,正如所说 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

我正在开发一个 XText 插件,我想在一个单独的文件中定义我自己的终端符号。可能吗?我该怎么做?

我尝试创建一个新的 Xtext 文件并将其附加到 org.eclipse.xtext.common.Terminals 之后,并只添加我的,但这两种解决方案都无法编译。

谢谢。

编辑

如果我在同一个项目中使用两个 xtext 文件,一个用于语法,一个用于语法的终端,我会在启动 mwe2 文件时遇到以下异常:

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)]

这两个 xtext 文件是:

语法

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"
....

和语法的终端

grammar org.xvr.language.sh.ShTerminals with org.eclipse.xtext.common.Terminals
generate shTerminals "http://www.xvr.org/language/sh/ShTerminals"

terminal Test : 'test';

最佳答案

您有很多选择(所有这些都记录在联机帮助中):

  1. 在您当前使用的语法中定义您的终端规则。
  2. 为终端符号创建一个新的专用语法,类似于我们对 common.Terminals 所做的。使用那个而不是通用终端。
  3. 为终端符号创建一个新的专用语法,在该语法中重用 common.Terminals 并在您的实际语言中使用您自己的终端语法。

我建议只用您的语言覆盖您想要更改的终端(选项 1),或者如果您想使用我将使用的同一组终端定义多种语言(选项 3)或将两者结合使用选项,例如

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: '/#' -> '#/'

关于eclipse - XText:使用自定义终端定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5660653/

相关文章:

java - 在 android eclipse 中创建第二个计数器

java - 如何配置避免加入 Javadoc 行的 Eclipse Java 格式?

java - 从 java 项目转换为插件项目的选项在哪里?

c++ - 如何阅读和理解 C & C++ 标准以及其中使用的语言语法?

java - 如何在 Java 中使用 JSGF 语法生成字符串?

haskell - 递归型镜头

android - eclipse上的Android问题的OpenCV 2.4.9

Eclipse & JSF & Tomcat 6 : "FacesContext not found"

java - eclipse显示点击事件的坐标

sqlite - 代号 一个在线数据库连接