Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
derekelkins committed Jan 13, 2008
0 parents commit 93826a6
Show file tree
Hide file tree
Showing 27 changed files with 2,166 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright 1999-2000, Daan Leijen; 2007, Paolo Martini. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

This software is provided by the copyright holders "as is" and any express or
implied warranties, including, but not limited to, the implied warranties of
merchantability and fitness for a particular purpose are disclaimed. In no
event shall the copyright holders be liable for any direct, indirect,
incidental, special, exemplary, or consequential damages (including, but not
limited to, procurement of substitute goods or services; loss of use, data,
or profits; or business interruption) however caused and on any theory of
liability, whether in contract, strict liability, or tort (including
negligence or otherwise) arising in any way out of the use of this software,
even if advised of the possibility of such damage.
6 changes: 6 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Main (main) where

import Distribution.Simple

main :: IO ()
main = defaultMain
36 changes: 36 additions & 0 deletions Text/Parsec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-----------------------------------------------------------------------------
-- |
-- Module : Text.Parsec
-- Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
-- License : BSD-style (see the LICENSE file)
--
-- Maintainer : paolo@nemail.it
-- Stability : provisional
-- Portability : portable
--
-----------------------------------------------------------------------------

module Text.Parsec
( module Text.Parsec.Prim
, module Text.Parsec.Char
, module Text.Parsec.Combinator
, module Text.Parsec.String
, module Text.Parsec.ByteString
, module Text.Parsec.LazyByteString
, ParseError
, errorPos
, SourcePos
, SourceName, Line, Column
, sourceName, sourceLine, sourceColumn
, incSourceLine, incSourceColumn
, setSourceLine, setSourceColumn, setSourceName
) where

import Text.Parsec.Pos
import Text.Parsec.Error
import Text.Parsec.Prim
import Text.Parsec.Char
import Text.Parsec.Combinator
import Text.Parsec.String hiding ( Parser, GenParser, parseFromFile )
import Text.Parsec.ByteString hiding ( Parser, GenParser, parseFromFile )
import Text.Parsec.LazyByteString hiding ( Parser, GenParser, parseFromFile )
38 changes: 38 additions & 0 deletions Text/Parsec/ByteString.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-----------------------------------------------------------------------------
-- |
-- Module : Text.Parsec.ByteString
-- Copyright : (c) Paolo Martini 2007
-- License : BSD-style (see the LICENSE file)
--
-- Maintainer : paolo@nemail.com
-- Stability : provisional
-- Portability : portable
--
-----------------------------------------------------------------------------

{-# LANGUAGE FlexibleInstances #-}

module Text.Parsec.ByteString
( Parser, GenParser, parseFromFile
) where

import Text.Parsec.Error
import Text.Parsec.Prim

import qualified Data.ByteString.Base as B
import qualified Data.ByteString.Char8 as C

instance (Monad m) => Stream C.ByteString m Char where
uncons s
| C.null s = return $ Nothing
| otherwise = return $ Just $
( toEnum $ fromEnum $ B.unsafeHead s
, B.unsafeTail s)

type Parser a = Parsec C.ByteString () a
type GenParser t st a = Parsec C.ByteString st a

parseFromFile :: Parser a -> String -> IO (Either ParseError a)
parseFromFile p fname
= do input <- C.readFile fname
return (runP p () fname input)
61 changes: 61 additions & 0 deletions Text/Parsec/Char.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-----------------------------------------------------------------------------
-- |
-- Module : Text.Parsec.Char
-- Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
-- License : BSD-style (see the LICENSE file)
--
-- Maintainer : paolo@nemail.it
-- Stability : provisional
-- Portability : portable
--
-- Commonly used character parsers.
--
-----------------------------------------------------------------------------

{-# LANGUAGE FlexibleContexts #-}

module Text.Parsec.Char where

import Data.Char
import Text.Parsec.Pos
import Text.Parsec.Prim

-- | Character parsers

oneOf, noneOf :: (Stream s m Char) => [Char] -> ParsecT s u m Char
oneOf cs = satisfy (\c -> elem c cs)
noneOf cs = satisfy (\c -> not (elem c cs))

spaces :: (Stream s m Char) => ParsecT s u m ()
spaces = skipMany space <?> "white space"

space, newline, tab :: (Stream s m Char) => ParsecT s u m Char
space = satisfy (isSpace) <?> "space"
newline = char '\n' <?> "new-line"
tab = char '\t' <?> "tab"

upper, lower, alphaNum, letter, digit, hexDigit, octDigit :: (Stream s m Char)
=> ParsecT s u m Char
upper = satisfy (isUpper) <?> "uppercase letter"
lower = satisfy (isLower) <?> "lowercase letter"
alphaNum = satisfy (isAlphaNum) <?> "letter or digit"
letter = satisfy (isAlpha) <?> "letter"
digit = satisfy (isDigit) <?> "digit"
hexDigit = satisfy (isHexDigit) <?> "hexadecimal digit"
octDigit = satisfy (isOctDigit) <?> "octal digit"

char :: (Stream s m Char) => Char -> ParsecT s u m Char
char c = satisfy (==c) <?> show [c]

anyChar :: (Stream s m Char) => ParsecT s u m Char
anyChar = satisfy (const True)

-- | Primitive character parsers

satisfy :: (Stream s m Char) => (Char -> Bool) -> ParsecT s u m Char
satisfy f = tokenPrim (\c -> show [c])
(\pos c cs -> updatePosChar pos c)
(\c -> if f c then Just c else Nothing)

string :: (Stream s m Char) => String -> ParsecT s u m String
string s = tokens show updatePosString s
150 changes: 150 additions & 0 deletions Text/Parsec/Combinator.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
-----------------------------------------------------------------------------
-- |
-- Module : Text.Parsec.Combinator
-- Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
-- License : BSD-style (see the LICENSE file)
--
-- Maintainer : paolo@nemail.it
-- Stability : provisional
-- Portability : portable
--
-- Commonly used generic combinators
--
-----------------------------------------------------------------------------

module Text.Parsec.Combinator
( choice
, count
, between
, option, optionMaybe, optional
, skipMany1
, many1
, sepBy, sepBy1
, endBy, endBy1
, sepEndBy, sepEndBy1
, chainl, chainl1
, chainr, chainr1
, eof, notFollowedBy
-- tricky combinators
, manyTill, lookAhead, anyToken
) where

import Control.Monad
import Text.Parsec.Prim

choice :: (Stream s m t) => [ParsecT s u m a] -> ParsecT s u m a
choice ps = foldr (<|>) mzero ps

option :: (Stream s m t) => a -> ParsecT s u m a -> ParsecT s u m a
option x p = p <|> return x

optionMaybe :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m (Maybe a)
optionMaybe p = option Nothing (liftM Just p)

optional :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m ()
optional p = do{ p; return ()} <|> return ()

between :: (Stream s m t) => ParsecT s u m open -> ParsecT s u m close
-> ParsecT s u m a -> ParsecT s u m a
between open close p
= do{ open; x <- p; close; return x }


skipMany1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m ()
skipMany1 p = do{ p; skipMany p }
{-
skipMany p = scan
where
scan = do{ p; scan } <|> return ()
-}

many1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m [a]
many1 p = do{ x <- p; xs <- many p; return (x:xs) }
{-
many p = scan id
where
scan f = do{ x <- p
; scan (\tail -> f (x:tail))
}
<|> return (f [])
-}

sepBy1,sepBy :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
sepBy p sep = sepBy1 p sep <|> return []
sepBy1 p sep = do{ x <- p
; xs <- many (sep >> p)
; return (x:xs)
}

sepEndBy1, sepEndBy :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
sepEndBy1 p sep = do{ x <- p
; do{ sep
; xs <- sepEndBy p sep
; return (x:xs)
}
<|> return [x]
}

sepEndBy p sep = sepEndBy1 p sep <|> return []


endBy1,endBy :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m sep -> ParsecT s u m [a]
endBy1 p sep = many1 (do{ x <- p; sep; return x })
endBy p sep = many (do{ x <- p; sep; return x })

count :: (Stream s m t) => Int -> ParsecT s u m a -> ParsecT s u m [a]
count n p | n <= 0 = return []
| otherwise = sequence (replicate n p)


chainr,chainl :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> a -> ParsecT s u m a
chainr p op x = chainr1 p op <|> return x
chainl p op x = chainl1 p op <|> return x

chainr1,chainl1 :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m (a -> a -> a) -> ParsecT s u m a
chainl1 p op = do{ x <- p; rest x }
where
rest x = do{ f <- op
; y <- p
; rest (f x y)
}
<|> return x

chainr1 p op = scan
where
scan = do{ x <- p; rest x }

rest x = do{ f <- op
; y <- scan
; return (f x y)
}
<|> return x

-----------------------------------------------------------
-- Tricky combinators
-----------------------------------------------------------
anyToken :: (Stream s m t, Show t) => ParsecT s u m t
anyToken = tokenPrim show (\pos tok toks -> pos) Just

eof :: (Stream s m t, Show t) => ParsecT s u m ()
eof = notFollowedBy anyToken <?> "end of input"

notFollowedBy :: (Stream s m t, Show t) => ParsecT s u m t -> ParsecT s u m ()
notFollowedBy p = try (do{ c <- p; unexpected (show [c]) }
<|> return ()
)

manyTill :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m end -> ParsecT s u m [a]
manyTill p end = scan
where
scan = do{ end; return [] }
<|>
do{ x <- p; xs <- scan; return (x:xs) }


lookAhead :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m a
lookAhead p = do{ state <- getParserState
; x <- p
; setParserState state
; return x
}
Loading

0 comments on commit 93826a6

Please sign in to comment.