Skip to content

Commit a083648

Browse files
committed
spec: document new Go2 number literals
This CL documents the new binary and octal integer literals, hexadecimal floats, generalized imaginary literals and digit separators for all number literals in the spec. Added empty lines between abutting paragraphs in some places (a more thorough cleanup can be done in a separate CL). A minor detail: A single 0 was considered an octal zero per the syntax (decimal integer literals always started with a non-zero digit). The new octal literal syntax allows 0o and 0O prefixes and when keeping the respective octal_lit syntax symmetric with all the others (binary_lit, hex_lit), a single 0 is not automatically part of it anymore. Rather than complicating the new octal_lit syntax to include 0 as before, it is simpler (and more natural) to accept a single 0 as part of a decimal_lit. This is purely a notational change. R=Go1.13 Updates #12711. Updates #19308. Updates #28493. Updates #29008. Change-Id: Ib9fdc6e781f6031cceeed37aaed9d05c7141adec Reviewed-on: https://go-review.googlesource.com/c/go/+/161098 Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 4b4f222 commit a083648

File tree

1 file changed

+126
-28
lines changed

1 file changed

+126
-28
lines changed

doc/go_spec.html

Lines changed: 126 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of February 16, 2019",
3+
"Subtitle": "Version of March 12, 2019",
44
"Path": "/ref/spec"
55
}-->
66

@@ -118,6 +118,7 @@ <h3 id="Letters_and_digits">Letters and digits</h3>
118118
<pre class="ebnf">
119119
letter = unicode_letter | "_" .
120120
decimal_digit = "0" … "9" .
121+
binary_digit = "0" | "1" .
121122
octal_digit = "0" … "7" .
122123
hex_digit = "0" … "9" | "A" … "F" | "a" … "f" .
123124
</pre>
@@ -273,78 +274,164 @@ <h3 id="Integer_literals">Integer literals</h3>
273274
<p>
274275
An integer literal is a sequence of digits representing an
275276
<a href="#Constants">integer constant</a>.
276-
An optional prefix sets a non-decimal base: <code>0</code> for octal, <code>0x</code> or
277-
<code>0X</code> for hexadecimal. In hexadecimal literals, letters
278-
<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
277+
An optional prefix sets a non-decimal base: <code>0b</code> or <code>0B</code>
278+
for binary, <code>0</code>, <code>0o</code>, or <code>0O</code> for octal,
279+
and <code>0x</code> or <code>0X</code> for hexadecimal.
280+
A single <code>0</code> is considered a decimal zero.
281+
In hexadecimal literals, letters <code>a</code> through <code>f</code>
282+
and <code>A</code> through <code>F</code> represent values 10 through 15.
283+
</p>
284+
285+
<p>
286+
For readability, an underscore character <code>_</code> may appear after
287+
a base prefix or between successive digits; such underscores do not change
288+
the literal's value.
279289
</p>
280290
<pre class="ebnf">
281-
int_lit = decimal_lit | octal_lit | hex_lit .
282-
decimal_lit = ( "1" … "9" ) { decimal_digit } .
283-
octal_lit = "0" { octal_digit } .
284-
hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
291+
int_lit = decimal_lit | binary_lit | octal_lit | hex_lit .
292+
decimal_lit = "0" | ( "1" … "9" ) [ [ "_" ] decimal_digits ] .
293+
binary_lit = "0" ( "b" | "B" ) [ "_" ] binary_digits .
294+
octal_lit = "0" [ "o" | "O" ] [ "_" ] octal_digits .
295+
hex_lit = "0" ( "x" | "X" ) [ "_" ] hex_digits .
296+
297+
decimal_digits = decimal_digit { [ "_" ] decimal_digit } .
298+
binary_digits = binary_digit { [ "_" ] binary_digit } .
299+
octal_digits = octal_digit { [ "_" ] octal_digit } .
300+
hex_digits = hex_digit { [ "_" ] hex_digit } .
285301
</pre>
286302

287303
<pre>
288304
42
305+
4_2
289306
0600
307+
0_600
308+
0o600
309+
0O600 // second character is capital letter 'O'
290310
0xBadFace
311+
0xBad_Face
312+
0x_67_7a_2f_cc_40_c6
291313
170141183460469231731687303715884105727
314+
170_141183_460469_231731_687303_715884_105727
315+
316+
_42 // an identifier, not an integer literal
317+
42_ // invalid: _ must separate successive digits
318+
4__2 // invalid: only one _ at a time
319+
0_xBadFace // invalid: _ must separate successive digits
292320
</pre>
293321

322+
294323
<h3 id="Floating-point_literals">Floating-point literals</h3>
324+
295325
<p>
296-
A floating-point literal is a decimal representation of a
326+
A floating-point literal is a decimal or hexadecimal representation of a
297327
<a href="#Constants">floating-point constant</a>.
298-
It has an integer part, a decimal point, a fractional part,
299-
and an exponent part. The integer and fractional part comprise
300-
decimal digits; the exponent part is an <code>e</code> or <code>E</code>
301-
followed by an optionally signed decimal exponent. One of the
302-
integer part or the fractional part may be elided; one of the decimal
303-
point or the exponent may be elided.
304328
</p>
329+
330+
<p>
331+
A decimal floating-point literal consists of an integer part (decimal digits),
332+
a decimal point, a fractional part (decimal digits), and an exponent part
333+
(<code>e</code> or <code>E</code> followed by an optional sign and decimal digits).
334+
One of the integer part or the fractional part may be elided; one of the decimal point
335+
or the exponent part may be elided.
336+
An exponent value exp scales the mantissa (integer and fractional part) by 10<sup>exp</sup>.
337+
</p>
338+
339+
<p>
340+
A hexadecimal floating-point literal consists of a <code>0x</code> or <code>0X</code>
341+
prefix, an integer part (hexadecimal digits), a radix point, a fractional part (hexadecimal digits),
342+
and an exponent part (<code>p</code> or <code>P</code> followed by an optional sign and decimal digits).
343+
One of the integer part or the fractional part may be elided; the radix point may be elided as well,
344+
but the exponent part is required. (This syntax matches the one given in IEEE 754-2008 §5.12.3.)
345+
An exponent value exp scales the mantissa (integer and fractional part) by 2<sup>exp</sup>.
346+
</p>
347+
348+
<p>
349+
For readability, an underscore character <code>_</code> may appear after
350+
a base prefix or between successive digits; such underscores do not change
351+
the literal value.
352+
</p>
353+
305354
<pre class="ebnf">
306-
float_lit = decimals "." [ decimals ] [ exponent ] |
307-
decimals exponent |
308-
"." decimals [ exponent ] .
309-
decimals = decimal_digit { decimal_digit } .
310-
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
355+
float_lit = decimal_float_lit | hex_float_lit .
356+
357+
decimal_float_lit = decimal_digits "." [ decimal_digits ] [ decimal_exponent ] |
358+
decimal_digits decimal_exponent |
359+
"." decimal_digits [ decimal_exponent ] .
360+
decimal_exponent = ( "e" | "E" ) [ "+" | "-" ] decimal_digits .
361+
362+
hex_float_lit = "0" ( "x" | "X" ) hex_mantissa hex_exponent .
363+
hex_mantissa = [ "_" ] hex_digits "." [ hex_digits ] |
364+
[ "_" ] hex_digits |
365+
"." hex_digits .
366+
hex_exponent = ( "p" | "P" ) [ "+" | "-" ] decimal_digits .
311367
</pre>
312368

313369
<pre>
314370
0.
315371
72.40
316-
072.40 // == 72.40
372+
072.40 // == 72.40
317373
2.71828
318374
1.e+0
319375
6.67428e-11
320376
1E6
321377
.25
322378
.12345E+5
379+
1_5. // == 15.0
380+
0.15e+0_2 // == 15.0
381+
382+
0x1p-2 // == 0.25
383+
0x2.p10 // == 2048.0
384+
0x1.Fp+0 // == 1.9375
385+
0X.8p-0 // == 0.5
386+
0X_1FFFP-16 // == 0.1249847412109375
387+
0x15e-2 // == 0x15e - 2 (integer subtraction)
388+
389+
0x.p1 // invalid: mantissa has no digits
390+
1p-2 // invalid: p exponent requires hexadecimal mantissa
391+
0x1.5e-2 // invalid: hexadecimal mantissa requires p exponent
392+
1_.5 // invalid: _ must separate successive digits
393+
1._5 // invalid: _ must separate successive digits
394+
1.5_e1 // invalid: _ must separate successive digits
395+
1.5e_1 // invalid: _ must separate successive digits
396+
1.5e1_ // invalid: _ must separate successive digits
323397
</pre>
324398

399+
325400
<h3 id="Imaginary_literals">Imaginary literals</h3>
401+
326402
<p>
327-
An imaginary literal is a decimal representation of the imaginary part of a
403+
An imaginary literal represents the imaginary part of a
328404
<a href="#Constants">complex constant</a>.
329-
It consists of a
330-
<a href="#Floating-point_literals">floating-point literal</a>
331-
or decimal integer followed
332-
by the lower-case letter <code>i</code>.
405+
It consists of an <a href="#Integer_literals">integer</a> or
406+
<a href="#Floating-point_literals">floating-point</a> literal
407+
followed by the lower-case letter <code>i</code>.
408+
The value of an imaginary literal is the value of the respective
409+
integer or floating-point literal multiplied by the imaginary unit <i>i</i>.
333410
</p>
411+
334412
<pre class="ebnf">
335-
imaginary_lit = (decimals | float_lit) "i" .
413+
imaginary_lit = (decimal_digits | int_lit | float_lit) "i" .
336414
</pre>
337415

416+
<p>
417+
For backward compatibility, an imaginary literal's integer part consisting
418+
entirely of decimal digits (and possibly underscores) is considered a decimal
419+
integer, even if it starts with a leading <code>0</code>.
420+
</p>
421+
338422
<pre>
339423
0i
340-
011i // == 11i
424+
0123i // == 123i for backward-compatibility
425+
0o123i // == 0o123 * 1i == 83i
426+
0xabci // == 0xabc * 1i == 2748i
341427
0.i
342428
2.71828i
343429
1.e+0i
344430
6.67428e-11i
345431
1E6i
346432
.25i
347433
.12345E+5i
434+
0x1p-2i // == 0x1p-2 * 1i == 0.25i
348435
</pre>
349436

350437

@@ -361,6 +448,7 @@ <h3 id="Rune_literals">Rune literals</h3>
361448
while multi-character sequences beginning with a backslash encode
362449
values in various formats.
363450
</p>
451+
364452
<p>
365453
The simplest form represents the single character within the quotes;
366454
since Go source text is Unicode characters encoded in UTF-8, multiple
@@ -370,6 +458,7 @@ <h3 id="Rune_literals">Rune literals</h3>
370458
<code>'ä'</code> holds two bytes (<code>0xc3</code> <code>0xa4</code>) representing
371459
a literal <code>a</code>-dieresis, U+00E4, value <code>0xe4</code>.
372460
</p>
461+
373462
<p>
374463
Several backslash escapes allow arbitrary values to be encoded as
375464
ASCII text. There are four ways to represent the integer value
@@ -380,6 +469,7 @@ <h3 id="Rune_literals">Rune literals</h3>
380469
In each case the value of the literal is the value represented by
381470
the digits in the corresponding base.
382471
</p>
472+
383473
<p>
384474
Although these representations all result in an integer, they have
385475
different valid ranges. Octal escapes must represent a value between
@@ -388,9 +478,11 @@ <h3 id="Rune_literals">Rune literals</h3>
388478
represent Unicode code points so within them some values are illegal,
389479
in particular those above <code>0x10FFFF</code> and surrogate halves.
390480
</p>
481+
391482
<p>
392483
After a backslash, certain single-character escapes represent special values:
393484
</p>
485+
394486
<pre class="grammar">
395487
\a U+0007 alert or bell
396488
\b U+0008 backspace
@@ -403,6 +495,7 @@ <h3 id="Rune_literals">Rune literals</h3>
403495
\' U+0027 single quote (valid escape only within rune literals)
404496
\" U+0022 double quote (valid escape only within string literals)
405497
</pre>
498+
406499
<p>
407500
All other sequences starting with a backslash are illegal inside rune literals.
408501
</p>
@@ -446,6 +539,7 @@ <h3 id="String_literals">String literals</h3>
446539
obtained from concatenating a sequence of characters. There are two forms:
447540
raw string literals and interpreted string literals.
448541
</p>
542+
449543
<p>
450544
Raw string literals are character sequences between back quotes, as in
451545
<code>`foo`</code>. Within the quotes, any character may appear except
@@ -457,6 +551,7 @@ <h3 id="String_literals">String literals</h3>
457551
Carriage return characters ('\r') inside raw string literals
458552
are discarded from the raw string value.
459553
</p>
554+
460555
<p>
461556
Interpreted string literals are character sequences between double
462557
quotes, as in <code>&quot;bar&quot;</code>.
@@ -596,6 +691,7 @@ <h2 id="Constants">Constants</h2>
596691
internal representation with limited precision. That said, every
597692
implementation must:
598693
</p>
694+
599695
<ul>
600696
<li>Represent integer constants with at least 256 bits.</li>
601697

@@ -613,12 +709,14 @@ <h2 id="Constants">Constants</h2>
613709
represent a floating-point or complex constant due to limits
614710
on precision.</li>
615711
</ul>
712+
616713
<p>
617714
These requirements apply both to literal constants and to the result
618715
of evaluating <a href="#Constant_expressions">constant
619716
expressions</a>.
620717
</p>
621718

719+
622720
<h2 id="Variables">Variables</h2>
623721

624722
<p>

0 commit comments

Comments
 (0)