From 8a0a2eae50237aaa8b9bd6600a0b9c8ef0fd70c7 Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Wed, 27 Dec 2023 17:17:50 +0100 Subject: [PATCH] Fix multiple line of expression MINOR --- lvtc/app/Main.hs | 5 ++--- lvtc/src/Expression.hs | 31 +++++++++++++++++++++++++++++-- lvtc/test/Spec.hs | 2 +- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lvtc/app/Main.hs b/lvtc/app/Main.hs index 8703237..aa337f0 100644 --- a/lvtc/app/Main.hs +++ b/lvtc/app/Main.hs @@ -7,11 +7,10 @@ module Main (main) where -import Lib import Expression import Parser main :: IO () --- main = print $ runParser (parseExpresion) "fn main () -> Int \n{\n <- 0;\n};\n" +main = print $ runParser (parseExpresion) "fn main () -> Int \n{\n <- 0;\n};\n" -- main = print $ runParser (parseExpresion) "alias abc def;\n" -main = print $ runParser (parseExpresion) "// this is a comment\n" +-- main = print $ runParser (parseExpresion) "// this is a comment\n" diff --git a/lvtc/src/Expression.hs b/lvtc/src/Expression.hs index 427a103..711bba1 100644 --- a/lvtc/src/Expression.hs +++ b/lvtc/src/Expression.hs @@ -28,8 +28,29 @@ instance Eq Expression where (==) (Comment str1) (Comment str2) = str1 == str2 (==) _ _ = False +countBracketsForFunction :: Int -> String -> Int +countBracketsForFunction _ [] = 0 +countBracketsForFunction 1 ['\n', '}', ';', '\n'] = 0 +countBracketsForFunction n ['\n', '}', ';', '\n'] = n +countBracketsForFunction 1 ('}':_) = 1 +countBracketsForFunction n ('{':xs) = countBracketsForFunction (n + 1) xs +countBracketsForFunction n ('}':xs) = countBracketsForFunction (n - 1) xs +countBracketsForFunction n ('\\':_:xs) = countBracketsForFunction n xs +countBracketsForFunction n (_:xs) = countBracketsForFunction n xs + parseFunction :: Parser Expression -parseFunction = Function <$> ((++) <$> (parseString "fn " <|> parseString "export fn") <*> parseAllCharUntil "\n};\n") +parseFunction = Parser f + where + f str = case runParser ( + Function <$> + ((++) <$> + (parseString "fn " <|> parseString "export fn") <*> + parseAllCharUntil "\n};\n")) str of + Nothing -> Nothing + Just (Function x, xs) -> case countBracketsForFunction 0 x of + 0 -> Just (Function x, xs) + _ -> Nothing + Just _ -> Nothing parseAlias :: Parser Expression parseAlias = Alias <$> ((++) <$> parseString "alias " <*> parseAllCharUntil ";\n") @@ -41,6 +62,12 @@ parseExpresion :: Parser Expression parseExpresion = parseAlias <|> parseFunction <|> parseComment parseAllExpression :: Parser [Expression] -parseAllExpression = some p +parseAllExpression = Parser f where p = parseExpresion <* many (parseAnyChar "\n") + f [] = Just ([], []) + f str = case runParser p str of + Nothing -> Nothing + Just (x, xs) -> case runParser parseAllExpression xs of + Nothing -> Nothing + Just (y, ys) -> Just (x : y, ys) diff --git a/lvtc/test/Spec.hs b/lvtc/test/Spec.hs index 5782ad9..4b717e0 100644 --- a/lvtc/test/Spec.hs +++ b/lvtc/test/Spec.hs @@ -26,7 +26,7 @@ testParserHelper str restExpected expressionExpected = case runParser parseExpresion str of Just (parsed, rest) -> assertEqual str restExpected rest >> assertEqual str expressionExpected parsed - Nothing -> assertFailure ("Parsing failed: " ++ str) + Nothing -> assertFailure ("Parsing failed for: `" ++ str ++ "`") testParserHelperFail :: String -> IO () testParserHelperFail str = case runParser parseExpresion str of