{- The library for decorated parsers. It provides the same interface as ParseBase (plus some extra functions) and transforms all ParseBase functions in such a way that they not only construct the (polymorphic) result, but also the parse tree (a.k.a. concrete syntax tree). As in ParseBase, run extracts the polymorphic result, while ptree extracts the parse tree in raw format. -} module ParseDecorate ( Parser , run , P(..), ptree, PAlgebra, foldP , B.failp, succeed -- elementary parsers that don't consume input , symbol, token -- elementary parsers , (B.<|>), (<*>), (<$>) -- elementary parser combinators , label, unit ) where import qualified ParseBase as B import PTree import Data.List (inits, tails) -- The type of parsers type Parser s a = B.Parser s (P s, a) run :: Parser s a -> [s] -> a run p = snd . B.run p ptree :: Parser s a -> [s] -> P s ptree p = fst . B.run p -- Elementary parsers succeed :: a -> Parser s a succeed r = PSucceed `annot` B.succeed r symbol :: Eq s => s -> Parser s s symbol s = PToken [s] `annot` B.symbol s token :: Eq s => [s] -> Parser s [s] token k = PToken k `annot` B.token k annot :: P s -> B.Parser s a -> Parser s a annot p bp = (,) p B.<$> bp unit :: Parser s a -> Parser s a unit p = (\(t, r) -> (PUnit t, r)) B.<$> p chain :: Parser s a -> Parser s a chain p = (\(t, r) -> (PChain t, r)) B.<$> p -- Elementary parser combinators infixl 7 <$> infixl 6 <*> (<$>) :: (a -> b) -> Parser s a -> Parser s b f <$> p = (\(t, r) -> (PApply t, f r)) B.<$> p (<*>) :: Parser s (b -> a) -> Parser s b -> Parser s a p <*> q = (\(pp, f) (pq, x) -> (PSeq pp pq, f x)) B.<$> p B.<*> q label :: String -> Parser s a -> Parser s a label name p = (\(t, r) -> (PLabel name t, r)) B.<$> p