Skip to content
Permalink
Browse files

codegen: remove experimental context stuff

  • Loading branch information...
kodebach committed Jun 7, 2019
1 parent db40b54 commit c2fcee362404915674a0ee35c79bc39ef7de5826
@@ -96,7 +96,6 @@ const char * elektraFindReference (Elektra * elektra, const char * name);
const char * elektraFindReferenceArrayElement (Elektra * elektra, const char * name, kdb_long_long_t index);

Key * elektraHelpKey (Elektra * elektra);
KeySet * elektraContext (Elektra * elektra);

// endregion Helpers for code generation

@@ -644,7 +644,6 @@ struct _Elektra
KeySet * defaults;
Key * lookupKey;
ElektraErrorHandler fatalErrorHandler;
KeySet * context;
char * resolvedReference;
size_t parentKeyLength;
};
@@ -86,7 +86,6 @@ Elektra * elektraOpen (const char * application, KeySet * defaults, ElektraError
elektra->config = config;
elektra->lookupKey = keyNew (NULL, KEY_END);
elektra->fatalErrorHandler = &defaultFatalErrorHandler;
elektra->context = ksNew (0, KS_END);
elektra->defaults = ksDup (defaults);

return elektra;
@@ -177,20 +176,6 @@ Key * elektraHelpKey (Elektra * elektra)
return ksLookupByName (elektra->config, "proc/elektra/gopts/help", 0);
}

/**
* The contextual KeySet of an Elektra instance can in principle be used to store
* any meta information about this Elektra instance. The most important use-case are
* the code-generation features.
*
* @param elektra Elektra instance to access
*
* @return The contextual KeySet of this Elektra instance, you MUST NOT call ksDel() on it.
*/
KeySet * elektraContext (Elektra * elektra)
{
return elektra->context;
}

/**
* Sets the fatal error handler that will be called, whenever a fatal error occurs.
*
@@ -219,7 +204,6 @@ void elektraClose (Elektra * elektra)
keyDel (elektra->parentKey);
ksDel (elektra->config);
keyDel (elektra->lookupKey);
ksDel (elektra->context);

if (elektra->resolvedReference != NULL)
{
@@ -56,9 +56,7 @@ static inline std::string getArgDescription (const kdb::Key & key, kdb_long_long
"Replaces occurence no. " + indexStr + " of " + kind + " in the keyname.";
}

static void getKeyArgsAndContext (const kdb::Key & key, const size_t parentKeyParts, kainjow::mustache::list & args,
std::vector<kainjow::mustache::object> & context, kainjow::mustache::list & contextAndArgs,
std::string & fmtString)
static void getKeyArgs (const kdb::Key & key, const size_t parentKeyParts, kainjow::mustache::list & args, std::string & fmtString)
{
using namespace kainjow::mustache;
auto parts = getKeyParts (key);
@@ -76,49 +74,31 @@ static void getKeyArgsAndContext (const kdb::Key & key, const size_t parentKeyPa
const std::string & argName = getArgName (key, names, "name");
auto arg = object{ { "native_type", "const char *" },
{ "name", argName },
{ "code", argName },
{ "index?", false },
{ "description", getArgDescription (key, names, "_") } };
args.push_back (arg);
fmt << "%s/";
++pos;
++names;
contextAndArgs.push_back (object{ { "code", argName } });
}
else if (part == "#")
{
const std::string & argName = getArgName (key, indices, "index");

std::string argCode = "elektra_len (" + argName + "), elektra_len (";
argCode += argName + "), \"#___________________\", (long long) ";
argCode += argName;

auto arg = object{ { "native_type", "kdb_long_long_t" },
{ "name", argName },
{ "code", argCode },
{ "index?", true },
{ "description", getArgDescription (key, indices, "#") } };
args.push_back (arg);
fmt << "%*.*s%lld/";
pos += 4;
++indices;

std::string argCode = "elektra_len (" + argName + "), elektra_len (";
argCode += argName + "), \"#___________________\", (long long) ";
argCode += argName;

contextAndArgs.push_back (object{ { "code", argCode } });
}
else if (part.front () == '%' && part.back () == '%')
{
// contextual value
auto name = part.substr (1, part.length () - 2);
auto cName = name;
escapeNonAlphaNum (cName);
auto ctx = object{ { "name", name },
{ "c_name", snakeCaseToCamelCase (cName) },
{ "key_name", "system/elektra/codegen/context/" + name },
{ "macro_name", snakeCaseToMacroCase (cName) },
{ "tag_name", snakeCaseToPascalCase (cName) } };
context.push_back (ctx);

fmt << "%s/";
++pos;

contextAndArgs.push_back (object{ { "code", snakeCaseToCamelCase (cName) } });
}
else
{
@@ -131,27 +111,12 @@ static void getKeyArgsAndContext (const kdb::Key & key, const size_t parentKeyPa
{
args.back ()["last?"] = true;
}
else if (!context.empty ())
{
// keep comma if args present as well
context.back ()["last?"] = true;
}

if (!contextAndArgs.empty ())
{
contextAndArgs.back ()["last?"] = true;
}

if (args.size () > 1)
{
args[args.size () - 2]["last_but_one?"] = true;
}

if (contextAndArgs.size () > 1)
{
contextAndArgs[contextAndArgs.size () - 2]["last_but_one?"] = true;
}

fmtString = fmt.str ();
fmtString.pop_back ();
}
@@ -250,7 +215,6 @@ kainjow::mustache::data ElektraGenTemplate::getTemplateData (const std::string &

auto parentKeyParts = getKeyParts (specParent);

std::map<std::string, object> contexts;
for (auto it = ks.begin (); it != ks.end (); ++it)
{
kdb::Key key = *it;
@@ -274,9 +238,7 @@ kainjow::mustache::data ElektraGenTemplate::getTemplateData (const std::string &

std::string fmtString;
list args;
std::vector<object> context;
list contextAndArgs;
getKeyArgsAndContext (key, parentKeyParts.size (), args, context, contextAndArgs, fmtString);
getKeyArgs (key, parentKeyParts.size (), args, fmtString);

if (!key.hasMeta ("default"))
{
@@ -336,41 +298,21 @@ kainjow::mustache::data ElektraGenTemplate::getTemplateData (const std::string &

if (!args.empty ())
{
keyObject["args_or_context?"] = object{ { "context_and_args", contextAndArgs } };
keyObject["args?"] = true;
keyObject["args?"] = object{ { "args", args } };
keyObject["args"] = args;
keyObject["fmt_string"] = fmtString;
}

if (!context.empty ())
{
keyObject["args_or_context?"] = object{ { "context_and_args", contextAndArgs } };
keyObject["context?"] = true;
keyObject["context"] = list (context.begin (), context.end ());
keyObject["fmt_string"] = fmtString;

for (const auto & ctx : context)
{
auto ctxName = ctx.find ("name")->second.string_value ();
contexts[ctxName] = object (ctx);
}
}

if (isArray)
{
if (args.size () > 1)
{
// remove last argument and last part of format string
auto arrayArgs = list{ args.begin (), args.end () - 1 };
arrayArgs.back ()["last?"] = true;
auto arrayContextAndArgs = list{ contextAndArgs.begin (), contextAndArgs.end () - 1 };
arrayContextAndArgs.back ()["last?"] = true;

keyObject["array_args_or_context?"] =
object ({ { "args", arrayArgs },
{ "context_and_args", arrayContextAndArgs },
{ "context", list (context.begin (), context.end ()) },
{ "fmt_string", fmtString.substr (0, fmtString.rfind ('/')) } });

keyObject["array_args?"] =
object ({ { "args", arrayArgs }, { "fmt_string", fmtString.substr (0, fmtString.rfind ('/')) } });
}
// remove last part ('/#') from name
keyObject["array_name"] = name.substr (cascadingParent.size () + 1, name.size () - cascadingParent.size () - 3);
@@ -483,16 +425,6 @@ kainjow::mustache::data ElektraGenTemplate::getTemplateData (const std::string &
kdb::KeySet contract;
contract.append (kdb::Key ("system/elektra/ensure/plugins/global/gopts", KEY_VALUE, "mounted", KEY_END));

list totalContext;
kdb::KeySet defaultContext;
for (auto & p : contexts)
{
totalContext.push_back (p.second);
const auto & keyName = p.second["key_name"].string_value ();
// TODO: contextual defaults
defaultContext.append (kdb::Key (keyName, KEY_VALUE, "", KEY_END));
}

data["keys_count"] = std::to_string (keys.size ());
data["keys"] = keys;
data["enums"] = enums;
@@ -502,8 +434,6 @@ kainjow::mustache::data ElektraGenTemplate::getTemplateData (const std::string &
data["spec"] = keySetToCCode (spec);
data["contract"] = keySetToCCode (contract);
data["specload_arg"] = specloadArg;
data["total_context"] = totalContext;
data["default_context"] = keySetToCCode (defaultContext);

return data;
}
@@ -94,10 +94,6 @@ int /*%& init_function_name %*/ (Elektra ** elektra, ElektraError ** error)
return 2;
}

KeySet * defaultContext = /*%& default_context %*/;
ksAppend (elektraContext (e), defaultContext);
ksDel (defaultContext);

*elektra = e;
return 0;
}
@@ -44,8 +44,6 @@ extern "C" {
#include "/*% . %*/"
/*%/ more_headers %*/

#define ELEKTRA_CONTEXT_SET(contextTag) elektraSetContextualValue##contextTag

/*%> partial.enum.decl.h %*/

/*%> partial.union.decl.h %*/
@@ -54,10 +52,6 @@ extern "C" {

/*%> partial.keys.tags.h %*/

/*%> partial.context.tags.h %*/

/*%> partial.context.fun.h %*/

/*%> partial.keys.fun.h %*/

int /*%& init_function_name %*/ (Elektra ** elektra, ElektraError ** error);
@@ -138,23 +132,6 @@ void /*%& specload_function_name %*/ (int argc, const char ** argv);
*/// {{=/*% %*/=}}
#define elektraSizeV(elektra, tag, ...) ELEKTRA_SIZE (tag) (elektra, __VA_ARGS__)

/*%={{ }}=%*/
/**
* @param elektra The elektra instance initialized with {{{ init_function_name }}}().
* @param contextTag The context tag for the contextual value you want to set.
* @param value The actual value you want to set.
*/// {{=/*% %*/=}}
#define elektraContextSet(elektra, contextTag, value) ELEKTRA_CONTEXT_SET (contextTag) (elektra, value)

/*%={{ }}=%*/
/**
* @param elektra The elektra instance initialized with {{{ init_function_name }}}().
* @param contextTag The context tag for the contextual value you want to set.
* @param value The actual value you want to set.
* @param ... Variable arguments depending on the given tag.
*/// {{=/*% %*/=}}
#define elektraContextSetV(elektra, contextTag, value, ...) ELEKTRA_CONTEXT_SET (contextTag) (elektra, value, __VA_ARGS__)

#ifdef __cplusplus
}
#endif

This file was deleted.

This file was deleted.

0 comments on commit c2fcee3

Please sign in to comment.
You can’t perform that action at this time.