Xtext: Associating Different Root Elements with Different File Extensions

StackOverflow https://stackoverflow.com/questions/21615399

  •  08-10-2022
  •  | 
  •  

Frage

To best illustrate my requirement, consider a java-like language that has classes and interfaces.

This language requires all classes to be in a file with the extension '.classdef', and all interfaces must be in files with the extension '.intdef'. The root grammar element in these files would be different.

In the interface file it would have a root element of Interface which could contain instances of AbstractMethod and StaticConstant.

The root grammar element for '.classdef' would be Class and could contain instances of MethodDeclaration, MemberVariable, StaticConstant etc.

Is it possible with xtext to have a different root grammar element depending on what the file extension is, from a single grammar file? It doesn't make sense to me to have to separate this into two grammars, that are related. I don't want two eclipse plugins / bundles for the different grammars.


The above is just an illustration. In reality, I have two different file types... but the two supported set of members between the two file types almost entirely overlap.

In theory, I could implement it within a validator, to prevent you from using the omitted type in one of the files. But then I'd also need to filter the suggestion out of the content assistant, etc. etc.

War es hilfreich?

Lösung

what about multiple dsls/languages in combination with inheritance e.g.

  • baselang (no editor contribution)
  • classlang extends baselang
  • interfacelang extends baselang
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top