libyang REFACTOR use lysp_module instead of lys_module as mod_def

Because imports from a specific parsed module
or submodule must be used, not imports from the
module and all its submodules.

Also, pmod (prefix module) is now the term
used for the module where import prefixes are
searched in and cur_mod (current module) is
the module of unprefixed identifiers.
diff --git a/src/parser_yang.c b/src/parser_yang.c
index 15ae77c..4e22218 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -41,8 +41,8 @@
  * @param[in] LEN length of the string in WORD to store.
  */
 #define INSERT_WORD_RET(CTX, BUF, TARGET, WORD, LEN) \
-    if (BUF) {LY_CHECK_RET(lydict_insert_zc((CTX)->ctx, WORD, &(TARGET)));}\
-    else {LY_CHECK_RET(lydict_insert((CTX)->ctx, LEN ? WORD : "", LEN, &(TARGET)));}
+    if (BUF) {LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(CTX), WORD, &(TARGET)));}\
+    else {LY_CHECK_RET(lydict_insert(PARSER_CTX(CTX), LEN ? WORD : "", LEN, &(TARGET)));}
 
 /**
  * @brief Read from the IN structure COUNT items. Also updates the indent value in yang parser context
@@ -174,7 +174,7 @@
 
     if (word_b && *word_b) {
         /* add another character into buffer */
-        if (buf_add_char(ctx->ctx, in, len, word_b, buf_len, word_len)) {
+        if (buf_add_char(PARSER_CTX(ctx), in, len, word_b, buf_len, word_len)) {
             return LY_EMEM;
         }
 
@@ -184,13 +184,13 @@
         /* first time we need a buffer, copy everything read up to now */
         if (*word_len) {
             *word_b = malloc(*word_len);
-            LY_CHECK_ERR_RET(!*word_b, LOGMEM(ctx->ctx), LY_EMEM);
+            LY_CHECK_ERR_RET(!*word_b, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
             *buf_len = *word_len;
             memcpy(*word_b, *word_p, *word_len);
         }
 
         /* add this new character into buffer */
-        if (buf_add_char(ctx->ctx, in, len, word_b, buf_len, word_len)) {
+        if (buf_add_char(PARSER_CTX(ctx), in, len, word_b, buf_len, word_len)) {
             return LY_EMEM;
         }
 
@@ -253,7 +253,7 @@
             }
             break;
         default:
-            LOGINT_RET(ctx->ctx);
+            LOGINT_RET(PARSER_CTX(ctx));
         }
 
         if (in->current[0] == '\n') {
@@ -612,7 +612,7 @@
     /* terminating NULL byte for buf */
     if (*word_b) {
         (*word_b) = ly_realloc(*word_b, (*word_len) + 1);
-        LY_CHECK_ERR_RET(!(*word_b), LOGMEM(ctx->ctx), LY_EMEM);
+        LY_CHECK_ERR_RET(!(*word_b), LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
         (*word_b)[*word_len] = '\0';
         *word_p = *word_b;
     }
@@ -786,7 +786,7 @@
         par_child->next = stmt;
     }
 
-    LY_CHECK_RET(lydict_insert(ctx->ctx, word, word_len, &stmt->stmt));
+    LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->stmt));
     stmt->kw = kw;
 
     /* get optional argument */
@@ -794,9 +794,9 @@
 
     if (word) {
         if (buf) {
-            LY_CHECK_RET(lydict_insert_zc(ctx->ctx, word, &stmt->arg));
+            LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), word, &stmt->arg));
         } else {
-            LY_CHECK_RET(lydict_insert(ctx->ctx, word, word_len, &stmt->arg));
+            LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), word, word_len, &stmt->arg));
         }
     }
 
@@ -829,10 +829,10 @@
     struct lysp_ext_instance *e;
     enum ly_stmt kw;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *exts, e, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *exts, e, LY_EMEM);
 
     /* store name and insubstmt info */
-    LY_CHECK_RET(lydict_insert(ctx->ctx, ext_name, ext_name_len, &e->name));
+    LY_CHECK_RET(lydict_insert(PARSER_CTX(ctx), ext_name, ext_name_len, &e->name));
     e->insubstmt = insubstmt;
     e->insubstmt_index = insubstmt_index;
 
@@ -971,9 +971,9 @@
 
     /* get value, it must match the main module */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    if (ly_strncmp(ctx->main_mod->name, word, word_len)) {
+    if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
         LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Submodule \"belongs-to\" value \"%.*s\" does not match its module name \"%s\".",
-                (int)word_len, word, ctx->main_mod->name);
+                (int)word_len, word, ctx->parsed_mod->mod->name);
         free(buf);
         return LY_EVALID;
     }
@@ -1070,7 +1070,7 @@
     enum ly_stmt kw;
     struct lysp_include *inc;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *includes, inc, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *includes, inc, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -1079,7 +1079,7 @@
 
     /* submodules share the namespace with the module names, so there must not be
      * a module of the same name in the context, no need for revision matching */
-    if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(ctx->ctx, inc->name)) {
+    if (!strcmp(module_name, inc->name) || ly_ctx_get_module_latest(PARSER_CTX(ctx), inc->name)) {
         LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", inc->name);
         return LY_EVALID;
     }
@@ -1127,7 +1127,7 @@
     enum ly_stmt kw;
     struct lysp_import *imp;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *imports, imp, LY_EVALID);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *imports, imp, LY_EVALID);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -1184,7 +1184,7 @@
     enum ly_stmt kw;
     struct lysp_revision *rev;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *revs, rev, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *revs, rev, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
@@ -1240,7 +1240,7 @@
     enum ly_stmt kw;
 
     /* allocate new pointer */
-    LY_ARRAY_NEW_RET(ctx->ctx, *texts, item, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *texts, item, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
@@ -1282,13 +1282,13 @@
     enum ly_stmt kw;
 
     /* allocate new pointer */
-    LY_ARRAY_NEW_RET(ctx->ctx, *qnames, item, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *qnames, item, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, arg, NULL, &word, &buf, &word_len));
 
     INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
-    item->mod = ctx->main_mod;
+    item->mod = ctx->parsed_mod;
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
         case LY_STMT_EXTENSION_INSTANCE:
@@ -1425,7 +1425,7 @@
 
     CHECK_NONEMPTY(ctx, word_len, ly_stmt2str(restr_kw));
     INSERT_WORD_RET(ctx, buf, restr->arg.str, word, word_len);
-    restr->arg.mod = ctx->main_mod;
+    restr->arg.mod = ctx->parsed_mod;
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
         case LY_STMT_DESCRIPTION:
@@ -1466,7 +1466,7 @@
 {
     struct lysp_restr *restr;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *restrs, restr, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *restrs, restr, LY_EMEM);
     return parse_restr(ctx, in, restr_kw, restr);
 }
 
@@ -1546,7 +1546,7 @@
     }
 
     when = calloc(1, sizeof *when);
-    LY_CHECK_ERR_RET(!when, LOGMEM(ctx->ctx), LY_EMEM);
+    LY_CHECK_ERR_RET(!when, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
     *when_p = when;
 
     /* get value */
@@ -1592,7 +1592,7 @@
     struct lysp_node_anydata *any;
 
     /* create new structure and insert into siblings */
-    LY_LIST_NEW_RET(ctx->ctx, siblings, any, next, LY_EMEM);
+    LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, any, next, LY_EMEM);
 
     any->nodetype = kw == LY_STMT_ANYDATA ? LYS_ANYDATA : LYS_ANYXML;
     any->parent = parent;
@@ -1743,7 +1743,7 @@
     enum ly_stmt kw;
     struct lysp_type_enum *enm;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *enums, enm, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *enums, enm, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, enum_kw == LY_STMT_ENUM ? Y_STR_ARG : Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -1940,13 +1940,13 @@
 
     /* replace the value in the dictionary */
     buf = malloc(strlen(*pat) + 1);
-    LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
+    LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
     strcpy(buf, *pat);
-    lydict_remove(ctx->ctx, *pat);
+    lydict_remove(PARSER_CTX(ctx), *pat);
 
     assert(buf[0] == 0x06);
     buf[0] = 0x15;
-    LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, pat));
+    LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -1979,7 +1979,7 @@
     enum ly_stmt kw;
     struct lysp_restr *restr;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *patterns, restr, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *patterns, restr, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
@@ -1991,12 +1991,12 @@
     } else {
         buf = malloc(word_len + 2);
     }
-    LY_CHECK_ERR_RET(!buf, LOGMEM(ctx->ctx), LY_EMEM);
+    LY_CHECK_ERR_RET(!buf, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
     memmove(buf + 1, word, word_len);
     buf[0] = 0x06; /* pattern's default regular-match flag */
     buf[word_len + 1] = '\0'; /* terminating NULL byte */
-    LY_CHECK_RET(lydict_insert_zc(ctx->ctx, buf, &restr->arg.str));
-    restr->arg.mod = ctx->main_mod;
+    LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
+    restr->arg.mod = ctx->parsed_mod;
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -2056,7 +2056,7 @@
     INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
 
     /* set module */
-    type->mod = ctx->main_mod;
+    type->pmod = ctx->parsed_mod;
 
     YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
         switch (kw) {
@@ -2082,7 +2082,7 @@
                 return LY_EVALID;
             }
             type->length = calloc(1, sizeof *type->length);
-            LY_CHECK_ERR_RET(!type->length, LOGMEM(ctx->ctx), LY_EMEM);
+            LY_CHECK_ERR_RET(!type->length, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
 
             LY_CHECK_RET(parse_restr(ctx, in, kw, type->length));
             type->flags |= LYS_SET_LENGTH;
@@ -2094,9 +2094,9 @@
             }
 
             LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_PATH, 0, &str_path, Y_STR_ARG, &type->exts));
-            ret = ly_path_parse(ctx->ctx, NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
+            ret = ly_path_parse(PARSER_CTX(ctx), NULL, str_path, 0, LY_PATH_BEGIN_EITHER, LY_PATH_LREF_TRUE,
                     LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_LEAFREF, &type->path);
-            lydict_remove(ctx->ctx, str_path);
+            lydict_remove(PARSER_CTX(ctx), str_path);
             LY_CHECK_RET(ret);
             type->flags |= LYS_SET_PATH;
             break;
@@ -2110,7 +2110,7 @@
                 return LY_EVALID;
             }
             type->range = calloc(1, sizeof *type->range);
-            LY_CHECK_ERR_RET(!type->range, LOGMEM(ctx->ctx), LY_EMEM);
+            LY_CHECK_ERR_RET(!type->range, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
 
             LY_CHECK_RET(parse_restr(ctx, in, kw, type->range));
             type->flags |= LYS_SET_RANGE;
@@ -2120,7 +2120,7 @@
             /* LYS_SET_REQINST checked and set inside parse_type_reqinstance() */
             break;
         case LY_STMT_TYPE:
-            LY_ARRAY_NEW_RET(ctx->ctx, type->types, nest_type, LY_EMEM);
+            LY_ARRAY_NEW_RET(PARSER_CTX(ctx), type->types, nest_type, LY_EMEM);
             LY_CHECK_RET(parse_type(ctx, in, nest_type));
             type->flags |= LYS_SET_TYPE;
             break;
@@ -2154,7 +2154,7 @@
     struct lysp_node_leaf *leaf;
 
     /* create new leaf structure */
-    LY_LIST_NEW_RET(ctx->ctx, siblings, leaf, next, LY_EMEM);
+    LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, leaf, next, LY_EMEM);
     leaf->nodetype = LYS_LEAF;
     leaf->parent = parent;
 
@@ -2170,7 +2170,7 @@
             break;
         case LY_STMT_DEFAULT:
             LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
-            leaf->dflt.mod = ctx->main_mod;
+            leaf->dflt.mod = ctx->parsed_mod;
             break;
         case LY_STMT_DESCRIPTION:
             LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
@@ -2420,7 +2420,7 @@
     struct lysp_node_leaflist *llist;
 
     /* create new leaf-list structure */
-    LY_LIST_NEW_RET(ctx->ctx, siblings, llist, next, LY_EMEM);
+    LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, llist, next, LY_EMEM);
     llist->nodetype = LYS_LEAFLIST;
     llist->parent = parent;
 
@@ -2508,7 +2508,7 @@
     enum ly_stmt kw;
     struct lysp_refine *rf;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *refines, rf, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
@@ -2521,7 +2521,7 @@
             LY_CHECK_RET(parse_config(ctx, in, &rf->flags, &rf->exts));
             break;
         case LY_STMT_DEFAULT:
-            LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
+            LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, &rf->dflts, Y_STR_ARG, &rf->exts));
             break;
         case LY_STMT_DESCRIPTION:
             LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &rf->dsc, Y_STR_ARG, &rf->exts));
@@ -2577,7 +2577,7 @@
     enum ly_stmt kw;
     struct lysp_tpdf *tpdf;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *typedefs, tpdf, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -2588,7 +2588,7 @@
         switch (kw) {
         case LY_STMT_DEFAULT:
             LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
-            tpdf->dflt.mod = ctx->main_mod;
+            tpdf->dflt.mod = ctx->parsed_mod;
             break;
         case LY_STMT_DESCRIPTION:
             LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
@@ -2734,7 +2734,7 @@
     enum ly_stmt kw;
     struct lysp_action *act;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *actions, act, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *actions, act, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -2815,7 +2815,7 @@
     enum ly_stmt kw;
     struct lysp_notif *notif;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *notifs, notif, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *notifs, notif, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -2907,7 +2907,7 @@
     enum ly_stmt kw;
     struct lysp_grp *grp;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *groupings, grp, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *groupings, grp, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -3000,7 +3000,7 @@
     enum ly_stmt kw;
     struct lysp_augment *aug;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *augments, aug, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *augments, aug, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
@@ -3098,7 +3098,7 @@
     struct lysp_node_uses *uses;
 
     /* create uses structure */
-    LY_LIST_NEW_RET(ctx->ctx, siblings, uses, next, LY_EMEM);
+    LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, uses, next, LY_EMEM);
     uses->nodetype = LYS_USES;
     uses->parent = parent;
 
@@ -3165,7 +3165,7 @@
     struct lysp_node_case *cas;
 
     /* create new case structure */
-    LY_LIST_NEW_RET(ctx->ctx, siblings, cas, next, LY_EMEM);
+    LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cas, next, LY_EMEM);
     cas->nodetype = LYS_CASE;
     cas->parent = parent;
 
@@ -3246,7 +3246,7 @@
     struct lysp_node_choice *choice;
 
     /* create new choice structure */
-    LY_LIST_NEW_RET(ctx->ctx, siblings, choice, next, LY_EMEM);
+    LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, choice, next, LY_EMEM);
     choice->nodetype = LYS_CHOICE;
     choice->parent = parent;
 
@@ -3281,7 +3281,7 @@
         case LY_STMT_DEFAULT:
             LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
                     &choice->exts));
-            choice->dflt.mod = ctx->main_mod;
+            choice->dflt.mod = ctx->parsed_mod;
             break;
 
         case LY_STMT_ANYDATA:
@@ -3339,7 +3339,7 @@
     struct lysp_node_container *cont;
 
     /* create new container structure */
-    LY_LIST_NEW_RET(ctx->ctx, siblings, cont, next, LY_EMEM);
+    LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, cont, next, LY_EMEM);
     cont->nodetype = LYS_CONTAINER;
     cont->parent = parent;
 
@@ -3447,7 +3447,7 @@
     struct lysp_node_list *list;
 
     /* create new list structure */
-    LY_LIST_NEW_RET(ctx->ctx, siblings, list, next, LY_EMEM);
+    LY_LIST_NEW_RET(PARSER_CTX(ctx), siblings, list, next, LY_EMEM);
     list->nodetype = LYS_LIST;
     list->parent = parent;
 
@@ -3663,7 +3663,7 @@
     enum ly_stmt kw;
     struct lysp_ext *ex;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *extensions, ex, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *extensions, ex, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -3714,7 +3714,8 @@
     struct lysp_deviate_add *d_add = NULL;
     struct lysp_deviate_rpl *d_rpl = NULL;
     struct lysp_deviate_del *d_del = NULL;
-    const char **d_units = NULL, ***d_uniques = NULL, ***d_dflts = NULL;
+    const char **d_units = NULL;
+    struct lysp_qname **d_uniques = NULL, **d_dflts = NULL;
     struct lysp_restr **d_musts = NULL;
     uint16_t *d_flags = 0;
     uint32_t *d_min = 0, *d_max = 0;
@@ -3741,11 +3742,11 @@
     switch (dev_mod) {
     case LYS_DEV_NOT_SUPPORTED:
         d = calloc(1, sizeof *d);
-        LY_CHECK_ERR_RET(!d, LOGMEM(ctx->ctx), LY_EMEM);
+        LY_CHECK_ERR_RET(!d, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
         break;
     case LYS_DEV_ADD:
         d_add = calloc(1, sizeof *d_add);
-        LY_CHECK_ERR_RET(!d_add, LOGMEM(ctx->ctx), LY_EMEM);
+        LY_CHECK_ERR_RET(!d_add, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
         d = (struct lysp_deviate *)d_add;
         d_units = &d_add->units;
         d_uniques = &d_add->uniques;
@@ -3757,7 +3758,7 @@
         break;
     case LYS_DEV_REPLACE:
         d_rpl = calloc(1, sizeof *d_rpl);
-        LY_CHECK_ERR_RET(!d_rpl, LOGMEM(ctx->ctx), LY_EMEM);
+        LY_CHECK_ERR_RET(!d_rpl, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
         d = (struct lysp_deviate *)d_rpl;
         d_units = &d_rpl->units;
         d_flags = &d_rpl->flags;
@@ -3766,7 +3767,7 @@
         break;
     case LYS_DEV_DELETE:
         d_del = calloc(1, sizeof *d_del);
-        LY_CHECK_ERR_RET(!d_del, LOGMEM(ctx->ctx), LY_EMEM);
+        LY_CHECK_ERR_RET(!d_del, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
         d = (struct lysp_deviate *)d_del;
         d_units = &d_del->units;
         d_uniques = &d_del->uniques;
@@ -3775,7 +3776,7 @@
         break;
     default:
         assert(0);
-        LOGINT_RET(ctx->ctx);
+        LOGINT_RET(PARSER_CTX(ctx));
     }
     d->mod = dev_mod;
 
@@ -3801,10 +3802,11 @@
                 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
                 return LY_EVALID;
             case LYS_DEV_REPLACE:
-                LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt, Y_STR_ARG, &d->exts));
+                LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
+                d_rpl->dflt.mod = ctx->parsed_mod;
                 break;
             default:
-                LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
+                LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
                 break;
             }
             break;
@@ -3865,7 +3867,7 @@
                     return LY_EVALID;
                 }
                 d_rpl->type = calloc(1, sizeof *d_rpl->type);
-                LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(ctx->ctx), LY_EMEM);
+                LY_CHECK_ERR_RET(!d_rpl->type, LOGMEM(PARSER_CTX(ctx)), LY_EMEM);
                 LY_CHECK_RET(parse_type(ctx, in, d_rpl->type));
                 break;
             }
@@ -3877,7 +3879,7 @@
                 LOGVAL_PARSER(ctx, LY_VCODE_INDEV, ly_devmod2str(dev_mod), ly_stmt2str(kw));
                 return LY_EVALID;
             default:
-                LY_CHECK_RET(parse_text_fields(ctx, in, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
+                LY_CHECK_RET(parse_qnames(ctx, in, LYEXT_SUBSTMT_UNIQUE, d_uniques, Y_STR_ARG, &d->exts));
                 break;
             }
             break;
@@ -3920,7 +3922,7 @@
     enum ly_stmt kw;
     struct lysp_deviation *dev;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *deviations, dev, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *deviations, dev, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
@@ -3975,7 +3977,7 @@
     enum ly_stmt kw;
     struct lysp_feature *feat;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *features, feat, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *features, feat, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -4024,7 +4026,7 @@
     enum ly_stmt kw;
     struct lysp_ident *ident;
 
-    LY_ARRAY_NEW_RET(ctx->ctx, *identities, ident, LY_EMEM);
+    LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *identities, ident, LY_EMEM);
 
     /* get value */
     LY_CHECK_RET(get_argument(ctx, in, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
@@ -4046,7 +4048,7 @@
             LY_CHECK_RET(parse_status(ctx, in, &ident->flags, &ident->exts));
             break;
         case LY_STMT_BASE:
-            if (ident->bases && (ctx->mod_version < 2)) {
+            if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
                 LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Identity can be derived from multiple base identities only in YANG 1.1 modules");
                 return LY_EVALID;
             }
@@ -4150,8 +4152,7 @@
         switch (kw) {
         /* module header */
         case LY_STMT_YANG_VERSION:
-            LY_CHECK_RET(parse_yangversion(ctx, in, &mod->mod->version, &mod->exts));
-            ctx->mod_version = mod->mod->version;
+            LY_CHECK_RET(parse_yangversion(ctx, in, &mod->version, &mod->exts));
             break;
         case LY_STMT_NAMESPACE:
             LY_CHECK_RET(parse_text_field(ctx, in, LYEXT_SUBSTMT_NAMESPACE, 0, &mod->mod->ns, Y_STR_ARG, &mod->exts));
@@ -4266,7 +4267,7 @@
 
     /* submodules share the namespace with the module names, so there must not be
      * a submodule of the same name in the context, no need for revision matching */
-    dup = ly_ctx_get_submodule(ctx->ctx, NULL, mod->mod->name, NULL);
+    dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, mod->mod->name, NULL);
     if (dup) {
         LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between module and submodule of name \"%s\".", mod->mod->name);
         return LY_EVALID;
@@ -4362,7 +4363,6 @@
         /* module header */
         case LY_STMT_YANG_VERSION:
             LY_CHECK_RET(parse_yangversion(ctx, in, &submod->version, &submod->exts));
-            ctx->mod_version = submod->version;
             break;
         case LY_STMT_BELONGS_TO:
             LY_CHECK_RET(parse_belongsto(ctx, in, &submod->prefix, &submod->exts));
@@ -4472,7 +4472,7 @@
 
     /* submodules share the namespace with the module names, so there must not be
      * a submodule of the same name in the context, no need for revision matching */
-    dup = ly_ctx_get_submodule(ctx->ctx, NULL, submod->name, NULL);
+    dup = ly_ctx_get_submodule(PARSER_CTX(ctx), NULL, submod->name, NULL);
     /* main modules may have different revisions */
     if (dup && strcmp(dup->mod->name, submod->mod->name)) {
         LOGVAL_PARSER(ctx, LYVE_SYNTAX_YANG, "Name collision between submodules of name \"%s\".", dup->name);
@@ -4496,11 +4496,15 @@
     *context = calloc(1, sizeof **context);
     LY_CHECK_ERR_RET(!(*context), LOGMEM(ly_ctx), LY_EMEM);
     (*context)->format = LYS_IN_YANG;
-    (*context)->main_mod = main_ctx->main_mod;
-    (*context)->ctx = ly_ctx;
     (*context)->pos_type = LY_VLOG_LINE;
     (*context)->line = 1;
 
+    mod_p = calloc(1, sizeof *mod_p);
+    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(ly_ctx); ret = LY_EMEM, cleanup);
+    mod_p->mod = main_ctx->parsed_mod->mod;
+    mod_p->parsing = 1;
+    (*context)->parsed_mod = (struct lysp_module *)mod_p;
+
     /* map the typedefs and groupings list from main context to the submodule's context */
     memcpy(&(*context)->tpdfs_nodes, &main_ctx->tpdfs_nodes, sizeof main_ctx->tpdfs_nodes);
     memcpy(&(*context)->grps_nodes, &main_ctx->grps_nodes, sizeof main_ctx->grps_nodes);
@@ -4510,7 +4514,7 @@
     LY_CHECK_GOTO(ret, cleanup);
 
     if (kw == LY_STMT_MODULE) {
-        LOGERR((*context)->ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
+        LOGERR(ly_ctx, LY_EDENIED, "Input data contains module in situation when a submodule is expected.");
         ret = LY_EINVAL;
         goto cleanup;
     } else if (kw != LY_STMT_SUBMODULE) {
@@ -4519,11 +4523,6 @@
         goto cleanup;
     }
 
-    mod_p = calloc(1, sizeof *mod_p);
-    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx); ret = LY_EMEM, cleanup);
-    mod_p->mod = main_ctx->main_mod;
-    mod_p->parsing = 1;
-
     /* substatements */
     ret = parse_submodule(*context, in, mod_p);
     LY_CHECK_GOTO(ret, cleanup);
@@ -4543,7 +4542,7 @@
 
 cleanup:
     if (ret) {
-        lysp_submodule_free((*context)->ctx, mod_p);
+        lysp_module_free((struct lysp_module *)mod_p);
         yang_parser_ctx_free(*context);
         *context = NULL;
     }
@@ -4564,17 +4563,21 @@
     *context = calloc(1, sizeof **context);
     LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
     (*context)->format = LYS_IN_YANG;
-    (*context)->main_mod = mod;
-    (*context)->ctx = mod->ctx;
     (*context)->pos_type = LY_VLOG_LINE;
     (*context)->line = 1;
 
+    mod_p = calloc(1, sizeof *mod_p);
+    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
+    mod_p->mod = mod;
+    mod_p->parsing = 1;
+    (*context)->parsed_mod = mod_p;
+
     /* "module"/"submodule" */
     ret = get_keyword(*context, in, &kw, &word, &word_len);
     LY_CHECK_GOTO(ret, cleanup);
 
     if (kw == LY_STMT_SUBMODULE) {
-        LOGERR((*context)->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
+        LOGERR(mod->ctx, LY_EDENIED, "Input data contains submodule which cannot be parsed directly without its main module.");
         ret = LY_EINVAL;
         goto cleanup;
     } else if (kw != LY_STMT_MODULE) {
@@ -4583,11 +4586,6 @@
         goto cleanup;
     }
 
-    mod_p = calloc(1, sizeof *mod_p);
-    LY_CHECK_ERR_GOTO(!mod_p, LOGMEM((*context)->ctx), cleanup);
-    mod_p->mod = mod;
-    mod_p->parsing = 1;
-
     /* substatements */
     ret = parse_module(*context, in, mod_p);
     LY_CHECK_GOTO(ret, cleanup);