module GHC.Utils.Lexeme (
isLexCon, isLexVar, isLexId, isLexSym,
isLexConId, isLexConSym, isLexVarId, isLexVarSym,
startsVarSym, startsVarId, startsConSym, startsConId,
okVarOcc, okConOcc, okTcOcc,
okVarIdOcc, okVarSymOcc, okConIdOcc, okConSymOcc
) where
import GHC.Prelude
import GHC.Data.FastString
import Data.Char
import qualified Data.Set as Set
import GHC.Lexeme
isLexCon, isLexVar, isLexId, isLexSym :: FastString -> Bool
isLexConId, isLexConSym, isLexVarId, isLexVarSym :: FastString -> Bool
isLexCon :: FastString -> Bool
isLexCon FastString
cs = FastString -> Bool
isLexConId FastString
cs Bool -> Bool -> Bool
|| FastString -> Bool
isLexConSym FastString
cs
isLexVar :: FastString -> Bool
isLexVar FastString
cs = FastString -> Bool
isLexVarId FastString
cs Bool -> Bool -> Bool
|| FastString -> Bool
isLexVarSym FastString
cs
isLexId :: FastString -> Bool
isLexId FastString
cs = FastString -> Bool
isLexConId FastString
cs Bool -> Bool -> Bool
|| FastString -> Bool
isLexVarId FastString
cs
isLexSym :: FastString -> Bool
isLexSym FastString
cs = FastString -> Bool
isLexConSym FastString
cs Bool -> Bool -> Bool
|| FastString -> Bool
isLexVarSym FastString
cs
isLexConId :: FastString -> Bool
isLexConId FastString
cs
| FastString -> Bool
nullFS FastString
cs = Bool
False
| FastString
cs FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq FastString
== ([Char] -> FastString
fsLit [Char]
"[]") = Bool
True
| Bool
otherwise = Char -> Bool
startsConId (FastString -> Char
headFS FastString
cs)
isLexVarId :: FastString -> Bool
isLexVarId FastString
cs
| FastString -> Bool
nullFS FastString
cs = Bool
False
| Bool
otherwise = Char -> Bool
startsVarId (FastString -> Char
headFS FastString
cs)
isLexConSym :: FastString -> Bool
isLexConSym FastString
cs
| FastString -> Bool
nullFS FastString
cs = Bool
False
| FastString
cs FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq FastString
== ([Char] -> FastString
fsLit [Char]
"->") = Bool
True
| Bool
otherwise = Char -> Bool
startsConSym (FastString -> Char
headFS FastString
cs)
isLexVarSym :: FastString -> Bool
isLexVarSym FastString
fs
| FastString
fs FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq FastString
== ([Char] -> FastString
fsLit [Char]
"~R#") = Bool
True
| Bool
otherwise
= case (if FastString -> Bool
nullFS FastString
fs then [] else FastString -> [Char]
unpackFS FastString
fs) of
[] -> Bool
False
(Char
c:[Char]
cs) -> Char -> Bool
startsVarSym Char
c Bool -> Bool -> Bool
&& (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
all Char -> Bool
isVarSymChar [Char]
cs
okVarOcc :: String -> Bool
okVarOcc :: [Char] -> Bool
okVarOcc str :: [Char]
str@(Char
c:[Char]
_)
| Char -> Bool
startsVarId Char
c
= [Char] -> Bool
okVarIdOcc [Char]
str
| Char -> Bool
startsVarSym Char
c
= [Char] -> Bool
okVarSymOcc [Char]
str
okVarOcc [Char]
_ = Bool
False
okConOcc :: String -> Bool
okConOcc :: [Char] -> Bool
okConOcc str :: [Char]
str@(Char
c:[Char]
_)
| Char -> Bool
startsConId Char
c
= [Char] -> Bool
okConIdOcc [Char]
str
| Char -> Bool
startsConSym Char
c
= [Char] -> Bool
okConSymOcc [Char]
str
| [Char]
str [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Eq a => Eq [a]
External instance of the constraint type Eq Char
== [Char]
"[]"
= Bool
True
okConOcc [Char]
_ = Bool
False
okTcOcc :: String -> Bool
okTcOcc :: [Char] -> Bool
okTcOcc [Char]
"[]" = Bool
True
okTcOcc [Char]
"->" = Bool
True
okTcOcc [Char]
"~" = Bool
True
okTcOcc str :: [Char]
str@(Char
c:[Char]
_)
| Char -> Bool
startsConId Char
c
= [Char] -> Bool
okConIdOcc [Char]
str
| Char -> Bool
startsConSym Char
c
= [Char] -> Bool
okConSymOcc [Char]
str
| Char -> Bool
startsVarSym Char
c
= [Char] -> Bool
okVarSymOcc [Char]
str
okTcOcc [Char]
_ = Bool
False
okVarIdOcc :: String -> Bool
okVarIdOcc :: [Char] -> Bool
okVarIdOcc [Char]
str = [Char] -> Bool
okIdOcc [Char]
str Bool -> Bool -> Bool
&&
([Char]
str [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type forall a. Eq a => Eq [a]
External instance of the constraint type Eq Char
== [Char]
"_" Bool -> Bool -> Bool
|| Bool -> Bool
not ([Char]
str [Char] -> Set [Char] -> Bool
forall a. Ord a => a -> Set a -> Bool
External instance of the constraint type forall a. Ord a => Ord [a]
External instance of the constraint type Ord Char
`Set.member` Set [Char]
reservedIds))
okVarSymOcc :: String -> Bool
okVarSymOcc :: [Char] -> Bool
okVarSymOcc [Char]
str = (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
all Char -> Bool
okSymChar [Char]
str Bool -> Bool -> Bool
&&
Bool -> Bool
not ([Char]
str [Char] -> Set [Char] -> Bool
forall a. Ord a => a -> Set a -> Bool
External instance of the constraint type forall a. Ord a => Ord [a]
External instance of the constraint type Ord Char
`Set.member` Set [Char]
reservedOps) Bool -> Bool -> Bool
&&
Bool -> Bool
not ([Char] -> Bool
isDashes [Char]
str)
okConIdOcc :: String -> Bool
okConIdOcc :: [Char] -> Bool
okConIdOcc [Char]
str = [Char] -> Bool
okIdOcc [Char]
str Bool -> Bool -> Bool
||
Bool -> [Char] -> Bool
is_tuple_name1 Bool
True [Char]
str Bool -> Bool -> Bool
||
Bool -> [Char] -> Bool
is_tuple_name1 Bool
False [Char]
str Bool -> Bool -> Bool
||
[Char] -> Bool
is_sum_name1 [Char]
str
where
is_tuple_name1 :: Bool -> [Char] -> Bool
is_tuple_name1 Bool
True (Char
'(' : [Char]
rest) = Bool -> [Char] -> Bool
is_tuple_name2 Bool
True [Char]
rest
is_tuple_name1 Bool
False (Char
'(' : Char
'#' : [Char]
rest) = Bool -> [Char] -> Bool
is_tuple_name2 Bool
False [Char]
rest
is_tuple_name1 Bool
_ [Char]
_ = Bool
False
is_tuple_name2 :: Bool -> [Char] -> Bool
is_tuple_name2 Bool
True [Char]
")" = Bool
True
is_tuple_name2 Bool
False [Char]
"#)" = Bool
True
is_tuple_name2 Bool
boxed (Char
',' : [Char]
rest) = Bool -> [Char] -> Bool
is_tuple_name2 Bool
boxed [Char]
rest
is_tuple_name2 Bool
boxed (Char
ws : [Char]
rest)
| Char -> Bool
isSpace Char
ws = Bool -> [Char] -> Bool
is_tuple_name2 Bool
boxed [Char]
rest
is_tuple_name2 Bool
_ [Char]
_ = Bool
False
is_sum_name1 :: [Char] -> Bool
is_sum_name1 (Char
'(' : Char
'#' : [Char]
rest) = Bool -> [Char] -> Bool
is_sum_name2 Bool
False [Char]
rest
is_sum_name1 [Char]
_ = Bool
False
is_sum_name2 :: Bool -> [Char] -> Bool
is_sum_name2 Bool
_ [Char]
"#)" = Bool
True
is_sum_name2 Bool
underscore (Char
'|' : [Char]
rest) = Bool -> [Char] -> Bool
is_sum_name2 Bool
underscore [Char]
rest
is_sum_name2 Bool
False (Char
'_' : [Char]
rest) = Bool -> [Char] -> Bool
is_sum_name2 Bool
True [Char]
rest
is_sum_name2 Bool
underscore (Char
ws : [Char]
rest)
| Char -> Bool
isSpace Char
ws = Bool -> [Char] -> Bool
is_sum_name2 Bool
underscore [Char]
rest
is_sum_name2 Bool
_ [Char]
_ = Bool
False
okConSymOcc :: String -> Bool
okConSymOcc :: [Char] -> Bool
okConSymOcc [Char]
":" = Bool
True
okConSymOcc [Char]
str = (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
all Char -> Bool
okSymChar [Char]
str Bool -> Bool -> Bool
&&
Bool -> Bool
not ([Char]
str [Char] -> Set [Char] -> Bool
forall a. Ord a => a -> Set a -> Bool
External instance of the constraint type forall a. Ord a => Ord [a]
External instance of the constraint type Ord Char
`Set.member` Set [Char]
reservedOps)
okIdOcc :: String -> Bool
okIdOcc :: [Char] -> Bool
okIdOcc [Char]
str
= let hashes :: [Char]
hashes = (Char -> Bool) -> [Char] -> [Char]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
okIdChar [Char]
str in
(Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Char
== Char
'#') [Char]
hashes
okIdChar :: Char -> Bool
okIdChar :: Char -> Bool
okIdChar Char
c = case Char -> GeneralCategory
generalCategory Char
c of
GeneralCategory
UppercaseLetter -> Bool
True
GeneralCategory
LowercaseLetter -> Bool
True
GeneralCategory
TitlecaseLetter -> Bool
True
GeneralCategory
ModifierLetter -> Bool
True
GeneralCategory
OtherLetter -> Bool
True
GeneralCategory
NonSpacingMark -> Bool
True
GeneralCategory
DecimalNumber -> Bool
True
GeneralCategory
OtherNumber -> Bool
True
GeneralCategory
_ -> Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Char
== Char
'\'' Bool -> Bool -> Bool
|| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Char
== Char
'_'
reservedIds :: Set.Set String
reservedIds :: Set [Char]
reservedIds = [[Char]] -> Set [Char]
forall a. Ord a => [a] -> Set a
External instance of the constraint type forall a. Ord a => Ord [a]
External instance of the constraint type Ord Char
Set.fromList [ [Char]
"case", [Char]
"class", [Char]
"data", [Char]
"default", [Char]
"deriving"
, [Char]
"do", [Char]
"else", [Char]
"foreign", [Char]
"if", [Char]
"import", [Char]
"in"
, [Char]
"infix", [Char]
"infixl", [Char]
"infixr", [Char]
"instance", [Char]
"let"
, [Char]
"module", [Char]
"newtype", [Char]
"of", [Char]
"then", [Char]
"type", [Char]
"where"
, [Char]
"_" ]
reservedOps :: Set.Set String
reservedOps :: Set [Char]
reservedOps = [[Char]] -> Set [Char]
forall a. Ord a => [a] -> Set a
External instance of the constraint type forall a. Ord a => Ord [a]
External instance of the constraint type Ord Char
Set.fromList [ [Char]
"..", [Char]
":", [Char]
"::", [Char]
"=", [Char]
"\\", [Char]
"|", [Char]
"<-", [Char]
"->"
, [Char]
"@", [Char]
"~", [Char]
"=>" ]
isDashes :: String -> Bool
isDashes :: [Char] -> Bool
isDashes (Char
'-' : Char
'-' : [Char]
rest) = (Char -> Bool) -> [Char] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
External instance of the constraint type Foldable []
all (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
External instance of the constraint type Eq Char
== Char
'-') [Char]
rest
isDashes [Char]
_ = Bool
False