Skip to content

Commit

Permalink
Merge pull request #35 from anuragsoni/update-parser
Browse files Browse the repository at this point in the history
discard result of Match
  • Loading branch information
anuragsoni authored May 4, 2019
2 parents 4ed3637 + cceb2fd commit f832d5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
27 changes: 13 additions & 14 deletions src/parser.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type 'a t =
| Return : 'a -> 'a t
| Empty : unit t
| Match : string -> string t
| Match : string -> unit t
| Apply : ('a -> 'b) t * 'a t -> 'b t
| SkipLeft : 'a t * 'b t -> 'b t
| SkipRight : 'a t * 'b t -> 'a t
Expand All @@ -21,6 +21,17 @@ let int64 = Int64
let bool = Bool
let str = Str
let empty = Empty
let choice ps = Choice ps

module Infix = struct
let ( <*> ) = apply
let ( </> ) = apply
let ( <$> ) f p = Apply (Return f, p)
let ( *> ) x y = SkipLeft (x, y)
let ( <* ) x y = SkipRight (x, y)
let ( <$ ) f t = SkipRight (Return f, t)
let ( <|> ) p1 p2 = choice [ p1; p2 ]
end

let verify f params =
match params with
Expand All @@ -45,7 +56,7 @@ let rec parse : type a. a t -> string list -> (a * string list) option =
(match params with
| [] -> Some ((), params)
| _ -> None)
| Match s -> verify (fun w -> if String.compare w s = 0 then Some w else None) params
| Match s -> verify (fun w -> if String.compare w s = 0 then Some () else None) params
| Int -> verify int_of_string_opt params
| Int32 -> verify Int32.of_string_opt params
| Int64 -> verify Int64.of_string_opt params
Expand Down Expand Up @@ -77,15 +88,3 @@ let rec parse : type a. a t -> string list -> (a * string list) option =
| None -> parse (Choice ps) params
| res -> res))
;;

let choice ps = Choice ps

module Infix = struct
let ( <*> ) = apply
let ( </> ) = apply
let ( <$> ) f p = Apply (Return f, p)
let ( *> ) x y = SkipLeft (x, y)
let ( <* ) x y = SkipRight (x, y)
let ( <$ ) f t = SkipRight (Return f, t)
let ( <|> ) p1 p2 = choice [ p1; p2 ]
end
5 changes: 3 additions & 2 deletions src/routes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ val apply : ('a -> 'b) t -> 'a t -> 'b t
a ['a t] context to a path param parser.
f <*> p is the same as f >>= fun f -> map ~f p *)

val s : string -> string t
(** [s word] returns a path parser that matches [word] exactly. *)
val s : string -> unit t
(** [s word] returns a path parser that matches [word] exactly and then
discards the result. *)

val int : int t
(** [int] parses a path parmeter and succeeds if its an integer. *)
Expand Down

0 comments on commit f832d5e

Please sign in to comment.