Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/strconv/moon.pkg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
"moonbitlang/core/builtin",
"moonbitlang/core/debug",
"moonbitlang/core/double",
"moonbitlang/core/error",
"moonbitlang/core/uint64",
"moonbitlang/core/char",
"moonbitlang/core/array",
Expand Down
25 changes: 19 additions & 6 deletions internal/strconv/pkg.generated.mbti
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
// Generated using `moon info`, DON'T EDIT IT
package "moonbitlang/core/internal/strconv"

import {
"moonbitlang/core/debug",
}

// Values
pub fn parse_bool(StringView) -> Bool raise
pub fn parse_bool(StringView) -> Bool raise StrConvError

pub fn parse_double(StringView) -> Double raise
pub fn parse_double(StringView) -> Double raise StrConvError

pub fn parse_int(StringView, base? : Int) -> Int raise
pub fn parse_int(StringView, base? : Int) -> Int raise StrConvError

pub fn parse_int64(StringView, base? : Int) -> Int64 raise
pub fn parse_int64(StringView, base? : Int) -> Int64 raise StrConvError

pub fn parse_uint(StringView, base? : Int) -> UInt raise
pub fn parse_uint(StringView, base? : Int) -> UInt raise StrConvError

pub fn parse_uint64(StringView, base? : Int) -> UInt64 raise
pub fn parse_uint64(StringView, base? : Int) -> UInt64 raise StrConvError

// Errors
pub(all) suberror StrConvError {
RangeError
SyntaxError
InvalidBase
} derive(Eq, @debug.Debug)
pub fn StrConvError::equal(Self, Self) -> Bool
pub fn StrConvError::message(Self) -> String
pub fn StrConvError::not_equal(Self, Self) -> Bool
pub fn StrConvError::to_repr(Self) -> @debug.Repr

// Types and methods

Expand Down
16 changes: 5 additions & 11 deletions internal/strconv/strconv_bool.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

///|
/// Parse a string and return the represented boolean value or an error.
pub fn parse_bool(str : StringView) -> Bool raise {
pub fn parse_bool(str : StringView) -> Bool raise StrConvError {
lexscan str with longest {
re"^(true|TRUE|True|t|T|1)$" => true
re"^(false|FALSE|False|f|F|0)$" => false
Expand All @@ -24,9 +24,9 @@ pub fn parse_bool(str : StringView) -> Bool raise {

///|
test "parse_bool" {
let tests : Array[(String, Result[Bool, String])] = [
("", Err(syntax_err_str)),
("zutomayo", Err(syntax_err_str)),
let tests : Array[(String, Result[Bool, StrConvError])] = [
("", Err(SyntaxError)),
("zutomayo", Err(SyntaxError)),
("0", Ok(false)),
("f", Ok(false)),
("F", Ok(false)),
Expand All @@ -41,12 +41,6 @@ test "parse_bool" {
("True", Ok(true)),
]
for t in tests {
@test.assert_eq(
Result::Ok(parse_bool(t.0)) catch {
Failure::Failure(err) => Err(err)
err => Err(err.to_string())
},
t.1,
)
@test.assert_eq(Result::Ok(parse_bool(t.0)) catch { err => Err(err) }, t.1)
}
}
21 changes: 9 additions & 12 deletions internal/strconv/strconv_coverage_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,35 @@

///|
/// Whether parsing `s` as an Int64 (in the given base) raises the expected
/// `Failure` (any other error propagates so it is not masked as a parse error).
fn int64_raises(s : String, base : Int) -> Bool raise {
/// `StrConvError`.
fn int64_raises(s : String, base : Int) -> Bool {
try {
@strconv.parse_int64(s, base~) |> ignore
false
} catch {
Failure::Failure(_) => true
err => raise err
RangeError | SyntaxError | InvalidBase => true
}
}

///|
/// Whether parsing `s` as a Double raises the expected `Failure`.
fn double_raises(s : String) -> Bool raise {
/// Whether parsing `s` as a Double raises the expected `StrConvError`.
fn double_raises(s : String) -> Bool {
try {
@strconv.parse_double(s) |> ignore
false
} catch {
Failure::Failure(_) => true
err => raise err
RangeError | SyntaxError | InvalidBase => true
}
}

///|
/// Whether parsing `s` as an Int (32-bit) raises the expected `Failure`.
fn int_raises(s : String) -> Bool raise {
/// Whether parsing `s` as an Int (32-bit) raises the expected `StrConvError`.
fn int_raises(s : String) -> Bool {
try {
@strconv.parse_int(s) |> ignore
false
} catch {
Failure::Failure(_) => true
err => raise err
RangeError | SyntaxError | InvalidBase => true
}
}

Expand Down
37 changes: 12 additions & 25 deletions internal/strconv/strconv_decimal.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ fn Decimal::from_int64_priv(v : Int64) -> Decimal {
}

///|
fn parse_decimal_priv(str : StringView) -> Decimal raise {
fn parse_decimal_priv(str : StringView) -> Decimal raise StrConvError {
parse_decimal_from_view(str)
}

///|
fn parse_decimal_from_view(str : StringView) -> Decimal raise {
fn parse_decimal_from_view(str : StringView) -> Decimal raise StrConvError {
let d = Decimal::new_priv()
let mut has_dp = false
let mut has_digits = false
Expand Down Expand Up @@ -172,7 +172,7 @@ fn parse_decimal_from_view(str : StringView) -> Decimal raise {
}

///|
fn Decimal::to_double_priv(self : Decimal) -> Double raise {
fn Decimal::to_double_priv(self : Decimal) -> Double raise StrConvError {
let mut exponent = 0
let mut mantissa = 0L
// check the underflow and overflow
Expand Down Expand Up @@ -679,25 +679,18 @@ test "parse decimal with underscore" {

///|
test "parse decimal error" {
assert_true(@test.expect_error(() => parse_decimal_priv("1e")) is SyntaxError)
assert_true(
@test.expect_error(() => parse_decimal_priv("1e"))
is Failure("invalid syntax"),
@test.expect_error(() => parse_decimal_priv("1e+")) is SyntaxError,
)
assert_true(
@test.expect_error(() => parse_decimal_priv("1e+"))
is Failure("invalid syntax"),
@test.expect_error(() => parse_decimal_priv("1e_")) is SyntaxError,
)
assert_true(
@test.expect_error(() => parse_decimal_priv("1e_"))
is Failure("invalid syntax"),
@test.expect_error(() => parse_decimal_priv("1-23")) is SyntaxError,
)
assert_true(
@test.expect_error(() => parse_decimal_priv("1-23"))
is Failure("invalid syntax"),
)
assert_true(
@test.expect_error(() => parse_decimal_priv("1.2.3"))
is Failure("invalid syntax"),
@test.expect_error(() => parse_decimal_priv("1.2.3")) is SyntaxError,
)
}

Expand Down Expand Up @@ -744,14 +737,8 @@ test "rounded_integer overflow when decimal_point > 20" {
///|
test "corner cases" {
let _ = parse_decimal_priv(".123")
assert_true(
@test.expect_error(() => parse_decimal_priv("."))
is Failure("invalid syntax"),
)
assert_true(
@test.expect_error(() => parse_decimal_priv("-"))
is Failure("invalid syntax"),
)
assert_true(@test.expect_error(() => parse_decimal_priv(".")) is SyntaxError)
assert_true(@test.expect_error(() => parse_decimal_priv("-")) is SyntaxError)
}

///|
Expand All @@ -776,14 +763,14 @@ test "parse_double large magnitude cancellation" {

let input = "1" + String::make(399, '0') + ".5"
try parse_double(input) catch {
e => inspect(e, content="Failure(value out of range)")
e => @debug.debug_inspect(e, content="RangeError")
} noraise {
_ => fail("expected parse_double to raise")
}

let input = "1" + String::make(800, '0') + ".5"
try parse_double(input) catch {
e => inspect(e, content="Failure(value out of range)")
e => @debug.debug_inspect(e, content="RangeError")
} noraise {
_ => fail("expected parse_double to raise")
}
Expand Down
75 changes: 31 additions & 44 deletions internal/strconv/strconv_double.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let max_mantissa_fast_path : UInt64 = 2UL << mantissa_explicit_bits
///
/// An exponent value exp scales the mantissa (significand) by 10^exp.
/// For example, "1.23e2" represents 1.23 × 10² = 123.
pub fn parse_double(str : StringView) -> Double raise {
pub fn parse_double(str : StringView) -> Double raise StrConvError {
guard !str.is_empty() else { syntax_err() }
guard check_underscore(str) else { syntax_err() }
// validate its a number
Expand Down Expand Up @@ -149,13 +149,13 @@ fn Number::try_fast_path(self : Number) -> Double? {

///|
test "parse_double" {
let tests : Array[(String, Result[Double, String])] = [
("", Err(syntax_err_str)),
("1x", Err(syntax_err_str)),
("1.1.", Err(syntax_err_str)),
("1e", Err(syntax_err_str)),
("1e-", Err(syntax_err_str)),
(".e-1", Err(syntax_err_str)),
let tests : Array[(String, Result[Double, StrConvError])] = [
("", Err(SyntaxError)),
("1x", Err(SyntaxError)),
("1.1.", Err(SyntaxError)),
("1e", Err(SyntaxError)),
("1e-", Err(SyntaxError)),
(".e-1", Err(SyntaxError)),
("1", Ok(1.0)),
("+1", Ok(1.0)),
("1e23", Ok(1.0e23)),
Expand Down Expand Up @@ -202,26 +202,14 @@ test "parse_double" {
("1.7976931348623158e308", Ok(1.7976931348623157e308)),
("-1.7976931348623158e308", Ok(-1.7976931348623157e308)),
("1e308", Ok(1.0e308)),
(
"1.7976931348623159e308",
Err(
// zeros
// large double
range_err_str,
),
),
(
"-1.7976931348623159e308",
Err(
// overflow
range_err_str,
),
),
("2e308", Err(range_err_str)),
("1e309", Err(range_err_str)),
("1e310", Err(range_err_str)),
("1e400", Err(range_err_str)),
("1e40000", Err(range_err_str)),
// zeros / large double overflow
("1.7976931348623159e308", Err(RangeError)),
("-1.7976931348623159e308", Err(RangeError)),
("2e308", Err(RangeError)),
("1e309", Err(RangeError)),
("1e310", Err(RangeError)),
("1e400", Err(RangeError)),
("1e40000", Err(RangeError)),
// denormalized
("1e-305", Ok(1.0e-305)),
("1e-306", Ok(1.0e-306)),
Expand All @@ -240,26 +228,25 @@ test "parse_double" {
("1e-400000", Ok(0.0)),
// underscores
("1_23.50_0_0e+1_2", Ok(1.235e+14)),
("-_123.5e+12", Err(syntax_err_str)),
("+_123.5e+12", Err(syntax_err_str)),
("_123.5e+12", Err(syntax_err_str)),
("1__23.5e+12", Err(syntax_err_str)),
("123_.5e+12", Err(syntax_err_str)),
("123._5e+12", Err(syntax_err_str)),
("123.5_e+12", Err(syntax_err_str)),
("123.5__0e+12", Err(syntax_err_str)),
("123.5e_+12", Err(syntax_err_str)),
("123.5e+_12", Err(syntax_err_str)),
("123.5e_-12", Err(syntax_err_str)),
("123.5e-_12", Err(syntax_err_str)),
("123.5e+1__2", Err(syntax_err_str)),
("123.5e+12_", Err(syntax_err_str)),
("-_123.5e+12", Err(SyntaxError)),
("+_123.5e+12", Err(SyntaxError)),
("_123.5e+12", Err(SyntaxError)),
("1__23.5e+12", Err(SyntaxError)),
("123_.5e+12", Err(SyntaxError)),
("123._5e+12", Err(SyntaxError)),
("123.5_e+12", Err(SyntaxError)),
("123.5__0e+12", Err(SyntaxError)),
("123.5e_+12", Err(SyntaxError)),
("123.5e+_12", Err(SyntaxError)),
("123.5e_-12", Err(SyntaxError)),
("123.5e-_12", Err(SyntaxError)),
("123.5e+1__2", Err(SyntaxError)),
("123.5e+12_", Err(SyntaxError)),
]
for t in tests {
@test.assert_eq(
Result::Ok(parse_double(t.0)) catch {
Failure::Failure(err) => Err(err)
err => Err(err.to_string())
err => Err(err)
},
t.1,
)
Expand Down
39 changes: 30 additions & 9 deletions internal/strconv/strconv_errors.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,46 @@
// limitations under the License.

///|
let range_err_str = "value out of range"
/// Structured parse errors for the internal strconv helpers.
///
/// In-module callers (e.g. the JSON number lexer) match on the variants to
/// distinguish out-of-range values from syntax problems instead of comparing
/// failure message strings. The public wrappers in `moonbitlang/core/string`
/// translate these back into `Failure` with the historical message strings,
/// so externally observable behavior is unchanged.
pub(all) suberror StrConvError {
RangeError
SyntaxError
InvalidBase
} derive(Eq, @debug.Debug)

///|
let syntax_err_str = "invalid syntax"
pub extend StrConvError with Eq::{equal, not_equal}

///|
let base_err_str = "invalid base"
pub extend StrConvError with @debug.Debug::{to_repr}

///|
fn[T] range_err() -> T raise {
raise Failure::Failure(range_err_str)
/// The historical human-readable message for this error.
pub fn StrConvError::message(self : Self) -> String {
match self {
RangeError => "value out of range"
SyntaxError => "invalid syntax"
InvalidBase => "invalid base"
}
}

///|
fn[T] syntax_err() -> T raise {
raise Failure::Failure(syntax_err_str)
fn[T] range_err() -> T raise StrConvError {
raise RangeError
}

///|
fn[T] base_err() -> T raise {
raise Failure::Failure(base_err_str)
fn[T] syntax_err() -> T raise StrConvError {
raise SyntaxError
}

///|
fn[T] base_err() -> T raise StrConvError {
raise InvalidBase
}
Loading
Loading