Xtext - multiple files language
Asked Answered
E

1

7

I'm pretty new to Xtext, so I don't understand very well all of the associated concepts. There's one question in particular I couldn't find an answer to:
how can I manage a grammar for a language with multiple files?

The DSL I'm working on typically uses four files, three of which should be referenced in the first one. All files share the same extension, though not the same grammar. Is that possible at all?

Elea answered 30/5, 2013 at 8:17 Comment(0)
A
7

How can I manage a grammar for a language with multiple files?

Xtext first parses the file, and then links crossreferences. These crossreferences can be "internal" in a file or "external". In both cases the linking and the scoping ystems will do the hard work for you.

All files share the same extension, though not the same grammar. Is that possible at all?

This seems to be a different question, but alas...

If the grammars are really different then you will have a hard time with Xtext. If Xtext sees a .foo file, how should it decide, which parser should be applied? Try each one until no error occurs? And what if the file is written in grammar B but really contains syntax errors? ...

But often there is a little trick: The is really one grammar, but the grammar contains two nearly separate parts. Which part is used is calculated by the first few keywords in the file.

A small example:

File A.foo:

module A {
   // more stuff here
}
module B {
   // also more stuff
}

File B.foo:

system X {
   use module A
   use module B
}

The 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 one file can only contain either module XOR system definitions but not a mix of them. The first occurrence of the keyword module or system determines what is allowed.

Addington answered 30/5, 2013 at 11:37 Comment(1)
Great answer! You might fix the links as well :)Ordinate

© 2022 - 2024 — McMap. All rights reserved.