-----------------------------------------------------------------------------
-- |
-- Module      :  Language.Haskell.Exts.Lexer
-- Copyright   :  (c) The GHC Team, 1997-2000
--                (c) Niklas Broberg, 2004-2012
-- License     :  BSD-style (see the file LICENSE.txt)
--
-- Maintainer  :  Niklas Broberg, niklas.broberg@chalmers.se
-- Stability   :  stable
-- Portability :  portable
--
-- Lexer for Haskell with extensions.
--
-----------------------------------------------------------------------------

module Language.Haskell.Exts.Lexer
    (
      lexTokenStream, lexTokenStreamWithMode,

      Token(..), Loc(..),

      showToken

    ) where

import Language.Haskell.Exts.InternalLexer
import Language.Haskell.Exts.ParseMonad
import Language.Haskell.Exts.SrcLoc

-- | Lex a string into a list of Haskell 2010 source tokens.
lexTokenStream :: String -> ParseResult [Loc Token]
lexTokenStream :: String -> ParseResult [Loc Token]
lexTokenStream = ParseMode -> String -> ParseResult [Loc Token]
lexTokenStreamWithMode ParseMode
defaultParseMode

-- | Lex a string into a list of Haskell source tokens, using an explicit mode.
lexTokenStreamWithMode :: ParseMode -> String -> ParseResult [Loc Token]
lexTokenStreamWithMode :: ParseMode -> String -> ParseResult [Loc Token]
lexTokenStreamWithMode ParseMode
mode = ParseMode -> P [Loc Token] -> String -> ParseResult [Loc Token]
forall a. ParseMode -> P a -> String -> ParseResult a
runParserWithMode ParseMode
mode P [Loc Token]
lexIt
  where lexIt :: P [Loc Token]
        lexIt :: P [Loc Token]
lexIt = Lex [Loc Token] [Loc Token]
-> ([Loc Token] -> P [Loc Token]) -> P [Loc Token]
forall r a. Lex r a -> (a -> P r) -> P r
runL Lex [Loc Token] [Loc Token]
go [Loc Token] -> P [Loc Token]
forall (m :: * -> *) a. Monad m => a -> m a
return
        go :: Lex [Loc Token] [Loc Token]
        go :: Lex [Loc Token] [Loc Token]
go = do
          Loc Token
ltok <- Lex [Loc Token] (Loc Token)
forall a. Lex a (Loc Token)
topLexer
          case Loc Token
ltok of
            Loc SrcSpan
_ Token
EOF -> [Loc Token] -> Lex [Loc Token] [Loc Token]
forall (m :: * -> *) a. Monad m => a -> m a
return []
            Loc Token
_ -> do [Loc Token]
ts <- Lex [Loc Token] [Loc Token]
go
                    [Loc Token] -> Lex [Loc Token] [Loc Token]
forall (m :: * -> *) a. Monad m => a -> m a
return (Loc Token
ltokLoc Token -> [Loc Token] -> [Loc Token]
forall a. a -> [a] -> [a]
:[Loc Token]
ts)