yin parser CHANGE add support for pattern element
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 95d9dce..a7a9ddb 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -428,6 +428,49 @@
}
/**
+ * @brief Parse pattern 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,out] patterns Restrictions to add to.
+ *
+ * @return LY_ERR values.
+ */
+static LY_ERR
+yin_parse_pattern(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
+ struct lysp_type *type)
+{
+ const char *real_value = NULL;
+ char *saved_value = NULL;
+ struct lysp_restr *restr;
+
+ LY_ARRAY_NEW_RET(ctx->xml_ctx.ctx, type->patterns, restr, LY_EMEM);
+ LY_CHECK_RET(yin_parse_attribute(ctx, attrs, YIN_ARG_VALUE, &real_value, Y_STR_ARG, YANG_PATTERN));
+ size_t len = strlen(real_value);
+
+ saved_value = malloc(len + 2);
+ LY_CHECK_ERR_RET(!saved_value, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
+ memmove(saved_value + 1, real_value, len);
+ FREE_STRING(ctx->xml_ctx.ctx, real_value);
+ saved_value[0] = 0x06;
+ saved_value[len + 1] = '\0';
+ restr->arg = lydict_insert_zc(ctx->xml_ctx.ctx, saved_value);
+ LY_CHECK_ERR_RET(!restr->arg, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
+ type->flags |= LYS_SET_PATTERN;
+
+ struct yin_subelement subelems[6] = {
+ {YANG_DESCRIPTION, &restr->dsc, YIN_SUBELEM_UNIQUE},
+ {YANG_ERROR_APP_TAG, &restr->eapptag, YIN_SUBELEM_UNIQUE},
+ {YANG_ERROR_MESSAGE, &restr->emsg, YIN_SUBELEM_UNIQUE},
+ {YANG_MODIFIER, &restr->arg, YIN_SUBELEM_UNIQUE},
+ {YANG_REFERENCE, &restr->ref, YIN_SUBELEM_UNIQUE},
+ {YANG_CUSTOM, NULL, 0}
+ };
+ return yin_parse_content(ctx, subelems, 6, data, YANG_PATTERN, NULL, &restr->exts);
+}
+
+/**
* @brief Parse simple element without any special constraints and argument mapped to yin attribute, that can have
* more instances, such as base or if-feature.
*
@@ -506,6 +549,7 @@
yin_parse_modifier(struct yin_parser_ctx *ctx, struct yin_arg_record *attrs, const char **data,
const char **pat, struct lysp_ext_instance **exts)
{
+ assert(**pat == 0x06);
const char *temp_val;
char *modified_val;
struct yin_subelement subelems[1] = {
@@ -518,13 +562,13 @@
FREE_STRING(ctx->xml_ctx.ctx, temp_val);
return LY_EVALID;
}
- assert(temp_val[0] == 0x06);
+ lydict_remove(ctx->xml_ctx.ctx, temp_val);
/* allocate new value */
- modified_val = malloc(strlen(temp_val) + 1);
+ modified_val = malloc(strlen(*pat) + 1);
LY_CHECK_ERR_RET(!modified_val, LOGMEM(ctx->xml_ctx.ctx), LY_EMEM);
- strcpy(modified_val, temp_val);
- lydict_remove(ctx->xml_ctx.ctx, temp_val);
+ strcpy(modified_val, *pat);
+ lydict_remove(ctx->xml_ctx.ctx, *pat);
/* modify the new value */
modified_val[0] = 0x15;
@@ -991,6 +1035,7 @@
case YANG_PATH:
break;
case YANG_PATTERN:
+ ret = yin_parse_pattern(ctx, subelem_attrs, data, (struct lysp_type *)subelem_info_rec->dest);
break;
case YANG_VALUE:
case YANG_POSITION: