Skip to content

Commit fec412a

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 23f586a commit fec412a

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
@@ -179,6 +179,7 @@ static const char ident_var_zero_multi_digit[] = "Numeric variables with more th
179179
#define CHECK_DOLLAR (1 << 2)
180180
#define IDFIRST_ONLY (1 << 3)
181181
#define STOP_AT_FIRST_NON_DIGIT (1 << 4)
182+
#define CHECK_ONLY (1 << 5)
182183

183184
#ifdef DEBUGGING
184185
static const char* const lex_state_names[] = {
@@ -10575,7 +10576,13 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1057510576
* identifier ends in the input. If no identifier was found, the return
1057610577
* will be the the input 's' unchanged.
1057710578
*
10578-
* If the identifier is illegal, the function croaks.
10579+
* If the identifier is illegal, the function croaks unless this flag is
10580+
* passed in: */
10581+
const bool check_only = flags & CHECK_ONLY;
10582+
10583+
/* In this case NULL is returned instead of croaking, and the contents
10584+
* of '*d' are undefined.
10585+
*
1057910586
* The possible reasons for failure are:
1058010587
* 1) There is not enough room for the entire source identifier to be
1058110588
* copied
@@ -10644,8 +10651,12 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1064410651
} while (isDIGIT_A(*s));
1064510652

1064610653
/* Leading zeros are not permitted */
10647-
if (is_zero && *d - digit_start > 1)
10654+
if (is_zero && *d - digit_start > 1) {
10655+
if (check_only) {
10656+
return NULL;
10657+
}
1064810658
croak(ident_var_zero_multi_digit);
10659+
}
1064910660

1065010661
break;
1065110662
}
@@ -10694,6 +10705,10 @@ S_parse_ident(pTHX_ const char *s, const char * const s_end,
1069410705
return (char *) s;
1069510706

1069610707
too_long:
10708+
if (check_only) {
10709+
return NULL;
10710+
}
10711+
1069710712
croak("%s", ident_too_long);
1069810713
}
1069910714

0 commit comments

Comments
 (0)