* toXML: propagate the context to allow derivations to be used in the

argument.
This commit is contained in:
Eelco Dolstra 2006-10-03 15:38:59 +00:00
parent 3837fb233c
commit 5fd44654db
4 changed files with 21 additions and 10 deletions

View file

@ -15,14 +15,21 @@ static XMLAttrs singletonAttrs(const string & name, const string & value)
} }
static void printTermAsXML(Expr e, XMLWriter & doc) static void printTermAsXML(Expr e, XMLWriter & doc, ATermList & context)
{ {
XMLAttrs attrs; XMLAttrs attrs;
ATerm s; ATerm s;
int i; int i;
Expr e2;
ATermList as, es, formals; ATermList as, es, formals;
ATerm body, pos; ATerm body, pos;
while (matchContext(e, es, e2)) {
e = e2;
for (ATermIterator i(es); i; ++i)
context = ATinsert(context, *i);
}
if (matchStr(e, s)) if (matchStr(e, s))
doc.writeEmptyElement("string", singletonAttrs("value", aterm2String(s))); doc.writeEmptyElement("string", singletonAttrs("value", aterm2String(s)));
@ -53,14 +60,14 @@ static void printTermAsXML(Expr e, XMLWriter & doc)
names.insert(aterm2String(i->key)); names.insert(aterm2String(i->key));
for (StringSet::iterator i = names.begin(); i != names.end(); ++i) { for (StringSet::iterator i = names.begin(); i != names.end(); ++i) {
XMLOpenElement _(doc, "attr", singletonAttrs("name", *i)); XMLOpenElement _(doc, "attr", singletonAttrs("name", *i));
printTermAsXML(attrs.get(toATerm(*i)), doc); printTermAsXML(attrs.get(toATerm(*i)), doc, context);
} }
} }
else if (matchList(e, es)) { else if (matchList(e, es)) {
XMLOpenElement _(doc, "list"); XMLOpenElement _(doc, "list");
for (ATermIterator i(es); i; ++i) for (ATermIterator i(es); i; ++i)
printTermAsXML(*i, doc); printTermAsXML(*i, doc, context);
} }
else if (matchFunction(e, formals, body, pos)) { else if (matchFunction(e, formals, body, pos)) {
@ -75,7 +82,7 @@ static void printTermAsXML(Expr e, XMLWriter & doc)
if (matchValidValues(valids, valids2)) { if (matchValidValues(valids, valids2)) {
for (ATermIterator j(valids2); j; ++j) { for (ATermIterator j(valids2); j; ++j) {
XMLOpenElement _(doc, "value"); XMLOpenElement _(doc, "value");
printTermAsXML(*j, doc); printTermAsXML(*j, doc, context);
} }
} }
} }
@ -86,11 +93,11 @@ static void printTermAsXML(Expr e, XMLWriter & doc)
} }
void printTermAsXML(Expr e, std::ostream & out) void printTermAsXML(Expr e, std::ostream & out, ATermList & context)
{ {
XMLWriter doc(true, out); XMLWriter doc(true, out);
XMLOpenElement root(doc, "expr"); XMLOpenElement root(doc, "expr");
printTermAsXML(e, doc); printTermAsXML(e, doc, context);
} }

View file

@ -5,10 +5,11 @@
#include <map> #include <map>
#include "nixexpr.hh" #include "nixexpr.hh"
#include "aterm.hh"
namespace nix { namespace nix {
void printTermAsXML(Expr e, std::ostream & out); void printTermAsXML(Expr e, std::ostream & out, ATermList & context);
} }

View file

@ -507,8 +507,9 @@ static Expr primToPath(EvalState & state, const ATermVector & args)
static Expr primToXML(EvalState & state, const ATermVector & args) static Expr primToXML(EvalState & state, const ATermVector & args)
{ {
std::ostringstream out; std::ostringstream out;
printTermAsXML(strictEvalExpr(state, args[0]), out); ATermList context = ATempty;
return makeStr(toATerm(out.str())); printTermAsXML(strictEvalExpr(state, args[0]), out, context);
return wrapInContext(context, makeStr(toATerm(out.str())));
} }

View file

@ -41,9 +41,11 @@ static bool indirectRoot = false;
static void printResult(EvalState & state, Expr e, static void printResult(EvalState & state, Expr e,
bool evalOnly, bool xmlOutput, const ATermMap & autoArgs) bool evalOnly, bool xmlOutput, const ATermMap & autoArgs)
{ {
ATermList context;
if (evalOnly) if (evalOnly)
if (xmlOutput) if (xmlOutput)
printTermAsXML(e, std::cout); printTermAsXML(e, std::cout, context);
else else
std::cout << format("%1%\n") % e; std::cout << format("%1%\n") % e;