javaxtexteclipse-emfxtendxbase

creating a simple domain-specific language using xtext-xbase-xtend toolchain


This is for a class project for creating a domain-specific language (DSL). Nothing fancy to consider. For myself, I've set this project as purely an exercise to learning the steps involved.

Files involved (with attached code files)::

1) Grammer in Xtext (entities.xtext)

grammar org.example.xbase.entities.Entities with org.eclipse.xtext.xbase.Xbase  

generate entities "http://www.example.org/xbase/entities/Entities"


Model:
    entities+=Entity*;


Entity:
    'entity' name=ID ('extends'
        superType=JvmParameterizedTypeReference)?
        '{'
            attributes+=Attribute*
            operations+=Operation*
        '}';

Attribute:
    'attr'(type=JvmTypeReference)? name=ID
    ('=' initexpression=XExpression)? ';';

Operation:
    'op'(type=JvmTypeReference)? name=ID
    '(' (params+=FullJvmFormalParameter (',' 
        params+=FullJvmFormalParameter)*)? ')'
    body=XBlockExpression;

2) JvmModelInferrer in xtend (entitiesJvmModelInferrer.xtend)

package org.example.xbase.entities.jvmmodel

import com.google.inject.Inject
import org.eclipse.xtext.xbase.jvmmodel.AbstractModelInferrer
import org.eclipse.xtext.xbase.jvmmodel.IJvmDeclaredTypeAcceptor
import org.eclipse.xtext.xbase.jvmmodel.JvmTypesBuilder
import org.eclipse.xtext.naming.IQualifiedNameProvider
import org.example.xbase.entities.entities.Entity

class EntitiesJvmModelInferrer extends AbstractModelInferrer {
@Inject extension JvmTypesBuilder
@Inject extension IQualifiedNameProvider

    def dispatch void infer(Entity entity, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
        acceptor.accept(entity.toClass("Animals."+entity.name)).
            initializeLater[
                documentation= entity.documentation
                if(entity.superType !=null)
                    superTypes += entity.superType.cloneWithProxies

                    //Create fields for each attribute in the entity
                    entity.attributes.forEach[
                        a | val type = a.type ?: a.initexpression?.inferredType
                        members += a.toField(a.name,type) [
                            documentation=a.documentation
                            if(a.initexpression !=null)
                                initializer = a.initexpression
                        ]
                        //create getter method
                        members +=a.toGetter(a.name,type)
                        //create setter method
                        members +=a.toSetter(a.name,type)
                    ]   
                    //Create method for each operation in the entity
                    entity.operations.forEach[
                        op|
                        members+= op.toMethod(op.name,op.type?:inferredType)[
                            documentation=op.documentation
                            for (p:op.params){
                                parameters +=p.toParameter(p.name,p.parameterType)
                            }
                            //create a main method in one of the classes, when called. Make sure it is also static!!!
                            if(op.name.equals('main')){
                                static=true
                            }
                            body=op.body
                        ]
                    ]

            ]
}
}

3) Source files in the new language (Animal.xentities and Main.xentities). The .xentities extensions are specific for files for this DSL project. - Main.xentities

entity Main{
    //define an instance of Animal
    attr me=new Animal;
    op main(){
        println('main-func')
    }
}

-generated Main.java

package Animals;

import Animals.Animal;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.InputOutput;

@SuppressWarnings("all")
public class Main {
  private Animal me = new Function0<Animal>() {
    public Animal apply() {
      Animal _animal = new Animal();
      return _animal;
    }
  }.apply();

  public Animal getMe() {
    return this.me;
  }

  public void setMe(final Animal me) {
    this.me = me;
  }

  public static String main() {
    //This is the main function I created to
    //have an entry point for program execution
    String _println = InputOutput.<String>println("main-func");
    return _println;
  }
}

-Animal.xentities

entity Animal{
//define an equivalent class in my domain-specific language
attr int nlegs;
op printAnimalSound(){
    println('I grunt and sniff')
}
}

-Generated Animal.java code

package Animals;

import org.eclipse.xtext.xbase.lib.InputOutput;

@SuppressWarnings("all")
public class Animal {
  private int nlegs;

  public int getNlegs() {
    return this.nlegs;
  }

  public void setNlegs(final int nlegs) {
    this.nlegs = nlegs;
  }

  public String printAnimalSound() {
    String _println = InputOutput.<String>println("I grunt and sniff");
    return _println;
  }
}

My Objective:: I want to develop a basic workflow infrastructure so that I can define classes with associated functions and attributes. Then I want to be able to execute them.

My problem:: Keep getting a message that says 'selection does not contain a main type'.

My attempt: In entity(class) 'Main.xentities', I create a 'op main()' function. In the generated .java code, this will show up as a public static String main() function. I thought this would work. However it didn't. I don't know what is missing. Any help is welcome.


Solution

  • Of course, this is not going to work!! The main method must return void and take arguments of type String[] args; or

    public static void main(String[] args){ ...}
    

    I will need to modify the JvmModelInferrer file to make this happen.