nix-super/src/libexpr/value-to-xml.cc

186 lines
5.6 KiB
C++
Raw Normal View History

2010-04-07 16:59:45 +03:00
#include "value-to-xml.hh"
2006-08-24 17:16:55 +03:00
#include "xml-writer.hh"
#include "eval-inline.hh"
#include "util.hh"
2006-08-24 17:16:55 +03:00
#include <cstdlib>
2006-08-24 17:16:55 +03:00
namespace nix {
2015-07-17 20:24:28 +03:00
static XMLAttrs singletonAttrs(const std::string & name, const std::string & value)
2006-08-24 17:16:55 +03:00
{
XMLAttrs attrs;
attrs[name] = value;
return attrs;
}
2010-05-07 17:46:47 +03:00
static void printValueAsXML(EvalState & state, bool strict, bool location,
Value & v, XMLWriter & doc, PathSet & context, PathSet & drvsSeen,
const PosIdx pos);
static void posToXML(EvalState & state, XMLAttrs & xmlAttrs, const Pos & pos)
2010-05-07 17:46:47 +03:00
{
xmlAttrs["path"] = pos.file;
xmlAttrs["line"] = (format("%1%") % pos.line).str();
xmlAttrs["column"] = (format("%1%") % pos.column).str();
}
static void showAttrs(EvalState & state, bool strict, bool location,
Bindings & attrs, XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
{
StringSet names;
2015-07-17 20:24:28 +03:00
for (auto & i : attrs)
names.emplace(state.symbols[i.name]);
2015-07-17 20:24:28 +03:00
for (auto & i : names) {
Attr & a(*attrs.find(state.symbols.create(i)));
2010-05-07 17:46:47 +03:00
XMLAttrs xmlAttrs;
2015-07-17 20:24:28 +03:00
xmlAttrs["name"] = i;
if (location && a.pos) posToXML(state, xmlAttrs, state.positions[a.pos]);
2015-07-17 20:24:28 +03:00
2010-05-07 17:46:47 +03:00
XMLOpenElement _(doc, "attr", xmlAttrs);
printValueAsXML(state, strict, location,
*a.value, doc, context, drvsSeen, a.pos);
}
}
2010-05-07 17:46:47 +03:00
static void printValueAsXML(EvalState & state, bool strict, bool location,
Value & v, XMLWriter & doc, PathSet & context, PathSet & drvsSeen,
const PosIdx pos)
2006-08-24 17:16:55 +03:00
{
checkInterrupt();
if (strict) state.forceValue(v, pos);
2015-07-17 20:24:28 +03:00
switch (v.type()) {
2006-08-24 17:16:55 +03:00
case nInt:
doc.writeEmptyElement("int", singletonAttrs("value", (format("%1%") % v.integer).str()));
break;
2006-08-24 17:16:55 +03:00
case nBool:
doc.writeEmptyElement("bool", singletonAttrs("value", v.boolean ? "true" : "false"));
break;
2006-08-24 17:16:55 +03:00
case nString:
/* !!! show the context? */
copyContext(v, context);
doc.writeEmptyElement("string", singletonAttrs("value", v.string.s));
break;
2006-08-24 17:16:55 +03:00
case nPath:
doc.writeEmptyElement("path", singletonAttrs("value", v.path));
break;
case nNull:
doc.writeEmptyElement("null");
break;
case nAttrs:
if (state.isDerivation(v)) {
XMLAttrs xmlAttrs;
2015-07-17 20:24:28 +03:00
Bindings::iterator a = v.attrs->find(state.symbols.create("derivation"));
Path drvPath;
a = v.attrs->find(state.sDrvPath);
2010-05-07 17:46:47 +03:00
if (a != v.attrs->end()) {
if (strict) state.forceValue(*a->value, a->pos);
if (a->value->type() == nString)
xmlAttrs["drvPath"] = drvPath = a->value->string.s;
2010-05-07 17:46:47 +03:00
}
2015-07-17 20:24:28 +03:00
a = v.attrs->find(state.sOutPath);
2010-05-07 17:46:47 +03:00
if (a != v.attrs->end()) {
if (strict) state.forceValue(*a->value, a->pos);
if (a->value->type() == nString)
xmlAttrs["outPath"] = a->value->string.s;
2010-05-07 17:46:47 +03:00
}
XMLOpenElement _(doc, "derivation", xmlAttrs);
if (drvPath != "" && drvsSeen.insert(drvPath).second)
2010-05-07 17:46:47 +03:00
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
else
doc.writeEmptyElement("repeated");
}
else {
XMLOpenElement _(doc, "attrs");
2010-05-07 17:46:47 +03:00
showAttrs(state, strict, location, *v.attrs, doc, context, drvsSeen);
}
2015-07-17 20:24:28 +03:00
break;
case nList: {
XMLOpenElement _(doc, "list");
for (auto v2 : v.listItems())
printValueAsXML(state, strict, location, *v2, doc, context, drvsSeen, pos);
break;
}
case nFunction: {
if (!v.isLambda()) {
// FIXME: Serialize primops and primopapps
doc.writeEmptyElement("unevaluated");
break;
}
2010-05-07 17:46:47 +03:00
XMLAttrs xmlAttrs;
if (location) posToXML(state, xmlAttrs, state.positions[v.lambda.fun->pos]);
2010-05-07 17:46:47 +03:00
XMLOpenElement _(doc, "function", xmlAttrs);
2015-07-17 20:24:28 +03:00
if (v.lambda.fun->hasFormals()) {
XMLAttrs attrs;
if (v.lambda.fun->arg) attrs["name"] = state.symbols[v.lambda.fun->arg];
if (v.lambda.fun->formals->ellipsis) attrs["ellipsis"] = "1";
XMLOpenElement _(doc, "attrspat", attrs);
for (auto & i : v.lambda.fun->formals->lexicographicOrder(state.symbols))
doc.writeEmptyElement("attr", singletonAttrs("name", state.symbols[i.name]));
} else
doc.writeEmptyElement("varpat", singletonAttrs("name", state.symbols[v.lambda.fun->arg]));
2015-07-17 20:24:28 +03:00
break;
2006-08-24 17:16:55 +03:00
}
case nExternal:
v.external->printValueAsXML(state, strict, location, doc, context, drvsSeen, pos);
break;
case nFloat:
doc.writeEmptyElement("float", singletonAttrs("value", (format("%1%") % v.fpoint).str()));
break;
case nThunk:
doc.writeEmptyElement("unevaluated");
2006-08-24 17:16:55 +03:00
}
}
void ExternalValueBase::printValueAsXML(EvalState & state, bool strict,
bool location, XMLWriter & doc, PathSet & context, PathSet & drvsSeen,
const PosIdx pos) const
{
doc.writeEmptyElement("unevaluated");
}
2010-05-07 17:46:47 +03:00
void printValueAsXML(EvalState & state, bool strict, bool location,
Value & v, std::ostream & out, PathSet & context, const PosIdx pos)
2006-08-24 17:16:55 +03:00
{
XMLWriter doc(true, out);
XMLOpenElement root(doc, "expr");
2015-07-17 20:24:28 +03:00
PathSet drvsSeen;
printValueAsXML(state, strict, location, v, doc, context, drvsSeen, pos);
2006-08-24 17:16:55 +03:00
}
2015-07-17 20:24:28 +03:00
}