0 | module Text.ILex.Lexer
2 | import public Data.Array
3 | import public Data.Enum
4 | import public Data.List
5 | import public Data.Prim.Bits32
6 | import public Text.ILex.RExp
8 | import Control.Monad.State
10 | import Data.ByteString
11 | import Data.Linear.Traverse1
12 | import Data.SortedMap as SM
13 | import Derive.Prelude
15 | import Text.ILex.Char.UTF8
16 | import Text.ILex.Internal.DFA
17 | import Text.ILex.Internal.Types
20 | %language ElabReflection
23 | record Env (q : Type) (s : Type -> Type) where
29 | 0 Step1 : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
30 | Step1 q r s = s q -> F1 q (Index r)
33 | 0 Fun1 : (q : Type) -> (s : Type -> Type) -> Type -> Type
34 | Fun1 q s a = (1 sk : Env q s) -> R1 q a
37 | 0 Run1 : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
38 | Run1 q r s = Fun1 q s (Index r)
41 | data Step : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type where
42 | Run : Run1 q r s -> Step q r s
47 | toState : Index r -> Step q r s
48 | toState v = Run $
\(E _ t) => v # t
55 | -> (s : Type -> Type)
57 | Keep : Transition n q r s
58 | Done : Run1 q r s -> Transition n q r s
59 | Ignore : Transition n q r s
60 | Move : Fin (S n) -> Run1 q r s -> Transition n q r s
61 | MoveI : Fin (S n) -> Transition n q r s
62 | MoveE : Fin (S n) -> Transition n q r s
63 | Bottom : Transition n q r s
65 | move : Step q r s -> Fin (S n) -> Transition n q r s
66 | move (Run f) y = Move y f
67 | move Ign y = MoveI y
70 | done : Step q r s -> Transition n q r s
71 | done (Run f) = Done f
77 | 0 ByteStep : Nat -> (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
78 | ByteStep n q r s = IArray 256 (Transition n q r s)
82 | 0 Stepper : Nat -> (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
83 | Stepper n q r s = IArray (S n) (ByteStep n q r s)
89 | record DFA q r s where
99 | next : Stepper states q r s
106 | 0 Steps : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
107 | Steps q r s = TokenMap (Step q r s)
109 | emptyRow : ByteStep n q r s
110 | emptyRow = fill _ Bottom
112 | emptyDFA : DFA q r s
113 | emptyDFA = L 0 (fill _ emptyRow)
118 | terminals : SortedMap Nat a -> Node -> Maybe (Nat, Either a a)
119 | terminals m (N n (t::_) []) = ((n,) . Left) <$> lookup t m
120 | terminals m (N n (t::_) _) = ((n,) . Right) <$> lookup t m
121 | terminals _ _ = Nothing
123 | nonFinal : SortedMap Nat (Either a a) -> Node -> Maybe Node
125 | case lookup n.pos m of
126 | Just (Left _) => Nothing
129 | index : {n : _} -> List (Nat,Node) -> SortedMap Nat (Fin (S n))
130 | index ns = SM.fromList $
mapMaybe (\(x,n) => (n.pos,) <$> tryNatToFin x) ns
133 | SortedMap Nat (Either (Step q r s) (Step q r s))
134 | -> (index : SortedMap Nat (Fin (S n)))
135 | -> (node : (Nat,Node))
136 | -> (Nat, ByteStep n q r s)
137 | node terms index (ix, N me _ out) =
138 | (ix, fromPairs _ Bottom $
mapMaybe pair (out >>= transitions))
140 | pair : (Bits8,Nat) -> Maybe (Nat, Transition n q r s)
142 | case lookup tgt terms of
143 | Nothing => case tgt == me of
144 | True => Just (cast b, Keep)
145 | False => ((cast b,) . MoveE) <$> lookup tgt index
146 | Just (Left f) => Just (cast b, done f)
147 | Just (Right f) => case tgt == me of
148 | True => Just (cast b, Keep)
149 | False => ((cast b,) . (move f)) <$> lookup tgt index
153 | byteDFA : (m : TokenMap8 (Step q r s)) -> DFA q r s
155 | let M tms graph := assert_total $
machine (toDFA m)
156 | terms := SM.fromList (mapMaybe (terminals tms . snd) graph)
157 | nodes := zipWithIndex $
mapMaybe (nonFinal terms . snd) graph
158 | S len := length nodes | 0 => emptyDFA
160 | trans := fromPairs (S len) emptyRow (map (node terms ix) nodes)
165 | dfa : Steps q r s -> DFA q r s
166 | dfa = byteDFA . map toUTF8