package lascar

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Some functions for building recursive descent parsers using the Stream and Genlex modules

Higher order parsers

val separated_list : string -> ?stop_on:string option -> (Genlex.token Stream.t -> 'a) -> Genlex.token Stream.t -> 'a list

separated_list sep p ss parses stream ss as a sep-separated list of elements parsed by p and returns the list of parsed element. Parsing fails if p fails or a token non equal to sep occurs in ss unless the stop_on optional argument is set to Some t. In this case, parsing stops and returns the list parsed so far when the token equal to c is read. This mechanism is required for parsing grammars in which empty list can be used as optional parts (see for example the syntax of !Fsm_transitions.

Useful wrappers

val run : lexer:(string -> Genlex.token Stream.t) -> parser:(Genlex.token Stream.t -> 'a) -> string -> ('a, Genlex.token option) Result.t

run lexer parser src builds a stream of token ss by applying lexer to src, applies parser to this stream and returns either Ok r if parsing succeeds (with result r) or Error e if parsing fails (with e=Some t and t the current lookahead token or e=None if the remaining input stream is empty.

exception Parse_error of string * Genlex.token option

Parsed string, lookahead token

val try_run : lexer:(string -> Genlex.token Stream.t) -> parser:(Genlex.token Stream.t -> 'a) -> string -> 'a

try_run lexer parser src is like run except that it just raises exception Parse_error if parsing fails

OCaml

Innovation. Community. Security.