numbers: Nat parser

This commit is contained in:
Nathan McCarty 2025-01-24 22:18:09 -05:00
parent b0504f04bc
commit fed48dd5eb

View file

@ -9,6 +9,10 @@ import Data.Vect
import Control.Eff
```
<!-- idris
import System
-->
## Base Abstraction
```idris
@ -52,6 +56,20 @@ hex = MkBase 16
```idris
export
nat : Base -> Parser Nat
nat b = do
error <- replaceError "Expected digit"
(first ::: rest) <- atLeastOne error parseDigit
pure $ foldl (\acc, e => 10 * acc + e) first rest
where
parseDigit : Parser Nat
parseDigit = do
GotChar char <- parseChar (hasDigit b) id
| GotError e => throwParseError "\{show e} is not a digit"
| EndOfInput => throwParseError "End Of Input"
case digitValue b char of
Nothing =>
throwParseError "Failed to parse as base \{show b.base}: \{show char}"
Just x => pure x
export
natBase10 : Parser Nat