Skip to content

Commit 84ca7a5

Browse files
committed
Zend: isolate the computation of the token name
Introduce "token_name()" whose only job is to map the kind of a token (say YYSYMBOL_T_IS_SMALLER_OR_EQUAL) to its name "T_IS_SMALLER_OR_EQUAL". In the future, rather than having to cram the token name in its string alias (%token T_IS_SMALLER_OR_EQUAL "<= (T_IS_SMALLER_OR_EQUAL)"), we should use clean aliases (%token T_IS_SMALLER_OR_EQUAL "<=") and generate the mapping for "token_name()" elsewhere.
1 parent b042b48 commit 84ca7a5

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

Zend/zend_language_parser.y

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,29 @@ static unsigned int umin (unsigned int a, unsigned int b)
13491349
return a < b ? a : b;
13501350
}
13511351

1352+
struct string {
1353+
const unsigned char *str;
1354+
unsigned int len;
1355+
};
1356+
1357+
// The name of this token.
1358+
// E.g. YYSYMBOL_T_IS_SMALLER_OR_EQUAL => "T_IS_SMALLER_OR_EQUAL"
1359+
// Or null.
1360+
static
1361+
struct string
1362+
token_name (yysymbol_kind_t kind)
1363+
{
1364+
const char *unexp_str = yysymbol_name (kind);
1365+
const unsigned int unexp_len = (unsigned int)strlen(unexp_str);
1366+
const unsigned char *tok1 = memchr(unexp_str, '(', unexp_len);
1367+
const unsigned char *tok2 = zend_memrchr(unexp_str, ')', unexp_len);
1368+
1369+
struct string res;
1370+
res.str = tok1;
1371+
res.len = tok1 != NULL && tok2 != NULL ? tok2 - tok1 + 1 : 0;
1372+
return res;
1373+
}
1374+
13521375
int
13531376
yyreport_syntax_error (const yypcontext_t *ctx)
13541377
{
@@ -1368,15 +1391,10 @@ yyreport_syntax_error (const yypcontext_t *ctx)
13681391
umin(30, end != NULL ? end - str : LANG_SCNG(yy_leng));
13691392

13701393
// Maybe include the token name.
1371-
// Strings are like "<<= (T_SL_EQUAL)": extract the part in parens.
1372-
const char *unexp_str = yysymbol_name (unexp);
1373-
const unsigned int unexp_len = (unsigned int)strlen(unexp_str);
1374-
const unsigned char *tok1 = memchr(unexp_str, '(', unexp_len);
1375-
const unsigned char *tok2 = zend_memrchr(unexp_str, ')', unexp_len);
1376-
const unsigned int toklen = tok1 != NULL && tok2 != NULL ? tok2 - tok1 + 1 : 0;
1377-
1378-
if (toklen) {
1379-
snprintf(unexpected, sizeof(unexpected), "'%.*s' %.*s", len, str, toklen, tok1);
1394+
struct string token = token_name (unexp);
1395+
1396+
if (token.len) {
1397+
snprintf(unexpected, sizeof(unexpected), "'%.*s' %.*s", len, str, token.len, token.str);
13801398
} else {
13811399
snprintf(unexpected, sizeof(unexpected), "'%.*s'", len, str);
13821400
}

0 commit comments

Comments
 (0)