{-# LANGUAGE FlexibleInstances #-}
{- |
   Module      : Text.Pandoc.Readers.LaTeX.Types
   Copyright   : Copyright (C) 2017-2021 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Types for LaTeX tokens and macros.
-}
module Text.Pandoc.Readers.LaTeX.Types ( Tok(..)
                                       , TokType(..)
                                       , Macro(..)
                                       , ArgSpec(..)
                                       , ExpansionPoint(..)
                                       , SourcePos
                                       )
where
import Data.Text (Text)
import Text.Parsec.Pos (SourcePos, sourceName)
import Text.Pandoc.Sources
import Data.List (groupBy)

data TokType = CtrlSeq Text | Spaces | Newline | Symbol | Word | Comment |
               Esc1    | Esc2   | Arg Int
     deriving (TokType -> TokType -> Bool
(TokType -> TokType -> Bool)
-> (TokType -> TokType -> Bool) -> Eq TokType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TokType -> TokType -> Bool
$c/= :: TokType -> TokType -> Bool
== :: TokType -> TokType -> Bool
$c== :: TokType -> TokType -> Bool
Eq, Eq TokType
Eq TokType
-> (TokType -> TokType -> Ordering)
-> (TokType -> TokType -> Bool)
-> (TokType -> TokType -> Bool)
-> (TokType -> TokType -> Bool)
-> (TokType -> TokType -> Bool)
-> (TokType -> TokType -> TokType)
-> (TokType -> TokType -> TokType)
-> Ord TokType
TokType -> TokType -> Bool
TokType -> TokType -> Ordering
TokType -> TokType -> TokType
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: TokType -> TokType -> TokType
$cmin :: TokType -> TokType -> TokType
max :: TokType -> TokType -> TokType
$cmax :: TokType -> TokType -> TokType
>= :: TokType -> TokType -> Bool
$c>= :: TokType -> TokType -> Bool
> :: TokType -> TokType -> Bool
$c> :: TokType -> TokType -> Bool
<= :: TokType -> TokType -> Bool
$c<= :: TokType -> TokType -> Bool
< :: TokType -> TokType -> Bool
$c< :: TokType -> TokType -> Bool
compare :: TokType -> TokType -> Ordering
$ccompare :: TokType -> TokType -> Ordering
$cp1Ord :: Eq TokType
Ord, Int -> TokType -> ShowS
[TokType] -> ShowS
TokType -> String
(Int -> TokType -> ShowS)
-> (TokType -> String) -> ([TokType] -> ShowS) -> Show TokType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TokType] -> ShowS
$cshowList :: [TokType] -> ShowS
show :: TokType -> String
$cshow :: TokType -> String
showsPrec :: Int -> TokType -> ShowS
$cshowsPrec :: Int -> TokType -> ShowS
Show)

data Tok = Tok SourcePos TokType Text
     deriving (Tok -> Tok -> Bool
(Tok -> Tok -> Bool) -> (Tok -> Tok -> Bool) -> Eq Tok
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Tok -> Tok -> Bool
$c/= :: Tok -> Tok -> Bool
== :: Tok -> Tok -> Bool
$c== :: Tok -> Tok -> Bool
Eq, Eq Tok
Eq Tok
-> (Tok -> Tok -> Ordering)
-> (Tok -> Tok -> Bool)
-> (Tok -> Tok -> Bool)
-> (Tok -> Tok -> Bool)
-> (Tok -> Tok -> Bool)
-> (Tok -> Tok -> Tok)
-> (Tok -> Tok -> Tok)
-> Ord Tok
Tok -> Tok -> Bool
Tok -> Tok -> Ordering
Tok -> Tok -> Tok
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Tok -> Tok -> Tok
$cmin :: Tok -> Tok -> Tok
max :: Tok -> Tok -> Tok
$cmax :: Tok -> Tok -> Tok
>= :: Tok -> Tok -> Bool
$c>= :: Tok -> Tok -> Bool
> :: Tok -> Tok -> Bool
$c> :: Tok -> Tok -> Bool
<= :: Tok -> Tok -> Bool
$c<= :: Tok -> Tok -> Bool
< :: Tok -> Tok -> Bool
$c< :: Tok -> Tok -> Bool
compare :: Tok -> Tok -> Ordering
$ccompare :: Tok -> Tok -> Ordering
$cp1Ord :: Eq Tok
Ord, Int -> Tok -> ShowS
[Tok] -> ShowS
Tok -> String
(Int -> Tok -> ShowS)
-> (Tok -> String) -> ([Tok] -> ShowS) -> Show Tok
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Tok] -> ShowS
$cshowList :: [Tok] -> ShowS
show :: Tok -> String
$cshow :: Tok -> String
showsPrec :: Int -> Tok -> ShowS
$cshowsPrec :: Int -> Tok -> ShowS
Show)

instance ToSources [Tok] where
  toSources :: [Tok] -> Sources
toSources = [(SourcePos, Text)] -> Sources
Sources
    ([(SourcePos, Text)] -> Sources)
-> ([Tok] -> [(SourcePos, Text)]) -> [Tok] -> Sources
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Tok] -> (SourcePos, Text)) -> [[Tok]] -> [(SourcePos, Text)]
forall a b. (a -> b) -> [a] -> [b]
map (\[Tok]
ts -> case [Tok]
ts of
                    Tok SourcePos
p TokType
_ Text
_ : [Tok]
_ -> (SourcePos
p, [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Tok -> Text) -> [Tok] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Tok -> Text
tokToText [Tok]
ts)
                    [Tok]
_ -> String -> (SourcePos, Text)
forall a. HasCallStack => String -> a
error String
"toSources [Tok] encountered empty group")
    ([[Tok]] -> [(SourcePos, Text)])
-> ([Tok] -> [[Tok]]) -> [Tok] -> [(SourcePos, Text)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Tok -> Tok -> Bool) -> [Tok] -> [[Tok]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
groupBy (\(Tok SourcePos
p1 TokType
_ Text
_) (Tok SourcePos
p2 TokType
_ Text
_) -> SourcePos -> String
sourceName SourcePos
p1 String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== SourcePos -> String
sourceName SourcePos
p2)

tokToText :: Tok -> Text
tokToText :: Tok -> Text
tokToText (Tok SourcePos
_ TokType
_ Text
t) = Text
t

data ExpansionPoint = ExpandWhenDefined | ExpandWhenUsed
     deriving (ExpansionPoint -> ExpansionPoint -> Bool
(ExpansionPoint -> ExpansionPoint -> Bool)
-> (ExpansionPoint -> ExpansionPoint -> Bool) -> Eq ExpansionPoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ExpansionPoint -> ExpansionPoint -> Bool
$c/= :: ExpansionPoint -> ExpansionPoint -> Bool
== :: ExpansionPoint -> ExpansionPoint -> Bool
$c== :: ExpansionPoint -> ExpansionPoint -> Bool
Eq, Eq ExpansionPoint
Eq ExpansionPoint
-> (ExpansionPoint -> ExpansionPoint -> Ordering)
-> (ExpansionPoint -> ExpansionPoint -> Bool)
-> (ExpansionPoint -> ExpansionPoint -> Bool)
-> (ExpansionPoint -> ExpansionPoint -> Bool)
-> (ExpansionPoint -> ExpansionPoint -> Bool)
-> (ExpansionPoint -> ExpansionPoint -> ExpansionPoint)
-> (ExpansionPoint -> ExpansionPoint -> ExpansionPoint)
-> Ord ExpansionPoint
ExpansionPoint -> ExpansionPoint -> Bool
ExpansionPoint -> ExpansionPoint -> Ordering
ExpansionPoint -> ExpansionPoint -> ExpansionPoint
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ExpansionPoint -> ExpansionPoint -> ExpansionPoint
$cmin :: ExpansionPoint -> ExpansionPoint -> ExpansionPoint
max :: ExpansionPoint -> ExpansionPoint -> ExpansionPoint
$cmax :: ExpansionPoint -> ExpansionPoint -> ExpansionPoint
>= :: ExpansionPoint -> ExpansionPoint -> Bool
$c>= :: ExpansionPoint -> ExpansionPoint -> Bool
> :: ExpansionPoint -> ExpansionPoint -> Bool
$c> :: ExpansionPoint -> ExpansionPoint -> Bool
<= :: ExpansionPoint -> ExpansionPoint -> Bool
$c<= :: ExpansionPoint -> ExpansionPoint -> Bool
< :: ExpansionPoint -> ExpansionPoint -> Bool
$c< :: ExpansionPoint -> ExpansionPoint -> Bool
compare :: ExpansionPoint -> ExpansionPoint -> Ordering
$ccompare :: ExpansionPoint -> ExpansionPoint -> Ordering
$cp1Ord :: Eq ExpansionPoint
Ord, Int -> ExpansionPoint -> ShowS
[ExpansionPoint] -> ShowS
ExpansionPoint -> String
(Int -> ExpansionPoint -> ShowS)
-> (ExpansionPoint -> String)
-> ([ExpansionPoint] -> ShowS)
-> Show ExpansionPoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ExpansionPoint] -> ShowS
$cshowList :: [ExpansionPoint] -> ShowS
show :: ExpansionPoint -> String
$cshow :: ExpansionPoint -> String
showsPrec :: Int -> ExpansionPoint -> ShowS
$cshowsPrec :: Int -> ExpansionPoint -> ShowS
Show)

data Macro = Macro ExpansionPoint [ArgSpec] (Maybe [Tok]) [Tok]
     deriving Int -> Macro -> ShowS
[Macro] -> ShowS
Macro -> String
(Int -> Macro -> ShowS)
-> (Macro -> String) -> ([Macro] -> ShowS) -> Show Macro
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Macro] -> ShowS
$cshowList :: [Macro] -> ShowS
show :: Macro -> String
$cshow :: Macro -> String
showsPrec :: Int -> Macro -> ShowS
$cshowsPrec :: Int -> Macro -> ShowS
Show

data ArgSpec = ArgNum Int | Pattern [Tok]
     deriving Int -> ArgSpec -> ShowS
[ArgSpec] -> ShowS
ArgSpec -> String
(Int -> ArgSpec -> ShowS)
-> (ArgSpec -> String) -> ([ArgSpec] -> ShowS) -> Show ArgSpec
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ArgSpec] -> ShowS
$cshowList :: [ArgSpec] -> ShowS
show :: ArgSpec -> String
$cshow :: ArgSpec -> String
showsPrec :: Int -> ArgSpec -> ShowS
$cshowsPrec :: Int -> ArgSpec -> ShowS
Show