0 | module Text.ILex.Debug
2 | import Control.Monad.State
3 | import Data.ByteString
4 | import Data.Linear.Traverse1
5 | import Data.SortedMap
8 | import Language.Reflection.Pretty
10 | import Text.ILex.Char.UTF8
11 | import Text.ILex.Stack
12 | import Text.ILex.Internal.DFA
13 | import Text.ILex.Internal.ENFA
14 | import Text.ILex.Internal.NFA
15 | import Text.ILex.Internal.Types
16 | import Text.ILex.Parser
17 | import Text.ILex.RExp
22 | appLst : {d : _} -> Doc d -> List (Doc d) -> Doc d
24 | appLst nm ds = nm `vappend` (indent 2 $
vsep ds)
27 | strLst : {d : _} -> String -> List (Doc d) -> Doc d
28 | strLst = appLst . line
31 | prettyNats : {d : _} -> List Nat -> Doc d
32 | prettyNats [] = line ""
33 | prettyNats [n] = line (show n)
34 | prettyNats (n::ns) = line (show n) <+> comma <+> prettyNats ns
39 | let l := lowerBound r
42 | in if l > u then line "<empty>"
43 | else if l == u then line (show l)
44 | else line "\{show l}-\{show u}"
47 | prettyEdge : {d : _} -> Edge -> Doc d
48 | prettyEdge (E r tgt) = pretty r <+> colon <++> line (show tgt)
51 | prettyENode : {d : _} -> (Nat,ENode) -> Doc d
52 | prettyENode (n, EN accs eps ds) =
53 | appLst (line "Node" <++> pretty n)
54 | [ line "acc: " <+> prettyNats accs
55 | , line "eps: " <+> prettyNats eps
56 | , strLst "deltas: " (map prettyEdge ds)
60 | prettyNEdge : {d : _} -> NEdge -> Doc d
61 | prettyNEdge (NE r tgts) = pretty r <+> colon <++> line (show tgts)
64 | prettyNNode : {d : _} -> (Nat,NNode) -> Doc d
65 | prettyNNode (n, NN _ accs ds) =
66 | appLst (line "Node" <++> pretty n)
67 | [ line "acc: " <+> prettyNats accs
68 | , strLst "deltas: " (map prettyNEdge ds)
72 | prettyNode : {d : _} -> (Nat,Node) -> Doc d
73 | prettyNode (n, N _ acc ds) =
74 | appLst (line "Node" <++> pretty n)
75 | [ line "acc: " <+> prettyNats acc
76 | , strLst "deltas: " (map prettyEdge ds)
80 | Pretty (List (Nat,ENode)) where
81 | prettyPrec p g = strLst "graph:" (map prettyENode g)
84 | Pretty (List (Nat,NNode)) where
85 | prettyPrec p g = strLst "graph:" (map prettyNNode g)
88 | Pretty (List (Nat,Node)) where
89 | prettyPrec p g = strLst "graph:" (map prettyNode g)
91 | terminal : Pretty a => {d : _} -> (Nat, a) -> Doc d
92 | terminal (n,c) = line (show n) <+> colon <++> pretty c
95 | Pretty a => Pretty b => Pretty (Machine a b) where
96 | prettyPrec p (M sm g) =
98 | [ appLst (line "Terminals") (map terminal $
SortedMap.toList sm)
103 | prettyENFA : Pretty a => TokenMap8 a -> IO ()
104 | prettyENFA tm = putPretty $
machine $
toENFA tm
107 | prettyNFA : Pretty a => TokenMap8 a -> IO ()
108 | prettyNFA tm = putPretty $
machine $
toNFA tm
111 | prettyDFA : Pretty a => TokenMap8 a -> IO ()
112 | prettyDFA tm = putPretty $
machine $
toDFA tm
118 | prettyByte : {d : _} -> Nat -> Doc d
119 | prettyByte n = line "\{pre} 0x\{toHex $ cast n}"
123 | case n >= 128 || Prelude.isControl (cast n) of
125 | False => "'\{String.singleton $ cast n}'"
128 | Pretty a => Pretty (Tok e a) where
129 | prettyPrec _ Ignore = line "<ignore>"
130 | prettyPrec _ (Const x) = pretty x
131 | prettyPrec _ (Txt f) = line "<Txt>"
132 | prettyPrec _ (Bytes f) = line "<Bytes>"
134 | prettyByteStep : {d : _} -> (Nat, ByteStep n q r s) -> Doc d
135 | prettyByteStep (x,bs) =
138 | , indent 2 $
vsep (mapMaybe trans $
zipWithIndex $
toList bs)
142 | trans : (Nat, Transition n q r s) -> Maybe (Doc d)
145 | Keep => Just (prettyByte byte <+> colon <++> line "stay")
146 | Done y => Just (prettyByte byte <+> colon <++> line "done")
147 | DoneBS y => Just (prettyByte byte <+> colon <++> line "done with bytes")
148 | Move y z => Just (prettyByte byte <+> colon <++> line "move (\{show y})")
149 | MoveE y => Just (prettyByte byte <+> colon <++> line "move non-terminal (\{show y})")
153 | Pretty (DFA q r s) where
154 | prettyPrec p (L _ next) =
155 | vsep $
prettyByteStep <$> zipWithIndex (toList next)
158 | prettyLexer : DFA q r s -> IO ()
159 | prettyLexer dfa = putPretty dfa
163 | {default False details : Bool}
164 | -> (p : P1 World e a)
165 | -> (PIx p -> String)
167 | prettyParser p shw = go 0 0
169 | go : Nat -> Bits32 -> IO ()
171 | case lt v p.states of
173 | let lx := p.lex `at` I v
175 | False => Prelude.do
176 | putStrLn "\{shw $ I v} (\{show $ S lx.states} states)"
177 | go (tot + lx.states) (assert_smaller v $
v+1)
179 | putStrLn "\{shw $ I v} (\{show $ S lx.states} states): "
182 | go (tot + lx.states) (assert_smaller v $
v+1)
183 | Nothing0 => putStrLn "Total: \{show tot} state transitions"