Skip to content

Commit

Permalink
add parser variations
Browse files Browse the repository at this point in the history
  • Loading branch information
tanakh committed Sep 25, 2011
1 parent 620c9f5 commit c57a736
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Text/Peggy/Prim.hs
Expand Up @@ -8,8 +8,11 @@ module Text.Peggy.Prim (
ParseError(..),
MemoTable(..),

memo,
parse,
parseString,
parseFile,

memo,

getPos,
setPos,
Expand Down Expand Up @@ -50,6 +53,7 @@ instance Error ParseError
nullError :: ParseError
nullError = ParseError (LocPos $ SrcPos "" 0 1 1) ""

errMerge :: ParseError -> ParseError -> ParseError
errMerge e1@(ParseError loc1 msg1) e2@(ParseError loc2 msg2)
| loc1 >= loc2 = e1
| otherwise = e2
Expand Down Expand Up @@ -116,6 +120,21 @@ parse p pos str = runST $ do
Parsed _ _ ret -> return $ Right ret
Failed err -> return $ Left err

parseString :: MemoTable tbl
=> (forall s . Parser tbl str s a)
-> String
-> str
-> Either ParseError a
parseString p inputName str =
parse p (SrcPos inputName 0 1 1) str

parseFile :: MemoTable tbl
=> (forall s . Parser tbl String s a)
-> FilePath
-> IO (Either ParseError a)
parseFile p fp =
parse p (SrcPos fp 0 1 1) <$> readFile fp

getPos :: Parser tbl str s SrcPos
getPos = Parser $ \_ pos str -> return $ Parsed pos str pos

Expand Down

0 comments on commit c57a736

Please sign in to comment.