@@ -3,6 +3,7 @@ module Text.Parser
3import Data.Bool.Extra
3import Data.Bool.Extra
5import public Text.Parser.Core
5import public Text.Parser.Core
6+import public Text.Quantity
7%access export
8%access export
8%default total
9%default total
@@ -12,8 +13,8 @@ import public Text.Parser.Core
12option : {c : Bool} ->
13option : {c : Bool} ->
13 (def : a) -> (p : Grammar tok c a) ->
14 (def : a) -> (p : Grammar tok c a) ->
14 Grammar tok False a
15 Grammar tok False a
15−option {c} def p = rewrite sym (andFalseFalse c) in
16+option {c = False} def p = p <|> pure def
16− p <|> pure def
17+option {c = True} def p = p <|> pure def
18||| Optionally parse a thing.
19||| Optionally parse a thing.
19||| To provide a default value, use `option` instead.
20||| To provide a default value, use `option` instead.
@@ -63,6 +64,26 @@ some' : (p : Grammar tok True a) ->
63 Grammar tok True (xs : List a ** NonEmpty xs)
64 Grammar tok True (xs : List a ** NonEmpty xs)
64some' p = pure (!p :: !(many p) ** IsNonEmpty)
65some' p = pure (!p :: !(many p) ** IsNonEmpty)
69+ count1 : (q : Quantity) ->
70+ (p : Grammar tok True a) ->
71+ Grammar tok True (List a)
72+ count1 q p = do x <- p
74+ (\xs => pure (x :: xs))
76+ ||| Parse `p`, repeated as specified by `q`, returning the list of values.
77+ count : (q : Quantity) ->
78+ (p : Grammar tok True a) ->
79+ Grammar tok (isSucc (min q)) (List a)
80+ count (Qty Z Nothing) p = many p
81+ count (Qty Z (Just Z)) _ = pure []
82+ count (Qty Z (Just (S max))) p = option [] $ count1 (atMost max) p
83+ count (Qty (S min) Nothing) p = count1 (atLeast min) p
84+ count (Qty (S min) (Just Z)) _ = fail "Quantity out of order"
85+ count (Qty (S min) (Just (S max))) p = count1 (between (S min) max) p
67 ||| Parse one or more instances of `p` until `end` succeeds, returning the
88 ||| Parse one or more instances of `p` until `end` succeeds, returning the
68 ||| list of values from `p`. Guaranteed to consume input.
89 ||| list of values from `p`. Guaranteed to consume input.