0 | module Text.ILex.Lexer
2 | import public Data.Array
3 | import public Data.List
4 | import public Data.Prim.Bits32
5 | import public Text.ILex.RExp
7 | import Control.Monad.State
9 | import Data.ByteString
10 | import Data.Linear.Traverse1
11 | import Data.SortedMap as SM
12 | import Derive.Prelude
14 | import Text.ILex.Char.UTF8
15 | import Text.ILex.Internal.DFA
16 | import Text.ILex.Internal.Types
19 | %language ElabReflection
22 | record Index (n : Bits32) where
25 | {auto 0 prf : val < n}
27 | %runElab deriveIndexed "Index" [Show,Eq,Ord]
30 | toIndex : {r : _} -> Bits32 -> Maybe (Index r)
34 | Just0 v => Just (I n)
37 | fromInteger : (n : Integer) -> (0 p : cast n < r) => Index r
38 | fromInteger n = I (cast n)
41 | Ini : (0 prf : 0 < n) => Index n
45 | 0 Step1 : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
46 | Step1 q r s = (1 sk : R1 q (s q)) -> R1 q (Index r)
49 | toState : Index r -> Step1 q r s
50 | toState v = \(_ # t) => v # t
57 | -> (s : Type -> Type)
59 | Keep : Transition n q r s
60 | Done : Step1 q r s -> Transition n q r s
61 | DoneBS : Step1 q r s -> Transition n q r s
62 | Move : Fin (S n) -> Step1 q r s -> Transition n q r s
63 | MoveE : Fin (S n) -> Transition n q r s
64 | Bottom : Transition n q r s
68 | 0 ByteStep : Nat -> (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
69 | ByteStep n q r s = IArray 256 (Transition n q r s)
73 | 0 Stepper : Nat -> (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
74 | Stepper n q r s = IArray (S n) (ByteStep n q r s)
80 | record DFA q r s where
90 | next : Stepper states q r s
97 | data Step : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type where
98 | Go : Step1 q r s -> Step q r s
99 | Rd : Step1 q r s -> Step q r s
102 | 0 Steps : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
103 | Steps q r s = TokenMap (Step q r s)
105 | emptyRow : ByteStep n q r s
106 | emptyRow = fill _ Bottom
108 | emptyDFA : DFA q r s
109 | emptyDFA = L 0 (fill _ emptyRow)
114 | terminals : SortedMap Nat a -> Node -> Maybe (Nat, Either a a)
115 | terminals m (N n (t::_) []) = ((n,) . Left) <$> lookup t m
116 | terminals m (N n (t::_) _) = ((n,) . Right) <$> lookup t m
117 | terminals _ _ = Nothing
119 | nonFinal : SortedMap Nat (Either a a) -> Node -> Maybe Node
121 | case lookup n.pos m of
122 | Just (Left _) => Nothing
125 | index : {n : _} -> List (Nat,Node) -> SortedMap Nat (Fin (S n))
126 | index ns = SM.fromList $
mapMaybe (\(x,n) => (n.pos,) <$> tryNatToFin x) ns
129 | SortedMap Nat (Either (Step q r s) (Step q r s))
130 | -> (index : SortedMap Nat (Fin (S n)))
131 | -> (node : (Nat,Node))
132 | -> (Nat, ByteStep n q r s)
133 | node terms index (ix, N me _ out) =
134 | (ix, fromPairs _ Bottom $
mapMaybe pair (out >>= transitions))
136 | pair : (Bits8,Nat) -> Maybe (Nat, Transition n q r s)
138 | case lookup tgt terms of
139 | Nothing => case tgt == me of
140 | True => Just (cast b, Keep)
141 | False => ((cast b,) . MoveE) <$> lookup tgt index
142 | Just (Left $
Go f) => Just (cast b, Done f)
143 | Just (Left $
Rd f) => Just (cast b, DoneBS f)
144 | Just (Right $
Go f) => case tgt == me of
145 | True => Just (cast b, Keep)
146 | False => ((cast b,) . (`Move` f)) <$> lookup tgt index
147 | Just (Right $
Rd 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