Commit 374cde615134924480af18cd2c51a531f5100ba9

Thomas de Grivel 2023-11-17T12:47:58

wip buf_parse_map

diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 06d7362..fc68b0a 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -1931,41 +1931,48 @@ sw buf_parse_map (s_buf *buf, s_map *dest)
 sw buf_parse_map_key (s_buf *buf, s_tag *dest)
 {
   sw r;
+  if ((r = buf_parse_map_key_str(buf, dest)) ||
+      (r = buf_parse_map_key_sym(buf, dest)) ||
+      (r = buf_parse_map_key_tag(buf, dest)))
+    return r;
+  return 0;
+}
+
+sw buf_parse_map_key_str (s_buf *buf, s_tag *dest)
+{
+  sw r;
   sw result = 0;
   s_buf_save save;
   s_str str;
-  s_tag tag;
+  assert(buf);
+  assert(dest);
   buf_save_init(buf, &save);
-  if ((r = buf_parse_map_key_str(buf, &tag)) ||
-      (r = buf_parse_map_key_sym(buf, &tag)) ||
-      (r = buf_parse_map_key_tag(buf, &tag)))
-    
-  if ((r = buf_parse_sym_str(buf, &str)) < 0)
+  if ((r = buf_parse_str(buf, &str)) < 0)
     goto clean;
   if (r > 0) {
     result += r;
-    if ((r = buf_read_1(buf, ":")) <= 0)
-      buf_save_restore_rpos(buf, &save);
-    else {
+    if ((r = buf_read_1(buf, ":")) < 0)
+      goto restore;
+    if (r > 0) {
       result += r;
       dest->type = TAG_SYM;
       dest->data.sym = str_to_sym(&str);
       str_clean(&str);
       goto ok;
     }
+    if ((r = buf_parse_comments(buf)) < 0)
+      goto restore;
+    result += r;
+    if ((r = buf_ignore_spaces(buf)) <= 0)
+      goto restore;
+    result += r;
+    if ((r = buf_read_1(buf, "=>")) <= 0)
+      goto restore;
+    result += r;
+    dest->type = TAG_STR;
+    dest->data.str = str;
+    goto ok;
   }
-  if ((r = buf_parse_tag(buf, &tag)) <= 0)
-    goto restore;
-  if ((r = buf_parse_comments(buf)) < 0)
-    goto restore;
-  result += r;
-  if ((r = buf_ignore_spaces(buf)) <= 0)
-    goto restore;
-  result += r;
-  if ((r = buf_read_1(buf, "=>")) <= 0)
-    goto restore;
-  result += r;
-  *dest = tag;
  ok:
   r = result;
   goto clean;
@@ -1976,41 +1983,56 @@ sw buf_parse_map_key (s_buf *buf, s_tag *dest)
   return r;
 }
 
-buf_parse_map_key_str (s_buf *buf, s_tag *dest)
+sw buf_parse_map_key_sym (s_buf *buf, s_tag *dest)
 {
+  sw r;
+  sw result = 0;
   s_buf_save save;
   s_str str;
-  s_tag tag;
   assert(buf);
   assert(dest);
   buf_save_init(buf, &save);
-  if ((r = buf_parse_str(buf, &str)) < 0)
+  if ((r = buf_parse_sym_str(buf, &str)) < 0)
     goto clean;
   if (r > 0) {
     result += r;
-    if ((r = buf_read_1(buf, ":")) < 0)
-      goto restore;
-    if (r > 0) {
-      result += r;
-      dest->type = TAG_SYM;
-      dest->data.sym = str_to_sym(&str);
-      str_clean(&str);
-      goto ok;
-    }
-    if ((r = buf_parse_comments(buf)) < 0)
-      goto restore;
-    result += r;
-    if ((r = buf_ignore_spaces(buf)) <= 0)
-      goto restore;
-    result += r;
-    if ((r = buf_read_1(buf, "=>")) <= 0)
+    if ((r = buf_read_1(buf, ":")) <= 0)
       goto restore;
     result += r;
-    dest->type = TAG_STR;
-    dest->data.str = str;
-    goto ok;
+    dest->type = TAG_SYM;
+    dest->data.sym = str_to_sym(&str);
+    str_clean(&str);
   }
- ok:
+  r = result;
+  goto clean;
+ restore:
+  buf_save_restore_rpos(buf, &save);
+ clean:
+  buf_save_clean(buf, &save);
+  return r;
+}
+
+sw buf_parse_map_key_tag (s_buf *buf, s_tag *dest)
+{
+  sw r;
+  sw result = 0;
+  s_buf_save save;
+  s_tag tag;
+  assert(buf);
+  assert(dest);
+  buf_save_init(buf, &save);
+  if ((r = buf_parse_tag(buf, &tag)) <= 0)
+    goto restore;
+  if ((r = buf_parse_comments(buf)) < 0)
+    goto restore;
+  result += r;
+  if ((r = buf_ignore_spaces(buf)) <= 0)
+    goto restore;
+  result += r;
+  if ((r = buf_read_1(buf, "=>")) <= 0)
+    goto restore;
+  result += r;
+  *dest = tag;
   r = result;
   goto clean;
  restore:
diff --git a/libc3/buf_parse.h b/libc3/buf_parse.h
index 3287a9f..d432c0e 100644
--- a/libc3/buf_parse.h
+++ b/libc3/buf_parse.h
@@ -81,6 +81,9 @@ sw buf_parse_list (s_buf *buf, s_list **dest);
 sw buf_parse_list_paren (s_buf *buf, s_list **dest);
 sw buf_parse_map (s_buf *buf, s_map *dest);
 sw buf_parse_map_key (s_buf *buf, s_tag *dest);
+sw buf_parse_map_key_str (s_buf *buf, s_tag *dest);
+sw buf_parse_map_key_sym (s_buf *buf, s_tag *dest);
+sw buf_parse_map_key_tag (s_buf *buf, s_tag *dest);
 sw buf_parse_module_name (s_buf *buf, const s_sym **dest);
 sw buf_parse_new_tag (s_buf *buf, s_tag **dest);
 sw buf_parse_paren_sym (s_buf *buf, const s_sym **dest);