From 953b23229f38f1b76d130086a213d565f13cdc06 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Tue, 25 May 2021 21:45:22 +0200 Subject: Make it work --- Parser.hs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'Parser.hs') diff --git a/Parser.hs b/Parser.hs index afc5b15..017544a 100644 --- a/Parser.hs +++ b/Parser.hs @@ -25,18 +25,23 @@ parseSourceFile fname source pat = runReader (runParserT pSourceFile () fname so pSourceFile :: Parser SourceFile pSourceFile = do - SourceFile . concat <$> many (pDataDef <|> pInstanceDef <|> pFunDef <|> skipLine) - where - skipLine :: Parser [Chunk] - skipLine = - fmap (pure . CSkip . build) $ - (\s -> B.fromString (s ++ "\n")) <$> manyTill anyChar newline + result <- concat <$> many (pDataDef <|> pInstanceDef <|> pFunDef <|> skipLine) + after <- skipLineEOF + return (SourceFile (result ++ after)) + +skipLine :: Parser [Chunk] +skipLine = try $ + fmap (pure . CSkip . build) $ + (\s -> B.fromString (s ++ "\n")) <$> manyTill anyChar newline + +skipLineEOF :: Parser [Chunk] +skipLineEOF = pure . CSkip . T.pack <$> manyTill (notFollowedBy newline >> anyChar) eof -- Parses a function definition on the current line. -- Assumes the cursor is at the start of a line, and ends at the start of a line again. -- Fails if no function definition can be found on this line. pFunDef :: Parser [Chunk] -pFunDef = try $ do +pFunDef = flip label "function definition" $ try $ do prefix <- build <$> combine [horSpaces ,option mempty (text "=" +++ horSpaces) @@ -58,8 +63,8 @@ pFunDef = try $ do return (CSkip (prefix `T.append` build s1) : parseTypeText pat typeText ++ [CSkip (build s2)]) pDataDef :: Parser [Chunk] -pDataDef = try $ do - s1 <- text "data" +pDataDef = flip label "data/type definition" $ try $ do + s1 <- text "data" <|> text "type" <|> text "newtype" let limiter = lookAhead $ try $ choice [eof ,do _ <- newline @@ -71,7 +76,7 @@ pDataDef = try $ do return (CSkip (build s1) : parseTypeText pat typeText ++ [CSkip (build s2)]) pInstanceDef :: Parser [Chunk] -pInstanceDef = try $ do +pInstanceDef = flip label "instance definition" $ try $ do s1 <- text "instance" let limiter = lookAhead $ try $ choice [eof @@ -80,8 +85,9 @@ pInstanceDef = try $ do _ <- string "where" eof <|> (satisfy prewordc >> return ())] typeText <- manyTill anyChar limiter + rest <- skipLine pat <- ask - return (CSkip (build s1) : parseTypeText pat typeText) + return (CSkip (build s1) : parseTypeText pat typeText ++ rest) parseTypeText :: Pattern -> String -> [Chunk] parseTypeText (Pattern patname _) inputText = -- cgit v1.2.3-70-g09d2