about summary refs log tree commit diff
path: root/src/libexpr/nixexpr.cc
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-10-20T06·44+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-10-20T07·13+0200
commitecc2c8f46452c5041dca34bcfd24bbd863d1d46e (patch)
tree9a67aa5cc40dbaf1d7f2621694bf16cbd6d787e8 /src/libexpr/nixexpr.cc
parent84a13dc576496f1227665259c61f86184f452f51 (diff)
Improve printing of ASTs
Diffstat (limited to 'src/libexpr/nixexpr.cc')
-rw-r--r--src/libexpr/nixexpr.cc66
1 files changed, 57 insertions, 9 deletions
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index c8521718b82a..6945e4daed80 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -16,6 +16,47 @@ std::ostream & operator << (std::ostream & str, Expr & e)
     return str;
 }
 
+static void showString(std::ostream & str, const string & s)
+{
+    str << '"';
+    for (auto c : (string) s)
+        if (c == '"' || c == '\\' || c == '$') str << "\\" << c;
+        else if (c == '\n') str << "\\n";
+        else if (c == '\r') str << "\\r";
+        else if (c == '\t') str << "\\t";
+        else str << c;
+    str << '"';
+}
+
+static void showId(std::ostream & str, const string & s)
+{
+    assert(!s.empty());
+    if (s == "if")
+        str << '"' << s << '"';
+    else {
+        char c = s[0];
+        if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')) {
+            showString(str, s);
+            return;
+        }
+        for (auto c : s)
+            if (!((c >= 'a' && c <= 'z') ||
+                  (c >= 'A' && c <= 'Z') ||
+                  (c >= '0' && c <= '9') ||
+                  c == '_' || c == '\'' || c == '-')) {
+                showString(str, s);
+                return;
+            }
+        str << s;
+    }
+}
+
+std::ostream & operator << (std::ostream & str, const Symbol & sym)
+{
+    showId(str, *sym.s);
+    return str;
+}
+
 void Expr::show(std::ostream & str)
 {
     abort();
@@ -28,7 +69,7 @@ void ExprInt::show(std::ostream & str)
 
 void ExprString::show(std::ostream & str)
 {
-    str << "\"" << s << "\""; // !!! escaping
+    showString(str, s);
 }
 
 void ExprPath::show(std::ostream & str)
@@ -44,12 +85,12 @@ void ExprVar::show(std::ostream & str)
 void ExprSelect::show(std::ostream & str)
 {
     str << "(" << *e << ")." << showAttrPath(attrPath);
-    if (def) str << " or " << *def;
+    if (def) str << " or (" << *def << ")";
 }
 
 void ExprOpHasAttr::show(std::ostream & str)
 {
-    str << "(" << *e << ") ? " << showAttrPath(attrPath);
+    str << "((" << *e << ") ? " << showAttrPath(attrPath) << ")";
 }
 
 void ExprAttrs::show(std::ostream & str)
@@ -85,6 +126,10 @@ void ExprLambda::show(std::ostream & str)
             str << i->name;
             if (i->def) str << " ? " << *i->def;
         }
+        if (formals->ellipsis) {
+            if (!first) str << ", ";
+            str << "...";
+        }
         str << " }";
         if (!arg.empty()) str << " @ ";
     }
@@ -94,23 +139,24 @@ void ExprLambda::show(std::ostream & str)
 
 void ExprLet::show(std::ostream & str)
 {
-    str << "let ";
+    str << "(let ";
     foreach (ExprAttrs::AttrDefs::iterator, i, attrs->attrs)
-        if (i->second.inherited)
+        if (i->second.inherited) {
             str << "inherit " << i->first << "; ";
+        }
         else
             str << i->first << " = " << *i->second.e << "; ";
-    str << "in " << *body;
+    str << "in " << *body << ")";
 }
 
 void ExprWith::show(std::ostream & str)
 {
-    str << "with " << *attrs << "; " << *body;
+    str << "(with " << *attrs << "; " << *body << ")";
 }
 
 void ExprIf::show(std::ostream & str)
 {
-    str << "if " << *cond << " then " << *then << " else " << *else_;
+    str << "(if " << *cond << " then " << *then << " else " << *else_ << ")";
 }
 
 void ExprAssert::show(std::ostream & str)
@@ -120,16 +166,18 @@ void ExprAssert::show(std::ostream & str)
 
 void ExprOpNot::show(std::ostream & str)
 {
-    str << "! " << *e;
+    str << "(! " << *e << ")";
 }
 
 void ExprConcatStrings::show(std::ostream & str)
 {
     bool first = true;
+    str << "(";
     foreach (vector<Expr *>::iterator, i, *es) {
         if (first) first = false; else str << " + ";
         str << **i;
     }
+    str << ")";
 }
 
 void ExprPos::show(std::ostream & str)