uulib 0.9.17 → 0.9.19
raw patch · 10 files changed
+163/−72 lines, 10 filesnew-uploader
Files
- README +2/−0
- src/UU/PPrint.hs +3/−3
- src/UU/Parsing/Derived.hs +46/−5
- src/UU/Parsing/Interface.hs +63/−28
- src/UU/Parsing/Merge.hs +0/−4
- src/UU/Parsing/Offside.hs +33/−12
- src/UU/Parsing/Perms.hs +0/−4
- src/UU/Scanner/GenTokenParser.hs +1/−5
- src/UU/Scanner/TokenParser.hs +2/−6
- uulib.cabal +13/−5
README view
@@ -1,2 +1,4 @@ This is a conventional cabal package and can be installed accordingly.++See also http://foswiki.cs.uu.nl/foswiki/HUT/WebHome
src/UU/PPrint.hs view
@@ -57,11 +57,11 @@ ) where +import System.IO (Handle,hPutStr,hPutChar,stdout)+ #if __GLASGOW_HASKELL__ >= 710-import Prelude hiding ( (<$>) )+import Prelude hiding ((<$>)) #endif--import System.IO (Handle,hPutStr,hPutChar,stdout) infixr 5 </>,<//>,<$>,<$$> infixr 6 <>,<+>
src/UU/Parsing/Derived.hs view
@@ -1,15 +1,54 @@ {-# LANGUAGE CPP #-} -module UU.Parsing.Derived where+module UU.Parsing.Derived+ ( -- * Checking+ acceptsepsilon+ , mnz+ + -- * Prelude defs+ , (<..>)+ , pExcept+ , opt -#if __GLASGOW_HASKELL__ >= 710-import Prelude hiding ( (<$>), (<*>), (<*), (*>) )-#endif+ -- * Sequential compositions+ , asList+ , asList1+ , asOpt+ , (<+>)+ , (<**>)+ , (<$$>)+ , (<??>)+ , (<?>)+ , pPacked + -- * Iterating parsers+ , pFoldr_ng, pFoldr_gr, pFoldr+ , pFoldr1_ng, pFoldr1_gr, pFoldr1+ , pFoldrSep_ng, pFoldrSep_gr, pFoldrSep+ , pFoldr1Sep_ng, pFoldr1Sep_gr, pFoldr1Sep+ + , pList_ng, pList_gr, pList+ , pList1_ng, pList1_gr, pList1+ , pListSep_ng, pListSep_gr, pListSep+ , pList1Sep_ng, pList1Sep_gr, pList1Sep+ + , pChainr_ng, pChainr_gr, pChainr+ , pChainl_ng, pChainl_gr, pChainl++ -- * Misc+ , pAny+ , pAnySym+ , pToks+ , pLocate+ )+ where+ import UU.Parsing.Interface+import Control.Applicative infixl 2 <?>-infixl 4 <**>, <??>, <+>+-- infixl 4 <**>+infixl 4 <??>, <+> infixl 2 `opt` infixl 5 <..> @@ -79,6 +118,7 @@ (<+>) :: (IsParser p s) => p a -> p b -> p (a, b) pa <+> pb = (,) <$> pa <*> pb +{- -- | Suppose we have a parser a with two alternatives that both start -- with recognizing a non-terminal p, then we will typically rewrite: --@@ -90,6 +130,7 @@ -- > a = p <**> (f <$$> q <|> g <$$> r) (<**>) :: (IsParser p s) => p a -> p (a -> b) -> p b p <**> q = (\ x f -> f x) <$> p <*> q+-} (<$$>) :: (IsParser p s) => (a -> b -> c) -> p b -> p (a -> c) f <$$> p = pSucceed (flip f) <*> p
src/UU/Parsing/Interface.hs view
@@ -6,27 +6,27 @@ ( AnaParser, pWrap, pMap , module UU.Parsing.MachineInterface , module UU.Parsing.Interface+ , (<*>), (<*), (*>), (<$>), (<$), (<|>) ) where -#if __GLASGOW_HASKELL__ >= 710-import Prelude hiding ( (<*>), (<*) )-#else-import Prelude hiding ((<*>))-#endif- import GHC.Prim import UU.Parsing.Machine import UU.Parsing.MachineInterface --import IOExts import System.IO.Unsafe import System.IO+import Control.Applicative+ -- ================================================================================== -- ===== PRIORITIES ====================================================================== -- =======================================================================================-infixl 3 <|>-infixl 4 <*>, <$> -infixl 4 <$, <*, *> +{- 20150402 AD: use of Applicative, Functor, Alternative+infixl 3 <|>:+infixl 4 <*>:, <$>: +infixl 4 <$:+infixl 4 <*:, *>:+-} -- ======================================================================================= -- ===== ANAPARSER INSTANCES =============================================================@@ -40,23 +40,37 @@ -- to write parsers. A minimal complete instance definition consists of -- definitions for '(<*>)', '(<|>)', 'pSucceed', 'pLow', 'pFail', -- 'pCostRange', 'pCostSym', 'getfirsts', 'setfirsts', and 'getzerop'.-class IsParser p s | p -> s where+-- All operators available through 'Applicative', 'Functor", and 'Alternative' have the same names suffixed with ':'.+class (Applicative p, Alternative p, Functor p) => IsParser p s | p -> s where+ {- 20150402 AD: use of Applicative, Functor, Alternative -- | Sequential composition. Often used in combination with <$>. -- The function returned by parsing the left-hand side is applied -- to the value returned by parsing the right-hand side. -- Note: Implementations of this combinator should lazily match on -- and evaluate the right-hand side parser. The derived combinators -- for list parsing will explode if they do not.- (<*>) :: p (a->b) -> p a -> p b+ (<*>:) :: p (a->b) -> p a -> p b -- | Value ignoring versions of sequential composition. These ignore -- either the value returned by the parser on the right-hand side or -- the left-hand side, depending on the visual direction of the -- combinator.- (<* ) :: p a -> p b -> p a- ( *>) :: p a -> p b -> p b+ (<*: ) :: p a -> p b -> p a+ ( *>:) :: p a -> p b -> p b -- | Applies the function f to the result of p after parsing p.- (<$>) :: (a->b) -> p a -> p b- (<$ ) :: b -> p a -> p b+ (<$>:) :: (a->b) -> p a -> p b+ (<$: ) :: b -> p a -> p b+ -}+ {- 20150402 AD: use of Applicative, Functor, Alternative+ f <$>: p = pSucceed f <*>: p+ f <$: q = pSucceed f <* q+ p <*: q = pSucceed const <*>: p <*>: q+ p *>: q = pSucceed (flip const) <*>: p <*>: q+ -}+ {- 20150402 AD: use of Applicative, Functor, Alternative+ -- | Alternative combinator. Succeeds if either of the two arguments+ -- succeed, and returns the result of the best success parse.+ (<|>:) :: p a -> p a -> p a+ -} -- | Two variants of the parser for empty strings. 'pSucceed' parses the -- empty string, and fully counts as an alternative parse. It returns the -- value passed to it.@@ -64,13 +78,7 @@ -- | 'pLow' parses the empty string, but alternatives to pLow are always -- preferred over 'pLow' parsing the empty string. pLow :: a -> p a- f <$> p = pSucceed f <*> p- f <$ q = pSucceed f <* q- p <* q = pSucceed const <*> p <*> q- p *> q = pSucceed (flip const) <*> p <*> q- -- | Alternative combinator. Succeeds if either of the two arguments- -- succeed, and returns the result of the best success parse.- (<|>) :: p a -> p a -> p a+ pSucceed = pure -- | This parser always fails, and never returns any value at all. pFail :: p a -- | Parses a range of symbols with an associated cost and the symbol to@@ -88,6 +96,7 @@ getfirsts :: p v -> Expecting s -- | Set the firsts set in the parser. setfirsts :: Expecting s -> p v -> p v+ pFail = empty pSym a = pCostSym 5# a a pRange = pCostRange 5# -- | 'getzerop' returns @Nothing@ if the parser can not parse the empty@@ -100,20 +109,26 @@ getonep :: p v -> Maybe (p v) +-- =======================================================================================+-- ===== AnaParser =======================================================================+-- =======================================================================================+ -- | The fast 'AnaParser' instance of the 'IsParser' class. Note that this -- requires a functioning 'Ord' for the symbol type s, as tokens are -- often compared using the 'compare' function in 'Ord' rather than always -- using '==' rom 'Eq'. The two do need to be consistent though, that is -- for any two @x1@, @x2@ such that @x1 == x2@ you must have -- @compare x1 x2 == EQ@.-instance (Ord s, Symbol s, InputState state s p, OutputState result) => IsParser (AnaParser state result s p) s where- (<*>) p q = anaSeq libDollar libSeq ($) p q- (<* ) p q = anaSeq libDollarL libSeqL const p q- ( *>) p q = anaSeq libDollarR libSeqR (flip const) p q+instance (Ord s, Symbol s, InputState state s p, OutputState result) => IsParser (AnaParser state result s p) s where+ {- 20150402 AD: use of Applicative, Functor, Alternative+ (<*>:) p q = anaSeq libDollar libSeq ($) p q+ (<*: ) p q = anaSeq libDollarL libSeqL const p q+ ( *>:) p q = anaSeq libDollarR libSeqR (flip const) p q pSucceed = anaSucceed- pLow = anaLow- (<|>) = anaOr+ (<|>:) = anaOr pFail = anaFail+ -}+ pLow = anaLow pCostRange = anaCostRange pCostSym i ins sym = anaCostRange i ins (mk_range sym sym) getfirsts = anaGetFirsts@@ -126,6 +141,26 @@ } getonep p = let tab = table (onep p) in if null tab then Nothing else Just (mkParser (leng p) Nothing (onep p))++instance (Ord s, Symbol s, InputState state s p, OutputState result) => Applicative (AnaParser state result s p) where+ (<*>) p q = anaSeq libDollar libSeq ($) p q+ {-# INLINE (<*>) #-}+ (<* ) p q = anaSeq libDollarL libSeqL const p q+ {-# INLINE (<*) #-}+ ( *>) p q = anaSeq libDollarR libSeqR (flip const) p q+ {-# INLINE (*>) #-}+ pure = anaSucceed+ {-# INLINE pure #-}++instance (Ord s, Symbol s, InputState state s p, OutputState result) => Alternative (AnaParser state result s p) where+ (<|>) = anaOr+ {-# INLINE (<|>) #-}+ empty = anaFail+ {-# INLINE empty #-}++instance (Ord s, Symbol s, InputState state s p, OutputState result, Applicative (AnaParser state result s p)) => Functor (AnaParser state result s p) where+ fmap f p = pure f <*> p+ {-# INLINE fmap #-} instance InputState [s] s (Maybe s) where splitStateE [] = Right' []
src/UU/Parsing/Merge.hs view
@@ -2,10 +2,6 @@ module UU.Parsing.Merge((<||>), pMerged, list_of) where -#if __GLASGOW_HASKELL__ >= 710-import Prelude hiding ( (<$>), (<*>) )-#endif- import UU.Parsing -- ==== merging
src/UU/Parsing/Offside.hs view
@@ -19,12 +19,9 @@ , OffsideParser(..) ) where -#if __GLASGOW_HASKELL__ >= 710-import Prelude hiding ( (<$>), (<$), (<*>), (<*), (*>) )-#endif- import GHC.Prim import Data.Maybe+import Control.Applicative import UU.Parsing.Interface import UU.Parsing.Machine import UU.Parsing.Derived(opt, pFoldr1Sep,pList,pList1, pList1Sep)@@ -48,7 +45,7 @@ data IndentContext = Cxt Bool -- properties: allows nesting on equal indentation (triggered by Trigger_IndentGE)- Int -- indentation+ Int -- indentation data OffsideInput inp s p = Off p -- position@@ -189,15 +186,17 @@ newtype OffsideParser i o s p a = OP (AnaParser (OffsideInput i s p) o (OffsideSymbol s) p a) instance (Symbol s, Ord s, InputState i s p, OutputState o) => IsParser (OffsideParser i o s p) s where- (<*>) = operator (<*>)- (<* ) = operator (<* )- ( *>) = operator ( *>)- (<|>) = operator (<|>)- (<$>) = operatorr (<$>)- (<$ ) = operatorr (<$ )+ {-+ (<*>:) = operator (<*>:)+ (<*: ) = operator (<*: )+ ( *>:) = operator ( *>:)+ (<|>:) = operator (<|>:)+ (<$>:) = operatorr (<$>:)+ (<$: ) = operatorr (<$ ) pSucceed = OP . pSucceed- pLow = OP . pLow pFail = OP pFail+ -}+ pLow = OP . pLow pCostRange c s (Range l r) = OP (getSymbol <$> pCostRange c (Symbol s) (Range (Symbol l)(Symbol r))) pCostSym c s t = OP (getSymbol <$> pCostSym c (Symbol s) (Symbol t)) pSym s = OP (getSymbol <$> pSym (Symbol s)) @@ -206,6 +205,28 @@ setfirsts exp (OP p) = OP (setfirsts (addSymbol exp) p) getzerop (OP p) = fmap OP (getzerop p) getonep (OP p) = fmap OP (getonep p)++instance (Symbol s, Ord s, InputState i s p, OutputState o) => Applicative (OffsideParser i o s p) where+ (<*>) = operator (<*>)+ {-# INLINE (<*>) #-}+ (<* ) = operator (<* )+ {-# INLINE (<*) #-}+ ( *>) = operator ( *>)+ {-# INLINE (*>) #-}+ pure = OP . pure+ {-# INLINE pure #-}++instance (Symbol s, Ord s, InputState i s p, OutputState o) => Alternative (OffsideParser i o s p) where+ (<|>) = operator (<|>)+ {-# INLINE (<|>) #-}+ empty = OP pFail+ {-# INLINE empty #-}++instance (Symbol s, Ord s, InputState i s p, OutputState o, Applicative (OffsideParser i o s p)) => Functor (OffsideParser i o s p) where+ fmap = operatorr fmap+ {-# INLINE fmap #-}+ (<$) = operatorr (<$)+ {-# INLINE (<$) #-} removeSymbol exp = case exp of ESym (Range (Symbol l) (Symbol r)) -> ESym (Range l r)
src/UU/Parsing/Perms.hs view
@@ -3,10 +3,6 @@ module UU.Parsing.Perms(Perms(), pPerms, pPermsSep, succeedPerms, (~*~), (~$~)) where -#if __GLASGOW_HASKELL__ >= 710-import Prelude hiding ( (<$>), (<$), (<*>) )-#endif- import UU.Parsing import Data.Maybe
src/UU/Scanner/GenTokenParser.hs view
@@ -4,12 +4,8 @@ module UU.Scanner.GenTokenParser where -#if __GLASGOW_HASKELL__ >= 710-import Prelude hiding ( (<$>) )-#endif- import GHC.Base-import UU.Parsing.Interface(IsParser(pCostSym, pSym, (<$>)))+import UU.Parsing.Interface(IsParser(pCostSym, pSym), (<$>)) import UU.Scanner.GenToken(GenToken(..)) import UU.Scanner.Position(Pos, noPos)
src/UU/Scanner/TokenParser.hs view
@@ -2,11 +2,7 @@ module UU.Scanner.TokenParser where -#if __GLASGOW_HASKELL__ >= 710-import Prelude hiding ( (<$>), (<$) )-#endif--import UU.Parsing.Interface(IsParser(..))+import UU.Parsing.Interface(IsParser(..), (<$), (<$>)) import UU.Parsing.Derived(pListSep, pPacked) import UU.Scanner.Position(Pos) import UU.Scanner.GenTokenParser(pReserved, pValToken)@@ -42,7 +38,7 @@ pFractionPos = pValToken TkFraction "0.0" pVaridPos = pValToken TkVarid "<identifier>" pConidPos = pValToken TkConid "<Identifier>" -pConsymPos = pValToken TkConOp "<conoperator>"+pConsymPos = pValToken TkConOp "<conoperator>" pVarsymPos = pValToken TkOp "<operator>" pTextnmPos = pValToken TkTextnm "<name>" pTextlnPos = pValToken TkTextln "<line>"
uulib.cabal view
@@ -1,22 +1,30 @@-cabal-version: >= 1.6-build-type: Simple name: uulib-version: 0.9.17+version: 0.9.19 license: BSD3 license-file: COPYRIGHT-maintainer: S. Doaitse Swierstra-homepage: http://www.cs.uu.nl/wiki/HUT/WebHome++maintainer: UU Computer Science+homepage: https://github.com/UU-ComputerScience/uulib+bug-reports: https://github.com/UU-ComputerScience/uulib/issues description: Fast Parser Combinators and Pretty Printing Combinators synopsis: Haskell Utrecht Tools Library category: Parsing stability: Stable+ copyright: Universiteit Utrecht+cabal-version: >= 1.6+build-type: Simple+ extra-source-files: README, examples/bibtex/Bibtex.hs, examples/parser/Example.hs, examples/parser/Makefile, examples/parser/README, examples/parser/Scanner.x++source-repository head+ type: git+ location: https://github.com/UU-ComputerScience/uulib.git library build-depends: base>=4 && <5, ghc-prim >= 0.2.0.0