yin parser CHANGE add support for augment element
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 0dfe89e..4d75d66 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -1749,7 +1749,6 @@
struct typedef_meta typedef_meta = {(struct lysp_node *)cont, &cont->typedefs};
struct notif_meta notif_meta = {(struct lysp_node *)cont, &cont->notifs};
struct yin_subelement subelems[21] = {
- /* TODO action */
{YANG_ACTION, &act_meta, YIN_SUBELEM_VER2},
{YANG_ANYDATA, &new_node_meta, YIN_SUBELEM_VER2},
{YANG_ANYXML, &new_node_meta, 0},
@@ -1939,6 +1938,16 @@
return LY_SUCCESS;
}
+/**
+ * @brief Parse action element.
+ *
+ * @param[in,out] ctx YIN parser context for logging and to store current state.
+ * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
+ * @param[in,out] data Data to read from, always moved to currently handled character.
+ * @param[in] act_meta Meta information about parent node and actions to add to.
+ *
+ * @return LY_ERR values.
+ */
static LY_ERR
yin_parse_action(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
struct action_meta *act_meta)
@@ -1976,6 +1985,61 @@
}
/**
+ * @brief Parse augment element.
+ *
+ * @param[in,out] ctx YIN parser context for logging and to store current state.
+ * @param[in] attrs [Sized array](@ref sizedarrays) of attributes of current element.
+ * @param[in,out] data Data to read from, always moved to currently handled character.
+ * @param[in] aug_meta Meta information about parent node and augments to add to.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_augment(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+ struct augment_meta *aug_meta)
+{
+ struct lysp_augment *aug;
+
+ /* create new augment */
+ LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, *aug_meta->augments, aug, LY_EMEM);
+ aug->nodetype = LYS_AUGMENT;
+ aug->parent = aug_meta->parent;
+
+ /* parse argument */
+ LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_TARGET_NODE, &aug->nodeid, Y_STR_ARG, YANG_AUGMENT));
+ YANG_CHECK_NONEMPTY((struct lys_parser_ctx *)ctx, strlen(aug->nodeid), "augment");
+
+ /* parser augment content */
+ struct action_meta act_meta = {(struct lysp_node *)aug, &aug->actions};
+ struct tree_node_meta node_meta = {(struct lysp_node *)aug, &aug->child};
+ struct notif_meta notif_meta = {(struct lysp_node *)aug, &aug->notifs};
+ struct yin_subelement subelems[17] = {
+ {YANG_ACTION, &act_meta, YIN_SUBELEM_VER2},
+ {YANG_ANYDATA, &node_meta, YIN_SUBELEM_VER2},
+ {YANG_ANYXML, &node_meta, 0},
+ {YANG_CASE, &node_meta, 0},
+ {YANG_CHOICE, &node_meta, 0},
+ {YANG_CONTAINER, &node_meta, 0},
+ {YANG_DESCRIPTION, &aug->dsc, YIN_SUBELEM_UNIQUE},
+ {YANG_IF_FEATURE, &aug->iffeatures, 0},
+ {YANG_LEAF, &node_meta, 0},
+ {YANG_LEAF_LIST, &node_meta, 0},
+ {YANG_LIST, &node_meta, 0},
+ {YANG_NOTIFICATION, ¬if_meta, YIN_SUBELEM_VER2},
+ {YANG_REFERENCE, &aug->ref, YIN_SUBELEM_UNIQUE},
+ {YANG_STATUS, &aug->flags, YIN_SUBELEM_UNIQUE},
+ {YANG_USES, &node_meta, 0},
+ {YANG_WHEN, &aug->when, YIN_SUBELEM_UNIQUE},
+ {YANG_CUSTOM, NULL, 0},
+ };
+ LY_CHECK_RET(yin_parse_content(ctx, subelems, 17, data, YANG_AUGMENT, NULL, &aug->exts));
+
+ LY_CHECK_RET(lysp_parse_finalize_reallocated((struct lys_parser_ctx *)ctx, NULL, NULL, aug->actions, aug->notifs));
+
+ return LY_SUCCESS;
+}
+
+/**
* @brief Map keyword type to substatement info.
*
* @param[in] kw Keyword type.
@@ -2134,6 +2198,7 @@
ret = yin_parse_argument_element(ctx, attrs, data, (struct yin_argument_meta *)subelem->dest, exts);
break;
case YANG_AUGMENT:
+ ret = yin_parse_augment(ctx, attrs, data, (struct augment_meta *)subelem->dest);
break;
case YANG_BASE:
if (current_element == YANG_TYPE) {
diff --git a/src/parser_yin.h b/src/parser_yin.h
index 3174abe..fca7c04 100644
--- a/src/parser_yin.h
+++ b/src/parser_yin.h
@@ -133,7 +133,7 @@
struct lysp_action_inout *inout_p; /**< inout_p Input/output pointer to write to. */
};
-/* Meta information passed to yin_parse_action */
+/* Meta information passed to yin_parse_action function */
struct action_meta {
struct lysp_node *parent; /**< Parent node. */
struct lysp_action **actions; /**< Actions to add to. */
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index d360e33..b1fa9e8 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -43,6 +43,7 @@
void lysp_grp_free(struct ly_ctx *ctx, struct lysp_grp *grp);
void lysp_action_inout_free(struct ly_ctx *ctx, struct lysp_action_inout *inout);
void lysp_action_free(struct ly_ctx *ctx, struct lysp_action *action);
+void lysp_augment_free(struct ly_ctx *ctx, struct lysp_augment *augment);
struct state {
struct ly_ctx *ctx;
@@ -57,7 +58,7 @@
int store = -1; /* negative for infinite logging, positive for limited logging */
/* set to 0 to printing error messages to stderr instead of checking them in code */
-#define ENABLE_LOGGER_CHECKING 1
+#define ENABLE_LOGGER_CHECKING 0
#if ENABLE_LOGGER_CHECKING
static void
@@ -2311,7 +2312,7 @@
"<description><text>desc</text></description>"
"<reference><text>ref</text></reference>"
"<refine target-node=\"target\"/>"
- /* TODO add uses-augment-stmt instance */
+ "<augment target-node=\"target\" />"
"</uses>"
ELEMENT_WRAPPER_END;
assert_int_equal(test_element_helper(st, &data, &node_meta, NULL, NULL, true), LY_SUCCESS);
@@ -2327,6 +2328,7 @@
assert_string_equal(parsed->ref, "ref");
assert_string_equal(parsed->refines->nodeid, "target");
assert_string_equal(parsed->when->cond, "cond");
+ assert_string_equal(parsed->augments->nodeid, "target");
lysp_node_free(st->ctx, siblings);
siblings = NULL;
@@ -3173,6 +3175,78 @@
st->finished_correctly = true;
}
+static void
+test_augment_elem(void **state)
+{
+ struct state *st = *state;
+ const char *data;
+ struct lysp_augment *augments = NULL;
+ struct augment_meta aug_meta = {NULL, &augments};
+
+ st->yin_ctx->mod_version = LYS_VERSION_1_1;
+ data = ELEMENT_WRAPPER_START
+ "<augment target-node=\"target\">"
+ "<action name=\"action\"/>"
+ "<anydata name=\"anyd\"/>"
+ "<anyxml name=\"anyx\"/>"
+ "<case name=\"case\"/>"
+ "<choice name=\"choice\"/>"
+ "<container name=\"subcont\"/>"
+ "<description><text>desc</text></description>"
+ "<if-feature name=\"iff\"/>"
+ "<leaf name=\"leaf\"/>"
+ "<leaf-list name=\"llist\"/>"
+ "<list name=\"list\"/>"
+ "<notification name=\"notif\"/>"
+ "<reference><text>ref</text></reference>"
+ "<status value=\"current\"/>"
+ "<uses name=\"uses\"/>"
+ "<when condition=\"when-cond\"/>"
+ "</augment>"
+ ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
+ assert_string_equal(augments->nodeid, "target");
+ assert_null(augments->parent);
+ assert_int_equal(augments->nodetype, LYS_AUGMENT);
+ assert_true(augments->flags & LYS_STATUS_CURR);
+ assert_string_equal(augments->dsc, "desc");
+ assert_string_equal(augments->ref, "ref");
+ assert_string_equal(augments->when->cond, "when-cond");
+ assert_string_equal(*augments->iffeatures, "iff");
+ assert_string_equal(augments->child->name, "anyd");
+ assert_int_equal(augments->child->nodetype, LYS_ANYDATA);
+ assert_string_equal(augments->child->next->name, "anyx");
+ assert_int_equal(augments->child->next->nodetype, LYS_ANYXML);
+ assert_string_equal(augments->child->next->next->name, "case");
+ assert_int_equal(augments->child->next->next->nodetype, LYS_CASE);
+ assert_string_equal(augments->child->next->next->next->name, "choice");
+ assert_int_equal(augments->child->next->next->next->nodetype, LYS_CHOICE);
+ assert_string_equal(augments->child->next->next->next->next->name, "subcont");
+ assert_int_equal(augments->child->next->next->next->next->nodetype, LYS_CONTAINER);
+ assert_string_equal(augments->child->next->next->next->next->next->name, "leaf");
+ assert_int_equal(augments->child->next->next->next->next->next->nodetype, LYS_LEAF);
+ assert_string_equal(augments->child->next->next->next->next->next->next->name, "llist");
+ assert_int_equal(augments->child->next->next->next->next->next->next->nodetype, LYS_LEAFLIST);
+ assert_string_equal(augments->child->next->next->next->next->next->next->next->name, "list");
+ assert_int_equal(augments->child->next->next->next->next->next->next->next->nodetype, LYS_LIST);
+ assert_string_equal(augments->child->next->next->next->next->next->next->next->next->name, "uses");
+ assert_int_equal(augments->child->next->next->next->next->next->next->next->next->nodetype, LYS_USES);
+ assert_null(augments->child->next->next->next->next->next->next->next->next->next);
+ assert_string_equal(augments->actions->name, "action");
+ assert_string_equal(augments->notifs->name, "notif");
+ assert_null(augments->exts);
+ FREE_ARRAY(st->ctx, augments, lysp_augment_free)
+ augments = NULL;
+
+ data = ELEMENT_WRAPPER_START "<augment target-node=\"target\" />" ELEMENT_WRAPPER_END;
+ assert_int_equal(test_element_helper(st, &data, &aug_meta, NULL, NULL, true), LY_SUCCESS);
+ assert_string_equal(augments->nodeid, "target");
+ FREE_ARRAY(st->ctx, augments, lysp_augment_free)
+ augments = NULL;
+
+ st->finished_correctly = true;
+}
+
int
main(void)
{
@@ -3241,6 +3315,7 @@
cmocka_unit_test_setup_teardown(test_choice_elem, setup_element_test, teardown_element_test),
cmocka_unit_test_setup_teardown(test_inout_elem, setup_element_test, teardown_element_test),
cmocka_unit_test_setup_teardown(test_action_elem, setup_element_test, teardown_element_test),
+ cmocka_unit_test_setup_teardown(test_augment_elem, setup_element_test, teardown_element_test),
};
return cmocka_run_group_tests(tests, setup_ly_ctx, destroy_ly_ctx);