Skip to content

Commit

Permalink
Allow starting a number with a decimal point
Browse files Browse the repository at this point in the history
Fixes #221.
  • Loading branch information
triallax committed May 13, 2022
1 parent 4a15f17 commit c774ab5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/Insect/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ whiteSpace = token.whiteSpace
-- | Parse a number.
number P Decimal
number = do
intPartdigits

mFracPart ← optionMaybe (append <$> string "." <*> digits)
let fracPart = fromMaybe "" mFracPart
decimalPartfractionalPart <|> do
intPart ← digits
mFracPart ← optionMaybe fractionalPart
pure (intPart <> fromMaybe "" mFracPart)

mExpPart ← optionMaybe $ try do
_ ← string "e"
Expand All @@ -103,7 +103,7 @@ number = do

whiteSpace

let floatStr = intPart <> fracPart <> expPart
let floatStr = decimalPart <> expPart

case fromString floatStr of
Just num →
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,17 @@ main = runTest do
, "+3.50"
]

allParseAs (Expression (scalar 0.2))
[ "0.2"
, " 0.2 "
, "+0.2 "
, ".2"
]

shouldFail "123.."
shouldFail "0.."
shouldFail ".0."
shouldFail "."
shouldFail ".2"

test "Large numbers" do
allParseAs (Expression (scalar 1234567890000000.0))
Expand Down

0 comments on commit c774ab5

Please sign in to comment.