Update PEP 515: new, much simpler rule.

This commit is contained in:
Georg Brandl 2016-02-11 08:57:12 +01:00
parent 1202d81471
commit 88fccc3d5c
1 changed files with 20 additions and 30 deletions

View File

@ -37,34 +37,18 @@ Examples::
Specification
=============
The current proposal is to allow underscores anywhere in numeric literals, with
these exceptions:
* Leading underscores cannot be allowed, since they already introduce
identifiers.
* Trailing underscores are not allowed, because they look confusing and don't
contribute much to readability.
* The number base prefixes ``0x``, ``0o``, and ``0b`` cannot be split up,
because they are fixed strings and not logically part of the number.
* No underscore allowed immediately after a sign in an exponent (``1e-_5``),
because underscores can also not be used after the signs in front of the
number (``-1e5``).
* No underscore allowed immediately after a decimal point, because this leads to
ambiguity with attribute access (the lexer cannot know that there is no number
literal in ``foo._5``).
There appears to be no reason to restrict the use of underscores otherwise.
The current proposal is to allow one or more consecutive underscores following
digits and base specifiers in numeric literals.
The production list for integer literals would therefore look like this::
integer: decimalinteger | octinteger | hexinteger | bininteger
decimalinteger: nonzerodigit [decimalrest] | "0" [("0" | "_")* "0"]
decimalinteger: nonzerodigit (digit | "_")* | "0" ("0" | "_")*
nonzerodigit: "1"..."9"
decimalrest: (digit | "_")* digit
digit: "0"..."9"
octinteger: "0" ("o" | "O") (octdigit | "_")* octdigit
hexinteger: "0" ("x" | "X") (hexdigit | "_")* hexdigit
bininteger: "0" ("b" | "B") (bindigit | "_")* bindigit
octinteger: "0" ("o" | "O") (octdigit | "_")*
hexinteger: "0" ("x" | "X") (hexdigit | "_")*
bininteger: "0" ("b" | "B") (bindigit | "_")*
octdigit: "0"..."7"
hexdigit: digit | "a"..."f" | "A"..."F"
bindigit: "0" | "1"
@ -72,11 +56,12 @@ The production list for integer literals would therefore look like this::
For floating-point and complex literals::
floatnumber: pointfloat | exponentfloat
pointfloat: [intpart] "_"* "." intpart | intpart "_"* "."
exponentfloat: (intpart | pointfloat) "_"* exponent
intpart: digit [(digit | "_")* digit]
exponent: ("e" | "E") "_"* ["+" | "-"] digit [decimalrest]
imagnumber: (floatnumber | intpart) "_"* ("j" | "J")
pointfloat: [intpart] fraction | intpart "."
exponentfloat: (intpart | pointfloat) exponent
intpart: digit (digit | "_")*
fraction: "." intpart
exponent: ("e" | "E") ["+" | "-"] intpart
imagnumber: (floatnumber | intpart) ("j" | "J")
Further Considerations
@ -131,11 +116,16 @@ rules for allowed placement of underscores. This is a listing placing the known
rules into three major groups. In cases where the language spec contradicts the
actual behavior, the actual behavior is listed.
**Group 1: liberal (like this PEP)**
**Group 1: liberal**
This group is the least homogeneous: the rules vary slightly between languages.
All of them allow trailing underscores. Some allow underscores after non-digits
like the ``e`` or the sign in exponents.
* D [2]_
* Perl 5 (although docs say it's more restricted) [3]_
* Rust [4]_
* Perl 5 (underscores basically allowed anywhere, although docs say it's more
restricted) [3]_
* Rust (allows between exponent sign and digits) [4]_
* Swift (although textual description says "between digits") [5]_
**Group 2: only between digits, multiple consecutive underscores**