diff options
author | Tom Smeding <tom@tomsmeding.com> | 2021-05-25 21:45:22 +0200 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2021-05-25 21:45:22 +0200 |
commit | 953b23229f38f1b76d130086a213d565f13cdc06 (patch) | |
tree | d59282f7a65fd2dc9ae2f40886930c632595232f /Parser.hs | |
parent | b03f51f3a363f861f9d5de30ec6a337fec316383 (diff) |
Diffstat (limited to 'Parser.hs')
-rw-r--r-- | Parser.hs | 28 |
1 files changed, 17 insertions, 11 deletions
@@ -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 = |