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:
parent
b9b1c18cd7
commit
95ec49cb70
1 changed files with 22 additions and 46 deletions
|
@ -168,75 +168,51 @@ error:
|
||||||
return NULL;
|
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)
|
static void *lexer_boolean(struct lexer *lexer)
|
||||||
{
|
{
|
||||||
backup(lexer);
|
backup(lexer);
|
||||||
|
|
||||||
switch (next(lexer)) {
|
switch (next(lexer)) {
|
||||||
case 't':
|
case 't':
|
||||||
if (next(lexer) != 'r') {
|
if (!accept_run(lexer, "rue")) {
|
||||||
goto error;
|
emit(lexer, JSON_TOK_TRUE);
|
||||||
|
return lexer_json;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
if (next(lexer) != 'u') {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (next(lexer) != 'e') {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit(lexer, JSON_TOK_TRUE);
|
|
||||||
return lexer_json;
|
|
||||||
case 'f':
|
case 'f':
|
||||||
if (next(lexer) != 'a') {
|
if (!accept_run(lexer, "alse")) {
|
||||||
goto error;
|
emit(lexer, JSON_TOK_FALSE);
|
||||||
|
return lexer_json;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
if (next(lexer) != 'l') {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (next(lexer) != 's') {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (next(lexer) != 'e') {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit(lexer, JSON_TOK_FALSE);
|
|
||||||
return lexer_json;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
error:
|
|
||||||
emit(lexer, JSON_TOK_ERROR);
|
emit(lexer, JSON_TOK_ERROR);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *lexer_null(struct lexer *lexer)
|
static void *lexer_null(struct lexer *lexer)
|
||||||
{
|
{
|
||||||
if (next(lexer) != 'u') {
|
if (accept_run(lexer, "ull") < 0) {
|
||||||
goto error;
|
emit(lexer, JSON_TOK_ERROR);
|
||||||
}
|
return NULL;
|
||||||
|
|
||||||
if (next(lexer) != 'l') {
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (next(lexer) != 'l') {
|
|
||||||
goto error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(lexer, JSON_TOK_NULL);
|
emit(lexer, JSON_TOK_NULL);
|
||||||
return lexer_json;
|
return lexer_json;
|
||||||
|
|
||||||
error:
|
|
||||||
emit(lexer, JSON_TOK_ERROR);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void *lexer_number(struct lexer *lexer)
|
static void *lexer_number(struct lexer *lexer)
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue