Commit b933599c24bfdd6ab018538c539bc90adccac4d8

Thomas de Grivel 2024-08-11T02:38:02

pretty_printer: test/ikc3/str.kc3

diff --git a/.ikc3_history b/.ikc3_history
index b1b03c4..e281f80 100644
--- a/.ikc3_history
+++ b/.ikc3_history
@@ -1,12 +1,3 @@
-server = Socket.listen("192.168.2.50", "58000")
-server_client = Socket.Buf.accept(server)
-req = HTTP.Request.buf_parse(server_client.buf_rw.r)
-%HTTP.Response{}
-Struct.offset(%HTTP.Response{}, :message)
-Sym.type_size(Str)
-Struct.offset(%HTTP.Response{}, :message)
-res = %HTTP.Response{}
-res.message
 Sym.type_size(Str)
 Sym.type_size(HTTP.Response)
 Struct.offset(%HTTP.Response{}, :message)
@@ -97,3 +88,12 @@ require HTTPd
 HTTP.Event.version
 HTTP.Event.version()
 %KC3.Operator{}
+quote %{a: %{b: "b", c: "c"}, d: "d"}
+%KC3.Operator{}
+%{op1: %KC3.Operator{}, op2: %KC3.Operator{}}
+quote do do do 1 2 3 end end end
+quote do do do 1; 2; 3 end end end
+if true do
+1
+else
+quote if true do if true do 1 else 2 end else if true do 3 else 4 end end
diff --git a/libkc3/buf.c b/libkc3/buf.c
index 27c4c6f..c65d3c5 100644
--- a/libkc3/buf.c
+++ b/libkc3/buf.c
@@ -1196,13 +1196,19 @@ sw buf_write_character_utf8 (s_buf *buf, character c)
 
 sw buf_write_character_utf8_size (s_pretty *pretty, character c)
 {
-  sw size = character_utf8_size(c);
-  if (size <= 0)
-    return size;
-  if (c == '\n')
+  sw csize;
+  sw size;
+  csize = character_utf8_size(c);
+  if (csize <= 0)
+    return csize;
+  if (c == '\n') {
+    size = csize + pretty->base_column;
     pretty->column = pretty->base_column;
-  else
+  }
+  else {
+    size = csize;
     pretty->column++;
+  }
   return size;
 }
 
diff --git a/libkc3/buf_inspect.c b/libkc3/buf_inspect.c
index d9512ee..d6cc79d 100644
--- a/libkc3/buf_inspect.c
+++ b/libkc3/buf_inspect.c
@@ -110,11 +110,14 @@ sw buf_inspect_array_data_rec (s_buf *buf, const s_array *array,
                                const u8 **data, const s_tag **tag,
                                uw *address, uw dimension)
 {
+  s_pretty_save pretty_save;
   sw r;
   sw result = 0;
   if ((r = buf_write_1(buf, "{")) <= 0)
     goto clean;
   result += r;
+  pretty_save_init(&pretty_save, &buf->pretty);
+  pretty_indent_from_column(&buf->pretty, 0);
   address[dimension] = 0;
   while (1) {
     if (dimension == array->dimension - 1) {
@@ -141,10 +144,17 @@ sw buf_inspect_array_data_rec (s_buf *buf, const s_array *array,
     address[dimension]++;
     if (address[dimension] == array->dimensions[dimension].count)
       break;
-    if ((r = buf_write_1(buf, ", ")) <= 0)
-      goto clean;
+    if (dimension == array->dimension - 1) {
+      if ((r = buf_write_1(buf, ", ")) <= 0)
+        goto clean;
+    }
+    else {
+      if ((r = buf_write_1(buf, ",\n")) <= 0)
+        goto clean;
+    }
     result += r;
   }
+  pretty_save_clean(&pretty_save, &buf->pretty);
   if ((r = buf_write_1(buf, "}")) <= 0)
     goto clean;
   result += r;
@@ -185,11 +195,14 @@ sw buf_inspect_array_data_size_rec (s_pretty *pretty,
                                     const s_tag **tag,
                                     uw *address, uw dimension)
 {
+  s_pretty_save pretty_save;
   sw r;
   sw result = 0;
   if ((r = buf_write_1_size(pretty, "{")) < 0)
     return r;
   result += r;
+  pretty_save_init(&pretty_save, pretty);
+  pretty_indent_from_column(pretty, 0);
   address[dimension] = 0;
   while (1) {
     if (dimension == array->dimension - 1) {
@@ -217,10 +230,17 @@ sw buf_inspect_array_data_size_rec (s_pretty *pretty,
     address[dimension]++;
     if (address[dimension] == array->dimensions[dimension].count)
       break;
-    if ((r = buf_write_1_size(pretty, ", ")) < 0)
-      goto clean;
+    if (dimension == array->dimension - 1) {
+      if ((r = buf_write_1_size(pretty, ", ")) < 0)
+        goto clean;
+    }
+    else {
+      if ((r = buf_write_1_size(pretty, ",\n")) < 0)
+        goto clean;
+    }
     result += r;
   }
+  pretty_save_clean(&pretty_save, pretty);
   if ((r = buf_write_1_size(pretty, "}")) < 0)
     goto clean;
   result += r;
@@ -2394,11 +2414,11 @@ sw buf_inspect_map (s_buf *buf, const s_map *map)
     k = map->key + i;
     if (k->type == TAG_SYM) {
       if (sym_has_reserved_characters(k->data.sym)) {
-        if ((r = buf_write_str(buf, &k->data.sym->str)) < 0)
+        if ((r = buf_inspect_str(buf, &k->data.sym->str)) < 0)
           return r;
       }
       else
-        if ((r = buf_write_1(buf, k->data.sym->str.ptr.pchar)) < 0)
+        if ((r = buf_write_str(buf, &k->data.sym->str)) < 0)
           return r;
       result += r;
       if ((r = buf_write_1(buf, ": ")) < 0)
@@ -2423,10 +2443,10 @@ sw buf_inspect_map (s_buf *buf, const s_map *map)
       result += r;
     }
   }
+  pretty_save_clean(&pretty_save, &buf->pretty);
   if ((r = buf_write_1(buf, "}")) < 0)
     return r;
   result += r;
-  pretty_save_clean(&pretty_save, &buf->pretty);
   return result;
 }
 
@@ -2434,18 +2454,25 @@ sw buf_inspect_map_size (s_pretty *pretty, const s_map *map)
 {
   uw i = 0;
   s_tag *k;
+  s_pretty_save pretty_save;
   sw r;
   sw result = 0;
   assert(map);
   if ((r = buf_write_1_size(pretty, "%{")) < 0)
     return r;
   result += r;
+  pretty_save_init(&pretty_save, pretty);
+  pretty_indent_from_column(pretty, 0);
   while (i < map->count) {
     k = map->key + i;
     if (k->type == TAG_SYM) {
-      if ((r = buf_write_1_size(pretty,
-                                k->data.sym->str.ptr.pchar)) < 0)
-        return r;
+      if (sym_has_reserved_characters(k->data.sym)) {
+        if ((r = buf_inspect_str_size(pretty, &k->data.sym->str)) < 0)
+          return r;
+      }
+      else
+        if ((r = buf_write_str_size(pretty, &k->data.sym->str)) < 0)
+          return r;
       result += r;
       if ((r = buf_write_1_size(pretty, ": ")) < 0)
         return r;
@@ -2464,11 +2491,12 @@ sw buf_inspect_map_size (s_pretty *pretty, const s_map *map)
     result += r;
     i++;
     if (i < map->count) {
-      if ((r = buf_write_1_size(pretty, ", ")) < 0)
+      if ((r = buf_write_1_size(pretty, ",\n")) < 0)
         return r;
       result += r;
     }
   }
+  pretty_save_clean(&pretty_save, pretty);
   if ((r = buf_write_1_size(pretty, "}")) < 0)
     return r;
   result += r;
@@ -2866,7 +2894,7 @@ sw buf_inspect_str_eval (s_buf *buf, const s_list *list)
   l = list;
   while (l) {
     if (l->tag.type == TAG_STR) {
-      if ((r = buf_write_str(buf, &l->tag.data.str)) < 0)
+      if ((r = buf_write_str_without_indent(buf, &l->tag.data.str)) < 0)
         return r;
     }
     else {
@@ -2898,7 +2926,8 @@ sw buf_inspect_str_eval_size (s_pretty *pretty, const s_list *list)
   l = list;
   while (l) {
     if (l->tag.type == TAG_STR) {
-      if ((r = buf_write_str_size(pretty, &l->tag.data.str)) < 0)
+      if ((r = buf_write_str_without_indent_size(pretty,
+                                                 &l->tag.data.str)) < 0)
         return r;
     }
     else {
@@ -3093,10 +3122,10 @@ sw buf_inspect_struct (s_buf *buf, const s_struct *s)
       }
     }
   }
+  pretty_save_clean(&pretty_save, &buf->pretty);
   if ((r = buf_write_1(buf, "}")) < 0)
     return r;
   result += r;
-  pretty_save_clean(&pretty_save, &buf->pretty);
   return result;
 }
 
@@ -3163,16 +3192,16 @@ sw buf_inspect_struct_size (s_pretty *pretty, const s_struct *s)
       }
       i++;
       if (i < s->type->map.count) {
-        if ((r = buf_write_1_size(pretty, ", ")) < 0)
+        if ((r = buf_write_1_size(pretty, ",\n")) < 0)
           return r;
         result += r;
       }
     }
   }
+  pretty_save_clean(&pretty_save, pretty);
   if ((r = buf_write_1_size(pretty, "}")) < 0)
     return r;
   result += r;
-  pretty_save_clean(&pretty_save, pretty);
   return result;
 }
 
diff --git a/libkc3/types.h b/libkc3/types.h
index 5ce9c6a..09172c5 100644
--- a/libkc3/types.h
+++ b/libkc3/types.h
@@ -284,6 +284,7 @@ struct map {
 struct pretty {
   uw base_column;
   sw column;
+  bool disable;
 };
 
 struct pretty_save {
diff --git a/test/ikc3/access.out.expected b/test/ikc3/access.out.expected
index da063d4..37be3c5 100644
--- a/test/ikc3/access.out.expected
+++ b/test/ikc3/access.out.expected
@@ -1,8 +1,14 @@
-%{a: 1, b: 2, c: 3}.a
+%{a: 1,
+  b: 2,
+  c: 3}.a
 1
-%{a: 1, b: 2, c: 3}.b
+%{a: 1,
+  b: 2,
+  c: 3}.b
 2
-%{a: 1, b: 2, c: 3}.c
+%{a: 1,
+  b: 2,
+  c: 3}.c
 3
 %{a: %{b: %{c: 1}}}.a
 %{b: %{c: 1}}
diff --git a/test/ikc3/str.out.expected b/test/ikc3/str.out.expected
index f1c43f3..5f3d92f 100644
--- a/test/ikc3/str.out.expected
+++ b/test/ikc3/str.out.expected
@@ -33,8 +33,9 @@
 "abc\ndef"
 "1 + 2 = #{1 + 2}"
 "1 + 2 = 3"
-"#{%{a: 1, b: 2}}"
-"%{a: 1, b: 2}"
+"#{%{a: 1,
+     b: 2}}"
+"%{a: 1,\n  b: 2}"
 "#{:+}"
 "+"
 "#{?}"
@@ -44,9 +45,12 @@
 "#{:left}"
 "left"
 "#{%KC3.Operator{}}"
-"%KC3.Operator{sym: :+, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}"
-"#{%KC3.Operator{sym: :-, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}}"
-"%KC3.Operator{sym: :-, symbol_value: ?, operator_precedence: 0, operator_associativity: :left}"
+"%KC3.Operator{sym: :+,\n              symbol_value: ?,\n              operator_precedence: 0,\n              operator_associativity: :left}"
+"#{%KC3.Operator{sym: :-,
+                 symbol_value: ?,
+                 operator_precedence: 0,
+                 operator_associativity: :left}}"
+"%KC3.Operator{sym: :-,\n              symbol_value: ?,\n              operator_precedence: 0,\n              operator_associativity: :left}"
 "#{false}"
 "false"
 "#{(Ptr) 0}"
@@ -56,6 +60,6 @@
 "#{(Uw) 0}"
 "0"
 "#{%Buf{}}"
-"%Buf{column: (Sw) 0, flush: (Ptr) 0x0, free: false, line: (Sw) 0, ptr: (Ptr) 0x0, read_only: false, refill: (Ptr) 0x0, rpos: (Uw) 0, save: (Ptr) 0x0, seek: (Ptr) 0x0, size: (Uw) 0, user_ptr: (Ptr) 0x0, wpos: (Uw) 0}"
+"%Buf{column: (Sw) 0,\n     flush: (Ptr) 0x0,\n     free: false,\n     line: (Sw) 0,\n     ptr: (Ptr) 0x0,\n     read_only: false,\n     refill: (Ptr) 0x0,\n     rpos: (Uw) 0,\n     save: (Ptr) 0x0,\n     seek: (Ptr) 0x0,\n     size: (Uw) 0,\n     user_ptr: (Ptr) 0x0,\n     wpos: (Uw) 0}"
 "#{%BufRW{}}"
-"%BufRW{r: (Ptr) 0x0, w: (Ptr) 0x0}"
+"%BufRW{r: (Ptr) 0x0,\n        w: (Ptr) 0x0}"