numeric_literals.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lex/testdata/numeric_literals.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lex/testdata/numeric_literals.carbon
  10. // --- valid.carbon
  11. // CHECK:STDOUT: - filename: valid.carbon
  12. // CHECK:STDOUT: tokens:
  13. fn F() {
  14. // CHECK:STDOUT: - { index: 1, kind: "Fn", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "fn", has_leading_space: true }
  15. // CHECK:STDOUT: - { index: 2, kind: "Identifier", line: {{ *}}[[@LINE-2]], column: 4, indent: 1, spelling: "F", identifier: 0, has_leading_space: true }
  16. // CHECK:STDOUT: - { index: 3, kind: "OpenParen", line: {{ *}}[[@LINE-3]], column: 5, indent: 1, spelling: "(", closing_token: 4 }
  17. // CHECK:STDOUT: - { index: 4, kind: "CloseParen", line: {{ *}}[[@LINE-4]], column: 6, indent: 1, spelling: ")", opening_token: 3 }
  18. // CHECK:STDOUT: - { index: 5, kind: "OpenCurlyBrace", line: {{ *}}[[@LINE-5]], column: 8, indent: 1, spelling: "{", closing_token: 60, has_leading_space: true }
  19. // 8 and 9 trigger special behavior in APInt when mishandling signed versus
  20. // unsigned, so we pay extra attention to those.
  21. var ints: array(i32, 6) = (
  22. // CHECK:STDOUT: - { index: 6, kind: "Var", line: {{ *}}[[@LINE-1]], column: 3, indent: 3, spelling: "var", has_leading_space: true }
  23. // CHECK:STDOUT: - { index: 7, kind: "Identifier", line: {{ *}}[[@LINE-2]], column: 7, indent: 3, spelling: "ints", identifier: 1, has_leading_space: true }
  24. // CHECK:STDOUT: - { index: 8, kind: "Colon", line: {{ *}}[[@LINE-3]], column: 11, indent: 3, spelling: ":" }
  25. // CHECK:STDOUT: - { index: 9, kind: "Array", line: {{ *}}[[@LINE-4]], column: 13, indent: 3, spelling: "array", has_leading_space: true }
  26. // CHECK:STDOUT: - { index: 10, kind: "OpenParen", line: {{ *}}[[@LINE-5]], column: 18, indent: 3, spelling: "(", closing_token: 14 }
  27. // CHECK:STDOUT: - { index: 11, kind: "IntTypeLiteral", line: {{ *}}[[@LINE-6]], column: 19, indent: 3, spelling: "i32" }
  28. // CHECK:STDOUT: - { index: 12, kind: "Comma", line: {{ *}}[[@LINE-7]], column: 22, indent: 3, spelling: "," }
  29. // CHECK:STDOUT: - { index: 13, kind: "IntLiteral", line: {{ *}}[[@LINE-8]], column: 24, indent: 3, spelling: "6", value: "6", has_leading_space: true }
  30. // CHECK:STDOUT: - { index: 14, kind: "CloseParen", line: {{ *}}[[@LINE-9]], column: 25, indent: 3, spelling: ")", opening_token: 10 }
  31. // CHECK:STDOUT: - { index: 15, kind: "Equal", line: {{ *}}[[@LINE-10]], column: 27, indent: 3, spelling: "=", has_leading_space: true }
  32. // CHECK:STDOUT: - { index: 16, kind: "OpenParen", line: {{ *}}[[@LINE-11]], column: 29, indent: 3, spelling: "(", closing_token: 31, has_leading_space: true }
  33. 8,
  34. // CHECK:STDOUT: - { index: 17, kind: "IntLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "8", value: "8", has_leading_space: true }
  35. // CHECK:STDOUT: - { index: 18, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 6, indent: 5, spelling: "," }
  36. 9,
  37. // CHECK:STDOUT: - { index: 19, kind: "IntLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "9", value: "9", has_leading_space: true }
  38. // CHECK:STDOUT: - { index: 20, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 6, indent: 5, spelling: "," }
  39. 0x8,
  40. // CHECK:STDOUT: - { index: 21, kind: "IntLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "0x8", value: "8", has_leading_space: true }
  41. // CHECK:STDOUT: - { index: 22, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 8, indent: 5, spelling: "," }
  42. 0b1000,
  43. // CHECK:STDOUT: - { index: 23, kind: "IntLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "0b1000", value: "8", has_leading_space: true }
  44. // CHECK:STDOUT: - { index: 24, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 11, indent: 5, spelling: "," }
  45. 0o10,
  46. // CHECK:STDOUT: - { index: 25, kind: "IntLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "0o10", value: "8", has_leading_space: true }
  47. // CHECK:STDOUT: - { index: 26, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 9, indent: 5, spelling: "," }
  48. 39999999999999999993,
  49. // CHECK:STDOUT: - { index: 27, kind: "IntLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "39999999999999999993", value: "39999999999999999993", has_leading_space: true }
  50. // CHECK:STDOUT: - { index: 28, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 25, indent: 5, spelling: "," }
  51. 12345678901234567890123456789012345678901234567890123456789,
  52. // CHECK:STDOUT: - { index: 29, kind: "IntLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "12345678901234567890123456789012345678901234567890123456789", value: "12345678901234567890123456789012345678901234567890123456789", has_leading_space: true }
  53. // CHECK:STDOUT: - { index: 30, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 64, indent: 5, spelling: "," }
  54. );
  55. // CHECK:STDOUT: - { index: 31, kind: "CloseParen", line: {{ *}}[[@LINE-1]], column: 3, indent: 3, spelling: ")", opening_token: 16, has_leading_space: true }
  56. // CHECK:STDOUT: - { index: 32, kind: "Semi", line: {{ *}}[[@LINE-2]], column: 4, indent: 3, spelling: ";" }
  57. var floats: array(f64, 7) = (
  58. // CHECK:STDOUT: - { index: 33, kind: "Var", line: {{ *}}[[@LINE-1]], column: 3, indent: 3, spelling: "var", has_leading_space: true }
  59. // CHECK:STDOUT: - { index: 34, kind: "Identifier", line: {{ *}}[[@LINE-2]], column: 7, indent: 3, spelling: "floats", identifier: 2, has_leading_space: true }
  60. // CHECK:STDOUT: - { index: 35, kind: "Colon", line: {{ *}}[[@LINE-3]], column: 13, indent: 3, spelling: ":" }
  61. // CHECK:STDOUT: - { index: 36, kind: "Array", line: {{ *}}[[@LINE-4]], column: 15, indent: 3, spelling: "array", has_leading_space: true }
  62. // CHECK:STDOUT: - { index: 37, kind: "OpenParen", line: {{ *}}[[@LINE-5]], column: 20, indent: 3, spelling: "(", closing_token: 41 }
  63. // CHECK:STDOUT: - { index: 38, kind: "FloatTypeLiteral", line: {{ *}}[[@LINE-6]], column: 21, indent: 3, spelling: "f64" }
  64. // CHECK:STDOUT: - { index: 39, kind: "Comma", line: {{ *}}[[@LINE-7]], column: 24, indent: 3, spelling: "," }
  65. // CHECK:STDOUT: - { index: 40, kind: "IntLiteral", line: {{ *}}[[@LINE-8]], column: 26, indent: 3, spelling: "7", value: "7", has_leading_space: true }
  66. // CHECK:STDOUT: - { index: 41, kind: "CloseParen", line: {{ *}}[[@LINE-9]], column: 27, indent: 3, spelling: ")", opening_token: 37 }
  67. // CHECK:STDOUT: - { index: 42, kind: "Equal", line: {{ *}}[[@LINE-10]], column: 29, indent: 3, spelling: "=", has_leading_space: true }
  68. // CHECK:STDOUT: - { index: 43, kind: "OpenParen", line: {{ *}}[[@LINE-11]], column: 31, indent: 3, spelling: "(", closing_token: 58, has_leading_space: true }
  69. 0.9,
  70. // CHECK:STDOUT: - { index: 44, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "0.9", value: "9*10^-1", has_leading_space: true }
  71. // CHECK:STDOUT: - { index: 45, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 8, indent: 5, spelling: "," }
  72. 8.0,
  73. // CHECK:STDOUT: - { index: 46, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "8.0", value: "80*10^-1", has_leading_space: true }
  74. // CHECK:STDOUT: - { index: 47, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 8, indent: 5, spelling: "," }
  75. 80.0,
  76. // CHECK:STDOUT: - { index: 48, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "80.0", value: "800*10^-1", has_leading_space: true }
  77. // CHECK:STDOUT: - { index: 49, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 9, indent: 5, spelling: "," }
  78. 1.0e7,
  79. // CHECK:STDOUT: - { index: 50, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "1.0e7", value: "10*10^6", has_leading_space: true }
  80. // CHECK:STDOUT: - { index: 51, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 10, indent: 5, spelling: "," }
  81. 1.0e8,
  82. // CHECK:STDOUT: - { index: 52, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "1.0e8", value: "10*10^7", has_leading_space: true }
  83. // CHECK:STDOUT: - { index: 53, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 10, indent: 5, spelling: "," }
  84. 1.0e-8,
  85. // CHECK:STDOUT: - { index: 54, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "1.0e-8", value: "10*10^-9", has_leading_space: true }
  86. // CHECK:STDOUT: - { index: 55, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 11, indent: 5, spelling: "," }
  87. 39999999999999999993.0e39999999999999999993,
  88. // CHECK:STDOUT: - { index: 56, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 5, indent: 5, spelling: "39999999999999999993.0e39999999999999999993", value: "399999999999999999930*10^39999999999999999992", has_leading_space: true }
  89. // CHECK:STDOUT: - { index: 57, kind: "Comma", line: {{ *}}[[@LINE-2]], column: 48, indent: 5, spelling: "," }
  90. );
  91. // CHECK:STDOUT: - { index: 58, kind: "CloseParen", line: {{ *}}[[@LINE-1]], column: 3, indent: 3, spelling: ")", opening_token: 43, has_leading_space: true }
  92. // CHECK:STDOUT: - { index: 59, kind: "Semi", line: {{ *}}[[@LINE-2]], column: 4, indent: 3, spelling: ";" }
  93. }
  94. // CHECK:STDOUT: - { index: 60, kind: "CloseCurlyBrace", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "}", opening_token: 5, has_leading_space: true }
  95. // --- fail_binary_real.carbon
  96. // CHECK:STDOUT: - filename: fail_binary_real.carbon
  97. // CHECK:STDOUT: tokens:
  98. // CHECK:STDERR: fail_binary_real.carbon:[[@LINE+4]]:4: error: binary real number literals are not supported [InvalidRealLiteralRadix]
  99. // CHECK:STDERR: 0b1.0
  100. // CHECK:STDERR: ^
  101. // CHECK:STDERR:
  102. 0b1.0
  103. // CHECK:STDOUT: - { index: 1, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "0b1.0", value: "2*2^-1", has_leading_space: true }
  104. // --- fail_octal_real.carbon
  105. // CHECK:STDOUT: - filename: fail_octal_real.carbon
  106. // CHECK:STDOUT: tokens:
  107. // CHECK:STDERR: fail_octal_real.carbon:[[@LINE+4]]:4: error: octal real number literals are not supported [InvalidRealLiteralRadix]
  108. // CHECK:STDERR: 0o1.0
  109. // CHECK:STDERR: ^
  110. // CHECK:STDERR:
  111. 0o1.0
  112. // CHECK:STDOUT: - { index: 1, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "0o1.0", value: "8*2^-3", has_leading_space: true }
  113. // --- fail_wrong_real_exponent.carbon
  114. // CHECK:STDOUT: - filename: fail_wrong_real_exponent.carbon
  115. // CHECK:STDOUT: tokens:
  116. // CHECK:STDERR: fail_wrong_real_exponent.carbon:[[@LINE+4]]:4: error: expected 'e' to introduce exponent [WrongRealLiteralExponent]
  117. // CHECK:STDERR: 1.0r3
  118. // CHECK:STDERR: ^
  119. // CHECK:STDERR:
  120. 1.0r3
  121. // CHECK:STDOUT: - { index: 1, kind: "Error", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "1.0r3", has_leading_space: true }
  122. // --- fail_invalid_digit.carbon
  123. // CHECK:STDOUT: - filename: fail_invalid_digit.carbon
  124. // CHECK:STDOUT: tokens:
  125. // CHECK:STDERR: fail_invalid_digit.carbon:[[@LINE+4]]:4: error: invalid digit 'a' in hexadecimal numeric literal [InvalidDigit]
  126. // CHECK:STDERR: 0x1a
  127. // CHECK:STDERR: ^
  128. // CHECK:STDERR:
  129. 0x1a
  130. // CHECK:STDOUT: - { index: 1, kind: "Error", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "0x1a", has_leading_space: true }
  131. // --- fail_invalid_separator.carbon
  132. // CHECK:STDOUT: - filename: fail_invalid_separator.carbon
  133. // CHECK:STDOUT: tokens:
  134. // CHECK:STDERR: fail_invalid_separator.carbon:[[@LINE+4]]:3: error: misplaced digit separator in numeric literal [InvalidDigitSeparator]
  135. // CHECK:STDERR: 1__2
  136. // CHECK:STDERR: ^
  137. // CHECK:STDERR:
  138. 1__2
  139. // CHECK:STDOUT: - { index: 1, kind: "IntLiteral", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "1__2", value: "12", has_leading_space: true }
  140. // --- fail_fraction_separator.carbon
  141. // CHECK:STDOUT: - filename: fail_fraction_separator.carbon
  142. // CHECK:STDOUT: tokens:
  143. // CHECK:STDERR: fail_fraction_separator.carbon:[[@LINE+4]]:7: error: misplaced digit separator in numeric literal [InvalidDigitSeparator]
  144. // CHECK:STDERR: 1.0000_0000
  145. // CHECK:STDERR: ^
  146. // CHECK:STDERR:
  147. 1.0000_0000
  148. // CHECK:STDOUT: - { index: 1, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "1.0000_0000", value: "100000000*10^-9", has_leading_space: true }
  149. // --- fail_fraction_separator_at_end.carbon
  150. // CHECK:STDOUT: - filename: fail_fraction_separator_at_end.carbon
  151. // CHECK:STDOUT: tokens:
  152. // CHECK:STDERR: fail_fraction_separator_at_end.carbon:[[@LINE+4]]:6: error: misplaced digit separator in numeric literal [InvalidDigitSeparator]
  153. // CHECK:STDERR: 1.000_
  154. // CHECK:STDERR: ^
  155. // CHECK:STDERR:
  156. 1.000_
  157. // CHECK:STDOUT: - { index: 1, kind: "RealLiteral", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "1.000_", value: "1000*10^-4", has_leading_space: true }
  158. // --- fail_unknown_base.carbon
  159. // CHECK:STDOUT: - filename: fail_unknown_base.carbon
  160. // CHECK:STDOUT: tokens:
  161. // CHECK:STDERR: fail_unknown_base.carbon:[[@LINE+4]]:1: error: unknown base specifier in numeric literal [UnknownBaseSpecifier]
  162. // CHECK:STDERR: 05
  163. // CHECK:STDERR: ^
  164. // CHECK:STDERR:
  165. 05
  166. // CHECK:STDOUT: - { index: 1, kind: "Error", line: {{ *}}[[@LINE-1]], column: 1, indent: 1, spelling: "05", has_leading_space: true }