-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Linear time composable parser for PEG grammars -- @package frisby @version 0.2 -- | Linear time composable parser for PEG grammars. -- -- frisby is a parser library that can parse arbitrary PEG grammars in -- linear time. Unlike other parsers of PEG grammars, frisby need not be -- supplied with all possible rules up front, allowing composition of -- smaller parsers. -- -- PEG parsers are never ambiguous and allow infinite lookahead with no -- backtracking penalty. Since PEG parsers can look ahead arbitrarily, -- they can easily express rules such as the maximal munch rule used in -- lexers, meaning no separate lexer is needed. -- -- In addition to many standard combinators, frisby provides routines to -- translate standard regex syntax into frisby parsers. -- -- PEG based parsers have a number of advantages over other parsing -- strategies: -- -- -- -- Traditionally, PEG parsers have suffered from two major flaws: -- -- -- -- frisby attempts to address both these concerns. -- -- frisby parsers achieve composability by having a compilation -- pass, recursive parsers are specified using the recursive do notation -- 'mdo' which builds up a description of your parser where the recursive -- calls for which memoized entries must be made are explicit. then -- runPeg takes this description and compiles it into a form that -- can be applied, during this compilation step it examines your composed -- parser, and collects the global table of rules needed for a packrat -- parser to work. -- -- Memory consumption is much less of an issue on modern machines; tests -- show it is not a major concern, however frisby uses a couple of -- techniques for reducing the impact. First it attempts to create -- parsers that are as lazy as possible -- this means that no more of the -- file is read into memory than is needed, and more importantly, memory -- used by the parser can be reclaimed as you process its output. -- -- frisby also attempts to optimize your parser, using -- specialized strategies when allowed to reduce the number of entries in -- your memoization tables. -- -- frisby attempts to be lazy in reading the results of parsers, parsers -- tend to work via sending out 'feeler' predicates to get an idea of -- what the rest of the file looks like before deciding what pass to -- take, frisby attempts to optimize these feeler predicates via extra -- lazyness such that they do not cause the actual computation of the -- results, but rather just compute enough to determine whether a -- predicate would have succeeded or not. -- -- (It is interesting to note that the memory efficiency of frisby -- depends vitally on being as lazy as possible, in contrast to -- traditional thoughts when it comes to memory consumption) -- -- frisby is a work in progress, it has a darcs repo at -- http://repetae.net/repos/frisby which may be browsed at -- http://repetae.net/dw/darcsweb.cgi?r=frisby;a=summary -- -- And its homepage is at http://repetae.net/computer/frisby -- -- To learn more about PEG parsers, see this paper -- http://pdos.csail.mit.edu/~baford/packrat/popl04 and Bryan -- Ford's packrat parsing page -- http://pdos.csail.mit.edu/~baford/packrat/ module Text.Parsers.Frisby data P s a data PM s a -- | Create a new rule, which may be used recursively and caches its -- results. -- -- This is intended to be use in an 'mdo' block. such as the following. -- --
--   additive = mdo
--       additive <- newRule $ multitive <> char '+' ->> additive ## uncurry (+) // multitive
--       multitive <- newRule $ primary <> char '*' ->> multitive ## uncurry (*) // primary
--       primary <- newRule $ char '(' ->> additive <<- char ')' // decimal
--       decimal <- newRule $ many1 (oneOf ['0' .. '9']) ## read
--       return additive
--   
-- -- All recursive calls must be bound via a rule. Left recursion should be -- avoided. newRule :: P s a -> PM s (P s a) -- | Run a PEG grammar. Takes the rank-2 argument in order to ensure a rule -- created in one PM session isn't returned and used in another PEG -- parser. -- -- There is no need for special error handling, as it can be trivially -- implemented via -- --
--   -- parse complete file, returning 'Nothing' if parse fails
--   fmap Just (myParser <<- eof) // unit Nothing
--   
-- -- There is also no need for the parser to return its unused input, as -- that can be retrieved via rest. -- --
--   -- Now this returns (a,String) where String is the unconsumed input.
--   myParser <> rest
--   
runPeg :: (forall s. PM s (P s a)) -> String -> a -- | Ordered choice, try left argument, if it fails try the right one. This -- does not introduce any backtracking or penalty. (//) :: P s a -> P s a -> P s a -- | Match first argument, then match the second, returning both in a tuple (<>) :: P s a -> P s b -> P s (a, b) -- | Match a pair of lists and concatenate them (<++>) :: P s [a] -> P s [a] -> P s [a] -- | Match first argument, then match the second, returning only the value -- on the right. -- --
--   x ->> y = x <> y ## snd
--   
(->>) :: P s a -> P s b -> P s b -- | Match first argument, then match the second, returning only the value -- on the left. -- --
--   x <<- y = x <> y ## fst
--   
(<<-) :: P s a -> P s b -> P s a -- | Ordered choice, try left argument, if it fails then return right -- argument. (//>) :: P s a -> a -> P s a -- | Map a parser through a function. a fancy version of fmap. (##) :: P s a -> (a -> b) -> P s b -- | Parse left argument and return the right argument. (##>) :: P s a -> b -> P s b -- | Match any character, fails on EOF anyChar :: P s Char -- | am at the beginning of the string. bof :: P s () -- | am at the end of string. eof :: P s () -- | Get current position in file as number of characters since the -- beginning. getPos :: P s Int -- | Match a specified character char :: Char -> P s Char -- | Match any character other than the ones in the list. noneOf :: [Char] -> P s Char -- | Match one of the set of characters. oneOf :: [Char] -> P s Char -- | Match some text text :: String -> P s String -- | Return a value, always succeeds unit :: a -> P s a -- | Immediately consume and return the rest of the input equivalent to -- (many anyChar), but more efficient. rest :: P s String -- | Throw away the result of something. -- --
--   discard p = p ->> unit ()
--   
discard :: P s a -> P s () -- | Fails, is identity of (//) and unit of (<>). parseFailure :: P s a -- | Parse something and return it, but do not advance the input stream. peek :: P s a -> P s a -- | Succeeds when the argument does not. doesNotMatch :: P s a -> P s () -- | always succeeds, returning true if it consumed something. isMatch :: P s a -> P s Bool -- | Succeed only if thing parsed passes a predicate. onlyIf :: P s a -> (a -> Bool) -> P s a -- | Succeeds when the argument does, but consumes no input. Equivalant to -- p -> discard (peek p) matches :: P s a -> P s () -- | Parse many of something. Behaves like * in regexes. This eats as much -- as it possibly can, if you want a minimal much rule, then use -- manyUntil which stops when a. many :: P s a -> P s [a] -- | Match one or more of something via maximal munch rule. many1 :: P s a -> P s [a] -- | Parse many of something via the minimal munch rule. behaves like *? in -- perl regexes. The final item is not consumed. manyUntil :: P s b -> P s a -> PM s (P s [a]) -- | Equivalent to -- --
--   between open close thing = open ->> thing <<- close
--   
between :: P s a -> P s b -> P s c -> P s c -- | First matching parse wins, a simple iteration of (//). choice :: [P s a] -> P s a -- | Parse something if you can, else return first value -- --
--   option a p = p // unit a
--   
option :: a -> P s a -> P s a -- | Parse something if you can, discarding it. -- --
--   option a p = discard p // unit ()
--   
optional :: P s a -> P s () -- | Create a new regular expression matching parser. it returns something -- in a possibly failing monad to indicate an error in the regular -- expression itself. newRegex :: Monad m => String -> m (PM s (P s String)) -- | Make a new regex but abort on an error in the regex string itself. regex :: String -> PM s (P s String) -- | Show a representation of the parsed regex, mainly for debugging. showRegex :: String -> IO () instance Eq Token instance Ord Token instance Num Token instance Show Token instance Ix Token instance Monad (PM s) instance MonadFix (PM s) instance Functor (PM s) instance Functor (P s) instance Applicative (P s) instance Alternative (P s) instance Monoid (P s a) instance Show Regex instance Eq Regex instance Ord Regex instance Functor Results instance Monoid (PE a) instance Alternative PE instance Applicative PE instance Functor PE instance Applicative (PM s) -- | Unicode character parsers. The character classification is identical -- to the classification in the Data.Char module. module Text.Parsers.Frisby.Char -- | Match a control character. control :: P s Char -- | Match a white-space character in the Latin-1 range. space :: P s Char -- | Match a lower-case alphabetic Unicode character. lower :: P s Char -- | Match an upper-case or title-case alphabetic Unicode character. upper :: P s Char -- | Match an alphabetic Unicode character. Equivalent to letter. alpha :: P s Char -- | Match an alphabetic or numeric digit Unicode character. alphaNum :: P s Char -- | Match a printable Unicode character. printable :: P s Char -- | Match an ASCII digit. digit :: P s Char -- | Match an ASCII octal digit. octDigit :: P s Char -- | Match an ASCII hexadecimal digit. hexDigit :: P s Char -- | Match an alphabetic Unicode character. Equivalent to alpha. letter :: P s Char -- | Match a Unicode mark character. mark :: P s Char -- | Match a Unicode numeric character. number :: P s Char -- | Match a Unicode punctuation character. punctuation :: P s Char -- | Match a Unicode symbol character. symbol :: P s Char -- | Match a Unicode space or separator character. separator :: P s Char -- | Match a character of the ASCII character set. ascii :: P s Char -- | Match a character of the ISO 8859-1 (Latin-1) character set. latin1 :: P s Char -- | Match an ASCII upper-case letter. asciiUpper :: P s Char -- | Match an ASCII lower-case letter. asciiLower :: P s Char