Commit d3787b7b authored by Stefan Behnel's avatar Stefan Behnel

Tighten grammar to disallow a mix of Py2 style octal literals with Py3 style...

Tighten grammar to disallow a mix of Py2 style octal literals with Py3 style underscores: "07" is still allowed, "0_7" is not and must be spelled "0o_7" and the like.
Basically, you get either Py2 style legacy octals or Py3 style underscore literals, but you have to decide what you want.
parent f0e8dd9d
......@@ -20,6 +20,7 @@ def make_lexicon():
from .Scanning import Method
letter = Any("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_")
nonzero_digit = Any("123456789")
digit = Any("0123456789")
bindigit = Any("01")
octdigit = Any("01234567")
......@@ -30,7 +31,7 @@ def make_lexicon():
return Rep1(d) + Rep(Str("_") + Rep1(d))
def prefixed_digits(prefix, digits):
return Any(prefix) + Opt(Str("_")) + underscore_digits(digits)
return prefix + Opt(Str("_")) + underscore_digits(digits)
decimal = underscore_digits(digit)
dot = Str(".")
......@@ -38,10 +39,13 @@ def make_lexicon():
decimal_fract = (decimal + dot + Opt(decimal)) | (dot + decimal)
name = letter + Rep(letter | digit)
# FIXME: in PY_VERSION_HEX < 2, octal literals can start with plain 0, but not in Py3.
intconst = decimal | (Str("0") + (prefixed_digits("Xx", hexdigit) |
prefixed_digits("Oo", octdigit) |
prefixed_digits("Bb", bindigit) ))
intconst = (prefixed_digits(nonzero_digit, digit) | # decimal literals with underscores must not start with '0'
(Str("0") + (prefixed_digits(Any("Xx"), hexdigit) |
prefixed_digits(Any("Oo"), octdigit) |
prefixed_digits(Any("Bb"), bindigit) )) |
underscore_digits(Str('0')) # 0_0_0_0... is allowed as a decimal literal
| Rep1(digit) # FIXME: remove these Py2 style decimal/octal literals (PY_VERSION_HEX < 3)
)
intsuffix = (Opt(Any("Uu")) + Opt(Any("Ll")) + Opt(Any("Ll"))) | (Opt(Any("Ll")) + Opt(Any("Ll")) + Opt(Any("Uu")))
intliteral = intconst + intsuffix
fltconst = (decimal_fract + Opt(exponent)) | (decimal + exponent)
......
......@@ -55,8 +55,8 @@ INVALID_UNDERSCORE_LITERALS = [
'0_o5',
# Old-style octal, still disallowed:
# FIXME: still need to support PY_VERSION_HEX < 3
#'0_7',
#'09_99',
'0_7',
'09_99',
# Multiple consecutive underscores:
'4_______2',
'0.1__4',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment