/usr/lib/hugs/packages/HaXml/Text/ParserCombinators/PolyStateLazy.hs is in libhugs-haxml-bundled 98.200609.21-5.3ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | module Text.ParserCombinators.PolyStateLazy
( -- * A Parser datatype parameterised on arbitrary token type and state type.
-- Parsers do not return explicit failure. An exception is raised
-- instead. This allows partial results to be returned before a
-- full parse is complete.
Parser(P) -- datatype, instance of: Functor, Monad
, runParser -- :: Parser s t a -> s -> [t] -> (a, s, [t])
, failBad -- :: String -> Parser s t a
, commit -- :: Parser s t a -> Parser s t a
-- * Combinators
-- ** primitives
, next -- :: Parser s t t
, satisfy -- :: (t->Bool) -> Parser s t t
, apply -- :: Parser t (a->b) -> Parser s t a -> Parser s t b
, discard -- :: Parser s t a -> Parser s t b -> Parser s t a
-- ** error-handling
, adjustErr -- :: Parser s t a -> (String->String) -> Parser s t a
, adjustErrBad-- :: Parser s t a -> (String->String) -> Parser s t a
, indent -- :: Int -> String -> String
-- ** choices
, onFail -- :: Parser s t a -> Parser s t a -> Parser s t a
, oneOf -- :: [Parser s t a] -> Parser s t a
, oneOf' -- :: [(String, Parser s t a)] -> Parser s t a
, optional -- :: Parser s t a -> Parser s t (Maybe a)
-- ** sequences
, many -- :: Parser s t a -> Parser s t [a]
, many1 -- :: Parser s t a -> Parser s t [a]
, sepBy -- :: Parser s t a -> Parser s t sep -> Parser s t [a]
, sepBy1 -- :: Parser s t a -> Parser s t sep -> Parser s t [a]
, bracketSep -- :: Parser s t bra -> Parser s t sep -> Parser s t ket
-- -> Parser s t a -> Parser s t [a]
, bracket -- :: Parser s t bra -> Parser s t ket -> Parser s t a
-- -> Parser s t a
, manyFinally -- :: Parser s t a -> Parser s t z -> Parser s t [a]
-- ** state-handling
, stUpdate -- :: (s->s) -> Parser s t ()
, stQuery -- :: (s->a) -> Parser s t a
, stGet -- :: Parser s t s
-- ** re-parsing
, reparse -- :: [t] -> Parser s t ()
) where
throwE :: String -> a
throwE msg = error msg
-- | The @Parser@ datatype is a fairly generic parsing monad with error
-- reporting and a running state. It can be used for arbitrary token
-- types, not just String input.
newtype Parser s t a = P (s -> [t] -> (Either String a, s, [t]))
-- | A return type like Either, that distinguishes not only between
-- right and wrong answers, but also had gradations of wrongness.
-- Not used in this library. !!!!!!!!!!!!!!!!!!!!!!!!!!
type EitherE a b = Either (Bool,a) b
-- | Apply a parser to an initial state and input token sequence.
-- The parser cannot return an error value explicitly, so errors
-- raise an exception. Thus, results can be partial (lazily constructed,
-- but containing undefined).
runParser :: Parser s t a -> s -> [t] -> (a, s, [t])
runParser (P p) s =
(\ (e,s,ts)-> (case e of {Left m->throwE m; Right x->x}, s, ts))
. p s
instance Functor (Parser s t) where
fmap f (P p) = P (\s ts-> case p s ts of
(Left msg, s', ts') -> (Left msg, s', ts')
(Right x, s', ts') -> (Right (f x), s', ts'))
instance Monad (Parser s t) where
return x = P (\s ts-> (Right x, s, ts))
(P f) >>= g = P (\s ts-> case f s ts of
(Left msg, s', ts') -> (Left msg, s', ts')
(Right x, s', ts') -> let (P g') = g x
in g' s' ts')
fail msg = P (\s ts-> (Left msg, s, ts))
-- | Simple failure can be corrected, but when a simple fail is not strong
-- enough, use failBad for emphasis. It guarantees parsing will terminate
-- with an exception.
failBad :: String -> Parser s t a
failBad msg = P (\s ts-> (throwE msg, s, ts))
-- | Commit is a way of raising the severity of any errors found within
-- its argument. Used in the middle of a parser definition, it means that
-- any operations prior to commitment fail softly, but after commitment,
-- they fail hard.
commit :: Parser s t a -> Parser s t a
commit (P p) = P (\s ts-> case p s ts of
(Left e, s', ts') -> (throwE e, s', ts')
right -> right )
-- Combinators
-- | One token
next :: Parser s t t
next = P (\s ts-> case ts of
[] -> (Left "Ran out of input (EOF)", s, [])
(t:ts') -> (Right t, s, ts') )
-- | One token satifying a predicate
satisfy :: (t->Bool) -> Parser s t t
satisfy p = do{ x <- next
; if p x then return x else fail "Parse.satisfy: failed"
}
infixl 3 `apply`
-- | Apply a parsed function to a parsed value
apply :: Parser s t (a->b) -> Parser s t a -> Parser s t b
--pf `apply` px = do { f <- pf; x <- px; return (f x) }
-- Needs to be lazier! Must not force the argument value too early.
(P pf) `apply` (P px) = P (\s ts->
case pf s ts of
(Left msg, s', ts') -> (Left msg, s', ts')
(Right f, s', ts') -> let (x',s'',ts'') = px s' ts'
x = case x' of
Right x -> x
Left e -> throwE e
in (Right (f x), s'', ts''))
infixl 3 `discard`
-- | @x `discard` y@ parses both x and y, but discards the result of y
discard :: Parser s t a -> Parser s t b -> Parser s t a
px `discard` py = do { x <- px; _ <- py; return x }
-- | @p `adjustErr` f@ applies the transformation @f@ to any error message
-- generated in @p@, having no effect if @p@ succeeds.
adjustErr :: Parser s t a -> (String->String) -> Parser s t a
(P p) `adjustErr` f =
P (\s ts-> case p s ts of
(Left msg, s', ts') -> (Left (f msg), s, ts')
right -> right )
-- | @adjustErrBad@ is just like @adjustErr@ except it also raises the
-- severity of the error.
adjustErrBad :: Parser s t a -> (String->String) -> Parser s t a
-- p `adjustErrBad` f = commit (p `adjustErr` f)
(P p) `adjustErrBad` f =
P (\s ts-> case p s ts of
(Left msg, s', ts') -> (throwE (f msg), s, ts')
right -> right )
infixl 6 `onFail` -- not sure about precedence 6?
-- | @p `onFail` q@ means parse p unless p fails in which case parse q instead.
-- Can be chained together to give multiple attempts to parse something.
-- (Note that q could itself be a failing parser, e.g. to change the error
-- message from that defined in p to something different.)
-- However, a *severe* failure in p cannot be ignored.
onFail :: Parser s t a -> Parser s t a -> Parser s t a
(P p) `onFail` (P q) = P (\s ts-> case p s ts of
(Left _, _, _) -> q s ts
right -> right )
-- | Parse the first alternative in the list that succeeds.
oneOf :: [Parser s t a] -> Parser s t a
oneOf [] = fail ("Failed to parse any of the possible choices")
oneOf (p:ps) = p `onFail` oneOf ps
-- | Parse the first alternative that succeeds, but if none succeed,
-- report only the severe errors, and if none of those, then report
-- all the soft errors.
oneOf' :: [(String, Parser s t a)] -> Parser s t a
oneOf' ps = accum [] ps
where accum errs [] =
case errs of
[] -> failBad ("internal failure in parser (oneOf'):\n"
++indent 2 (show (map fst ps)))
[(_,e)] -> fail e
es -> fail ("one of the following failures occurred:\n"
++indent 2 (concatMap showErr (reverse es)))
accum errs ((e,P p):ps) =
P (\u ts-> case p u ts of
(Left err,_,_) -> let (P p) = accum ((e,err):errs) ps
in p u ts
right -> right )
showErr (name,err) = name++":\n"++indent 2 err
-- | Helper for formatting error messages: indents all lines by a fixed amount.
indent :: Int -> String -> String
indent n = unlines . map (replicate n ' ' ++) . lines
-- | 'optional' indicates whether the parser succeeded through the Maybe type.
optional :: Parser s t a -> Parser s t (Maybe a)
optional p = fmap Just p `onFail` return Nothing
-- | 'many p' parses a list of elements with individual parser p.
-- Cannot fail, since an empty list is a valid return value.
many :: Parser s t a -> Parser s t [a]
many p = many1 p `onFail` return []
-- | Parse a non-empty list of items.
many1 :: Parser s t a -> Parser s t [a]
many1 p = do { x <- p `adjustErr` (("In a sequence:\n"++). indent 2)
; xs <- many p
; return (x:xs)
}
-- `adjustErr` ("When looking for a non-empty sequence:\n"++)
-- | Parse a list of items separated by discarded junk.
sepBy :: Parser s t a -> Parser s t sep -> Parser s t [a]
sepBy p sep = do sepBy1 p sep `onFail` return []
-- | Parse a non-empty list of items separated by discarded junk.
sepBy1 :: Parser s t a -> Parser s t sep -> Parser s t [a]
sepBy1 p sep = do { x <- p
; xs <- many (do {sep; p})
; return (x:xs)
}
`adjustErr` ("When looking for a non-empty sequence with separators:\n"++)
-- | Parse a list of items, discarding the start, end, and separator
-- items.
bracketSep :: Parser s t bra -> Parser s t sep -> Parser s t ket
-> Parser s t a -> Parser s t [a]
bracketSep open sep close p =
do { open; close; return [] }
`onFail`
do { open `adjustErr` ("Missing opening bracket:\n"++)
; x <- p `adjustErr` ("After first bracket in a group:\n"++)
; xs <- many (do {sep; p})
; close `adjustErrBad` ("When looking for closing bracket:\n"++)
; return (x:xs)
}
-- | Parse a bracketed item, discarding the brackets.
bracket :: Parser s t bra -> Parser s t ket -> Parser s t a -> Parser s t a
bracket open close p = do
do { open `adjustErr` ("Missing opening bracket:\n"++)
; x <- p
; close `adjustErrBad` ("Missing closing bracket:\n"++)
; return x
}
-- | 'manyFinally e t' parses a possibly-empty sequence of e's,
-- terminated by a t. Any parse failures could be due either to
-- a badly-formed terminator or a badly-formed element, so raise
-- both possible errors.
manyFinally :: Parser s t a -> Parser s t z -> Parser s t [a]
manyFinally pp@(P p) pt@(P t) = P (\s ts ->
case p s ts of
(Left e, _, _) ->
case t s ts of
(Right _, s', ts') -> (Right [], s', ts')
(Left e, s', ts') -> (Left e, s', ts')
(Right x, s', ts') ->
let (tail,s'',ts'') = runParser (manyFinally pp pt) s' ts'
in (Right (x:tail), s'', ts'') )
------------------------------------------------------------------------
-- State handling
-- | Update the internal state.
stUpdate :: (s->s) -> Parser s t ()
stUpdate f = P (\s ts-> (Right (), f s, ts))
-- | Query the internal state.
stQuery :: (s->a) -> Parser s t a
stQuery f = P (\s ts-> (Right (f s), s, ts))
-- | Deliver the entire internal state.
stGet :: Parser s t s
stGet = P (\s ts-> (Right s, s, ts))
------------------------------------------------------------------------
-- | Push some tokens back onto the front of the input stream and reparse.
-- This is useful e.g. for recursively expanding macros. When the
-- user-parser recognises a macro use, it can lookup the macro
-- expansion from the parse state, lex it, and then stuff the
-- lexed expansion back down into the parser.
reparse :: [t] -> Parser s t ()
reparse ts = P (\s inp-> (Right (), s, ts++inp))
------------------------------------------------------------------------
|