0 | module Text.ILex.DFA
  1 |
  2 | import public Data.Array
  3 | import public Data.List
  4 | import public Text.ILex.RExp
  5 |
  6 | import Data.Linear.Traverse1
  7 | import Control.Monad.State
  8 |
  9 | import Data.SortedMap as SM
 10 |
 11 | import Text.ILex.Char.UTF8
 12 | import Text.ILex.Internal.DFA
 13 | import Text.ILex.Internal.Types
 14 |
 15 | %default total
 16 |
 17 | public export
 18 | data Trans : Type -> Type where
 19 |   Run : (res : a) -> Trans a
 20 |   Ign : Trans a
 21 |   Err : Trans a
 22 |
 23 | public export
 24 | data Transition : (n : Nat) -> Type -> Type where
 25 |   Keep   : Transition n a
 26 |   Done   : (val : a) -> Transition n a
 27 |   Ignore : Transition n a
 28 |   Move   : Fin (S n) -> (val : a) -> Transition n a
 29 |   MoveI  : Fin (S n) -> Transition n a
 30 |   MoveE  : Fin (S n) -> Transition n a
 31 |   Bottom : Transition n a
 32 |
 33 | move : Trans a -> Fin (S n) -> Transition n a
 34 | move (Run f) y = Move y f
 35 | move Ign     y = MoveI y
 36 | move Err     y = Bottom
 37 |
 38 | done : Trans a -> Transition n a
 39 | done (Run f) = Done f
 40 | done Ign     = Ignore
 41 | done Err     = Bottom
 42 |
 43 | ||| An array of arrays describing a lexer's state machine.
 44 | public export
 45 | 0 ByteTrans : Nat -> Type -> Type
 46 | ByteTrans n a = IArray 256 (Transition n a)
 47 |
 48 | ||| An array of arrays describing a lexer's state machine.
 49 | public export
 50 | 0 Machine : Nat -> Type -> Type
 51 | Machine n a = IArray (S n) (ByteTrans n a)
 52 |
 53 | ||| A discrete finite automaton (DFA) encoded as
 54 | ||| an array of state transitions plus an array
 55 | ||| describing the terminal token states.
 56 | public export
 57 | record DFA a where
 58 |   constructor L
 59 |   ||| Number of non-zero states in the automaton.
 60 |   states : Nat
 61 |
 62 |   ||| State transitions matrix.
 63 |   |||
 64 |   ||| If `cur` is the current state (encoded as a `Fin (S states)`
 65 |   ||| and `b` is the current input byte, the next state is determined
 66 |   ||| by `next[cur][b]` (in pseudo C-syntax).
 67 |   next   : Machine states a
 68 |
 69 | --------------------------------------------------------------------------------
 70 | -- Lexer Generator
 71 | --------------------------------------------------------------------------------
 72 |
 73 | public export
 74 | 0 Results : Type -> Type
 75 | Results a = TokenMap (Trans a)
 76 |
 77 | emptyRow : ByteTrans n a
 78 | emptyRow = fill _ Bottom
 79 |
 80 | emptyDFA : DFA a
 81 | emptyDFA = L 0 (fill _ emptyRow)
 82 |
 83 | -- Extracts the terminal state of a node
 84 | -- `Left` : This is a final node with no more state transitions
 85 | -- `Right`: This node has additional state transitions
 86 | terminals : SortedMap Nat a -> Node -> Maybe (Nat, Either a a)
 87 | terminals m (N n (t::_) []) = ((n,) . Left) <$> lookup t m
 88 | terminals m (N n (t::_) _)  = ((n,) . Right) <$> lookup t m
 89 | terminals _ _               = Nothing
 90 |
 91 | nonFinal : SortedMap Nat (Either a a) -> Node -> Maybe Node
 92 | nonFinal m n =
 93 |   case lookup n.pos m of
 94 |     Just (Left _) => Nothing
 95 |     _             => Just n
 96 |
 97 | index : {n : _} -> List (Nat,Node) -> SortedMap Nat (Fin (S n))
 98 | index ns = SM.fromList $ mapMaybe (\(x,n) => (n.pos,) <$> tryNatToFin x) ns
 99 |
100 | node :
101 |      SortedMap Nat (Either (Trans a) (Trans a))
102 |   -> (index : SortedMap Nat (Fin (S n)))
103 |   -> (node  : (Nat,Node))
104 |   -> (Nat, ByteTrans n a)
105 | node terms index (ix, N me _ out) =
106 |   (ix, fromPairs _ Bottom $ mapMaybe pair (out >>= transitions))
107 |   where
108 |     pair : (Bits8,Nat) -> Maybe (Nat, Transition n a)
109 |     pair (b,tgt) =
110 |       case lookup tgt terms of
111 |         Nothing        => case tgt == me of
112 |           True  => Just (cast b, Keep)
113 |           False => ((cast b,) . MoveE) <$> lookup tgt index
114 |         Just (Left f)  => Just (cast b, done f)
115 |         Just (Right f) => case tgt == me of
116 |           True  => Just (cast b, Keep)
117 |           False => ((cast b,) . (move f)) <$> lookup tgt index
118 |
119 | ||| A DFA operating on raw bytes.
120 | export
121 | byteDFA : (m : TokenMap8 (Trans a)) -> DFA a
122 | byteDFA m =
123 |   let M tms graph := assert_total $ machine (toDFA m)
124 |       terms       := SM.fromList (mapMaybe (terminals tms . snd) graph)
125 |       nodes       := zipWithIndex $ mapMaybe (nonFinal terms . snd) graph
126 |       S len       := length nodes | 0 => emptyDFA
127 |       ix          := index nodes
128 |       trans       := fromPairs (S len) emptyRow (map (node terms ix) nodes)
129 |    in L len trans
130 |
131 | ||| A utf-8 aware DFA operating on text.
132 | export
133 | dfa : Results a -> DFA a
134 | dfa = byteDFA . map toUTF8
135 |