Skip to content

Commit ab69f3e

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 9082b27 commit ab69f3e

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[] = {
@@ -10327,7 +10328,13 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1032710328
* identifier ends in the input. If no identifier was found, the return
1032810329
* will be the the input 's' unchanged.
1032910330
*
10330-
* If the identifier is illegal, the function croaks.
10331+
* If the identifier is illegal, the function croaks unless this flag is
10332+
* passed in: */
10333+
const bool check_only = flags & CHECK_ONLY;
10334+
10335+
/* In this case NULL is returned instead of croaking, and the contents
10336+
* of '*d' are undefined.
10337+
*
1033110338
* The possible reasons for failure are:
1033210339
* 1) There is not enough room for the entire source identifier to be
1033310340
* copied
@@ -10396,8 +10403,12 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1039610403
} while (isDIGIT_A(*s));
1039710404

1039810405
/* Leading zeros are not permitted */
10399-
if (is_zero && *d - digit_start > 1)
10406+
if (is_zero && *d - digit_start > 1) {
10407+
if (check_only) {
10408+
return NULL;
10409+
}
1040010410
croak(ident_var_zero_multi_digit);
10411+
}
1040110412

1040210413
break;
1040310414
}
@@ -10446,6 +10457,10 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1044610457
return (char *) s;
1044710458

1044810459
too_long:
10460+
if (check_only) {
10461+
return NULL;
10462+
}
10463+
1044910464
croak("%s", ident_too_long);
1045010465
}
1045110466

0 commit comments

Comments
 (0)