1 | module TyRE.Text.Parser
3 | import public Data.Bool
4 | import public Data.List
5 | import public Data.List1
6 | import public Data.Nat
7 | import public Data.Vect
9 | import public TyRE.Text.Parser.Core
10 | import public TyRE.Text.Quantity
11 | import public TyRE.Text.Token
15 | match : (Eq k, TokenKind k) =>
17 | Grammar (Token k) True (TokType kind)
18 | match kind = terminal "Unrecognised input" $
19 | \(Tok kind' text) => if kind' == kind
20 | then Just $
tokValue kind text
26 | option : {c : Bool} ->
27 | (def : a) -> (p : Grammar tok c a) ->
29 | option {c = False} def p = p <|> pure def
30 | option {c = True} def p = p <|> pure def
35 | optional : {c : _} ->
36 | (p : Grammar tok c a) ->
37 | Grammar tok False (Maybe a)
38 | optional p = option Nothing (map Just p)
44 | choose : {c1, c2 : _} ->
45 | (l : Grammar tok c1 a) ->
46 | (r : Grammar tok c2 b) ->
47 | Grammar tok (c1 && c2) (Either a b)
48 | choose l r = map Left l <|> map Right r
54 | choiceMap : {c : Bool} ->
55 | (a -> Grammar tok c b) ->
56 | Foldable t => t a ->
58 | choiceMap f xs = foldr (\x, acc => rewrite sym (andSameNeutral c) in
60 | (fail "No more options") xs
68 | Foldable t => t (Grammar tok c a) ->
70 | choice = Parser.choiceMap id
75 | some : Grammar tok True a ->
76 | Grammar tok True (List1 a)
77 | some p = pure (!p ::: !(many p))
81 | many : Grammar tok True a ->
82 | Grammar tok False (List a)
83 | many p = option [] (forget <$> some p)
88 | some' : (p : Grammar tok True a) ->
89 | Grammar tok True (xs : List a ** NonEmpty xs)
90 | some' p = pure (
!p :: !(many p) ** IsNonEmpty)
94 | count1 : (q : Quantity) ->
95 | (p : Grammar tok True a) ->
96 | Grammar tok True (List a)
97 | count1 q p = do x <- p
99 | (\xs => pure (x :: xs))
103 | count : (q : Quantity) ->
104 | (p : Grammar tok True a) ->
105 | Grammar tok (isSucc (min q)) (List a)
106 | count (Qty Z Nothing) p = many p
107 | count (Qty Z (Just Z)) _ = pure []
108 | count (Qty Z (Just (S max))) p = option [] $
count1 (atMost max) p
109 | count (Qty (S min) Nothing) p = count1 (atLeast min) p
110 | count (Qty (S min) (Just Z)) _ = fail "Quantity out of order"
111 | count (Qty (S min) (Just (S max))) p = count1 (between min max) p
115 | countExactly : (n : Nat) ->
116 | (p : Grammar tok True a) ->
117 | Grammar tok (isSucc n) (Vect n a)
118 | countExactly Z p = Empty []
119 | countExactly (S k) p = [| p :: countExactly k p |]
125 | someTill : {c : Bool} ->
126 | (end : Grammar tok c e) ->
127 | (p : Grammar tok True a) ->
128 | Grammar tok True (List1 a)
129 | someTill {c} end p = do x <- p
130 | seq (manyTill end p)
131 | (\xs => pure (x ::: xs))
136 | manyTill : {c : Bool} ->
137 | (end : Grammar tok c e) ->
138 | (p : Grammar tok True a) ->
139 | Grammar tok c (List a)
140 | manyTill {c} end p = rewrite sym (andTrueNeutral c) in
141 | map (const []) end <|> (forget <$> someTill end p)
147 | afterSome : {c : Bool} ->
148 | (skip : Grammar tok True s) ->
149 | (p : Grammar tok c a) ->
151 | afterSome skip p = do ignore skip
157 | afterMany : {c : Bool} ->
158 | (skip : Grammar tok True s) ->
159 | (p : Grammar tok c a) ->
161 | afterMany {c} skip p = rewrite sym (andTrueNeutral c) in
162 | p <|> afterSome skip p
166 | sepBy1 : {c : Bool} ->
167 | (sep : Grammar tok True s) ->
168 | (p : Grammar tok c a) ->
169 | Grammar tok c (List1 a)
170 | sepBy1 {c} sep p = rewrite sym (orFalseNeutral c) in
171 | [| p ::: many (sep *> p) |]
176 | sepBy : {c : Bool} ->
177 | (sep : Grammar tok True s) ->
178 | (p : Grammar tok c a) ->
179 | Grammar tok False (List a)
180 | sepBy sep p = option [] $
forget <$> sepBy1 sep p
185 | sepEndBy1 : {c : Bool} ->
186 | (sep : Grammar tok True s) ->
187 | (p : Grammar tok c a) ->
188 | Grammar tok c (List1 a)
189 | sepEndBy1 {c} sep p = rewrite sym (orFalseNeutral c) in
190 | sepBy1 sep p <* optional sep
195 | sepEndBy : {c : Bool} ->
196 | (sep : Grammar tok True s) ->
197 | (p : Grammar tok c a) ->
198 | Grammar tok False (List a)
199 | sepEndBy sep p = option [] $
forget <$> sepEndBy1 sep p
203 | endBy1 : {c : Bool} ->
204 | (sep : Grammar tok True s) ->
205 | (p : Grammar tok c a) ->
206 | Grammar tok True (List1 a)
207 | endBy1 sep p = some $
rewrite sym (orTrueTrue c) in
211 | endBy : {c : Bool} ->
212 | (sep : Grammar tok True s) ->
213 | (p : Grammar tok c a) ->
214 | Grammar tok False (List a)
215 | endBy sep p = option [] $
forget <$> endBy1 sep p
219 | between : {c : _} ->
220 | (left : Grammar tok True l) ->
221 | (right : Inf (Grammar tok True r)) ->
222 | (p : Inf (Grammar tok c a)) ->
224 | between left right contents = left *> contents <* right