Xtext - multilingual language

I am new to Xtext, so I don't understand all the related concepts very well. There is one question, in particular, I could not find the answer: how can I manage the grammar for a language with several files?

The DSL I'm working on usually uses four files, three of which should be listed in the first. All files have the same extension, but not the same grammar. Is this even possible?

+6
source share
1 answer

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 { // more stuff here } module B { // also more stuff } 

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.

+6
source

Source: https://habr.com/ru/post/946157/