1 -- | Legacy code no longer used, for reminder only.
2 module Language.Symantic.Parsing.Token where
4 import Data.Semigroup (Semigroup(..))
6 import Language.Symantic.Grammar (Gram_Meta(..))
9 type Token ss = TokenR ss ss
12 data TokenR (ss::[*]) (rs::[*]) (s:: *) where
13 TokenZ :: TokenT ss s -> TokenR ss (s ': rs) s
14 TokenS :: TokenR ss rs s -> TokenR ss (not_s ': rs) s
17 -- ** Data family 'TokenT'
18 data family TokenT (ss::[*]) (s:: *) :: *
21 type Eq_Token ss = TestEquality (Token ss)
23 ( Eq (TokenT ss (Proxy s))
24 , TestEquality (TokenR ss (r ': rs))
25 ) => TestEquality (TokenR ss (Proxy s ': r ': rs)) where
26 ctxtEquality (TokenZ x) (TokenZ y) | x == y = Just Refl
27 ctxtEquality (TokenS x) (TokenS y) | Just Refl <- ctxtEquality x y = Just Refl
28 ctxtEquality _ _ = Nothing
30 Eq (TokenT ss (Proxy s)) =>
31 TestEquality (TokenR ss (Proxy s ': '[])) where
32 ctxtEquality (TokenZ x) (TokenZ y) | x == y = Just Refl
33 ctxtEquality _ _ = Nothing
34 instance TestEquality (TokenR ss rs) => Eq (TokenR ss rs s) where
35 x == y = isJust $ ctxtEquality x y
37 -- ** Type 'Show_Token'
38 type Show_Token ss = Show_TokenR ss ss
40 -- *** Type 'Show_TokenR'
41 class Show_TokenR (ss::[*]) (rs::[*]) where
42 show_TokenR :: TokenR ss rs x -> String
44 ( Show (TokenT ss (Proxy s))
45 , Show_TokenR ss (r ': rs)
46 ) => Show_TokenR ss (Proxy s ': r ': rs) where
47 show_TokenR (TokenZ s) = show s
48 show_TokenR (TokenS rs) = show_TokenR rs
50 Show (TokenT ss (Proxy s)) =>
51 Show_TokenR ss (Proxy s ': '[]) where
52 show_TokenR (TokenZ s) = show s
53 show_TokenR TokenS{} = error "Oops, the impossible happened..."
54 instance Show_TokenR ss rs => Show (TokenR ss rs s) where
58 -- | Convenient type synonym wrapping 'Inj_TokenP'
59 -- applied on the correct 'Index'.
61 = Inj_TokenP (Index ss (Proxy s)) ss ss s
64 :: forall ss s. Inj_Token ss s
65 => TokenT ss (Proxy s)
67 inj_Token = inj_TokenP (Proxy @(Index ss (Proxy s)))
69 -- ** Class 'Inj_TokenP'
70 class Inj_TokenP p ss rs (s::ks) where
71 inj_TokenP :: Proxy p -> TokenT ss (Proxy s) -> TokenR ss rs (Proxy s)
72 instance Inj_TokenP Zero ss (Proxy s ': rs) s where
75 Inj_TokenP p ss rs s =>
76 Inj_TokenP (Succ p) ss (not_t ': rs) s where
77 inj_TokenP _p = TokenS . inj_TokenP (Proxy @p)
79 -- ** Type 'Inj_Tokens'
80 type Inj_Tokens ss ts_to_inj
81 = Concat_Constraints (Inj_TokensR ss ts_to_inj)
83 -- *** Type family 'Inj_TokensR'
84 type family Inj_TokensR ss ts_to_inj where
85 Inj_TokensR ss '[] = '[]
86 Inj_TokensR ss (Proxy s ': rs) = Inj_Token ss s ': Inj_TokensR ss rs
88 -- * Class 'Proj_Token'
89 -- | Convenient type synonym wrapping 'Proj_TokenP'
90 -- applied on the correct 'Index'.
92 = Proj_TokenP (Index ss (Proxy s)) ss ss s
98 -> Maybe (Proxy s :~: u, TokenT ss u)
99 proj_Token = proj_TokenP (Proxy @(Index ss (Proxy s)))
101 -- ** Type 'Proj_TokenP'
102 class Proj_TokenP p ss rs s where
104 :: Proxy p -> TokenR ss rs u
105 -> Maybe (Proxy s :~: u, TokenT ss u)
106 instance Proj_TokenP Zero ss (Proxy s ': rs) s where
107 proj_TokenP _p (TokenZ tok) = Just (Refl, tok)
108 proj_TokenP _p TokenS{} = Nothing
109 instance Proj_TokenP p ss rs s => Proj_TokenP (Succ p) ss (not_t ': rs) s where
110 proj_TokenP _p TokenZ{} = Nothing
111 proj_TokenP _p (TokenS u) = proj_TokenP (Proxy @p) u
114 -- | Existential 'Token'.
115 data EToken src (ss::[*])
116 = forall s. EToken src (Token ss (Proxy s))
118 instance Eq_Token ss => Eq (EToken src ss) where
119 EToken _sx x == EToken _sy y = isJust $ ctxtEquality x y
120 instance Show_Token ss => Show (EToken src ss) where
121 show (EToken _src x) = show x
122 type instance SourceOf (EToken src ss) = src
123 instance Source src => Sourceable (EToken src ss) where
124 sourceOf (EToken src _tok) = src
125 setSource (EToken _src tok) src = EToken src tok
130 => src -> TokenT ss (Proxy s) -> EToken src ss
131 inj_EToken src = EToken src . inj_TokenP (Proxy @(Index ss (Proxy s)))
137 -> Maybe (src, TokenT ss (Proxy s))
138 proj_EToken (EToken src (tok::TokenR ss ss (Proxy u))) =
139 case proj_Token tok of
140 Just (Refl :: Proxy s :~: Proxy u, to) -> Just (src, to)