xtextxbase

Import for custom class in XbaseCompiler


I'm writing a DSL using XBase, and I've added a new parser rule which returns an XExpression in the grammar:

DatastepExpression returns xbase::XExpression: {DatastepExpression} 'data' name=ID '{' '}';

and added the appropriate function to the XbaseTypeComputer subclass:

protected def _computeTypes(DatastepExpression expression, ITypeComputationState state) {
    var type = getTypeForName(typeof(FileDataset), state)
    state.acceptActualType(type)
}

Now I'm trying to add in the method to the XbaseCompiler subclass:

override protected doInternalToJavaStatement(XExpression expr, ITreeAppendable it, boolean isReferenced) {
    switch expr {
        DatastepExpression: {
            newLine
            append('''FileDataset «expr.name»;''')
        }
        default:
            super.doInternalToJavaStatement(expr, it, isReferenced)
    }
}

where FileDataset is a custom class in my language API. How do I get this class to appear in the imports at the top of the generated files?

At the moment, when I create a new file in my language (in the runtime Eclipse) the generated Java file contains the FileDataset variable declaration but it has a red wavy line underneath and error "FileDataset cannot be resolved to a type"


Solution

  • ITreeAppendable has methods to append instances of Class, JvmType or LightweightTypeReference. A plain

    it.append(FileDataset).append(' ').append(expr.name)
    

    should do the trick.