save
[protos/libecoli.git] / lib / ecoli_node_sh_lex.c
index b92af1f..59b917f 100644 (file)
@@ -235,93 +235,81 @@ static struct ec_strvec *tokenize(const char *str, int completion,
        return NULL;
 }
 
-static struct ec_parsed *ec_node_sh_lex_parse(const struct ec_node *gen_node,
-       const struct ec_strvec *strvec)
+static int
+ec_node_sh_lex_parse(const struct ec_node *gen_node,
+               struct ec_parsed *state,
+               const struct ec_strvec *strvec)
 {
        struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
-       struct ec_strvec *new_vec = NULL, *match_strvec;
-       struct ec_parsed *parsed = NULL, *child_parsed;
+       struct ec_strvec *new_vec = NULL;
+       struct ec_parsed *child_parsed;
        const char *str;
+       int ret;
 
-       parsed = ec_parsed();
-       if (parsed == NULL)
-               return NULL;
-
-       if (ec_strvec_len(strvec) == 0)
-               return parsed;
-
-       str = ec_strvec_val(strvec, 0);
-       new_vec = tokenize(str, 0, 0, NULL);
-       if (new_vec == NULL)
-               goto fail;
-
-       child_parsed = ec_node_parse_strvec(node->child, new_vec);
-       if (child_parsed == NULL)
+       if (ec_strvec_len(strvec) == 0) {
+               new_vec = ec_strvec();
+       } else {
+               str = ec_strvec_val(strvec, 0);
+               new_vec = tokenize(str, 0, 0, NULL);
+       }
+       if (new_vec == NULL) {
+               ret = -ENOMEM;
                goto fail;
+       }
 
-       if (!ec_parsed_matches(child_parsed) ||
-                       ec_parsed_len(child_parsed) !=
-                               ec_strvec_len(new_vec)) {
-               ec_strvec_free(new_vec);
-               ec_parsed_free(child_parsed);
-               return parsed;
+       ret = ec_node_parse_child(node->child, state, new_vec);
+       if (ret >= 0) {
+               if ((unsigned)ret == ec_strvec_len(new_vec)) {
+                       ret = 1;
+               } else {
+                       child_parsed = ec_parsed_get_last_child(state);
+                       ec_parsed_del_child(state, child_parsed);
+                       ec_parsed_free(child_parsed);
+                       ret = EC_PARSED_NOMATCH;
+               }
        }
+
        ec_strvec_free(new_vec);
        new_vec = NULL;
 
-       ec_parsed_add_child(parsed, child_parsed);
-       match_strvec = ec_strvec_ndup(strvec, 0, 1);
-       if (match_strvec == NULL)
-               goto fail;
-       ec_parsed_set_match(parsed, gen_node, match_strvec);
-
-       return parsed;
+       return ret;
 
  fail:
        ec_strvec_free(new_vec);
-       ec_parsed_free(parsed);
-
-       return NULL;
+       return ret;
 }
 
-static struct ec_completed *ec_node_sh_lex_complete(const struct ec_node *gen_node,
-       const struct ec_strvec *strvec)
+static int
+ec_node_sh_lex_complete(const struct ec_node *gen_node,
+                       struct ec_completed *completed,
+                       struct ec_parsed *parsed,
+                       const struct ec_strvec *strvec)
 {
        struct ec_node_sh_lex *node = (struct ec_node_sh_lex *)gen_node;
-       struct ec_completed *completed, *child_completed = NULL;
        struct ec_strvec *new_vec = NULL;
        const char *str;
        char missing_quote;
-
-//     printf("==================\n");
-       completed = ec_completed();
-       if (completed == NULL)
-               return NULL;
+       int ret;
 
        if (ec_strvec_len(strvec) != 1)
-               return completed;
+               return 0;
 
        str = ec_strvec_val(strvec, 0);
        new_vec = tokenize(str, 1, 1, &missing_quote);
        if (new_vec == NULL)
                goto fail;
 
-//     ec_strvec_dump(new_vec, stdout);
-
-       child_completed = ec_node_complete_strvec(node->child, new_vec);
-       if (child_completed == NULL)
+       ret = ec_node_complete_child(node->child, completed, parsed, new_vec);
+       if (ret < 0)
                goto fail;
 
        ec_strvec_free(new_vec);
-       new_vec = NULL;
-       ec_completed_merge(completed, child_completed);
 
-       return completed;
+       return 0;
 
  fail:
        ec_strvec_free(new_vec);
-       ec_completed_free(completed);
-       return NULL;
+       return -1;
 }
 
 static void ec_node_sh_lex_free_priv(struct ec_node *gen_node)
@@ -359,6 +347,7 @@ struct ec_node *ec_node_sh_lex(const char *id, struct ec_node *child)
        return &node->gen;
 }
 
+/* LCOV_EXCL_START */
 static int ec_node_sh_lex_testcase(void)
 {
        struct ec_node *node;
@@ -450,6 +439,7 @@ static int ec_node_sh_lex_testcase(void)
        ec_node_free(node);
        return ret;
 }
+/* LCOV_EXCL_STOP */
 
 static struct ec_test ec_node_sh_lex_test = {
        .name = "node_sh_lex",