Skip to content

Commit

Permalink
Update for modern GHC. v0.1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMcCaskey committed Nov 22, 2016
1 parent 8d434d0 commit bfc4f38
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pappy.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: pappy
Version: 0.1.0.2
Version: 0.1.0.3
Synopsis: Packrat parsing; linear-time parsers for grammars in TDPL.
Description: Packrat parsing is a novel and practical
method for implementing linear-time parsers
Expand Down Expand Up @@ -87,4 +87,5 @@ Executable pappy
MemoAnalysis,
WriteParser
Hs-source-dirs: src
ghc-options: -Wall
Build-depends: base >= 4 && < 5
55 changes: 55 additions & 0 deletions src/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,61 @@ infixl 2 </> -- ordered choice
infixl 1 <?> -- error labeling
infixl 1 <?!> -- unconditional error labeling

{-
- I wonder if the implementations of fmap/applicative actually matters if
- they're not used. Either way, this code should be considered unsafe until
- a proper examination and implementation of them is done.
-}

--

instance Derivs d => Functor (Parser d) where

fmap f (Parser p1) = Parser parse

where parse dvs = first (p1 dvs)

first (Parsed val rem err) = Parsed (f val) rem err
-- This probably violates the functor laws (not sure what to do here)
first (NoParse x) = NoParse x

-- Standard Applicative combinators
instance Derivs d => Applicative (Parser d) where

-- Copied from return
pure x = Parser (\dvs -> Parsed x dvs (nullError dvs))

-- Copied from >>=
-- a :: Parser d (a -> b)
-- b :: Parser d a
-- return type should be Parser d b
{-
Not sure if this is correct, but this applies parser p2, then applies parser p1 to its output
and then applies the function obtained from p1's val to p2's val, wrapping it all in a Parsed
value
-}
(Parser p1) <*> (Parser p2) = Parser parse
where parse dvs = case b of
(Parsed val rem err) ->
case a of
(Parsed val' rem' err') -> Parsed (val' val) rem' (joinErrors err err')
(NoParse err') -> NoParse (joinErrors err err')
where a = (p1 rem)
(NoParse err) -> NoParse err
where b = (p2 dvs)

{-
where parse dvs = first (f p1)
first (Parsed val rem err) = Parsed (f val) rem err
first (NoParse err) = NoParse err
second err1 (Parsed val rem err) =
Parsed val rem (joinErrors err1 err)
second err1 (NoParse err) =
NoParse (joinErrors err1 err)
-}


-- Standard monadic combinators
instance Derivs d => Monad (Parser d) where

Expand Down

0 comments on commit bfc4f38

Please sign in to comment.