on master help sign in
Workaround GHC tokenizer bug while extracting LANGUAGE pragmas
commitbe6d09d14871181bd9cb51c28183cbea53c2d7c7 authorUtku Demir <me@utdemir.com> dateFri 9 Aug 2019, 09:49 (7 years ago) parentc2b32da
4 files changed+20−10
data/examples/other/language-pragma-out.hs+3 data/examples/other/language-pragma.hs+4−1 src/Ormolu/Parser/LanguagePragma.hs+10−9 tests/Ormolu/Parser/LanguagePragmaSpec.hs+3
data/examples/other/language-pragma-out.hs+3
@@ -1,5 +1,8 @@
1+{-# LANGUAGE AllowAmbiguousTypes #-}
12{-# LANGUAGE DataKinds #-}
3+{-# LANGUAGE IncoherentInstances #-}
24{-# LANGUAGE LambdaCase #-}
5+{-# LANGUAGE OverloadedStrings #-}
36{-# LANGUAGE ViewPatterns #-}
47
58-- | Header comment.
data/examples/other/language-pragma.hs+4−1
@@ -1,7 +1,10 @@
11-- | Header comment.
22
33{-# LANGUAGE LambdaCase #-}
4−{-# LANGuagE ViewPatterns #-}
4+{-#LANGuagE ViewPatterns #-}
5+{-# LANGUAGE OverloadedStrings#-}
56{-# language DataKinds, LambdaCase #-}
7+{-# language IncoherentInstances
8+ , AllowAmbiguousTypes #-}
69
710module Foo () where
src/Ormolu/Parser/LanguagePragma.hs+10−9
@@ -8,8 +8,9 @@ module Ormolu.Parser.LanguagePragma
88 )
99where
1010
11+import Control.Monad
1112import Data.Char (toLower)
12−import Data.List (isPrefixOf)
13+import Data.List (stripPrefix, isSuffixOf)
1314import Data.Set (Set)
1415import FastString (mkFastString, unpackFS)
1516import Module (newSimpleUnitId, ComponentId (..))
@@ -25,20 +26,20 @@ import qualified Lexer as L
2526parseLanguagePragma
2627 :: String -- ^ Comment to try to parse
2728 -> Maybe (Set String)
28−parseLanguagePragma input =
29− if "{-#" `isPrefixOf` input
30− then do
31− toks0 <- tokenize (drop 3 input)
32− toks1 <- dropLanguage toks0
33− S.fromList <$> parseExtensions toks1
34− else Nothing
29+parseLanguagePragma input = do
30+ inputNoPrefix <- stripPrefix "{-#" input
31+ guard ("#-}" `isSuffixOf` input)
32+ let contents = take (length inputNoPrefix - 3) inputNoPrefix
33+ toks0 <- tokenize contents
34+ toks1 <- dropLanguage toks0
35+ S.fromList <$> parseExtensions toks1
3536
3637-- | Assuming the input consists of a series of tokens from a language
3738-- pragma, return the set of enabled extensions.
3839
3940parseExtensions :: [L.Token] -> Maybe [String]
4041parseExtensions = \case
41− (L.ITconid ext : L.ITclose_prag : []) -> return [unpackFS ext]
42+ (L.ITconid ext : []) -> return [unpackFS ext]
4243 (L.ITconid ext : L.ITcomma : xs) -> (unpackFS ext :) <$> parseExtensions xs
4344 _ -> Nothing
4445
tests/Ormolu/Parser/LanguagePragmaSpec.hs+3
@@ -9,6 +9,9 @@ spec = do
99 describe "parseLanguagePragma" $ do
1010 stdTest "{-# LANGUAGE Foo #-}" (Just ["Foo"])
1111 stdTest "{-# language Foo #-}" (Just ["Foo"])
12+ stdTest "{-#LANGUAGE Foo#-}" (Just ["Foo"])
13+ stdTest "{-# LANGUAGE Foo#-}" (Just ["Foo"])
14+ stdTest "{-#language Foo#-}" (Just ["Foo"])
1215 stdTest "{-# lAngUAGe Foo #-}" (Just ["Foo"])
1316 stdTest "{-# LANGUAGE Foo, Bar #-}" (Just ["Bar", "Foo"])
1417 stdTest "{-# LANGUAGE Foo Bar #-}" Nothing