From 4ceecb2606bad38b2cf4d353e0579244c471220d Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Fri, 17 Mar 2023 20:17:44 -0400 Subject: [PATCH] chore: Switch to using an enum --- stdlib/number.gr | 3 +++ stdlib/number.md | 25 +++++++++++++++++++++---- stdlib/runtime/atoi/parse.gr | 11 ++++++++--- stdlib/runtime/atoi/parse.md | 18 +++++++++++++++++- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/stdlib/number.gr b/stdlib/number.gr index 009e2e9563..36ec78ba95 100644 --- a/stdlib/number.gr +++ b/stdlib/number.gr @@ -30,6 +30,9 @@ include "runtime/atof/parse" as Atof include "runtime/unsafe/tags" include "runtime/exception" +from Atoi use { type ParseIntError } + +provide { type ParseIntError } /** * Pi represented as a Number value. * diff --git a/stdlib/number.md b/stdlib/number.md index d8cca04dd6..614fb544ac 100644 --- a/stdlib/number.md +++ b/stdlib/number.md @@ -13,6 +13,22 @@ 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. @@ -709,7 +725,8 @@ No other changes yet. ```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 @@ -731,7 +748,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.**parseFloat** @@ -767,7 +784,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`. @@ -783,7 +800,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 589c620eb4..7efabf8a45 100644 --- a/stdlib/runtime/atoi/parse.gr +++ b/stdlib/runtime/atoi/parse.gr @@ -20,9 +20,14 @@ include "runtime/bigint" as BI include "runtime/numbers" from Numbers use { reducedInteger } -provide exception EmptyString -provide exception InvalidDigit -provide exception InvalidRadix +/** + * Represents an error that can occur when parsing ints. + */ +provide enum ParseIntError { + EmptyString, + InvalidDigit, + InvalidRadix, +} primitive (&&) = "@and" primitive (||) = "@or" diff --git a/stdlib/runtime/atoi/parse.md b/stdlib/runtime/atoi/parse.md index 92d29ceb3a..2b2b1913e3 100644 --- a/stdlib/runtime/atoi/parse.md +++ b/stdlib/runtime/atoi/parse.md @@ -2,6 +2,22 @@ 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. @@ -9,6 +25,6 @@ Functions and constants included in the Parse module. ### Parse.**parseInt** ```grain -parseInt : (string: String, radix: Number) => Result +parseInt : (string: String, radix: Number) => Result ```