为Xtext语法做一个自己的解释器
我有一个在Xtext中创建的语法,我可以从plugin.xml启动一个eclipse应用程序并测试我的语法。现在我需要做一个解释器来启动我的DSL代码。为Xtext语法做一个自己的解释器
我用类解释器做了一个包,但我不知道如何访问在eclipse编辑器中打开的文件以便启动。 另一方面,我认为解释器逐行读取编辑器中的文件并运行句子,是这样吗?
我的最后一个问题是,如果你知道一个教程或更好的方式来实现Xtext语法的解释器,并且所有的工作都在一起?我尝试了解乌龟的例子,但我什么都不明白。
谢谢!
那么这是一个很难给出一般性答案的问题。它很大程度上取决于你的口译员的工作以及它如何给出反馈。即使一行一行地工作也许根本没有意义,而只是遍历模型内容。你可以想象当用户输入文件时在“autoedit”上执行此操作。这就是你用xtext附带的算术例子所做的。或者你可以用编辑器切换视图 - 这就是乌龟的例子(https://github.com/xtext/seven-languages-xtext/blob/c04e8d56e362bfb8d6163f4b001b22ab878686ca/languages/org.xtext.tortoiseshell.lib/src/org/xtext/tortoiseshell/lib/view/TortoiseView.xtend)。或者您可以简单地通过右键单击上下文菜单(或快捷键)来调用eclipse命令。这里是一个小的片段,如何构建一个在打开的文件上工作的Eclipse Command Handler。 (从https://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/拍摄)
public class InterpretCodeHandler extends AbstractHandler implements IHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
IFile file = (IFile) activeEditor.getEditorInput().getAdapter(IFile.class);
if (file != null) {
IProject project = file.getProject();
if (activeEditor instanceof XtextEditor) {
((XtextEditor)activeEditor).getDocument().readOnly(new IUnitOfWork<Boolean, XtextResource>() {
@Override
public Boolean exec(XtextResource state)
throws Exception {
// TODO your code here
return Boolean.TRUE;
}
});
}
}
return null;
}
@Override
public boolean isEnabled() {
return true;
}
}
它基本上指甲下调用XtextEditor.getDocument()。只读(在) 使您可以访问到XTEXT资源,你可以使用它。
和这里它的注册(S)
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="popup:#TextEditorContext?after=additions">
<command commandId="org.xtext.example.mydsl.ui.handler.InterpreterCommand" style="push">
<visibleWhen checkEnabled="false">
<reference definitionId="org.xtext.example.mydsl.MyDsl.Editor.opened"></reference>
</visibleWhen>
</command>
</menuContribution>
</extension>
<extension point="org.eclipse.ui.handlers">
<handler class="org.xtext.example.mydsl.ui.MyDslExecutableExtensionFactory:org.xtext.example.mydsl.ui.handler.InterpretCodeHandler" commandId="org.xtext.example.mydsl.ui.handler.InterpreterCommand">
</handler>
</extension>
<extension point="org.eclipse.ui.commands">
<command name="Interpret Code" id="org.xtext.example.mydsl.ui.handler.InterpreterCommand">
</command>
</extension>