diff --git a/src/Parser.hs b/src/Parser.hs index d792985..611d35c 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -20,6 +20,7 @@ data SaplingAST deriving (Show, Eq, Ord) parseSapling :: String -> SaplingAST +parseSapling "" = error "Empty input provided to parseSapling" parseSapling input = case runParser parseExpression "" (lexSapling input) of Left err -> error "Failed to parse input" Right ast -> ast @@ -122,8 +123,14 @@ parseListLiteral = do satisfy (== LCloseBracket) return (SList elements) +--parseListItem :: Parser SaplingAST +--parseListItem = parseGroupedItem <|> parseSingleItem parseListItem :: Parser SaplingAST -parseListItem = parseGroupedItem <|> parseSingleItem +parseListItem = choice + [ parseGroupedItem -- Handle expressions inside parentheses + , parseListLiteral -- Allow nested lists + , parseSingleItem -- Handle single tokens like `t` or identifiers + ] parseGroupedItem :: Parser SaplingAST parseGroupedItem = do diff --git a/test/Spec.hs b/test/Spec.hs index b9e43f5..c6b568c 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -5,6 +5,7 @@ import Lexer import Parser import Research +import Control.Exception (evaluate, try, SomeException) import qualified Data.Map as Map import Test.Tasty import Test.Tasty.HUnit @@ -58,6 +59,27 @@ parserTests = testGroup "Parser Tests" let input = "t t t" let expected = TFork TLeaf TLeaf parseSapling input @?= expected + + , testCase "Parse mixed list literals" $ do + -- You must put non-listliterals in parentheses + let input = "[t (\"hello\") t]" + let expected = SList [TLeaf, SStr "hello", TLeaf] + parseSapling input @?= expected + + , testCase "Parse function with applications" $ do + let input = "f x = t x" + let expected = SFunc "f" ["x"] (SApp TLeaf [SVar "x"]) + parseSapling input @?= expected + + , testCase "Parse nested lists" $ do + let input = "[t [(t t)]]" + let expected = SList [TLeaf, SList [TStem TLeaf]] + parseSapling input @?= expected + + , testCase "Parse complex parentheses" $ do + let input = "t (t t (t t))" + let expected = TStem (TFork TLeaf (TStem TLeaf)) + parseSapling input @?= expected ] integrationTests :: TestTree @@ -138,4 +160,3 @@ propertyTests = testGroup "Property Tests" Left _ -> property True Right ast -> parseSapling input === ast ] -