From fa32199f5ffc6405bd405e055051e11e85c80668 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Thu, 3 Oct 2024 01:58:23 -0600 Subject: Another monolithic change. Not good git ettiquite. Import statements are fully implemented including compiling to an interface file for faster compilations. --- src/Language/Fiddle/Parser.hs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/Language/Fiddle/Parser.hs') diff --git a/src/Language/Fiddle/Parser.hs b/src/Language/Fiddle/Parser.hs index d41cc64..8dfaaae 100644 --- a/src/Language/Fiddle/Parser.hs +++ b/src/Language/Fiddle/Parser.hs @@ -52,9 +52,14 @@ isComment (Token t _) = stripTrailingComments :: [Token s] -> [Token s] stripTrailingComments = reverse . dropWhile isComment . reverse -directedP :: Pa t -> PaS (Directed t 'Parsed) -directedP subparser = withMeta $ do - Directed <$> many directiveP <*> subparser +directedP :: (Annotated (t Parsed)) => Pa t -> PaS (Directed t 'Parsed) +directedP subparser = withMetaLeaveComments $ do + comments <- many commentP + Directed <$> many directiveP <*> pushComments comments subparser + +pushComments :: (Annotated t) => [Comment] -> PaS t -> PaS t +pushComments comments subparse = do + setAnnot (\(Commented coms a) -> Commented (comments ++ coms) a) <$> subparse directiveP :: PaS Directive directiveP = @@ -103,7 +108,7 @@ directiveExpressionP = withMeta $ do fiddleUnit :: Pa FiddleUnit fiddleUnit = do withMeta - ( FiddleUnit <$> many1 (directedP fiddleDeclP <* tok TokSemi) + ( FiddleUnit () <$> many1 (directedP fiddleDeclP <* tok TokSemi) ) <* many commentP @@ -359,7 +364,7 @@ defer p0 pb = do packageBodyP :: Pa PackageBody packageBodyP = - withMeta $ + withMetaLeaveComments $ PackageBody <$> many ( directedP $ @@ -399,6 +404,15 @@ withMeta p = do end <- getPosition return $ fn (Commented comments (SourceSpan start end)) +-- Takes a some parsable thing p and automatically parses the comments before +-- and after and sets the positions and adds it to the annotation. +withMetaLeaveComments :: P (Commented SourceSpan -> b) -> P b +withMetaLeaveComments p = do + start <- getPosition + fn <- p + end <- getPosition + return $ fn (Commented [] (SourceSpan start end)) + token :: (T -> Maybe a) -> ParsecT S u Identity a token fn = Text.Parsec.token -- cgit