D E F I N I N G P R I M I T I V E P A R S E R S P a r s e r t y p e type Parser a = String -> Maybe (a, String) A parser of type 'a' takes an input 'String' and attempts to find a string representation of something of type 'a' at the beginning of the 'String'. If it succeeds it returns Just(v, rest), where v is the value of type 'a' found and rest is the remainder of the input string after the representation of v is removed. If it fails it returns Nothing. l i t e r a l lit :: Char -> Parser Char lit c takes a literal character 'c' as a parameter and returns a parser that attempts to find 'c' as the first character of its input string. example: parseOpenParen2 = lit '(' parseOpenParen2 is now a Parser Char that succeeds if '(' is the first character of its input string. C O M P O S I N G P A R S E R S s e q u e n c e The sequence operation (#) takes two parsers and creates a new parser that recognizes whether a string starts with a substring recognized by the first followed by a substring recognized by the second. The newly-created parser returns an ordered pair of parsed values if it succeeds. (#) :: Parser a -> Parser b -> Parser (a,b) example: parseOC2 = lit '(' # lit ')' parseOC2 is a 'Parser (Char,Char)' that succeeds if the first two characters of the input string are '(' and ')'. s e q u e n c e a n d s e l e c t o n e r e s u l t The operations (-#) and (#-) take two parsers create a new parser that performs as (#), but then instead of returning a pair of parses returns only one and throws the other one away. If the '-' occurs in the first position, ignore the value parsed by the first parser. If the '-' occurs in the second position, ignore the value parsed by the second parser. (-#) :: Parser a -> Parser b -> Parser b (#-) :: Parser a -> Parser b -> Parser a example: parseParen2 = lit '(' -# lit ')' parseParen2 ('(' : ')': rest) (where rest :: String) returns Just (')', rest) c o n s Takes two parsers, one which returns a value and the second of which returns a list of values, and returns a parser that conses their results into one list. (#:) :: Parser a -> Parser [a] -> Parser [a] example: takeParse :: Parser a -> Int - Parser [a] takeParse m 0 = returnParse [] takeParse m i = m #: takeParse m (i-1) a p p e n d Takes two parsers, both of which find return a list of values, and returns a parser that concatenates their results into one list. (#++) :: Parser [a] -> Parser [a] -> Parser [a] example: If parseDigits parses a maximal contiguous sequence of digits, then parseDigits #++ ('.' #: parseDigits) parses a number with a decimal point, e.g. "3.14" o r Takes two parsers and returns a parser that succeeds if either parser succeeds. If both parsers succeed, then returns the result of the first. (!) :: Parser a -> Parser a -> Parser a example: parseParen = lit '(' ! lit ')' Succeeds if the first character is either a '(' or a ')'. t r a n s f o r m e r Returns a parser that transforms the output of another parser by applying a function. (>->) :: Parser a -> (a->b) -> Parser b example: m # n >-> snd Applies snd to the ordered pair returned by m # n, returning the second value. b o o l e a n t e s t Returns a parser that parses using m, but only succeeds if m succeeds and p is true. (?) :: Parser a -> (a -> Bool) -> Parser a example: let (a->Bool) = isAlpha. letter2 = (char ? isAlpha) letter2 is now a 'Parser Char'. r e p e t i t i o n s takeParse :: Parser a -> Int -> Parser [a] takeParse p n creates a parser that applies parser p to the input string n times. That newly-created parser returns a list of the n values if it succeeds. takeWhileParse :: Parser a -> Parser [a] takeWhileParse p creates a parser that applies parser p to the input string as long as it continues to succeed. That newly-created parser returns a list of the successful parses. Could return [] if p fails the first time. takeRepeatParse :: Parser a -> Parser [a] takeRepeatParse p creates a parser that applies parser p to the input string at least once, repeating as long as p continues to succeed. The newly-created parser returns a list of the successful parses. Fails if the first parse fails. W o r d S e l e c t i o n parseWord :: Parser String parseWord returns the next word (sequence of alphabetic characters), eliminating spaces before and after word :: String -> Parser String word w extracts a single word (eliminating surrounding whitespace), but fails if the extracted word is not w. wordChoice :: [String] -> Parser String wordchoice wordlist extracts a single word (eliminating surrounding whitespace), but fails if the extracted word is not in the list wordlist. F i x e d - v a l u e p a r s e r s returnParse :: a -> Parser a returnParse v returns a parser that always succeeds, returning the fixed value v. failParse :: Parser a failParse is a parser that always fails