!r<O      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~                        !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNMGraham Hutton (University of Nottingham), Erik Meijer (University of Utrecht)BSD/Malcolm Wallace <Malcolm.Wallace@cs.york.ac.uk>StableAll A LIBRARY OF MONADIC PARSER COMBINATORS 29th July 1996 Graham Hutton Erik Meijer University of Nottingham University of UtrechtSafe| polyparseThe parser monad$  !"#$  !"#5Safe6+ polyparse:The parser type is parametrised on the types of the state s, the input tokens t , error-type e, and the result value aB. The state and remaining input are threaded through the monad., polyparse"Deliver the first remaining token.- polyparse#Fail if end of input is not reachedO polyparseBDeliver the first parse result only, eliminating any backtracking.. polyparseaApply the parser to some real input, given an initial state value. If the parser fails, raise P to halt the program. (This is the original exported behaviour - to allow the caller to deal with the error differently, see papply'.)/ polyparseApply the parser to some real input, given an initial state value. If the parser fails, return a diagnostic message to the caller.0 polyparse7A choice between parsers. Keep only the first success.Q polyparse4Deliver the first token if it satisfies a predicate.1 polyparse2Deliver the first token if it equals the argument.2 polyparse:Deliver the first token if it does not equal the argument.3 polyparseDeliver zero or more values of a.4 polyparseDeliver one or more values of a.5 polyparseDeliver zero or more values of a separated by b's.6 polyparseDeliver one or more values of a separated by b's.= polyparse=Accept a complete parse of the input only, no partial parses.R polyparsenReturn an error using the supplied diagnostic string, and a token type which includes position information.> polyparse/If the parser fails, generate an error message.? polyparseUpdate the internal state.@ polyparseQuery the internal state.A polyparse"Deliver the entire internal state.B polyparseThis is useful 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.*+,-./0123456789:;<=>?@AB*+,-./0123456789:;<=>?@AB05SafeoSI polyparseThe  PolyParse class is an abstraction gathering all of the common features that a two-level error-handling parser requires: the applicative parsing interface, the monadic interface, and commitment.There are two additional basic combinators that we expect to be implemented afresh for every concrete type, but which (for technical reasons) cannot be class methods. They are next and satisfy.J polyparseThe  Commitment class is an abstraction over all the current concrete representations of monadic/applicative parser combinators in this package. The common feature is two-level error-handling. Some primitives must be implemented specific to each parser type (e.g. depending on whether the parser has a running state, or whether it is lazy). But given those primitives, large numbers of combinators do not depend any further on the internal structure of the particular parser.K polyparseCommit 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.L polyparsep L f applies the transformation f& to any error message generated in p, having no effect if p succeeds.M polyparseParse 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.N polyparselApply a parsed function to a parsed value. Rather like ordinary function application lifted into parsers.O polyparsex O yC parses both x and y, but discards the result of y. Rather like const lifted into parsers.P polyparseWhen a simple fail is not strong enough, use failBad for emphasis. An emphasised (severe) error cannot be overridden by choice operators.Q polyparse adjustErrBad is just like  adjustErr4 except it also raises the severity of the error.R polyparse6Parse the first alternative in the list that succeeds.S polyparseJHelper for formatting error messages: indents all lines by a fixed amount.T polyparseH'exactly n p' parses precisely n items, using the parser p, in sequence.U polyparseD'upto n p' parses n or fewer items, using the parser p, in sequence.V polyparse Parse a non-empty list of items.W polyparse2Parse a list of items separated by discarded junk.X polyparse<Parse a non-empty list of items separated by discarded junk.Y polyparseIParse a list of items, discarding the start, end, and separator items.Z polyparseJParse a bracketed item, discarding the brackets. If everything matches except the closing bracket, the whole parse fails soft, which can give less-than-satisfying error messages. If you want better error messages, try calling with e.g.  bracket open (commit close) item[ polyparsemanyFinally e t% parses a possibly-empty sequence of e's, terminated by a t . The final t is discarded. Any parse failures could be due either to a badly-formed terminator or a badly-formed element, so it raises both possible errors.\ polyparse manyFinally' is like  manyFinallyM, except when the terminator parser overlaps with the element parser. In manyFinally e t, the parser t is tried only when parser e fails, whereas in manyFinally' e t , the parser t' is always tried first, then parser e8 only if the terminator is not found. For instance, &manyFinally (accept "01") (accept "0") on input  "0101010" returns ["01","01","01"] , whereas  manyFinally'. with the same arguments and input returns [].IJKLMNOPQRSTUVWXYZ[\JKLMINOPQSRTUVWXYZ[\N3O3Safev] polyparse=A return type like Either, that distinguishes not only between right and wrong answers, but also has commitment, so that a failure cannot be undone. This should only be used for writing very primitive parsers - really it is an internal detail of the library. The z type is the remaining unconsumed input.a polyparseJConvert a Result to an Either, paired with the remaining unconsumed input.]^_`a]^_`aSafec polyparseThis Parser datatype is a fairly generic parsing monad with error reporting. It can be used for arbitrary token types, not just String input. (If you require a running state, use module Poly.State instead)e polyparsep e qE 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.f polyparse6Simply return the next token in the input tokenstream.g polyparseBSucceed if the end of file/input has been reached, fail otherwise.h polyparse:Return the next token if it satisfies the given predicate.i polyparseReturn the next token if it satisfies the given predicate. The String argument describes the function, for better error messages.j polyparse3Push 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. ]^_`cdefghij cd]^_`fghieje6SafeJr polyparse*Apply a parser to an input token sequence.>STUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ[\]^_`cdefghijr cd]^_`rfghiejSafe>STUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ[\]^_`cdefghijrSafes polyparse The class Parse is a replacement for Read, operating over String input. Essentially, it permits better error messages for why something failed to parse. It is rather important that parseM can read back exactly what is generated by the corresponding instance of show*. To apply a parser to some text, use  runParser.t polyparse|A straightforward parser for an item. (A minimal definition of a class instance requires either |parse| or |parsePrec|.)u polyparseA straightforward parser for an item, given the precedence of any surrounding expression. (Precedence determines whether parentheses are mandatory or optional.)v polyparseParsing a list of items by default accepts the [] and comma syntax, except when the list is really a character string using "".w polyparse7A synonym for Parser Char, i.e. string input (no state)x polyparseIf there already exists a Read instance for a type, then we can make a Parser for it, but with only poor error-reporting. The string argument is the expected type or value (for error-reporting only).y polyparseyIf you have a TextParser for a type, you can easily make it into a Read instance, by throwing away any error messages.z polyparseyIf you have a TextParser for a type, you can easily make it into a Read instance, by throwing away any error messages.{ polyparseOne lexical chunk. This is Haskell'98-style lexing - the result should match Prelude.lex apart from better error-reporting.p polyparsepOne lexical chunk (Haskell'98-style lexing - the result should match Prelude.lex apart from error-reporting).| polyparseEnsure that the next input word is the given string. (Note the input is lexed as haskell, so wordbreaks at spaces, symbols, etc.)} polyparsemEnsure that the next input word is the given string. (No lexing, so mixed spaces, symbols, are accepted.)~ polyparse#Allow nested parens around an item. polyparseHAllow nested parens around an item (one set required when Bool is True). polyparsewDeal with named field syntax. The string argument is the field name, and the parser returns the value of the field. polyparseParse one of a bunch of alternative constructors. In the list argument, the first element of the pair is the constructor name, and the second is the parser for the rest of the value. The first matching parse is returned. polyparseParse one of the given nullary constructors (an enumeration). The string argument is the name of the type, and the list argument should contain all of the possible enumeration values. polyparseKParse a Haskell character literal, including the surrounding single quotes. polyparseKParse a Haskell character literal, excluding the surrounding single quotes. polyparse0Simply return the entire remaining input String.WSTUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ[\]^_`cdefghijrstuvwxyz{|}~wstuvxyz{|}~SafeY polyparseThis Parser datatype is a specialised parsing monad with error reporting. This version is specialised to pre-lexed String input, where the lexer has been written to yield a  LexReturn. polyparseIn a strict language, where creating the entire input list of tokens in one shot may be infeasible, we can use a lazy "callback" kind of architecture instead. The lexer returns a single token at a time, together with a continuation. The nextf parser is responsible for pulling on the token stream, applying the continuation where necessary. polyparse*Apply a parser to an input token sequence. polyparsep  qE 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. polyparse6Simply return the next token in the input tokenstream. polyparseBSucceed if the end of file/input has been reached, fail otherwise. polyparse:Return the next token if it satisfies the given predicate. polyparse3Push 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.@STUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ[\]^_`]^_`6 NoneM polyparseThe only differences between a Plain and a Lazy parser are the instance of Applicative, and the type (and implementation) of runParser. We therefore need to newtypeG the original Parser type, to allow it to have a different instance. polyparse*Apply a parser to an input token sequence. polyparse6Simply return the next token in the input tokenstream. polyparseBSucceed if the end of file/input has been reached, fail otherwise. polyparse:Return the next token if it satisfies the given predicate. polyparseReturn the next token if it satisfies the given predicate. The String argument describes the predicate for better error messages. polyparsep  qE 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. polyparse3Push 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.>STUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ[\]^_` ]^_` Safe  polyparseThis Parser datatype is a specialised parsing monad with error reporting. Whereas the standard version can be used for arbitrary token types, this version is specialised to ByteString input only. polyparse*Apply a parser to an input token sequence. polyparse6Simply return the next token in the input tokenstream. polyparseBSucceed if the end of file/input has been reached, fail otherwise. polyparse:Return the next token if it satisfies the given predicate. polyparsep  qE 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. polyparse manySatisfy p& is a more efficient fused version of many (satisfy p) polyparsemany1Satisfy p& is a more efficient fused version of many1 (satisfy p) polyparse3Push 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."IJKLMNOPQRSTUVWXYZ[\]^_`]^_` Safe? polyparse The class Parse is a replacement for Read, operating over String input. Essentially, it permits better error messages for why something failed to parse. It is rather important that parseM can read back exactly what is generated by the corresponding instance of show*. To apply a parser to some text, use  runParser. polyparseA straightforward parser for an item. (A minimal definition of a class instance requires either |parse| or |parsePrec|. In general, for a type that never needs parens, you should define |parse|, but for a type that _may_ need parens, you should define |parsePrec|.) polyparseA straightforward parser for an item, given the precedence of any surrounding expression. (Precedence determines whether parentheses are mandatory or optional.) polyparseParsing a list of items by default accepts the [] and comma syntax, except when the list is really a character string using "". polyparseCA synonym for a ByteString Parser, i.e. bytestring input (no state) polyparseeIf there already exists a Read instance for a type, then we can make a Parser for it, but with only poor error-reporting. The string argument is the expected type or value (for error-reporting only). Use of this wrapper function is NOT recommended with ByteString, because there is a lot of inefficiency in repeated conversions to/from String. polyparseIf you have a TextParser for a type, you can easily make it into a Read instance, by throwing away any error messages. Use of this wrapper function is NOT recommended with ByteString, because there is a lot of inefficiency in conversions to/from String. polyparseIf you have a TextParser for a type, you can easily make it into a Read instance, by throwing away any error messages. Use of this wrapper function is NOT recommended with ByteString, because there is a lot of inefficiency in conversions to/from String. polyparse)One lexical chunk (Haskell-style lexing). polyparseEnsure that the next input word is the given string. (Note the input is lexed as haskell, so wordbreaks at spaces, symbols, etc.) polyparsemEnsure that the next input word is the given string. (No lexing, so mixed spaces, symbols, are accepted.) polyparse3Allow optional nested string parens around an item. polyparseHAllow nested parens around an item (one set required when Bool is True). polyparsewDeal with named field syntax. The string argument is the field name, and the parser returns the value of the field. polyparseParse one of a bunch of alternative constructors. In the list argument, the first element of the pair is the constructor name, and the second is the parser for the rest of the value. The first matching parse is returned. polyparseParse one of the given nullary constructors (an enumeration). The string argument is the name of the type, and the list argument should contain all of the possible enumeration values. polyparse>For any numeric parser, permit a negation sign in front of it. polyparseParse any (unsigned) Integral numeric literal. Needs a base, radix, isDigit predicate, and digitToInt converter, appropriate to the result type. polyparseKParse a decimal, octal, or hexadecimal (unsigned) Integral numeric literal. polyparseKParse a decimal, octal, or hexadecimal (unsigned) Integral numeric literal. polyparseKParse a decimal, octal, or hexadecimal (unsigned) Integral numeric literal. polyparseparseUnsignedInteger uses the underlying ByteString readInteger, so will be a lot faster than the generic character-by-character parseInt. polyparseDParse any (unsigned) Floating numeric literal, e.g. Float or Double. polyparseGParse a Haskell character literal, including surrounding single quotes. polyparseGParse a Haskell character literal, excluding surrounding single quotes. polyparse-Simply return the remaining input ByteString. polyparse.Simply return the remaining input as a String.=IJKLMNOPQRSTUVWXYZ[\]^_` SafeV  polyparseThis Parser datatype is a specialised parsing monad with error reporting. Whereas the standard version can be used for arbitrary token types, this version is specialised to ByteString input only. polyparse*Apply a parser to an input token sequence. polyparse6Simply return the next token in the input tokenstream. polyparseBSucceed if the end of file/input has been reached, fail otherwise. polyparse:Return the next token if it satisfies the given predicate. polyparsep  qE 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. polyparse manySatisfy p& is a more efficient fused version of many (satisfy p) polyparsemany1Satisfy p& is a more efficient fused version of many1 (satisfy p) polyparse3Push 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."IJKLMNOPQRSTUVWXYZ[\]^_`]^_` Safej  polyparseThis Parser datatype is a fairly generic parsing monad with error reporting, and running state. It can be used for arbitrary token types, not just String input. (If you do not require a running state, use module Poly.Plain instead) polyparsep  qE 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. polyparse6Simply return the next token in the input tokenstream.  polyparseBSucceed if the end of file/input has been reached, fail otherwise.  polyparse:Return the next token if it satisfies the given predicate.  polyparseUpdate the internal state.  polyparseQuery the internal state.  polyparse"Deliver the entire internal state. polyparse3Push 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.]^_`     ]^_`     6NoneM  polyparseThe only differences between a State and a StateLazy parser are the instance of Applicative, and the type (and implementation) of runParser. We therefore need to newtypeG the original Parser type, to allow it to have a different instance. polyparse*Apply a parser to an input token sequence. polyparse6Simply return the next token in the input tokenstream. polyparseBSucceed if the end of file/input has been reached, fail otherwise. polyparse:Return the next token if it satisfies the given predicate. polyparsep  qE 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. polyparse3Push 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. polyparseUpdate the internal state. polyparseQuery the internal state.  polyparse"Deliver the entire internal state.@STUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ\]^_` !#]^_`! IJKLMNOPQRSTUVWXYZ\Safen) polyparse*Apply a parser to an input token sequence.@STUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ[\]^_`     )]^_`)     Safe * polyparseThis Parser datatype is a specialised parsing monad with error reporting. Whereas the standard version can be used for arbitrary token types, this version is specialised to Text input only., polyparse*Apply a parser to an input token sequence.- polyparse6Simply return the next token in the input tokenstream.. polyparseBSucceed if the end of file/input has been reached, fail otherwise./ polyparse:Return the next token if it satisfies the given predicate.0 polyparsep 0 qE 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.1 polyparse manySatisfy p& is a more efficient fused version of many (satisfy p)2 polyparsemany1Satisfy p& is a more efficient fused version of many1 (satisfy p)3 polyparseUpdate the internal state.4 polyparseQuery the internal state.5 polyparse"Deliver the entire internal state.6 polyparse3Push 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.BSTUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ[\]^_`*+,-./0123456*+]^_`,-./0123456Safe > polyparseThis Parser datatype is a specialised parsing monad with error reporting. Whereas the standard version can be used for arbitrary token types, this version is specialised to Text input only.@ polyparse*Apply a parser to an input token sequence.A polyparse6Simply return the next token in the input tokenstream.B polyparseBSucceed if the end of file/input has been reached, fail otherwise.C polyparse:Return the next token if it satisfies the given predicate.D polyparsep D qE 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.E polyparse manySatisfy p& is a more efficient fused version of many (satisfy p)F polyparsemany1Satisfy p& is a more efficient fused version of many1 (satisfy p)G polyparse3Push 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.?STUVWXYZ[\]^_`abcdefghijklmnoIJKLMNOPQRSTUVWXYZ[\]^_`>?@ABCDEFG>?]^_`@ABCDEFGq !"#$%&'()*+,-./0123456789:;<=>?@ !"#ABCDEF789:;<GHIJKLMNOPQRSTUV#WXYZ[\]^_`=abFcd89:;<efghijklmnopqrstuvwxyz{|}~e_`=aFd8;c9:<   e ` = a b _ F d 8 ; < : 9 c   e ` = a _ F d 8 ; c 9 : < f g h i j k l m n o p q r s t u v w x y z { | } ~    e ` = a _ F d 8 ; c 9 : <   _ ` = a F c d 8 9 : ; <e`=a_FWd8;<:9cee`=a_Fd8;c9:<e`=a_Fd8;c9:<%polyparse-1.13-EKcHViyOzJG8eHtbiavVB2#Text.ParserCombinators.HuttonMeijer*Text.ParserCombinators.HuttonMeijerWallace Text.ParserCombinators.Poly.Base"Text.ParserCombinators.Poly.Result"Text.ParserCombinators.Poly.Parser!Text.ParserCombinators.Poly.Plain Text.ParseText.ParserCombinators.Poly.Lex Text.ParserCombinators.Poly.Lazy*Text.ParserCombinators.Poly.ByteStringCharText.Parse.ByteString&Text.ParserCombinators.Poly.ByteString'Text.ParserCombinators.Poly.StateParser%Text.ParserCombinators.Poly.StateLazy!Text.ParserCombinators.Poly.State%Text.ParserCombinators.Poly.StateText Text.ParserCombinators.Poly.TextText.ParserCombinators.PolyParserPitemfirstpapply+++satmanymany1sepbysepby1chainlchainl1chainrchainr1opsbracketchardigitlowerupperletteralphanumstringidentnatintspacescommentjunkskiptokennaturalintegersymbol identifier$fMonadPlusParser$fAlternativeParser$fMonadFailParser $fMonadParser$fApplicativeParser$fFunctorParsereofpapply'toknottoktoEOFelserrorstupdstquerystgetreparse PolyParse Commitmentcommit adjustErroneOf'applydiscardfailBad adjustErrBadoneOfindentexactlyuptosepBysepBy1 bracketSep manyFinally manyFinally'ResultSuccessFailure CommittedresultToEither$fFunctorResultonFailnextsatisfy satisfyMsg$fCommitmentParser$fPolyParseParser runParserParseparse parsePrec parseList TextParser parseByRead readByParsereadsPrecByParsePrecwordisWordliteraloptionalParensparensfield constructors enumeration parseSignedparseIntparseDecparseOctparseHex parseFloat parseLitChar' parseLitChar allAsString $fParse[] $fParseEither $fParseMaybe $fParse(,,) $fParse(,) $fParse()$fParseOrdering $fParseBool $fParseChar $fParseDouble $fParseFloat$fParseInteger $fParseInt LexReturn LexFinish manySatisfy many1SatisfyparseUnsignedIntegerallAsByteStringstUpdatestQuerystGetbaseGHC.Errerror parseerrorGHC.Base<$ Applicativepure<*>*>liftA2<*Control.Applicativeoptional WrappedMonad WrapMonad unwrapMonad WrappedArrow WrapArrow unwrapArrowZipList getZipListData.Functor.ConstConstgetConst Data.Functor<$>liftA3liftA<**> Alternativeempty<|>someoldword