[英]How to resolve field in qualifed name expression
這是我的“類型” 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 )
}
}
在語法上
QName:
data=[Data|ID] '.' path=[Field|ID]
;
因此, ab
將被分為兩個參考范圍。 因此,您要么必須在范圍提供者中反映出來
// TODO: context will be a qname. ask it for its data. ask that for its data and collect fields from there and then
// scope for path
EObjectDescription.create(
QualifiedName.create(field.name ),
field ))
例如
override getScope(EObject context, EReference reference) {
if (reference === MyDslPackage.Literals.QNAME__PATH) {
if (context instanceof QName) {
val result = newArrayList
for (field : context.data.type.fields) {
result.add(EObjectDescription.create(QualifiedName.create(field.name), field))
}
System.err.println(result)
return new SimpleScope(IScope.NULLSCOPE, result)
}
}
super.getScope(context, reference)
}
或者你有語法來反映你的范圍
DataOrField: Data | Field;
QName: dataOrField=[DataOrField|FQN]
FQN: ID ("." ID)?;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.