plugins exts CHANGE ext parsing isolated into a callback

Lots of refactoring and finishing up included.
diff --git a/src/plugins_exts/metadata.c b/src/plugins_exts/metadata.c
index f6e1580..fc49e2c 100644
--- a/src/plugins_exts/metadata.c
+++ b/src/plugins_exts/metadata.c
@@ -1,9 +1,10 @@
 /**
  * @file metadata.c
  * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
  * @brief libyang extension plugin - Metadata (RFC 7952)
  *
- * Copyright (c) 2019 CESNET, z.s.p.o.
+ * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -21,93 +22,150 @@
 #include "libyang.h"
 #include "plugins_exts.h"
 
-/**
- * @brief Representation of the compiled metadata substatements - simplify storage for the items available via
- * ::lysc_ext_substmt.
- */
-struct lyext_metadata {
-    struct lysc_type *type;            /**< type of the metadata (mandatory) */
+struct lysp_ext_metadata {
+    struct lysp_type *type;            /**< type of the metadata (mandatory) */
     const char *units;                 /**< units of the leaf's type */
-    struct lysc_iffeature *iffeatures; /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
+    struct lysp_qname *iffeatures;     /**< list of if-feature expressions ([sized array](@ref sizedarrays)) */
     const char *dsc;                   /**< description */
     const char *ref;                   /**< reference */
     uint16_t flags;                    /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */
 };
 
+struct lysc_ext_metadata {
+    struct lysc_type *type;            /**< type of the metadata (mandatory) */
+    const char *units;                 /**< units of the leaf's type */
+    const char *dsc;                   /**< description */
+    const char *ref;                   /**< reference */
+    uint16_t flags;                    /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_* values are allowed */
+};
+
+/**
+ * @brief Parse annotation extension instances.
+ *
+ * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
+ */
+static LY_ERR
+annotation_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
+{
+    LY_ERR r;
+    struct lysp_ext_metadata *ann_pdata;
+    struct lysp_module *pmod;
+    LY_ARRAY_COUNT_TYPE u;
+
+    /* annotations can appear only at the top level of a YANG module or submodule */
+    if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is allowed only at the top level of a YANG module or "
+                "submodule, but it is placed in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt));
+        return LY_EVALID;
+    }
+
+    pmod = ext->parent;
+
+    /* check for duplication */
+    LY_ARRAY_FOR(pmod->exts, u) {
+        if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
+            /* duplication of the same annotation extension in a single module */
+            lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
+            return LY_EVALID;
+        }
+    }
+
+    /* parse annotation substatements */
+    ext->parsed = ann_pdata = calloc(1, sizeof *ann_pdata);
+    if (!ann_pdata) {
+        goto emem;
+    }
+    LY_ARRAY_CREATE_GOTO(lyplg_extp_cur_pmod(pctx)->mod->ctx, ext->substmts, 6, r, emem);
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[0].stmt = LY_STMT_IF_FEATURE;
+    ext->substmts[0].storage = &ann_pdata->iffeatures;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[1].stmt = LY_STMT_UNITS;
+    ext->substmts[1].storage = &ann_pdata->units;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[2].stmt = LY_STMT_STATUS;
+    ext->substmts[2].storage = &ann_pdata->flags;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[3].stmt = LY_STMT_TYPE;
+    ext->substmts[3].storage = &ann_pdata->type;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[4].stmt = LY_STMT_DESCRIPTION;
+    ext->substmts[4].storage = &ann_pdata->dsc;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[5].stmt = LY_STMT_REFERENCE;
+    ext->substmts[5].storage = &ann_pdata->ref;
+
+    if ((r = lyplg_ext_parse_extension_instance(pctx, ext))) {
+        return r;
+    }
+
+    /* check for mandatory substatements */
+    if (!ann_pdata->type) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Missing mandatory keyword \"type\" as a child of \"%s %s\".",
+                ext->name, ext->argument);
+        return LY_EVALID;
+    }
+
+    return LY_SUCCESS;
+
+emem:
+    lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
+    return LY_EMEM;
+}
+
 /**
  * @brief Compile annotation extension instances.
  *
  * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
  */
 static LY_ERR
-annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
+annotation_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
 {
     LY_ERR ret;
-    struct lyext_metadata *annotation;
-    struct lysc_module *mod_c;
-    LY_ARRAY_COUNT_TYPE u;
-
-    /* annotations can appear only at the top level of a YANG module or submodule */
-    if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                "Extension %s is allowed only at the top level of a YANG module or submodule, but it is placed in \"%s\" statement.",
-                p_ext->name, ly_stmt2str(c_ext->parent_stmt));
-        return LY_EVALID;
-    }
-    /* check mandatory argument */
-    if (!c_ext->argument) {
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                "Extension %s is instantiated without mandatory argument representing metadata name.", p_ext->name);
-        return LY_EVALID;
-    }
-
-    mod_c = (struct lysc_module *)c_ext->parent;
-
-    /* check for duplication */
-    LY_ARRAY_FOR(mod_c->exts, u) {
-        if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
-            /* duplication of the same annotation extension in a single module */
-            lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
-            return LY_EVALID;
-        }
-    }
+    struct lysc_ext_metadata *ann_cdata;
 
     /* compile annotation substatements */
-    c_ext->data = annotation = calloc(1, sizeof *annotation);
-    if (!annotation) {
+    ext->compiled = ann_cdata = calloc(1, sizeof *ann_cdata);
+    if (!ann_cdata) {
         goto emem;
     }
-    LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), c_ext->substmts, 6, ret, emem);
+    LY_ARRAY_CREATE_GOTO(lysc_ctx_get_ctx(cctx), ext->substmts, 6, ret, emem);
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[ANNOTATION_SUBSTMT_IFF].stmt = LY_STMT_IF_FEATURE;
-    c_ext->substmts[ANNOTATION_SUBSTMT_IFF].storage = &annotation->iffeatures;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[0].stmt = LY_STMT_IF_FEATURE;
+    ext->substmts[0].storage = NULL;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].stmt = LY_STMT_UNITS;
-    c_ext->substmts[ANNOTATION_SUBSTMT_UNITS].storage = &annotation->units;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[1].stmt = LY_STMT_UNITS;
+    ext->substmts[1].storage = &ann_cdata->units;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].stmt = LY_STMT_STATUS;
-    c_ext->substmts[ANNOTATION_SUBSTMT_STATUS].storage = &annotation->flags;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[2].stmt = LY_STMT_STATUS;
+    ext->substmts[2].storage = &ann_cdata->flags;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].stmt = LY_STMT_TYPE;
-    c_ext->substmts[ANNOTATION_SUBSTMT_TYPE].storage = &annotation->type;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[3].stmt = LY_STMT_TYPE;
+    ext->substmts[3].storage = &ann_cdata->type;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[ANNOTATION_SUBSTMT_DSC].stmt = LY_STMT_DESCRIPTION;
-    c_ext->substmts[ANNOTATION_SUBSTMT_DSC].storage = &annotation->dsc;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[4].stmt = LY_STMT_DESCRIPTION;
+    ext->substmts[4].storage = &ann_cdata->dsc;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[ANNOTATION_SUBSTMT_REF].stmt = LY_STMT_REFERENCE;
-    c_ext->substmts[ANNOTATION_SUBSTMT_REF].storage = &annotation->ref;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[5].stmt = LY_STMT_REFERENCE;
+    ext->substmts[5].storage = &ann_cdata->ref;
 
-    ret = lys_compile_extension_instance(cctx, p_ext, c_ext);
+    ret = lyplg_ext_compile_extension_instance(cctx, extp, ext);
     return ret;
 
 emem:
-    lyplg_ext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
+    lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
     return LY_EMEM;
 }
 
@@ -117,27 +175,43 @@
  * Implementation of ::lyplg_ext_schema_printer_clb set as ::lyext_plugin::sprinter
  */
 static LY_ERR
-annotation_schema_printer(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
+annotation_sprinter(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
 {
-    lysc_print_extension_instance(ctx, ext, flag);
+    lyplg_ext_print_extension_instance(ctx, ext, flag);
 
     return LY_SUCCESS;
 }
 
 /**
- * @brief Free annotation extension instances' data.
+ * @brief Free parsed annotation extension instance data.
  *
- * Implementation of ::lyplg_ext_free_clb callback set as ::lyext_plugin::free.
+ * Implementation of ::lyplg_ext_parse_free_clb callback set as ::lyext_plugin::pfree.
  */
 static void
-annotation_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+annotation_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
 {
     if (!ext->substmts) {
         return;
     }
 
-    lyplg_ext_instance_substatements_free(ctx, ext->substmts);
-    free(ext->data);
+    lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
+    free(ext->parsed);
+}
+
+/**
+ * @brief Free compiled annotation extension instance data.
+ *
+ * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree.
+ */
+static void
+annotation_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+{
+    if (!ext->substmts) {
+        return;
+    }
+
+    lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
+    free(ext->compiled);
 }
 
 /**
@@ -153,13 +227,15 @@
         .revision = "2016-08-05",
         .name = "annotation",
 
-        .plugin.id = "libyang 2 - metadata, version 1",
-        .plugin.compile = &annotation_compile,
-        .plugin.sprinter = &annotation_schema_printer,
-        .plugin.free = annotation_free,
+        .plugin.id = "ly2 metadata v1",
+        .plugin.parse = annotation_parse,
+        .plugin.compile = annotation_compile,
+        .plugin.sprinter = annotation_sprinter,
         .plugin.node = NULL,
         .plugin.snode = NULL,
-        .plugin.validate = NULL
+        .plugin.validate = NULL,
+        .plugin.pfree = annotation_pfree,
+        .plugin.cfree = annotation_cfree,
     },
     {0}     /* terminating zeroed record */
 };
diff --git a/src/plugins_exts/metadata.h b/src/plugins_exts/metadata.h
index 6387e1f..59ea2bf 100644
--- a/src/plugins_exts/metadata.h
+++ b/src/plugins_exts/metadata.h
@@ -23,13 +23,6 @@
 extern "C" {
 #endif
 
-#define ANNOTATION_SUBSTMT_IFF     0 /**< index for the LY_STMT_IF_FEATURE substatement in annotation's ::lysc_ext_instance.substmts */
-#define ANNOTATION_SUBSTMT_UNITS   1 /**< index for the LY_STMT_UNITS substatement in annotation's ::lysc_ext_instance.substmts */
-#define ANNOTATION_SUBSTMT_STATUS  2 /**< index for the LY_STMT_STATUS substatement in annotation's ::lysc_ext_instance.substmts */
-#define ANNOTATION_SUBSTMT_TYPE    3 /**< index for the LY_STMT_TYPE substatement in annotation's ::lysc_ext_instance.substmts */
-#define ANNOTATION_SUBSTMT_DSC     4 /**< index for the LY_STMT_DSC substatement in annotation's ::lysc_ext_instance.substmts */
-#define ANNOTATION_SUBSTMT_REF     5 /**< index for the LY_STMT_REF substatement in annotation's ::lysc_ext_instance.substmts */
-
 /**
  * @brief Metadata structure.
  *
diff --git a/src/plugins_exts/nacm.c b/src/plugins_exts/nacm.c
index a83e32b..7323cd2 100644
--- a/src/plugins_exts/nacm.c
+++ b/src/plugins_exts/nacm.c
@@ -1,9 +1,10 @@
 /**
  * @file nacm.c
  * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
  * @brief libyang extension plugin - NACM (RFC 6536)
  *
- * Copyright (c) 2019 CESNET, z.s.p.o.
+ * Copyright (c) 2019 - 2022 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -16,11 +17,12 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "compat.h"
 #include "libyang.h"
 #include "plugins_exts.h"
 
 struct nacm_dfs_arg {
-    struct lysc_ext_instance *c_ext;
+    struct lysc_ext_instance *ext;
     struct lysc_node *parent;
 };
 
@@ -39,7 +41,7 @@
     if ((node != arg->parent) && !(node->nodetype & (LYS_INPUT | LYS_OUTPUT))) {
         /* check that the node does not have its own NACM extension instance */
         LY_ARRAY_FOR(node->exts, u) {
-            if (node->exts[u].def == arg->c_ext->def) {
+            if (node->exts[u].def == arg->ext->def) {
                 /* the child already have its own NACM flag, so skip the subtree */
                 *dfs_continue = 1;
                 return LY_SUCCESS;
@@ -49,99 +51,99 @@
         /* duplicate this one to inherit it to the child */
         LY_ARRAY_NEW_GOTO(node->module->ctx, node->exts, inherited, ret, emem);
 
-        inherited->def = lysc_ext_dup(arg->c_ext->def);
+        inherited->def = lysc_ext_dup(arg->ext->def);
         inherited->parent = node;
-        inherited->parent_stmt = lys_nodetype2stmt(node->nodetype);
-        if (arg->c_ext->argument) {
-            LY_ERR ret;
-
-            if ((ret = lydict_insert(node->module->ctx, arg->c_ext->argument, strlen(arg->c_ext->argument),
-                    &inherited->argument))) {
+        inherited->parent_stmt = lyplg_ext_nodetype2stmt(node->nodetype);
+        if (arg->ext->argument) {
+            if ((ret = lydict_insert(node->module->ctx, arg->ext->argument, 0, &inherited->argument))) {
                 return ret;
             }
         }
         /* TODO duplicate extension instances */
-        inherited->data = arg->c_ext->data;
+        inherited->compiled = arg->ext->compiled;
     }
 
     return LY_SUCCESS;
 
 emem:
-    lyplg_ext_log(arg->c_ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s()).", __func__);
+    lyplg_ext_compile_log(NULL, arg->ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
     return ret;
 }
 
 /**
- * @brief Compile NAMC's extension instances.
+ * @brief Parse NACM extension instances.
  *
- * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
+ * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
  */
 static LY_ERR
-nacm_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
+nacm_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
 {
-    LY_ERR ret;
-    struct lysc_node *parent = NULL;
+    struct lysp_node *parent = NULL;
     LY_ARRAY_COUNT_TYPE u;
-    struct nacm_dfs_arg dfs_arg;
-
-    static const uint8_t nacm_deny_all = 1;
-    static const uint8_t nacm_deny_write = 2;
-
-    /* store the NACM flag */
-    if (!strcmp(c_ext->def->name, "default-deny-write")) {
-        c_ext->data = (void *)&nacm_deny_write;
-    } else if (!strcmp(c_ext->def->name, "default-deny-all")) {
-        c_ext->data = (void *)&nacm_deny_all;
-    } else {
-        return LY_EINT;
-    }
 
     /* check that the extension is instantiated at an allowed place - data node */
-    if (!LY_STMT_IS_NODE(c_ext->parent_stmt)) {
-        lyplg_ext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
-                "Extension %s is allowed only in a data nodes, but it is placed in \"%s\" statement.",
-                p_ext->name, ly_stmt2str(c_ext->parent_stmt));
+    if (!(ext->parent_stmt & LY_STMT_NODE_MASK)) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is allowed only in a data nodes, but it is placed in "
+                "\"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt));
         return LY_ENOT;
-    } else {
-        parent = (struct lysc_node *)c_ext->parent;
-        if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA |
-                LYS_CASE | LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
-            /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang
-             * passes all their extensions to their children nodes */
-invalid_parent:
-            lyplg_ext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
-                    "Extension %s is not allowed in %s statement.", p_ext->name, lys_nodetype2str(parent->nodetype));
-            return LY_ENOT;
-        }
-        if ((c_ext->data == (void *)&nacm_deny_write) && (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))) {
-            goto invalid_parent;
-        }
+    }
+
+    parent = ext->parent;
+    if (!(parent->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_CHOICE | LYS_ANYDATA |
+            LYS_CASE | LYS_RPC | LYS_ACTION | LYS_NOTIF)) || (!strcmp(strchr(ext->name, ':') + 1, "default-deny-write") &&
+            (parent->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)))) {
+        /* note LYS_AUGMENT and LYS_USES is not in the list since they are not present in the compiled tree. Instead, libyang
+         * passes all their extensions to their children nodes */
+        lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is not allowed in %s statement.", ext->name,
+                lys_nodetype2str(parent->nodetype));
+        return LY_ENOT;
     }
 
     /* check for duplication */
     LY_ARRAY_FOR(parent->exts, u) {
-        if ((&parent->exts[u] != c_ext) && parent->exts[u].def->plugin &&
-                (parent->exts[u].def->plugin->compile == c_ext->def->plugin->compile)) {
+        if ((&parent->exts[u] != ext) && parent->exts[u].record && (parent->exts[u].record->plugin.id == ext->record->plugin.id)) {
             /* duplication of a NACM extension on a single node
              * We check for all NACM plugins since we want to catch even the situation that there is default-deny-all
              * AND default-deny-write */
-            if (parent->exts[u].def == c_ext->def) {
-                lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                        "Extension %s is instantiated multiple times.", p_ext->name);
+            if (parent->exts[u].name == ext->name) {
+                lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
             } else {
-                lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+                lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID,
                         "Extension nacm:default-deny-write is mixed with nacm:default-deny-all.");
             }
             return LY_EVALID;
         }
     }
 
-    /* inherit the extension instance to all the children nodes */
-    dfs_arg.c_ext = c_ext;
-    dfs_arg.parent = parent;
-    ret = lysc_tree_dfs_full(parent, nacm_inherit_clb, &dfs_arg);
+    return LY_SUCCESS;
+}
 
-    return ret;
+/**
+ * @brief Compile NACM extension instances.
+ *
+ * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
+ */
+static LY_ERR
+nacm_compile(struct lysc_ctx *UNUSED(cctx), const struct lysp_ext_instance *UNUSED(extp), struct lysc_ext_instance *ext)
+{
+    struct nacm_dfs_arg dfs_arg;
+
+    static const uint8_t nacm_deny_all = 1;
+    static const uint8_t nacm_deny_write = 2;
+
+    /* store the NACM flag */
+    if (!strcmp(ext->def->name, "default-deny-write")) {
+        ext->compiled = (void *)&nacm_deny_write;
+    } else if (!strcmp(ext->def->name, "default-deny-all")) {
+        ext->compiled = (void *)&nacm_deny_all;
+    } else {
+        return LY_EINT;
+    }
+
+    /* inherit the extension instance to all the children nodes */
+    dfs_arg.ext = ext;
+    dfs_arg.parent = ext->parent;
+    return lysc_tree_dfs_full(ext->parent, nacm_inherit_clb, &dfs_arg);
 }
 
 /**
@@ -157,49 +159,57 @@
         .revision = "2012-02-22",
         .name = "default-deny-write",
 
-        .plugin.id = "libyang 2 - NACM, version 1",
-        .plugin.compile = &nacm_compile,
+        .plugin.id = "ly2 NACM v1",
+        .plugin.parse = nacm_parse,
+        .plugin.compile = nacm_compile,
         .plugin.sprinter = NULL,
-        .plugin.free = NULL,
         .plugin.node = NULL,
         .plugin.snode = NULL,
-        .plugin.validate = NULL
+        .plugin.validate = NULL,
+        .plugin.pfree = NULL,
+        .plugin.cfree = NULL
     }, {
         .module = "ietf-netconf-acm",
         .revision = "2018-02-14",
         .name = "default-deny-write",
 
-        .plugin.id = "libyang 2 - NACM, version 1",
-        .plugin.compile = &nacm_compile,
+        .plugin.id = "ly2 NACM v1",
+        .plugin.parse = nacm_parse,
+        .plugin.compile = nacm_compile,
         .plugin.sprinter = NULL,
-        .plugin.free = NULL,
         .plugin.node = NULL,
         .plugin.snode = NULL,
-        .plugin.validate = NULL
+        .plugin.validate = NULL,
+        .plugin.pfree = NULL,
+        .plugin.cfree = NULL
     }, {
         .module = "ietf-netconf-acm",
         .revision = "2012-02-22",
         .name = "default-deny-all",
 
-        .plugin.id = "libyang 2 - NACM, version 1",
-        .plugin.compile = &nacm_compile,
+        .plugin.id = "ly2 NACM v1",
+        .plugin.parse = nacm_parse,
+        .plugin.compile = nacm_compile,
         .plugin.sprinter = NULL,
-        .plugin.free = NULL,
         .plugin.node = NULL,
         .plugin.snode = NULL,
-        .plugin.validate = NULL
+        .plugin.validate = NULL,
+        .plugin.pfree = NULL,
+        .plugin.cfree = NULL
     }, {
         .module = "ietf-netconf-acm",
         .revision = "2018-02-14",
         .name = "default-deny-all",
 
-        .plugin.id = "libyang 2 - NACM, version 1",
-        .plugin.compile = &nacm_compile,
+        .plugin.id = "ly2 NACM v1",
+        .plugin.parse = nacm_parse,
+        .plugin.compile = nacm_compile,
         .plugin.sprinter = NULL,
-        .plugin.free = NULL,
         .plugin.node = NULL,
         .plugin.snode = NULL,
-        .plugin.validate = NULL
+        .plugin.validate = NULL,
+        .plugin.pfree = NULL,
+        .plugin.cfree = NULL
     },
     {0} /* terminating zeroed item */
 };
diff --git a/src/plugins_exts/schema_mount.c b/src/plugins_exts/schema_mount.c
index 042bebb..04679aa 100644
--- a/src/plugins_exts/schema_mount.c
+++ b/src/plugins_exts/schema_mount.c
@@ -21,6 +21,7 @@
 #include <string.h>
 
 #include "common.h"
+#include "compat.h"
 #include "dict.h"
 #include "libyang.h"
 #include "log.h"
@@ -56,68 +57,95 @@
     } inln;                             /**< inline mount points */
 };
 
-#define EXT_LOGERR_MEM_RET(ext) \
-        lyplg_ext_log(ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \
+#define EXT_LOGERR_MEM_RET(cctx, ext) \
+        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \
         return LY_EMEM
 
-#define EXT_LOGERR_MEM_GOTO(ext, rc, label) \
-        lyplg_ext_log(ext, LY_LLERR, LY_EMEM, NULL, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \
+#define EXT_LOGERR_MEM_GOTO(cctx, ext, rc, label) \
+        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s:%d).", __FILE__, __LINE__); \
         rc = LY_EMEM; \
         goto label
 
-#define EXT_LOGERR_INT_RET(ext) \
-        lyplg_ext_log(ext, LY_LLERR, LY_EINT, NULL, "Internal error (%s:%d).", __FILE__, __LINE__); \
+#define EXT_LOGERR_INT_RET(cctx, ext) \
+        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EINT, "Internal error (%s:%d).", __FILE__, __LINE__); \
         return LY_EINT
 
 /**
- * @brief Check if given mount point is unique among its' siblings
+ * @brief Check if given mount point is unique among its siblings
  *
- * @param cctx Compilation context.
- * @param c_ext Compiled extension instance for checking uniqueness.
- * @param p_ext Extension instance of the mount-point for comparison.
+ * @param[in] pctx Parse context.
+ * @param[in] ext Parsed extension instance.
  * @return LY_SUCCESS if is unique;
  * @return LY_EINVAL otherwise.
  */
 static LY_ERR
-schema_mount_compile_unique_mp(struct lysc_ctx *cctx, const struct lysc_ext_instance *c_ext,
-        const struct lysp_ext_instance *p_ext)
+schema_mount_parse_unique_mp(struct lysp_ctx *pctx, const struct lysp_ext_instance *ext)
 {
-    struct lysc_ext_instance *exts;
+    struct lysp_ext_instance *exts;
     LY_ARRAY_COUNT_TYPE u;
-    struct lysc_node *parent;
+    struct lysp_node *parent;
 
-    /* check if it is the only instance of the mount-point among its' siblings */
-    parent = (struct lysc_node *)c_ext->parent;
+    /* check if it is the only instance of the mount-point among its siblings */
+    parent = ext->parent;
     exts = parent->exts;
     LY_ARRAY_FOR(exts, u) {
-        if (&exts[u] == c_ext) {
+        if (&exts[u] == ext) {
             continue;
         }
 
-        if (!strcmp(exts[u].def->module->name, "ietf-yang-schema-mount") && !strcmp(exts[u].def->name, "mount-point")) {
-            lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Multiple extension \"%s\" instances.",
-                    p_ext->name);
+        if (!strcmp(exts[u].name, ext->name)) {
+            lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Multiple extension \"%s\" instances.", ext->name);
             return LY_EINVAL;
         }
     }
     return LY_SUCCESS;
 }
 
+/**
+ * @brief Schema mount parse.
+ * Checks if it can be a valid extension instance for yang schema mount.
+ *
+ * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
+ */
+static LY_ERR
+schema_mount_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
+{
+    /* check YANG version 1.1 */
+    if (lyplg_ext_parse_get_cur_pmod(pctx)->version != LYS_VERSION_1_1) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension \"%s\" instance not allowed in YANG version 1 module.",
+                ext->name);
+        return LY_EINVAL;
+    }
+
+    /* check parent nodetype */
+    if ((ext->parent_stmt != LY_STMT_CONTAINER) && (ext->parent_stmt != LY_STMT_LIST)) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension \"%s\" instance allowed only in container or list statement.",
+                ext->name);
+        return LY_EINVAL;
+    }
+
+    /* check uniqueness */
+    if (schema_mount_parse_unique_mp(pctx, ext)) {
+        return LY_EINVAL;
+    }
+
+    /* nothing to actually parse */
+    return LY_SUCCESS;
+}
+
 struct lyplg_ext_sm_shared_cb_data {
     const struct lysc_ext_instance *ext;
     struct lyplg_ext_sm_shared *sm_shared;
 };
 
 static LY_ERR
-schema_mount_compile_mod_dfs_cb(struct lysc_node *node, void *data, ly_bool *dfs_continue)
+schema_mount_compile_mod_dfs_cb(struct lysc_node *node, void *data, ly_bool *UNUSED(dfs_continue))
 {
     struct lyplg_ext_sm_shared_cb_data *cb_data = data;
     struct lyplg_ext_sm *sm_data;
     struct lysc_ext_instance *exts;
     LY_ARRAY_COUNT_TYPE u;
 
-    (void)dfs_continue;
-
     if (node == cb_data->ext->parent) {
         /* parent of the current compiled extension, skip */
         return LY_SUCCESS;
@@ -129,7 +157,7 @@
         if (!strcmp(exts[u].def->module->name, "ietf-yang-schema-mount") && !strcmp(exts[u].def->name, "mount-point") &&
                 (exts[u].argument == cb_data->ext->argument)) {
             /* same mount point, break the DFS search */
-            sm_data = exts[u].data;
+            sm_data = exts[u].compiled;
             cb_data->sm_shared = sm_data->shared;
             return LY_EEXIST;
         }
@@ -164,56 +192,33 @@
  * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
  */
 static LY_ERR
-schema_mount_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
+schema_mount_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *UNUSED(extp), struct lysc_ext_instance *ext)
 {
-    const struct lys_module *cur_mod;
     const struct lysc_node *node;
     struct lyplg_ext_sm *sm_data;
 
-    assert(!strcmp(p_ext->name, "yangmnt:mount-point"));
-
-    /* check YANG version 1.1 */
-    cur_mod = lysc_ctx_get_cur_mod(cctx);
-    if (cur_mod->parsed->version != LYS_VERSION_1_1) {
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                "Extension \"%s\" instance not allowed in YANG version 1 module.", p_ext->name);
-        return LY_EINVAL;
-    }
-
-    /* check parent nodetype */
-    if ((p_ext->parent_stmt != LY_STMT_CONTAINER) && (p_ext->parent_stmt != LY_STMT_LIST)) {
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                "Extension \"%s\" instance allowed only in container or list statement.", p_ext->name);
-        return LY_EINVAL;
-    }
-
-    /* check uniqueness */
-    if (schema_mount_compile_unique_mp(cctx, c_ext, p_ext)) {
-        return LY_EINVAL;
-    }
-
     /* init internal data */
     sm_data = calloc(1, sizeof *sm_data);
     if (!sm_data) {
-        EXT_LOGERR_MEM_RET(c_ext);
+        EXT_LOGERR_MEM_RET(cctx, ext);
     }
-    c_ext->data = sm_data;
+    ext->compiled = sm_data;
 
     /* find the owner module */
-    node = c_ext->parent;
+    node = ext->parent;
     while (node->parent) {
         node = node->parent;
     }
 
     /* reuse/init shared schema */
-    sm_data->shared = schema_mount_compile_find_shared(node->module, c_ext);
+    sm_data->shared = schema_mount_compile_find_shared(node->module, ext);
     if (sm_data->shared) {
         ++sm_data->shared->ref_count;
     } else {
         sm_data->shared = calloc(1, sizeof *sm_data->shared);
         if (!sm_data->shared) {
             free(sm_data);
-            EXT_LOGERR_MEM_RET(c_ext);
+            EXT_LOGERR_MEM_RET(cctx, ext);
         }
         pthread_mutex_init(&sm_data->shared->lock, NULL);
         sm_data->shared->ref_count = 1;
@@ -242,7 +247,7 @@
     /* find the mount point */
     if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']", ext->module->name,
             ext->argument) == -1) {
-        EXT_LOGERR_MEM_RET(ext);
+        EXT_LOGERR_MEM_RET(NULL, ext);
     }
     r = ext_data ? lyd_find_path(ext_data, path, 0, &mpoint) : LY_ENOTFOUND;
     free(path);
@@ -264,7 +269,7 @@
     /* check schema-ref */
     if (lyd_find_path(mpoint, "shared-schema", 0, NULL)) {
         if (lyd_find_path(mpoint, "inline", 0, NULL)) {
-            EXT_LOGERR_INT_RET(ext);
+            EXT_LOGERR_INT_RET(NULL, ext);
         }
         *shared = 0;
     } else {
@@ -308,7 +313,7 @@
 
     /* create the context based on the data */
     if ((rc = ly_ctx_new_yldata(sdirs, ext_data, ly_ctx_get_options(ext->module->ctx), ext_ctx))) {
-        lyplg_ext_log(ext, LY_LLERR, rc, NULL, "Failed to create context for the schema-mount data.");
+        lyplg_ext_compile_log(NULL, ext, LY_LLERR, rc, "Failed to create context for the schema-mount data.");
         goto cleanup;
     }
 
@@ -348,7 +353,7 @@
 schema_mount_get_ctx_shared(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
         const struct ly_ctx **ext_ctx)
 {
-    struct lyplg_ext_sm *sm_data = ext->data;
+    struct lyplg_ext_sm *sm_data = ext->compiled;
     LY_ERR ret = LY_SUCCESS, r;
     struct lyd_node *node = NULL;
     struct ly_ctx *new_ctx = NULL;
@@ -369,13 +374,14 @@
         }
     }
     if (!content_id) {
-        lyplg_ext_log(ext, LY_LLERR, LY_EVALID, NULL, "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data.");
+        lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EVALID,
+                "Missing \"content-id\" or \"module-set-id\" in ietf-yang-library data.");
         return LY_EVALID;
     }
 
     /* LOCK */
     if ((r = pthread_mutex_lock(&sm_data->shared->lock))) {
-        lyplg_ext_log(ext, LY_LLERR, LY_ESYS, NULL, "Mutex lock failed (%s).", strerror(r));
+        lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_ESYS, "Mutex lock failed (%s).", strerror(r));
         return LY_ESYS;
     }
 
@@ -389,7 +395,7 @@
     if (i < sm_data->shared->schema_count) {
         /* schema exists already */
         if (strcmp(content_id, sm_data->shared->schemas[i].content_id)) {
-            lyplg_ext_log(ext, LY_LLERR, LY_EVALID, "/ietf-yang-library:yang-library/content-id",
+            lyplg_ext_compile_log_path("/ietf-yang-library:yang-library/content-id", ext, LY_LLERR, LY_EVALID,
                     "Shared-schema yang-library content-id \"%s\" differs from \"%s\" used previously.",
                     content_id, sm_data->shared->schemas[i].content_id);
             ret = LY_EVALID;
@@ -406,7 +412,7 @@
         mem = realloc(sm_data->shared->schemas, (i + 1) * sizeof *sm_data->shared->schemas);
         if (!mem) {
             ly_ctx_destroy(new_ctx);
-            EXT_LOGERR_MEM_GOTO(ext, ret, cleanup);
+            EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup);
         }
         sm_data->shared->schemas = mem;
         ++sm_data->shared->schema_count;
@@ -440,7 +446,7 @@
 schema_mount_get_ctx_inline(struct lysc_ext_instance *ext, const struct lyd_node *ext_data, ly_bool config,
         const struct ly_ctx **ext_ctx)
 {
-    struct lyplg_ext_sm *sm_data = ext->data;
+    struct lyplg_ext_sm *sm_data = ext->compiled;
     LY_ERR r;
     struct ly_ctx *new_ctx = NULL;
     uint32_t i;
@@ -459,7 +465,7 @@
     mem = realloc(sm_data->inln.schemas, (i + 1) * sizeof *sm_data->inln.schemas);
     if (!mem) {
         ly_ctx_destroy(new_ctx);
-        EXT_LOGERR_MEM_RET(ext);
+        EXT_LOGERR_MEM_RET(NULL, ext);
     }
     sm_data->inln.schemas = mem;
     ++sm_data->inln.schema_count;
@@ -497,7 +503,7 @@
     LY_LIST_FOR(ext_data, iter) {
         if (iter->flags & LYD_NEW) {
             /* must be validated for the parent-reference prefix data to be stored */
-            lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "Provided ext data have not been validated.");
+            lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated.");
             ret = LY_EINVAL;
             goto cleanup;
         }
@@ -566,7 +572,7 @@
     /* get all parent references of this mount point */
     if (asprintf(&path, "/ietf-yang-schema-mount:schema-mounts/mount-point[module='%s'][label='%s']"
             "/shared-schema/parent-reference", ext->module->name, ext->argument) == -1) {
-        EXT_LOGERR_MEM_GOTO(ext, ret, cleanup);
+        EXT_LOGERR_MEM_GOTO(NULL, ext, ret, cleanup);
     }
     if ((ret = lyd_find_xpath(ext_data, path, set))) {
         goto cleanup;
@@ -603,7 +609,7 @@
 
     if (!ext_data) {
         /* we expect the same ext data as before and there must be some for data to be parsed */
-        lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "No ext data provided.");
+        lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "No ext data provided.");
         ret = LY_EINVAL;
         goto cleanup;
     }
@@ -625,7 +631,8 @@
         LYD_VALUE_GET(&term->value, xp_val);
         if ((ret = lyd_find_xpath4(ctx_node, ctx_node, lyxp_get_expr(xp_val->exp), xp_val->format, xp_val->prefix_data,
                 NULL, &par_set))) {
-            lyplg_ext_log(ext, LY_LLERR, ret, NULL, "Parent reference \"%s\" evaluation failed.", lyxp_get_expr(xp_val->exp));
+            lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Parent reference \"%s\" evaluation failed.",
+                    lyxp_get_expr(xp_val->exp));
             goto cleanup;
         }
 
@@ -759,7 +766,7 @@
 
     if (!sibling) {
         /* some data had to be parsed for this callback to be called */
-        EXT_LOGERR_INT_RET(ext);
+        EXT_LOGERR_INT_RET(NULL, ext);
     }
 
     /* get operational data with ietf-yang-library and ietf-yang-schema-mount data */
@@ -770,7 +777,7 @@
     LY_LIST_FOR(ext_data, iter) {
         if (iter->flags & LYD_NEW) {
             /* must be validated for the parent-reference prefix data to be stored */
-            lyplg_ext_log(ext, LY_LLERR, LY_EINVAL, NULL, "Provided ext data have not been validated.");
+            lyplg_ext_compile_log(NULL, ext, LY_LLERR, LY_EINVAL, "Provided ext data have not been validated.");
             ret = LY_EINVAL;
             goto cleanup;
         }
@@ -821,15 +828,15 @@
     LY_LIST_FOR(sibling, iter) {
         iter->flags |= LYD_EXT;
     }
-    lyd_insert_ext(orig_parent, sibling);
+    lyplg_ext_insert(orig_parent, sibling);
 
     if (ret) {
         /* log the error in the original context */
         err = ly_err_first(LYD_CTX(sibling));
         if (!err) {
-            lyplg_ext_log(ext, LY_LLERR, ret, NULL, "Unknown validation error (err code %d).", ret);
+            lyplg_ext_compile_log(NULL, ext, LY_LLERR, ret, "Unknown validation error (err code %d).", ret);
         } else {
-            lyplg_ext_log(ext, LY_LLERR, err->no, err->path, "%s", err->msg);
+            lyplg_ext_compile_log_path(err->path, ext, LY_LLERR, err->no, "%s", err->msg);
         }
         goto cleanup;
     }
@@ -845,7 +852,7 @@
         if ((ret = lyd_dup_single(lyd_parent(sibling), NULL, LYD_DUP_WITH_PARENTS | LYD_DUP_NO_META, &diff_parent))) {
             goto cleanup;
         }
-        if ((ret = lyd_insert_ext(diff_parent, ext_diff))) {
+        if ((ret = lyplg_ext_insert(diff_parent, ext_diff))) {
             goto cleanup;
         }
         ext_diff = NULL;
@@ -876,14 +883,14 @@
 }
 
 /**
- * @brief Schema mount free.
+ * @brief Schema mount compile free.
  *
- * Implementation of ::lyplg_ext_free_clb callback set as ::lyext_plugin::free.
+ * Implementation of ::lyplg_ext_compile_free_clb callback set as ::lyext_plugin::cfree.
  */
 static void
-schema_mount_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+schema_mount_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
 {
-    struct lyplg_ext_sm *sm_data = ext->data;
+    struct lyplg_ext_sm *sm_data = ext->compiled;
     uint32_t i;
 
     if (!sm_data) {
@@ -957,13 +964,15 @@
         .revision = "2019-01-14",
         .name = "mount-point",
 
-        .plugin.id = "libyang 2 - Schema Mount, version 1",
-        .plugin.compile = &schema_mount_compile,
+        .plugin.id = "ly2 schema mount v1",
+        .plugin.parse = schema_mount_parse,
+        .plugin.compile = schema_mount_compile,
         .plugin.sprinter = NULL,
-        .plugin.free = &schema_mount_free,
         .plugin.node = NULL,
-        .plugin.snode = &schema_mount_snode,
-        .plugin.validate = &schema_mount_validate
+        .plugin.snode = schema_mount_snode,
+        .plugin.validate = schema_mount_validate,
+        .plugin.pfree = NULL,
+        .plugin.cfree = schema_mount_cfree
     },
     {0} /* terminating zeroed item */
 };
diff --git a/src/plugins_exts/structure.c b/src/plugins_exts/structure.c
index 12c548c..3a0bf8a 100644
--- a/src/plugins_exts/structure.c
+++ b/src/plugins_exts/structure.c
@@ -1,7 +1,7 @@
 /**
  * @file structure.c
  * @author Michal Vasko <mvasko@cesnet.cz>
- * @brief libyang extension plugin - strcture (RFC 8791)
+ * @brief libyang extension plugin - structure (RFC 8791)
  *
  * Copyright (c) 2022 CESNET, z.s.p.o.
  *
@@ -20,16 +20,31 @@
 #include "libyang.h"
 #include "plugins_exts.h"
 
-struct lysc_ext_instance_structure {
-    struct lysc_must *musts;
+struct lysp_ext_instance_structure {
+    struct lysp_restr *musts;
     uint16_t flags;
     const char *dsc;
     const char *ref;
     struct lysp_tpdf *typedefs;
     struct lysp_node_grp *groupings;
+    struct lysp_node *child;
+};
+
+struct lysc_ext_instance_structure {
+    struct lysc_must *musts;
+    uint16_t flags;
+    const char *dsc;
+    const char *ref;
     struct lysc_node *child;
 };
 
+struct lysp_ext_instance_augment_structure {
+    uint16_t flags;
+    const char *dsc;
+    const char *ref;
+    struct lysp_node *child;
+};
+
 struct lysc_ext_instance_augment_structure {
     uint16_t flags;
     const char *dsc;
@@ -37,116 +52,206 @@
 };
 
 /**
+ * @brief Parse structure extension instances.
+ *
+ * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
+ */
+static LY_ERR
+structure_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
+{
+    LY_ERR rc;
+    LY_ARRAY_COUNT_TYPE u;
+    struct lysp_module *pmod;
+    struct lysp_ext_instance_structure *struct_pdata;
+
+    /* structure can appear only at the top level of a YANG module or submodule */
+    if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID,
+                "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name,
+                lyplg_ext_stmt2str(ext->parent_stmt));
+        return LY_EVALID;
+    }
+
+    pmod = ext->parent;
+
+    /* check for duplication */
+    LY_ARRAY_FOR(pmod->exts, u) {
+        if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
+            /* duplication of the same structure extension in a single module */
+            lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
+            return LY_EVALID;
+        }
+    }
+
+    /* allocate the storage */
+    struct_pdata = calloc(1, sizeof *struct_pdata);
+    if (!struct_pdata) {
+        goto emem;
+    }
+    ext->parsed = struct_pdata;
+    LY_ARRAY_CREATE_GOTO(lyplg_extp_cur_pmod(pctx)->mod->ctx, ext->substmts, 14, rc, emem);
+
+    /* parse substatements */
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[0].stmt = LY_STMT_MUST;
+    ext->substmts[0].storage = &struct_pdata->musts;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[1].stmt = LY_STMT_STATUS;
+    ext->substmts[1].storage = &struct_pdata->flags;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[2].stmt = LY_STMT_DESCRIPTION;
+    ext->substmts[2].storage = &struct_pdata->dsc;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[3].stmt = LY_STMT_REFERENCE;
+    ext->substmts[3].storage = &struct_pdata->ref;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[4].stmt = LY_STMT_TYPEDEF;
+    ext->substmts[4].storage = &struct_pdata->typedefs;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[5].stmt = LY_STMT_GROUPING;
+    ext->substmts[5].storage = &struct_pdata->groupings;
+
+    /* data-def-stmt */
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[6].stmt = LY_STMT_CONTAINER;
+    ext->substmts[6].storage = &struct_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[7].stmt = LY_STMT_LEAF;
+    ext->substmts[7].storage = &struct_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[8].stmt = LY_STMT_LEAF_LIST;
+    ext->substmts[8].storage = &struct_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[9].stmt = LY_STMT_LIST;
+    ext->substmts[9].storage = &struct_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[10].stmt = LY_STMT_CHOICE;
+    ext->substmts[10].storage = &struct_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[11].stmt = LY_STMT_ANYDATA;
+    ext->substmts[11].storage = &struct_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[12].stmt = LY_STMT_ANYXML;
+    ext->substmts[12].storage = &struct_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[13].stmt = LY_STMT_USES;
+    ext->substmts[13].storage = &struct_pdata->child;
+
+    rc = lyplg_ext_parse_extension_instance(pctx, ext);
+    return rc;
+
+emem:
+    lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
+    return LY_EMEM;
+}
+
+/**
  * @brief Compile structure extension instances.
  *
  * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
  */
 static LY_ERR
-structure_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
+structure_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
 {
     LY_ERR rc;
-    LY_ARRAY_COUNT_TYPE u;
     struct lysc_module *mod_c;
     const struct lysc_node *child;
-    struct lysc_ext_instance_structure *struct_data;
-    uint32_t prev_options = *lysc_ctx_get_options(cctx);
+    struct lysc_ext_instance_structure *struct_cdata;
+    uint32_t prev_options = *lyplg_ext_compile_get_options(cctx);
 
-    /* structure can appear only at the top level of a YANG module or submodule */
-    if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                "Extension %s must not be used as a non top-level statement in \"%s\" statement.",
-                p_ext->name, ly_stmt2str(c_ext->parent_stmt));
-        return LY_EVALID;
-    }
+    mod_c = ext->parent;
 
-    mod_c = (struct lysc_module *)c_ext->parent;
-
-    /* check identifier namespace */
-    LY_ARRAY_FOR(mod_c->exts, u) {
-        if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
-            /* duplication of the same structure extension in a single module */
-            lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
-            return LY_EVALID;
-        }
-    }
+    /* check identifier namespace with the compiled nodes */
     LY_LIST_FOR(mod_c->data, child) {
-        if (!strcmp(child->name, c_ext->argument)) {
+        if (!strcmp(child->name, ext->argument)) {
             /* identifier collision */
-            lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s collides "
-                    "with a %s with the same identifier.", p_ext->name, lys_nodetype2str(child->nodetype));
+            lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,  "Extension %s collides with a %s with the same identifier.",
+                    extp->name, lys_nodetype2str(child->nodetype));
             return LY_EVALID;
         }
     }
 
     /* allocate the storage */
-    struct_data = calloc(1, sizeof *struct_data);
-    if (!struct_data) {
+    struct_cdata = calloc(1, sizeof *struct_cdata);
+    if (!struct_cdata) {
         goto emem;
     }
-    c_ext->data = struct_data;
+    ext->compiled = struct_cdata;
 
     /* compile substatements */
-    LY_ARRAY_CREATE_GOTO(cctx->ctx, c_ext->substmts, 14, rc, emem);
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[0].stmt = LY_STMT_MUST;
-    c_ext->substmts[0].storage = &struct_data->musts;
+    LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 14, rc, emem);
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[0].stmt = LY_STMT_MUST;
+    ext->substmts[0].storage = &struct_cdata->musts;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[1].stmt = LY_STMT_STATUS;
-    c_ext->substmts[1].storage = &struct_data->flags;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[1].stmt = LY_STMT_STATUS;
+    ext->substmts[1].storage = &struct_cdata->flags;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[2].stmt = LY_STMT_DESCRIPTION;
-    c_ext->substmts[2].storage = &struct_data->dsc;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[2].stmt = LY_STMT_DESCRIPTION;
+    ext->substmts[2].storage = &struct_cdata->dsc;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[3].stmt = LY_STMT_REFERENCE;
-    c_ext->substmts[3].storage = &struct_data->ref;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[3].stmt = LY_STMT_REFERENCE;
+    ext->substmts[3].storage = &struct_cdata->ref;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[4].stmt = LY_STMT_TYPEDEF;
-    c_ext->substmts[4].storage = &struct_data->typedefs;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[4].stmt = LY_STMT_TYPEDEF;
+    ext->substmts[4].storage = NULL;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[5].stmt = LY_STMT_GROUPING;
-    c_ext->substmts[5].storage = &struct_data->groupings;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[5].stmt = LY_STMT_GROUPING;
+    ext->substmts[5].storage = NULL;
 
     /* data-def-stmt */
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[6].stmt = LY_STMT_CONTAINER;
-    c_ext->substmts[6].storage = &struct_data->child;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[6].stmt = LY_STMT_CONTAINER;
+    ext->substmts[6].storage = &struct_cdata->child;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[7].stmt = LY_STMT_LEAF;
-    c_ext->substmts[7].storage = &struct_data->child;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[7].stmt = LY_STMT_LEAF;
+    ext->substmts[7].storage = &struct_cdata->child;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[8].stmt = LY_STMT_LEAF_LIST;
-    c_ext->substmts[8].storage = &struct_data->child;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[8].stmt = LY_STMT_LEAF_LIST;
+    ext->substmts[8].storage = &struct_cdata->child;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[9].stmt = LY_STMT_LIST;
-    c_ext->substmts[9].storage = &struct_data->child;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[9].stmt = LY_STMT_LIST;
+    ext->substmts[9].storage = &struct_cdata->child;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[10].stmt = LY_STMT_CHOICE;
-    c_ext->substmts[10].storage = &struct_data->child;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[10].stmt = LY_STMT_CHOICE;
+    ext->substmts[10].storage = &struct_cdata->child;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[11].stmt = LY_STMT_ANYDATA;
-    c_ext->substmts[11].storage = &struct_data->child;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[11].stmt = LY_STMT_ANYDATA;
+    ext->substmts[11].storage = &struct_cdata->child;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[12].stmt = LY_STMT_ANYXML;
-    c_ext->substmts[12].storage = &struct_data->child;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[12].stmt = LY_STMT_ANYXML;
+    ext->substmts[12].storage = &struct_cdata->child;
 
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[13].stmt = LY_STMT_USES;
-    c_ext->substmts[13].storage = &struct_data->child;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[13].stmt = LY_STMT_USES;
+    ext->substmts[13].storage = &struct_cdata->child;
 
-    *lysc_ctx_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
-    rc = lys_compile_extension_instance(cctx, p_ext, c_ext);
-    *lysc_ctx_get_options(cctx) = prev_options;
+    *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
+    rc = lyplg_ext_compile_extension_instance(cctx, extp, ext);
+    *lyplg_ext_compile_get_options(cctx) = prev_options;
     if (rc) {
         return rc;
     }
@@ -154,134 +259,7 @@
     return LY_SUCCESS;
 
 emem:
-    lyplg_ext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
-    return LY_EMEM;
-}
-
-/**
- * @brief Compile augment-structure extension instances.
- *
- * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
- */
-static LY_ERR
-structure_aug_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
-{
-    LY_ERR rc;
-    struct lysp_stmt *stmt;
-    struct lysc_node *aug_target;
-    struct lysc_ext_instance *target_ext;
-    struct lysc_ext_instance_structure *target_data;
-    struct lysc_ext_instance_augment_structure *aug_data;
-    uint32_t prev_options = *lysc_ctx_get_options(cctx), i;
-
-    /* augment-structure can appear only at the top level of a YANG module or submodule */
-    if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                "Extension %s must not be used as a non top-level statement in \"%s\" statement.",
-                p_ext->name, ly_stmt2str(c_ext->parent_stmt));
-        return LY_EVALID;
-    }
-
-    /* augment-structure must define some data-def-stmt */
-    LY_LIST_FOR(p_ext->child, stmt) {
-        if (LY_STMT_IS_DATA_NODE(stmt->kw)) {
-            break;
-        }
-    }
-    if (!stmt) {
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                "Extension %s does not define any data-def-stmt statements.", p_ext->name);
-        return LY_EVALID;
-    }
-
-    /* find the target struct ext instance */
-    if ((rc = lys_compile_extension_instance_find_augment_target(cctx, p_ext->argument, &target_ext, &aug_target))) {
-        return rc;
-    }
-
-    /* check target_ext */
-    if (strcmp(target_ext->def->name, "structure") || strcmp(target_ext->def->module->name, "ietf-yang-structure-ext")) {
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
-                "Extension %s can only target extension instances of \"ietf-yang-structure-ext:structure\".", p_ext->name);
-        return LY_EVALID;
-    }
-    target_data = target_ext->data;
-
-    /* allocate the storage */
-    aug_data = calloc(1, sizeof *aug_data);
-    if (!aug_data) {
-        goto emem;
-    }
-    c_ext->data = aug_data;
-
-    /* compile substatements */
-    LY_ARRAY_CREATE_GOTO(cctx->ctx, c_ext->substmts, 12, rc, emem);
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[0].stmt = LY_STMT_STATUS;
-    c_ext->substmts[0].storage = &aug_data->flags;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[1].stmt = LY_STMT_DESCRIPTION;
-    c_ext->substmts[1].storage = &aug_data->dsc;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[2].stmt = LY_STMT_REFERENCE;
-    c_ext->substmts[2].storage = &aug_data->ref;
-
-    /* data-def-stmt */
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[3].stmt = LY_STMT_CONTAINER;
-    c_ext->substmts[3].storage = &target_data->child;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[4].stmt = LY_STMT_LEAF;
-    c_ext->substmts[4].storage = &target_data->child;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[5].stmt = LY_STMT_LEAF_LIST;
-    c_ext->substmts[5].storage = &target_data->child;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[6].stmt = LY_STMT_LIST;
-    c_ext->substmts[6].storage = &target_data->child;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[7].stmt = LY_STMT_CHOICE;
-    c_ext->substmts[7].storage = &target_data->child;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[8].stmt = LY_STMT_ANYDATA;
-    c_ext->substmts[8].storage = &target_data->child;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[9].stmt = LY_STMT_ANYXML;
-    c_ext->substmts[9].storage = &target_data->child;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[10].stmt = LY_STMT_USES;
-    c_ext->substmts[10].storage = &target_data->child;
-
-    /* case */
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[11].stmt = LY_STMT_CASE;
-    c_ext->substmts[11].storage = &target_data->child;
-
-    *lysc_ctx_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
-    rc = lys_compile_extension_instance_augment(cctx, p_ext, c_ext, aug_target);
-    *lysc_ctx_get_options(cctx) = prev_options;
-    if (rc) {
-        return rc;
-    }
-
-    /* data-def-statements are now part of the target extension (do not print nor free them) */
-    for (i = 0; i < 9; ++i) {
-        LY_ARRAY_DECREMENT(c_ext->substmts);
-    }
-
-    return LY_SUCCESS;
-
-emem:
-    lyplg_ext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
+    lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
     return LY_EMEM;
 }
 
@@ -291,22 +269,239 @@
  * Implementation of ::lyplg_ext_schema_printer_clb set as ::lyext_plugin::sprinter
  */
 static LY_ERR
-structure_schema_printer(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
+structure_sprinter(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
 {
-    lysc_print_extension_instance(ctx, ext, flag);
+    lyplg_ext_print_extension_instance(ctx, ext, flag);
     return LY_SUCCESS;
 }
 
 /**
- * @brief Free structure extension instances' data.
+ * @brief Free parsed structure extension instance data.
  *
- * Implementation of ::lyplg_clb_free_clb callback set as lyext_plugin::free.
+ * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree.
  */
 static void
-structure_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+structure_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
 {
-    lyplg_ext_instance_substatements_free(ctx, ext->substmts);
-    free(ext->data);
+    lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
+    free(ext->parsed);
+}
+
+/**
+ * @brief Free compiled structure extension instance data.
+ *
+ * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree.
+ */
+static void
+structure_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+{
+    lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
+    free(ext->compiled);
+}
+
+/**
+ * @brief Parse augment-structure extension instances.
+ *
+ * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
+ */
+static LY_ERR
+structure_aug_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
+{
+    LY_ERR rc;
+    struct lysp_stmt *stmt;
+    struct lysp_ext_instance_augment_structure *aug_pdata;
+
+    /* augment-structure can appear only at the top level of a YANG module or submodule */
+    if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID,
+                "Extension %s must not be used as a non top-level statement in \"%s\" statement.", ext->name,
+                lyplg_ext_stmt2str(ext->parent_stmt));
+        return LY_EVALID;
+    }
+
+    /* augment-structure must define some data-def-stmt */
+    LY_LIST_FOR(ext->child, stmt) {
+        if (stmt->kw & LY_STMT_DATA_NODE_MASK) {
+            break;
+        }
+    }
+    if (!stmt) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s does not define any data-def-stmt statements.",
+                ext->name);
+        return LY_EVALID;
+    }
+
+    /* allocate the storage */
+    aug_pdata = calloc(1, sizeof *aug_pdata);
+    if (!aug_pdata) {
+        goto emem;
+    }
+    ext->parsed = aug_pdata;
+    LY_ARRAY_CREATE_GOTO(lyplg_extp_cur_pmod(pctx)->mod->ctx, ext->substmts, 12, rc, emem);
+
+    /* parse substatements */
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[0].stmt = LY_STMT_STATUS;
+    ext->substmts[0].storage = &aug_pdata->flags;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[1].stmt = LY_STMT_DESCRIPTION;
+    ext->substmts[1].storage = &aug_pdata->dsc;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[2].stmt = LY_STMT_REFERENCE;
+    ext->substmts[2].storage = &aug_pdata->ref;
+
+    /* data-def-stmt */
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[3].stmt = LY_STMT_CONTAINER;
+    ext->substmts[3].storage = &aug_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[4].stmt = LY_STMT_LEAF;
+    ext->substmts[4].storage = &aug_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[5].stmt = LY_STMT_LEAF_LIST;
+    ext->substmts[5].storage = &aug_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[6].stmt = LY_STMT_LIST;
+    ext->substmts[6].storage = &aug_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[7].stmt = LY_STMT_CHOICE;
+    ext->substmts[7].storage = &aug_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[8].stmt = LY_STMT_ANYDATA;
+    ext->substmts[8].storage = &aug_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[9].stmt = LY_STMT_ANYXML;
+    ext->substmts[9].storage = &aug_pdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[10].stmt = LY_STMT_USES;
+    ext->substmts[10].storage = &aug_pdata->child;
+
+    /* case */
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[11].stmt = LY_STMT_CASE;
+    ext->substmts[11].storage = &aug_pdata->child;
+
+    rc = lyplg_ext_parse_extension_instance(pctx, ext);
+    return rc;
+
+emem:
+    lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
+    return LY_EMEM;
+}
+
+/**
+ * @brief Compile augment-structure extension instances.
+ *
+ * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
+ */
+static LY_ERR
+structure_aug_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
+{
+    LY_ERR rc;
+    struct lysc_node *aug_target;
+    struct lysc_ext_instance *target_ext;
+    struct lysc_ext_instance_structure *target_cdata;
+    struct lysc_ext_instance_augment_structure *aug_cdata;
+    uint32_t prev_options = *lyplg_ext_compile_get_options(cctx), i;
+
+    /* find the target struct ext instance */
+    if ((rc = lys_compile_extension_instance_find_augment_target(cctx, extp->argument, &target_ext, &aug_target))) {
+        return rc;
+    }
+
+    /* check target_ext */
+    if (strcmp(target_ext->def->name, "structure") || strcmp(target_ext->def->module->name, "ietf-yang-structure-ext")) {
+        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
+                "Extension %s can only target extension instances of \"ietf-yang-structure-ext:structure\".", extp->name);
+        return LY_EVALID;
+    }
+    target_cdata = target_ext->compiled;
+
+    /* allocate the storage */
+    aug_cdata = calloc(1, sizeof *aug_cdata);
+    if (!aug_cdata) {
+        goto emem;
+    }
+    ext->compiled = aug_cdata;
+
+    /* compile substatements */
+    LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 12, rc, emem);
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[0].stmt = LY_STMT_STATUS;
+    ext->substmts[0].storage = &aug_cdata->flags;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[1].stmt = LY_STMT_DESCRIPTION;
+    ext->substmts[1].storage = &aug_cdata->dsc;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[2].stmt = LY_STMT_REFERENCE;
+    ext->substmts[2].storage = &aug_cdata->ref;
+
+    /* data-def-stmt */
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[3].stmt = LY_STMT_CONTAINER;
+    ext->substmts[3].storage = &target_cdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[4].stmt = LY_STMT_LEAF;
+    ext->substmts[4].storage = &target_cdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[5].stmt = LY_STMT_LEAF_LIST;
+    ext->substmts[5].storage = &target_cdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[6].stmt = LY_STMT_LIST;
+    ext->substmts[6].storage = &target_cdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[7].stmt = LY_STMT_CHOICE;
+    ext->substmts[7].storage = &target_cdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[8].stmt = LY_STMT_ANYDATA;
+    ext->substmts[8].storage = &target_cdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[9].stmt = LY_STMT_ANYXML;
+    ext->substmts[9].storage = &target_cdata->child;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[10].stmt = LY_STMT_USES;
+    ext->substmts[10].storage = &target_cdata->child;
+
+    /* case */
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[11].stmt = LY_STMT_CASE;
+    ext->substmts[11].storage = &target_cdata->child;
+
+    *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
+    rc = lyplg_ext_compile_extension_instance_augment(cctx, extp, ext, aug_target);
+    *lyplg_ext_compile_get_options(cctx) = prev_options;
+    if (rc) {
+        return rc;
+    }
+
+    /* data-def-statements are now part of the target extension (do not print nor free them) */
+    for (i = 0; i < 9; ++i) {
+        LY_ARRAY_DECREMENT(ext->substmts);
+    }
+
+    return LY_SUCCESS;
+
+emem:
+    lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
+    return LY_EMEM;
 }
 
 /**
@@ -322,26 +517,30 @@
         .revision = "2020-06-17",
         .name = "structure",
 
-        .plugin.id = "libyang 2 - structure, version 1",
+        .plugin.id = "ly2 structure v1",
+        .plugin.parse = structure_parse,
         .plugin.compile = structure_compile,
-        .plugin.sprinter = structure_schema_printer,
-        .plugin.free = structure_free,
+        .plugin.sprinter = structure_sprinter,
         .plugin.node = NULL,
         .plugin.snode = NULL,
-        .plugin.validate = NULL
+        .plugin.validate = NULL,
+        .plugin.pfree = structure_pfree,
+        .plugin.cfree = structure_cfree
     },
     {
         .module = "ietf-yang-structure-ext",
         .revision = "2020-06-17",
         .name = "augment-structure",
 
-        .plugin.id = "libyang 2 - structure, version 1",
+        .plugin.id = "ly2 structure v1",
+        .plugin.parse = structure_aug_parse,
         .plugin.compile = structure_aug_compile,
-        .plugin.sprinter = structure_schema_printer,
-        .plugin.free = structure_free,
+        .plugin.sprinter = structure_sprinter,
         .plugin.node = NULL,
         .plugin.snode = NULL,
-        .plugin.validate = NULL
+        .plugin.validate = NULL,
+        .plugin.pfree = NULL,
+        .plugin.cfree = structure_cfree
     },
     {0}     /* terminating zeroed record */
 };
diff --git a/src/plugins_exts/yangdata.c b/src/plugins_exts/yangdata.c
index 939709b..32229d7 100644
--- a/src/plugins_exts/yangdata.c
+++ b/src/plugins_exts/yangdata.c
@@ -1,9 +1,10 @@
 /**
  * @file yangdata.c
  * @author Radek Krejci <rkrejci@cesnet.cz>
+ * @author Michal Vasko <mvasko@cesnet.cz>
  * @brief libyang extension plugin - yang-data (RFC 8040)
  *
- * Copyright (c) 2021 CESNET, z.s.p.o.
+ * Copyright (c) 2021 - 2022 CESNET, z.s.p.o.
  *
  * This source code is licensed under BSD 3-Clause License (the "License").
  * You may not use this file except in compliance with the License.
@@ -19,15 +20,61 @@
 #include "libyang.h"
 #include "plugins_exts.h"
 
+static void yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext);
+
 /**
- * @brief Free yang-data extension instances' data.
+ * @brief Parse yang-data extension instances.
  *
- * Implementation of ::lyplg_clb_free_clb callback set as lyext_plugin::free.
+ * Implementation of ::lyplg_ext_parse_clb callback set as lyext_plugin::parse.
  */
-static void
-yangdata_free(struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+static LY_ERR
+yangdata_parse(struct lysp_ctx *pctx, struct lysp_ext_instance *ext)
 {
-    lyplg_ext_instance_substatements_free(ctx, ext->substmts);
+    LY_ERR ret;
+    LY_ARRAY_COUNT_TYPE u;
+    struct lysp_module *pmod;
+
+    /* yang-data can appear only at the top level of a YANG module or submodule */
+    if ((ext->parent_stmt != LY_STMT_MODULE) && (ext->parent_stmt != LY_STMT_SUBMODULE)) {
+        lyplg_ext_parse_log(pctx, ext, LY_LLWRN, 0, "Extension %s is ignored since it appears as a non top-level statement "
+                "in \"%s\" statement.", ext->name, lyplg_ext_stmt2str(ext->parent_stmt));
+        return LY_ENOT;
+    }
+
+    pmod = ext->parent;
+
+    /* check for duplication */
+    LY_ARRAY_FOR(pmod->exts, u) {
+        if ((&pmod->exts[u] != ext) && (pmod->exts[u].name == ext->name) && !strcmp(pmod->exts[u].argument, ext->argument)) {
+            /* duplication of the same yang-data extension in a single module */
+            lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EVALID, "Extension %s is instantiated multiple times.", ext->name);
+            return LY_EVALID;
+        }
+    }
+
+    /* parse yang-data substatements */
+    LY_ARRAY_CREATE_GOTO(lyplg_extp_cur_pmod(pctx)->mod->ctx, ext->substmts, 3, ret, emem);
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[0].stmt = LY_STMT_CONTAINER;
+    ext->substmts[0].storage = &ext->parsed;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[1].stmt = LY_STMT_CHOICE;
+    ext->substmts[1].storage = &ext->parsed;
+
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[2].stmt = LY_STMT_USES;
+    ext->substmts[2].storage = &ext->parsed;
+
+    if ((ret = lyplg_ext_parse_extension_instance(pctx, ext))) {
+        return ret;
+    }
+
+    return LY_SUCCESS;
+
+emem:
+    lyplg_ext_parse_log(pctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
+    return LY_EMEM;
 }
 
 /**
@@ -36,109 +83,84 @@
  * Implementation of ::lyplg_ext_compile_clb callback set as lyext_plugin::compile.
  */
 static LY_ERR
-yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *p_ext, struct lysc_ext_instance *c_ext)
+yangdata_compile(struct lysc_ctx *cctx, const struct lysp_ext_instance *extp, struct lysc_ext_instance *ext)
 {
     LY_ERR ret;
-    LY_ARRAY_COUNT_TYPE u;
-    struct lysc_module *mod_c;
     const struct lysc_node *child;
     ly_bool valid = 1;
-    uint32_t prev_options = *lysc_ctx_get_options(cctx);
+    uint32_t prev_options = *lyplg_ext_compile_get_options(cctx);
 
-    /* yang-data can appear only at the top level of a YANG module or submodule */
-    if ((c_ext->parent_stmt != LY_STMT_MODULE) && (c_ext->parent_stmt != LY_STMT_SUBMODULE)) {
-        lyplg_ext_log(c_ext, LY_LLWRN, 0, lysc_ctx_get_path(cctx),
-                "Extension %s is ignored since it appears as a non top-level statement in \"%s\" statement.",
-                p_ext->name, ly_stmt2str(c_ext->parent_stmt));
-        return LY_ENOT;
-    }
+    /* compile yangg-data substatements */
+    LY_ARRAY_CREATE_GOTO(cctx->ctx, ext->substmts, 3, ret, emem);
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[0].stmt = LY_STMT_CONTAINER;
+    ext->substmts[0].storage = &ext->compiled;
 
-    mod_c = (struct lysc_module *)c_ext->parent;
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[1].stmt = LY_STMT_CHOICE;
+    ext->substmts[1].storage = &ext->compiled;
 
-    /* check for duplication */
-    LY_ARRAY_FOR(mod_c->exts, u) {
-        if ((&mod_c->exts[u] != c_ext) && (mod_c->exts[u].def == c_ext->def) && !strcmp(mod_c->exts[u].argument, c_ext->argument)) {
-            /* duplication of the same yang-data extension in a single module */
-            lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx), "Extension %s is instantiated multiple times.", p_ext->name);
-            return LY_EVALID;
-        }
-    }
+    LY_ARRAY_INCREMENT(ext->substmts);
+    ext->substmts[2].stmt = LY_STMT_USES;
+    ext->substmts[2].storage = &ext->compiled;
 
-    /* compile annotation substatements
-     * To let the compilation accept different statements possibly leading to the container top-level node, there are 3
-     * allowed substatements pointing to a single storage. But when compiled, the substaments list is compressed just to
-     * a single item providing the schema tree. */
-    LY_ARRAY_CREATE_GOTO(cctx->ctx, c_ext->substmts, 3, ret, emem);
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[0].stmt = LY_STMT_CONTAINER;
-    c_ext->substmts[0].storage = &c_ext->data;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[1].stmt = LY_STMT_CHOICE;
-    c_ext->substmts[1].storage = &c_ext->data;
-
-    LY_ARRAY_INCREMENT(c_ext->substmts);
-    c_ext->substmts[2].stmt = LY_STMT_USES;
-    c_ext->substmts[2].storage = &c_ext->data;
-
-    *lysc_ctx_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
-    ret = lys_compile_extension_instance(cctx, p_ext, c_ext);
-    *lysc_ctx_get_options(cctx) = prev_options;
-    LY_ARRAY_DECREMENT(c_ext->substmts);
-    LY_ARRAY_DECREMENT(c_ext->substmts);
+    *lyplg_ext_compile_get_options(cctx) |= LYS_COMPILE_NO_CONFIG | LYS_COMPILE_NO_DISABLED;
+    ret = lyplg_ext_compile_extension_instance(cctx, extp, ext);
+    *lyplg_ext_compile_get_options(cctx) = prev_options;
     if (ret) {
         return ret;
     }
 
     /* check that we have really just a single container data definition in the top */
-    child = *(struct lysc_node **)c_ext->substmts[0].storage;
+    child = ext->compiled;
     if (!child) {
         valid = 0;
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
                 "Extension %s is instantiated without any top level data node, but exactly one container data node is expected.",
-                p_ext->name);
+                extp->name);
     } else if (child->next) {
         valid = 0;
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
                 "Extension %s is instantiated with multiple top level data nodes, but only a single container data node is allowed.",
-                p_ext->name);
+                extp->name);
     } else if (child->nodetype == LYS_CHOICE) {
         /* all the choice's case are expected to result to a single container node */
+        struct lysc_module *mod_c = ext->parent;
         const struct lysc_node *snode = NULL;
 
         while ((snode = lys_getnext(snode, child, mod_c, 0))) {
             if (snode->next) {
                 valid = 0;
-                lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+                lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
                         "Extension %s is instantiated with multiple top level data nodes (inside a single choice's case), "
-                        "but only a single container data node is allowed.", p_ext->name);
+                        "but only a single container data node is allowed.", extp->name);
                 break;
             } else if (snode->nodetype != LYS_CONTAINER) {
                 valid = 0;
-                lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+                lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
                         "Extension %s is instantiated with %s top level data node (inside a choice), "
-                        "but only a single container data node is allowed.", p_ext->name, lys_nodetype2str(snode->nodetype));
+                        "but only a single container data node is allowed.", extp->name, lys_nodetype2str(snode->nodetype));
                 break;
             }
         }
     } else if (child->nodetype != LYS_CONTAINER) {
         /* via uses */
         valid = 0;
-        lyplg_ext_log(c_ext, LY_LLERR, LY_EVALID, lysc_ctx_get_path(cctx),
+        lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EVALID,
                 "Extension %s is instantiated with %s top level data node, but only a single container data node is allowed.",
-                p_ext->name, lys_nodetype2str(child->nodetype));
+                extp->name, lys_nodetype2str(child->nodetype));
     }
 
     if (!valid) {
-        yangdata_free(lysc_ctx_get_ctx(cctx), c_ext);
-        c_ext->data = c_ext->substmts = NULL;
+        yangdata_cfree(lyplg_ext_compile_get_ctx(cctx), ext);
+        ext->compiled = ext->substmts = NULL;
         return LY_EVALID;
     }
 
     return LY_SUCCESS;
 
 emem:
-    lyplg_ext_log(c_ext, LY_LLERR, LY_EMEM, lysc_ctx_get_path(cctx), "Memory allocation failed (%s()).", __func__);
+    lyplg_ext_compile_log(cctx, ext, LY_LLERR, LY_EMEM, "Memory allocation failed (%s()).", __func__);
     return LY_EMEM;
 }
 
@@ -150,11 +172,33 @@
 static LY_ERR
 yangdata_schema_printer(struct lyspr_ctx *ctx, struct lysc_ext_instance *ext, ly_bool *flag)
 {
-    lysc_print_extension_instance(ctx, ext, flag);
+    lyplg_ext_print_extension_instance(ctx, ext, flag);
     return LY_SUCCESS;
 }
 
 /**
+ * @brief Free parsed yang-data extension instance data.
+ *
+ * Implementation of ::lyplg_clb_parse_free_clb callback set as lyext_plugin::pfree.
+ */
+static void
+yangdata_pfree(const struct ly_ctx *ctx, struct lysp_ext_instance *ext)
+{
+    lyplg_ext_pfree_instance_substatements(ctx, ext->substmts);
+}
+
+/**
+ * @brief Free compiled yang-data extension instance data.
+ *
+ * Implementation of ::lyplg_clb_compile_free_clb callback set as lyext_plugin::cfree.
+ */
+static void
+yangdata_cfree(const struct ly_ctx *ctx, struct lysc_ext_instance *ext)
+{
+    lyplg_ext_cfree_instance_substatements(ctx, ext->substmts);
+}
+
+/**
  * @brief Plugin descriptions for the yang-data extension
  *
  * Note that external plugins are supposed to use:
@@ -167,13 +211,15 @@
         .revision = "2017-01-26",
         .name = "yang-data",
 
-        .plugin.id = "libyang 2 - yang-data, version 1",
+        .plugin.id = "ly2 yang-data v1",
+        .plugin.parse = yangdata_parse,
         .plugin.compile = yangdata_compile,
         .plugin.sprinter = yangdata_schema_printer,
-        .plugin.free = yangdata_free,
         .plugin.node = NULL,
         .plugin.snode = NULL,
-        .plugin.validate = NULL
+        .plugin.validate = NULL,
+        .plugin.pfree = yangdata_pfree,
+        .plugin.cfree = yangdata_cfree
     },
     {0}     /* terminating zeroed record */
 };