module CCParse:sig
..end
status still a bit unstable, the type 'a t
might still change.
Examples:
open Containers_string.Parse;;
type tree = L of int | N of tree * tree;;
let mk_leaf x = L x
let mk_node x y = N(x,y)
let ptree = fix @@ fun self ->
skip_space *>
( (char '(' *> (pure mk_node <*> self <*> self) <* char ')')
<|>
(U.int >|= mk_leaf) )
;;
parse_string_exn "(1 (2 3))" ptree;;
parse_string_exn "((1 2) (3 (4 5)))" ptree;;
open Containers_string.Parse;;
let p = U.list ~sep:"," U.word;;
parse_string_exn "[abc , de, hello ,world ]" p;;
let p = CCParse.(U.list ~sep:"," U.int);;
let l = CCList.(1 -- 100_000);;
let l_printed =
CCFormat.to_string (CCList.print ~sep:"," ~start:"[" ~stop:"]" CCInt.print) l;;
let l' = CCParse.parse_string_exn ~p l_printed;;
assert (l=l');;
type'a
or_error =[ `Error of string | `Ok of 'a ]
typeline_num =
int
typecol_num =
int
exception ParseError of line_num * col_num * (unit -> string)
This type changed at 0.13
module MemoTbl:sig
..end
type
input = {
|
is_done : |
(* |
End of input?
| *) |
|
cur : |
(* |
Current char
| *) |
|
next : |
(* |
Returns current char;
if not
is_done , move to next char,
otherwise throw ParseError | *) |
|
pos : |
(* |
Current pos
| *) |
|
lnum : |
(* |
Line number
Since 0.13 | *) |
|
cnum : |
(* |
Column number
Since 0.13 | *) |
|
memo : |
(* |
Memoization table, if any
| *) |
|
backtrack : |
(* |
Restore to previous pos
| *) |
|
sub : |
(* | sub pos len extracts slice from pos with len | *) |
val input_of_string : string -> input
val input_of_chan : ?size:int -> Pervasives.in_channel -> input
input_of_chan ic
reads lazily the content of ic
as parsing goes.
All content that is read is saved to an internal buffer for backtracking.size
: number of bytes read at once from ic
type'a
t =input -> ok:('a -> unit) -> err:(exn -> unit) -> unit
ok
to call with the result when it's doneerr
to call when the parser met an errorParseError
in case of failureval return : 'a -> 'a t
val pure : 'a -> 'a t
CCParse.return
val (>|=) : 'a t -> ('a -> 'b) -> 'b t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b t
val (<*>) : ('a -> 'b) t -> 'a t -> 'b t
val ( <* ) : 'a t -> 'b t -> 'a t
a <* b
parses a
into x
, parses b
and ignores its result,
and returns x
val ( *> ) : 'b t -> 'a t -> 'a t
a *> b
parses a
, then parses b
into x
, and returns x
. The
results of a
is ignored.val fail : string -> 'a t
fail msg
fails with the given message. It can trigger a backtrackval eoi : unit t
val nop : unit t
()
val char : char -> char t
char c
parses the char c
and nothing elseval char_if : (char -> bool) -> char t
char_if f
parses a character c
if f c = true
val chars_if : (char -> bool) -> string t
chars_if f
parses a string of chars that satisfy f
val chars1_if : (char -> bool) -> string t
CCParse.chars_if
, but only non-empty stringsval endline : char t
val space : char t
val white : char t
val skip_chars : (char -> bool) -> unit t
val skip_space : unit t
val skip_white : unit t
val is_alpha : char -> bool
val is_num : char -> bool
val is_alpha_num : char -> bool
val is_space : char -> bool
val is_white : char -> bool
val (~~~) : (char -> bool) -> char -> bool
val (|||) : (char -> bool) -> (char -> bool) -> char -> bool
val (&&&) : (char -> bool) -> (char -> bool) -> char -> bool
val (<|>) : 'a t -> 'a t -> 'a t
a <|> b
tries to parse a
, and if a
fails, backtracks and tries
to parse b
. Therefore, it succeeds if either succeedsval string : string -> string t
string s
parses exactly the string s
, and nothing elseval many : 'a t -> 'a list t
many p
parses a list of p
, eagerly (as long as possible)val many1 : 'a t -> 'a list t
val skip : 'a t -> unit t
skip p
parses p
and ignores its resultval sep : by:'b t -> 'a t -> 'a list t
sep ~by p
parses a list of p
separated by by
val sep1 : by:'b t -> 'a t -> 'a list t
sep1 ~by p
parses a non empty list of p
, separated by by
val fix : ('a t -> 'a t) -> 'a t
val memo : 'a t -> 'a t
memo p
will behave like p
, but when called
in a state (read: position in input) it has already processed, memo p
returns a result directly. The implementation uses an underlying
hashtable.
This can be costly in memory, but improve the run time a lot if there
is a lot of backtracking involving p
.
This function is not thread-safe.
Since 0.13
val fix_memo : ('a t -> 'a t) -> 'a t
Those functions have a label ~p
on the parser, since 0.14.
val parse : input:input -> p:'a t -> 'a or_error
parse ~input p
applies p
on the input, and returns `Ok x
if
p
succeeds with x
, or `Error s
otherwiseval parse_exn : input:input -> p:'a t -> 'a
ParseError
if it failsval parse_string : string -> p:'a t -> 'a or_error
CCParse.parse
for string inputsval parse_string_exn : string -> p:'a t -> 'a
ParseError
if it failsval parse_file : ?size:int -> file:string -> p:'a t -> 'a or_error
parse_file ~file p
parses file
with p
by opening the file
and using CCParse.input_of_chan
.size
: size of chunks read from fileval parse_file_exn : ?size:int -> file:string -> p:'a t -> 'a
module U:sig
..end