KEMBAR78
Parser combinator in swift | PDF
Combinators
Can you compose?
Agenda
• Given a character parser can you make
JSON parser?
Character parser
• (String) -> (Character, String)

• Parser consumes the parsed character from
the string
HELLO HParser ELLOH
hParser
feeding in the input string. Pipe operator. Input is consumed
Generalising
• Parser struct 

• To add typed functions
Parser Struct Input
Parser Expectation Remaining Input
Lets glue!
Parsing 2 chars
Parsed chars Remaining Input
InputComposition
Parse a Boolean
AND
pChar pChar
andThenedParser
tChar AND uChar
’t’ ‘u’
(’t’, ‘u’)
1. andThen
Can we avoid this repetition?
Pair
true Parser
true/false Parser
orElse
T TorElse T
T UorElse T
orElse
digit Parser
Wouldn’t it be great to have Parser<Int>
1. Should Parser be functor?
YES
Why?
Parser<(Char, Char)>
to Parser<[Char]>
Parser<Character>
to Parser<Int> Explain
more?
We need to map
Parser<T> -> Parser<U>
Okay things
have to be
mappable?
Thats what a functor is!
Yo! Go on!
Whatever!
Functor
1. andThen ->>-
1,something
keepLeft
parse1 parseComma 1 something
keepLeft ->>
.123
keepRight
parseDot parseDigit 1 23
keepRight >>-
Many
123something parseDigit
many
something[‘1’,’2’, ‘3’]
something parseDigit something[]
Many1
something
many1
parseDigit Failure Expected 1 or many digit
DEMO
Expression blow up

Parser combinator in swift