@@ -1,4 +1,4 @@
1−-- | A module for parsing of language pragmas from comments.
1+-- | A module for parsing of pragmas from comments.
3{-# LANGUAGE LambdaCase #-}
3{-# LANGUAGE LambdaCase #-}
4{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE OverloadedStrings #-}
@@ -9,8 +9,9 @@ module Ormolu.Parser.Pragma
12import Data.Char (toLower, isSpace)
13import Data.Char (toLower, isSpace)
13−import Data.List (stripPrefix, dropWhileEnd)
14+import Data.List
14import FastString (mkFastString, unpackFS)
15import FastString (mkFastString, unpackFS)
15import Module (newSimpleUnitId, ComponentId (..))
16import Module (newSimpleUnitId, ComponentId (..))
16import SrcLoc
17import SrcLoc
@@ -18,30 +19,31 @@ import StringBuffer
18import qualified EnumSet as ES
19import qualified EnumSet as ES
19import qualified Lexer as L
20import qualified Lexer as L
22+-- | Ormolu's representation of pragmas.
21data Pragma
24data Pragma
22− = PragmaLanguage [String]
25+ = PragmaLanguage [String] -- ^ Language pragma
23− | PragmaOptionsGHC String
26+ | PragmaOptionsGHC String -- ^ GHC options pragma
24− | PragmaOptionsHaddock String
27+ | PragmaOptionsHaddock String -- ^ Haddock options pragma
25 deriving (Show, Eq)
28 deriving (Show, Eq)
27−-- | Extract a Pragma from a comment if possible, or return 'Nothing'
30+-- | Extract a pragma from a comment if possible, or return 'Nothing'
28-- otherwise.
31-- otherwise.
30−parsePragma :: String -> Maybe Pragma
33+parsePragma
34+ :: String -- ^ Comment to try to parse
31parsePragma input = do
36parsePragma input = do
32− contents <- trimSpaces <$> stripEnclosing "{-#" "#-}" input
37+ inputNoPrefix <- stripPrefix "{-#" input
33− let (name, cs) = separateName contents
38+ guard ("#-}" `isSuffixOf` input)
39+ let contents = take (length inputNoPrefix - 3) inputNoPrefix
40+ (pragmaName, cs) = (break isSpace . dropWhile isSpace) contents
34− case map toLower name of
41+ case toLower <$> pragmaName of
35 "language" -> PragmaLanguage <$> parseExtensions cs
42 "language" -> PragmaLanguage <$> parseExtensions cs
36 "options_ghc" -> Just $ PragmaOptionsGHC (trimSpaces cs)
43 "options_ghc" -> Just $ PragmaOptionsGHC (trimSpaces cs)
37 "options_haddock" -> Just $ PragmaOptionsHaddock (trimSpaces cs)
44 "options_haddock" -> Just $ PragmaOptionsHaddock (trimSpaces cs)
38 _ -> Nothing
45 _ -> Nothing
40− -- stripEnclosing pre suf (pre ++ str ++ suf) == Just str
41− stripEnclosing :: String -> String -> String -> Maybe String
42− stripEnclosing pre suf str =
43− fmap reverse $ stripPrefix pre str >>= stripPrefix (reverse suf) . reverse
45 trimSpaces :: String -> String
47 trimSpaces :: String -> String
46 trimSpaces = dropWhileEnd isSpace . dropWhile isSpace
48 trimSpaces = dropWhileEnd isSpace . dropWhile isSpace
@@ -56,11 +58,6 @@ parseExtensions str = tokenize str >>= go
56 (L.ITconid ext : L.ITcomma : xs) -> (unpackFS ext :) <$> go xs
58 (L.ITconid ext : L.ITcomma : xs) -> (unpackFS ext :) <$> go xs
57 _ -> Nothing
59 _ -> Nothing
59−-- | Split a pragma to name and contents
61−separateName :: String -> (String, String)
62−separateName = span (not . isSpace) . dropWhile isSpace
64-- | Tokenize a given input using GHC's lexer.
61-- | Tokenize a given input using GHC's lexer.
66tokenize :: String -> Maybe [L.Token]
63tokenize :: String -> Maybe [L.Token]