module CCParse:sig..end
open CCParse;;
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 *>
( (try_ (char '(') *> (pure mk_node <*> self <*> self) <* char ')')
<|>
(U.int >|= mk_leaf) )
;;
parse_string_exn ptree "(1 (2 3))" ;;
parse_string_exn ptree "((1 2) (3 (4 5)))" ;;
open Containers.Parse;;
let p = U.list ~sep:"," U.word;;
parse_string_exn p "[abc , de, hello ,world ]";;
let p = CCParse.(U.list ~sep:"," U.int);;
let l = CCList.(1 -- 100_000);;
let l_printed =
CCFormat.(to_string (within "[" "]" (list ~sep:(return ",@,") int))) l;;
let l' = CCParse.parse_string_exn p l_printed;;
assert (l=l');;
type'aor_error =('a, string) Result.result
typeline_num =int
typecol_num =int
type parse_branch
val string_of_branch : parse_branch -> string
exception ParseError of parse_branch * (unit -> string)
type position
type state
val state_of_string : string -> statetype'at =state -> 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 tval pure : 'a -> 'a tCCParse.returnval (>|=) : 'a t -> ('a -> 'b) -> 'b tval map : ('a -> 'b) -> 'a t -> 'b t
val map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c t
val map3 : ('a -> 'b -> 'c -> 'd) ->
'a t -> 'b t -> 'c t -> 'd t
val (>>=) : 'a t -> ('a -> 'b t) -> 'b tval (<*>) : ('a -> 'b) t -> 'a t -> 'b tval ( <* ) : 'a t -> 'b t -> 'a ta <* b parses a into x, parses b and ignores its result,
and returns xval ( *> ) : 'b t -> 'a t -> 'a ta *> b parses a, then parses b into x, and returns x. The
results of a is ignored.val fail : string -> 'a tfail msg fails with the given message. It can trigger a backtrackval failf : ('a, unit, string, 'b t) Pervasives.format4 -> 'a
val parsing : string -> 'a t -> 'a tparsing s p behaves the same as p, with the information that
we are parsing s, if p failsval eoi : unit tval nop : unit t()val char : char -> char tchar c parses the char c and nothing elseval char_if : (char -> bool) -> char tchar_if f parses a character c if f c = trueval chars_if : (char -> bool) -> string tchars_if f parses a string of chars that satisfy fval chars1_if : (char -> bool) -> string tCCParse.chars_if, but only non-empty stringsval endline : char tval space : char tval white : char tval skip_chars : (char -> bool) -> unit tval skip_space : unit tval skip_white : unit tval is_alpha : char -> boolval is_num : char -> boolval is_alpha_num : char -> bool
val is_space : char -> boolval is_white : char -> boolval (<|>) : 'a t -> 'a t -> 'a ta <|> b tries to parse a, and if a fails without
consuming any input, backtracks and tries
to parse b, otherwise it fails as a.
See CCParse.try_ to ensure a does not consume anything (but it is best
to avoid wrapping large parsers with CCParse.try_)val (<?>) : 'a t -> string -> 'a ta <?> msg behaves like a, but if a fails without
consuming any input, it fails with msg
instead. Useful as the last choice in a series of <|>:
a <|> b <|> c <?> "expected a|b|c"val try_ : 'a t -> 'a ttry_ p tries to parse like p, but backtracks if p fails.
Useful in combination with <|>val suspend : (unit -> 'a t) -> 'a tsuspend f is the same as f (), but evaluates f () only
when neededval string : string -> string tstring s parses exactly the string s, and nothing elseval many : 'a t -> 'a list tmany p parses a list of p, eagerly (as long as possible)val many1 : 'a t -> 'a list tval skip : 'a t -> unit tskip p parses zero or more times p and ignores its resultval sep : by:'b t -> 'a t -> 'a list tsep ~by p parses a list of p separated by byval sep1 : by:'b t -> 'a t -> 'a list tsep1 ~by p parses a non empty list of p, separated by byval fix : ('a t -> 'a t) -> 'a tval memo : 'a t -> 'a tmemo 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.
val fix_memo : ('a t -> 'a t) -> 'a tCCParse.fix, but the fixpoint is memoized.val get_lnum : int tval get_cnum : int tval get_pos : (int * int) t
Those functions have a label ~p on the parser, since 0.14.
val parse : 'a t -> state -> 'a or_errorparse p st applies p on the input, and returns Ok x if
p succeeds with x, or Error s otherwiseval parse_exn : 'a t -> state -> 'a
val parse_string : 'a t -> string -> 'a or_errorCCParse.parse for string inputsval parse_string_exn : 'a t -> string -> 'aParseError if it failsval parse_file : 'a t -> string -> 'a or_errorparse_file p file parses file with p by opening the file
and reading it whole.val parse_file_exn : 'a t -> string -> 'aParseError if it failsmodule Infix:sig..end
This is useful to parse OCaml-like values in a simple way.
module U:sig..end