cmd/compile: accept new Go2 number literals

This CL introduces compiler support for the new binary and octal integer
literals, hexadecimal floats, and digit separators for all number literals.

The new Go 2 number literal scanner accepts the following liberal format:

number   = [ prefix ] digits [ "." digits ] [ exponent ] [ "i" ] .
prefix   = "0" [ "b" |"B" | "o" | "O" | "x" | "X" ] .
digits   = { digit | "_" } .
exponent = ( "e" | "E" | "p" | "P" ) [ "+" | "-" ] digits .

If the number starts with "0x" or "0X", digit is any hexadecimal digit;
otherwise, digit is any decimal digit. If the accepted number is not valid,
errors are reported accordingly.

See the new test cases in scanner_test.go for a selection of valid and
invalid numbers and the respective error messages.

R=Go1.13

Updates #12711.
Updates #19308.
Updates #28493.
Updates #29008.

Change-Id: Ic8febc7bd4dc5186b16a8c8897691e81125cf0ca
Reviewed-on: https://go-review.googlesource.com/c/157677
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Robert Griesemer 2019-01-12 20:33:58 -08:00
parent 7bc2aa670f
commit ceb849dd97
7 changed files with 487 additions and 88 deletions

View file

@ -4,7 +4,8 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Expects to see error messages on 'p' exponents.
// Expects to see error messages on 'p' exponents
// for non-hexadecimal floats.
package main
@ -16,16 +17,9 @@ const (
x3 = 0x1e10 // integer (e is a hex digit)
)
// 'p' exponents are invalid - the 'p' is not considered
// part of a floating-point number, but introduces a new
// (unexpected) name.
//
// Error recovery is not ideal and we use a new declaration
// each time for the parser to recover.
const x4 = 0x1p10 // ERROR "unexpected p10"
const x5 = 1p10 // ERROR "unexpected p10"
const x6 = 0p0 // ERROR "unexpected p0"
const x4 = 0x1p10 // valid hexadecimal float
const x5 = 1p10 // ERROR "'p' exponent requires hexadecimal mantissa"
const x6 = 0P0 // ERROR "'P' exponent requires hexadecimal mantissa"
func main() {
fmt.Printf("%g %T\n", x1, x1)