How can I manage the grammar for a language with multiple files?
Xtext parses the file first, and then links the links. These cross-references can be โinternalโ in the file or โexternalโ. In both cases, linking and scoping ystems will do the hard work.
All files have the same extension, but not the same grammar. Is this even possible?
This seems to be another question, but alas ...
If the grammars are really different from each other, it will not be easy for you to work with Xtext. If Xtext sees a .foo file, how should it decide which parser should be used? Try each one until an error occurs? But what if the file is written in grammar B but does contain syntax errors? ...
But often there is a little trick: indeed the grammar is one , but the grammar contains two almost separate parts. Which part is used is calculated by the first few keywords in the file.
A small example:
A.foo file:
module A {
B.foo file:
system X { use module A use module B }
Grammar might look like this:
Model: Modules | Systems; Modules: modules += Module; Module: 'module' name=ID '{' '}'; Systems: systems += System; System: 'system' name=ID '{' used+=UsedModule* '}'; UsedModule: 'use' 'module' module=[Module];
In this grammar, a single file can contain only module XOR system definitions, but not a combination of them. The first occurrence of the module or system keyword determines what is allowed.
source share