类型推断中的XText链接依赖关系

时间:2013-05-14 13:26:00

标签: types dsl type-inference xtext

在我的实验中,当多个XExpression块之间存在一系列依赖关系时,似乎XText无法解析变量类型。

一个最小的例子,来说明。我有一个语法:

grammar eg.types.inference.TypeInferenceExample with org.eclipse.xtext.xbase.Xbase 

generate typeInferenceExample "example.org/types/inference/TypeInferenceExample"

Model:
    blocks += Block*
;

Block:
    '{'
        'name' ':' name=QualifiedName
        'from' ':' ('none' | from=[Block|QualifiedName])
        'block' ':' expression=XBlockExpression
    '}'
; 

界面:

package eg.lib;

public interface IModelBlock {
    public void push(org.eclipse.xtext.xbase.lib.Pair<String, ?> toPush);
}

和JvmModelInferrer:

package eg.types.inference.jvmmodel

import com.google.inject.Inject
import eg.lib.IModelBlock
import eg.types.inference.typeInferenceExample.Block
import eg.types.inference.typeInferenceExample.Model
import org.eclipse.xtext.xbase.XBinaryOperation
import org.eclipse.xtext.xbase.XExpression
import org.eclipse.xtext.xbase.XFeatureCall
import org.eclipse.xtext.xbase.XStringLiteral
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder

class TypeInferenceExampleJvmModelInferrer extends AbstractModelInferrer {

    @Inject extension JvmTypesBuilder

    def dispatch void infer(Model model, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        model.blocks.forEach [block |
            acceptor.accept(block.toClass(block.name)).initializeLater [
                superTypes += block.newTypeRef(typeof(IModelBlock))
                members += block.toMethod("invoke", newTypeRef(Void::TYPE)) [
                    if (block.from != null) {
                        block.from.pushType.forEach [p |
                            parameters += block.toParameter(p.key, p.value)
                        ]
                    }
                    body = block.expression
                ]
            ]
        ]
    }

    def private pushType(Block block) {
        return block.eAllContents.filter[ // List of push calls in this Block
            it instanceof XFeatureCall && (it as XFeatureCall).concreteSyntaxFeatureName.equals("push")
        ].map [ 
            val call = it as XFeatureCall
            // Add entry for push call as an OutputDeclaration
            return call.featureCallArguments.map[
                if (!(it instanceof XBinaryOperation)) {
                    throw new RuntimeException("Must push using -> operator")
                }
                val key = (it as XBinaryOperation).leftOperand
                val value = (it as XBinaryOperation).rightOperand
                return key.name -> value.inferredType
            ]
        ].head
    }

    def private String name(XExpression literal) {
        if (!(literal instanceof XStringLiteral)) {
            throw new UnsupportedOperationException("Literal was not a string literal")
        }
        return (literal as XStringLiteral).value
    }

}

当我为此DSL创建一个简单示例时,例如:

{
    name : BlockOne
    from : none
    block : {
        val i = 42 * 3.6
        push("index" -> i)
    }
}

{
    name : BlockTwo
    from : BlockOne
    block : {
        val res = "Another Value from " + index
        push("result" -> res)
    }
}

生成代码很好(类型推断在生成输出Java时成功地计算出indexres的类型。我在push的{​​{1}}调用中使用对来推断BlockOne的调用方法的接口。此BlockTwo方法来自上面的push界面。如果我在这个例子中添加第三个块,那么:

IModelBlock

推断失败,{ name : BlockThree from : BlockTwo block : { val out = "This one came from: " + result push("out" -> out) } } (来自UnsupportedOperationException: TODO: import a functional handle on the type resolution that delegates to the best available (current, but evolving) resultOnChangeEvictingCache.execWithoutCacheClear)。

我是否应该使用其他一些技术来推导在XText中具有链式依赖关系的变量类型?

感谢您的帮助!

1 个答案:

答案 0 :(得分:-1)

这似乎是您使用的版本中的错误。尝试使用最新版本。