2006-08-24 16:16:55 +02:00
|
|
|
#include "expr-to-xml.hh"
|
|
|
|
#include "xml-writer.hh"
|
|
|
|
#include "nixexpr-ast.hh"
|
|
|
|
#include "aterm.hh"
|
2007-01-13 15:48:41 +01:00
|
|
|
#include "util.hh"
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2008-05-21 13:17:31 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2006-09-04 23:06:23 +02:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2006-08-24 16:16:55 +02:00
|
|
|
static XMLAttrs singletonAttrs(const string & name, const string & value)
|
|
|
|
{
|
|
|
|
XMLAttrs attrs;
|
|
|
|
attrs[name] = value;
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
static void printValueAsXML(EvalState & state, bool strict, Value & v,
|
|
|
|
XMLWriter & doc, PathSet & context, PathSet & drvsSeen);
|
2007-01-13 16:11:10 +01:00
|
|
|
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
static void showAttrs(EvalState & state, bool strict, Bindings & attrs,
|
|
|
|
XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
|
2007-01-13 16:11:10 +01:00
|
|
|
{
|
|
|
|
StringSet names;
|
2010-04-07 15:55:46 +02:00
|
|
|
foreach (Bindings::iterator, i, attrs)
|
|
|
|
names.insert(aterm2String(i->first));
|
|
|
|
foreach (StringSet::iterator, i, names) {
|
2007-01-13 16:11:10 +01:00
|
|
|
XMLOpenElement _(doc, "attr", singletonAttrs("name", *i));
|
2010-04-07 15:55:46 +02:00
|
|
|
printValueAsXML(state, strict, attrs[toATerm(*i)], doc, context, drvsSeen);
|
2007-01-13 16:11:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-14 14:53:29 +02:00
|
|
|
static void printPatternAsXML(Pattern pat, XMLWriter & doc)
|
2008-08-14 12:04:22 +02:00
|
|
|
{
|
|
|
|
ATerm name;
|
|
|
|
ATermList formals;
|
2008-08-14 16:00:44 +02:00
|
|
|
ATermBool ellipsis;
|
2008-08-14 12:04:22 +02:00
|
|
|
if (matchVarPat(pat, name))
|
|
|
|
doc.writeEmptyElement("varpat", singletonAttrs("name", aterm2String(name)));
|
2010-03-25 13:19:41 +01:00
|
|
|
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
|
|
|
XMLAttrs attrs;
|
|
|
|
if (name != sNoAlias) attrs["name"] = aterm2String(name);
|
|
|
|
if (ellipsis == eTrue) attrs["ellipsis"] = "1";
|
|
|
|
XMLOpenElement _(doc, "attrspat", attrs);
|
2008-08-14 12:04:22 +02:00
|
|
|
for (ATermIterator i(formals); i; ++i) {
|
|
|
|
Expr name; ATerm dummy;
|
|
|
|
if (!matchFormal(*i, name, dummy)) abort();
|
|
|
|
doc.writeEmptyElement("attr", singletonAttrs("name", aterm2String(name)));
|
|
|
|
}
|
2008-08-14 14:53:29 +02:00
|
|
|
}
|
2008-08-14 12:04:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
static void printValueAsXML(EvalState & state, bool strict, Value & v,
|
|
|
|
XMLWriter & doc, PathSet & context, PathSet & drvsSeen)
|
2006-08-24 16:16:55 +02:00
|
|
|
{
|
2007-01-13 15:48:41 +01:00
|
|
|
checkInterrupt();
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
if (strict) state.forceValue(v);
|
|
|
|
|
|
|
|
switch (v.type) {
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tInt:
|
|
|
|
doc.writeEmptyElement("int", singletonAttrs("value", (format("%1%") % v.integer).str()));
|
|
|
|
break;
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tBool:
|
|
|
|
doc.writeEmptyElement("bool", singletonAttrs("value", v.boolean ? "true" : "false"));
|
|
|
|
break;
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tString:
|
|
|
|
/* !!! show the context? */
|
|
|
|
doc.writeEmptyElement("string", singletonAttrs("value", v.string.s));
|
|
|
|
break;
|
2006-08-24 16:16:55 +02:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tPath:
|
|
|
|
doc.writeEmptyElement("path", singletonAttrs("value", v.path));
|
|
|
|
break;
|
2007-01-13 16:11:10 +01:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tNull:
|
|
|
|
doc.writeEmptyElement("null");
|
|
|
|
break;
|
2007-01-13 16:11:10 +01:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tAttrs:
|
|
|
|
if (state.isDerivation(v)) {
|
|
|
|
XMLAttrs xmlAttrs;
|
2007-01-13 16:11:10 +01:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
Bindings::iterator a = v.attrs->find(toATerm("derivation"));
|
|
|
|
|
|
|
|
Path drvPath;
|
|
|
|
a = v.attrs->find(toATerm("drvPath"));
|
|
|
|
if (a != v.attrs->end() && a->second.type == tString)
|
|
|
|
xmlAttrs["drvPath"] = drvPath = a->second.string.s;
|
2007-01-13 16:11:10 +01:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
a = v.attrs->find(toATerm("outPath"));
|
|
|
|
if (a != v.attrs->end() && a->second.type == tString)
|
|
|
|
xmlAttrs["outPath"] = a->second.string.s;
|
|
|
|
|
|
|
|
XMLOpenElement _(doc, "derivation", xmlAttrs);
|
|
|
|
|
|
|
|
if (drvPath != "" && drvsSeen.find(drvPath) == drvsSeen.end()) {
|
|
|
|
drvsSeen.insert(drvPath);
|
|
|
|
showAttrs(state, strict, *v.attrs, doc, context, drvsSeen);
|
|
|
|
} else
|
|
|
|
doc.writeEmptyElement("repeated");
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
XMLOpenElement _(doc, "attrs");
|
|
|
|
showAttrs(state, strict, *v.attrs, doc, context, drvsSeen);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2007-01-13 16:11:10 +01:00
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tList: {
|
|
|
|
XMLOpenElement _(doc, "list");
|
|
|
|
for (unsigned int n = 0; n < v.list.length; ++n)
|
|
|
|
printValueAsXML(state, strict, v.list.elems[n], doc, context, drvsSeen);
|
|
|
|
break;
|
2007-01-13 16:11:10 +01:00
|
|
|
}
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
case tLambda: {
|
|
|
|
XMLOpenElement _(doc, "function");
|
|
|
|
printPatternAsXML(v.lambda.pat, doc);
|
|
|
|
break;
|
2006-08-24 16:16:55 +02:00
|
|
|
}
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
default:
|
|
|
|
doc.writeEmptyElement("unevaluated");
|
2006-08-24 16:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-07 15:55:46 +02:00
|
|
|
void printValueAsXML(EvalState & state, bool strict,
|
|
|
|
Value & v, std::ostream & out, PathSet & context)
|
2006-08-24 16:16:55 +02:00
|
|
|
{
|
|
|
|
XMLWriter doc(true, out);
|
|
|
|
XMLOpenElement root(doc, "expr");
|
2010-04-07 15:55:46 +02:00
|
|
|
PathSet drvsSeen;
|
|
|
|
printValueAsXML(state, strict, v, doc, context, drvsSeen);
|
2006-08-24 16:16:55 +02:00
|
|
|
}
|
2006-09-04 23:06:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
}
|