通过Xtext和XBase进行语法限制的JVMModelInferrer继承 [英] Grammatically restricted JVMModelInferrer inheritence with Xtext and XBase

查看:159
本文介绍了通过Xtext和XBase进行语法限制的JVMModelInferrer继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下规范的XBase实体语法(来自用Xtext和Xtend实现特定于域的语言" Bettini)允许实体扩展 any Java类.如注释行所示,我想在语法上强制实体从实体继承.

grammar org.example.xbase.entities.Entities with org.eclipse.xtext.xbase.Xbase

generate entities "http://www.example.org/xbase/entities/Entities"

Model:
    importSection=XImportSection?
    entities+=Entity*;

Entity:
    'entity' name=ID ('extends' superType=JvmParameterizedTypeReference)? '{'
//  'entity' name=ID ('extends' superType=[Entity|QualifiedName])? '{'
        attributes += Attribute*
        constructors+=Constructor*
        operations += Operation*
    '}';

Attribute:
    'attr' (type=JvmTypeReference)? name=ID ('=' initexpression=XExpression)? ';';

Operation:
    'op' (type=JvmTypeReference)? name=ID 
    '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' 
        body=XBlockExpression;

Constructor:    'new'  
    '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' 
        body=XBlockExpression;

这是上面模型的有效JVMModelInferrer,再次在注释行(和其他方法)中反映了我的意图.

package org.example.xbase.entities.jvmmodel

import com.google.inject.Inject
import org.eclipse.xtext.common.types.JvmTypeReference
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.example.xbase.entities.entities.Entity

class EntitiesJvmModelInferrer extends AbstractModelInferrer {

    @Inject extension JvmTypesBuilder
    @Inject extension IQualifiedNameProvider

    def dispatch void infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        acceptor.accept(entity.toClass("entities." + entity.name)) [
            documentation = entity.documentation
            if (entity.superType !== null) {
                superTypes += entity.superType.cloneWithProxies
                //superTypes += entity.superType.jvmTypeReference.cloneWithProxies
            }

            entity.attributes.forEach [ a |
                val type = a.type ?: a.initexpression?.inferredType
                members += a.toField(a.name, type) [
                    documentation = a.documentation
                    if (a.initexpression != null)
                        initializer = a.initexpression
                ]
                members += a.toGetter(a.name, type)
                members += a.toSetter(a.name, type)
            ]
            entity.operations.forEach [ op |
                members += op.toMethod(op.name, op.type ?: inferredType) [
                    documentation = op.documentation
                    for (p : op.params) {
                        parameters += p.toParameter(p.name, p.parameterType)
                    }
                    body = op.body
                ]
            ]
            entity.constructors.forEach [ con |
                members += entity.toConstructor [
                    for (p : con.params) {
                        parameters += p.toParameter(p.name, p.parameterType)
                    }
                    body = con.body
                ]
            ]
        ]
    }

    def JvmTypeReference getJvmTypeReference(Entity e) {
        e.toClass(e.fullyQualifiedName).typeRef
    }

}

以下简单实例可以完美地解析和推断(带有适当的注释).

entity A {
    attr String y;

    new(String y) {
        this.y=y        
    }

}    

entity B extends A {
    new() {
        super("Hello World!")
    }
} 

但是,如果我同时取消注释(并在上面的相应行中注释)语法和推理程序(并重新生成),则上述实例将不再解析.消息为方法super(String)未定义".

我了解如何使继承保持宽松"状态并限制使用验证器等,但我更愿意将其强力键入模型中.

由于XBase和JvmModelInferrer的作用,我不确定如何解决问题,因此我不知道该如何解决.指针(或引用)就足够了.

[...我能够为该语法的 non -xbase版本实现所有范围界定问题...]

解决方案

这将不起作用.您要么必须保留语法,然后自定义投标提供者和验证.否则您必须使用"f.q.n.o.y.Entity".typeRef.您可以使用NodeModelUtils来读取FQN或尝试使用("entities."+entity.superType.name).typeRef

之类的方法

The following canonical XBase entities grammar (from "Implementing Domain-Specific Languages with Xtext and Xtend" Bettini) permits entities to extend any Java class. As the commented line indicates, I would like to grammatically force entities to only inherit from entities.

grammar org.example.xbase.entities.Entities with org.eclipse.xtext.xbase.Xbase

generate entities "http://www.example.org/xbase/entities/Entities"

Model:
    importSection=XImportSection?
    entities+=Entity*;

Entity:
    'entity' name=ID ('extends' superType=JvmParameterizedTypeReference)? '{'
//  'entity' name=ID ('extends' superType=[Entity|QualifiedName])? '{'
        attributes += Attribute*
        constructors+=Constructor*
        operations += Operation*
    '}';

Attribute:
    'attr' (type=JvmTypeReference)? name=ID ('=' initexpression=XExpression)? ';';

Operation:
    'op' (type=JvmTypeReference)? name=ID 
    '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' 
        body=XBlockExpression;

Constructor:    'new'  
    '(' (params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)? ')' 
        body=XBlockExpression;

Here is a working JVMModelInferrer for the model above, again where the commented line (and extra method) reflect my intention.

package org.example.xbase.entities.jvmmodel

import com.google.inject.Inject
import org.eclipse.xtext.common.types.JvmTypeReference
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.example.xbase.entities.entities.Entity

class EntitiesJvmModelInferrer extends AbstractModelInferrer {

    @Inject extension JvmTypesBuilder
    @Inject extension IQualifiedNameProvider

    def dispatch void infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        acceptor.accept(entity.toClass("entities." + entity.name)) [
            documentation = entity.documentation
            if (entity.superType !== null) {
                superTypes += entity.superType.cloneWithProxies
                //superTypes += entity.superType.jvmTypeReference.cloneWithProxies
            }

            entity.attributes.forEach [ a |
                val type = a.type ?: a.initexpression?.inferredType
                members += a.toField(a.name, type) [
                    documentation = a.documentation
                    if (a.initexpression != null)
                        initializer = a.initexpression
                ]
                members += a.toGetter(a.name, type)
                members += a.toSetter(a.name, type)
            ]
            entity.operations.forEach [ op |
                members += op.toMethod(op.name, op.type ?: inferredType) [
                    documentation = op.documentation
                    for (p : op.params) {
                        parameters += p.toParameter(p.name, p.parameterType)
                    }
                    body = op.body
                ]
            ]
            entity.constructors.forEach [ con |
                members += entity.toConstructor [
                    for (p : con.params) {
                        parameters += p.toParameter(p.name, p.parameterType)
                    }
                    body = con.body
                ]
            ]
        ]
    }

    def JvmTypeReference getJvmTypeReference(Entity e) {
        e.toClass(e.fullyQualifiedName).typeRef
    }

}

The following simple instance parses and infers perfectly (with the comments in place).

entity A {
    attr String y;

    new(String y) {
        this.y=y        
    }

}    

entity B extends A {
    new() {
        super("Hello World!")
    }
} 

If, however, I uncomment (and comment in the corresponding line above) both the grammar and the inferrer (and regenerate), the above instance no longer parses. The message is "The method super(String) is undefined".

I understand how to leave the inheritance "loose" and restrict using validators, etc., but would far prefer to strongly type this into the model.

I am lost as to how to solve this, as I am not sure where things are breaking, given the role of XBase and the JvmModelInferrer. A pointer (or reference) would suffice.

[... I am able to implement all the scoping issues for a non-xbase version of this grammar ...]

解决方案

this won't work. you either have to leave the grammar as is and customize proposal provider and validation. or you have to use "f.q.n.o.y.Entity".typeRef. You can use NodeModelUtils to read the FQN or try something like ("entities."+entity.superType.name).typeRef

这篇关于通过Xtext和XBase进行语法限制的JVMModelInferrer继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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