4 | import public Data.List1
6 | import public Text.Parser.Core
7 | import public Text.Quantity
8 | import public Text.Token
14 | match : TokenKind k => Eq k =>
16 | Grammar state (Token k) True (TokType kind)
17 | match k = terminal "Unrecognised input" $
18 | \t => if t.kind == k
19 | then Just $
tokValue k t.text
25 | option : {c : Bool} ->
26 | (def : a) -> (p : Grammar state tok c a) ->
27 | Grammar state tok False a
28 | option {c = False} def p = p <|> pure def
29 | option {c = True} def p = p <|> pure def
34 | optional : {c : Bool} ->
35 | (p : Grammar state tok c a) ->
36 | Grammar state tok False (Maybe a)
37 | optional p = option Nothing (map Just p)
43 | choose : {c1, c2 : Bool} ->
44 | (l : Grammar state tok c1 a) ->
45 | (r : Grammar state tok c2 b) ->
46 | Grammar state tok (c1 && c2) (Either a b)
47 | choose l r = map Left l <|> map Right r
53 | choiceMap : {c : Bool} ->
54 | (a -> Grammar state tok c b) ->
55 | Foldable t => t a ->
56 | Grammar state tok c b
57 | choiceMap {c} f xs = foldr (\x, acc => rewrite sym (andSameNeutral c) in
59 | (fail "No more options") xs
66 | choice : Foldable t =>
68 | t (Grammar state tok c a) ->
69 | Grammar state tok c a
70 | choice = choiceMap id
75 | some : Grammar state tok True a ->
76 | Grammar state tok True (List1 a)
77 | some p = pure (!p ::: !(many p))
81 | many : Grammar state tok True a ->
82 | Grammar state tok False (List a)
83 | many p = option [] (forget <$> some p)
87 | count1 : (q : Quantity) ->
88 | (p : Grammar state tok True a) ->
89 | Grammar state tok True (List a)
90 | count1 q p = do x <- p
92 | (\xs => pure (x :: xs))
96 | count : (q : Quantity) ->
97 | (p : Grammar state tok True a) ->
98 | Grammar state tok (isSucc (min q)) (List a)
99 | count (Qty Z Nothing) p = many p
100 | count (Qty Z (Just Z)) _ = pure []
101 | count (Qty Z (Just (S max))) p = option [] $
count1 (atMost max) p
102 | count (Qty (S min) Nothing) p = count1 (atLeast min) p
103 | count (Qty (S min) (Just Z)) _ = fail "Quantity out of order"
104 | count (Qty (S min) (Just (S max))) p = count1 (between (S min) max) p
110 | someTill : {c : Bool} ->
111 | (end : Grammar state tok c e) ->
112 | (p : Grammar state tok True a) ->
113 | Grammar state tok True (List1 a)
114 | someTill {c} end p = do x <- p
115 | seq (manyTill end p)
116 | (\xs => pure (x ::: xs))
121 | manyTill : {c : Bool} ->
122 | (end : Grammar state tok c e) ->
123 | (p : Grammar state tok True a) ->
124 | Grammar state tok c (List a)
125 | manyTill {c} end p = rewrite sym (andTrueNeutral c) in
126 | map (const []) end <|> (forget <$> someTill end p)
132 | afterSome : {c : Bool} ->
133 | (skip : Grammar state tok True s) ->
134 | (p : Grammar state tok c a) ->
135 | Grammar state tok True a
136 | afterSome skip p = do ignore $
skip
142 | afterMany : {c : Bool} ->
143 | (skip : Grammar state tok True s) ->
144 | (p : Grammar state tok c a) ->
145 | Grammar state tok c a
146 | afterMany {c} skip p = rewrite sym (andTrueNeutral c) in
147 | p <|> afterSome skip p
151 | sepBy1 : {c : Bool} ->
152 | (sep : Grammar state tok True s) ->
153 | (p : Grammar state tok c a) ->
154 | Grammar state tok c (List1 a)
155 | sepBy1 {c} sep p = rewrite sym (orFalseNeutral c) in
156 | [| p ::: many (sep *> p) |]
161 | sepBy : {c : Bool} ->
162 | (sep : Grammar state tok True s) ->
163 | (p : Grammar state tok c a) ->
164 | Grammar state tok False (List a)
165 | sepBy sep p = option [] $
forget <$> sepBy1 sep p
170 | sepEndBy1 : {c : Bool} ->
171 | (sep : Grammar state tok True s) ->
172 | (p : Grammar state tok c a) ->
173 | Grammar state tok c (List1 a)
174 | sepEndBy1 {c} sep p = rewrite sym (orFalseNeutral c) in
175 | sepBy1 sep p <* optional sep
180 | sepEndBy : {c : Bool} ->
181 | (sep : Grammar state tok True s) ->
182 | (p : Grammar state tok c a) ->
183 | Grammar state tok False (List a)
184 | sepEndBy sep p = option [] $
forget <$> sepEndBy1 sep p
188 | endBy1 : {c : Bool} ->
189 | (sep : Grammar state tok True s) ->
190 | (p : Grammar state tok c a) ->
191 | Grammar state tok True (List1 a)
192 | endBy1 {c} sep p = some $
rewrite sym (orTrueTrue c) in
196 | endBy : {c : Bool} ->
197 | (sep : Grammar state tok True s) ->
198 | (p : Grammar state tok c a) ->
199 | Grammar state tok False (List a)
200 | endBy sep p = option [] $
forget <$> endBy1 sep p
204 | between : {c : Bool} ->
205 | (left : Grammar state tok True l) ->
206 | (right : Grammar state tok True r) ->
207 | (p : Grammar state tok c a) ->
208 | Grammar state tok True a
209 | between left right contents = left *> contents <* right
212 | location : Grammar state token False (Int, Int)
213 | location = startBounds <$> position
216 | endLocation : Grammar state token False (Int, Int)
217 | endLocation = endBounds <$> position
220 | column : Grammar state token False Int
221 | column = snd <$> location
224 | when : Bool -> Lazy (Grammar state token False ()) -> Grammar state token False ()
226 | when False y = pure ()