@@ -1280,6 +1280,28 @@ PyToken_ThreeChars(int c1, int c2, int c3)
1280
1280
return OP ;
1281
1281
}
1282
1282
1283
+ static int
1284
+ syntaxerror (struct tok_state * tok , const char * format , ...)
1285
+ {
1286
+ #ifndef PGEN
1287
+ va_list vargs ;
1288
+ #ifdef HAVE_STDARG_PROTOTYPES
1289
+ va_start (vargs , format );
1290
+ #else
1291
+ va_start (vargs );
1292
+ #endif
1293
+ PyErr_FormatV (PyExc_SyntaxError , format , vargs );
1294
+ va_end (vargs );
1295
+ PyErr_SyntaxLocationObject (tok -> filename ,
1296
+ tok -> lineno ,
1297
+ tok -> cur - tok -> line_start );
1298
+ tok -> done = E_ERROR ;
1299
+ #else
1300
+ tok -> done = E_TOKEN ;
1301
+ #endif
1302
+ return ERRORTOKEN ;
1303
+ }
1304
+
1283
1305
static int
1284
1306
indenterror (struct tok_state * tok )
1285
1307
{
@@ -1333,8 +1355,8 @@ tok_decimal_tail(struct tok_state *tok)
1333
1355
}
1334
1356
c = tok_nextc (tok );
1335
1357
if (!isdigit (c )) {
1336
- tok -> done = E_TOKEN ;
1337
1358
tok_backup (tok , c );
1359
+ syntaxerror (tok , "invalid decimal literal" );
1338
1360
return 0 ;
1339
1361
}
1340
1362
}
@@ -1562,9 +1584,8 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
1562
1584
c = tok_nextc (tok );
1563
1585
}
1564
1586
if (!isxdigit (c )) {
1565
- tok -> done = E_TOKEN ;
1566
1587
tok_backup (tok , c );
1567
- return ERRORTOKEN ;
1588
+ return syntaxerror ( tok , "invalid hexadecimal literal" ) ;
1568
1589
}
1569
1590
do {
1570
1591
c = tok_nextc (tok );
@@ -1579,14 +1600,23 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
1579
1600
c = tok_nextc (tok );
1580
1601
}
1581
1602
if (c < '0' || c >= '8' ) {
1582
- tok -> done = E_TOKEN ;
1583
1603
tok_backup (tok , c );
1584
- return ERRORTOKEN ;
1604
+ if (isdigit (c )) {
1605
+ return syntaxerror (tok ,
1606
+ "invalid digit '%c' in octal literal" , c );
1607
+ }
1608
+ else {
1609
+ return syntaxerror (tok , "invalid octal literal" );
1610
+ }
1585
1611
}
1586
1612
do {
1587
1613
c = tok_nextc (tok );
1588
1614
} while ('0' <= c && c < '8' );
1589
1615
} while (c == '_' );
1616
+ if (isdigit (c )) {
1617
+ return syntaxerror (tok ,
1618
+ "invalid digit '%c' in octal literal" , c );
1619
+ }
1590
1620
}
1591
1621
else if (c == 'b' || c == 'B' ) {
1592
1622
/* Binary */
@@ -1596,14 +1626,23 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
1596
1626
c = tok_nextc (tok );
1597
1627
}
1598
1628
if (c != '0' && c != '1' ) {
1599
- tok -> done = E_TOKEN ;
1600
1629
tok_backup (tok , c );
1601
- return ERRORTOKEN ;
1630
+ if (isdigit (c )) {
1631
+ return syntaxerror (tok ,
1632
+ "invalid digit '%c' in binary literal" , c );
1633
+ }
1634
+ else {
1635
+ return syntaxerror (tok , "invalid binary literal" );
1636
+ }
1602
1637
}
1603
1638
do {
1604
1639
c = tok_nextc (tok );
1605
1640
} while (c == '0' || c == '1' );
1606
1641
} while (c == '_' );
1642
+ if (isdigit (c )) {
1643
+ return syntaxerror (tok ,
1644
+ "invalid digit '%c' in binary literal" , c );
1645
+ }
1607
1646
}
1608
1647
else {
1609
1648
int nonzero = 0 ;
@@ -1613,9 +1652,8 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
1613
1652
if (c == '_' ) {
1614
1653
c = tok_nextc (tok );
1615
1654
if (!isdigit (c )) {
1616
- tok -> done = E_TOKEN ;
1617
1655
tok_backup (tok , c );
1618
- return ERRORTOKEN ;
1656
+ return syntaxerror ( tok , "invalid decimal literal" ) ;
1619
1657
}
1620
1658
}
1621
1659
if (c != '0' ) {
@@ -1642,9 +1680,11 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
1642
1680
}
1643
1681
else if (nonzero ) {
1644
1682
/* Old-style octal: now disallowed. */
1645
- tok -> done = E_TOKEN ;
1646
1683
tok_backup (tok , c );
1647
- return ERRORTOKEN ;
1684
+ return syntaxerror (tok ,
1685
+ "leading zeros in decimal integer "
1686
+ "literals are not permitted; "
1687
+ "use an 0o prefix for octal integers" );
1648
1688
}
1649
1689
}
1650
1690
}
@@ -1676,9 +1716,8 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
1676
1716
if (c == '+' || c == '-' ) {
1677
1717
c = tok_nextc (tok );
1678
1718
if (!isdigit (c )) {
1679
- tok -> done = E_TOKEN ;
1680
1719
tok_backup (tok , c );
1681
- return ERRORTOKEN ;
1720
+ return syntaxerror ( tok , "invalid decimal literal" ) ;
1682
1721
}
1683
1722
} else if (!isdigit (c )) {
1684
1723
tok_backup (tok , c );
0 commit comments