module CCApp_parse:sig..end
status: deprecated
Example: basic S-expr parser
open Containers_string.App_parse;;
type sexp = Atom of string | List of sexp list;;
let mkatom a = Atom a;;
let mklist l = List l;;
let ident_char = alpha_num <+> any_of "|!;$#@%&-_/=*.:~+[]<>'" ;;
let ident = many1 ident_char >|= str_of_l ;;
let atom = (ident <+> quoted) >|= mkatom ;;
let sexp = fix (fun sexp ->
white >>
(atom <+>
((char '(' >> many sexp << char ')') >|= mklist)
)
);;
Str.parse_exn "(a (b c d) e)" sexp;;
type('a, 'b)result =[ `Error of 'b | `Ok of 'a ]
type 'a t
val return : 'a -> 'a tval pure : 'a -> 'a tCCApp_parse.returnval junk : unit tval fail : string -> 'a tfail msg fails with the given error messageval failf : ('a, unit, string, 'b t) Pervasives.format4 -> 'a
val app : ('a -> 'b) t -> 'a t -> 'b tval map : ('a -> 'b) -> 'a t -> 'b tval int : int tval float : float tval bool : bool tval char : char -> char tchar c parses c and c onlyval any_of : string -> char tval alpha_lower : char t
val alpha_upper : char t
val alpha : char t
val symbols : char tval num : char t
val alpha_num : char t
val word : string tword parses any identifier not starting with an integer and
not containing any whitespace nor delimiter
TODO: specifyval quoted : string tval str_of_l : char list -> stringval spaces : unit t'\t' and ' 'val spaces1 : unit tCCApp_parse.spaces but requires at least one spaceval white : unit t'\t', '\n' and ' 'val white1 : unit t
val eof : unit tval many : ?sep:unit t -> 'a t -> 'a list tsep : separator between elements of the list (for instance, space)val many1 : ?sep:unit t -> 'a t -> 'a list tCCApp_parse.many, but needs at least one elementval skip : 'a t -> unit tval skip1 : 'a t -> unit t
val opt : 'a t -> 'a option topt x tries to parse x, and returns None otherwiseval filter : ('a -> bool) -> 'a t -> 'a tfilter f p parses the same as p, but fails if the returned value
does not satisfy fval switch_c : ?default:'a t ->
(char * 'a t) list -> 'a tswitch_c l matches the next char and uses the corresponding parser.
Fails if the next char is not in the list, unless default is defined.Invalid_argument if some char occurs several times in ldefault : parser to use if no char matchesval switch_s : (string * 'a t) list -> 'a tswitch_s l attempts to match matches any of the strings in l.
If one of those strings matches, the corresponding parser
is used from now on.Invalid_argument if some string is a prefix of another string,
or is empty, or if the list is emptyval choice : 'a t list -> 'a tchoice l chooses between the parsers, unambiguouslyInvalid_argument if the list is empty, or if some parsers
overlap, making the choice ambiguousval fix : ('a t -> 'a t) -> 'a tfix f makes a fixpointmodule Infix:sig..end
include CCApp_parse.Infix
type error = {
|
line : |
|
col : |
|
msg : |
val string_of_error : error -> string
exception Error of error
module type S =sig..end
module type INPUT =sig..end
module Make(I:INPUT):Swith type source = I.t
val print : Format.formatter -> 'a t -> unittype token =
| |
Yield of |
| |
EOF |
module type READER =sig..end
module MakeFromReader(R:READER):Swith type source = R.source
module Str:Swith type source = string
module Chan:Swith type source = in_channel