yin parser CHANGE add support for position and value elements
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 5b84bfa..26223be 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -19,6 +19,7 @@
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
+#include <errno.h>
#include "context.h"
#include "dict.h"
@@ -336,7 +337,7 @@
signed char subelem_info_size, enum yang_keyword current_element)
{
for (signed char i = 0; i < subelem_info_size; ++i) {
- /* if there is element that is mandatory and isn't parsed log error and rturn LY_EVALID */
+ /* if there is element that is mandatory and isn't parsed log error and return LY_EVALID */
if (subelem_info[i].flags & YIN_SUBELEM_MANDATORY && !(subelem_info[i].flags & YIN_SUBELEM_PARSED)) {
LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LYVE_SYNTAX_YIN, "Missing mandatory subelement %s of %s element.",
ly_stmt2str(subelem_info[i].type), ly_stmt2str(current_element));
@@ -374,7 +375,6 @@
return LY_SUCCESS;
}
-/* TODO add something like ifdef NDEBUG around this function, this is supposed to be checked only in debug mode */
/**
* @brief Helper function to check if array of information about subelements is in ascending order.
*
@@ -383,6 +383,7 @@
*
* @return True iff subelem_info array is in ascending order, False otherwise.
*/
+#ifndef NDEBUG
static bool
is_ordered(struct yin_subelement *subelem_info, signed char subelem_info_size)
{
@@ -397,6 +398,7 @@
return true;
}
+#endif
/**
* @brief Parse simple element without any special constraints and argument mapped to yin attribute,
@@ -452,6 +454,77 @@
}
/**
+ * @brief Parse position or value 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] kw Type of current element, can be set to YANG_POSITION or YANG_VALUE.
+ * @param[out] enm Enum structure to save value, flags and extensions.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_value_pos_element(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+ enum yang_keyword kw, struct lysp_type_enum *enm)
+{
+ assert(kw == YANG_POSITION || kw == YANG_VALUE);
+ const char *temp_val = NULL;
+ char *ptr;
+ long int num;
+ unsigned long int unum;
+
+ /* set value flag */
+ enm->flags |= LYS_SET_VALUE;
+
+ /* get attribute value */
+ yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &temp_val, Y_STR_ARG, kw);
+ if (!temp_val || (temp_val[0] == '+') || ((temp_val[0] == '0') && (temp_val[0] != '\0')) || ((kw == YANG_VALUE) && !strcmp(temp_val, "-0"))) {
+ LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
+ goto error;
+ }
+
+ /* convert value */
+ errno = 0;
+ if (kw == YANG_VALUE) {
+ num = strtol(temp_val, &ptr, 10);
+ if (num < INT64_C(-2147483648) || num > INT64_C(2147483647)) {
+ LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
+ goto error;
+ }
+ } else {
+ unum = strtoul(temp_val, &ptr, 10);
+ if (unum > UINT64_C(4294967295)) {
+ LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
+ goto error;
+ }
+ }
+ /* check if whole argument value was converted */
+ if (*ptr != '\0') {
+ LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_INVAL, strlen(temp_val), temp_val, ly_stmt2str(kw));
+ }
+ if (errno == ERANGE) {
+ LOGVAL_PARSER((struct lys_parser_ctx *)ctx, LY_VCODE_OOB, strlen(temp_val), temp_val, ly_stmt2str(kw));
+ goto error;
+ }
+ /* save correctly ternary operator can't be used because num and unum have different signes */
+ if (kw == YANG_VALUE) {
+ enm->value = num;
+ } else {
+ enm->value = unum;
+ }
+ FREE_STRING(ctx->xml_ctx.ctx, temp_val);
+
+ /* parse subelements */
+ struct yin_subelement subelems[1] = {{YANG_CUSTOM, NULL, 0}};
+ return yin_parse_content(ctx, subelems, 1, data, kw, NULL, &enm->exts);
+
+ error:
+ FREE_STRING(ctx->xml_ctx.ctx, temp_val);
+ return LY_EVALID;
+}
+
+/**
* @brief Function to parse meta tags (description, contact, ...) eg. elements with
* text element as child
*
@@ -575,7 +648,6 @@
}
}
-
/**
* @brief Parse belongs-to element.
*
@@ -777,7 +849,10 @@
break;
case YANG_PATTERN:
break;
+ case YANG_VALUE:
case YANG_POSITION:
+ ret = yin_parse_value_pos_element(ctx, subelem_attrs, data, kw,
+ (struct lysp_type_enum *)subelem_info_rec->dest);
break;
case YANG_PREFIX:
ret = yin_parse_simple_element(ctx, subelem_attrs, data, kw,
@@ -817,8 +892,6 @@
break;
case YANG_USES:
break;
- case YANG_VALUE:
- break;
case YANG_WHEN:
ret = yin_parse_when(ctx, subelem_attrs, data, (struct lysp_when **)subelem_info_rec->dest);
break;
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
index 4b9d70a..180dc4b 100644
--- a/tests/src/test_parser_yin.c
+++ b/tests/src/test_parser_yin.c
@@ -674,6 +674,8 @@
"<error-app-tag value=\"err-app-tag\"/>"
"<units name=\"radians\"></units>"
"<default value=\"default-value\"/>"
+ "<position value=\"25\"></position>"
+ "<value value=\"-5\"/>"
"</prefix>";
struct lysp_ext_instance *exts = NULL;
const char **if_features = NULL;
@@ -681,22 +683,25 @@
const char *value, *err_msg, *app_tag, *units, *def;
struct lysp_ext *ext_def = NULL;
struct lysp_when *when_p = NULL;
+ struct lysp_type_enum pos_enum = {.flags = 0}, val_enum = {.flags = 0};
uint8_t config = 0;
lyxml_get_element(&st->yin_ctx->xml_ctx, &data, &prefix.value, &prefix.len, &name.value, &name.len);
yin_load_attributes(st->yin_ctx, &data, &attrs);
- struct yin_subelement subelems[10] = {{YANG_CONFIG, &config, 0},
+ struct yin_subelement subelems[12] = {{YANG_CONFIG, &config, 0},
{YANG_DEFAULT, &def, 0},
{YANG_ERROR_APP_TAG, &app_tag, 0},
{YANG_ERROR_MESSAGE, &err_msg, 0},
{YANG_EXTENSION, &ext_def, 0},
{YANG_IF_FEATURE, &if_features, 0},
+ {YANG_POSITION, &pos_enum, 0},
{YANG_UNITS, &units, 0},
+ {YANG_VALUE, &val_enum, 0},
{YANG_WHEN, &when_p, 0},
{YANG_CUSTOM, NULL, 0},
{YIN_TEXT, &value, 0}};
- ret = yin_parse_content(st->yin_ctx, subelems, 10, &data, YANG_PREFIX, NULL, &exts);
+ ret = yin_parse_content(st->yin_ctx, subelems, 12, &data, YANG_PREFIX, NULL, &exts);
assert_int_equal(ret, LY_SUCCESS);
assert_int_equal(st->yin_ctx->xml_ctx.status, LYXML_END);
/* check parsed values */
@@ -709,6 +714,10 @@
assert_string_equal(when_p->dsc, "when_desc");
assert_string_equal(when_p->ref, "when_ref");
assert_int_equal(config, LYS_CONFIG_W);
+ assert_int_equal(pos_enum.value, 25);
+ assert_true(pos_enum.flags | LYS_SET_VALUE);
+ assert_int_equal(val_enum.value, -5);
+ assert_true(val_enum.flags | LYS_SET_VALUE);
assert_string_equal(err_msg, "error-msg");
assert_string_equal(app_tag, "err-app-tag");
/* cleanup */