Tue, 15 Sep 2009 18:36:21 -0700
6860965: Project Coin: binary literals
6860973: Project Coin: Underscores in literals
Summary: [Portions contributed by Bruce Chapman]
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/javac/code/Source.java Tue Sep 15 12:20:55 2009 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Source.java Tue Sep 15 18:36:21 2009 -0700 1.3 @@ -159,6 +159,12 @@ 1.4 public boolean allowTypeAnnotations() { 1.5 return compareTo(JDK1_7) >= 0; 1.6 } 1.7 + public boolean allowBinaryLiterals() { 1.8 + return compareTo(JDK1_7) >= 0; 1.9 + } 1.10 + public boolean allowUnderscoresInLiterals() { 1.11 + return compareTo(JDK1_7) >= 0; 1.12 + } 1.13 public static SourceVersion toSourceVersion(Source source) { 1.14 switch(source) { 1.15 case JDK1_2:
2.1 --- a/src/share/classes/com/sun/tools/javac/parser/Scanner.java Tue Sep 15 12:20:55 2009 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/parser/Scanner.java Tue Sep 15 18:36:21 2009 -0700 2.3 @@ -100,6 +100,18 @@ 2.4 */ 2.5 private boolean allowHexFloats; 2.6 2.7 + /** Allow binary literals. 2.8 + */ 2.9 + private boolean allowBinaryLiterals; 2.10 + 2.11 + /** Allow underscores in literals. 2.12 + */ 2.13 + private boolean allowUnderscoresInLiterals; 2.14 + 2.15 + /** The source language setting. 2.16 + */ 2.17 + private Source source; 2.18 + 2.19 /** The token's position, 0-based offset from beginning of text. 2.20 */ 2.21 private int pos; 2.22 @@ -162,10 +174,13 @@ 2.23 2.24 /** Common code for constructors. */ 2.25 private Scanner(Factory fac) { 2.26 - this.log = fac.log; 2.27 - this.names = fac.names; 2.28 - this.keywords = fac.keywords; 2.29 - this.allowHexFloats = fac.source.allowHexFloats(); 2.30 + log = fac.log; 2.31 + names = fac.names; 2.32 + keywords = fac.keywords; 2.33 + source = fac.source; 2.34 + allowBinaryLiterals = source.allowBinaryLiterals(); 2.35 + allowHexFloats = source.allowHexFloats(); 2.36 + allowUnderscoresInLiterals = source.allowBinaryLiterals(); 2.37 } 2.38 2.39 private static final boolean hexFloatsWork = hexFloatsWork(); 2.40 @@ -396,23 +411,42 @@ 2.41 scanLitChar(true); 2.42 } 2.43 2.44 + private void scanDigits(int digitRadix) { 2.45 + char saveCh; 2.46 + int savePos; 2.47 + do { 2.48 + if (ch != '_') { 2.49 + putChar(ch); 2.50 + } else { 2.51 + if (!allowUnderscoresInLiterals) { 2.52 + lexError("unsupported.underscore", source.name); 2.53 + allowUnderscoresInLiterals = true; 2.54 + } 2.55 + } 2.56 + saveCh = ch; 2.57 + savePos = bp; 2.58 + scanChar(); 2.59 + } while (digit(digitRadix) >= 0 || ch == '_'); 2.60 + if (saveCh == '_') 2.61 + lexError(savePos, "illegal.underscore"); 2.62 + } 2.63 + 2.64 /** Read fractional part of hexadecimal floating point number. 2.65 */ 2.66 private void scanHexExponentAndSuffix() { 2.67 if (ch == 'p' || ch == 'P') { 2.68 putChar(ch); 2.69 scanChar(); 2.70 + skipIllegalUnderscores(); 2.71 if (ch == '+' || ch == '-') { 2.72 putChar(ch); 2.73 scanChar(); 2.74 } 2.75 + skipIllegalUnderscores(); 2.76 if ('0' <= ch && ch <= '9') { 2.77 - do { 2.78 - putChar(ch); 2.79 - scanChar(); 2.80 - } while ('0' <= ch && ch <= '9'); 2.81 + scanDigits(10); 2.82 if (!allowHexFloats) { 2.83 - lexError("unsupported.fp.lit"); 2.84 + lexError("unsupported.fp.lit", source.name); 2.85 allowHexFloats = true; 2.86 } 2.87 else if (!hexFloatsWork) 2.88 @@ -438,23 +472,22 @@ 2.89 /** Read fractional part of floating point number. 2.90 */ 2.91 private void scanFraction() { 2.92 - while (digit(10) >= 0) { 2.93 - putChar(ch); 2.94 - scanChar(); 2.95 + skipIllegalUnderscores(); 2.96 + if ('0' <= ch && ch <= '9') { 2.97 + scanDigits(10); 2.98 } 2.99 int sp1 = sp; 2.100 if (ch == 'e' || ch == 'E') { 2.101 putChar(ch); 2.102 scanChar(); 2.103 + skipIllegalUnderscores(); 2.104 if (ch == '+' || ch == '-') { 2.105 putChar(ch); 2.106 scanChar(); 2.107 } 2.108 + skipIllegalUnderscores(); 2.109 if ('0' <= ch && ch <= '9') { 2.110 - do { 2.111 - putChar(ch); 2.112 - scanChar(); 2.113 - } while ('0' <= ch && ch <= '9'); 2.114 + scanDigits(10); 2.115 return; 2.116 } 2.117 lexError("malformed.fp.lit"); 2.118 @@ -487,10 +520,10 @@ 2.119 assert ch == '.'; 2.120 putChar(ch); 2.121 scanChar(); 2.122 - while (digit(16) >= 0) { 2.123 + skipIllegalUnderscores(); 2.124 + if (digit(16) >= 0) { 2.125 seendigit = true; 2.126 - putChar(ch); 2.127 - scanChar(); 2.128 + scanDigits(16); 2.129 } 2.130 if (!seendigit) 2.131 lexError("invalid.hex.number"); 2.132 @@ -498,28 +531,35 @@ 2.133 scanHexExponentAndSuffix(); 2.134 } 2.135 2.136 + private void skipIllegalUnderscores() { 2.137 + if (ch == '_') { 2.138 + lexError(bp, "illegal.underscore"); 2.139 + while (ch == '_') 2.140 + scanChar(); 2.141 + } 2.142 + } 2.143 + 2.144 /** Read a number. 2.145 - * @param radix The radix of the number; one of 8, 10, 16. 2.146 + * @param radix The radix of the number; one of 2, j8, 10, 16. 2.147 */ 2.148 private void scanNumber(int radix) { 2.149 this.radix = radix; 2.150 // for octal, allow base-10 digit in case it's a float literal 2.151 - int digitRadix = (radix <= 10) ? 10 : 16; 2.152 + int digitRadix = (radix == 8 ? 10 : radix); 2.153 boolean seendigit = false; 2.154 - while (digit(digitRadix) >= 0) { 2.155 + if (digit(digitRadix) >= 0) { 2.156 seendigit = true; 2.157 - putChar(ch); 2.158 - scanChar(); 2.159 + scanDigits(digitRadix); 2.160 } 2.161 if (radix == 16 && ch == '.') { 2.162 scanHexFractionAndSuffix(seendigit); 2.163 } else if (seendigit && radix == 16 && (ch == 'p' || ch == 'P')) { 2.164 scanHexExponentAndSuffix(); 2.165 - } else if (radix <= 10 && ch == '.') { 2.166 + } else if (digitRadix == 10 && ch == '.') { 2.167 putChar(ch); 2.168 scanChar(); 2.169 scanFractionAndSuffix(); 2.170 - } else if (radix <= 10 && 2.171 + } else if (digitRadix == 10 && 2.172 (ch == 'e' || ch == 'E' || 2.173 ch == 'f' || ch == 'F' || 2.174 ch == 'd' || ch == 'D')) { 2.175 @@ -821,6 +861,7 @@ 2.176 scanChar(); 2.177 if (ch == 'x' || ch == 'X') { 2.178 scanChar(); 2.179 + skipIllegalUnderscores(); 2.180 if (ch == '.') { 2.181 scanHexFractionAndSuffix(false); 2.182 } else if (digit(16) < 0) { 2.183 @@ -828,8 +869,25 @@ 2.184 } else { 2.185 scanNumber(16); 2.186 } 2.187 + } else if (ch == 'b' || ch == 'B') { 2.188 + if (!allowBinaryLiterals) { 2.189 + lexError("unsupported.binary.lit", source.name); 2.190 + allowBinaryLiterals = true; 2.191 + } 2.192 + scanChar(); 2.193 + skipIllegalUnderscores(); 2.194 + scanNumber(2); 2.195 } else { 2.196 putChar('0'); 2.197 + if (ch == '_') { 2.198 + int savePos = bp; 2.199 + do { 2.200 + scanChar(); 2.201 + } while (ch == '_'); 2.202 + if (digit(10) < 0) { 2.203 + lexError(savePos, "illegal.underscore"); 2.204 + } 2.205 + } 2.206 scanNumber(8); 2.207 } 2.208 return;
3.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Sep 15 12:20:55 2009 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Tue Sep 15 18:36:21 2009 -0700 3.3 @@ -216,6 +216,8 @@ 3.4 illegal line end in character literal 3.5 compiler.err.illegal.nonascii.digit=\ 3.6 illegal non-ASCII digit 3.7 +compiler.err.illegal.underscore=\ 3.8 + illegal underscore 3.9 compiler.err.illegal.qual.not.icls=\ 3.10 illegal qualifier; {0} is not an inner class 3.11 compiler.err.illegal.start.of.expr=\ 3.12 @@ -1163,7 +1165,16 @@ 3.13 # Diagnostics for language feature changes 3.14 ######################################## 3.15 compiler.err.unsupported.fp.lit=\ 3.16 - hexadecimal floating-point literals are not supported before -source 5 3.17 + hexadecimal floating point literals are not supported in -source {0}\n\ 3.18 +(use -source 5 or higher to enable hexadecimal floating point literals) 3.19 + 3.20 +compiler.err.unsupported.binary.lit=\ 3.21 + binary literals are not supported in -source {0}\n\ 3.22 +(use -source 7 or higher to enable binary literals) 3.23 + 3.24 +compiler.err.unsupported.underscore.lit=\ 3.25 + underscores in literals are not supported in -source {0}\n\ 3.26 +(use -source 7 or higher to enable underscores in literals) 3.27 3.28 compiler.warn.enum.as.identifier=\ 3.29 as of release 5, ''enum'' is a keyword, and may not be used as an identifier\n\
4.1 --- a/test/tools/javac/enum/6384542/T6384542.out Tue Sep 15 12:20:55 2009 -0700 4.2 +++ b/test/tools/javac/enum/6384542/T6384542.out Tue Sep 15 18:36:21 2009 -0700 4.3 @@ -1,6 +1,6 @@ 4.4 T6384542.java:10:8: compiler.err.static.import.not.supported.in.source: 1.4 4.5 T6384542.java:12:8: compiler.err.enums.not.supported.in.source: 1.4 4.6 -T6384542.java:14:13: compiler.err.unsupported.fp.lit 4.7 +T6384542.java:14:13: compiler.err.unsupported.fp.lit: 1.4 4.8 T6384542.java:15:9: compiler.err.generics.not.supported.in.source: 1.4 4.9 T6384542.java:16:35: compiler.err.varargs.not.supported.in.source: 1.4 4.10 T6384542.java:17:25: compiler.err.foreach.not.supported.in.source: 1.4
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/literals/BadBinaryLiterals.6.out Tue Sep 15 18:36:21 2009 -0700 5.3 @@ -0,0 +1,8 @@ 5.4 +BadBinaryLiterals.java:10:17: compiler.err.unsupported.binary.lit: 1.6 5.5 +BadBinaryLiterals.java:11:24: compiler.err.expected: ';' 5.6 +BadBinaryLiterals.java:13:21: compiler.err.int.number.too.large: 111111111111111111111111111111111 5.7 +BadBinaryLiterals.java:15:21: compiler.err.int.number.too.large: 11111111111111111111111111111111111111111111111111111111111111111 5.8 +BadBinaryLiterals.java:16:27: compiler.err.expected: ';' 5.9 +BadBinaryLiterals.java:17:27: compiler.err.expected: ';' 5.10 +BadBinaryLiterals.java:17:30: compiler.err.expected: token.identifier 5.11 +7 errors
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/literals/BadBinaryLiterals.7.out Tue Sep 15 18:36:21 2009 -0700 6.3 @@ -0,0 +1,7 @@ 6.4 +BadBinaryLiterals.java:11:24: compiler.err.expected: ';' 6.5 +BadBinaryLiterals.java:13:21: compiler.err.int.number.too.large: 111111111111111111111111111111111 6.6 +BadBinaryLiterals.java:15:21: compiler.err.int.number.too.large: 11111111111111111111111111111111111111111111111111111111111111111 6.7 +BadBinaryLiterals.java:16:27: compiler.err.expected: ';' 6.8 +BadBinaryLiterals.java:17:27: compiler.err.expected: ';' 6.9 +BadBinaryLiterals.java:17:30: compiler.err.expected: token.identifier 6.10 +6 errors
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/test/tools/javac/literals/BadBinaryLiterals.java Tue Sep 15 18:36:21 2009 -0700 7.3 @@ -0,0 +1,18 @@ 7.4 +/* 7.5 + * @test /nodynamiccopyright/ 7.6 + * @bug 6860965 7.7 + * @summary Project Coin: binary literals 7.8 + * @compile/fail/ref=BadBinaryLiterals.6.out -XDrawDiagnostics -source 6 BadBinaryLiterals.java 7.9 + * @compile/fail/ref=BadBinaryLiterals.7.out -XDrawDiagnostics BadBinaryLiterals.java 7.10 + */ 7.11 + 7.12 +public class BadBinaryLiterals { 7.13 + int valid = 0b0; // valid literal, illegal in source 6 7.14 + int baddigit = 0b012; // bad digit 7.15 + //aaaabbbbccccddddeeeeffffgggghhhh 7.16 + int overflow1 = 0b111111111111111111111111111111111; // too long for int 7.17 + //aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooopppp 7.18 + int overflow2 = 0b11111111111111111111111111111111111111111111111111111111111111111L; // too long for long 7.19 + float badfloat1 = 0b01.01; // no binary floats 7.20 + float badfloat2 = 0b01e01; // no binary floats 7.21 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/literals/BadUnderscoreLiterals.6.out Tue Sep 15 18:36:21 2009 -0700 8.3 @@ -0,0 +1,21 @@ 8.4 +BadUnderscoreLiterals.java:14:17: compiler.err.unsupported.underscore: 1.6 8.5 +BadUnderscoreLiterals.java:18:15: compiler.err.illegal.underscore 8.6 +BadUnderscoreLiterals.java:22:19: compiler.err.illegal.underscore 8.7 +BadUnderscoreLiterals.java:25:14: compiler.err.unsupported.binary.lit: 1.6 8.8 +BadUnderscoreLiterals.java:25:16: compiler.err.illegal.underscore 8.9 +BadUnderscoreLiterals.java:26:17: compiler.err.illegal.underscore 8.10 +BadUnderscoreLiterals.java:29:16: compiler.err.illegal.underscore 8.11 +BadUnderscoreLiterals.java:30:17: compiler.err.illegal.underscore 8.12 +BadUnderscoreLiterals.java:33:17: compiler.err.illegal.underscore 8.13 +BadUnderscoreLiterals.java:34:18: compiler.err.illegal.underscore 8.14 +BadUnderscoreLiterals.java:35:19: compiler.err.illegal.underscore 8.15 +BadUnderscoreLiterals.java:36:19: compiler.err.illegal.underscore 8.16 +BadUnderscoreLiterals.java:37:18: compiler.err.illegal.underscore 8.17 +BadUnderscoreLiterals.java:38:19: compiler.err.illegal.underscore 8.18 +BadUnderscoreLiterals.java:41:19: compiler.err.illegal.underscore 8.19 +BadUnderscoreLiterals.java:42:20: compiler.err.illegal.underscore 8.20 +BadUnderscoreLiterals.java:43:21: compiler.err.illegal.underscore 8.21 +BadUnderscoreLiterals.java:44:22: compiler.err.illegal.underscore 8.22 +BadUnderscoreLiterals.java:45:21: compiler.err.illegal.underscore 8.23 +BadUnderscoreLiterals.java:46:22: compiler.err.illegal.underscore 8.24 +20 errors
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/test/tools/javac/literals/BadUnderscoreLiterals.7.out Tue Sep 15 18:36:21 2009 -0700 9.3 @@ -0,0 +1,19 @@ 9.4 +BadUnderscoreLiterals.java:18:15: compiler.err.illegal.underscore 9.5 +BadUnderscoreLiterals.java:22:19: compiler.err.illegal.underscore 9.6 +BadUnderscoreLiterals.java:25:16: compiler.err.illegal.underscore 9.7 +BadUnderscoreLiterals.java:26:17: compiler.err.illegal.underscore 9.8 +BadUnderscoreLiterals.java:29:16: compiler.err.illegal.underscore 9.9 +BadUnderscoreLiterals.java:30:17: compiler.err.illegal.underscore 9.10 +BadUnderscoreLiterals.java:33:17: compiler.err.illegal.underscore 9.11 +BadUnderscoreLiterals.java:34:18: compiler.err.illegal.underscore 9.12 +BadUnderscoreLiterals.java:35:19: compiler.err.illegal.underscore 9.13 +BadUnderscoreLiterals.java:36:19: compiler.err.illegal.underscore 9.14 +BadUnderscoreLiterals.java:37:18: compiler.err.illegal.underscore 9.15 +BadUnderscoreLiterals.java:38:19: compiler.err.illegal.underscore 9.16 +BadUnderscoreLiterals.java:41:19: compiler.err.illegal.underscore 9.17 +BadUnderscoreLiterals.java:42:20: compiler.err.illegal.underscore 9.18 +BadUnderscoreLiterals.java:43:21: compiler.err.illegal.underscore 9.19 +BadUnderscoreLiterals.java:44:22: compiler.err.illegal.underscore 9.20 +BadUnderscoreLiterals.java:45:21: compiler.err.illegal.underscore 9.21 +BadUnderscoreLiterals.java:46:22: compiler.err.illegal.underscore 9.22 +18 errors
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/test/tools/javac/literals/BadUnderscoreLiterals.java Tue Sep 15 18:36:21 2009 -0700 10.3 @@ -0,0 +1,48 @@ 10.4 +/* 10.5 + * @test /nodynamiccopyright/ 10.6 + * @bug 6860973 10.7 + * @summary Project Coin: underscores in literals 10.8 + * 10.9 + * @compile/fail BadUnderscoreLiterals.java 10.10 + * @compile/fail/ref=BadUnderscoreLiterals.7.out -XDrawDiagnostics BadUnderscoreLiterals.java 10.11 + * 10.12 + * @compile/fail -source 6 BadUnderscoreLiterals.java 10.13 + * @compile/fail/ref=BadUnderscoreLiterals.6.out -XDrawDiagnostics -source 6 BadUnderscoreLiterals.java 10.14 + */ 10.15 + 10.16 +public class BadUnderscoreLiterals { 10.17 + int valid = 1_1; // valid literal; illegal in -source 6 10.18 + 10.19 + // test zero 10.20 + int z1 = _0; // valid (but undefined) variable 10.21 + int z2 = 0_; // trailing underscore 10.22 + 10.23 + // test simple (decimal) integers 10.24 + int i1 = _1_2_3; // valid (but undefined) variable 10.25 + int i2 = 1_2_3_; // trailing underscore 10.26 + 10.27 + // test binary integers 10.28 + int b1 = 0b_0; // leading underscore after radix 10.29 + int b2 = 0b0_; // trailing underscore 10.30 + 10.31 + // test hexadecimal integers 10.32 + int x1 = 0x_0; // leading underscore after radix 10.33 + int x2 = 0x0_; // trailing underscore 10.34 + 10.35 + // test floating point numbers 10.36 + float f1 = 0_.1; // trailing underscore before decimal point 10.37 + float f2 = 0._1; // leading underscore after decimal point 10.38 + float f3 = 0.1_; // trailing underscore 10.39 + float f4 = 0.1_e0; // trailing underscore before exponent 10.40 + float f5 = 0e_1; // leading underscore in exponent 10.41 + float f6 = 0e1_; // trailing underscore in exponent 10.42 + 10.43 + // hexadecimal floating point 10.44 + float xf1 = 0x_0.1p0; // leading underscore after radix 10.45 + float xf2 = 0x0_.1p0; // trailing underscore before decimal point 10.46 + float xf3 = 0x0._1p0; // leading underscore after decimal point 10.47 + float xf4 = 0x0.1_p0; // trailing underscore before exponent 10.48 + float xf5 = 0x0p_1; // leading underscore after exponent 10.49 + float xf6 = 0x0p1_; // trailing underscore 10.50 +} 10.51 +
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/test/tools/javac/literals/BinaryLiterals.java Tue Sep 15 18:36:21 2009 -0700 11.3 @@ -0,0 +1,132 @@ 11.4 +/* 11.5 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 11.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.7 + * 11.8 + * This code is free software; you can redistribute it and/or modify it 11.9 + * under the terms of the GNU General Public License version 2 only, as 11.10 + * published by the Free Software Foundation. 11.11 + * 11.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 11.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11.15 + * version 2 for more details (a copy is included in the LICENSE file that 11.16 + * accompanied this code). 11.17 + * 11.18 + * You should have received a copy of the GNU General Public License version 11.19 + * 2 along with this work; if not, write to the Free Software Foundation, 11.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 11.21 + * 11.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 11.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 11.24 + * have any questions. 11.25 + */ 11.26 + 11.27 +/* 11.28 + * @test 11.29 + * @bug 6860965 11.30 + * @summary Project Coin: binary literals 11.31 + */ 11.32 + 11.33 +public class BinaryLiterals { 11.34 + public static void main(String... args) throws Exception { 11.35 + new BinaryLiterals().run(); 11.36 + } 11.37 + 11.38 + public void run() throws Exception { 11.39 + test(0, 0B0); 11.40 + test(1, 0B1); 11.41 + test(2, 0B10); 11.42 + test(3, 0B11); 11.43 + 11.44 + test(0, 0b0); 11.45 + test(1, 0b1); 11.46 + test(2, 0b10); 11.47 + test(3, 0b11); 11.48 + 11.49 + test(-0, -0b0); 11.50 + test(-1, -0b1); 11.51 + test(-2, -0b10); 11.52 + test(-3, -0b11); 11.53 + 11.54 + test(-1, 0b11111111111111111111111111111111); 11.55 + test(-2, 0b11111111111111111111111111111110); 11.56 + test(-3, 0b11111111111111111111111111111101); 11.57 + 11.58 + test( 1, -0b11111111111111111111111111111111); 11.59 + test( 2, -0b11111111111111111111111111111110); 11.60 + test( 3, -0b11111111111111111111111111111101); 11.61 + 11.62 + test(0, 0b00); 11.63 + test(1, 0b001); 11.64 + test(2, 0b00010); 11.65 + test(3, 0b000011); 11.66 + 11.67 + // aaaabbbbccccddddeeeeffffgggghhhh 11.68 + test( 0x10, 0b10000); 11.69 + test( 0x100, 0b100000000); 11.70 + test( 0x10000, 0b10000000000000000); 11.71 + test(0x80000000, 0b10000000000000000000000000000000); 11.72 + test(0xffffffff, 0b11111111111111111111111111111111); 11.73 + 11.74 + test(0L, 0b0L); 11.75 + test(1L, 0b1L); 11.76 + test(2L, 0b10L); 11.77 + test(3L, 0b11L); 11.78 + 11.79 + test(0, 0b00L); 11.80 + test(1, 0b001L); 11.81 + test(2, 0b00010L); 11.82 + test(3, 0b000011L); 11.83 + 11.84 + // aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooopppp 11.85 + test( 0x10L, 0b10000L); 11.86 + test( 0x100L, 0b100000000L); 11.87 + test( 0x10000L, 0b10000000000000000L); 11.88 + test( 0x80000000L, 0b10000000000000000000000000000000L); 11.89 + test( 0xffffffffL, 0b11111111111111111111111111111111L); 11.90 + test(0x8000000000000000L, 0b1000000000000000000000000000000000000000000000000000000000000000L); 11.91 + test(0xffffffffffffffffL, 0b1111111111111111111111111111111111111111111111111111111111111111L); 11.92 + 11.93 + test(0l, 0b0l); 11.94 + test(1l, 0b1l); 11.95 + test(2l, 0b10l); 11.96 + test(3l, 0b11l); 11.97 + 11.98 + test(0, 0b00l); 11.99 + test(1, 0b001l); 11.100 + test(2, 0b00010l); 11.101 + test(3, 0b000011l); 11.102 + 11.103 + // aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmmmmnnnnoooopppp 11.104 + test( 0x10l, 0b10000l); 11.105 + test( 0x100l, 0b100000000l); 11.106 + test( 0x10000l, 0b10000000000000000l); 11.107 + test( 0x80000000l, 0b10000000000000000000000000000000l); 11.108 + test( 0xffffffffl, 0b11111111111111111111111111111111l); 11.109 + test(0x8000000000000000l, 0b1000000000000000000000000000000000000000000000000000000000000000l); 11.110 + test(0xffffffffffffffffl, 0b1111111111111111111111111111111111111111111111111111111111111111l); 11.111 + 11.112 + if (errors > 0) 11.113 + throw new Exception(errors + " errors found"); 11.114 + } 11.115 + 11.116 + void test(int expect, int found) { 11.117 + count++; 11.118 + if (found != expect) 11.119 + error("test " + count + "\nexpected: 0x" + Integer.toHexString(expect) + "\n found: 0x" + Integer.toHexString(found)); 11.120 + } 11.121 + 11.122 + void test(long expect, long found) { 11.123 + count++; 11.124 + if (found != expect) 11.125 + error("test " + count + "\nexpected: 0x" + Long.toHexString(expect) + "\n found: 0x" + Long.toHexString(found)); 11.126 + } 11.127 + 11.128 + void error(String message) { 11.129 + System.out.println(message); 11.130 + errors++; 11.131 + } 11.132 + 11.133 + int count; 11.134 + int errors; 11.135 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/test/tools/javac/literals/UnderscoreLiterals.java Tue Sep 15 18:36:21 2009 -0700 12.3 @@ -0,0 +1,195 @@ 12.4 +/* 12.5 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 12.7 + * 12.8 + * This code is free software; you can redistribute it and/or modify it 12.9 + * under the terms of the GNU General Public License version 2 only, as 12.10 + * published by the Free Software Foundation. 12.11 + * 12.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 12.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12.15 + * version 2 for more details (a copy is included in the LICENSE file that 12.16 + * accompanied this code). 12.17 + * 12.18 + * You should have received a copy of the GNU General Public License version 12.19 + * 2 along with this work; if not, write to the Free Software Foundation, 12.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 12.21 + * 12.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 12.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 12.24 + * have any questions. 12.25 + */ 12.26 + 12.27 +/* 12.28 + * @test 12.29 + * @bug 6860973 12.30 + * @summary Project Coin: Underscores in literals 12.31 + */ 12.32 + 12.33 + 12.34 +public class UnderscoreLiterals { 12.35 + public static void main(String... args) throws Exception { 12.36 + new UnderscoreLiterals().run(); 12.37 + } 12.38 + 12.39 + public void run() throws Exception { 12.40 + // decimal 12.41 + test(1, 1); 12.42 + test(10, 10); 12.43 + test(1_0, 10); 12.44 + test(1__0, 10); 12.45 + test(1_0_0, 100); 12.46 + test(1__0__0, 100); 12.47 + test(123_456_789, 123456789); 12.48 + 12.49 + // long 12.50 + test(1l, 1l); 12.51 + test(10l, 10l); 12.52 + test(1_0l, 10l); 12.53 + test(1__0l, 10l); 12.54 + test(1_0_0l, 100l); 12.55 + test(1__0__0l, 100l); 12.56 + test(123_456_789l, 123456789l); 12.57 + 12.58 + // float 12.59 + test(.1f, .1f); 12.60 + test(.10f, .10f); 12.61 + test(.1_0f, .10f); 12.62 + test(.1__0f, .10f); 12.63 + test(.1_0_0f, .100f); 12.64 + test(.1__0__0f, .100f); 12.65 + test(1e1, 1e1); 12.66 + test(1e10, 1e10); 12.67 + test(1e1_0, 1e10); 12.68 + test(1e1__0, 1e10); 12.69 + test(1e1_0_0, 1e100); 12.70 + test(1e1__0__0, 1e100); 12.71 + test(.123_456_789f, .123456789f); 12.72 + test(0.1f, 0.1f); 12.73 + test(0.10f, 0.10f); 12.74 + test(0.1_0f, 0.10f); 12.75 + test(0.1__0f, 0.10f); 12.76 + test(0.1_0_0f, 0.100f); 12.77 + test(0.1__0__0f, 0.100f); 12.78 + test(0.123_456_789f, 0.123456789f); 12.79 + test(1_1.1f, 1_1.1f); 12.80 + test(1_1.10f, 1_1.10f); 12.81 + test(1_1.1_0f, 1_1.10f); 12.82 + test(1_1.1__0f, 1_1.10f); 12.83 + test(1_1.1_0_0f, 1_1.100f); 12.84 + test(1_1.1__0__0f, 1_1.100f); 12.85 + test(1_1.123_456_789f, 1_1.123456789f); 12.86 + 12.87 + // double 12.88 + test(.1d, .1d); 12.89 + test(.10d, .10d); 12.90 + test(.1_0d, .10d); 12.91 + test(.1__0d, .10d); 12.92 + test(.1_0_0d, .100d); 12.93 + test(.1__0__0d, .100d); 12.94 + test(1e1, 1e1); 12.95 + test(1e10, 1e10); 12.96 + test(1e1_0, 1e10); 12.97 + test(1e1__0, 1e10); 12.98 + test(1e1_0_0, 1e100); 12.99 + test(1e1__0__0, 1e100); 12.100 + test(.123_456_789d, .123456789d); 12.101 + test(0.1d, 0.1d); 12.102 + test(0.10d, 0.10d); 12.103 + test(0.1_0d, 0.10d); 12.104 + test(0.1__0d, 0.10d); 12.105 + test(0.1_0_0d, 0.100d); 12.106 + test(0.1__0__0d, 0.100d); 12.107 + test(0.123_456_789d, 0.123456789d); 12.108 + test(1_1.1d, 1_1.1d); 12.109 + test(1_1.10d, 1_1.10d); 12.110 + test(1_1.1_0d, 1_1.10d); 12.111 + test(1_1.1__0d, 1_1.10d); 12.112 + test(1_1.1_0_0d, 1_1.100d); 12.113 + test(1_1.1__0__0d, 1_1.100d); 12.114 + test(1_1.123_456_789d, 1_1.123456789d); 12.115 + 12.116 + // binary 12.117 + test(0b1, 1); 12.118 + test(0b10, 2); 12.119 + test(0b1_0, 2); 12.120 + test(0b1__0, 2); 12.121 + test(0b1_0_0, 4); 12.122 + test(0b1__0__0, 4); 12.123 + test(0b0001_0010_0011, 0x123); 12.124 + 12.125 + // octal 12.126 + test(01, 1); 12.127 + test(010, 8); 12.128 + test(01_0, 8); 12.129 + test(01__0, 8); 12.130 + test(01_0_0, 64); 12.131 + test(01__0__0, 64); 12.132 + test(0_1, 1); 12.133 + test(0_10, 8); 12.134 + test(0_1_0, 8); 12.135 + test(0_1__0, 8); 12.136 + test(0_1_0_0, 64); 12.137 + test(0_1__0__0, 64); 12.138 + test(0_001_002_003, 01002003); 12.139 + 12.140 + // hexadecimal 12.141 + test(0x1, 1); 12.142 + test(0x10, 16); 12.143 + test(0x1_0, 16); 12.144 + test(0x1__0, 16); 12.145 + test(0x1_0_0, 256); 12.146 + test(0x1__0__0, 256); 12.147 + test(0x01_02_03_04, 0x1020304); 12.148 + 12.149 + // misc 12.150 + long creditCardNumber = 1234_5678_9012_3456L; 12.151 + test(creditCardNumber, 1234567890123456L); 12.152 + long socialSecurityNumbers = 999_99_9999L; 12.153 + test(socialSecurityNumbers, 999999999L); 12.154 + double monetaryAmount = 12_345_132.12d; 12.155 + test(monetaryAmount, 12345132.12d); 12.156 + long hexBytes = 0xFF_EC_DE_5E; 12.157 + test(hexBytes, 0xffecde5e); 12.158 + long hexWords = 0xFFEC_DE5E; 12.159 + test(hexWords, 0xffecde5e); 12.160 + long maxLong = 0x7fff_ffff_ffff_ffffL; 12.161 + test(maxLong, 0x7fffffffffffffffL); 12.162 + long maxLongDecimal = 9223372036854775807L; 12.163 + long alsoMaxLong = 9_223_372_036_854_775_807L; 12.164 + test(alsoMaxLong, maxLongDecimal); 12.165 + double whyWouldYouEverDoThis = 0x1.ffff_ffff_ffff_fp10_23; 12.166 + double whyWouldYouEverDoEvenThis = 0x1.fffffffffffffp1023; 12.167 + test(whyWouldYouEverDoThis, whyWouldYouEverDoEvenThis); 12.168 + 12.169 + if (errors > 0) 12.170 + throw new Exception(errors + " errors found"); 12.171 + } 12.172 + 12.173 + void test(int value, int expect) { 12.174 + count++; 12.175 + if (value != expect) 12.176 + error("test " + count + "\nexpected: 0x" + Integer.toHexString(expect) + "\n found: 0x" + Integer.toHexString(value)); 12.177 + } 12.178 + 12.179 + void test(double value, double expect) { 12.180 + count++; 12.181 + if (value != expect) 12.182 + error("test " + count + "\nexpected: 0x" + expect + "\n found: 0x" + value); 12.183 + } 12.184 + 12.185 + void test(long value, long expect) { 12.186 + count++; 12.187 + if (value != expect) 12.188 + error("test " + count + "\nexpected: 0x" + Long.toHexString(expect) + "\n found: 0x" + Long.toHexString(value)); 12.189 + } 12.190 + 12.191 + void error(String message) { 12.192 + System.out.println(message); 12.193 + errors++; 12.194 + } 12.195 + 12.196 + int count; 12.197 + int errors; 12.198 +}