0 | module Text.ILex.Runner
3 | import Text.ILex.Internal.Runner
4 | import public Text.ParseError
5 | import public Text.FC
6 | import public Text.ILex.Parser
12 | -> (l : Parser1 e a)
14 | -> {auto x : Ix pos n}
20 | run : {n : _} -> Parser1 e a -> IBuffer n -> Either e a
21 | run l buf = runFrom l n buf
25 | runString : Parser1 e a -> String -> Either e a
26 | runString l s = run l (fromString s)
30 | runBytes : Parser1 e a -> ByteString -> Either e a
31 | runBytes l (BS s $
BV buf off lte) =
32 | runFrom l s {x = offsetToIx off} (take (off+s) buf)
40 | -> Parser1 (BoundedErr e) a
43 | -> Either (ParseError e) a
44 | parse l o buf = mapFst (toParseError o (toString buf 0 n)) (run l buf)
49 | Parser1 (BoundedErr e) a
52 | -> Either (ParseError e) a
53 | parseString l o s = parse l o (fromString s)
58 | Parser1 (BoundedErr e) a
61 | -> Either (ParseError e) a
62 | parseBytes l o bs = mapFst (toParseError o (toString bs)) (runBytes l bs)
68 | parameters {0 q,e,a : Type}
73 | (bytes : Ref q ByteString)
77 | -> (dfa : PStepper k parser)
78 | -> (cur : PByteStep k parser)
81 | -> {auto x : Ix pos n}
82 | -> {auto 0 lte2 : LTE (ixToNat from) (ixToNat x)}
83 | -> F1 q (Either e a)
87 | -> (dfa : PStepper k parser)
88 | -> (cur : PByteStep k parser)
89 | -> (last : PStep parser)
92 | -> {auto x : Ix pos n}
93 | -> {auto 0 lte2 : LTE (ixToNat from) (ixToNat x)}
94 | -> F1 q (Either e a)
96 | loop : (st : PIx parser) -> (pos : Nat) -> (x : Ix pos n) => F1 q (Either e a)
98 | let _ # t := write1 bytes "" t
99 | in parser.eoi st stck t
101 | let L _ dfa := parser.lex `at` st
103 | in case cur `atByte` (buf `ix` k) of
104 | Done f => let s2 # t := f (stck # t) in loop s2 k t
105 | Move nxt f => succ st dfa (dfa `at` nxt) f x k t
106 | MoveE nxt => step st dfa (dfa `at` nxt) x k t
108 | let _ # t := writeBS buf x k bytes t
109 | s2 # t := f (stck # t)
112 | let _ # t := writeBS buf x k bytes t
113 | in fail parser st stck t
115 | succ st dfa cur f from 0 t =
116 | let _ # t := writeBS buf from 0 bytes t
117 | in lastStep parser f st stck t
118 | succ st dfa cur f from (S k) t =
119 | let byte := buf `ix` k
120 | in case cur `atByte` byte of
121 | Keep => succ st dfa cur f from k t
122 | Done f => let s2 # t := f (stck # t) in loop s2 k t
123 | Move nxt f => succ st dfa (dfa `at` nxt) f from k t
124 | MoveE nxt => step st dfa (dfa `at` nxt) from k t
126 | let _ # t := writeBS buf from k bytes t
127 | s2 # t := f (stck # t)
130 | let _ # t := writeBS buf from (S k) bytes t
131 | s2 # t := f (stck # t)
134 | step st dfa cur from 0 t =
135 | let _ # t := writeBS buf from 0 bytes t
136 | in fail parser st stck t
137 | step st dfa cur from (S k) t =
138 | let byte := buf `ix` k
139 | in case cur `atByte` byte of
140 | Keep => step st dfa cur from k t
141 | Done f => let s2 # t := f (stck # t) in loop s2 k t
142 | Move nxt f => succ st dfa (dfa `at` nxt) f from k t
143 | MoveE nxt => step st dfa (dfa `at` nxt) from k t
145 | let _ # t := writeBS buf from k bytes t
146 | s2 # t := f (stck # t)
149 | let _ # t := writeBS buf from k bytes t
150 | in fail parser st stck t
152 | runFrom p pos buf = run1 (go p)
154 | go : P1 q e a -> F1 q (Either e a)
156 | let x # t := stck p t
157 | in loop p x buf (bytes @{p.hasb} x) p.init pos t