parser FEATURE set of currently parsed modules

... instead of only the last one. It is used
to handle cyclic includes.
diff --git a/src/parser_yang.c b/src/parser_yang.c
index 79a1683..692064b 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -760,8 +760,7 @@
     } else if (*kw == LY_STMT_SYNTAX_LEFT_BRACE) {
         ctx->depth++;
         if (ctx->depth > LY_MAX_BLOCK_DEPTH) {
-            LOGERR(ctx->parsed_mod->mod->ctx, LY_EINVAL,
-                    "The maximum number of block nestings has been exceeded.");
+            LOGERR(PARSER_CTX(ctx), LY_EINVAL, "The maximum number of block nestings has been exceeded.");
             return LY_EINVAL;
         }
         goto success;
@@ -885,7 +884,7 @@
     }
 
     stmt->format = LY_VALUE_SCHEMA;
-    stmt->prefix_data = ctx->parsed_mod;
+    stmt->prefix_data = PARSER_CUR_PMOD(ctx);
     stmt->kw = kw;
 
     YANG_READ_SUBSTMT_FOR(ctx, child_kw, word, word_len, ret, return LY_SUCCESS, return ret) {
@@ -935,7 +934,7 @@
     /* store the rest of information */
     e->format = LY_VALUE_SCHEMA;
     e->parsed = NULL;
-    e->prefix_data = ctx->parsed_mod;
+    e->prefix_data = PARSER_CUR_PMOD(ctx);
     e->parent_stmt = insubstmt;
     e->parent_stmt_index = insubstmt_index;
 
@@ -1064,9 +1063,9 @@
 
     /* get value, it must match the main module */
     LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
-    if (ly_strncmp(ctx->parsed_mod->mod->name, word, word_len)) {
+    if (ly_strncmp(PARSER_CUR_PMOD(ctx)->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->parsed_mod->mod->name);
+                (int)word_len, word, PARSER_CUR_PMOD(ctx)->mod->name);
         free(buf);
         return LY_EVALID;
     }
@@ -1375,7 +1374,7 @@
     LY_CHECK_RET(get_argument(ctx, arg, NULL, &word, &buf, &word_len));
 
     INSERT_WORD_RET(ctx, buf, item->str, word, word_len);
-    item->mod = ctx->parsed_mod;
+    item->mod = PARSER_CUR_PMOD(ctx);
     YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
         switch (kw) {
         case LY_STMT_EXTENSION_INSTANCE:
@@ -1509,7 +1508,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->parsed_mod;
+    restr->arg.mod = PARSER_CUR_PMOD(ctx);
     YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
         switch (kw) {
         case LY_STMT_DESCRIPTION:
@@ -2071,7 +2070,7 @@
     buf[0] = LYSP_RESTR_PATTERN_ACK; /* pattern's default regular-match flag */
     buf[word_len + 1] = '\0'; /* terminating NULL byte */
     LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, &restr->arg.str));
-    restr->arg.mod = ctx->parsed_mod;
+    restr->arg.mod = PARSER_CUR_PMOD(ctx);
 
     YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
         switch (kw) {
@@ -2130,7 +2129,7 @@
     INSERT_WORD_RET(ctx, buf, type->name, word, word_len);
 
     /* set module */
-    type->pmod = ctx->parsed_mod;
+    type->pmod = PARSER_CUR_PMOD(ctx);
 
     YANG_READ_SUBSTMT_FOR(ctx, kw, word, word_len, ret, return LY_SUCCESS, return ret) {
         switch (kw) {
@@ -2250,7 +2249,7 @@
             break;
         case LY_STMT_DEFAULT:
             LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &leaf->dflt.str, Y_STR_ARG, &leaf->exts));
-            leaf->dflt.mod = ctx->parsed_mod;
+            leaf->dflt.mod = PARSER_CUR_PMOD(ctx);
             break;
         case LY_STMT_DESCRIPTION:
             LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &leaf->dsc, Y_STR_ARG, &leaf->exts));
@@ -2663,7 +2662,7 @@
         switch (kw) {
         case LY_STMT_DEFAULT:
             LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &tpdf->dflt.str, Y_STR_ARG, &tpdf->exts));
-            tpdf->dflt.mod = ctx->parsed_mod;
+            tpdf->dflt.mod = PARSER_CUR_PMOD(ctx);
             break;
         case LY_STMT_DESCRIPTION:
             LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DESCRIPTION, 0, &tpdf->dsc, Y_STR_ARG, &tpdf->exts));
@@ -3339,7 +3338,7 @@
         case LY_STMT_DEFAULT:
             LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &choice->dflt.str, Y_PREF_IDENTIF_ARG,
                     &choice->exts));
-            choice->dflt.mod = ctx->parsed_mod;
+            choice->dflt.mod = PARSER_CUR_PMOD(ctx);
             break;
 
         case LY_STMT_ANYDATA:
@@ -3849,7 +3848,7 @@
                 return LY_EVALID;
             case LYS_DEV_REPLACE:
                 LY_CHECK_RET(parse_text_field(ctx, LY_STMT_DEFAULT, 0, &d_rpl->dflt.str, Y_STR_ARG, &d->exts));
-                d_rpl->dflt.mod = ctx->parsed_mod;
+                d_rpl->dflt.mod = PARSER_CUR_PMOD(ctx);
                 break;
             default:
                 LY_CHECK_RET(parse_qnames(ctx, LY_STMT_DEFAULT, d_dflts, Y_STR_ARG, &d->exts));
@@ -4091,7 +4090,7 @@
             LY_CHECK_RET(parse_status(ctx, &ident->flags, &ident->exts));
             break;
         case LY_STMT_BASE:
-            if (ident->bases && (ctx->parsed_mod->version < LYS_VERSION_1_1)) {
+            if (ident->bases && (PARSER_CUR_PMOD(ctx)->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;
             }
@@ -4575,9 +4574,12 @@
 
     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->mod = PARSER_CUR_PMOD(main_ctx)->mod;
     mod_p->parsing = 1;
-    (*context)->parsed_mod = (struct lysp_module *)mod_p;
+
+    /* use main context parsed mods adding the current one */
+    (*context)->parsed_mods = main_ctx->parsed_mods;
+    ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
 
     LOG_LOCINIT(NULL, NULL, NULL, in);
 
@@ -4639,13 +4641,14 @@
     *context = calloc(1, sizeof **context);
     LY_CHECK_ERR_RET(!(*context), LOGMEM(mod->ctx), LY_EMEM);
     (*context)->format = LYS_IN_YANG;
+    LY_CHECK_ERR_RET(ly_set_new(&(*context)->parsed_mods), free(*context); LOGMEM(mod->ctx), LY_EMEM);
     (*context)->in = in;
     (*context)->main_ctx = (struct lys_parser_ctx *)(*context);
 
     mod_p = calloc(1, sizeof *mod_p);
     LY_CHECK_ERR_GOTO(!mod_p, LOGMEM(mod->ctx), cleanup);
     mod_p->mod = mod;
-    (*context)->parsed_mod = mod_p;
+    ly_set_add((*context)->parsed_mods, mod_p, 1, NULL);
 
     LOG_LOCINIT(NULL, NULL, NULL, in);