Module CCParse
Very Simple Parser Combinators
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)))" ;;Parse a list of words
open Containers.Parse;;
let p = U.list ~sep:"," U.word;;
parse_string_exn p "[abc , de, hello ,world ]";;Stress Test
This makes a list of 100_000 integers, prints it and parses it back.
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 'a or_error= ('a, string) Pervasives.resulttype line_num= inttype col_num= inttype parse_branch
val string_of_branch : parse_branch -> string
exceptionParseError of parse_branch * unit -> stringparsing branch * message.
Input
val state_of_string : string -> state
Combinators
type 'a t= state -> ok:('a -> unit) -> err:(exn -> unit) -> unitTakes the input and two continuations:
okto call with the result when it's doneerrto call when the parser met an error
- raises ParseError
in case of failure.
val return : 'a -> 'a tAlways succeeds, without consuming its input.
val map : ('a -> 'b) -> 'a t -> 'b tval map2 : ('a -> 'b -> 'c) -> 'a t -> 'b t -> 'c tval map3 : ('a -> 'b -> 'c -> 'd) -> 'a t -> 'b t -> 'c t -> 'd tval (>>=) : 'a t -> ('a -> 'b t) -> 'b tMonadic bind.
p >>= fresults in a new parser which behaves aspthen, in case of success, appliesfto the result.
val (<*) : 'a t -> _ t -> 'a ta <* bparsesaintox, parsesband ignores its result, and returnsx.
val (*>) : _ t -> 'a t -> 'a ta *> bparsesa, then parsesbintox, and returnsx. The results ofais ignored.
val fail : string -> 'a tfail msgfails with the given message. It can trigger a backtrack.
val parsing : string -> 'a t -> 'a tparsing s pbehaves the same asp, with the information that we are parsings, ifpfails.
val eoi : unit tExpect the end of input, fails otherwise.
val nop : unit tSucceed with
().
val char : char -> char tchar cparses the charactercand nothing else.
val char_if : (char -> bool) -> char tchar_if fparses a characterciff c = true.
val chars_if : (char -> bool) -> string tchars_if fparses a string of chars that satisfyf.
val endline : char tParse '\n'.
val space : char tTab or space.
val white : char tTab or space or newline.
val skip_chars : (char -> bool) -> unit tSkip 0 or more chars satisfying the predicate.
val skip_space : unit tSkip ' ' and '\t'.
val skip_white : unit tSkip ' ' and '\t' and '\n'.
val (<|>) : 'a t -> 'a t -> 'a ta <|> btries to parsea, and ifafails without consuming any input, backtracks and tries to parseb, otherwise it fails asa. Seetry_to ensureadoes not consume anything (but it is best to avoid wrapping large parsers withtry_).
val (<?>) : 'a t -> string -> 'a ta <?> msgbehaves likea, but ifafails without consuming any input, it fails withmsginstead. Useful as the last choice in a series of<|>:a <|> b <|> c <?> "expected a|b|c".
val try_ : 'a t -> 'a ttry_ ptries to parse likep, but backtracks ifpfails. Useful in combination with<|>.
val suspend : (unit -> 'a t) -> 'a tsuspend fis the same asf (), but evaluatesf ()only when needed.
val string : string -> string tstring sparses exactly the strings, and nothing else.
val memo : 'a t -> 'a tMemoize the parser.
memo pwill behave likep, but when called in a state (read: position in input) it has already processed,memo preturns 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 involvingp.This function is not thread-safe.
val get_lnum : int tReflect the current line number.
val get_cnum : int tReflect the current column number.
val get_pos : (int * int) tReflect the current (line, column) numbers.
Parse
Those functions have a label ~p on the parser, since 0.14.
val parse : 'a t -> state -> 'a or_errorparse p stappliespon the input, and returnsOk xifpsucceeds withx, orError sotherwise.
val parse_string_exn : 'a t -> string -> 'a- raises ParseError
if it fails.
val parse_file : 'a t -> string -> 'a or_errorparse_file p fileparsesfilewithpby opening the file and reading it whole.
val parse_file_exn : 'a t -> string -> 'a- raises ParseError
if it fails.
Infix
module Infix : sig ... endUtils
This is useful to parse OCaml-like values in a simple way.
module U : sig ... endLet operators on OCaml >= 4.08.0, nothing otherwise
- since
- 2.8