1 -----------------------------------------------------------------------------
3 -- Module : Text.Parsec.Perm
4 -- Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
5 -- License : BSD-style (see the file libraries/parsec/LICENSE)
7 -- Maintainer : derek.a.elkins@gmail.com
8 -- Stability : provisional
9 -- Portability : non-portable (uses existentially quantified data constructors)
11 -- This module implements permutation parsers. The algorithm used
12 -- is fairly complex since we push the type system to its limits :-)
13 -- The algorithm is described in:
15 -- /Parsing Permutation Phrases,/
16 -- by Arthur Baars, Andres Loh and Doaitse Swierstra.
17 -- Published as a functional pearl at the Haskell Workshop 2001.
19 -----------------------------------------------------------------------------
21 {-# LANGUAGE ExistentialQuantification #-}
23 module Text.Parsec.Perm
25 , StreamPermParser -- abstract
34 import Control.Monad.Identity
40 {---------------------------------------------------------------
41 test -- parse a permutation of
42 * an optional string of 'a's
45 ---------------------------------------------------------------}
48 = parse (do{ x <- ptest; eof; return x }) "" input
50 ptest :: Parser (String,Char,Char)
53 (,,) <$?> ("",many1 (char 'a'))
58 {---------------------------------------------------------------
59 Building a permutation parser
60 ---------------------------------------------------------------}
62 -- | The expression @perm \<||> p@ adds parser @p@ to the permutation
63 -- parser @perm@. The parser @p@ is not allowed to accept empty input -
64 -- use the optional combinator ('<|?>') instead. Returns a
65 -- new permutation parser that includes @p@.
67 (<||>) :: (Stream s Identity tok) => StreamPermParser s st (a -> b) -> Parsec s st a -> StreamPermParser s st b
68 (<||>) perm p = add perm p
70 -- | The expression @f \<$$> p@ creates a fresh permutation parser
71 -- consisting of parser @p@. The the final result of the permutation
72 -- parser is the function @f@ applied to the return value of @p@. The
73 -- parser @p@ is not allowed to accept empty input - use the optional
74 -- combinator ('<$?>') instead.
76 -- If the function @f@ takes more than one parameter, the type variable
77 -- @b@ is instantiated to a functional type which combines nicely with
78 -- the adds parser @p@ to the ('<||>') combinator. This
79 -- results in stylized code where a permutation parser starts with a
80 -- combining function @f@ followed by the parsers. The function @f@
81 -- gets its parameters in the order in which the parsers are specified,
82 -- but actual input can be in any order.
84 (<$$>) :: (Stream s Identity tok) => (a -> b) -> Parsec s st a -> StreamPermParser s st b
85 (<$$>) f p = newperm f <||> p
87 -- | The expression @perm \<||> (x,p)@ adds parser @p@ to the
88 -- permutation parser @perm@. The parser @p@ is optional - if it can
89 -- not be applied, the default value @x@ will be used instead. Returns
90 -- a new permutation parser that includes the optional parser @p@.
92 (<|?>) :: (Stream s Identity tok) => StreamPermParser s st (a -> b) -> (a, Parsec s st a) -> StreamPermParser s st b
93 (<|?>) perm (x,p) = addopt perm x p
95 -- | The expression @f \<$?> (x,p)@ creates a fresh permutation parser
96 -- consisting of parser @p@. The the final result of the permutation
97 -- parser is the function @f@ applied to the return value of @p@. The
98 -- parser @p@ is optional - if it can not be applied, the default value
99 -- @x@ will be used instead.
101 (<$?>) :: (Stream s Identity tok) => (a -> b) -> (a, Parsec s st a) -> StreamPermParser s st b
102 (<$?>) f (x,p) = newperm f <|?> (x,p)
104 {---------------------------------------------------------------
106 ---------------------------------------------------------------}
108 -- | Provided for backwards compatibility. The tok type is ignored.
110 type PermParser tok st a = StreamPermParser String st a
112 -- | The type @StreamPermParser s st a@ denotes a permutation parser that,
113 -- when converted by the 'permute' function, parses
114 -- @s@ streams with user state @st@ and returns a value of
115 -- type @a@ on success.
117 -- Normally, a permutation parser is first build with special operators
118 -- like ('<||>') and than transformed into a normal parser
121 data StreamPermParser s st a = Perm (Maybe a) [StreamBranch s st a]
123 -- type Branch st a = StreamBranch String st a
125 data StreamBranch s st a = forall b. Branch (StreamPermParser s st (b -> a)) (Parsec s st b)
127 -- | The parser @permute perm@ parses a permutation of parser described
128 -- by @perm@. For example, suppose we want to parse a permutation of:
129 -- an optional string of @a@'s, the character @b@ and an optional @c@.
130 -- This can be described by:
132 -- > test = permute (tuple <$?> ("",many1 (char 'a'))
134 -- > <|?> ('_',char 'c'))
136 -- > tuple a b c = (a,b,c)
138 -- transform a permutation tree into a normal parser
139 permute :: (Stream s Identity tok) => StreamPermParser s st a -> Parsec s st a
140 permute (Perm def xs)
141 = choice (map branch xs ++ empty)
148 branch (Branch perm p)
154 -- build permutation trees
155 newperm :: (Stream s Identity tok) => (a -> b) -> StreamPermParser s st (a -> b)
159 add :: (Stream s Identity tok) => StreamPermParser s st (a -> b) -> Parsec s st a -> StreamPermParser s st b
160 add perm@(Perm _mf fs) p
161 = Perm Nothing (first:map insert fs)
163 first = Branch perm p
164 insert (Branch perm' p')
165 = Branch (add (mapPerms flip perm') p) p'
167 addopt :: (Stream s Identity tok) => StreamPermParser s st (a -> b) -> a -> Parsec s st a -> StreamPermParser s st b
168 addopt perm@(Perm mf fs) x p
169 = Perm (fmap ($ x) mf) (first:map insert fs)
171 first = Branch perm p
172 insert (Branch perm' p')
173 = Branch (addopt (mapPerms flip perm') x p) p'
176 mapPerms :: (Stream s Identity tok) => (a -> b) -> StreamPermParser s st a -> StreamPermParser s st b
177 mapPerms f (Perm x xs)
178 = Perm (fmap f x) (map mapBranch xs)
180 mapBranch (Branch perm p)
181 = Branch (mapPerms (f.) perm) p