{-# LANGUAGE StandaloneDeriving #-}
module GLL.Types.Grammar where
import Data.Text
type Nt = Text
data Prod t = Prod (Nt) (Symbols t)
type Prods t = [Prod t]
type Grammar t = (Nt, Prods t)
data Slot t = Slot (Nt) (([Symbol t])) (([Symbol t]))
data Symbol t = Nt Nt
| Term t
type Symbols t = [Symbol t]
data Token = Char Char
| Keyword String
| EOS
| Epsilon
| IntLit (Maybe Int)
| FloatLit (Maybe Double)
| BoolLit (Maybe Bool)
| StringLit (Maybe String)
| CharLit (Maybe Char)
| IDLit (Maybe String)
| AltIDLit (Maybe String)
| Token String (Maybe String)
type Tokens = [Token]
class (Ord a, Eq a, Show a) => Parseable a where
eos :: a
eps :: a
matches :: a -> a -> Bool
unlex :: a -> String
unlex = forall a. Show a => a -> String
show
class SubsumesToken a where
upcast :: Token -> a
downcast :: a -> Maybe Token
instance SubsumesToken Token where
upcast :: Token -> Token
upcast = forall a. a -> a
id
downcast :: Token -> Maybe Token
downcast = forall a. a -> Maybe a
Just
deriving instance Ord Token
deriving instance Eq Token
instance Show Token where
show :: Token -> String
show (Char Char
c) = String
"keychar('" forall a. [a] -> [a] -> [a]
++ [Char
c] forall a. [a] -> [a] -> [a]
++ String
"')"
show (Keyword String
s) = String
"keyword(\"" forall a. [a] -> [a] -> [a]
++ String
s forall a. [a] -> [a] -> [a]
++ String
"\")"
show (Token
EOS) = String
"<end-of-string>"
show (Token
Epsilon) = String
"<epsilon>"
show (IntLit (Just Int
i)) = String
"int(" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i forall a. [a] -> [a] -> [a]
++ String
")"
show (IntLit Maybe Int
_) = String
"<int>"
show (FloatLit (Just Double
i)) = String
"float(" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Double
i forall a. [a] -> [a] -> [a]
++ String
")"
show (FloatLit Maybe Double
_) = String
"<float>"
show (BoolLit (Just Bool
b)) = String
"bool(" forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Bool
b forall a. [a] -> [a] -> [a]
++ String
")"
show (BoolLit Maybe Bool
_) = String
"<bool>"
show (StringLit (Just String
s)) = String
"string(\"" forall a. [a] -> [a] -> [a]
++ String
s forall a. [a] -> [a] -> [a]
++ String
"\")"
show (StringLit Maybe String
_) = String
"<string>"
show (CharLit (Just Char
c)) = String
"char('" forall a. [a] -> [a] -> [a]
++ [Char
c] forall a. [a] -> [a] -> [a]
++ String
"')"
show (CharLit Maybe Char
Nothing) = String
"<char>"
show (AltIDLit (Just String
id)) = String
"altid(\"" forall a. [a] -> [a] -> [a]
++ String
id forall a. [a] -> [a] -> [a]
++ String
"\")"
show (AltIDLit Maybe String
Nothing) = String
"<altid>"
show (IDLit (Just String
id)) = String
"id(\"" forall a. [a] -> [a] -> [a]
++ String
id forall a. [a] -> [a] -> [a]
++ String
"\")"
show (IDLit Maybe String
Nothing) = String
"<id>"
show (Token String
nm (Just String
s)) = String
nm forall a. [a] -> [a] -> [a]
++ String
"(\"" forall a. [a] -> [a] -> [a]
++ String
s forall a. [a] -> [a] -> [a]
++ String
"\")"
show (Token String
nm Maybe String
_) = String
"<" forall a. [a] -> [a] -> [a]
++ String
nm forall a. [a] -> [a] -> [a]
++ String
">"
instance Parseable Token where
eos :: Token
eos = Token
EOS
eps :: Token
eps = Token
Epsilon
unlex :: Token -> String
unlex = Token -> String
unlexToken
Token String
k Maybe String
_ matches :: Token -> Token -> Bool
`matches` Token String
k' Maybe String
_ = String
k' forall a. Eq a => a -> a -> Bool
== String
k
Char Char
c `matches` Char Char
c' = Char
c' forall a. Eq a => a -> a -> Bool
== Char
c
Keyword String
k `matches` Keyword String
k' = String
k' forall a. Eq a => a -> a -> Bool
== String
k
Token
EOS `matches` Token
EOS = Bool
True
Token
Epsilon `matches` Token
Epsilon = Bool
True
StringLit Maybe String
_ `matches` StringLit Maybe String
_ = Bool
True
CharLit Maybe Char
_ `matches` CharLit Maybe Char
_ = Bool
True
IntLit Maybe Int
_ `matches` IntLit Maybe Int
_ = Bool
True
FloatLit Maybe Double
_ `matches` FloatLit Maybe Double
_ = Bool
True
BoolLit Maybe Bool
_ `matches` BoolLit Maybe Bool
_ = Bool
True
AltIDLit Maybe String
_ `matches` AltIDLit Maybe String
_ = Bool
True
IDLit Maybe String
_ `matches` IDLit Maybe String
_ = Bool
True
Token
_ `matches` Token
_ = Bool
False
unlexTokens :: [Token] -> String
unlexTokens :: [Token] -> String
unlexTokens = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
Prelude.concatMap Token -> String
unlexToken
unlexToken :: Token -> String
unlexToken :: Token -> String
unlexToken Token
t = case Token
t of
Char Char
c -> [Char
c]
Keyword String
s -> String
s
IntLit (Just Int
i) -> forall a. Show a => a -> String
show Int
i
BoolLit (Just Bool
b) -> forall a. Show a => a -> String
show Bool
b
StringLit (Just String
s) -> String
s
CharLit (Just Char
c) -> [Char
c]
AltIDLit (Just String
s) -> String
s
IDLit (Just String
s) -> String
s
Token String
_ (Just String
s) -> String
s
Token
_ -> String
""
isNt :: Symbol t -> Bool
isNt (Nt Nt
_) = Bool
True
isNt Symbol t
_ = Bool
False
isTerm :: Symbol t -> Bool
isTerm (Term t
_) = Bool
True
isTerm Symbol t
_ = Bool
False
instance (Show t) => Show (Slot t) where
show :: Slot t -> String
show (Slot Nt
x [Symbol t]
alpha [Symbol t]
beta) = forall a. Show a => a -> String
show Nt
x forall a. [a] -> [a] -> [a]
++ String
" ::= " forall a. [a] -> [a] -> [a]
++ forall {a}. Show a => [Symbol a] -> String
showRhs [Symbol t]
alpha forall a. [a] -> [a] -> [a]
++ String
"." forall a. [a] -> [a] -> [a]
++ forall {a}. Show a => [Symbol a] -> String
showRhs [Symbol t]
beta
where showRhs :: [Symbol a] -> String
showRhs [] = String
""
showRhs ((Term a
t):[Symbol a]
rhs) = forall a. Show a => a -> String
show a
t forall a. [a] -> [a] -> [a]
++ [Symbol a] -> String
showRhs [Symbol a]
rhs
showRhs ((Nt Nt
x):[Symbol a]
rhs) = forall a. Show a => a -> String
show Nt
x forall a. [a] -> [a] -> [a]
++ [Symbol a] -> String
showRhs [Symbol a]
rhs
instance (Show t) => Show (Symbol t) where
show :: Symbol t -> String
show (Nt Nt
s) = Nt -> String
unpack Nt
s
show (Term t
t) = forall a. Show a => a -> String
show t
t
deriving instance (Ord t) => Ord (Slot t)
deriving instance (Eq t) => Eq (Slot t)
deriving instance (Show t) => Show (Prod t)
deriving instance (Ord t) => Ord (Prod t)
deriving instance (Eq t) => Eq (Prod t)
deriving instance (Eq t) => Eq (Symbol t)
deriving instance (Ord t) => Ord (Symbol t)