5

I try to define some XText for specifiying variables according to following syntax

variables
  MyVar1 : Bool at 0x020 value=true;
  MyVar2, MyVar3 : Int at 0x030 value 200;
end-variables

So each definition is syntactically

VarName ["," VarName]* ":" Type ["at" HEX]? ["value" VALUE]? ";"

All variables shall be accessible by their reference and the result in the outline shall be someting like this:

variables
+-MyVar1 : Bool
+-MyVar2 : Int
+-MyVar3 : Int

Edit: As requested here my actual grammar, which is equivalent to the syntactical definition some lines above.

Variable:
  name=ID
;
Declaration_Var:
  'variables'
  vars+=Declaration_Var_Body+
  'end-variables' ';'
;
Declaration_Var_Body:
  varDecl+=Variable(',' varDecl +=Variable)*
  ':' type=[TR_Any]
  ('at' address=HEX)?
;
TR_Any:
  ...
;
terminal HEX:
  ...
;

With this, following sequence of characters

variables
  Test1, Test2, Test3 : DWORD at 0x20;
end_var;

results in an outline like this:

<unnamed>
+-- 0x20
|   +-- Test1
|   +-- Test2
|   +-- Test3

which is almost the opposite of what I expected. What I expected and what I want to generate in the outline is something similar to this (the datatype and address must not show up there, but at least they must be accessible as properties of the generated class for variable declarations)

Test1
+-- DWORD
+-- 0x20
Test2
+-- DWORD
+-- 0x20
Test3
+-- DWORD
+-- 0x20
4
  • Could you please post your current grammar and ask concrete questions? Mar 15, 2013 at 15:24
  • I added some more detailed description there
    – abs
    Mar 18, 2013 at 18:07
  • Ok, now I understand your problem. Are you actually only interested in getting nice looking Outline or also getting inner structures altered in the way you described? Mar 19, 2013 at 10:44
  • I also want the internal structures to match this structure, because I think it will make further development much easier.
    – abs
    Mar 19, 2013 at 17:07

1 Answer 1

4

See the screencast covering your case here: http://xtextcasts.org/episodes/18-model-optimization

You need to modify the model and meta-model. To modify the meta-model you need to define a post-processor. Following post-processor adds type attribute directory to the Variable class.

For more details see: http://christiandietrich.wordpress.com/tag/postprocessor/

class MyXtext2EcorePostProcessor implements IXtext2EcorePostProcessor {
  override process(GeneratedMetamodel metamodel) {
    metamodel.EPackage.process
  }

  def process(EPackage p) {
    for (clazz : p.EClassifiers.filter(typeof(EClass))) {
      if (clazz.name == typeof(Variable).simpleName) {
        val typeAttribute = EcoreFactory::eINSTANCE.createEAttribute
        typeAttribute.name = "type"
        typeAttribute.EType = EcorePackage::eINSTANCE.EString
        clazz.EStructuralFeatures += typeAttribute
      }
    }
  }
}

Then you have to bind it extending Generator like this:

public class ExtendedGenerator extends Generator {
  public ExtendedGenerator() {
    new XtextStandaloneSetup() {
      @Override
      public Injector createInjector() {
        return Guice.createInjector(new XtextRuntimeModule() {
          @Override
          public Class<? extends IXtext2EcorePostProcessor>
                                 bindIXtext2EcorePostProcessor() {
            return MyXtext2EcorePostProcessor.class;
          }
        });
      }
    }.createInjectorAndDoEMFRegistration();
  }
}

And finally use the new ExtendedGenerator in your mwe2-workflow:

... 
Workflow {
  ...
  bean = StandaloneSetup {
    ...
    component = postprocessor.ExtendedGenerator { // Set ExtendedGenerator!
      ...
    }
    ...
  }
  ...
}
...

Then you have to fill your new type attribute with data. You can do it implementing the IDerivedStateComputer interface.

class MyDerivedStateComputer implements IDerivedStateComputer {

  override discardDerivedState(DerivedStateAwareResource resource) {
    resource.allContents.filter(typeof(VariableDefinition)).forEach [
      type = null
    ]
  }

  override installDerivedState(DerivedStateAwareResource resource,
                               boolean preLinkingPhase) {
    resource.allContents.filter(typeof(VariableDefinition)).forEach [
      type = (eContainer as DefinitionBlock).type
    ]
  }
}

Then you have to bind it like this (second and third bind-methods are only necessary for non-Xbase-projects)

public class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
  public Class<? extends IDerivedStateComputer> bindIDerivedStateComputer() {
    return MyDerivedStateComputer.class;
  }

  // Not needed for Xbase-projects
  @Override
  public Class<? extends XtextResource> bindXtextResource() {
    return DerivedStateAwareResource.class;
  }

  // Not needed for Xbase-projects
  public Class<? extends IResourceDescription.Manager>
                         bindIResourceDescriptionManager() {
    return DerivedStateAwareResourceDescriptionManager.class;
  }
}

You can go even further and restructure your model on the fly to suit your needs. Please, post your solution here as a separate answer in case you go that far.

1
  • Not exactly what I'm looking for. In your Grammar a variable list can be specified like MyVar1 : MyType, MyVar2 : MyType,... I need to specify them like this: MyVar1, MyVar2,... : MyType (See the syntactical definition in the question)
    – abs
    Mar 18, 2013 at 18:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.