diff --git a/src/Insect/Parser.purs b/src/Insect/Parser.purs index e96bf23..0184d32 100644 --- a/src/Insect/Parser.purs +++ b/src/Insect/Parser.purs @@ -89,10 +89,10 @@ whiteSpace = token.whiteSpace -- | Parse a number. number ∷ P Decimal number = do - intPart ← digits - - mFracPart ← optionMaybe (append <$> string "." <*> digits) - let fracPart = fromMaybe "" mFracPart + decimalPart ← fractionalPart <|> do + intPart ← digits + mFracPart ← optionMaybe fractionalPart + pure (intPart <> fromMaybe "" mFracPart) mExpPart ← optionMaybe $ try do _ ← string "e" @@ -103,7 +103,7 @@ number = do whiteSpace - let floatStr = intPart <> fracPart <> expPart + let floatStr = decimalPart <> expPart case fromString floatStr of Just num → @@ -118,6 +118,9 @@ number = do ds ← some $ oneOf ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] "a digit" pure $ fromCharArray (fromFoldable ds) + fractionalPart ∷ P String + fractionalPart = (<>) <$> string "." <*> digits + fromCharArray = fromCodePointArray <<< map codePointFromChar signAndDigits ∷ P String diff --git a/test/Main.purs b/test/Main.purs index fbfa6de..c6c5314 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -110,11 +110,28 @@ main = runTest do , "+3.50" ] + allParseAs (Expression (scalar 0.2)) + [ "0.2" + , " 0.2 " + , "+0.2 " + , ".2" + , "+.2" + ] + + allParseAs (Expression (Negate (scalar 0.61))) + [ "-0.61" + , "-.61" + , "- .61" + ] + + shouldParseAs (Expression (scalar 0.05)) ".05" + shouldFail "123.." shouldFail "0.." shouldFail ".0." shouldFail "." - shouldFail ".2" + shouldFail ". 2" + shouldFail "..2" test "Large numbers" do allParseAs (Expression (scalar 1234567890000000.0))