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;