Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xtext: Inferring type in variable declaration not working in interface generation

Tags:

java

xtext

I am writing my DSL's Model inferrer, which extends from AbstractModelInferrer. Until now, I have successfully generated classes for some grammar constructs, however when I try to generate an interface the type inferrer does not work and I get the following Exception:

0    [Worker-2] ERROR org.eclipse.xtext.builder.BuilderParticipant  - Error during compilation of 'platform:/resource/pascani/src/org/example/namespaces/SLA.pascani'.
java.lang.IllegalStateException: equivalent could not be computed

The Model inferrer code is:

def dispatch void infer(Namespace namespace, IJvmDeclaredTypeAcceptor acceptor, boolean isPreIndexingPhase) {
    acceptor.accept(processNamespace(namespace, isPreIndexingPhase))
}

def JvmGenericType processNamespace(Namespace namespace, boolean isPreIndexingPhase) {
    namespace.toInterface(namespace.fullyQualifiedName.toString) [
        if (!isPreIndexingPhase) {
            documentation = namespace.documentation
            for (e : namespace.expressions) {
                switch (e) {
                    Namespace: {
                        members +=
                            e.toMethod("get" + Strings.toFirstUpper(e.name), typeRef(e.fullyQualifiedName.toString)) [
                                abstract = true
                            ]
                        members += processNamespace(e, isPreIndexingPhase);
                    }
                    XVariableDeclaration: {
                        members += processNamespaceVarDecl(e)
                    }
                }
            }
        }
    ]
}

def processNamespaceVarDecl(XVariableDeclaration decl) {
    val EList<JvmMember> members = new BasicEList();

    val field = decl.toField(decl.name, inferredType(decl.right))[initializer = decl.right]
    // members += field
    members += decl.toMethod("get" + Strings.toFirstUpper(decl.name), field.type) [
        abstract = true
    ]

    if (decl.isWriteable) {
        members += decl.toMethod("set" + Strings.toFirstUpper(decl.name), typeRef(Void.TYPE)) [
            parameters += decl.toParameter(decl.name, field.type)
            abstract = true
        ]
    }

    return members
}

I have tried using the lazy initializer after the acceptor.accept method, but it still does not work.

When I uncomment the line members += field, which adds a field to an interface, the model inferrer works fine; however, as you know, interfaces cannot have fields.

This seems like a bug to me. I have read tons of posts in the Eclipse forum but nothing seems to solve my problem. In case it is needed, this is my grammar:

grammar org.pascani.Pascani with org.eclipse.xtext.xbase.Xbase

import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types
import "http://www.eclipse.org/xtext/xbase/Xbase"

generate pascani "http://www.pascani.org/Pascani"

Model
    :   ('package' name = QualifiedName ->';'?)? 
        imports = XImportSection?
        typeDeclaration = TypeDeclaration?
    ;

TypeDeclaration
    :   MonitorDeclaration 
    |   NamespaceDeclaration
    ;

MonitorDeclaration returns Monitor
    :   'monitor' name = ValidID 
        ('using' usings += [Namespace | ValidID] (',' usings += [Namespace | ValidID])*)?  
        body = '{' expressions += InternalMonitorDeclaration* '}'
    ;

NamespaceDeclaration returns Namespace
    :   'namespace' name = ValidID body = '{' expressions += InternalNamespaceDeclaration* '}'
    ;

InternalMonitorDeclaration returns XExpression
    :   XVariableDeclaration
    |   EventDeclaration
    |   HandlerDeclaration
    ;

InternalNamespaceDeclaration returns XExpression
    :   XVariableDeclaration
    |   NamespaceDeclaration
    ;

HandlerDeclaration
    :   'handler' name = ValidID '(' param = FullJvmFormalParameter ')' body = XBlockExpression
    ;

EventDeclaration returns Event
    :   'event' name = ValidID 'raised' (periodically ?= 'periodically')? 'on'? emitter = EventEmitter ->';'?
    ;

EventEmitter
    :   eventType = EventType 'of' emitter = QualifiedName (=> specifier = RelationalEventSpecifier)? ('using' probe = ValidID)?
    |   cronExpression = CronExpression
    ;

enum EventType
    :   invoke
    |   return
    |   change
    |   exception
    ;

RelationalEventSpecifier returns EventSpecifier
    :   EventSpecifier ({RelationalEventSpecifier.left = current} operator = RelationalOperator right = EventSpecifier)*
    ;

enum RelationalOperator
    :   and
    |   or
    ;

EventSpecifier
    :   (below ?= 'below' | above ?= 'above' | equal ?= 'equal' 'to') value = EventSpecifierValue
    |   '(' RelationalEventSpecifier ')'
    ;

EventSpecifierValue
    :   value = Number (percentage ?= '%')?
    |   variable = QualifiedName
    ;

CronExpression
    :   seconds = CronElement       // 0-59
        minutes = CronElement       // 0-59
        hours   = CronElement       // 0-23
        days    = CronElement       // 1-31
        months  = CronElement       // 1-2 or Jan-Dec
        daysOfWeek = CronElement    // 0-6 or Sun-Sat
    |   constant = CronConstant
    ;

enum CronConstant
    :   reboot      // Run at startup
    |   yearly      // 0 0 0 1 1 *
    |   annually    // Equal to @yearly
    |   monthly     // 0 0 0 1 * *
    |   weekly      // 0 0 0 * * 0
    |   daily       // 0 0 0 * * *
    |   hourly      // 0 0 * * * *
    |   minutely    // 0 * * * * *
    |   secondly    // * * * * * *
    ;

CronElement
   :    RangeCronElement | PeriodicCronElement
   ;

RangeCronElement hidden()
   :    TerminalCronElement ({RangeCronElement.start = current} '-' end = TerminalCronElement)?
   ;

TerminalCronElement
   :    expression = (IntLiteral | ValidID | '*' | '?')
   ;

PeriodicCronElement hidden()
   :    expression = TerminalCronElement '/' elements = RangeCronList
   ;

RangeCronList hidden()
   :    elements += RangeCronElement (',' elements +=RangeCronElement)*
   ;

IntLiteral
    :   INT
    ;

UPDATE

The use of a field was a way to continue working in other stuff until I find a solution. The actual code is:

def processNamespaceVarDecl(XVariableDeclaration decl) {
    val EList<JvmMember> members = new BasicEList();
    val type = if (decl.right != null) inferredType(decl.right) else decl.type

    members += decl.toMethod("get" + Strings.toFirstUpper(decl.name), type) [
        abstract = true
    ]

    if (decl.isWriteable) {
        members += decl.toMethod("set" + Strings.toFirstUpper(decl.name), typeRef(Void.TYPE)) [
            parameters += decl.toParameter(decl.name, type)
            abstract = true
        ]
    }

    return members
}
like image 870
Miguel Jiménez Avatar asked May 30 '26 04:05

Miguel Jiménez


1 Answers

From the answer in the Eclipse forum:

i dont know if that you are doing is a good idea. the inferrer maps your concepts to java concepts and this enables the scoping for the expressions. if you do not have a place for your expressions then it wont work. their types never will be computed

thus i think you have a usecase which is not possible using xbase without customizations. your semantics is not quite clear to me.

Christian Dietrich

My answer:

Thanks Christian, I though I was doing something wrong. If it seems not to be a common use case, then there is no problem, I will make sure the user explicitly defines a variable type.

Just to clarify a little bit, a Namespace is intended to define variables that are used in Monitors. That's why a Namespace becomes an interface, and a Monitor becomes a class.

Read the Eclipse forum thread

like image 150
Miguel Jiménez Avatar answered May 31 '26 17:05

Miguel Jiménez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!