-- |
-- Module      : Data.Binding.Hobbits.PatternParser
-- Copyright   : (c) 2011 Edwin Westbrook, Nicolas Frisby, and Paul Brauner
--
-- License     : BSD3
--
-- Maintainer  : emw4@rice.edu
-- Stability   : experimental
-- Portability : GHC
--
-- Using the haskell-src-meta package to parse Haskell patterns.

module Data.Binding.Hobbits.PatternParser (parsePattern) where

import Language.Haskell.TH

import qualified Language.Haskell.Exts.Parser as Meta

import qualified Language.Haskell.Meta.Parse as Meta

import qualified Language.Haskell.Meta.Parse as Sloppy
import qualified Language.Haskell.Meta.Syntax.Translate as Translate

import qualified Language.Haskell.Exts.Extension as Exts

parsePatternExtensions :: [Extension]
parsePatternExtensions =
  (KnownExtension -> Extension) -> [KnownExtension] -> [Extension]
forall a b. (a -> b) -> [a] -> [b]
map KnownExtension -> Extension
Exts.EnableExtension ([KnownExtension] -> [Extension])
-> [KnownExtension] -> [Extension]
forall a b. (a -> b) -> a -> b
$ KnownExtension
Exts.ViewPatterns KnownExtension -> [KnownExtension] -> [KnownExtension]
forall a. a -> [a] -> [a]
: [KnownExtension]
Sloppy.myDefaultExtensions

parsePattern :: String -> String -> Either String Pat
parsePattern :: String -> String -> Either String Pat
parsePattern String
fn =
  (Pat SrcSpanInfo -> Pat)
-> Either String (Pat SrcSpanInfo) -> Either String Pat
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pat SrcSpanInfo -> Pat
forall a. ToPat a => a -> Pat
Translate.toPat (Either String (Pat SrcSpanInfo) -> Either String Pat)
-> (String -> Either String (Pat SrcSpanInfo))
-> String
-> Either String Pat
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseResult (Pat SrcSpanInfo) -> Either String (Pat SrcSpanInfo)
forall a. ParseResult a -> Either String a
Meta.parseResultToEither (ParseResult (Pat SrcSpanInfo) -> Either String (Pat SrcSpanInfo))
-> (String -> ParseResult (Pat SrcSpanInfo))
-> String
-> Either String (Pat SrcSpanInfo)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  ParseMode -> String -> ParseResult (Pat SrcSpanInfo)
Meta.parsePatWithMode (ParseMode
Sloppy.myDefaultParseMode
                    {parseFilename :: String
Meta.parseFilename = String
fn,
                     extensions :: [Extension]
Meta.extensions = [Extension]
parsePatternExtensions })