Merge remote-tracking branch 'upstream/libyang2' into libyang2
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0ad549c..b45c692 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -162,6 +162,7 @@
     src/tree_schema_compile.c
     src/tree_schema_helpers.c
     src/parser_yang.c
+    src/parser_yin.c
     src/xml.c
     src/xpath.c)
 
diff --git a/src/parser_yin.c b/src/parser_yin.c
new file mode 100644
index 0000000..e8b0333
--- /dev/null
+++ b/src/parser_yin.c
@@ -0,0 +1,319 @@
+/**
+ * @file parser_yin.c
+ * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
+ * @brief YIN parser.
+ *
+ * Copyright (c) 2015 - 2018 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.
+ * You may obtain a copy of the License at
+ *
+ *     https://opensource.org/licenses/BSD-3-Clause
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "common.h"
+#include "context.h"
+#include "libyang.h"
+#include "xml.h"
+#include "tree_schema_internal.h"
+
+enum YIN_ARGUMENT {
+    YIN_ARG_NONE = 0,
+    YIN_ARG_NAME,
+    YIN_ARG_TARGET_NODE,
+    YIN_ARG_MODULE,
+    YIN_ARG_VALUE,
+    YIN_ARG_TEXT,
+    YIN_ARG_CONDITION,
+    YIN_ARG_URI,
+    YIN_ARG_DATE,
+    YIN_ARG_TAG,
+};
+
+enum YIN_ARGUMENT
+match_argument_name(const char *name, size_t len)
+{
+    enum YIN_ARGUMENT arg = YIN_ARG_NONE;
+    size_t already_read = 0;
+
+#define MOVE_DATA(DATA, COUNT) already_read+=COUNT;
+#define IF_ARG(STR, LEN, STMT) if (!strncmp((name) + already_read, STR, LEN)) {MOVE_DATA(name, LEN);arg=STMT;}
+#define IF_ARG_PREFIX(STR, LEN) if (!strncmp((name) + already_read, STR, LEN)) {MOVE_DATA(name, LEN);
+#define IF_ARG_PREFIX_END }
+
+    switch(*name) {
+        case 'c':
+            MOVE_DATA(name, 1);
+            IF_ARG("ondition", 8, YIN_ARG_CONDITION);
+        break;
+
+        case 'd':
+            MOVE_DATA(name, 1);
+            IF_ARG("ate", 3, YIN_ARG_DATE);
+        break;
+
+        case 'm':
+            MOVE_DATA(name, 1);
+            IF_ARG("odule", 5, YIN_ARG_MODULE);
+        break;
+
+        case 'n':
+            MOVE_DATA(name, 1);
+            IF_ARG("ame", 3, YIN_ARG_NAME);
+        break;
+
+        case 't':
+            MOVE_DATA(name, 1);
+            IF_ARG_PREFIX("a", 1)
+                IF_ARG("g", 1, YIN_ARG_TAG)
+                else IF_ARG("rget-node", 9, YIN_ARG_TARGET_NODE)
+            IF_ARG_PREFIX_END
+            else IF_ARG("ext", 3, YIN_ARG_TEXT)
+        break;
+
+        case 'u':
+            MOVE_DATA(name, 1);
+            IF_ARG("ri", 2, YIN_ARG_URI)
+        break;
+
+        case 'v':
+            MOVE_DATA(name, 1);
+            IF_ARG("alue", 4, YIN_ARG_VALUE);
+        break;
+    }
+
+    /* whole argument must be matched */
+    if (already_read != len) {
+        arg = YIN_ARG_NONE;
+    }
+
+    return arg;
+}
+
+LY_ERR
+parser_belongs_to(struct lyxml_context *xml_ctx, const char **data, const char **belongsto, const char **prefix, struct lysp_ext **extensions)
+{
+    enum yang_keyword kw = YANG_NONE;
+    LY_ERR ret = LY_SUCCESS;
+    const char *prefix_out, *name;
+    size_t prefix_len, name_len;
+
+    char *buf = NULL, *out = NULL;
+    size_t buf_len = 0, out_len = 0;
+    int dynamic;
+
+    /* check if belongs-to has argument module */
+    ret = lyxml_get_attribute(xml_ctx, data, &prefix_out, &prefix_len, &name, &name_len);
+    LY_CHECK_RET1(ret);
+    if (match_argument_name(name, name_len) != YIN_ARG_MODULE) {
+        LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"module\".", name);
+        return LY_EINVAL;
+    }
+
+    /* read content of argument */
+    ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
+    LY_CHECK_RET1(ret);
+    *belongsto = lydict_insert(xml_ctx->ctx, out, out_len);
+    LY_CHECK_ERR_RET(!belongsto, LOGMEM(xml_ctx->ctx), LY_EMEM);
+
+    /* read substatements */
+    while (xml_ctx->status == LYXML_ATTRIBUTE) {
+        ret = lyxml_get_attribute(xml_ctx, data, &prefix_out, &prefix_len, &name, &name_len);
+        LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), ret);
+        kw = match_keyword(name);
+
+        switch (kw) {
+        case YANG_PREFIX:
+            ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
+            *prefix = lydict_insert(xml_ctx->ctx, out, out_len);
+            break;
+        case YANG_CUSTOM:
+            /* TODO parse extension */
+            break;
+        default:
+            LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Unexpected attribute");
+            return LY_EVALID;
+        }
+    }
+
+    if (!prefix) {
+        LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing prefix");
+        return LY_EVALID;
+    }
+
+    return LY_SUCCESS;
+}
+
+LY_ERR
+parse_namespace(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p)
+{
+    LY_ERR ret = LY_SUCCESS;
+    const char *prefix, *name;
+    size_t prefix_len, name_len;
+
+    char *buf = NULL, *out = NULL;
+    size_t buf_len = 0, out_len = 0;
+    int dynamic;
+
+    /* check if namespace has argument uri */
+    ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
+    LY_CHECK_RET1(ret);
+    if (match_argument_name(name, name_len) != YIN_ARG_URI) {
+        LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"uri\".", name);
+        return LY_EVALID;
+    }
+
+    ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
+    LY_CHECK_RET1(ret);
+    (*mod_p)->ns = lydict_insert(xml_ctx->ctx, out, out_len);
+    LY_CHECK_ERR_RET(!(*mod_p)->ns, LOGMEM(xml_ctx->ctx), LY_EMEM);
+
+    /* namespace can only have one argument */
+    ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
+    LY_CHECK_RET1(ret);
+    if (name) {
+        LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Unexpected argument \"%s\".", name);
+        return LY_EVALID;
+    }
+
+    return LY_SUCCESS;
+}
+
+LY_ERR
+parse_prefix(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p)
+{
+    LY_ERR ret = LY_SUCCESS;
+    const char *prefix, *name;
+    size_t prefix_len, name_len;
+
+    char *buf = NULL, *out = NULL;
+    size_t buf_len = 0, out_len = 0;
+    int dynamic;
+
+    /* check if prefix has argument value */
+    ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
+    LY_CHECK_RET1(ret);
+    if (match_argument_name(name, name_len) != YIN_ARG_VALUE) {
+        LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"value\".", name);
+        return LY_EVALID;
+    }
+
+    ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
+    LY_CHECK_RET1(ret);
+    (*mod_p)->prefix = lydict_insert(xml_ctx->ctx, out, out_len);
+    LY_CHECK_ERR_RET(!(*mod_p)->prefix, LOGMEM(xml_ctx->ctx), LY_EMEM);
+
+    /* prefix element can only have one argument */
+    ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
+    LY_CHECK_RET1(ret);
+    if (name) {
+        LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Unexpected argument \"%s\".", name);
+        return LY_EVALID;
+    }
+    return LY_SUCCESS;
+}
+
+LY_ERR
+parse_submodule(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p)
+{
+    LY_ERR ret = LY_SUCCESS;
+    enum yang_keyword kw = YANG_NONE;
+    const char *prefix, *name;
+    size_t prefix_len, name_len;
+
+    char *buf = NULL, *out = NULL;
+    size_t buf_len = 0, out_len = 0;
+    int dynamic;
+
+    /* check if module/submodule has argument "name" */
+    ret = lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
+    LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
+    if (match_argument_name(name, name_len) != YIN_ARG_NAME) {
+        LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"name\".", name);
+    }
+
+    /* read module name */
+    if (xml_ctx->status != LYXML_ATTR_CONTENT) {
+        LOGVAL(xml_ctx->ctx, LY_VLOG_LINE, &xml_ctx->line, LYVE_SYNTAX, "Missing value of argument \"name\"");
+    }
+    ret = lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
+    LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
+    (*mod_p)->name = lydict_insert(xml_ctx->ctx, out, out_len);
+    LY_CHECK_ERR_RET(!(*mod_p)->name, LOGMEM(xml_ctx->ctx), LY_EMEM);
+
+    /* read all attributes and their content only for testing */
+    while (xml_ctx->status == LYXML_ATTRIBUTE) {
+        lyxml_get_attribute(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
+        while (xml_ctx->status == LYXML_ATTR_CONTENT) {
+            lyxml_get_string(xml_ctx, data, &buf, &buf_len, &out, &out_len, &dynamic);
+        }
+    }
+
+
+    while (xml_ctx->status == LYXML_ELEMENT || xml_ctx->status == LYXML_ELEM_CONTENT) {
+        ret = lyxml_get_element(xml_ctx, data, &prefix, &prefix_len, &name, &name_len);
+        LY_CHECK_ERR_RET(ret != LY_SUCCESS, LOGMEM(xml_ctx->ctx), LY_EMEM);
+        kw = match_keyword(name);
+
+        switch (kw) {
+            case YANG_NAMESPACE:
+                ret = parse_namespace(xml_ctx, data, mod_p);
+            break;
+            case YANG_PREFIX:
+                ret = parse_prefix(xml_ctx, data, mod_p);
+                /* TODO change lysp_check_prefix function to work with ctx and not parser_ctx */
+                //LY_CHECK_RET(lysp_check_prefix(&xml_ctx->ctx, *mod_p, &((*mod_p)->prefix)), LY_EVALID);
+            break;
+            case YANG_BELONGS_TO:
+                ret = parser_belongs_to(xml_ctx, data, &(*mod_p)->belongsto, &(*mod_p)->prefix, &(*mod_p)->extensions);
+            break;
+
+            default:
+                /* error */
+            break;
+        }
+    }
+
+    return ret;
+}
+
+LY_ERR
+yin_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
+{
+    LY_ERR ret = LY_SUCCESS;
+    enum yang_keyword kw = YANG_NONE;
+    struct lyxml_context xml_ctx;
+
+    memset(&xml_ctx, 0, sizeof xml_ctx);
+    xml_ctx.ctx = ctx;
+    xml_ctx.line = 1;
+
+    const char *prefix, *name;
+    size_t prefix_len, name_len;
+
+    /* check if root element is module or submodule */
+    ret = lyxml_get_element(&xml_ctx, &data, &prefix, &prefix_len, &name, &name_len);
+    LY_CHECK_GOTO(ret != LY_SUCCESS, error);
+    kw = match_keyword(name);
+    if (kw != YANG_MODULE && kw != YANG_SUBMODULE) {
+        LOGVAL(xml_ctx.ctx, LY_VLOG_LINE, &xml_ctx.line, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", name);
+    }
+
+    if (kw == YANG_SUBMODULE) {
+        (*mod_p)->submodule = 1;
+    }
+
+    ret = parse_submodule(&xml_ctx, &data, mod_p);
+
+    lyxml_context_clear(&xml_ctx);
+    return ret;
+
+error:
+    lyxml_context_clear(&xml_ctx);
+    return ret;
+}
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_helpers.c
index 742b882..396b9f7 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_helpers.c
@@ -1183,3 +1183,198 @@
     return NULL;
 }
 
+enum yang_keyword
+match_keyword(const char *data)
+{
+/* TODO make this function usable in get_keyword function */
+#define MOVE_IN(DATA, COUNT) (data)+=COUNT;
+#define IF_KEYWORD(STR, LEN, STMT) if (!strncmp((data), STR, LEN)) {MOVE_IN(data, LEN);kw=STMT;}
+#define IF_KEYWORD_PREFIX(STR, LEN) if (!strncmp((data), STR, LEN)) {MOVE_IN(data, LEN);
+#define IF_KEYWORD_PREFIX_END }
+
+    enum yang_keyword kw = YANG_NONE;
+    /* read the keyword itself */
+    switch (*data) {
+    case 'a':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("rgument", 7, YANG_ARGUMENT)
+        else IF_KEYWORD("ugment", 6, YANG_AUGMENT)
+        else IF_KEYWORD("ction", 5, YANG_ACTION)
+        else IF_KEYWORD_PREFIX("ny", 2)
+            IF_KEYWORD("data", 4, YANG_ANYDATA)
+            else IF_KEYWORD("xml", 3, YANG_ANYXML)
+        IF_KEYWORD_PREFIX_END
+        break;
+    case 'b':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("ase", 3, YANG_BASE)
+        else IF_KEYWORD("elongs-to", 9, YANG_BELONGS_TO)
+        else IF_KEYWORD("it", 2, YANG_BIT)
+        break;
+    case 'c':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("ase", 3, YANG_CASE)
+        else IF_KEYWORD("hoice", 5, YANG_CHOICE)
+        else IF_KEYWORD_PREFIX("on", 2)
+            IF_KEYWORD("fig", 3, YANG_CONFIG)
+            else IF_KEYWORD_PREFIX("ta", 2)
+                IF_KEYWORD("ct", 2, YANG_CONTACT)
+                else IF_KEYWORD("iner", 4, YANG_CONTAINER)
+            IF_KEYWORD_PREFIX_END
+        IF_KEYWORD_PREFIX_END
+        break;
+    case 'd':
+        MOVE_IN(data, 1);
+        IF_KEYWORD_PREFIX("e", 1)
+            IF_KEYWORD("fault", 5, YANG_DEFAULT)
+            else IF_KEYWORD("scription", 9, YANG_DESCRIPTION)
+            else IF_KEYWORD_PREFIX("viat", 4)
+                IF_KEYWORD("e", 1, YANG_DEVIATE)
+                else IF_KEYWORD("ion", 3, YANG_DEVIATION)
+            IF_KEYWORD_PREFIX_END
+        IF_KEYWORD_PREFIX_END
+        break;
+    case 'e':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("num", 3, YANG_ENUM)
+        else IF_KEYWORD_PREFIX("rror-", 5)
+            IF_KEYWORD("app-tag", 7, YANG_ERROR_APP_TAG)
+            else IF_KEYWORD("message", 7, YANG_ERROR_MESSAGE)
+        IF_KEYWORD_PREFIX_END
+        else IF_KEYWORD("xtension", 8, YANG_EXTENSION)
+        break;
+    case 'f':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("eature", 6, YANG_FEATURE)
+        else IF_KEYWORD("raction-digits", 14, YANG_FRACTION_DIGITS)
+        break;
+    case 'g':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("rouping", 7, YANG_GROUPING)
+        break;
+    case 'i':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("dentity", 7, YANG_IDENTITY)
+        else IF_KEYWORD("f-feature", 9, YANG_IF_FEATURE)
+        else IF_KEYWORD("mport", 5, YANG_IMPORT)
+        else IF_KEYWORD_PREFIX("n", 1)
+            IF_KEYWORD("clude", 5, YANG_INCLUDE)
+            else IF_KEYWORD("put", 3, YANG_INPUT)
+        IF_KEYWORD_PREFIX_END
+        break;
+    case 'k':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("ey", 2, YANG_KEY)
+        break;
+    case 'l':
+        MOVE_IN(data, 1);
+        IF_KEYWORD_PREFIX("e", 1)
+            IF_KEYWORD("af-list", 7, YANG_LEAF_LIST)
+            else IF_KEYWORD("af", 2, YANG_LEAF)
+            else IF_KEYWORD("ngth", 4, YANG_LENGTH)
+        IF_KEYWORD_PREFIX_END
+        else IF_KEYWORD("ist", 3, YANG_LIST)
+        break;
+    case 'm':
+        MOVE_IN(data, 1);
+        IF_KEYWORD_PREFIX("a", 1)
+            IF_KEYWORD("ndatory", 7, YANG_MANDATORY)
+            else IF_KEYWORD("x-elements", 10, YANG_MAX_ELEMENTS)
+        IF_KEYWORD_PREFIX_END
+        else IF_KEYWORD("in-elements", 11, YANG_MIN_ELEMENTS)
+        else IF_KEYWORD("ust", 3, YANG_MUST)
+        else IF_KEYWORD_PREFIX("od", 2)
+            IF_KEYWORD("ule", 3, YANG_MODULE)
+            else IF_KEYWORD("ifier", 5, YANG_MODIFIER)
+        IF_KEYWORD_PREFIX_END
+        break;
+    case 'n':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("amespace", 8, YANG_NAMESPACE)
+        else IF_KEYWORD("otification", 11, YANG_NOTIFICATION)
+        break;
+    case 'o':
+        MOVE_IN(data, 1);
+        IF_KEYWORD_PREFIX("r", 1)
+            IF_KEYWORD("dered-by", 8, YANG_ORDERED_BY)
+            else IF_KEYWORD("ganization", 10, YANG_ORGANIZATION)
+        IF_KEYWORD_PREFIX_END
+        else IF_KEYWORD("utput", 5, YANG_OUTPUT)
+        break;
+    case 'p':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("ath", 3, YANG_PATH)
+        else IF_KEYWORD("attern", 6, YANG_PATTERN)
+        else IF_KEYWORD("osition", 7, YANG_POSITION)
+        else IF_KEYWORD_PREFIX("re", 2)
+            IF_KEYWORD("fix", 3, YANG_PREFIX)
+            else IF_KEYWORD("sence", 5, YANG_PRESENCE)
+        IF_KEYWORD_PREFIX_END
+        break;
+    case 'r':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("ange", 4, YANG_RANGE)
+        else IF_KEYWORD_PREFIX("e", 1)
+            IF_KEYWORD_PREFIX("f", 1)
+                IF_KEYWORD("erence", 6, YANG_REFERENCE)
+                else IF_KEYWORD("ine", 3, YANG_REFINE)
+            IF_KEYWORD_PREFIX_END
+            else IF_KEYWORD("quire-instance", 14, YANG_REQUIRE_INSTANCE)
+            else IF_KEYWORD("vision-date", 11, YANG_REVISION_DATE)
+            else IF_KEYWORD("vision", 6, YANG_REVISION)
+        IF_KEYWORD_PREFIX_END
+        else IF_KEYWORD("pc", 2, YANG_RPC)
+        break;
+    case 's':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("tatus", 5, YANG_STATUS)
+        else IF_KEYWORD("ubmodule", 8, YANG_SUBMODULE)
+        break;
+    case 't':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("ypedef", 6, YANG_TYPEDEF)
+        else IF_KEYWORD("ype", 3, YANG_TYPE)
+        break;
+    case 'u':
+        MOVE_IN(data, 1);
+        IF_KEYWORD_PREFIX("ni", 2)
+            IF_KEYWORD("que", 3, YANG_UNIQUE)
+            else IF_KEYWORD("ts", 2, YANG_UNITS)
+        IF_KEYWORD_PREFIX_END
+        else IF_KEYWORD("ses", 3, YANG_USES)
+        break;
+    case 'v':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("alue", 4, YANG_VALUE)
+        break;
+    case 'w':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("hen", 3, YANG_WHEN)
+        break;
+    case 'y':
+        MOVE_IN(data, 1);
+        IF_KEYWORD("ang-version", 11, YANG_YANG_VERSION)
+        else IF_KEYWORD("in-element", 10, YANG_YIN_ELEMENT)
+        break;
+    case ';':
+        MOVE_IN(data, 1);
+        kw = YANG_SEMICOLON;
+        //goto success;
+        break;
+    case '{':
+        MOVE_IN(data, 1);
+        kw = YANG_LEFT_BRACE;
+        //goto success;
+        break;
+    case '}':
+        MOVE_IN(data, 1);
+        kw = YANG_RIGHT_BRACE;
+        //goto success;
+        break;
+    default:
+        break;
+    }
+
+    /* TODO important fix whole keyword must be matched */
+    return kw;
+}
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index 6bb131e..1d9ff5d 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -542,4 +542,8 @@
  */
 LY_ERR ly_ctx_module_implement_internal(struct ly_ctx *ctx, struct lys_module *mod, uint8_t implemented);
 
+/**
+ * @brief match yang keyword
+ */
+enum yang_keyword match_keyword(const char *data);
 #endif /* LY_TREE_SCHEMA_INTERNAL_H_ */
diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt
index 81753dc..e535650 100644
--- a/tests/src/CMakeLists.txt
+++ b/tests/src/CMakeLists.txt
@@ -5,6 +5,7 @@
     src_hash_table
     src_xml
     src_parser_yang
+    src_parser_yin
     src_tree_schema_compile
     src_tree_schema_helpers)
 set(local_tests_wraps
@@ -15,6 +16,7 @@
     " "
     " "
     " "
+    " "
     " ")
 set(tests ${tests} ${local_tests} PARENT_SCOPE)
 set(tests_wraps ${tests_wraps} ${local_tests_wraps} PARENT_SCOPE)
diff --git a/tests/src/test_parser_yin.c b/tests/src/test_parser_yin.c
new file mode 100644
index 0000000..870e5b4
--- /dev/null
+++ b/tests/src/test_parser_yin.c
@@ -0,0 +1,184 @@
+#include "../../src/common.c"
+#include "../../src/set.c"
+#include "../../src/log.c"
+#include "../../src/hash_table.c"
+#include "../../src/xpath.c"
+#include "../../src/parser_yang.c"
+#include "../../src/context.c"
+#include "../../src/tree_schema_helpers.c"
+#include "../../src/tree_schema_free.c"
+#include "../../src/tree_schema_compile.c"
+#include "../../src/tree_schema.c"
+#include "../../src/xml.c"
+#include "../../src/parser_yin.c"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libyang.h"
+
+static void
+test_parse(void **state)
+{
+    (void)state; /* unused */
+
+    struct ly_ctx *ctx;
+    struct lysp_module *mod;
+
+    ly_ctx_new(NULL, 0, &ctx);
+    mod = calloc(1, sizeof(*mod));
+    mod->ctx = ctx;
+
+    yin_parse(ctx, "<module name=\"example-foo\"\
+                    xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\"\
+                    xmlns:foo=\"urn:example:foo\"\
+                    xmlns:myext=\"urn:example:extensions\">\
+                    <namespace uri=\"urn:example:foo\"/>\
+                    <prefix value=\"foo\"/>\
+                    \
+                    <import module=\"example-extensions\">\
+                        <prefix value=\"myext\"/>\
+                    </import>\
+                    \
+                    <list name=\"interface\">\
+                        <key value=\"name\"/>\
+                        <leaf name=\"name\">\
+                        <type name=\"string\"/>\
+                        </leaf>\
+                        <leaf name=\"mtu\">\
+                        <type name=\"uint32\"/>\
+                        <description>\
+                            <text>The MTU of the interface.</text>\
+                        </description>\
+                        <myext:c-define name=\"MY_MTU\"/>\
+                        </leaf>\
+                    </list>\
+                    </module>",
+                &mod);
+
+    assert_string_equal(mod->name, "example-foo");
+    assert_string_equal(mod->prefix, "foo");
+
+    lysp_module_free(mod);
+    ly_ctx_destroy(ctx, NULL);
+}
+
+static int
+test_setup(void **state) {
+    (void)state; /* unused */
+    return 0;
+}
+
+static void
+test_match_keyword(void **state)
+{
+    (void)state; /* unused */
+
+    assert_int_equal(match_keyword("anydata"), YANG_ANYDATA);
+    assert_int_equal(match_keyword("asdasd"), YANG_NONE);
+    assert_int_equal(match_keyword(""), YANG_NONE);
+    assert_int_equal(match_keyword("anydata"), YANG_ANYDATA);
+    assert_int_equal(match_keyword("anyxml"), YANG_ANYXML);
+    assert_int_equal(match_keyword("argument"), YANG_ARGUMENT);
+    assert_int_equal(match_keyword("augment"), YANG_AUGMENT);
+    assert_int_equal(match_keyword("base"), YANG_BASE);
+    assert_int_equal(match_keyword("belongs-to"), YANG_BELONGS_TO);
+    assert_int_equal(match_keyword("bit"), YANG_BIT);
+    assert_int_equal(match_keyword("case"), YANG_CASE);
+    assert_int_equal(match_keyword("choice"), YANG_CHOICE);
+    assert_int_equal(match_keyword("config"), YANG_CONFIG);
+    assert_int_equal(match_keyword("contact"), YANG_CONTACT);
+    assert_int_equal(match_keyword("container"), YANG_CONTAINER);
+    assert_int_equal(match_keyword("default"), YANG_DEFAULT);
+    assert_int_equal(match_keyword("description"), YANG_DESCRIPTION);
+    assert_int_equal(match_keyword("deviate"), YANG_DEVIATE);
+    assert_int_equal(match_keyword("deviation"), YANG_DEVIATION);
+    assert_int_equal(match_keyword("enum"), YANG_ENUM);
+    assert_int_equal(match_keyword("error-app-tag"), YANG_ERROR_APP_TAG);
+    assert_int_equal(match_keyword("error-message"), YANG_ERROR_MESSAGE);
+    assert_int_equal(match_keyword("extension"), YANG_EXTENSION);
+    assert_int_equal(match_keyword("feature"), YANG_FEATURE);
+    assert_int_equal(match_keyword("fraction-digits"), YANG_FRACTION_DIGITS);
+    assert_int_equal(match_keyword("grouping"), YANG_GROUPING);
+    assert_int_equal(match_keyword("identity"), YANG_IDENTITY);
+    assert_int_equal(match_keyword("if-feature"), YANG_IF_FEATURE);
+    assert_int_equal(match_keyword("import"), YANG_IMPORT);
+    assert_int_equal(match_keyword("include"), YANG_INCLUDE);
+    assert_int_equal(match_keyword("input"), YANG_INPUT);
+    assert_int_equal(match_keyword("key"), YANG_KEY);
+    assert_int_equal(match_keyword("leaf"), YANG_LEAF);
+    assert_int_equal(match_keyword("leaf-list"), YANG_LEAF_LIST);
+    assert_int_equal(match_keyword("length"), YANG_LENGTH);
+    assert_int_equal(match_keyword("list"), YANG_LIST);
+    assert_int_equal(match_keyword("mandatory"), YANG_MANDATORY);
+    assert_int_equal(match_keyword("max-elements"), YANG_MAX_ELEMENTS);
+    assert_int_equal(match_keyword("min-elements"), YANG_MIN_ELEMENTS);
+    assert_int_equal(match_keyword("modifier"), YANG_MODIFIER);
+    assert_int_equal(match_keyword("module"), YANG_MODULE);
+    assert_int_equal(match_keyword("must"), YANG_MUST);
+    assert_int_equal(match_keyword("namespace"), YANG_NAMESPACE);
+    assert_int_equal(match_keyword("notification"), YANG_NOTIFICATION);
+    assert_int_equal(match_keyword("ordered-by"), YANG_ORDERED_BY);
+    assert_int_equal(match_keyword("organization"), YANG_ORGANIZATION);
+    assert_int_equal(match_keyword("output"), YANG_OUTPUT);
+    assert_int_equal(match_keyword("path"), YANG_PATH);
+    assert_int_equal(match_keyword("pattern"), YANG_PATTERN);
+    assert_int_equal(match_keyword("position"), YANG_POSITION);
+    assert_int_equal(match_keyword("prefix"), YANG_PREFIX);
+    assert_int_equal(match_keyword("presence"), YANG_PRESENCE);
+    assert_int_equal(match_keyword("range"), YANG_RANGE);
+    assert_int_equal(match_keyword("reference"), YANG_REFERENCE);
+    assert_int_equal(match_keyword("refine"), YANG_REFINE);
+    assert_int_equal(match_keyword("require-instance"), YANG_REQUIRE_INSTANCE);
+    assert_int_equal(match_keyword("revision"), YANG_REVISION);
+    assert_int_equal(match_keyword("revision-date"), YANG_REVISION_DATE);
+    assert_int_equal(match_keyword("rpc"), YANG_RPC);
+    assert_int_equal(match_keyword("status"), YANG_STATUS);
+    assert_int_equal(match_keyword("submodule"), YANG_SUBMODULE);
+    assert_int_equal(match_keyword("type"), YANG_TYPE);
+    assert_int_equal(match_keyword("typedef"), YANG_TYPEDEF);
+    assert_int_equal(match_keyword("unique"), YANG_UNIQUE);
+    assert_int_equal(match_keyword("units"), YANG_UNITS);
+    assert_int_equal(match_keyword("uses"), YANG_USES);
+    assert_int_equal(match_keyword("value"), YANG_VALUE);
+    assert_int_equal(match_keyword("when"), YANG_WHEN);
+    assert_int_equal(match_keyword("yang-version"), YANG_YANG_VERSION);
+    assert_int_equal(match_keyword("yin-element"), YANG_YIN_ELEMENT);
+}
+
+static void
+test_match_argument(void **state)
+{
+    (void)state;
+
+    assert_int_equal(match_argument_name("", 5), YIN_ARG_NONE);
+    assert_int_equal(match_argument_name("qwertyasd", 5), YIN_ARG_NONE);
+    assert_int_equal(match_argument_name("conditionasd", 8), YIN_ARG_NONE);
+    assert_int_equal(match_argument_name("condition", 9), YIN_ARG_CONDITION);
+    assert_int_equal(match_argument_name("date", 4), YIN_ARG_DATE);
+    assert_int_equal(match_argument_name("module", 6), YIN_ARG_MODULE);
+    assert_int_equal(match_argument_name("name", 4), YIN_ARG_NAME);
+    assert_int_equal(match_argument_name("tag", 3), YIN_ARG_TAG);
+    assert_int_equal(match_argument_name("target-node", 11), YIN_ARG_TARGET_NODE);
+    assert_int_equal(match_argument_name("text", 4), YIN_ARG_TEXT);
+    assert_int_equal(match_argument_name("uri", 3), YIN_ARG_URI);
+    assert_int_equal(match_argument_name("value", 5), YIN_ARG_VALUE);
+}
+
+int
+main(void)
+{
+
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test_setup(test_parse, test_setup),
+        cmocka_unit_test(test_match_keyword),
+        cmocka_unit_test(test_match_argument),
+    };
+
+    return cmocka_run_group_tests(tests, NULL, NULL);
+}