-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[project @ 2002-05-31 12:22:33 by panne]
Moved Parsec to its new home
- Loading branch information
panne
committed
May 31, 2002
1 parent
c7b9b8a
commit 792c0b5
Showing
37 changed files
with
4,149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
----------------------------------------------------------------------------- | ||
-- | | ||
-- Module : Text.ParserCombinators.Parsec | ||
-- Copyright : (c) Daan Leijen 1999-2001 | ||
-- License : BSD-style (see the file libraries/base/LICENSE) | ||
-- | ||
-- Maintainer : daan@cs.uu.nl | ||
-- Stability : provisional | ||
-- Portability : portable | ||
-- | ||
-- Parsec, the Fast Monadic Parser combinator library. | ||
-- <http://www.cs.uu.nl/people/daan/parsec.html> | ||
-- | ||
-- This helper module exports elements from the basic libraries. | ||
-- Inspired by: | ||
-- | ||
-- * Graham Hutton and Erik Meijer: | ||
-- Monadic Parser Combinators. | ||
-- Technical report NOTTCS-TR-96-4. | ||
-- Department of Computer Science, University of Nottingham, 1996. | ||
-- <http://www.cs.nott.ac.uk/Department/Staff/gmh/monparsing.ps> | ||
-- | ||
-- * Andrew Partridge, David Wright: | ||
-- Predictive parser combinators need four values to report errors. | ||
-- Journal of Functional Programming 6(2): 355-364, 1996 | ||
-- | ||
----------------------------------------------------------------------------- | ||
|
||
module Text.ParserCombinators.Parsec | ||
( -- complete modules | ||
module Text.ParserCombinators.Parsec.Prim | ||
, module Text.ParserCombinators.Parsec.Combinator | ||
, module Text.ParserCombinators.Parsec.Char | ||
|
||
-- module Text.ParserCombinators.Parsec.Error | ||
, ParseError | ||
, errorPos | ||
|
||
-- module Text.ParserCombinators.Parsec.Pos | ||
, SourcePos | ||
, SourceName, Line, Column | ||
, sourceName, sourceLine, sourceColumn | ||
, incSourceLine, incSourceColumn | ||
, setSourceLine, setSourceColumn, setSourceName | ||
|
||
) where | ||
|
||
import Text.ParserCombinators.Parsec.Pos -- textual positions | ||
import Text.ParserCombinators.Parsec.Error -- parse errors | ||
import Text.ParserCombinators.Parsec.Prim -- primitive combinators | ||
import Text.ParserCombinators.Parsec.Combinator -- derived combinators | ||
import Text.ParserCombinators.Parsec.Char -- character parsers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
----------------------------------------------------------------------------- | ||
-- | | ||
-- Module : Text.ParserCombinators.Parsec.Char | ||
-- Copyright : (c) Daan Leijen 1999-2001 | ||
-- License : BSD-style (see the file libraries/base/LICENSE) | ||
-- | ||
-- Maintainer : daan@cs.uu.nl | ||
-- Stability : provisional | ||
-- Portability : portable | ||
-- | ||
-- Commonly used character parsers. | ||
-- | ||
----------------------------------------------------------------------------- | ||
|
||
module Text.ParserCombinators.Parsec.Char | ||
( CharParser | ||
, spaces, space | ||
, newline, tab | ||
, upper, lower, alphaNum, letter | ||
, digit, hexDigit, octDigit | ||
, char, string | ||
, anyChar, oneOf, noneOf | ||
, satisfy | ||
) where | ||
|
||
import Data.Char | ||
import Text.ParserCombinators.Parsec.Pos( updatePosChar, updatePosString ) | ||
import Text.ParserCombinators.Parsec.Prim | ||
|
||
----------------------------------------------------------- | ||
-- Type of character parsers | ||
----------------------------------------------------------- | ||
type CharParser st a = GenParser Char st a | ||
|
||
----------------------------------------------------------- | ||
-- Character parsers | ||
----------------------------------------------------------- | ||
oneOf cs = satisfy (\c -> elem c cs) | ||
noneOf cs = satisfy (\c -> not (elem c cs)) | ||
|
||
spaces = skipMany space <?> "white space" | ||
space = satisfy (isSpace) <?> "space" | ||
|
||
newline = char '\n' <?> "new-line" | ||
tab = char '\t' <?> "tab" | ||
|
||
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 c = satisfy (==c) <?> show [c] | ||
anyChar = satisfy (const True) | ||
|
||
----------------------------------------------------------- | ||
-- Primitive character parsers | ||
----------------------------------------------------------- | ||
satisfy :: (Char -> Bool) -> CharParser st Char | ||
satisfy f = tokenPrim (\c -> show [c]) | ||
(\pos c cs -> updatePosChar pos c) | ||
(\c -> if f c then Just c else Nothing) | ||
|
||
string :: String -> CharParser st String | ||
string s = tokens show updatePosString s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
----------------------------------------------------------------------------- | ||
-- | | ||
-- Module : Text.ParserCombinators.Parsec.Combinator | ||
-- Copyright : (c) Daan Leijen 1999-2001 | ||
-- License : BSD-style (see the file libraries/base/LICENSE) | ||
-- | ||
-- Maintainer : daan@cs.uu.nl | ||
-- Stability : provisional | ||
-- Portability : portable | ||
-- | ||
-- Commonly used generic combinators | ||
-- | ||
----------------------------------------------------------------------------- | ||
|
||
module Text.ParserCombinators.Parsec.Combinator | ||
( choice | ||
, count | ||
, between | ||
, option, 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.ParserCombinators.Parsec.Prim | ||
|
||
|
||
---------------------------------------------------------------- | ||
-- | ||
---------------------------------------------------------------- | ||
choice :: [GenParser tok st a] -> GenParser tok st a | ||
choice ps = foldr (<|>) mzero ps | ||
|
||
option :: a -> GenParser tok st a -> GenParser tok st a | ||
option x p = p <|> return x | ||
|
||
optional :: GenParser tok st a -> GenParser tok st () | ||
optional p = do{ p; return ()} <|> return () | ||
|
||
between :: GenParser tok st open -> GenParser tok st close | ||
-> GenParser tok st a -> GenParser tok st a | ||
between open close p | ||
= do{ open; x <- p; close; return x } | ||
|
||
|
||
skipMany1 :: GenParser tok st a -> GenParser tok st () | ||
skipMany1 p = do{ p; skipMany p } | ||
{- | ||
skipMany p = scan | ||
where | ||
scan = do{ p; scan } <|> return () | ||
-} | ||
|
||
many1 :: GenParser tok st a -> GenParser tok st [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 :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a] | ||
sepBy p sep = sepBy1 p sep <|> return [] | ||
sepBy1 p sep = do{ x <- p | ||
; xs <- many (sep >> p) | ||
; return (x:xs) | ||
} | ||
|
||
sepEndBy1, sepEndBy :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [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 :: GenParser tok st a -> GenParser tok st sep -> GenParser tok st [a] | ||
endBy1 p sep = many1 (do{ x <- p; sep; return x }) | ||
endBy p sep = many (do{ x <- p; sep; return x }) | ||
|
||
count :: Int -> GenParser tok st a -> GenParser tok st [a] | ||
count n p | n <= 0 = return [] | ||
| otherwise = sequence (replicate n p) | ||
|
||
|
||
chainr p op x = chainr1 p op <|> return x | ||
chainl p op x = chainl1 p op <|> return x | ||
|
||
chainr1,chainl1 :: GenParser tok st a -> GenParser tok st (a -> a -> a) -> GenParser tok st 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 :: Show tok => GenParser tok st tok | ||
anyToken = tokenPrim show (\pos tok toks -> pos) Just | ||
|
||
eof :: Show tok => GenParser tok st () | ||
eof = notFollowedBy anyToken <?> "end of input" | ||
|
||
notFollowedBy :: Show tok => GenParser tok st tok -> GenParser tok st () | ||
notFollowedBy p = try (do{ c <- p; unexpected (show [c]) } | ||
<|> return () | ||
) | ||
|
||
manyTill :: GenParser tok st a -> GenParser tok st end -> GenParser tok st [a] | ||
manyTill p end = scan | ||
where | ||
scan = do{ end; return [] } | ||
<|> | ||
do{ x <- p; xs <- scan; return (x:xs) } | ||
|
||
|
||
lookAhead :: GenParser tok st a -> GenParser tok st a | ||
lookAhead p = do{ state <- getParserState | ||
; x <- p | ||
; setParserState state | ||
; return x | ||
} |
Oops, something went wrong.