Search in sources :

Example 1 with IDLParser

use of com.eprosima.idl.parser.grammar.IDLParser 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)

Aggregations

TemplateGroup (com.eprosima.idl.generator.manager.TemplateGroup)1 TemplateManager (com.eprosima.idl.generator.manager.TemplateManager)1 IDLLexer (com.eprosima.idl.parser.grammar.IDLLexer)1 IDLParser (com.eprosima.idl.parser.grammar.IDLParser)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 CppReader (org.anarres.cpp.CppReader)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1