diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2024-10-03 01:58:23 -0600 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2024-10-03 01:58:23 -0600 |
commit | fa32199f5ffc6405bd405e055051e11e85c80668 (patch) | |
tree | 87effa6909f7cc6f05782f818c01d0a983a620fb /src/Language/Fiddle/Parser.hs | |
parent | 719c8f8ed3d1e6337f27d3b9d5a033a4b63726b8 (diff) | |
download | fiddle-fa32199f5ffc6405bd405e055051e11e85c80668.tar.gz fiddle-fa32199f5ffc6405bd405e055051e11e85c80668.tar.bz2 fiddle-fa32199f5ffc6405bd405e055051e11e85c80668.zip |
Another monolithic change. Not good git ettiquite.
Import statements are fully implemented including compiling to an
interface file for faster compilations.
Diffstat (limited to 'src/Language/Fiddle/Parser.hs')
-rw-r--r-- | src/Language/Fiddle/Parser.hs | 24 |
1 files changed, 19 insertions, 5 deletions
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 |