Skip to content

Commit 0f52eaa

Browse files
authored
Merge pull request #2538 from Kodiologist/digit-separator-docs
Document and test weirder uses of digit separators
2 parents 1e5d503 + 67a5343 commit 0f52eaa

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

docs/syntax.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ few extensions:
146146

147147
- Commas (``,``) can be used like underscores (``_``) to separate digits
148148
without changing the result. Thus, ``10_000_000_000`` may also be written
149-
``10,000,000,000``.
149+
``10,000,000,000``. Hy is also more permissive about the placement of
150+
separators than Python: several may be in a row, and they may be after all
151+
digits, after ``.``, ``e``, or ``j``, or even inside a radix prefix. Separators
152+
before the first digit are still forbidden because e.g. ``_1`` is a legal
153+
Python variable name, so it's a symbol in Hy rather than an integer.
150154
- Integers can begin with leading zeroes, even without a radix prefix like
151155
``0x``. Leading zeroes don't automatically cause the literal to be
152156
interpreted in octal like they do in C. For octal, use the prefix ``0o``, as

tests/test_reader.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,15 @@ def test_lex_digit_separators():
259259

260260
assert tokenize("0x_af") == [Integer(0xAF)]
261261
assert tokenize("0x,af") == [Integer(0xAF)]
262+
assert tokenize("0_xaf") == [Integer(0xAF)]
262263
assert tokenize("0b_010") == [Integer(0b010)]
263264
assert tokenize("0b,010") == [Integer(0b010)]
264265
assert tokenize("0o_373") == [Integer(0o373)]
265266
assert tokenize("0o,373") == [Integer(0o373)]
266267

267-
assert tokenize("1_2.3,4") == [Float(12.34)]
268-
assert tokenize("1_2e3,4") == [Float(12e34)]
269-
assert tokenize("1,0_00j") == [Complex(1000j)]
268+
assert tokenize("1_2._3,4") == [Float(12.34)]
269+
assert tokenize("1_2e_3,4") == [Float(12e34)]
270+
assert tokenize("1,0_00j,") == [Complex(1000j)]
270271

271272
assert tokenize("1,,,,___,____,,__,,2__,,,__") == [Integer(12)]
272273
assert tokenize("_1,,,,___,____,,__,,2__,,,__") == [

0 commit comments

Comments
 (0)