{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StrictData    #-}
-- | An Alex lexer, as far as generating text for its tokens needs it. Macros
-- are expanded while parsing, so a regex stands on its own.
module Language.Alex.Ast
    ( AlexFile (..)
    , CharSet (..)
    , Regex (..)
    , Rule (..)
    ) where

import           Data.Set     (Set)
import           Data.Text    (Text)
import           GHC.Generics (Generic)

-- | A set of characters: these characters, or all characters but these.
data CharSet
    = Only (Set Char)
    | AllBut (Set Char)
    deriving (Int -> CharSet -> ShowS
[CharSet] -> ShowS
CharSet -> String
(Int -> CharSet -> ShowS)
-> (CharSet -> String) -> ([CharSet] -> ShowS) -> Show CharSet
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CharSet -> ShowS
showsPrec :: Int -> CharSet -> ShowS
$cshow :: CharSet -> String
show :: CharSet -> String
$cshowList :: [CharSet] -> ShowS
showList :: [CharSet] -> ShowS
Show, CharSet -> CharSet -> Bool
(CharSet -> CharSet -> Bool)
-> (CharSet -> CharSet -> Bool) -> Eq CharSet
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CharSet -> CharSet -> Bool
== :: CharSet -> CharSet -> Bool
$c/= :: CharSet -> CharSet -> Bool
/= :: CharSet -> CharSet -> Bool
Eq, Eq CharSet
Eq CharSet =>
(CharSet -> CharSet -> Ordering)
-> (CharSet -> CharSet -> Bool)
-> (CharSet -> CharSet -> Bool)
-> (CharSet -> CharSet -> Bool)
-> (CharSet -> CharSet -> Bool)
-> (CharSet -> CharSet -> CharSet)
-> (CharSet -> CharSet -> CharSet)
-> Ord CharSet
CharSet -> CharSet -> Bool
CharSet -> CharSet -> Ordering
CharSet -> CharSet -> CharSet
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
$ccompare :: CharSet -> CharSet -> Ordering
compare :: CharSet -> CharSet -> Ordering
$c< :: CharSet -> CharSet -> Bool
< :: CharSet -> CharSet -> Bool
$c<= :: CharSet -> CharSet -> Bool
<= :: CharSet -> CharSet -> Bool
$c> :: CharSet -> CharSet -> Bool
> :: CharSet -> CharSet -> Bool
$c>= :: CharSet -> CharSet -> Bool
>= :: CharSet -> CharSet -> Bool
$cmax :: CharSet -> CharSet -> CharSet
max :: CharSet -> CharSet -> CharSet
$cmin :: CharSet -> CharSet -> CharSet
min :: CharSet -> CharSet -> CharSet
Ord, (forall x. CharSet -> Rep CharSet x)
-> (forall x. Rep CharSet x -> CharSet) -> Generic CharSet
forall x. Rep CharSet x -> CharSet
forall x. CharSet -> Rep CharSet x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CharSet -> Rep CharSet x
from :: forall x. CharSet -> Rep CharSet x
$cto :: forall x. Rep CharSet x -> CharSet
to :: forall x. Rep CharSet x -> CharSet
Generic)

-- | A regex with its macros expanded.
data Regex
    = RString Text
    | RSet CharSet
    | RStar Regex
    | RPlus Regex
    | ROpt Regex
    | RSeq [Regex]
    | RAlt [Regex]
    deriving (Int -> Regex -> ShowS
[Regex] -> ShowS
Regex -> String
(Int -> Regex -> ShowS)
-> (Regex -> String) -> ([Regex] -> ShowS) -> Show Regex
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Regex -> ShowS
showsPrec :: Int -> Regex -> ShowS
$cshow :: Regex -> String
show :: Regex -> String
$cshowList :: [Regex] -> ShowS
showList :: [Regex] -> ShowS
Show, Regex -> Regex -> Bool
(Regex -> Regex -> Bool) -> (Regex -> Regex -> Bool) -> Eq Regex
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Regex -> Regex -> Bool
== :: Regex -> Regex -> Bool
$c/= :: Regex -> Regex -> Bool
/= :: Regex -> Regex -> Bool
Eq, Eq Regex
Eq Regex =>
(Regex -> Regex -> Ordering)
-> (Regex -> Regex -> Bool)
-> (Regex -> Regex -> Bool)
-> (Regex -> Regex -> Bool)
-> (Regex -> Regex -> Bool)
-> (Regex -> Regex -> Regex)
-> (Regex -> Regex -> Regex)
-> Ord Regex
Regex -> Regex -> Bool
Regex -> Regex -> Ordering
Regex -> Regex -> Regex
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
$ccompare :: Regex -> Regex -> Ordering
compare :: Regex -> Regex -> Ordering
$c< :: Regex -> Regex -> Bool
< :: Regex -> Regex -> Bool
$c<= :: Regex -> Regex -> Bool
<= :: Regex -> Regex -> Bool
$c> :: Regex -> Regex -> Bool
> :: Regex -> Regex -> Bool
$c>= :: Regex -> Regex -> Bool
>= :: Regex -> Regex -> Bool
$cmax :: Regex -> Regex -> Regex
max :: Regex -> Regex -> Regex
$cmin :: Regex -> Regex -> Regex
min :: Regex -> Regex -> Regex
Ord, (forall x. Regex -> Rep Regex x)
-> (forall x. Rep Regex x -> Regex) -> Generic Regex
forall x. Rep Regex x -> Regex
forall x. Regex -> Rep Regex x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Regex -> Rep Regex x
from :: forall x. Regex -> Rep Regex x
$cto :: forall x. Rep Regex x -> Regex
to :: forall x. Rep Regex x -> Regex
Generic)

-- | A lexer rule: where it applies, what it matches, and its action.
data Rule = Rule
    { Rule -> [Text]
ruleStartCodes :: [Text]
      -- ^ Empty means every start code.
    , Rule -> Regex
ruleRegex      :: Regex
    , Rule -> Text
ruleCode       :: Text
      -- ^ The action, without braces, or @;@ for a rule that makes no token.
    }
    deriving (Int -> Rule -> ShowS
[Rule] -> ShowS
Rule -> String
(Int -> Rule -> ShowS)
-> (Rule -> String) -> ([Rule] -> ShowS) -> Show Rule
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Rule -> ShowS
showsPrec :: Int -> Rule -> ShowS
$cshow :: Rule -> String
show :: Rule -> String
$cshowList :: [Rule] -> ShowS
showList :: [Rule] -> ShowS
Show, Rule -> Rule -> Bool
(Rule -> Rule -> Bool) -> (Rule -> Rule -> Bool) -> Eq Rule
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Rule -> Rule -> Bool
== :: Rule -> Rule -> Bool
$c/= :: Rule -> Rule -> Bool
/= :: Rule -> Rule -> Bool
Eq, Eq Rule
Eq Rule =>
(Rule -> Rule -> Ordering)
-> (Rule -> Rule -> Bool)
-> (Rule -> Rule -> Bool)
-> (Rule -> Rule -> Bool)
-> (Rule -> Rule -> Bool)
-> (Rule -> Rule -> Rule)
-> (Rule -> Rule -> Rule)
-> Ord Rule
Rule -> Rule -> Bool
Rule -> Rule -> Ordering
Rule -> Rule -> Rule
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
$ccompare :: Rule -> Rule -> Ordering
compare :: Rule -> Rule -> Ordering
$c< :: Rule -> Rule -> Bool
< :: Rule -> Rule -> Bool
$c<= :: Rule -> Rule -> Bool
<= :: Rule -> Rule -> Bool
$c> :: Rule -> Rule -> Bool
> :: Rule -> Rule -> Bool
$c>= :: Rule -> Rule -> Bool
>= :: Rule -> Rule -> Bool
$cmax :: Rule -> Rule -> Rule
max :: Rule -> Rule -> Rule
$cmin :: Rule -> Rule -> Rule
min :: Rule -> Rule -> Rule
Ord, (forall x. Rule -> Rep Rule x)
-> (forall x. Rep Rule x -> Rule) -> Generic Rule
forall x. Rep Rule x -> Rule
forall x. Rule -> Rep Rule x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Rule -> Rep Rule x
from :: forall x. Rule -> Rep Rule x
$cto :: forall x. Rep Rule x -> Rule
to :: forall x. Rep Rule x -> Rule
Generic)

-- | The rules of a lexer, in order.
newtype AlexFile = AlexFile
    { AlexFile -> [Rule]
alexRules :: [Rule]
    }
    deriving (Int -> AlexFile -> ShowS
[AlexFile] -> ShowS
AlexFile -> String
(Int -> AlexFile -> ShowS)
-> (AlexFile -> String) -> ([AlexFile] -> ShowS) -> Show AlexFile
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AlexFile -> ShowS
showsPrec :: Int -> AlexFile -> ShowS
$cshow :: AlexFile -> String
show :: AlexFile -> String
$cshowList :: [AlexFile] -> ShowS
showList :: [AlexFile] -> ShowS
Show, AlexFile -> AlexFile -> Bool
(AlexFile -> AlexFile -> Bool)
-> (AlexFile -> AlexFile -> Bool) -> Eq AlexFile
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AlexFile -> AlexFile -> Bool
== :: AlexFile -> AlexFile -> Bool
$c/= :: AlexFile -> AlexFile -> Bool
/= :: AlexFile -> AlexFile -> Bool
Eq, Eq AlexFile
Eq AlexFile =>
(AlexFile -> AlexFile -> Ordering)
-> (AlexFile -> AlexFile -> Bool)
-> (AlexFile -> AlexFile -> Bool)
-> (AlexFile -> AlexFile -> Bool)
-> (AlexFile -> AlexFile -> Bool)
-> (AlexFile -> AlexFile -> AlexFile)
-> (AlexFile -> AlexFile -> AlexFile)
-> Ord AlexFile
AlexFile -> AlexFile -> Bool
AlexFile -> AlexFile -> Ordering
AlexFile -> AlexFile -> AlexFile
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
$ccompare :: AlexFile -> AlexFile -> Ordering
compare :: AlexFile -> AlexFile -> Ordering
$c< :: AlexFile -> AlexFile -> Bool
< :: AlexFile -> AlexFile -> Bool
$c<= :: AlexFile -> AlexFile -> Bool
<= :: AlexFile -> AlexFile -> Bool
$c> :: AlexFile -> AlexFile -> Bool
> :: AlexFile -> AlexFile -> Bool
$c>= :: AlexFile -> AlexFile -> Bool
>= :: AlexFile -> AlexFile -> Bool
$cmax :: AlexFile -> AlexFile -> AlexFile
max :: AlexFile -> AlexFile -> AlexFile
$cmin :: AlexFile -> AlexFile -> AlexFile
min :: AlexFile -> AlexFile -> AlexFile
Ord, (forall x. AlexFile -> Rep AlexFile x)
-> (forall x. Rep AlexFile x -> AlexFile) -> Generic AlexFile
forall x. Rep AlexFile x -> AlexFile
forall x. AlexFile -> Rep AlexFile x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AlexFile -> Rep AlexFile x
from :: forall x. AlexFile -> Rep AlexFile x
$cto :: forall x. Rep AlexFile x -> AlexFile
to :: forall x. Rep AlexFile x -> AlexFile
Generic)