Skip to content

Commit

Permalink
Several changes to support fastddsgen -genapi [19957] (#105)
Browse files Browse the repository at this point in the history
* Refs #19944. Context uses TemplateManager

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

* Refs #19944. Fix setting scope

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>

---------

Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
  • Loading branch information
richiware authored and adriancampo committed Feb 28, 2024
1 parent d45d73c commit 9936b24
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 43 deletions.
22 changes: 3 additions & 19 deletions src/main/antlr/com/eprosima/idl/parser/grammar/IDL.g4
Original file line number Diff line number Diff line change
Expand Up @@ -177,27 +177,11 @@ module returns [Pair<com.eprosima.idl.parser.tree.Module, TemplateGroup> returnP
if(moduleObject == null)
{
// Create the Module object.
moduleObject = new com.eprosima.idl.parser.tree.Module(ctx.getScopeFile(), ctx.isInScopedFile(), ctx.getScope(), name, tk);
//ctx.addPendingModule(moduleObject);
moduleObject = ctx.createModule(ctx.getScopeFile(), ctx.isInScopedFile(), ctx.getScope(), name, tk);
}

// Add the module to the context.
ctx.addModule(moduleObject);

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);
// Add the module to the context and update to the new namespace (internally call ctx.setScope)
moduleTemplates = ctx.addModule(moduleObject);
}
// Each definition is stored in the Module and each TemplateGroup is set as attribute in the TemplateGroup of the module.
LEFT_BRACE
Expand Down
73 changes: 69 additions & 4 deletions src/main/java/com/eprosima/idl/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.eprosima.idl.context;

import com.eprosima.idl.generator.manager.TemplateGroup;
import com.eprosima.idl.generator.manager.TemplateManager;
import com.eprosima.idl.parser.exception.ParseException;
import com.eprosima.idl.parser.tree.Annotation;
import com.eprosima.idl.parser.tree.AnnotationDeclaration;
Expand Down Expand Up @@ -63,16 +65,23 @@
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import org.antlr.v4.runtime.Token;
import org.stringtemplate.v4.STGroupFile;




public class Context
{
public Context(
TemplateManager tmanager,
String file,
ArrayList<String> includePaths)
ArrayList<String> includePaths,
boolean generate_typesc
)
{
tmanager_ = tmanager;
generate_typesc_ = generate_typesc;

// Detect OS
m_os = System.getProperty("os.name");
m_userdir = System.getProperty("user.dir");
Expand Down Expand Up @@ -120,7 +129,7 @@ public Context(
m_scopeFilesStack = new Stack<Pair<String, Integer>>();
for (int i = 0; i < includePaths.size(); ++i)
{
String include = (String)includePaths.get(i);
String include = includePaths.get(i);
if (startsWith(include, includeFlag))
{
include = include.substring(includeFlag.length());
Expand Down Expand Up @@ -172,6 +181,20 @@ public Context(
}
}

// Load IDL types for stringtemplates
TypeCode.ctx = this;
TypeCode.idltypesgr = new STGroupFile("com/eprosima/idl/templates/idlTypes.stg", '$', '$');
if (generate_typesc)
{
TypeCode.cpptypesgr = new STGroupFile("com/eprosima/idl/templates/TypesCInterface.stg", '$', '$');
}
else
{
TypeCode.cpptypesgr = new STGroupFile("com/eprosima/idl/templates/Types.stg", '$', '$');
}
TypeCode.ctypesgr = new STGroupFile("com/eprosima/idl/templates/CTypes.stg", '$', '$');
TypeCode.javatypesgr = new STGroupFile("com/eprosima/idl/templates/JavaTypes.stg", '$', '$');

// Add here builtin annotations? (IDL 4.2 - 8.3.1 section)
AnnotationDeclaration idann = createAnnotationDeclaration(Annotation.id_str, null);
idann.addMember(new AnnotationMember(Annotation.value_str, new PrimitiveTypeCode(Kind.KIND_LONG), "-1"));
Expand Down Expand Up @@ -377,13 +400,36 @@ public ArrayList<Definition> getDefinitions()
* @brief This function adds a module to the context.
* This function is used in the parser.
*/
public void addModule(
public TemplateGroup addModule(
com.eprosima.idl.parser.tree.Module module)
{
TemplateGroup moduleTemplates = null;

if (!m_modules.containsKey(module.getScopedname()))
{
m_modules.put(module.getScopedname(), module);
}

if(isInScopedFile() || isScopeLimitToAll()) {
if(tmanager_ != null) {
moduleTemplates = tmanager_.createTemplateGroup("module");
moduleTemplates.setAttribute("ctx", this);
// Set the module object to the TemplateGroup of the module.
moduleTemplates.setAttribute("module", module);
}
}

// Change context scope.
if (m_scope.isEmpty())
{
setScope(module.getName());
}
else
{
setScope(m_scope + "::" + module.getName());
}

return moduleTemplates;
}

public com.eprosima.idl.parser.tree.Module existsModule(
Expand Down Expand Up @@ -533,6 +579,16 @@ public com.eprosima.idl.parser.tree.Exception getException(
return returnedValue;
}

public com.eprosima.idl.parser.tree.Module createModule(
String scope_file,
boolean is_in_scope,
String scope,
String name,
Token token)
{
return new com.eprosima.idl.parser.tree.Module (scope_file, is_in_scope, scope, name, token);
}

public Operation createOperation(
String name,
Token token)
Expand Down Expand Up @@ -690,7 +746,7 @@ public TypeCode getTypeCode(
TypeCode returnedValue = null;
TypeDeclaration typedecl = m_types.get(name);

// Probar si no tiene scope, con el scope actual.
// Wether the name doesn't contain scope, test with the current scope.
if (typedecl == null)
{
String scope = m_scope;
Expand Down Expand Up @@ -1354,6 +1410,11 @@ public String evaluate_literal(
return aux_str;
}

public boolean isGenerateTypesC()
{
return generate_typesc_;
}

// OS
String m_os = null;
String m_userdir = null;
Expand Down Expand Up @@ -1404,4 +1465,8 @@ public String evaluate_literal(
private HashSet<String> m_keywords = null;

private boolean m_ignore_case = true;

private boolean generate_typesc_ = false;

protected TemplateManager tmanager_ = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package com.eprosima.idl.generator.manager;

import com.eprosima.idl.generator.manager.TemplateGroup;
import com.eprosima.idl.parser.typecode.TypeCode;
import com.eprosima.idl.context.Context;

import java.util.Iterator;
import java.util.Map;
Expand All @@ -35,23 +33,6 @@ public class TemplateManager

private boolean st_error_ = false;

public TemplateManager(String stackTemplateNames, Context ctx, boolean generate_typesc)
{
// Load IDL types for stringtemplates
TypeCode.idltypesgr = new STGroupFile("com/eprosima/idl/templates/idlTypes.stg", '$', '$');
if (generate_typesc)
{
TypeCode.cpptypesgr = new STGroupFile("com/eprosima/idl/templates/TypesCInterface.stg", '$', '$');
}
else
{
TypeCode.cpptypesgr = new STGroupFile("com/eprosima/idl/templates/Types.stg", '$', '$');
}
TypeCode.ctypesgr = new STGroupFile("com/eprosima/idl/templates/CTypes.stg", '$', '$');
TypeCode.javatypesgr = new STGroupFile("com/eprosima/idl/templates/JavaTypes.stg", '$', '$');
TypeCode.ctx = ctx;
}

public void addGroup(String groupname)
{
STGroup group = new STGroupFile(groupname, '$', '$');
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/eprosima/idl/test/TestIDLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


import com.eprosima.idl.context.Context;
import com.eprosima.idl.generator.manager.TemplateManager;
import com.eprosima.idl.parser.grammar.IDLLexer;
import com.eprosima.idl.parser.grammar.IDLParser;
import com.eprosima.idl.parser.tree.Annotation;
Expand Down Expand Up @@ -75,7 +76,8 @@ public void parse(String idlFileName) {
return;
}

Context context = new Context(idlFileName, m_includePaths);
TemplateManager tmanager = new TemplateManager();
Context context = new Context(tmanager, idlFileName, m_includePaths, false);

try {

Expand Down

0 comments on commit 9936b24

Please sign in to comment.