Couldn't resolve reference to Field 'x' [message #1811345] |
Sat, 31 August 2019 12:37  |
Eclipse User |
|
|
|
Here is my 'Types' xText grammar:
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
;
Here is an instance of the 'Types' grammar:
struct Sample {
number n
string s
}
Sample sample
sample.n = 12
sample.s = "Hello"
The two last lines, which reference fields 'n' and 's', generates the error:
Quote:Couldn't resolve reference to Field 'x'.'
I've coded the following custom scope provider without success:
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 ) {
result.add(
EObjectDescription.create(
QualifiedName.create( data.name ),
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 )
}
}
How can I improve TypesScopeProvider to resolve 'n' and 's' as field of stuct 'Sample'?
|
|
|
|
|
|
|
Re: Couldn't resolve reference to Field 'x' [message #1814063 is a reply to message #1811579] |
Mon, 02 September 2019 12:04  |
Eclipse User |
|
|
|
Here is the solution, thanks to Christian Dietrich on StackOverflow.
class TypesScopeProvider extends AbstractTypesScopeProvider {
override getScope( EObject context, EReference reference ) {
if( reference === TypesPackage.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)
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03526 seconds