Skip to content

Commit

Permalink
Makes "float" parser of GenTokenParser parse negative numbers.
Browse files Browse the repository at this point in the history
Resolves #115
  • Loading branch information
mstream authored and jamesdbrock committed Jun 5, 2023
1 parent 2251099 commit accf929
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Notable changes to this project are documented in this file. The format is based
## [Unreleased]

Bugfixes:
- `float` parser of `GenTokenParser` does not parse negative numbers (by @mstream)

Breaking changes:

Expand Down
9 changes: 6 additions & 3 deletions src/Parsing/Token.purs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import Data.Int (toNumber)
import Data.List (List(..))
import Data.List as List
import Data.List.NonEmpty (NonEmptyList)
import Data.Maybe (Maybe(..), maybe)
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.Number (pow)
import Data.String (null, toLower)
import Data.String.CodePoints (codePointFromChar)
Expand All @@ -43,7 +43,7 @@ import Data.String.CodeUnits as SCU
import Data.String.Unicode as Unicode
import Data.Tuple (Tuple(..))
import Parsing (ParseState(..), ParserT, Position, consume, fail, getParserT, stateParserT)
import Parsing.Combinators (between, choice, notFollowedBy, option, sepBy, sepBy1, skipMany, skipMany1, try, tryRethrow, (<?>), (<??>))
import Parsing.Combinators (between, choice, notFollowedBy, option, optionMaybe, sepBy, sepBy1, skipMany, skipMany1, try, tryRethrow, (<?>), (<??>))
import Parsing.String (char, satisfy, satisfyCodePoint, string)
import Parsing.String.Basic (alphaNum, digit, hexDigit, letter, noneOf, octDigit, oneOf, space, upper)
import Parsing.String.Basic as Basic
Expand Down Expand Up @@ -608,7 +608,10 @@ makeTokenParser (LanguageDef languageDef) =

-- floats
floating :: ParserT String m Number
floating = decimal >>= fractExponent
floating = do
f <- fromMaybe identity <$> optionMaybe sign
x <- decimal >>= fractExponent
pure $ f x

natFloat :: ParserT String m (Either Int Number)
natFloat = char '0' *> zeroNumFloat
Expand Down
3 changes: 3 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ tokenParserFloatTest = do
-- parse float
parseTest "100.5" 100.5 testTokenParser.float

-- parse a negative float
parseTest "-100.5" (-100.5) testTokenParser.float

-- parse float with exponent
parseTest "100e1" 1000.0 testTokenParser.float

Expand Down

0 comments on commit accf929

Please sign in to comment.