Skip to content

Commit 66b1b8c

Browse files
committed
S_parse_ident: Add flag to check for legality and not croak
When this flag is set, when an illegal identifier is found, instead of croaking the function returns NULL. This will allow future commits to use this function when all that is desired is to determine if the identifier is legal or not.
1 parent 8e404c8 commit 66b1b8c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

toke.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ static const char ident_var_zero_multi_digit[] = "Numeric variables with more th
173173
#define CHECK_DOLLAR (1 << 2)
174174
#define IDFIRST_ONLY (1 << 3)
175175
#define STOP_AT_FIRST_NON_DIGIT (1 << 4)
176+
#define CHECK_ONLY (1 << 5)
176177

177178
#ifdef DEBUGGING
178179
static const char* const lex_state_names[] = {
@@ -10334,7 +10335,13 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1033410335
* identifier ends in the input. If no identifier was found, the return
1033510336
* will be the the input 's' unchanged.
1033610337
*
10337-
* If the identifier is illegal, the function croaks.
10338+
* If the identifier is illegal, the function croaks unless this flag is
10339+
* passed in: */
10340+
const bool check_only = flags & CHECK_ONLY;
10341+
10342+
/* In this case NULL is returned instead of croaking, and the contents
10343+
* of '*d' are undefined.
10344+
*
1033810345
* The possible reasons for failure are:
1033910346
* 1) There is not enough room for the entire source identifier to be
1034010347
* copied
@@ -10403,8 +10410,12 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1040310410
} while (isDIGIT_A(*s));
1040410411

1040510412
/* Leading zeros are not permitted */
10406-
if (is_zero && *d - digit_start > 1)
10413+
if (is_zero && *d - digit_start > 1) {
10414+
if (check_only) {
10415+
return NULL;
10416+
}
1040710417
croak(ident_var_zero_multi_digit);
10418+
}
1040810419

1040910420
break;
1041010421
}
@@ -10453,6 +10464,10 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1045310464
return (char *) s;
1045410465

1045510466
too_long:
10467+
if (check_only) {
10468+
return NULL;
10469+
}
10470+
1045610471
croak("%s", ident_too_long);
1045710472
}
1045810473

0 commit comments

Comments
 (0)