Refactor init_token and token_error.

This commit is contained in:
Dianne Skoll
2024-09-12 10:58:35 -04:00
parent b2d47ae979
commit 3389f1c91b

View File

@@ -119,6 +119,21 @@ Token TokArray[] = {
static int TokStrCmp (Token const *t, char const *s); static int TokStrCmp (Token const *t, char const *s);
static void
init_token(Token *t)
{
t->name = NULL;
t->type = T_Illegal;
t->val = 0;
}
static void
token_error(Token *t, int errcode)
{
t->type = T_Illegal;
t->val = -errcode;
}
/***************************************************************/ /***************************************************************/
/* */ /* */
/* FindInitialToken */ /* FindInitialToken */
@@ -132,8 +147,7 @@ char const *FindInitialToken(Token *tok, char const *s)
DynamicBuffer buf; DynamicBuffer buf;
DBufInit(&buf); DBufInit(&buf);
tok->type = T_Illegal; init_token(tok);
tok->val = 0;
while (isempty(*s)) s++; while (isempty(*s)) s++;
@@ -160,8 +174,7 @@ void FindToken(char const *s, Token *tok)
int top, bot, mid, r, max; int top, bot, mid, r, max;
int l; int l;
tok->type = T_Illegal; init_token(tok);
tok->val = 0;
if (! *s) { if (! *s) {
tok->type = T_Empty; tok->type = T_Empty;
return; return;
@@ -237,8 +250,7 @@ void FindNumericToken(char const *s, Token *t)
int ampm = 0; int ampm = 0;
int r; int r;
t->type = T_Illegal; init_token(t);
t->val = 0;
if (isdigit(*s)) { if (isdigit(*s)) {
PARSENUM(t->val, s); PARSENUM(t->val, s);
@@ -264,9 +276,7 @@ void FindNumericToken(char const *s, Token *t)
t->type = T_DateTime; t->type = T_DateTime;
t->val = MINUTES_PER_DAY * dse + tim; t->val = MINUTES_PER_DAY * dse + tim;
} else { } else {
t->type = T_Illegal; token_error(t, r);
/* Store error message negated as val! */
t->val = -r;
} }
return; return;
} }
@@ -285,15 +295,13 @@ void FindNumericToken(char const *s, Token *t)
s++; s++;
hour = t->val; hour = t->val;
if (!isdigit(*s)) { if (!isdigit(*s)) {
t->type = T_Illegal; token_error(t, E_BAD_TIME);
t->val = -E_BAD_TIME;
return; return;
} }
PARSENUM(min, s); PARSENUM(min, s);
if (min > 59) { if (min > 59) {
/* Illegal time */ /* Illegal time */
t->type = T_Illegal; token_error(t, E_BAD_TIME);
t->val = -E_BAD_TIME;
return; return;
} }
/* Check for p[m] or a[m] */ /* Check for p[m] or a[m] */
@@ -305,14 +313,12 @@ void FindNumericToken(char const *s, Token *t)
} }
} }
if (*s) { if (*s) {
t->type = T_Illegal; token_error(t, E_BAD_TIME);
t->val = -E_BAD_TIME;
return; return;
} }
if (ampm) { if (ampm) {
if (hour < 1 || hour > 12) { if (hour < 1 || hour > 12) {
t->type = T_Illegal; token_error(t, E_BAD_TIME);
t->val = -E_BAD_TIME;
return; return;
} }
if (ampm == 'a') { if (ampm == 'a') {
@@ -337,8 +343,7 @@ void FindNumericToken(char const *s, Token *t)
/* If we hit a non-digit, error! */ /* If we hit a non-digit, error! */
if (*s) { if (*s) {
t->type = T_Illegal; token_error(t, E_BAD_NUMBER);
t->val = -E_BAD_NUMBER;
return; return;
} }
@@ -355,8 +360,7 @@ void FindNumericToken(char const *s, Token *t)
PARSENUM(t->val, s); PARSENUM(t->val, s);
if (*s) { if (*s) {
/* Illegal token if followed by non-numeric char */ /* Illegal token if followed by non-numeric char */
t->type = T_Illegal; token_error(t, E_BAD_NUMBER);
t->val = -E_BAD_NUMBER;
return; return;
} }
t->type = T_Rep; t->type = T_Rep;
@@ -368,8 +372,7 @@ void FindNumericToken(char const *s, Token *t)
PARSENUM(t->val, s); PARSENUM(t->val, s);
if (*s) { if (*s) {
/* Illegal token if followed by non-numeric char */ /* Illegal token if followed by non-numeric char */
t->type = T_Illegal; token_error(t, E_BAD_NUMBER);
t->val = -E_BAD_NUMBER;
return; return;
} }
t->type = T_Delta; t->type = T_Delta;
@@ -381,8 +384,7 @@ void FindNumericToken(char const *s, Token *t)
PARSENUM(t->val, s); PARSENUM(t->val, s);
if (*s) { if (*s) {
/* Illegal token if followed by non-numeric char */ /* Illegal token if followed by non-numeric char */
t->type = T_Illegal; token_error(t, E_BAD_NUMBER);
t->val = -E_BAD_NUMBER;
return; return;
} }
t->type = T_Back; t->type = T_Back;
@@ -395,8 +397,7 @@ void FindNumericToken(char const *s, Token *t)
PARSENUM(t->val, s); PARSENUM(t->val, s);
if (*s) { if (*s) {
/* Illegal token if followed by non-numeric char */ /* Illegal token if followed by non-numeric char */
t->type = T_Illegal; token_error(t, E_BAD_NUMBER);
t->val = -E_BAD_NUMBER;
return; return;
} }
t->type = T_BackAdj; t->type = T_BackAdj;