以编程方式将eObject添加到解析树 [英] Add eObject to the parse tree programmatically

查看:145
本文介绍了以编程方式将eObject添加到解析树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的语法的快照:

Sort:
 name=ID 
;
Variable
 name=ID ':' type=[Sort]

我的要求是要有一个预定义的 Sort ,我们将其称为 Loc .用户无需定义此排序,因此,当使用 Loc 类型定义 Variable 时,Xtext应该自动将其引用到我的预定义Sort.如何启动程序,以便在开始时生成Sort实例?我已经在我的 validator 类中使用了Factory方法'CreateSort',但是没有用.

My requirement is to have a predefined Sort let's call it Loc. There is no need for the user to define this sort, thus when a Variable is defined with a Loc type, Xtext should automatically reference it to my predefined Sort. How can I initiate the program so that at the beginning a Sort instance is generated? I already used the Factory method 'CreateSort' in my validator class, but no use.

推荐答案

您的带有createSort Factory方法的直觉很好,但是您必须在正确的时间调用它. 必须在链接步骤之前创建 Loc 实例.为此,您必须绑定一个自定义链接程序并将其覆盖.

Your intuition with createSort Factory method is good but you have to call it at the right time. The Loc instance must be created before linking step. To do so, you have to bind a custom Linker and override it.

public class CustomLinker extends LazyLinker {

    @Override
    protected void beforeModelLinked(EObject model,
            IDiagnosticConsumer diagnosticsConsumer) {
        super.beforeModelLinked(model, diagnosticsConsumer);
        if (model instanceof Root) {
            Root root = (Root) model;
            Sort locSort = MyDslFactory.eINSTANCE.createSort();
            locSort.setName("Loc");
            root.getContent().add(locSort);
        }
    }
}

然后,在运行时模块中绑定此自定义链接器类:

Then, you bind this custom linker class in Runtime Module:

public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {

    @Override
    public Class<? extends ILinker> bindILinker() {
        return CustomLinker.class;
    }
}

现在您可以编写一个包含

Now you can write a file containing

变量:位置

variable : Loc

该引用将被解决.

这篇关于以编程方式将eObject添加到解析树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆