libyang REFACTOR avoid constant literals
Improve readability of the code.
Includes also unification of "no break in the case" comment to "fall
through".
diff --git a/src/parser_yang.c b/src/parser_yang.c
index 28c7785..9df5183 100644
--- a/src/parser_yang.c
+++ b/src/parser_yang.c
@@ -104,8 +104,9 @@
LY_ERR
buf_add_char(struct ly_ctx *ctx, struct ly_in *in, size_t len, char **buf, size_t *buf_len, size_t *buf_used)
{
+#define BUF_STEP 16;
if (*buf_len <= (*buf_used) + len) {
- *buf_len += 16;
+ *buf_len += BUF_STEP;
*buf = ly_realloc(*buf, *buf_len);
LY_CHECK_ERR_RET(!*buf, LOGMEM(ctx), LY_EMEM);
}
@@ -117,6 +118,7 @@
(*buf_used) += len;
return LY_SUCCESS;
+#undef BUF_STEP
}
/**
@@ -223,34 +225,35 @@
LY_ERR
skip_comment(struct lys_yang_parser_ctx *ctx, struct ly_in *in, uint8_t comment)
{
- /* internal statuses: 0 - comment ended,
- * 1 - in line comment,
- * 2 - in block comment,
- * 3 - in block comment with last read character '*'
- */
+ /* internal statuses: */
+#define COMMENT_NO 0 /* comment ended */
+#define COMMENT_LINE 1 /* in line comment */
+#define COMMENT_BLOCK 2 /* in block comment */
+#define COMMENT_BLOCK_END 3 /* in block comment with last read character '*' */
+
while (in->current[0] && comment) {
switch (comment) {
- case 1:
+ case COMMENT_LINE:
if (in->current[0] == '\n') {
- comment = 0;
+ comment = COMMENT_NO;
++ctx->line;
}
break;
- case 2:
+ case COMMENT_BLOCK:
if (in->current[0] == '*') {
- comment = 3;
+ comment = COMMENT_BLOCK_END;
} else if (in->current[0] == '\n') {
++ctx->line;
}
break;
- case 3:
+ case COMMENT_BLOCK_END:
if (in->current[0] == '/') {
- comment = 0;
+ comment = COMMENT_NO;
} else if (in->current[0] != '*') {
if (in->current[0] == '\n') {
++ctx->line;
}
- comment = 2;
+ comment = COMMENT_BLOCK;
}
break;
default:
@@ -265,12 +268,17 @@
++in->current;
}
- if (!in->current[0] && (comment > 1)) {
+ if (!in->current[0] && (comment >= COMMENT_BLOCK)) {
LOGVAL_PARSER(ctx, LYVE_SYNTAX, "Unexpected end-of-input, non-terminated comment.");
return LY_EVALID;
}
return LY_SUCCESS;
+
+#undef COMMENT_NO
+#undef COMMENT_LINE
+#undef COMMENT_BLOCK
+#undef COMMENT_BLOCK_END
}
/**
@@ -293,9 +301,14 @@
read_qstring(struct lys_yang_parser_ctx *ctx, struct ly_in *in, enum yang_arg arg, char **word_p, char **word_b,
size_t *word_len, size_t *buf_len)
{
- /* string: 0 - string ended, 1 - string with ', 2 - string with ", 3 - string with " with last character \,
- * 4 - string finished, now skipping whitespaces looking for +,
- * 5 - string continues after +, skipping whitespaces */
+ /* string parsing status: */
+#define STRING_ENDED 0 /* string ended */
+#define STRING_SINGLE_QUOTED 1 /* string with ' */
+#define STRING_DOUBLE_QUOTED 2 /* string with " */
+#define STRING_DOUBLE_QUOTED_ESCAPED 3 /* string with " with last character \ */
+#define STRING_PAUSED_NEXTSTRING 4 /* string finished, now skipping whitespaces looking for + */
+#define STRING_PAUSED_CONTINUE 5 /* string continues after +, skipping whitespaces */
+
uint8_t string;
uint64_t block_indent = 0, current_indent = 0;
ly_bool need_buf = 0;
@@ -304,21 +317,21 @@
uint64_t trailing_ws = 0; /* current number of stored trailing whitespace characters */
if (in->current[0] == '\"') {
- string = 2;
+ string = STRING_DOUBLE_QUOTED;
current_indent = block_indent = ctx->indent + 1;
} else {
assert(in->current[0] == '\'');
- string = 1;
+ string = STRING_SINGLE_QUOTED;
}
MOVE_INPUT(ctx, in, 1);
while (in->current[0] && string) {
switch (string) {
- case 1:
+ case STRING_SINGLE_QUOTED:
switch (in->current[0]) {
case '\'':
/* string may be finished, but check for + */
- string = 4;
+ string = STRING_PAUSED_NEXTSTRING;
MOVE_INPUT(ctx, in, 1);
break;
default:
@@ -327,17 +340,17 @@
break;
}
break;
- case 2:
+ case STRING_DOUBLE_QUOTED:
switch (in->current[0]) {
case '\"':
/* string may be finished, but check for + */
- string = 4;
+ string = STRING_PAUSED_NEXTSTRING;
MOVE_INPUT(ctx, in, 1);
trailing_ws = 0;
break;
case '\\':
/* special character following */
- string = 3;
+ string = STRING_DOUBLE_QUOTED_ESCAPED;
/* the backslash sequence is substituted, so we will need a buffer to store the result */
need_buf = 1;
@@ -365,8 +378,8 @@
case '\t':
if (current_indent < block_indent) {
assert(need_buf);
- current_indent += 8;
- ctx->indent += 8;
+ current_indent += Y_TAB_SPACES;
+ ctx->indent += Y_TAB_SPACES;
for ( ; current_indent > block_indent; --current_indent, --ctx->indent) {
/* store leftover spaces from the tab */
c = in->current;
@@ -381,7 +394,7 @@
LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
trailing_ws++;
/* additional characters for indentation - only 1 was count in buf_store_char */
- ctx->indent += 7;
+ ctx->indent += Y_TAB_SPACES - 1;
}
break;
case '\n':
@@ -416,7 +429,7 @@
break;
}
break;
- case 3:
+ case STRING_DOUBLE_QUOTED_ESCAPED:
/* string encoded characters */
c = in->current;
switch (in->current[0]) {
@@ -439,19 +452,19 @@
/* check and store character */
LY_CHECK_RET(buf_store_char(ctx, in, arg, word_p, word_len, word_b, buf_len, need_buf, &prefix));
- string = 2;
+ string = STRING_DOUBLE_QUOTED;
in->current = c + 1;
break;
- case 4:
+ case STRING_PAUSED_NEXTSTRING:
switch (in->current[0]) {
case '+':
/* string continues */
- string = 5;
+ string = STRING_PAUSED_CONTINUE;
need_buf = 1;
break;
case '\n':
++ctx->line;
- /* fallthrough */
+ /* fall through */
case ' ':
case '\t':
/* just skip */
@@ -462,20 +475,20 @@
}
MOVE_INPUT(ctx, in, 1);
break;
- case 5:
+ case STRING_PAUSED_CONTINUE:
switch (in->current[0]) {
case '\n':
++ctx->line;
- /* fallthrough */
+ /* fall through */
case ' ':
case '\t':
/* skip */
break;
case '\'':
- string = 1;
+ string = STRING_SINGLE_QUOTED;
break;
case '\"':
- string = 2;
+ string = STRING_DOUBLE_QUOTED;
break;
default:
/* it must be quoted again */
@@ -496,6 +509,13 @@
return LY_EVALID;
}
return LY_SUCCESS;
+
+#undef STRING_ENDED
+#undef STRING_SINGLE_QUOTED
+#undef STRING_DOUBLE_QUOTED
+#undef STRING_DOUBLE_QUOTED_ESCAPED
+#undef STRING_PAUSED_NEXTSTRING
+#undef STRING_PAUSED_CONTINUE
}
/**
@@ -568,7 +588,7 @@
goto str_end;
}
/* tabs count for 8 spaces */
- ctx->indent += 8;
+ ctx->indent += Y_TAB_SPACES;
++in->current;
break;
@@ -673,7 +693,7 @@
break;
case '\t':
/* skip whitespaces (optsep) */
- ctx->indent += 8;
+ ctx->indent += Y_TAB_SPACES;
break;
default:
/* either a keyword start or an invalid character */
@@ -711,7 +731,7 @@
if ((*kw == LY_STMT_INPUT) || (*kw == LY_STMT_OUTPUT)) {
break;
}
- /* fallthrough */
+ /* fall through */
default:
MOVE_INPUT(ctx, in, 1);
LOGVAL_PARSER(ctx, LY_VCODE_INSTREXP, (int)(in->current - word_start), word_start,
@@ -925,7 +945,7 @@
if ((word_len == 1) && !strncmp(word, "1", word_len)) {
*version = LYS_VERSION_1_0;
- } else if ((word_len == 3) && !strncmp(word, "1.1", word_len)) {
+ } else if ((word_len == ly_strlen_const("1.1")) && !strncmp(word, "1.1", word_len)) {
*version = LYS_VERSION_1_1;
} else {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yang-version");
@@ -1329,9 +1349,9 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
- if ((word_len == 4) && !strncmp(word, "true", word_len)) {
+ if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
*flags |= LYS_CONFIG_W;
- } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
+ } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
*flags |= LYS_CONFIG_R;
} else {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "config");
@@ -1379,9 +1399,9 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
- if ((word_len == 4) && !strncmp(word, "true", word_len)) {
+ if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
*flags |= LYS_MAND_TRUE;
- } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
+ } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
*flags |= LYS_MAND_FALSE;
} else {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "mandatory");
@@ -1497,11 +1517,11 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
- if ((word_len == 7) && !strncmp(word, "current", word_len)) {
+ if ((word_len == ly_strlen_const("current")) && !strncmp(word, "current", word_len)) {
*flags |= LYS_STATUS_CURR;
- } else if ((word_len == 10) && !strncmp(word, "deprecated", word_len)) {
+ } else if ((word_len == ly_strlen_const("deprecated")) && !strncmp(word, "deprecated", word_len)) {
*flags |= LYS_STATUS_DEPRC;
- } else if ((word_len == 8) && !strncmp(word, "obsolete", word_len)) {
+ } else if ((word_len == ly_strlen_const("obsolete")) && !strncmp(word, "obsolete", word_len)) {
*flags |= LYS_STATUS_OBSLT;
} else {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "status");
@@ -1680,13 +1700,13 @@
errno = 0;
if (val_kw == LY_STMT_VALUE) {
- num = strtol(word, &ptr, 10);
+ num = strtol(word, &ptr, LY_BASE_DEC);
if ((num < INT64_C(-2147483648)) || (num > INT64_C(2147483647))) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
goto error;
}
} else {
- unum = strtoul(word, &ptr, 10);
+ unum = strtoul(word, &ptr, LY_BASE_DEC);
if (unum > UINT64_C(4294967295)) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, ly_stmt2str(val_kw));
goto error;
@@ -1826,14 +1846,14 @@
}
errno = 0;
- num = strtoul(word, &ptr, 10);
+ num = strtoul(word, &ptr, LY_BASE_DEC);
/* we have not parsed the whole argument */
if ((size_t)(ptr - word) != word_len) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "fraction-digits");
free(buf);
return LY_EVALID;
}
- if ((errno == ERANGE) || (num > 18)) {
+ if ((errno == ERANGE) || (num > LY_TYPE_DEC64_FD_MAX)) {
LOGVAL_PARSER(ctx, LY_VCODE_OOB, word_len, word, "fraction-digits");
free(buf);
return LY_EVALID;
@@ -1883,9 +1903,9 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
- if ((word_len == 4) && !strncmp(word, "true", word_len)) {
+ if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
*reqinst = 1;
- } else if ((word_len != 5) || strncmp(word, "false", word_len)) {
+ } else if ((word_len != ly_strlen_const("false")) || strncmp(word, "false", word_len)) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "require-instance");
free(buf);
return LY_EVALID;
@@ -1924,7 +1944,7 @@
size_t word_len;
enum ly_stmt kw;
- if ((*pat)[0] == 0x15) {
+ if ((*pat)[0] == LYSP_RESTR_PATTERN_NACK) {
LOGVAL_PARSER(ctx, LY_VCODE_DUPSTMT, "modifier");
return LY_EVALID;
}
@@ -1932,7 +1952,7 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
- if ((word_len != 12) || strncmp(word, "invert-match", word_len)) {
+ if ((word_len != ly_strlen_const("invert-match")) || strncmp(word, "invert-match", word_len)) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "modifier");
free(buf);
return LY_EVALID;
@@ -1945,8 +1965,8 @@
strcpy(buf, *pat);
lydict_remove(PARSER_CTX(ctx), *pat);
- assert(buf[0] == 0x06);
- buf[0] = 0x15;
+ assert(buf[0] == LYSP_RESTR_PATTERN_ACK);
+ buf[0] = LYSP_RESTR_PATTERN_NACK;
LY_CHECK_RET(lydict_insert_zc(PARSER_CTX(ctx), buf, pat));
YANG_READ_SUBSTMT_FOR(ctx, in, kw, word, word_len, ret, ) {
@@ -1987,14 +2007,14 @@
/* add special meaning first byte */
if (buf) {
- buf = ly_realloc(buf, word_len + 2);
+ buf = ly_realloc(buf, word_len + 2);
word = buf;
} else {
buf = malloc(word_len + 2);
}
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[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;
@@ -2257,7 +2277,7 @@
if (ly_strncmp("unbounded", word, word_len)) {
errno = 0;
- num = strtoul(word, &ptr, 10);
+ num = strtoul(word, &ptr, LY_BASE_DEC);
/* we have not parsed the whole argument */
if ((size_t)(ptr - word) != word_len) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "max-elements");
@@ -2324,7 +2344,7 @@
}
errno = 0;
- num = strtoul(word, &ptr, 10);
+ num = strtoul(word, &ptr, LY_BASE_DEC);
/* we have not parsed the whole argument */
if ((size_t)(ptr - word) != word_len) {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "min-elements");
@@ -2378,9 +2398,9 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
- if ((word_len == 6) && !strncmp(word, "system", word_len)) {
+ if ((word_len == ly_strlen_const("system")) && !strncmp(word, "system", word_len)) {
*flags |= LYS_ORDBY_SYSTEM;
- } else if ((word_len == 4) && !strncmp(word, "user", word_len)) {
+ } else if ((word_len == ly_strlen_const("user")) && !strncmp(word, "user", word_len)) {
*flags |= LYS_ORDBY_USER;
} else {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "ordered-by");
@@ -3577,9 +3597,9 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
- if ((word_len == 4) && !strncmp(word, "true", word_len)) {
+ if ((word_len == ly_strlen_const("true")) && !strncmp(word, "true", word_len)) {
*flags |= LYS_YINELEM_TRUE;
- } else if ((word_len == 5) && !strncmp(word, "false", word_len)) {
+ } else if ((word_len == ly_strlen_const("false")) && !strncmp(word, "false", word_len)) {
*flags |= LYS_YINELEM_FALSE;
} else {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "yin-element");
@@ -3724,13 +3744,13 @@
/* get value */
LY_CHECK_RET(get_argument(ctx, in, Y_STR_ARG, NULL, &word, &buf, &word_len));
- if ((word_len == 13) && !strncmp(word, "not-supported", word_len)) {
+ if ((word_len == ly_strlen_const("not-supported")) && !strncmp(word, "not-supported", word_len)) {
dev_mod = LYS_DEV_NOT_SUPPORTED;
- } else if ((word_len == 3) && !strncmp(word, "add", word_len)) {
+ } else if ((word_len == ly_strlen_const("add")) && !strncmp(word, "add", word_len)) {
dev_mod = LYS_DEV_ADD;
- } else if ((word_len == 7) && !strncmp(word, "replace", word_len)) {
+ } else if ((word_len == ly_strlen_const("replace")) && !strncmp(word, "replace", word_len)) {
dev_mod = LYS_DEV_REPLACE;
- } else if ((word_len == 6) && !strncmp(word, "delete", word_len)) {
+ } else if ((word_len == ly_strlen_const("delete")) && !strncmp(word, "delete", word_len)) {
dev_mod = LYS_DEV_DELETE;
} else {
LOGVAL_PARSER(ctx, LY_VCODE_INVAL, word_len, word, "deviate");