Restrict references scope to objects declared before (including objects from imports) [message #1773761] |
Wed, 04 October 2017 06:23  |
Eclipse User |
|
|
|
Hello,
I am currently working on the validation of my DSL.
I would like to restrict references to all objects declared before in the text (both included and locally defined objects)
Here is an example grammar :
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
specification :
includes+=include*
definitions+=definition*
;
include :
'#include' importURI=STRING ';'
;
definition :
module | var_dcl
;
module :
'module' name=ID '{'
definitions+=definition*
'}'
;
type_dcl :
'typedef' type=type_spec name=ID ';'
;
var_dcl :
type=type_spec name=ID ';'
;
type_spec :
baseType | type_ref
;
type_ref :
referencedType = [type_dcl | scoped_name]
;
baseType : 'double' | 'int' | 'char' ;
scoped_name: '.'? ID ('.' ID)*;
and an example model:
test2.myDSL
test.myDSL
#include "test2" ;
module myModule {
Int1 var1; // OK, Int1 is defined before
Int2 var2; // Wrong, Int2 is defined after
Int3 var3; // Wrong, Int3 is defined after
typedef int Int2;
};
typedef int Int3;
I use the ImportURIScopingFragment in the MWE2 and I've bound an ImportURIResolver custom implementation in the RuntimeModule which resolves the include directive path against a list of base paths stored in the preferences. All references are correctly resolved.
Now, I'd like to restrict the scope to objects defined before their use as a reference.
I'm trying to cope with a custom scope provider, but I find it quite difficult...
Help welcome !
Erwann
|
|
|
|
Re: Restrict references scope to objects declared before (including objects from imports) [message #1778484 is a reply to message #1778469] |
Thu, 14 December 2017 13:19  |
Eclipse User |
|
|
|
what es your exact problem doing that.?
unfortuantely what you shared is very few. maybe this pseudocode helps:
class MyDslScopeProvider extends AbstractMyDslScopeProvider {
override getScope(EObject context, EReference reference) {
if (reference == MyDslPackage.Literals.TYPE_REF__REFERENCED_TYPE) {
val List<EObject> scope = new ArrayList
val root =EcoreUtil2.getContainerOfType(context, specification)
// traverse model until you are at context
//scope += element found
return org.eclipse.xtext.scoping.Scopes.scopeFor(scope, delegateGetScope(context,reference))
}
super.getScope(context, reference)
}
}
your grammar naming convention is terrible btw
|
|
|
Powered by
FUDForum. Page generated in 0.03271 seconds