packages feed

bibdb-0.4.2: src/Parser/Token.hs

-----------------------------------------------------------------------------
-- |
-- Module      : Parser.Token
-- Description : Tokens generated by the lexer
-- Maintainer  : coskuacay@gmail.com
-- Stability   : experimental
-----------------------------------------------------------------------------
module Parser.Token
  ( Token (..)
  , Lexeme (..)
  , token
  ) where

import Parser.Location (SrcSpan, Located(..))

import Text.PrettyPrint
import Text.PrettyPrint.HughesPJClass (Pretty (..), prettyShow)


data Token =
    TType String
  | TIdent String
  | TColon
  | TAs
  | TEof
  deriving (Eq, Ord)


data Lexeme = Lexeme SrcSpan Token

token :: Lexeme -> Token
token (Lexeme _ t) = t

instance Located Lexeme where
  location (Lexeme s _) = s


----------------------------------------------------------------------------
-- * Printing
----------------------------------------------------------------------------

instance Pretty Token where
  pPrint (TType t) = text "type" <> parens (text t)
  pPrint (TIdent id) = text "identifier" <> parens (text id)
  pPrint TColon = text ":"
  pPrint TAs = text "as"
  pPrint TEof = text "<EOF>"


instance Pretty Lexeme where
  pPrint (Lexeme loc token) = pPrint token <+> text "at" <+> pPrint loc


----------------------------------------------------------------------------
-- * Showing
----------------------------------------------------------------------------

instance Show Token where
  show = prettyShow

instance Show Lexeme where
  show = prettyShow