diff --git a/compiler/test/stdlib/number.test.gr b/compiler/test/stdlib/number.test.gr
index c1c7e28485..1b22bf2e2f 100644
--- a/compiler/test/stdlib/number.test.gr
+++ b/compiler/test/stdlib/number.test.gr
@@ -600,6 +600,18 @@ assert Result.isErr(Number.parseInt("zzzzz", 37))
assert Result.isErr(Number.parseInt("zzzzz", 9223372036854775807))
assert Result.isErr(Number.parseInt("10", 1.23))
assert Result.isErr(Number.parseInt("10", 2/3))
+assert match (Number.parseInt("zzzzz", 10)) {
+ Err(Number.PraseIntInvalidDigit) => true,
+ _ => false,
+}
+assert match (Number.parseInt("", 10)) {
+ Err(Number.PraseIntEmptyString) => true,
+ _ => false,
+}
+assert match (Number.parseInt("10", 2/3)) {
+ Err(Number.ParseIntInvalidRadix) => true,
+ _ => false,
+}
// Number.parse
// These tests primarily focus on rational parsing
diff --git a/stdlib/number.gr b/stdlib/number.gr
index 518541ef2b..a300f28e45 100644
--- a/stdlib/number.gr
+++ b/stdlib/number.gr
@@ -30,9 +30,17 @@ include "runtime/atof/parse" as Atof
include "runtime/unsafe/tags"
include "runtime/exception"
-from Atoi use { type ParseIntError }
+from Atoi use {
+ exception PraseIntEmptyString,
+ exception PraseIntInvalidDigit,
+ exception ParseIntInvalidRadix,
+}
-provide { type ParseIntError }
+provide {
+ exception PraseIntEmptyString,
+ exception PraseIntInvalidDigit,
+ exception ParseIntInvalidRadix,
+}
/**
* Pi represented as a Number value.
*
@@ -423,9 +431,10 @@ provide let isInfinite = (x: Number) => {
*
* @param string: The string to parse
* @param radix: The number system base to use when parsing the input string
- * @returns `Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise
+ * @returns `Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing a parseInt exception
*
* @since v0.4.5
+ * @history v0.6.0: Switched from a string based error message to a custom exception
*/
provide let parseInt = Atoi.parseInt
diff --git a/stdlib/number.md b/stdlib/number.md
index afcec33a92..6f1f6129b6 100644
--- a/stdlib/number.md
+++ b/stdlib/number.md
@@ -13,22 +13,6 @@ No other changes yet.
include "number"
```
-## Types
-
-Type declarations included in the Number module.
-
-### Number.**ParseIntError**
-
-```grain
-enum ParseIntError {
- EmptyString,
- InvalidDigit,
- InvalidRadix,
-}
-```
-
-Represents an error that can occur when parsing ints.
-
## Values
Functions and constants included in the Number module.
@@ -719,14 +703,20 @@ Returns:
### Number.**parseInt**
-
-Added in 0.4.5
-No other changes yet.
+
+Added in 0.4.5
+
+
+
version
changes
+
+
+
next
Switched from a string based error message to a custom exception
+
+
```grain
-parseInt :
- (string: String, radix: Number) => Result
+parseInt : (string: String, radix: Number) => Result
```
Parses a string representation of an integer into a `Number` using the
@@ -748,7 +738,7 @@ Returns:
|type|description|
|----|-----------|
-|`Result`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
+|`Result`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing a parseInt exception|
### Number.**parseFloat**
@@ -784,7 +774,7 @@ No other changes yet.
```grain
-parse : (input: String) => Result
+parse : (input: String) => Result
```
Parses a string representation of an integer, float, or rational into a `Number`.
@@ -800,7 +790,7 @@ Returns:
|type|description|
|----|-----------|
-|`Result`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
+|`Result`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
### Number.**sin**
diff --git a/stdlib/runtime/atoi/parse.gr b/stdlib/runtime/atoi/parse.gr
index 7efabf8a45..885e601ba5 100644
--- a/stdlib/runtime/atoi/parse.gr
+++ b/stdlib/runtime/atoi/parse.gr
@@ -21,13 +21,23 @@ include "runtime/numbers"
from Numbers use { reducedInteger }
/**
- * Represents an error that can occur when parsing ints.
+ * Represents an error caused by trying to parse an empty string.
+ *
+ * @since v0.6.0
*/
-provide enum ParseIntError {
- EmptyString,
- InvalidDigit,
- InvalidRadix,
-}
+provide exception PraseIntEmptyString
+/**
+ * Represents an error caused by trying to parse a string with an invalid character.
+ *
+ * @since v0.6.0
+ */
+provide exception PraseIntInvalidDigit
+/**
+ * Represents an error caused by trying to parse with an invalid radix.
+ *
+ * @since v0.6.0
+ */
+provide exception ParseIntInvalidRadix
primitive (&&) = "@and"
primitive (||) = "@or"
@@ -67,11 +77,11 @@ provide let parseInt = (string: String, radix: Number) => {
radix < WasmI32.fromGrain(2) ||
radix > WasmI32.fromGrain(36)
) {
- return Err(InvalidRadix)
+ return Err(ParseIntInvalidRadix)
}
if (WasmI32.eqz(strLen)) {
- return Err(EmptyString)
+ return Err(PraseIntEmptyString)
}
let mut char = WasmI32.load8U(offset, 0n)
@@ -132,12 +142,12 @@ provide let parseInt = (string: String, radix: Number) => {
c when c - _CHAR_A < 26n => digit = char - _CHAR_A + 10n,
c when c - _CHAR_a < 26n => digit = char - _CHAR_a + 10n,
_ => {
- return Err(InvalidDigit)
+ return Err(PraseIntInvalidDigit)
},
}
if (digit >= WasmI32.wrapI64(radix)) {
- return Err(InvalidDigit)
+ return Err(PraseIntInvalidDigit)
}
let digit = WasmI64.extendI32U(digit)
@@ -183,7 +193,7 @@ provide let parseInt = (string: String, radix: Number) => {
}
from WasmI64 use { (*) }
// TODO: Verify this is suitable for handling "_"
- if (WasmI32.eqz(sawDigit)) return Err(InvalidDigit)
+ if (WasmI32.eqz(sawDigit)) return Err(PraseIntInvalidDigit)
if (WasmI32.eqz(isBigInt)) {
let value = if (negative) value else value * -1N
diff --git a/stdlib/runtime/atoi/parse.md b/stdlib/runtime/atoi/parse.md
index 2b2b1913e3..92d29ceb3a 100644
--- a/stdlib/runtime/atoi/parse.md
+++ b/stdlib/runtime/atoi/parse.md
@@ -2,22 +2,6 @@
title: Parse
---
-## Types
-
-Type declarations included in the Parse module.
-
-### Parse.**ParseIntError**
-
-```grain
-enum ParseIntError {
- EmptyString,
- InvalidDigit,
- InvalidRadix,
-}
-```
-
-Represents an error that can occur when parsing ints.
-
## Values
Functions and constants included in the Parse module.
@@ -25,6 +9,6 @@ Functions and constants included in the Parse module.
### Parse.**parseInt**
```grain
-parseInt : (string: String, radix: Number) => Result
+parseInt : (string: String, radix: Number) => Result
```