{-|
Module      : Toml.Lexer.Utils
Description : Wrapper and actions for generated lexer
Copyright   : (c) Eric Mertens, 2023
License     : ISC
Maintainer  : emertens@gmail.com

This module provides a custom engine for the Alex generated
lexer. This lexer drive provides nested states, unicode support,
and file location tracking.

The various states of this module are needed to deal with the varying
lexing rules while lexing values, keys, and string-literals.

-}
module Toml.Lexer.Utils (

    -- * Types
    Action,
    Context(..),
    Outcome(..),

    -- * Input processing
    locatedUncons,

    -- * Actions
    token,
    token_,

    timeValue,
    eofToken,

    failure,

    -- * String literals
    strFrag,
    startMlBstr,
    startBstr,
    startMlLstr,
    startLstr,
    endStr,
    unicodeEscape,
    recommendEscape,

    mkError,
    ) where

import Data.Char (ord, chr, isAscii, isControl)
import Data.Foldable (asum)
import Data.Time.Format (parseTimeM, defaultTimeLocale, ParseTime)
import Numeric (readHex)
import Text.Printf (printf)
import Toml.Lexer.Token (Token(..))
import Toml.Located (Located(..))
import Toml.Position (move, Position)

-- | Type of actions associated with lexer patterns
type Action = Located String -> Context -> Outcome

data Outcome
  = Resume Context
  | LexerError (Located String)
  | EmitToken (Located Token)

-- | Representation of the current lexer state.
data Context
  = TopContext -- ^ top-level where @[[@ and @]]@ have special meaning
  | TableContext -- ^ inline table - lex key names
  | ValueContext -- ^ value lexer - lex number literals
  | MlBstrContext Position [String] -- ^ multiline basic string: position of opening delimiter and list of fragments
  | BstrContext   Position [String] -- ^ basic string: position of opening delimiter and list of fragments
  | MlLstrContext Position [String] -- ^ multiline literal string: position of opening delimiter and list of fragments
  | LstrContext   Position [String] -- ^ literal string: position of opening delimiter and list of fragments
  deriving Int -> Context -> ShowS
[Context] -> ShowS
Context -> String
(Int -> Context -> ShowS)
-> (Context -> String) -> ([Context] -> ShowS) -> Show Context
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Context -> ShowS
showsPrec :: Int -> Context -> ShowS
$cshow :: Context -> String
show :: Context -> String
$cshowList :: [Context] -> ShowS
showList :: [Context] -> ShowS
Show

-- | Add a literal fragment of a string to the current string state.
strFrag :: Action
strFrag :: Action
strFrag (Located Position
_ String
s) = \case
  BstrContext   Position
p [String]
acc -> Context -> Outcome
Resume (Position -> [String] -> Context
BstrContext   Position
p (String
s String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
acc))
  MlBstrContext Position
p [String]
acc -> Context -> Outcome
Resume (Position -> [String] -> Context
MlBstrContext Position
p (String
s String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
acc))
  LstrContext   Position
p [String]
acc -> Context -> Outcome
Resume (Position -> [String] -> Context
LstrContext   Position
p (String
s String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
acc))
  MlLstrContext Position
p [String]
acc -> Context -> Outcome
Resume (Position -> [String] -> Context
MlLstrContext Position
p (String
s String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
acc))
  Context
_                   -> String -> Outcome
forall a. HasCallStack => String -> a
error String
"strFrag: panic"

-- | End the current string state and emit the string literal token.
endStr :: Action
endStr :: Action
endStr (Located Position
_ String
x) = \case
    BstrContext   Position
p [String]
acc -> Located Token -> Outcome
EmitToken (Position -> Token -> Located Token
forall a. Position -> a -> Located a
Located Position
p (String -> Token
TokString   ([String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> [String]
forall a. [a] -> [a]
reverse (String
x String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
acc)))))
    MlBstrContext Position
p [String]
acc -> Located Token -> Outcome
EmitToken (Position -> Token -> Located Token
forall a. Position -> a -> Located a
Located Position
p (String -> Token
TokMlString ([String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> [String]
forall a. [a] -> [a]
reverse (String
x String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
acc)))))
    LstrContext   Position
p [String]
acc -> Located Token -> Outcome
EmitToken (Position -> Token -> Located Token
forall a. Position -> a -> Located a
Located Position
p (String -> Token
TokString   ([String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> [String]
forall a. [a] -> [a]
reverse (String
x String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
acc)))))
    MlLstrContext Position
p [String]
acc -> Located Token -> Outcome
EmitToken (Position -> Token -> Located Token
forall a. Position -> a -> Located a
Located Position
p (String -> Token
TokMlString ([String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([String] -> [String]
forall a. [a] -> [a]
reverse (String
x String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
acc)))))
    Context
_                  -> String -> Outcome
forall a. HasCallStack => String -> a
error String
"endStr: panic"

-- | Start a basic string literal
startBstr :: Action
startBstr :: Action
startBstr (Located Position
p String
_) Context
_ = Context -> Outcome
Resume (Position -> [String] -> Context
BstrContext Position
p [])

-- | Start a literal string literal
startLstr :: Action
startLstr :: Action
startLstr (Located Position
p String
_) Context
_ = Context -> Outcome
Resume (Position -> [String] -> Context
LstrContext Position
p [])

-- | Start a multi-line basic string literal
startMlBstr :: Action
startMlBstr :: Action
startMlBstr (Located Position
p String
_) Context
_ = Context -> Outcome
Resume (Position -> [String] -> Context
MlBstrContext Position
p [])

-- | Start a multi-line literal string literal
startMlLstr :: Action
startMlLstr :: Action
startMlLstr (Located Position
p String
_) Context
_ = Context -> Outcome
Resume (Position -> [String] -> Context
MlLstrContext Position
p [])

-- | Resolve a unicode escape sequence and add it to the current string literal
unicodeEscape :: Action
unicodeEscape :: Action
unicodeEscape (Located Position
p String
lexeme) Context
ctx =
  case ReadS Int
forall a. (Eq a, Num a) => ReadS a
readHex (Int -> ShowS
forall a. Int -> [a] -> [a]
drop Int
2 String
lexeme) of
    [(Int
n,String
_)] | Int
0xd800 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
n, Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0xe000 -> Located String -> Outcome
LexerError (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p String
"non-scalar unicode escape")
      | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0x110000                 -> Located String -> Outcome
LexerError (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p String
"unicode escape too large")
      | Bool
otherwise                     -> Action
strFrag (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p [Int -> Char
chr Int
n]) Context
ctx
    [(Int, String)]
_                                 -> String -> Outcome
forall a. HasCallStack => String -> a
error String
"unicodeEscape: panic"

recommendEscape :: Action
recommendEscape :: Action
recommendEscape (Located Position
p String
x) Context
_ =
  Located String -> Outcome
LexerError (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p (String -> Int -> String
forall r. PrintfType r => String -> r
printf String
"control characters must be escaped, use: \\u%04X" (Char -> Int
ord (String -> Char
forall a. HasCallStack => [a] -> a
head String
x))))

-- | Emit a token ignoring the current lexeme
token_ :: Token -> Action
token_ :: Token -> Action
token_ Token
t Located String
x Context
_ = Located Token -> Outcome
EmitToken (Token
t Token -> Located String -> Located Token
forall a b. a -> Located b -> Located a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Located String
x)

-- | Emit a token using the current lexeme
token :: (String -> Token) -> Action
token :: (String -> Token) -> Action
token String -> Token
f Located String
x Context
_ = Located Token -> Outcome
EmitToken (String -> Token
f (String -> Token) -> Located String -> Located Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Located String
x)

-- | Attempt to parse the current lexeme as a date-time token.
timeValue ::
  ParseTime a =>
  String       {- ^ description for error messages -} ->
  [String]     {- ^ possible valid patterns        -} ->
  (a -> Token) {- ^ token constructor              -} ->
  Action
timeValue :: forall a.
ParseTime a =>
String -> [String] -> (a -> Token) -> Action
timeValue String
description [String]
patterns a -> Token
constructor (Located Position
p String
str) Context
_ =
  case [Maybe a] -> Maybe a
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [Bool -> TimeLocale -> String -> String -> Maybe a
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
False TimeLocale
defaultTimeLocale String
pat String
str | String
pat <- [String]
patterns] of
    Maybe a
Nothing -> Located String -> Outcome
LexerError (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p (String
"malformed " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
description))
    Just a
t  -> Located Token -> Outcome
EmitToken (Position -> Token -> Located Token
forall a. Position -> a -> Located a
Located Position
p (a -> Token
constructor a
t))

-- | Pop the first character off a located string if it's not empty.
-- The resulting 'Int' will either be the ASCII value of the character
-- or @1@ for non-ASCII Unicode values. To avoid a clash, @\x1@ is
-- remapped to @0@.
locatedUncons :: Located String -> Maybe (Int, Located String)
locatedUncons :: Located String -> Maybe (Int, Located String)
locatedUncons Located { locPosition :: forall a. Located a -> Position
locPosition = Position
p, locThing :: forall a. Located a -> a
locThing = String
str } =
  case String
str of
    String
"" -> Maybe (Int, Located String)
forall a. Maybe a
Nothing
    Char
x:String
xs
      | Located String
rest Located String -> Bool -> Bool
forall a b. a -> b -> b
`seq` Bool
False -> Maybe (Int, Located String)
forall a. HasCallStack => a
undefined
      | Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\1' -> (Int, Located String) -> Maybe (Int, Located String)
forall a. a -> Maybe a
Just (Int
0,     Located String
rest)
      | Char -> Bool
isAscii Char
x -> (Int, Located String) -> Maybe (Int, Located String)
forall a. a -> Maybe a
Just (Char -> Int
ord Char
x, Located String
rest)
      | Bool
otherwise -> (Int, Located String) -> Maybe (Int, Located String)
forall a. a -> Maybe a
Just (Int
1,     Located String
rest)
      where
        rest :: Located String
rest = Located { locPosition :: Position
locPosition = Char -> Position -> Position
move Char
x Position
p, locThing :: String
locThing = String
xs }

-- | Generate the correct terminating token given the current lexer state.
eofToken :: Context -> Located String -> Either (Located String) (Located Token, Located String)
eofToken :: Context
-> Located String
-> Either (Located String) (Located Token, Located String)
eofToken (MlBstrContext Position
p [String]
_) Located String
_ = Located String
-> Either (Located String) (Located Token, Located String)
forall a b. a -> Either a b
Left (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p String
"unterminated multi-line basic string")
eofToken (BstrContext   Position
p [String]
_) Located String
_ = Located String
-> Either (Located String) (Located Token, Located String)
forall a b. a -> Either a b
Left (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p String
"unterminated basic string")
eofToken (MlLstrContext Position
p [String]
_) Located String
_ = Located String
-> Either (Located String) (Located Token, Located String)
forall a b. a -> Either a b
Left (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p String
"unterminated multi-line literal string")
eofToken (LstrContext   Position
p [String]
_) Located String
_ = Located String
-> Either (Located String) (Located Token, Located String)
forall a b. a -> Either a b
Left (Position -> String -> Located String
forall a. Position -> a -> Located a
Located Position
p String
"unterminated literal string")
eofToken Context
_                  Located String
t = (Located Token, Located String)
-> Either (Located String) (Located Token, Located String)
forall a b. b -> Either a b
Right (Token
TokEOF Token -> Located String -> Located Token
forall a b. a -> Located b -> Located a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Located String
t, Located String
t)

failure :: String -> Action
failure :: String -> Action
failure String
err Located String
t Context
_ = Located String -> Outcome
LexerError (String
err String -> Located String -> Located String
forall a b. a -> Located b -> Located a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Located String
t)

-- | Generate an error message given the current string being lexed.
mkError :: String -> String
mkError :: ShowS
mkError String
""    = String
"unexpected end-of-input"
mkError (Char
'\n':String
_) = String
"unexpected end-of-line"
mkError (Char
'\r':Char
'\n':String
_) = String
"unexpected end-of-line"
mkError (Char
x:String
_)
    | Char -> Bool
isControl Char
x = String
"control characters prohibited"
    | Bool
otherwise   = String
"unexpected " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Char -> String
forall a. Show a => a -> String
show Char
x