diff --git a/Parser/Core.hs b/Parser/Core.hs index d0fdf91..a258311 100644 --- a/Parser/Core.hs +++ b/Parser/Core.hs @@ -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 [] diff --git a/Parser/Meshparser.hs b/Parser/Meshparser.hs index af0368d..96a180a 100644 --- a/Parser/Meshparser.hs +++ b/Parser/Meshparser.hs @@ -24,5 +24,5 @@ meshToArr = parseVertice :: Parser (Double, Double) parseVertice = (,) <$> - (char 'v' *> spaces *> posDouble) <*> - (spaces *> posDouble) + (char 'v' *> spaces *> allDouble) <*> + (spaces *> allDouble)