@@ -8,8 +8,9 @@ module Ormolu.Parser.LanguagePragma
11import Data.Char (toLower)
12import Data.Char (toLower)
12−import Data.List (isPrefixOf)
13+import Data.List (stripPrefix, isSuffixOf)
13import Data.Set (Set)
14import Data.Set (Set)
14import FastString (mkFastString, unpackFS)
15import FastString (mkFastString, unpackFS)
15import Module (newSimpleUnitId, ComponentId (..))
16import Module (newSimpleUnitId, ComponentId (..))
@@ -25,20 +26,20 @@ import qualified Lexer as L
25parseLanguagePragma
26parseLanguagePragma
26 :: String -- ^ Comment to try to parse
27 :: String -- ^ Comment to try to parse
27 -> Maybe (Set String)
28 -> Maybe (Set String)
28−parseLanguagePragma input =
29+parseLanguagePragma input = do
29− if "{-#" `isPrefixOf` input
30+ inputNoPrefix <- stripPrefix "{-#" input
30− then do
31+ guard ("#-}" `isSuffixOf` input)
31− toks0 <- tokenize (drop 3 input)
32+ let contents = take (length inputNoPrefix - 3) inputNoPrefix
33+ toks0 <- tokenize contents
32− toks1 <- dropLanguage toks0
34+ toks1 <- dropLanguage toks0
33− S.fromList <$> parseExtensions toks1
35+ S.fromList <$> parseExtensions toks1
36-- | Assuming the input consists of a series of tokens from a language
37-- | Assuming the input consists of a series of tokens from a language
37-- pragma, return the set of enabled extensions.
38-- pragma, return the set of enabled extensions.
39parseExtensions :: [L.Token] -> Maybe [String]
40parseExtensions :: [L.Token] -> Maybe [String]
40parseExtensions = \case
41parseExtensions = \case
41− (L.ITconid ext : L.ITclose_prag : []) -> return [unpackFS ext]
42+ (L.ITconid ext : []) -> return [unpackFS ext]
42 (L.ITconid ext : L.ITcomma : xs) -> (unpackFS ext :) <$> parseExtensions xs
43 (L.ITconid ext : L.ITcomma : xs) -> (unpackFS ext :) <$> parseExtensions xs
43 _ -> Nothing
44 _ -> Nothing