gll-0.3.0.0: src/GLL/Types/Abstract.hs
{-# LANGUAGE StandaloneDeriving #-}
-- UUAGC 0.9.52.1 (src/GLL/Types/Abstract.ag)
module GLL.Types.Abstract where
{-# LINE 1 "src/GLL/Types/Abstract.ag" #-}
{-# LINE 12 "dist/build/GLL/Types/Abstract.hs" #-}
-- | Identifier for nonterminals.
type Nt = String
-- Prod ---------------------------------------------------------
-- |
-- A production binds a nonterminal identifier (left-hand side) to a list of symbols
--(the right-hand side of the production).
data Prod t = Prod (Nt) (Symbols t)
-- Prods --------------------------------------------------------
-- | A list of 'Prod's.
type Prods t = [Prod t]
-- Grammar -----------------------------------------------------
-- |
-- A grammar is a start symbol and a list of productions.
type Grammar t = (Nt, Prods t)
-- Slot --------------------------------------------------------
-- |
-- A grammar slot acts as a label to identify progress of matching a production.
-- As such, a slot is a "Prod" with its right-hand side split in two:
-- a part before and a part after 'the dot'.
-- The dot indicates which part of the right-hand side has been processed thus far.
data Slot t = Slot (Nt) (([Symbol t])) (([Symbol t]))
-- Symbol ------------------------------------------------------
-- |
-- A 'Symbol' is either a nonterminal or a terminal,
-- where a terminal contains some arbitrary token.
data Symbol t = Nt Nt
| Term t
-- | Error (Token) (Token)
-- Symbols -----------------------------------------------------
-- |
-- A list of 'Symbol's
type Symbols t = [Symbol t]
-- Token -------------------------------------------------------
-- |
-- A datatype for representing tokens with some builtins
-- and an aribitrary Token constructor.
-- This datatype stores (optional) lexemes.
data Token = Char Char
| Keyword String
| EOS
| Epsilon
| IntLit (Maybe Int)
| BoolLit (Maybe Bool)
| StringLit (Maybe String)
| IDLit (Maybe String)
| Token String (Maybe String)
-- Tokens ------------------------------------------------------
-- |
-- A list of 'Token's
type Tokens = [Token]
-- | Class that captures elements of an input string (tokens).
--
-- * 'eos' is the end-of-string symbol
-- * 'eps' is the empty-string symbol
--
-- Both 'eos' and 'eps' must be distinct from eachother and from all
-- tokens in the input string.
-- The show instance is required to throw error messages.
class (Ord a, Eq a, Show a) => Parseable a where
eos :: a
eps :: a
-- | This function is used for matching grammar tokens and input tokens.
-- Override this method if, for example, your input tokens store lexemes
-- while the grammar tokens do not
matches :: a -> a -> Bool
matches = (==)
deriving instance Ord Token
deriving instance Eq Token
instance Show Token where
show (Char c) = "char('" ++ [c] ++ "')"
show (Keyword s) = "keyword(\"" ++ s ++ "\")"
show (EOS) = "<end-of-string>"
show (Epsilon) = "<epsilon>"
show (IntLit (Just i)) = "int(" ++ show i ++ ")"
show (IntLit _) = "<int>"
show (BoolLit (Just b)) = "bool(" ++ show b ++ ")"
show (BoolLit _) = "<bool>"
show (StringLit (Just s)) = "string(\"" ++ s ++ "\")"
show (StringLit _) = "<string>"
show (IDLit (Just id)) = "id(\"" ++ id ++ "\")"
show (IDLit Nothing) = "<id>"
show (Token nm (Just s)) = nm ++ "(\"" ++ s ++ "\")"
show (Token nm _) = "<" ++ nm ++ ">"
instance Parseable Token where
eos = EOS
eps = Epsilon
Token k _ `matches` Token k' _ = k' == k
Char c `matches` Char c' = c' == c
Keyword k `matches` Keyword k' = k' == k
EOS `matches` EOS = True
Epsilon `matches` Epsilon = True
StringLit _ `matches` StringLit _ = True
IntLit _ `matches` IntLit _ = True
BoolLit _ `matches` BoolLit _ = True
IDLit _ `matches` IDLit _ = True
_ `matches` _ = False