这是我的“类型”Xtext 语法:
grammar sample.types.Types with org.eclipse.xtext.common.Terminals
generate types "http://www.types.sample/Types"
Model:
structs += Struct*
data += Data*
assignments += Assignment*
;
Struct:
'struct' name=ID '{'
fields += Field*
'}'
;
Field:
type=Type name=ID
;
Type:
'number'
| 'string'
;
Data:
type=[Struct|ID] name=ID
;
Assignment:
qname=QName '=' value=Value
;
QName:
data=[Data|ID] '.' path=[Field|ID]
;
Value:
INT
| STRING
;
这是“类型”语法的一个实例:
struct SampleA {
number n
string s
}
struct SampleB {
number n
string s
}
SampleA sampleA1
SampleA sampleA2
SampleB sampleB
sampleA1.n = 12
sampleA1.s = "Hello"
sampleA2.n = 12
sampleA2.s = "Hello"
sampleB.n = 42
sampleB.s = "Hello"
引用字段“n”和“s”的最后六行会生成错误:
无法解析对字段“x”的引用。
我编写了以下自定义范围提供程序但没有成功:
class TypesScopeProvider extends AbstractTypesScopeProvider {
override getScope( EObject context, EReference reference ) {
if( reference === TypesPackage.Literals.QNAME__PATH ) {
val model = EcoreUtil2.getContainerOfType(context, Model)
if( model !== null ) {
val result = newArrayList
for( data : model.data ) {
for( field : data.type.fields ) {
result.add(
EObjectDescription.create(
QualifiedName.create( data.name, field.name ),
field ))
}
}
return new SimpleScope(IScope.NULLSCOPE, result)
}
}
super.getScope( context, reference )
}
}