Skip to content

Commit

Permalink
Add parser example for my blog 馃挻
Browse files Browse the repository at this point in the history
  • Loading branch information
kutyel committed Apr 2, 2024
1 parent 6341aa6 commit bde30da
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Codewars/Kata/Parsers.hs
@@ -0,0 +1,27 @@
module Codewars.Kata.Parsers where

import Control.Applicative ((<|>))
import Text.Trifecta (Parser, char, runParser, stringLiteral, symbol)

data Import
= Idl String
| Protocol String
| Schema String
deriving (Eq, Show, Ord)

parser :: Parser Import
parser =
symbol "import"
*> ( importHelper Idl "idl"
<|> importHelper Protocol "protocol"
<|> importHelper Schema "schema"
)
where
importHelper :: (String -> Import) -> String -> Parser Import
importHelper f s = f <$> (symbol s *> stringLiteral <* char ';')

main :: IO ()
main = do
print $ runParser parser mempty "import protocol \"foo.avpr\";"

-- > Success (ProtocolImport "foo.avpr")
2 changes: 2 additions & 0 deletions haskell-kata.cabal
Expand Up @@ -17,12 +17,14 @@ library
Codewars.Kata.FactorialComposition
Codewars.Kata.MissingElement
Codewars.Kata.MultiplesOf3And5
Codewars.Kata.Parsers

build-depends:
base
, containers
, megaparsec
, mtl
, split
, trifecta

default-language: Haskell2010

0 comments on commit bde30da

Please sign in to comment.