Merge pull request #1171 from amykyta3/feature/base-integer-literals

Add support for hex, octal, and binary integer literals
This commit is contained in:
David Lord 2021-04-05 10:40:02 -07:00 committed by GitHub
commit fd001b216a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -50,6 +50,8 @@ Unreleased
extension. :issue:`441`
- The ``|indent`` filter's ``width`` argument can be a string to
indent by. :pr:`1167`
- The parser understands hex, octal, and binary integer literals.
:issue:`1170`
Version 2.11.3

View File

@ -23,7 +23,22 @@ newline_re = re.compile(r"(\r\n|\r|\n)")
string_re = re.compile(
r"('([^'\\]*(?:\\.[^'\\]*)*)'" r'|"([^"\\]*(?:\\.[^"\\]*)*)")', re.S
)
integer_re = re.compile(r"(\d+_)*\d+")
integer_re = re.compile(
r"""
(
0b(_?[0-1])+ # binary
|
0o(_?[0-7])+ # octal
|
0x(_?[\da-f])+ # hex
|
[1-9](_?\d)* # decimal
|
0(_?0)* # decimal zero
)
""",
re.IGNORECASE | re.VERBOSE,
)
float_re = re.compile(
r"""
(?<!\.) # doesn't start with a .
@ -613,7 +628,7 @@ class Lexer:
msg = str(e).split(":")[-1].strip()
raise TemplateSyntaxError(msg, lineno, name, filename)
elif token == TOKEN_INTEGER:
value = int(value.replace("_", ""))
value = int(value.replace("_", ""), 0)
elif token == TOKEN_FLOAT:
# remove all "_" first to support more Python versions
value = literal_eval(value.replace("_", ""))

View File

@ -412,6 +412,13 @@ class TestSyntax:
("2.5e+100", "2.5e+100"),
("25.6e-10", "2.56e-09"),
("1_2.3_4e5_6", "1.234e+57"),
("0", "0"),
("0_00", "0"),
("0b1001_1111", "159"),
("0o123", "83"),
("0o1_23", "83"),
("0x123abc", "1194684"),
("0x12_3abc", "1194684"),
),
)
def test_numeric_literal(self, env, value, expect):