Search in sources :

Example 6 with TemplateGroup

use of com.eprosima.idl.generator.manager.TemplateGroup in project ihmc-pub-sub by ihmcrobotics.

the class IDLGenerator method execute.

/**
 * Generate java classes from an IDL file
 *
 * @param idlFile IDL file to parse
 * @param packageName Target package (IDL Module gets added to this)
 * @param targetDirectory Directory to save the generated files in. The whole package structure is generated in this directory
 * @throws IOException
 */
public static void execute(File idlFile, String packageName, File targetDirectory, List<File> includePath) throws IOException {
    String idlFilename = idlFile.getAbsolutePath();
    Field field;
    try {
        field = TemplateManager.class.getDeclaredField("m_loaderDirectories");
        field.setAccessible(true);
        field.set(null, "us/ihmc/idl/templates");
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
        throw new RuntimeException("API changed, fixme", e);
    }
    String onlyFileName = Util.getIDLFileNameOnly(idlFilename);
    IDLContext context = new IDLContext(onlyFileName, idlFilename, new ArrayList<>());
    context.setPackage(packageName);
    TypeCode.javapackage = context.isIsPackageEmpty() ? "" : (context.getPackage() + ".");
    // Create default @default annotation.
    AnnotationDeclaration defaultAnnotation = context.createAnnotationDeclaration("defaultValue", null);
    defaultAnnotation.addMember(new AnnotationMember("value", new PrimitiveTypeCode(TypeCode.KIND_STRING), ""));
    // Create default @Key annotation.
    AnnotationDeclaration keyann = context.createAnnotationDeclaration("Key", null);
    keyann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(TypeCode.KIND_BOOLEAN), "true"));
    // Create default @Topic annotation.
    AnnotationDeclaration topicann = context.createAnnotationDeclaration("Topic", null);
    topicann.addMember(new AnnotationMember("value", new PrimitiveTypeCode(TypeCode.KIND_BOOLEAN), "true"));
    AnnotationDeclaration abstractann = context.createAnnotationDeclaration("Abstract", null);
    abstractann.addMember(new AnnotationMember("type", new PrimitiveTypeCode(TypeCode.KIND_STRING), "java.lang.Object"));
    abstractann.addMember(new AnnotationMember("impl", new PrimitiveTypeCode(TypeCode.KIND_STRING), ""));
    AnnotationDeclaration typecode = context.createAnnotationDeclaration("TypeCode", null);
    typecode.addMember(new AnnotationMember("type", new PrimitiveTypeCode(TypeCode.KIND_BOOLEAN), "INVALID_TYPE_CODE"));
    // Create template manager
    TemplateManager tmanager = new TemplateManager("FastCdrCommon:Common");
    // Create main template
    TemplateGroup maintemplates = tmanager.createTemplateGroup("main");
    maintemplates.setAttribute("ctx", context);
    if (idlFile.exists()) {
        Reader reader = createPreProcessedInputStream(idlFile, includePath);
        ANTLRInputStream input = new ANTLRInputStream(reader);
        IDLLexer lexer = new IDLLexer(input);
        lexer.setContext(context);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        // printTokenStream(tokens);
        IDLParser parser = new IDLParser(tokens);
        // Pass the filename without the extension
        parser.specification(context, tmanager, maintemplates);
        File packageDir = new File(targetDirectory, context.getPackageDir());
        if (packageDir.isDirectory() || packageDir.mkdirs()) {
            TypesGenerator gen = new TypesGenerator(tmanager, true);
            if (!gen.generate(context, packageDir.getPath() + "/", context.getPackage(), null)) {
                throw new IOException("Cannot create Java files");
            }
        } else {
            throw new IOException("Cannot create output dir " + packageDir);
        }
    } else {
        throw new IOException("The File " + idlFilename + " was not found.");
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Reader(java.io.Reader) CppReader(org.anarres.cpp.CppReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) AnnotationMember(com.eprosima.idl.parser.tree.AnnotationMember) IDLParser(com.eprosima.idl.parser.grammar.IDLParser) PrimitiveTypeCode(com.eprosima.idl.parser.typecode.PrimitiveTypeCode) Field(java.lang.reflect.Field) TemplateGroup(com.eprosima.idl.generator.manager.TemplateGroup) AnnotationDeclaration(com.eprosima.idl.parser.tree.AnnotationDeclaration) IDLLexer(com.eprosima.idl.parser.grammar.IDLLexer) TemplateManager(com.eprosima.idl.generator.manager.TemplateManager) File(java.io.File) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Example 7 with TemplateGroup

use of com.eprosima.idl.generator.manager.TemplateGroup in project ihmc-pub-sub by ihmcrobotics.

the class IDLParser method module.

public final ModuleContext module() throws RecognitionException {
    ModuleContext _localctx = new ModuleContext(_ctx, getState());
    enterRule(_localctx, 6, RULE_module);
    Module moduleObject = null;
    TemplateGroup moduleTemplates = null;
    TemplateGroup tg = null;
    // Store old namespace.
    String name = null, old_scope = ctx.getScope();
    Token tk = null;
    try {
        enterOuterAlt(_localctx, 1);
        {
            {
                setState(375);
                match(KW_MODULE);
            }
            tk = _input.LT(1);
            setState(377);
            ((ModuleContext) _localctx).identifier = identifier();
            name = ((ModuleContext) _localctx).identifier.id;
            // Check if the module already was defined.
            moduleObject = ctx.existsModule(ctx.getScope() + "::" + name);
            if (moduleObject != null) {
                // Add the module to the context.
                ctx.addModule(moduleObject);
            } else {
                // Create the Module object.
                moduleObject = new Module(ctx.getScopeFile(), ctx.isInScopedFile(), ctx.getScope(), name, tk);
            }
            if (ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
                if (tmanager != null) {
                    moduleTemplates = tmanager.createTemplateGroup("module");
                    moduleTemplates.setAttribute("ctx", ctx);
                    // Set the module object to the TemplateGroup of the module.
                    moduleTemplates.setAttribute("module", moduleObject);
                }
            }
            // Update to a new namespace.
            if (old_scope.isEmpty())
                ctx.setScope(name);
            else
                ctx.setScope(old_scope + "::" + name);
            setState(379);
            match(LEFT_BRACE);
            setState(380);
            ((ModuleContext) _localctx).definition_list = definition_list(moduleObject);
            tg = ((ModuleContext) _localctx).definition_list.dlTemplates;
            if (moduleTemplates != null && tg != null)
                moduleTemplates.setAttribute("definition_list", tg);
            setState(382);
            match(RIGHT_BRACE);
            // Set the old namespace.
            ctx.setScope(old_scope);
            // Create the returned data.
            ((ModuleContext) _localctx).returnPair = new Pair<Module, TemplateGroup>(moduleObject, moduleTemplates);
        }
    } catch (RecognitionException re) {
        _localctx.exception = re;
        _errHandler.reportError(this, re);
        _errHandler.recover(this, re);
    } finally {
        exitRule();
    }
    return _localctx;
}
Also used : TemplateGroup(com.eprosima.idl.generator.manager.TemplateGroup) Pair(com.eprosima.idl.util.Pair)

Example 8 with TemplateGroup

use of com.eprosima.idl.generator.manager.TemplateGroup in project ihmc-pub-sub by ihmcrobotics.

the class IDLParser method enum_type.

public final Enum_typeContext enum_type() throws RecognitionException {
    Enum_typeContext _localctx = new Enum_typeContext(_ctx, getState());
    enterRule(_localctx, 174, RULE_enum_type);
    String name = null;
    Vector<TypeCode> vector = null;
    EnumTypeCode enumTP = null;
    TemplateGroup enumTemplates = null;
    try {
        enterOuterAlt(_localctx, 1);
        {
            setState(1081);
            match(KW_ENUM);
            setState(1082);
            ((Enum_typeContext) _localctx).identifier = identifier();
            name = ((Enum_typeContext) _localctx).identifier.id;
            enumTP = new EnumTypeCode(ctx.getScope(), name);
            setState(1084);
            match(LEFT_BRACE);
            setState(1085);
            enumerator_list(enumTP);
            setState(1086);
            match(RIGHT_BRACE);
            if (ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
                if (tmanager != null) {
                    enumTemplates = tmanager.createTemplateGroup("enum_type");
                    enumTemplates.setAttribute("ctx", ctx);
                    enumTemplates.setAttribute("enum", enumTP);
                }
            }
            // Return the returned data.
            vector = new Vector<TypeCode>();
            vector.add(enumTP);
            ((Enum_typeContext) _localctx).returnPair = new Pair<Vector<TypeCode>, TemplateGroup>(vector, enumTemplates);
        }
    } catch (RecognitionException re) {
        _localctx.exception = re;
        _errHandler.reportError(this, re);
        _errHandler.recover(this, re);
    } finally {
        exitRule();
    }
    return _localctx;
}
Also used : TemplateGroup(com.eprosima.idl.generator.manager.TemplateGroup) Vector(java.util.Vector) Pair(com.eprosima.idl.util.Pair)

Example 9 with TemplateGroup

use of com.eprosima.idl.generator.manager.TemplateGroup in project ihmc-pub-sub by ihmcrobotics.

the class IDLParser method interface_decl.

public final Interface_declContext interface_decl(Vector<Annotation> annotations) throws RecognitionException {
    Interface_declContext _localctx = new Interface_declContext(_ctx, getState(), annotations);
    enterRule(_localctx, 12, RULE_interface_decl);
    Token tk = null;
    Interface interfaceObject = null;
    TemplateGroup interfaceTemplates = null;
    TemplateGroup tg = null;
    String name = null, old_scope = ctx.getScope();
    int _la;
    try {
        enterOuterAlt(_localctx, 1);
        {
            {
                setState(399);
                _la = _input.LA(1);
                if (_la == KW_ABSTRACT || _la == KW_LOCAL) {
                    {
                        setState(398);
                        _la = _input.LA(1);
                        if (!(_la == KW_ABSTRACT || _la == KW_LOCAL)) {
                            _errHandler.recoverInline(this);
                        }
                        consume();
                    }
                }
                {
                    setState(401);
                    match(KW_INTERFACE);
                }
                tk = _input.LT(1);
                setState(403);
                ((Interface_declContext) _localctx).identifier = identifier();
                name = ((Interface_declContext) _localctx).identifier.id;
                // Create the Interface object.
                interfaceObject = ctx.createInterface(name, tk);
                // Add annotations.
                for (Annotation annotation : annotations) interfaceObject.addAnnotation(ctx, annotation);
                if (ctx.isInScopedFile() || ctx.isScopeLimitToAll()) {
                    if (tmanager != null) {
                        interfaceTemplates = tmanager.createTemplateGroup("interface");
                        interfaceTemplates.setAttribute("ctx", ctx);
                        // Set the the interface object to the TemplateGroup of the module.
                        interfaceTemplates.setAttribute("interface", interfaceObject);
                    }
                }
                // Update to a new namespace.
                if (old_scope.isEmpty())
                    ctx.setScope(name);
                else
                    ctx.setScope(old_scope + "::" + name);
                setState(406);
                _la = _input.LA(1);
                if (_la == COLON) {
                    {
                        setState(405);
                        interface_inheritance_spec(interfaceObject);
                    }
                }
                setState(408);
                match(LEFT_BRACE);
                setState(409);
                ((Interface_declContext) _localctx).interface_body = interface_body(interfaceObject);
                tg = ((Interface_declContext) _localctx).interface_body.elTemplates;
                if (interfaceTemplates != null && tg != null)
                    interfaceTemplates.setAttribute("export_list", tg);
                setState(411);
                match(RIGHT_BRACE);
            }
            // Set the old namespace.
            ctx.setScope(old_scope);
            // Create the returned data.
            ((Interface_declContext) _localctx).returnPair = new Pair<Interface, TemplateGroup>(interfaceObject, interfaceTemplates);
        }
    } catch (RecognitionException re) {
        _localctx.exception = re;
        _errHandler.reportError(this, re);
        _errHandler.recover(this, re);
    } finally {
        exitRule();
    }
    return _localctx;
}
Also used : TemplateGroup(com.eprosima.idl.generator.manager.TemplateGroup) Pair(com.eprosima.idl.util.Pair)

Example 10 with TemplateGroup

use of com.eprosima.idl.generator.manager.TemplateGroup in project ihmc-pub-sub by ihmcrobotics.

the class IDLParser method op_decl.

public final Op_declContext op_decl(Vector<Annotation> annotations) throws RecognitionException {
    Op_declContext _localctx = new Op_declContext(_ctx, getState(), annotations);
    enterRule(_localctx, 196, RULE_op_decl);
    Operation operationObject = null;
    TemplateGroup operationTemplates = null;
    if (tmanager != null) {
        operationTemplates = tmanager.createTemplateGroup("operation");
    }
    TemplateGroup tpl = null;
    String name = "";
    Token tk = null, tkoneway = null;
    TypeCode retType = null;
    Vector<Pair<String, Token>> exceptions = null;
    int _la;
    try {
        enterOuterAlt(_localctx, 1);
        {
            setState(1183);
            _la = _input.LA(1);
            if (_la == KW_ONEWAY) {
                {
                    setState(1180);
                    ((Op_declContext) _localctx).op_attribute = op_attribute();
                    tkoneway = ((Op_declContext) _localctx).op_attribute.token;
                }
            }
            setState(1185);
            ((Op_declContext) _localctx).op_type_spec = op_type_spec();
            retType = ((Op_declContext) _localctx).op_type_spec.typecode;
            tk = _input.LT(1);
            name += tk.getText();
            setState(1188);
            match(ID);
            // Create the Operation object.
            operationObject = ctx.createOperation(name, tk);
            // Add annotations.
            for (Annotation annotation : annotations) operationObject.addAnnotation(ctx, annotation);
            if (operationTemplates != null) {
                operationTemplates.setAttribute("ctx", ctx);
                // Set the the interface object to the TemplateGroup of the module.
                operationTemplates.setAttribute("operation", operationObject);
            }
            // Set return type
            operationObject.setRettype(retType);
            // Set oneway
            if (tkoneway != null) {
                operationObject.setOneway(true);
                if (retType != null) {
                    throw new ParseException(tkoneway, ". Oneway function cannot have a return type.");
                }
            }
            setState(1190);
            ((Op_declContext) _localctx).parameter_decls = parameter_decls(operationObject);
            tpl = ((Op_declContext) _localctx).parameter_decls.tpl;
            setState(1196);
            _la = _input.LA(1);
            if (_la == KW_RAISES) {
                {
                    setState(1192);
                    ((Op_declContext) _localctx).raises_expr = raises_expr();
                    exceptions = ((Op_declContext) _localctx).raises_expr.exlist;
                    // Search global exceptions and add them to the operation.
                    for (Pair<String, Token> pair : exceptions) {
                        com.eprosima.idl.parser.tree.Exception exception = ctx.getException(pair.first());
                        if (exception != null)
                            operationObject.addException(exception);
                        else
                            operationObject.addUnresolvedException(pair.first());
                    }
                }
            }
            setState(1199);
            _la = _input.LA(1);
            if (_la == KW_CONTEXT) {
                {
                    setState(1198);
                    context_expr();
                }
            }
            if (operationTemplates != null) {
                // Store the parameter list template group in the operation template group.
                operationTemplates.setAttribute("param_list", tpl);
            }
            // Create the returned data.
            ((Op_declContext) _localctx).returnPair = new Pair<Operation, TemplateGroup>(operationObject, operationTemplates);
        }
    } catch (RecognitionException re) {
        _localctx.exception = re;
        _errHandler.reportError(this, re);
        _errHandler.recover(this, re);
    } finally {
        exitRule();
    }
    return _localctx;
}
Also used : org.antlr.v4.runtime.tree(org.antlr.v4.runtime.tree) com.eprosima.idl.parser.tree(com.eprosima.idl.parser.tree) TemplateGroup(com.eprosima.idl.generator.manager.TemplateGroup) ParseException(com.eprosima.idl.parser.exception.ParseException) Pair(com.eprosima.idl.util.Pair)

Aggregations

TemplateGroup (com.eprosima.idl.generator.manager.TemplateGroup)14 Pair (com.eprosima.idl.util.Pair)11 Vector (java.util.Vector)5 com.eprosima.idl.parser.tree (com.eprosima.idl.parser.tree)2 ArrayList (java.util.ArrayList)2 org.antlr.v4.runtime.tree (org.antlr.v4.runtime.tree)2 TemplateManager (com.eprosima.idl.generator.manager.TemplateManager)1 ParseException (com.eprosima.idl.parser.exception.ParseException)1 IDLLexer (com.eprosima.idl.parser.grammar.IDLLexer)1 IDLParser (com.eprosima.idl.parser.grammar.IDLParser)1 DefaultErrorListener (com.eprosima.idl.parser.listener.DefaultErrorListener)1 AnnotationDeclaration (com.eprosima.idl.parser.tree.AnnotationDeclaration)1 AnnotationMember (com.eprosima.idl.parser.tree.AnnotationMember)1 PrimitiveTypeCode (com.eprosima.idl.parser.typecode.PrimitiveTypeCode)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1