4

I would like to reuse grammar definitions.

I have a grammar like this:

Person:
  'contact' name=ID '{'
    'phone' phone=INT
  '}'
;

I would like to have another grammar like this:

include "uri/to/other/project/to/other/grammar/definitions"

Call:
  'call' person=Person
;

Person is not known by the second grammar. Is Xtext therefore able to insert or include the Person definition from the first grammar into the second grammar?

A further step is the generation of the Person. I would like to know how to accomplish that too.

bline
  • 227
  • 3
  • 15
  • 1
    I had the same need and haven't found another solution than merging both grammars... – Aubin May 08 '13 at 15:28
  • Copy/Paste/Integrate one into the other, by hand! – Aubin May 08 '13 at 17:23
  • Use "import"? It's even in the getting started tutorials... I must be missing something you need. – Ed Staub May 08 '13 at 23:32
  • As is understand, import allows to refer to an existing `Person` at runtime of the editor. E.g. you can refer from your calls.mydsl2 to an existing EObject of persons.mydsl1. This is a kind of scoping across diffrent DSLs. Plus, you cannot declare the feature `person` in `Call` as i stated, because the import keyword just allows referencing and nothing else (please correct me if i'm wrong). What i want is that Xtext can lookup definitions in other xtext files(in whatever project they are) while i am writing a new grammar and compile the dsl with those definitions included. – bline May 09 '13 at 07:42

2 Answers2

4

I found the solution. You can use the keyword "with" as it is used to include the Terminals.

The necessary steps:

  1. Create Xtext Project com.mydsl.A (A) and com.mydsl.B (B)
  2. Write grammar for A
  3. Add A as dependency in META-INF/MANIFEST of B
  4. Add A.ui as dependency in META-INF/MANIFEST of B.ui
  5. Add registration of A's genmodel in B's workflow like that: In StandaloneSetup: registerGenModelFile = "platform:/resource/A/src-gen/path/to/A.genmodel"
  6. Change first line of B to grammar B with A
  7. You can use EClasses of A now while writing grammar B

with cannot be used to include more than one grammar, so the terminal definitions must be stated in A.

The generation is executed in B's IGenerator, but you can reuse generation of A's EClasses if you extend A's Generator.

This approach is a kind of inheritance, since proposal, validation, etc classes are extended by A's counterparts. I didn't find out if multiple inheritance is supported. You can place a comma after with A but it doesn't work.

bline
  • 227
  • 3
  • 15
0

I think accessing elements of a model expressed in dsl A from a model expressed in DSL B (with another meta-model) is not exactly what bline requested. Although he gave a correct answer to his own problem, I think it's usefull to read the documentation on this: https://eclipse.org/Xtext/documentation/301_grammarlanguage.html#grammar-mixins especially when it comes to overwriting rules and priorities.

Community
  • 1
  • 1
Emilien
  • 2,385
  • 16
  • 24