PARSER: add parser for negative doubles

This commit is contained in:
hasufell 2014-10-11 04:39:51 +02:00
parent 7fb3588300
commit 6c66a7acb3
2 changed files with 27 additions and 2 deletions

View File

@ -6,6 +6,8 @@ module Parser.Core (Parser,
char,
posInt,
posDouble,
negDouble,
allDouble,
oneOrMore,
zeroOrMore,
spaces) where
@ -94,6 +96,29 @@ posDouble =
where (ns, rest) = span isDigit xs
-- |Creates a Parser that accepts negative doubles.
-- Both -131.31 and -132 are valid.
negDouble :: Parser Double
negDouble =
(negate <$>) $
(read <$>) $
(\x y z -> x ++ [y] ++ z) <$>
(char '-' *> MkParser f) <*>
char '.' <*>
MkParser f <|>
(char '-' *> MkParser f)
where
f xs
| null ns = Nothing
| otherwise = Just (ns, rest)
where (ns, rest) = span isDigit xs
-- |Creates a Parser that accepts both positive and negative doubles.
allDouble :: Parser Double
allDouble = negDouble <|> posDouble
-- |Convert a given Parser to a Parser that accepts zero or more occurences.
zeroOrMore :: Parser a -> Parser [a]
zeroOrMore p = oneOrMore p <|> pure []

View File

@ -24,5 +24,5 @@ meshToArr =
parseVertice :: Parser (Double, Double)
parseVertice =
(,) <$>
(char 'v' *> spaces *> posDouble) <*>
(spaces *> posDouble)
(char 'v' *> spaces *> allDouble) <*>
(spaces *> allDouble)