mirror of
https://github.com/mrkkrp/megaparsec.git
synced 2024-12-18 22:01:41 +03:00
50 lines
1.5 KiB
Haskell
50 lines
1.5 KiB
Haskell
-- |
|
|
-- Module : Text.Megaparsec.ShowToken
|
|
-- Copyright : © 2015 Megaparsec contributors
|
|
-- License : BSD3
|
|
--
|
|
-- Maintainer : Mark Karpov <markkarpov@opmbx.org>
|
|
-- Stability : experimental
|
|
-- Portability : portable
|
|
--
|
|
-- Pretty printing function and instances for use in error messages.
|
|
|
|
module Text.Megaparsec.ShowToken (ShowToken (..)) where
|
|
|
|
-- | Typeclass 'ShowToken' defines single function 'showToken' that can be
|
|
-- used to “pretty-print” various tokens. By default, all commonly used
|
|
-- instances are defined, but you can add your own, of course.
|
|
|
|
class Show a => ShowToken a where
|
|
showToken :: a -> String
|
|
|
|
instance ShowToken Char where
|
|
showToken = prettyChar
|
|
|
|
-- | @prettyChar ch@ returns user-friendly string representation of given
|
|
-- character @ch@, suitable for using in error messages, for example.
|
|
|
|
prettyChar :: Char -> String
|
|
prettyChar '\0' = "null"
|
|
prettyChar '\a' = "bell"
|
|
prettyChar '\b' = "backspace"
|
|
prettyChar '\t' = "tab"
|
|
prettyChar '\n' = "newline"
|
|
prettyChar '\v' = "vertical tab"
|
|
prettyChar '\f' = "form feed"
|
|
prettyChar '\r' = "carriage return"
|
|
prettyChar ' ' = "space"
|
|
prettyChar x = "'" ++ [x] ++ "'"
|
|
|
|
instance ShowToken String where
|
|
showToken = prettyString
|
|
|
|
-- | @prettyString s@ returns pretty representation of string @s@. This is
|
|
-- used when printing string tokens in error messages.
|
|
|
|
prettyString :: String -> String
|
|
prettyString "" = ""
|
|
prettyString [x] = prettyChar x
|
|
prettyString "\r\n" = "crlf newline"
|
|
prettyString xs = "\"" ++ xs ++ "\""
|