Skip to content

Commit

Permalink
add string_if function (#2)
Browse files Browse the repository at this point in the history
* add string_if function

* update CHANGES.md
  • Loading branch information
bikallem authored Jul 30, 2020
1 parent 5b677e0 commit e6b2a69
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.0.2 2020-07-30 UK
--------------------
- Add string_if parser.

v1.0.1 2020-07-30 UK
--------------------
- Add sexp_of_error.
Expand Down
9 changes: 9 additions & 0 deletions lib/reparse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ let string s state =
else msgf state "%d: string \"%s\" not found" state.offset s
| None -> msgf state "%d: got EOF while parsing string \"%s\"" state.offset s

let string_if s state =
let len = String.length s in
match substring len state with
| Some s2 ->
if s = s2 then
R.map (fun (state, ()) -> (state, Some s)) (advance len state)
else R.ok (state, None)
| None -> R.ok (state, None)

let rec skip_while f state =
match satisfy f state with
| Ok (state, _) -> skip_while f state
Expand Down
6 changes: 5 additions & 1 deletion lib/reparse.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ val char : char -> (char, [> error ]) t
Otherwise.*)

val char_if : (char -> bool) -> (char option, [> error ]) t
(** [char_if f] accepts and returns a [Some c] if [f c] is true. Otherwise it
(** [char_if f] accepts and returns [Some c] if [f c] is true. Otherwise it
returns [None]. Always succeeds. *)

val satisfy : (char -> bool) -> (char, [> error ]) t
Expand All @@ -72,6 +72,10 @@ val peek_string : int -> (string option, [> error ]) t
val string : string -> (string, [> error ]) t
(** [string s] accepts [s] exactly and returns it. *)

val string_if : string -> (string option, [> error ]) t
(** [string_if s] accepts and returns [Some s] if [s] matches input. Otherwise
it returns false. Always succeeds. *)

val skip_while : (char -> bool) -> (unit, [> error ]) t
(** [skip_while f] keeps accepting [c] if [f c] is [true]. [c] is discarded.
Always succeeds. *)
Expand Down

0 comments on commit e6b2a69

Please sign in to comment.