yang parser CHANGE improve readability
hide module's substatements order check into a local macro
diff --git a/src/common.h b/src/common.h
index 13f20aa..f5147a0 100644
--- a/src/common.h
+++ b/src/common.h
@@ -136,21 +136,6 @@
* Parsers
*****************************************************************************/
-enum yang_module_stmt {
- Y_MOD_MODULE_HEADER,
- Y_MOD_LINKAGE,
- Y_MOD_META,
- Y_MOD_REVISION,
- Y_MOD_BODY
-};
-
-enum yang_arg {
- Y_IDENTIF_ARG, /**< YANG "identifier-arg-str" rule */
- Y_PREF_IDENTIF_ARG, /**< YANG "identifier-ref-arg-str" rule */
- Y_STR_ARG, /**< YANG "string" rule */
- Y_MAYBE_STR_ARG /**< optional YANG "string" rule */
-};
-
enum yang_keyword {
YANG_NONE = 0,
YANG_ACTION,
diff --git a/src/parser_yang.c b/src/parser_yang.c
index b098a45..69482e0 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -4415,6 +4415,9 @@
YANG_READ_SUBSTMT_FOR(ctx, data, kw, word, word_len, ret) {
LY_CHECK_RET(ret);
+#define CHECK_ORDER(SECTION) \
+ if (mod_stmt > SECTION) {LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw)); return LY_EVALID;}mod_stmt = SECTION
+
switch (kw) {
/* module header */
case YANG_NAMESPACE:
@@ -4423,49 +4426,35 @@
LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "submodule");
return LY_EVALID;
}
- /* fallthrough */
+ CHECK_ORDER(Y_MOD_MODULE_HEADER);
+ break;
case YANG_BELONGS_TO:
- if ((kw == YANG_BELONGS_TO) && !mod->submodule) {
+ if (!mod->submodule) {
LOGVAL_YANG(ctx, LY_VCODE_INCHILDSTMT, ly_stmt2str(kw), "module");
return LY_EVALID;
}
- /* fallthrough */
+ CHECK_ORDER(Y_MOD_MODULE_HEADER);
+ break;
case YANG_YANG_VERSION:
- if (mod_stmt > Y_MOD_MODULE_HEADER) {
- LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw));
- return LY_EVALID;
- }
+ CHECK_ORDER(Y_MOD_MODULE_HEADER);
break;
/* linkage */
case YANG_INCLUDE:
case YANG_IMPORT:
- if (mod_stmt > Y_MOD_LINKAGE) {
- LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw));
- return LY_EVALID;
- }
- mod_stmt = Y_MOD_LINKAGE;
+ CHECK_ORDER(Y_MOD_LINKAGE);
break;
/* meta */
case YANG_ORGANIZATION:
case YANG_CONTACT:
case YANG_DESCRIPTION:
case YANG_REFERENCE:
- if (mod_stmt > Y_MOD_META) {
- LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw));
- return LY_EVALID;
- }
- mod_stmt = Y_MOD_META;
+ CHECK_ORDER(Y_MOD_META);
break;
/* revision */
case YANG_REVISION:
- if (mod_stmt > Y_MOD_REVISION) {
- LOGVAL_YANG(ctx, LY_VCODE_INORD, ly_stmt2str(kw), ly_stmt2str(prev_kw));
- return LY_EVALID;
- }
- mod_stmt = Y_MOD_REVISION;
+ CHECK_ORDER(Y_MOD_REVISION);
break;
-
/* body */
case YANG_ANYDATA:
case YANG_ANYXML:
@@ -4491,8 +4480,9 @@
/* error handled in the next switch */
break;
}
- prev_kw = kw;
+#undef CHECK_ORDER
+ prev_kw = kw;
switch (kw) {
/* module header */
case YANG_YANG_VERSION:
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index dda9593..df5823f 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -16,6 +16,27 @@
#define LY_TREE_SCHEMA_INTERNAL_H_
/**
+ * @brief List of YANG statement groups - the (sub)module's substatements
+ */
+enum yang_module_stmt {
+ Y_MOD_MODULE_HEADER,
+ Y_MOD_LINKAGE,
+ Y_MOD_META,
+ Y_MOD_REVISION,
+ Y_MOD_BODY
+};
+
+/**
+ * @brief Types of arguments of YANG statements
+ */
+enum yang_arg {
+ Y_IDENTIF_ARG, /**< YANG "identifier-arg-str" rule */
+ Y_PREF_IDENTIF_ARG, /**< YANG "identifier-ref-arg-str" rule */
+ Y_STR_ARG, /**< YANG "string" rule */
+ Y_MAYBE_STR_ARG /**< optional YANG "string" rule */
+};
+
+/**
* @brief internal context for schema parsers
*/
struct ly_parser_ctx {