Skip to content

Commit

Permalink
Add pattern SString
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Nov 3, 2021
1 parent d155e79 commit 85aa1c9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@
name: Avoid return
lhs: return
rhs: pure

# hlint fails to account for ViewPatterns usage inside PatternSynonyms
- ignore: {name: Unused LANGUAGE pragma, within: Prettyprinter.Internal}
4 changes: 4 additions & 0 deletions aux/version-compatibility-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@

#define FAIL_IN_MONAD !(MIN_VERSION_base(4,13,0))

#define BIDI_PATTERN_SYNONYMS MIN_VERSION_base(4,8,0)
#define PATTERN_SYNONYM_SIGNATURES MIN_VERSION_base(4,9,0)
#define COMPLETE_PRAGMA MIN_VERSION_base(4,10,0)

#endif
28 changes: 28 additions & 0 deletions prettyprinter/src/Prettyprinter/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

#include "version-compatibility-macros.h"

#if BIDI_PATTERN_SYNONYMS
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
#if PATTERN_SYNONYM_SIGNATURES
{-# OPTIONS_GHC -fno-warn-missing-pattern-synonym-signatures #-}
#endif
#endif

-- | __Warning: internal module!__ This means that the API may change
-- arbitrarily between versions without notice. Depending on this module may
-- lead to unexpected breakages, so proceed with caution!
Expand Down Expand Up @@ -68,6 +76,9 @@ module Prettyprinter.Internal (

-- * Layout
SimpleDocStream(..),
#if BIDI_PATTERN_SYNONYMS
pattern SString,
#endif
PageWidth(..), defaultPageWidth,
LayoutOptions(..), defaultLayoutOptions,
layoutPretty, layoutCompact, layoutSmart,
Expand Down Expand Up @@ -1596,6 +1607,23 @@ data SimpleDocStream ann =
| SAnnPop (SimpleDocStream ann)
deriving (Eq, Ord, Show, Generic, Typeable)

#if BIDI_PATTERN_SYNONYMS

#ifdef MIN_VERSION_text
-- | 'length' is /O(n)/, so we cache it in the 'Int' field.
pattern SString a b c <- SText a (T.unpack -> b) c where
SString a b c = SText a (T.pack b) c
#else
-- | 'length' is /O(n)/, so we cache it in the 'Int' field.
pattern SString a b c = SText a b c
#endif

#if COMPLETE_PRAGMA
{-# COMPLETE SFail, SEmpty, SChar, SString, SLine, SAnnPush, SAnnPop #-}
#endif

#endif

-- | Remove all trailing space characters.
--
-- This has some performance impact, because it does an entire additional pass
Expand Down

0 comments on commit 85aa1c9

Please sign in to comment.