Commit 0258c1d9 authored by Camil Staps's avatar Camil Staps 🚀

Merge branch 'export-parser-type' into 'master'

export parser type so things can be derived for it

See merge request !324
parents 631806b9 f3f6a6e7
Pipeline #41400 passed with stage
in 1 minute and 47 seconds
......@@ -18,7 +18,7 @@ from Text import class Text(concat), instance Text String
import Data.Functor
import Control.Applicative
import Control.Monad
from Text.Parsers.Simple.Core import :: Parser, :: Error,
from Text.Parsers.Simple.Core import :: Parser,
instance Functor (Parser t), instance pure (Parser t),
instance <*> (Parser t), instance Alternative (Parser t),
instance Monad (Parser t), pToken, pSepBy, pSepBy1, pList, pSatisfy, pPeek,
......
......@@ -4,7 +4,6 @@ from StdOverloaded import class fromInt, class toInt, class toString, class from
from Data.Either import :: Either
from Data.Maybe import :: Maybe
from Text.Parsers.Simple.Core import :: Error
:: IRCMessage =
{ irc_prefix :: Maybe (Either IRCUser String)
......@@ -22,7 +21,7 @@ from Text.Parsers.Simple.Core import :: Error
, irc_host :: Maybe String
}
parseIRCMessage :: String -> Either [Error] IRCMessage
parseIRCMessage :: String -> Either [String] IRCMessage
instance toString IRCCommand, IRCReplies, IRCErrors, IRCMessage, IRCUser, IRCNumReply
instance fromInt IRCReplies, IRCErrors
......
......@@ -16,7 +16,7 @@ import Text.Parsers.Simple.Core
derive gPrint IRCErrors, IRCReplies, Maybe, Either, IRCUser, IRCNumReply
parseIRCMessage :: String -> Either [Error] IRCMessage
parseIRCMessage :: String -> Either [String] IRCMessage
parseIRCMessage s = case runParser parsePrefix (fromString s) of
// Prefix is parsed
([(prefix, rest):_], _)
......@@ -61,7 +61,7 @@ where
parseName = toString <$> pSome (pAlpha <|> pDigit <|> pOneOf ['-', '/'])
//Parse Cmd
parseCmd :: [Char] -> Either Error IRCCommand
parseCmd :: [Char] -> Either String IRCCommand
parseCmd cs = fst $ gIRCParse{|*|} $ argfun $ split " " $ toString cs
where
argfun :: [String] -> [String]
......@@ -408,7 +408,7 @@ derive gIRCPrint IRCCommand, (,)
pOne [] = (Left "Expected an argument", [])
pOne [a:as] = (Right a, as)
generic gIRCParse a :: [String] -> (Either Error a, [String])
generic gIRCParse a :: [String] -> (Either String a, [String])
gIRCParse{|UNIT|} a = (Right UNIT, a)
gIRCParse{|String|} as = pOne as
gIRCParse{|Int|} as = appFst (fmap toInt) $ pOne as
......
......@@ -6,8 +6,7 @@ from Data.Either import :: Either
from Data.Functor import class Functor
from StdOverloaded import class ==
:: Error :== String
:: Parser t a
:: Parser t a =: Parser ([t] -> ([(a, [t])], [String]))
// AMF instances
instance Functor (Parser t)
......@@ -20,18 +19,18 @@ instance Monad (Parser t)
instance MonadPlus (Parser t)
// Functions to run the parser
parse :: !(Parser t a) [t] -> Either [Error] a
runParser :: !(Parser t a) [t] -> ([(a, [t])], [Error])
parse :: !(Parser t a) [t] -> Either [String] a
runParser :: !(Parser t a) [t] -> ([(a, [t])], [String])
// Core combinators
pFail :: Parser t a
pYield :: a -> Parser t a
pSatisfy :: (t -> Bool) -> Parser t t
pError :: Error -> Parser t a
pError :: String -> Parser t a
pPeek :: Parser t [t]
// Convenience parsers
(@!) infixr 4 :: (Parser t a) Error -> Parser t a
(@!) infixr 4 :: (Parser t a) String -> Parser t a
(<<|>) infixr 4 :: (Parser t a) (Parser t a) -> Parser t a
(<|>>) infixr 4 :: (Parser t a) (Parser t a) -> Parser t a
......
......@@ -9,9 +9,6 @@ import Data.Func
import Data.Functor
import Data.List
:: PCont t a :== [t] -> ([(a, [t])], [Error])
:: Parser t a = Parser (PCont t a)
instance Functor (Parser t) where
fmap f p = pMap f p
......@@ -37,7 +34,7 @@ instance MonadPlus (Parser t) where
mzero = pFail
mplus l r = l <|> r
parse :: !(Parser t a) [t] -> Either [Error] a
parse :: !(Parser t a) [t] -> Either [String] a
parse p ts
= case runParser p ts of
(rs, []) -> case [r \\ (r, []) <- rs] of
......@@ -45,16 +42,16 @@ parse p ts
_ -> Left ["No parse results with a fully consumed input."]
(_, es) -> Left es
runParser :: !(Parser t a) [t] -> ([(a, [t])], [Error])
runParser :: !(Parser t a) [t] -> ([(a, [t])], [String])
runParser (Parser f) xs = f xs
pFail :: Parser t a
pFail = Parser (\_ -> ([], []))
pError :: Error -> Parser t a
pError :: String -> Parser t a
pError err = Parser (\_ -> ([], [err]))
(@!) infixr 4 :: (Parser t a) Error -> Parser t a
(@!) infixr 4 :: (Parser t a) String -> Parser t a
(@!) p err = p <<|> pError err
pYield :: a -> Parser t a
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment