1 : #include "expr-to-xml.hh"
2 : #include "xml-writer.hh"
3 : #include "nixexpr-ast.hh"
4 : #include "aterm.hh"
5 :
6 :
7 : namespace nix {
8 :
9 :
10 : static XMLAttrs singletonAttrs(const string & name, const string & value)
11 23 : {
12 23 : XMLAttrs attrs;
13 23 : attrs[name] = value;
14 23 : return attrs;
15 : }
16 :
17 :
18 : static void printTermAsXML(Expr e, XMLWriter & doc, ATermList & context)
19 17 : {
20 17 : XMLAttrs attrs;
21 17 : ATerm s;
22 17 : int i;
23 17 : Expr e2;
24 17 : ATermList as, es, formals;
25 17 : ATerm body, pos;
26 :
27 17 : while (matchContext(e, es, e2)) {
28 0 : e = e2;
29 0 : for (ATermIterator i(es); i; ++i)
30 0 : context = ATinsert(context, *i);
31 : }
32 :
33 17 : if (matchStr(e, s))
34 9 : doc.writeEmptyElement("string", singletonAttrs("value", aterm2String(s)));
35 :
36 8 : else if (matchPath(e, s))
37 0 : doc.writeEmptyElement("path", singletonAttrs("value", aterm2String(s)));
38 :
39 8 : else if (matchUri(e, s))
40 0 : doc.writeEmptyElement("uri", singletonAttrs("value", aterm2String(s)));
41 :
42 8 : else if (matchNull(e))
43 0 : doc.writeEmptyElement("null");
44 :
45 8 : else if (matchInt(e, i))
46 2 : doc.writeEmptyElement("int", singletonAttrs("value", (format("%1%") % i).str()));
47 :
48 6 : else if (e == eTrue)
49 1 : doc.writeEmptyElement("bool", singletonAttrs("value", "true"));
50 :
51 5 : else if (e == eFalse)
52 1 : doc.writeEmptyElement("bool", singletonAttrs("value", "false"));
53 :
54 4 : else if (matchAttrs(e, as)) {
55 2 : XMLOpenElement _(doc, "attrs");
56 2 : ATermMap attrs(128);
57 2 : queryAllAttrs(e, attrs);
58 2 : StringSet names;
59 9 : for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i)
60 7 : names.insert(aterm2String(i->key));
61 9 : for (StringSet::iterator i = names.begin(); i != names.end(); ++i) {
62 7 : XMLOpenElement _(doc, "attr", singletonAttrs("name", *i));
63 7 : printTermAsXML(attrs.get(toATerm(*i)), doc, context);
64 : }
65 : }
66 :
67 2 : else if (matchList(e, es)) {
68 1 : XMLOpenElement _(doc, "list");
69 4 : for (ATermIterator i(es); i; ++i)
70 3 : printTermAsXML(*i, doc, context);
71 : }
72 :
73 1 : else if (matchFunction(e, formals, body, pos)) {
74 1 : XMLOpenElement _(doc, "function");
75 :
76 4 : for (ATermIterator i(formals); i; ++i) {
77 3 : Expr name; ValidValues valids; ATerm dummy;
78 3 : if (!matchFormal(*i, name, valids, dummy)) abort();
79 3 : XMLOpenElement _(doc, "arg", singletonAttrs("name", aterm2String(name)));
80 :
81 3 : ATermList valids2;
82 3 : if (matchValidValues(valids, valids2)) {
83 7 : for (ATermIterator j(valids2); j; ++j) {
84 5 : XMLOpenElement _(doc, "value");
85 5 : printTermAsXML(*j, doc, context);
86 : }
87 : }
88 : }
89 : }
90 :
91 : else
92 0 : doc.writeEmptyElement("unevaluated");
93 : }
94 :
95 :
96 : void printTermAsXML(Expr e, std::ostream & out, ATermList & context)
97 2 : {
98 2 : XMLWriter doc(true, out);
99 2 : XMLOpenElement root(doc, "expr");
100 2 : printTermAsXML(e, doc, context);
101 : }
102 :
103 :
104 121 : }
|