Xtext,多文件交叉引用
Xtext, multi-file Cross-reference
我想调用在另一个文件中声明的变量。
文件的包含是使用交叉引用完成的。以及宣布的电话。这是语法:
Script:
includes+=(Include)* assignments+=(Assignment)* g=GetLog? clock=Clock? tests+=Test*
;
Assignment:
Config |Cosem ;
Include:
'INCLUDE' includedScript=[Script|STRING];
Cosem:
name=ID '=' 'COSEM' '(' classid=INT ',' version=INT ',' obis=STRING ')' ;
AttributeRef:
name=[Cosem] "." attributeRef =IDValue
;
声明是 Cosem
规则。
根据文档,我了解到我必须在 IResourceDescription
中做一些事情,但我不知道具体是什么
编辑
public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider {
QualifiedName qualifiedName(Script script) {
return QualifiedName.create(script.eResource().getURI().trimFileExtension().lastSegment(), script.eResource().getURI().fileExtension());
}
}
您要找的是 "scoping" xtext。它在 YourDslScopeProvider
中实现
这看起来像
class MyDslScopeProvider extends AbstractMyDslScopeProvider {
override getScope(EObject context, EReference reference) {
if (reference === MyDslPackage.Literals.ATTRIBUTE_REF__NAME) {
// we are scoping the AttributeRef.name cross reference
val script = EcoreUtil2.getContainerOfType(context, Script)
if (script !== null) {
val allImportedCosems = script.includes.map[includedScript.assignments.filter(Cosem)].flatten
// put the imported cosems into scope
return Scopes.scopeFor(allImportedCosems)
}
}
super.getScope(context, reference)
}
}
我想调用在另一个文件中声明的变量。 文件的包含是使用交叉引用完成的。以及宣布的电话。这是语法:
Script:
includes+=(Include)* assignments+=(Assignment)* g=GetLog? clock=Clock? tests+=Test*
;
Assignment:
Config |Cosem ;
Include:
'INCLUDE' includedScript=[Script|STRING];
Cosem:
name=ID '=' 'COSEM' '(' classid=INT ',' version=INT ',' obis=STRING ')' ;
AttributeRef:
name=[Cosem] "." attributeRef =IDValue
;
声明是 Cosem
规则。
根据文档,我了解到我必须在 IResourceDescription
中做一些事情,但我不知道具体是什么
编辑
public class MyDslQNP extends DefaultDeclarativeQualifiedNameProvider {
QualifiedName qualifiedName(Script script) {
return QualifiedName.create(script.eResource().getURI().trimFileExtension().lastSegment(), script.eResource().getURI().fileExtension());
}
}
您要找的是 "scoping" xtext。它在 YourDslScopeProvider
这看起来像
class MyDslScopeProvider extends AbstractMyDslScopeProvider {
override getScope(EObject context, EReference reference) {
if (reference === MyDslPackage.Literals.ATTRIBUTE_REF__NAME) {
// we are scoping the AttributeRef.name cross reference
val script = EcoreUtil2.getContainerOfType(context, Script)
if (script !== null) {
val allImportedCosems = script.includes.map[includedScript.assignments.filter(Cosem)].flatten
// put the imported cosems into scope
return Scopes.scopeFor(allImportedCosems)
}
}
super.getScope(context, reference)
}
}