I'm trying to learn Xtext by defining a grammar with java-like packages, classes and imports. A fragment of my grammar looks like this with CompilationUnit being the root object.
CompilationUnit:
packageDeclaration=PackageDeclaration?
imports+=ImportDeclaration*
topClass=Class
;
packageDeclaration:
'package' path=QualifiedName ';'
;
ImportDeclaration:
'import' importedNamespace=QualifiedNameWithWildCard ';'
;
Class:
{Class} visibility=Visibility isStatic?='static' 'class' name=ID ('extends' superClass=[Class|QualifiedName])? body=ClassBody
;
For importing cross-references I use the DefaultGlobalScopeProvider and I have overriden the QualifiedNameProvider with my own version that appends the package name as a prefix for the QualifiedName of topClass. For automating the own package import I edited the project specific ScopeProvider. All this seems to be working beatifully and using the generated Eclipse IDE I am able to import classes from other files using "import [packageName].[*|ClassName]". (Still work in progress on automatic imports for Classes in the same package with the referrer, but for the moment I can manage with explicit import)
The next step I'm trying is the validation of the import and package declarations. I want to implement the same limitation as Java that the package declaration of a file should equal to the file's relative path and on the other hand I want to validate the existance of an imported Class or package. The problem is that through the eResource of an EObject I am only able to access the resources full URI (e.g. platform:/resource/Sample/src/mypack/Sample.myjava) while the path name relative to the source folder would be shorter (mypack/Sample.myjava). I haven't yet figured out if I should clip the URI with some logic or have some completely different approach.
One possible idea would perhaps be to somehow gain the URIs of each classpath directory of the project and work from there, but I haven't yet figured out how to do that.
Any idea how I should validate my package and import declarations? I keep having the feeling that I'm very close, but yet so far.
Edit: Deleted a missthought on DefaultGlobalScopeProvider behaviour. That is not related to file hierarchy, but only qualified names. Also I got the automatic imports of own package to work.
Update: Thinking of it, I should propably validate imports just by enumerating available resources and checking their qualified names. Then only the package declaration validation would require file hierarchy checks.
Update2: It seems that in eclipse the resource URI is always in the format "platform:/resource/[Project]/[src folder]/...". Assuming this would mean that I could just hard check against that, but doing this in the grammar project validator would create a grammar-level dependency on Eclipse, which would propably not be a good idea for any serious DSL project. However a comment in this post led me to think that maybe I should consider not doing the package location validation at all in the grammar level, but only in the UI project (which I haven't altered yet). The idea is that the location of a resource should perhaps be left in more abstracted form than expecting a traditional file system hierarchy.