about summary refs log tree commit diff
path: root/src/libnix
diff options
context:
space:
mode:
Diffstat (limited to 'src/libnix')
-rw-r--r--src/libnix/aterm.hh22
-rw-r--r--src/libnix/expr.cc32
2 files changed, 34 insertions, 20 deletions
diff --git a/src/libnix/aterm.hh b/src/libnix/aterm.hh
index 1e4ee80eeab5..16d8d6bb6dfd 100644
--- a/src/libnix/aterm.hh
+++ b/src/libnix/aterm.hh
@@ -14,6 +14,28 @@ string atPrint(ATerm t);
 /* Write an ATerm to an output stream. */
 ostream & operator << (ostream & stream, ATerm e);
 
+class ATermIterator
+{
+    ATermList t;
+
+public:
+    ATermIterator(ATermList _t) : t(_t) { }
+    ATermIterator & operator ++ ()
+    {
+        t = ATgetNext(t);
+        return *this;
+    }
+    ATerm operator * ()
+    {
+        return ATgetFirst(t);
+    }
+    operator bool ()
+    {
+        return t != ATempty;
+    }
+};
+
+
 /* Type-safe matching. */
 
 struct ATMatcher 
diff --git a/src/libnix/expr.cc b/src/libnix/expr.cc
index 67fa69f72fab..7bb1f5306129 100644
--- a/src/libnix/expr.cc
+++ b/src/libnix/expr.cc
@@ -43,13 +43,11 @@ Path writeTerm(ATerm t, const string & suffix)
 static void parsePaths(ATermList paths, PathSet & out)
 {
     ATMatcher m;
-    while (!ATisEmpty(paths)) {
+    for (ATermIterator i(paths); i; ++i) {
         string s;
-        ATerm t = ATgetFirst(paths);
-        if (!(atMatch(m, t) >> s))
-            throw badTerm("not a path", t);
+        if (!(atMatch(m, *i) >> s))
+            throw badTerm("not a path", *i);
         out.insert(s);
-        paths = ATgetNext(paths);
     }
 }
 
@@ -91,16 +89,14 @@ static bool parseClosure(ATerm t, Closure & closure)
 
     parsePaths(roots, closure.roots);
 
-    while (!ATisEmpty(elems)) {
+    for (ATermIterator i(elems); i; ++i) {
         string path;
         ATermList refs;
-        ATerm t = ATgetFirst(elems);
-        if (!(atMatch(m, t) >> "" >> path >> refs))
-            throw badTerm("not a closure element", t);
+        if (!(atMatch(m, *i) >> "" >> path >> refs))
+            throw badTerm("not a closure element", *i);
         ClosureElem elem;
         parsePaths(refs, elem.refs);
         closure.elems[path] = elem;
-        elems = ATgetNext(elems);
     }
 
     checkClosure(closure);
@@ -124,22 +120,18 @@ static bool parseDerivation(ATerm t, Derivation & derivation)
     derivation.builder = builder;
     derivation.platform = platform;
     
-    while (!ATisEmpty(args)) {
+    for (ATermIterator i(args); i; ++i) {
         string s;
-        ATerm arg = ATgetFirst(args);
-        if (!(atMatch(m, arg) >> s))
-            throw badTerm("string expected", arg);
+        if (!(atMatch(m, *i) >> s))
+            throw badTerm("string expected", *i);
         derivation.args.push_back(s);
-        args = ATgetNext(args);
     }
 
-    while (!ATisEmpty(bnds)) {
+    for (ATermIterator i(bnds); i; ++i) {
         string s1, s2;
-        ATerm bnd = ATgetFirst(bnds);
-        if (!(atMatch(m, bnd) >> "" >> s1 >> s2))
-            throw badTerm("tuple of strings expected", bnd);
+        if (!(atMatch(m, *i) >> "" >> s1 >> s2))
+            throw badTerm("tuple of strings expected", *i);
         derivation.env[s1] = s2;
-        bnds = ATgetNext(bnds);
     }
 
     return true;