0 | module TyRE.Parser.SMConstruction
6 | import TyRE.Parser.SM
7 | import TyRE.Parser.GroupThompson
11 | compile : {0 a : Type} -> (tyre : TyRE a) -> SM a
14 | let lookup : Void -> SnocList Type
16 | init : InitStatesType () Void lookup
17 | init = [(
Nothing ** [< Push ()] `Element` [< InitPush])
]
18 | next : TransitionRelation () Void lookup
20 | in MkSM Void lookup init next
22 | compile (MatchChar f) =
23 | let lookup : () -> SnocList Type
25 | init : InitStatesType Char () lookup
26 | init = [(
Just () ** [<] `Element` [<])
]
27 | next : TransitionRelation Char () lookup
30 | then [(
Nothing ** [< PushChar])
]
32 | in MkSM () lookup init next
34 | compile ((<*>) {a,b} x y) =
35 | let MkSM s1 l1 i1 n1 := compile x
36 | MkSM s2 l2 i2 n2 := compile y
39 | 0 lookup : T -> SnocList Type
40 | lookup (Left s) = l1 s
41 | lookup (Right s) = [< a] ++ l2 s
42 | init : InitStatesType (a, b) T lookup
46 | (
Nothing ** r `Element` p)
=>
48 | (
Nothing ** r2 `Element` p2)
=>
50 | ** r ++ lift r2 :< ReducePair MkPair `Element`
51 | p ++ lift p2 :< InitReducePair)
52 | (
Just st ** r2 `Element` p2)
=>
54 | ** (r ++ lift r2) `Element`
57 | (
Just st ** r)
=> [(
Just (Left st) ** r)
]))
58 | next : TransitionRelation (a,b) T lookup
64 | (
Nothing ** r2 `Element` _)
=>
66 | ** r ++ lift r2 :< ReducePair MkPair)
67 | (
Just s2 ** r2 `Element` _)
=>
71 | (
Just st ** r)
=> [(
Just (Left st) ** r)
]))
74 | (
Nothing ** r)
=> (
Nothing ** lift r :< ReducePair MkPair)
75 | (
Just st ** r)
=> (
Just (Right st) ** lift r)
)
77 | in MkSM T lookup init next
79 | compile ((<|>) {a,b} x y) =
80 | let MkSM s1 l1 i1 n1 := compile x
81 | MkSM s2 l2 i2 n2 := compile y
84 | 0 lookup : T -> SnocList Type
85 | lookup (Left s) = l1 s
86 | lookup (Right s) = l2 s
87 | init : InitStatesType (Either a b) T lookup
89 | (
Nothing ** rt `Element` p)
=>
91 | ** rt :< Transform Left `Element`
93 | (
Just st ** r)
=> (
Just (Left st) ** r)
)
96 | (
Nothing ** rt `Element` p)
=>
98 | ** rt :< (Transform Right) `Element`
100 | (
Just st ** r)
=> (
Just (Right st) ** r)
)
102 | next : TransitionRelation (Either a b) T lookup
106 | (
Nothing ** rt :< Transform Left)
107 | (
Just st ** rt)
=> (
Just (Left st) ** rt)
)
112 | (
Nothing ** rt :< Transform Right)
113 | (
Just st ** rt)
=> (
Just (Right st) ** rt)
)
115 | in MkSM T lookup init next
117 | compile (Rep {a} re) =
118 | let MkSM t lookupPrev initPrev nextPrev := compile re
121 | 0 lookup : T -> SnocList Type
122 | lookup s = [< SnocList a] ++ lookupPrev s
123 | init : InitStatesType (SnocList a) T lookup
124 | init = (
Nothing ** [< Push [<]] `Element` [< InitPush])
126 | (
Nothing ** r `Element` p)
=>
127 | (
Nothing ** [< Push [<]] `Element` [< InitPush])
128 | (
Just st ** r `Element` p)
=>
129 | (
Just st ** ([< Push Prelude.Basics.Lin] ++ lift r)
130 | `Element` ([< InitPush] ++ lift p))
)
132 | next : TransitionRelation (SnocList a) T lookup
137 | (
Nothing ** lift r :< ReducePair (:<))
139 | (
Nothing ** r2 `Element` _)
=>
140 | (
Nothing ** lift r :< ReducePair (:<))
141 | (
Just st ** r2 `Element` _)
=>
142 | (
Just st ** lift r :< ReducePair (:<) ++ lift r2)
)
144 | (
Just st ** r)
=> [(
Just st ** lift r)
]
145 | in MkSM T lookup init next
147 | compile {a = String} (Group r) = asSM (groupSM r) where
148 | asSM : GroupSM -> SM String
149 | asSM (MkGroupSM initStates statesWithNext max) =
150 | let lookup : Nat -> SnocList Type
152 | init : InitStatesType String Nat lookup
154 | Just s => (
Just s ** [< Record] `Element` [< InitRecord])
155 | Nothing => (
Nothing ** [< EmitString] `Element`
156 | [< InitEmitString])
)
158 | next : TransitionRelation String Nat lookup
159 | next s c with (find (\case (n, ns) => n == s) statesWithNext)
160 | next s c | Nothing = []
161 | next s c | (Just (_, (MkNextStates condition isSat))) =
162 | if satisfies condition c
164 | Nothing => (
Nothing ** [< EmitString])
165 | Just s => (
Just s ** [<])
) isSat
167 | in MkSM Nat lookup init next
168 | compile (Conv {a,b} re f) =
169 | let MkSM t lookup initPrev nextPrev := compile re
170 | init : InitStatesType b t lookup
172 | (
Nothing ** r `Element` p)
=>
173 | (
Nothing ** r :< Transform f `Element`
174 | p :< InitTransform)
175 | (
Just st ** rp)
=> (
Just st ** rp)
)
177 | next : TransitionRelation b t lookup
178 | next st c = map (\case
179 | (
Nothing ** r)
=> (
Nothing ** r :< Transform f)
180 | (
Just st ** r)
=> (
Just st ** r)
)
182 | in MkSM t lookup init next