Skip to content

Instantly share code, notes, and snippets.

@gengar
Created September 22, 2015 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gengar/77625fe9a5d8200abc76 to your computer and use it in GitHub Desktop.
Save gengar/77625fe9a5d8200abc76 to your computer and use it in GitHub Desktop.
{-# LANGUAGE FlexibleContexts #-}
module Lexer ( tokenize, token, tokenEq, tokenChar, tokenString
, BaseToken, BaseLexer, BaseParser
) where
import Control.Applicative
import Control.Monad.Identity
import Text.Parsec hiding (token)
import qualified Text.Parsec as Parsec (token)
type BaseToken tok = (SourcePos, tok)
type BaseLexer p tok = p tok
type BaseParser tok a = Parsec [tok] () a
tokenize :: (Monad m) => ParsecT s u m tok -> ParsecT s u m (BaseToken tok)
tokenize = ((,) <$> getPosition <*>)
token :: (Show t, Stream s Identity (BaseToken t)) => (t -> Maybe a) -> Parsec s u a
token match = Parsec.token (show . snd) fst (match . snd)
tokenEq :: (Eq t, Show t, Stream s Identity (BaseToken t)) => t -> Parsec s u t
tokenEq t = token match
where
match t1 | t1 == t = Just t1
| otherwise = Nothing
tokenChar :: (Stream s m Char) => a -> Char -> ParsecT s u m a
tokenChar tok c = char c >> return tok
tokenString :: (Stream s m Char) => a -> String -> ParsecT s u m a
tokenString tok s = string s >> return tok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment