{- arch-tag: Parsec utilities
Copyright (C) 2004 John Goerzen <jgoerzen@complete.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}{- |
Module : Text.ParserCombinators.Parsec.Utils
Copyright : Copyright (C) 2004 John Goerzen
License : GNU GPL, version 2 or above
Maintainer : John Goerzen <jgoerzen@complete.org>
Stability : provisional
Portability: portable
Written by John Goerzen, jgoerzen\@complete.org
-}moduleText.ParserCombinators.Parsec.Utils(-- * Generalized Utilities-- | These functions are generalized versions of-- ones you might see in the Char parser.GeneralizedToken,GeneralizedTokenParser,togtok,tokeng,satisfyg,oneOfg,noneOfg,specificg,allg,-- * Other UtilitiesnotMatching)whereimportText.ParserCombinators.ParsectypeGeneralizedTokena=(SourcePos,a)typeGeneralizedTokenParserastb=GenParser(GeneralizedTokena)stb{- | Generate (return) a 'GeneralizedToken'. -}togtok::a->GenParserbst(GeneralizedTokena)togtoktok=dox<-getPositionreturn(x,tok){- | Retrieve the next token from a 'GeneralizedToken' stream.
The given function should return the value to use, or Nothing
to cause an error. -}tokeng::(Showa)=>(a->Maybeb)->GeneralizedTokenParserastbtokengtest=token(show.snd)(fst)(test.snd){- | A shortcut to 'tokeng'; the test here is just a function that returns
a Bool. If the result is true; return that value -- otherwise, an error.
-}satisfyg::(Showa)=>(a->Bool)->GeneralizedTokenParserastasatisfygtest=tokeng(\t->iftesttthenJusttelseNothing){- | Matches one item in a list and returns it. -}oneOfg::(Eqa,Showa)=>[a]->GeneralizedTokenParserastaoneOfgi=satisfyg(\x->elemxi){- | Matches all items and returns them -}allg::(Showa)=>GeneralizedTokenParserast[a]allg=many$satisfyg(\_->True){- | Matches one item not in a list and returns it. -}noneOfg::(Eqa,Showa)=>[a]->GeneralizedTokenParserastanoneOfgl=satisfyg(\x->not(elemxl)){- | Matches one specific token and returns it. -}specificg::(Eqa,Showa)=>a->GeneralizedTokenParserastaspecificgi=satisfyg(==i)<?>showi{- Matches a list of tokens and returns it. -}{-
listg :: (Eq a, Show a) => [GeneralizedToken a] -> GeneralizedTokenParser a st [GeneralizedToken a]
listg l = tokens (show . map fst) nextpos l
where
tokpos = fst
nextpos
nextposs _ _ (tok:toks) = tokpos tok
nextposs _ tok [] = tokpos tok
nextpos pos x = nextposs pos [x]
-}{- | Running @notMatching p msg@ will try to apply parser p.
If it fails, returns (). If it succeds, cause a failure and raise
the given error message. It will not consume input in either case. -}notMatching::GenParserabc->String->GenParserab()notMatchingperrormsg=letmaybeRead=try(dox<-preturn(Justx))<|>returnNothingworkerFunc=dox<-maybeReadcasexofNothing->return()Just_->unexpectederrormsgintryworkerFunc