module BNFC.Backend.Common.Utils where

import BNFC.Prelude

import Prettyprinter
import Prettyprinter.Render.String

-- | The name of a module, e.g. "Foo.Abs", "Foo.Print" etc.
type ModuleName = String

-- | Generalization of 'Control.Monad.unless'.

unless :: Monoid m => Bool -> m -> m
unless :: Bool -> m -> m
unless Bool
False m
m = m
m
unless Bool
True  m
_ = m
forall a. Monoid a => a
mempty

-- | Generalization of 'Control.Monad.when'.

when :: Monoid m => Bool -> m -> m
when :: Bool -> m -> m
when Bool
True  m
m = m
m
when Bool
False m
_ = m
forall a. Monoid a => a
mempty

prPrec :: Int -> Int -> Doc () -> Doc ()
prPrec :: Int -> Int -> Doc () -> Doc ()
prPrec Int
i Int
j Doc ()
d = if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
j then Doc () -> Doc ()
forall ann. Doc ann -> Doc ann
parens Doc ()
d else Doc ()
d

docToString :: LayoutOptions -> Doc () -> String
docToString :: LayoutOptions -> Doc () -> String
docToString LayoutOptions
layoutOpts = SimpleDocStream () -> String
forall ann. SimpleDocStream ann -> String
renderString (SimpleDocStream () -> String)
-> (Doc () -> SimpleDocStream ()) -> Doc () -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LayoutOptions -> Doc () -> SimpleDocStream ()
forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutSmart LayoutOptions
layoutOpts

-- | Replace all occurences of a value by another value
replace :: (Eq a, Functor f) =>
           a    -- ^ Value to replace
        -> a    -- ^ Value to replace it with
        -> f a
        -> f a
replace :: a -> a -> f a -> f a
replace a
x a
y f a
xs = (\a
z ->  if a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x then a
y else a
z) (a -> a) -> f a -> f a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
xs