parser_yin ADD keyword and module validation
work in progress
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f15b60f..3d5e883 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -160,6 +160,7 @@
     src/tree_schema.c
     src/tree_schema_helpers.c
     src/parser_yang.c
+    src/parser_yin.c
     src/xml.c)
 
 #set(lintsrc
diff --git a/src/parser_yin.c b/src/parser_yin.c
index 92118e9..29fa079 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -1,15 +1,262 @@
 /**
  * @file parser_yin.c
  * @author David Sedlák <xsedla1d@stud.fit.vutbr.cz>
- * @brief YIN parser
+ * @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 "common.h"
 #include "context.h"
 #include "libyang.h"
+#include "xml.h"
+#include "tree_schema_internal.h"
+#include "string.h"
 
 LY_ERR
-parse_yin(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
+match_argument_name()
 {
 
 }
+
+#define MOVE_START(array, offset) array = &array[offset];
+#define IF_KW(STR, LEN, SHIFT, STMT) if (!strncmp((word), STR, LEN)) {MOVE_START(word, SHIFT);kw=STMT;}
+#define IF_KW_PREFIX(STR, LEN, SHIFT) if (!strncmp((word), STR, LEN)) {MOVE_START(word, SHIFT);
+#define IF_KW_PREFIX_END }
+
+enum yang_keyword
+match_keyword(const char *word, size_t len)
+{
+    if (!word) {
+        return YANG_NONE;
+    }
+    enum yang_keyword kw = YANG_NONE;
+
+    /* try to match the keyword */
+    switch (*word) {
+    case 'a':
+        MOVE_START(word, 1);
+        IF_KW("rgument", len, 7, YANG_ARGUMENT)
+        else IF_KW("ugment", len, 6, YANG_AUGMENT)
+        else IF_KW("ction", len, 5, YANG_ACTION)
+        else IF_KW_PREFIX("ny", len, 2)
+            IF_KW("data", len, 4, YANG_ANYDATA)
+            else IF_KW("xml", len, 3, YANG_ANYXML)
+        IF_KW_PREFIX_END
+        break;
+    case 'b':
+        MOVE_START(word, 1);
+        IF_KW("ase", len, 3, YANG_BASE)
+        else IF_KW("elongs-to", len, 9, YANG_BELONGS_TO)
+        else IF_KW("it", len, 2, YANG_BIT)
+        break;
+    case 'c':
+        MOVE_START(word, 1);
+        IF_KW("ase", len, 3, YANG_CASE)
+        else IF_KW("hoice", len, 5, YANG_CHOICE)
+        else IF_KW_PREFIX("on", len, 2)
+            IF_KW("fig", len, 3, YANG_CONFIG)
+            else IF_KW_PREFIX("ta", len, 2)
+                IF_KW("ct", len, 2, YANG_CONTACT)
+                else IF_KW("iner", len, 4, YANG_CONTAINER)
+            IF_KW_PREFIX_END
+        IF_KW_PREFIX_END
+        break;
+    case 'd':
+        MOVE_START(word, 1);
+        IF_KW_PREFIX("e", len, 1)
+            IF_KW("fault", len, 5, YANG_DEFAULT)
+            else IF_KW("scription", len, 9, YANG_DESCRIPTION)
+            else IF_KW_PREFIX("viat", len, 4)
+                IF_KW("e", len, 1, YANG_DEVIATE)
+                else IF_KW("ion", len, 3, YANG_DEVIATION)
+            IF_KW_PREFIX_END
+        IF_KW_PREFIX_END
+        break;
+    case 'e':
+        MOVE_START(word, 1);
+        IF_KW("num", len, 3, YANG_ENUM)
+        else IF_KW_PREFIX("rror-", len, 5)
+            IF_KW("app-tag", len, 7, YANG_ERROR_APP_TAG)
+            else IF_KW("message", len, 7, YANG_ERROR_MESSAGE)
+        IF_KW_PREFIX_END
+        else IF_KW("xtension", len, 8, YANG_EXTENSION)
+        break;
+    case 'f':
+        MOVE_START(word, 1);
+        IF_KW("eature", len, 6, YANG_FEATURE)
+        else IF_KW("raction-digits", len, 14, YANG_FRACTION_DIGITS)
+        break;
+    case 'g':
+        MOVE_START(word, 1);
+        IF_KW("rouping", len, 7, YANG_GROUPING)
+        break;
+    case 'i':
+        MOVE_START(word, 1);
+        IF_KW("dentity", len, 7, YANG_IDENTITY)
+        else IF_KW("f-feature", len, 9, YANG_IF_FEATURE)
+        else IF_KW("mport", len, 5, YANG_IMPORT)
+        else IF_KW_PREFIX("n", len, 1)
+            IF_KW("clude", len, 5, YANG_INCLUDE)
+            else IF_KW("put", len, 3, YANG_INPUT)
+        IF_KW_PREFIX_END
+        break;
+    case 'k':
+        MOVE_START(word, 1);
+        IF_KW("ey", len, 2, YANG_KEY)
+        break;
+    case 'l':
+        MOVE_START(word, 1);
+        IF_KW_PREFIX("e", len, 1)
+            IF_KW("af-list", len, 7, YANG_LEAF_LIST)
+            else IF_KW("af", len, 2, YANG_LEAF)
+            else IF_KW("ngth", len, 4, YANG_LENGTH)
+        IF_KW_PREFIX_END
+        else IF_KW("ist", len, 3, YANG_LIST)
+        break;
+    case 'm':
+        MOVE_START(word, 1);
+        IF_KW_PREFIX("a", len, 1)
+            IF_KW("ndatory", len, 7, YANG_MANDATORY)
+            else IF_KW("x-elements", len, 10, YANG_MAX_ELEMENTS)
+        IF_KW_PREFIX_END
+        else IF_KW("in-elements", len, 11, YANG_MIN_ELEMENTS)
+        else IF_KW("ust", len, 3, YANG_MUST)
+        else IF_KW_PREFIX("od", len, 2)
+            IF_KW("ule", len, 3, YANG_MODULE)
+            else IF_KW("ifier", len, 5, YANG_MODIFIER)
+        IF_KW_PREFIX_END
+        break;
+    case 'n':
+        MOVE_START(word, 1);
+        IF_KW("amespace", len, 8, YANG_NAMESPACE)
+        else IF_KW("otification", len, 11, YANG_NOTIFICATION)
+        break;
+    case 'o':
+        MOVE_START(word, 1);
+        IF_KW_PREFIX("r", len, 1)
+            IF_KW("dered-by", len, 8, YANG_ORDERED_BY)
+            else IF_KW("ganization", len, 10, YANG_ORGANIZATION)
+        IF_KW_PREFIX_END
+        else IF_KW("utput", len, 5, YANG_OUTPUT)
+        break;
+    case 'p':
+        MOVE_START(word, 1);
+        IF_KW("ath", len, 3, YANG_PATH)
+        else IF_KW("attern", len, 6, YANG_PATTERN)
+        else IF_KW("osition", len, 7, YANG_POSITION)
+        else IF_KW_PREFIX("re", len, 2)
+            IF_KW("fix", len, 3, YANG_PREFIX)
+            else IF_KW("sence", len, 5, YANG_PRESENCE)
+        IF_KW_PREFIX_END
+        break;
+    case 'r':
+        MOVE_START(word, 1);
+        IF_KW("ange", len, 4, YANG_RANGE)
+        else IF_KW_PREFIX("e", len, 1)
+            IF_KW_PREFIX("f", len, 1)
+                IF_KW("erence", len, 6, YANG_REFERENCE)
+                else IF_KW("ine", len, 3, YANG_REFINE)
+            IF_KW_PREFIX_END
+            else IF_KW("quire-instance", len, 14, YANG_REQUIRE_INSTANCE)
+            else IF_KW("vision-date", len, 11, YANG_REVISION_DATE)
+            else IF_KW("vision", len, 6, YANG_REVISION)
+        IF_KW_PREFIX_END
+        else IF_KW("pc", len, 2, YANG_RPC)
+        break;
+    case 's':
+        MOVE_START(word, 1);
+        IF_KW("tatus", len, 5, YANG_STATUS)
+        else IF_KW("ubmodule", len, 8, YANG_SUBMODULE)
+        break;
+    case 't':
+        MOVE_START(word, 1);
+        IF_KW("ypedef", len, 6, YANG_TYPEDEF)
+        else IF_KW("ype", len, 3, YANG_TYPE)
+        break;
+    case 'u':
+        MOVE_START(word, 1);
+        IF_KW_PREFIX("ni", len, 2)
+            IF_KW("que", len, 3, YANG_UNIQUE)
+            else IF_KW("ts", len, 2, YANG_UNITS)
+        IF_KW_PREFIX_END
+        else IF_KW("ses", len, 3, YANG_USES)
+        break;
+    case 'v':
+        MOVE_START(word, 1);
+        IF_KW("alue", len, 4, YANG_VALUE)
+        break;
+    case 'w':
+        MOVE_START(word, 1);
+        IF_KW("hen", len, 3, YANG_WHEN)
+        break;
+    case 'y':
+        MOVE_START(word, 1);
+        IF_KW("ang-version", len, 11, YANG_YANG_VERSION)
+        else IF_KW("in-element", len, 10, YANG_YIN_ELEMENT)
+        break;
+    default:
+        break;
+    }
+
+    return kw;
+}
+
+LY_ERR
+parse_submodule(struct lyxml_context *xml_ctx, const char **data, struct lysp_module **mod_p)
+{
+    LY_ERR ret = 0;
+
+    const char *prefix, *name, *elem;
+    size_t prefix_len, name_len;
+
+    /* inefficient keyword check - just temporary */
+    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);
+    if ((strncmp("module", name, name_len) != 0) && (strncmp("submodule", name, name_len) != 0)) {
+        LOGVAL(xml_ctx->ctx, xml_ctx->line, &xml_ctx->line, LYVE_SYNTAX, "Invalid keyword \"%s\", expected \"module\" or \"submodule\".", name);
+    }
+
+    /* inefficient argument name check - just temporary */
+    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 (strncmp("name", name, name_len)) {
+        LOGVAL(xml_ctx->ctx, xml_ctx->line, &xml_ctx->line, LYVE_SYNTAX, "Invalid argument name \"%s\", expected \"name\".", name);
+    }
+
+    char *buf = NULL, *out = NULL;
+    size_t buf_len = 0, out_len = 0;
+    int dynamic;
+
+    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);
+    return 0;
+}
+
+LY_ERR
+yin_parse(struct ly_ctx *ctx, const char *data, struct lysp_module **mod_p)
+{
+    LY_ERR ret = 0;
+    struct ly_parser_ctx context = {0};
+
+    struct lyxml_context xml_ctx;
+    xml_ctx.ctx = ctx;
+
+    const char *prefix, *name, *elem;
+    size_t prefix_len, name_len;
+
+    parse_submodule(&xml_ctx, &data, mod_p);
+
+    return LY_SUCCESS;
+}
diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt
index 04d0d0d..583e1f4 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..572fea8
--- /dev/null
+++ b/tests/src/test_parser_yin.c
@@ -0,0 +1,121 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libyang.h"
+#include "tree_schema_internal.h"
+#include "../../src/parser_yin.c"
+
+static void
+test(void **state)
+{
+    struct ly_ctx *ctx;
+    struct lysp_module *mod;
+    ly_ctx_new(NULL, 0, &ctx);
+    mod = malloc(sizeof(*mod));
+
+    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\">",
+                &mod);
+    (void)state; /* unused */
+    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", 7), 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);
+}
+
+int
+main(void)
+{
+
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test_setup(test, test_setup),
+        cmocka_unit_test(test_match_keyword),
+    };
+
+    return cmocka_run_group_tests(tests, NULL, NULL);
+}