hayleigh-dot-dev / nibble

A string parsing library heavily inspired by elm/parser.
MIT License
51 stars 7 forks source link

🐛 `int` and `float` do not handle negative numbers. #2

Closed hayleigh-dot-dev closed 6 months ago

hayleigh-dot-dev commented 1 year ago

Leading -'s are ignored in the int/float parsers, meaning negative inputs will fail! This is an easy fix, we can take the int/float parsers we have, move them into int_help/float_help and do:

pub fn int() -> Parser(Int, ctx) {
 one_of[
    // Attempt to parse a leading `-` and then negate the resulting integer.
    succeed(int.negate)
    |> drop(grapheme("-"))
    |> keep(int_help()),
    // No leading `-` was found so just trying parsing a regular integer.
    int_help()
  ])
}