PARSER: add parser for negative doubles
This commit is contained in:
parent
7fb3588300
commit
6c66a7acb3
@ -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 []
|
||||
|
@ -24,5 +24,5 @@ meshToArr =
|
||||
parseVertice :: Parser (Double, Double)
|
||||
parseVertice =
|
||||
(,) <$>
|
||||
(char 'v' *> spaces *> posDouble) <*>
|
||||
(spaces *> posDouble)
|
||||
(char 'v' *> spaces *> allDouble) <*>
|
||||
(spaces *> allDouble)
|
||||
|
Loading…
Reference in New Issue
Block a user