Skip to content

Commit bde30da

Browse files
committed
Add parser example for my blog 💴
1 parent 6341aa6 commit bde30da

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

Codewars/Kata/Parsers.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module Codewars.Kata.Parsers where
2+
3+
import Control.Applicative ((<|>))
4+
import Text.Trifecta (Parser, char, runParser, stringLiteral, symbol)
5+
6+
data Import
7+
= Idl String
8+
| Protocol String
9+
| Schema String
10+
deriving (Eq, Show, Ord)
11+
12+
parser :: Parser Import
13+
parser =
14+
symbol "import"
15+
*> ( importHelper Idl "idl"
16+
<|> importHelper Protocol "protocol"
17+
<|> importHelper Schema "schema"
18+
)
19+
where
20+
importHelper :: (String -> Import) -> String -> Parser Import
21+
importHelper f s = f <$> (symbol s *> stringLiteral <* char ';')
22+
23+
main :: IO ()
24+
main = do
25+
print $ runParser parser mempty "import protocol \"foo.avpr\";"
26+
27+
-- > Success (ProtocolImport "foo.avpr")

haskell-kata.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ library
1717
Codewars.Kata.FactorialComposition
1818
Codewars.Kata.MissingElement
1919
Codewars.Kata.MultiplesOf3And5
20+
Codewars.Kata.Parsers
2021

2122
build-depends:
2223
base
2324
, containers
2425
, megaparsec
2526
, mtl
2627
, split
28+
, trifecta
2729

2830
default-language: Haskell2010

0 commit comments

Comments
 (0)