Skip to content

Commit 8db79a6

Browse files
committed
chore: Switch back to an exception
1 parent 8ba7ea0 commit 8db79a6

File tree

5 files changed

+60
-55
lines changed

5 files changed

+60
-55
lines changed

compiler/test/stdlib/number.test.gr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,18 @@ assert Result.isErr(Number.parseInt("zzzzz", 37))
600600
assert Result.isErr(Number.parseInt("zzzzz", 9223372036854775807))
601601
assert Result.isErr(Number.parseInt("10", 1.23))
602602
assert Result.isErr(Number.parseInt("10", 2/3))
603+
assert match (Number.parseInt("zzzzz", 10)) {
604+
Err(Number.PraseIntInvalidDigit) => true,
605+
_ => false,
606+
}
607+
assert match (Number.parseInt("", 10)) {
608+
Err(Number.PraseIntEmptyString) => true,
609+
_ => false,
610+
}
611+
assert match (Number.parseInt("10", 2/3)) {
612+
Err(Number.ParseIntInvalidRadix) => true,
613+
_ => false,
614+
}
603615

604616
// Number.parse
605617
// These tests primarily focus on rational parsing

stdlib/number.gr

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@ include "runtime/atof/parse" as Atof
3030
include "runtime/unsafe/tags"
3131
include "runtime/exception"
3232

33-
from Atoi use { type ParseIntError }
33+
from Atoi use {
34+
exception PraseIntEmptyString,
35+
exception PraseIntInvalidDigit,
36+
exception ParseIntInvalidRadix,
37+
}
3438

35-
provide { type ParseIntError }
39+
provide {
40+
exception PraseIntEmptyString,
41+
exception PraseIntInvalidDigit,
42+
exception ParseIntInvalidRadix,
43+
}
3644
/**
3745
* Pi represented as a Number value.
3846
*
@@ -423,9 +431,10 @@ provide let isInfinite = (x: Number) => {
423431
*
424432
* @param string: The string to parse
425433
* @param radix: The number system base to use when parsing the input string
426-
* @returns `Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise
434+
* @returns `Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing a parseInt exception
427435
*
428436
* @since v0.4.5
437+
* @history v0.6.0: Switched from a string based error message to a custom exception
429438
*/
430439
provide let parseInt = Atoi.parseInt
431440

stdlib/number.md

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,6 @@ No other changes yet.
1313
include "number"
1414
```
1515

16-
## Types
17-
18-
Type declarations included in the Number module.
19-
20-
### Number.**ParseIntError**
21-
22-
```grain
23-
enum ParseIntError {
24-
EmptyString,
25-
InvalidDigit,
26-
InvalidRadix,
27-
}
28-
```
29-
30-
Represents an error that can occur when parsing ints.
31-
3216
## Values
3317

3418
Functions and constants included in the Number module.
@@ -719,14 +703,20 @@ Returns:
719703

720704
### Number.**parseInt**
721705

722-
<details disabled>
723-
<summary tabindex="-1">Added in <code>0.4.5</code></summary>
724-
No other changes yet.
706+
<details>
707+
<summary>Added in <code>0.4.5</code></summary>
708+
<table>
709+
<thead>
710+
<tr><th>version</th><th>changes</th></tr>
711+
</thead>
712+
<tbody>
713+
<tr><td><code>next</code></td><td>Switched from a string based error message to a custom exception</td></tr>
714+
</tbody>
715+
</table>
725716
</details>
726717

727718
```grain
728-
parseInt :
729-
(string: String, radix: Number) => Result<Number, Atoi.ParseIntError>
719+
parseInt : (string: String, radix: Number) => Result<Number, Exception>
730720
```
731721

732722
Parses a string representation of an integer into a `Number` using the
@@ -748,7 +738,7 @@ Returns:
748738

749739
|type|description|
750740
|----|-----------|
751-
|`Result<Number, Atoi.ParseIntError>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
741+
|`Result<Number, Exception>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing a parseInt exception|
752742

753743
### Number.**parseFloat**
754744

@@ -784,7 +774,7 @@ No other changes yet.
784774
</details>
785775

786776
```grain
787-
parse : (input: String) => Result<Number, Atoi.ParseIntError>
777+
parse : (input: String) => Result<Number, Exception>
788778
```
789779

790780
Parses a string representation of an integer, float, or rational into a `Number`.
@@ -800,7 +790,7 @@ Returns:
800790

801791
|type|description|
802792
|----|-----------|
803-
|`Result<Number, Atoi.ParseIntError>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
793+
|`Result<Number, Exception>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
804794

805795
### Number.**sin**
806796

stdlib/runtime/atoi/parse.gr

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,23 @@ include "runtime/numbers"
2121
from Numbers use { reducedInteger }
2222

2323
/**
24-
* Represents an error that can occur when parsing ints.
24+
* Represents an error caused by trying to parse an empty string.
25+
*
26+
* @since v0.6.0
2527
*/
26-
provide enum ParseIntError {
27-
EmptyString,
28-
InvalidDigit,
29-
InvalidRadix,
30-
}
28+
provide exception PraseIntEmptyString
29+
/**
30+
* Represents an error caused by trying to parse a string with an invalid character.
31+
*
32+
* @since v0.6.0
33+
*/
34+
provide exception PraseIntInvalidDigit
35+
/**
36+
* Represents an error caused by trying to parse with an invalid radix.
37+
*
38+
* @since v0.6.0
39+
*/
40+
provide exception ParseIntInvalidRadix
3141

3242
primitive (&&) = "@and"
3343
primitive (||) = "@or"
@@ -67,11 +77,11 @@ provide let parseInt = (string: String, radix: Number) => {
6777
radix < WasmI32.fromGrain(2) ||
6878
radix > WasmI32.fromGrain(36)
6979
) {
70-
return Err(InvalidRadix)
80+
return Err(ParseIntInvalidRadix)
7181
}
7282

7383
if (WasmI32.eqz(strLen)) {
74-
return Err(EmptyString)
84+
return Err(PraseIntEmptyString)
7585
}
7686

7787
let mut char = WasmI32.load8U(offset, 0n)
@@ -132,12 +142,12 @@ provide let parseInt = (string: String, radix: Number) => {
132142
c when c - _CHAR_A < 26n => digit = char - _CHAR_A + 10n,
133143
c when c - _CHAR_a < 26n => digit = char - _CHAR_a + 10n,
134144
_ => {
135-
return Err(InvalidDigit)
145+
return Err(PraseIntInvalidDigit)
136146
},
137147
}
138148

139149
if (digit >= WasmI32.wrapI64(radix)) {
140-
return Err(InvalidDigit)
150+
return Err(PraseIntInvalidDigit)
141151
}
142152

143153
let digit = WasmI64.extendI32U(digit)
@@ -183,7 +193,7 @@ provide let parseInt = (string: String, radix: Number) => {
183193
}
184194
from WasmI64 use { (*) }
185195
// TODO: Verify this is suitable for handling "_"
186-
if (WasmI32.eqz(sawDigit)) return Err(InvalidDigit)
196+
if (WasmI32.eqz(sawDigit)) return Err(PraseIntInvalidDigit)
187197

188198
if (WasmI32.eqz(isBigInt)) {
189199
let value = if (negative) value else value * -1N

stdlib/runtime/atoi/parse.md

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,13 @@
22
title: Parse
33
---
44

5-
## Types
6-
7-
Type declarations included in the Parse module.
8-
9-
### Parse.**ParseIntError**
10-
11-
```grain
12-
enum ParseIntError {
13-
EmptyString,
14-
InvalidDigit,
15-
InvalidRadix,
16-
}
17-
```
18-
19-
Represents an error that can occur when parsing ints.
20-
215
## Values
226

237
Functions and constants included in the Parse module.
248

259
### Parse.**parseInt**
2610

2711
```grain
28-
parseInt : (string: String, radix: Number) => Result<Number, ParseIntError>
12+
parseInt : (string: String, radix: Number) => Result<Number, Exception>
2913
```
3014

0 commit comments

Comments
 (0)