{---- Monadic parser combinators which incorporate lexing. ----} module Lexing where import Control.Monad.State import MonParser -- -- Combinators for parsing a language -- spaces :: Parser () spaces = do many1 (sat isSpace); return () where isSpace x = x == ' ' || x == '\n' || x == '\t' comment :: Parser () comment = do string "--"; many' (sat (/= '\n')); return () bigComment :: Parser () bigComment = do bracket (string "{-") (many' (bigComment +++ notEndComment)) (string "-}") return () where notEndComment = do sat (/= '-') +++ (do char '-'; sat (/= '}')); return () junk :: Parser () junk = do many' (spaces +++ comment +++ bigComment); return () parse :: Parser a -> Parser a parse p = do junk; p token :: Parser a -> Parser a token p = do v <- p; junk; return v natural :: Parser Int natural = token nat integer :: Parser Int integer = token int symbol :: String -> Parser String symbol x = token (string x) identifier :: [String] -> Parser String identifier ks = token (do x <- first ident; if not (elem x ks) then return x else mzero)