lib: json: Simplify lexing of "true", "false", and "null" tokens

Roll the loop in an accept_run() function and use it to match "rue" and
"alse" depending on the first character of the token.  Use that to lex
"ull" after finding "n" as well.  This reduces the code slightly.

Jira: ZEP-1607
Change-Id: Iec8ff6ae2fb79e7fe65d476d1574c5943d23e14f
Signed-off-by: Leandro Pereira <leandro.pereira@intel.com>
This commit is contained in:
Leandro Pereira 2017-03-16 10:11:00 -07:00 committed by Anas Nashif
commit 95ec49cb70

View file

@ -168,75 +168,51 @@ error:
return NULL;
}
static int accept_run(struct lexer *lexer, const char *run)
{
for (; *run; run++) {
if (next(lexer) != *run) {
return -EINVAL;
}
}
return 0;
}
static void *lexer_boolean(struct lexer *lexer)
{
backup(lexer);
switch (next(lexer)) {
case 't':
if (next(lexer) != 'r') {
goto error;
}
if (next(lexer) != 'u') {
goto error;
}
if (next(lexer) != 'e') {
goto error;
}
if (!accept_run(lexer, "rue")) {
emit(lexer, JSON_TOK_TRUE);
return lexer_json;
}
break;
case 'f':
if (next(lexer) != 'a') {
goto error;
}
if (next(lexer) != 'l') {
goto error;
}
if (next(lexer) != 's') {
goto error;
}
if (next(lexer) != 'e') {
goto error;
}
if (!accept_run(lexer, "alse")) {
emit(lexer, JSON_TOK_FALSE);
return lexer_json;
}
break;
}
error:
emit(lexer, JSON_TOK_ERROR);
return NULL;
}
static void *lexer_null(struct lexer *lexer)
{
if (next(lexer) != 'u') {
goto error;
}
if (next(lexer) != 'l') {
goto error;
}
if (next(lexer) != 'l') {
goto error;
if (accept_run(lexer, "ull") < 0) {
emit(lexer, JSON_TOK_ERROR);
return NULL;
}
emit(lexer, JSON_TOK_NULL);
return lexer_json;
error:
emit(lexer, JSON_TOK_ERROR);
return NULL;
}
static void *lexer_number(struct lexer *lexer)
{
while (true) {