Beluga_parser.Make_parsing
Functor creating a Beluga parser using the given implementation of state.
module Parser_state :
PARSER_STATE
with type token = Located_token.t
and type location = Beluga_syntax.Location.t
include PARSER_STATE
with type token = Located_token.t
and type location = Beluga_syntax.Location.t
with type state = Parser_state.state
include Support.State.STATE with type state = Parser_state.state
type state = Parser_state.state
The type of states inside the monad.
eval a init
is run a init = (final, v)
, but outputs only v
.
exec a init
is run a init = (final, v)
, but outputs only final
.
val traverse_list :
('a -> state -> state * 'b) ->
'a Support.List.t ->
state ->
state * 'b Support.List.t
val traverse_list1 :
('a -> state -> state * 'b) ->
'a Support.List1.t ->
state ->
state * 'b Support.List1.t
val traverse_list2 :
('a -> state -> state * 'b) ->
'a Support.List2.t ->
state ->
state * 'b Support.List2.t
val traverse_list_void :
('a -> state -> state * _) ->
'a Support.List.t ->
state ->
state * Stdlib.Unit.t
val traverse_list1_void :
('a -> state -> state * _) ->
'a Support.List1.t ->
state ->
state * Stdlib.Unit.t
val traverse_list2_void :
('a -> state -> state * _) ->
'a Support.List2.t ->
state ->
state * Stdlib.Unit.t
val traverse_reverse_list :
('a -> state -> state * 'b) ->
'a Support.List.t ->
state ->
state * 'b Support.List.t
val traverse_reverse_list1 :
('a -> state -> state * 'b) ->
'a Support.List1.t ->
state ->
state * 'b Support.List1.t
val traverse_reverse_list2 :
('a -> state -> state * 'b) ->
'a Support.List2.t ->
state ->
state * 'b Support.List2.t
val traverse_reverse_list_void :
('a -> state -> state * _) ->
'a Support.List.t ->
state ->
state * Stdlib.Unit.t
val traverse_reverse_list1_void :
('a -> state -> state * _) ->
'a Support.List1.t ->
state ->
state * Stdlib.Unit.t
val traverse_reverse_list2_void :
('a -> state -> state * _) ->
'a Support.List2.t ->
state ->
state * Stdlib.Unit.t
val traverse_option :
('a -> state -> state * 'b) ->
'a Support.Option.t ->
state ->
state * 'b Support.Option.t
val traverse_option_void :
('a -> state -> state * _) ->
'a Support.Option.t ->
state ->
state * Stdlib.Unit.t
val seq_list :
(state -> state * 'a) Support.List.t ->
state ->
state * 'a Support.List.t
val seq_list1 :
(state -> state * 'a) Support.List1.t ->
state ->
state * 'a Support.List1.t
seq_list_void [x1; x2; ...; xn]
performs x1
, x2
, ..., xn
in order.
include Support.Monad.MONAD with type 'a t := state -> state * 'a
include Support.Functor.FUNCTOR with type 'a t := state -> state * 'a
include Support.Apply.APPLY with type 'a t := state -> state * 'a
val peek : state -> state * Located_token.t option
peek state
is (state', token_opt)
where token_opt
is the next unconsumed token in state
. If token_opt = Option.None
, then the end of the input stream was reached. Otherwise, token_opt = Option.Some token
. The token is not consumed, meaning that state'
is not advanced to the next token in the stream.
val observe : state -> state * Located_token.t option
observed state
is like peek state
, but the observed token is consumed.
accept state
is (state', ())
where state'
is derived from state
by consuming the next token in the input stream. This effectively observes and discards the next token in state
.
val insert : Located_token.t -> state -> state * unit
insert token state
is (state', ())
where state'
is derived from state
by inserting token
at the beginning of the input stream. That is, token
is the next token in state'
.
val next_location : state -> state * Beluga_syntax.Location.t option
next_location state
is (state', next_location_opt)
where next_location_opt
is the location of the next token in state
. This does not advance the input stream. next_location_opt = Option.None
at the end of the input stream.
val previous_location : state -> state * Beluga_syntax.Location.t option
previous_location state
is (state', previous_location_opt)
where previous_location_opt
is the location of the last token in state
to have been consumed. previous_location_opt = Option.None
at the beginning of the input stream.
enable_backtracking state
is (state', ())
where state'
has backtracking enabled.
enable_backtracking state
is (state', ())
where state'
has backtracking disabled.
can_backtrack state
is (state', flag)
where flag = true
indicates that state
and state'
allow backtracking out of an erroneous result, and flag = false
otherwise.
val allow_backtracking_on_error :
(state -> state * ('a, 'e) Stdlib.result) ->
state ->
state * ('a, 'e) Stdlib.result
allow_backtracking_on_error m
is m'
such that m' state
is (state', x)
with backtracking enabled in state'
if x = Result.Error cause
. That is, this combinator enables backtracking if m'
produces an error. Backtracking then needs to be manually disabled afterwards.
val with_checkpoint :
(state -> state * ('a, 'e) Stdlib.result) ->
state ->
state
* ('a, [> `Backtracked of 'e | `Did_not_backtrack of 'e ]) Stdlib.result
with_checkpoint m
is m'
such that m' state
marks the current state, performs m
, then either
m state = (state', Result.Ok x)
,m state = (state', Result.Error cause)
.The output error from m
is additionally annotated with whether backtracking occurred.
In the case of a parser m
, backtracking can occur if backtracking is enabled with allow_backtracking_on_error
, or if m
did not consume any input.
include Parser_combinator.PARSER
with type state := state
and type location := Beluga_syntax.Location.t
and type token := Located_token.t
include Support.Monad.MONAD
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Functor.FUNCTOR
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Apply.APPLY
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Common_parser.COMMON_PARSER
with type state := state
and type location := Beluga_syntax.Location.t
and type token := Located_token.t
include Parser_combinator.PARSER
with type state := state
with type location := Beluga_syntax.Location.t
with type token := Located_token.t
include Support.Monad.MONAD
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Functor.FUNCTOR
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Apply.APPLY
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Lf_parser.LF_PARSER with type state := state
include Common_parser.COMMON_PARSER with type state := state
include Parser_combinator.PARSER with type state := state
include Support.Monad.MONAD
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Functor.FUNCTOR
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Apply.APPLY
with type 'a t := state -> state * ('a, exn) Stdlib.result
val lf_kind : state -> state * (Synprs.lf_object, exn) Stdlib.result
val lf_typ : state -> state * (Synprs.lf_object, exn) Stdlib.result
val lf_term : state -> state * (Synprs.lf_object, exn) Stdlib.result
include Clf_parser.CLF_PARSER with type state := state
include Common_parser.COMMON_PARSER with type state := state
include Parser_combinator.PARSER with type state := state
include Support.Monad.MONAD
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Functor.FUNCTOR
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Apply.APPLY
with type 'a t := state -> state * ('a, exn) Stdlib.result
val clf_typ : state -> state * (Synprs.clf_object, exn) Stdlib.result
val clf_term : state -> state * (Synprs.clf_object, exn) Stdlib.result
val clf_term_pattern : state -> state * (Synprs.clf_object, exn) Stdlib.result
val clf_context :
state ->
state * (Synprs.clf_context_object, exn) Stdlib.result
val clf_substitution :
state ->
state * (Synprs.clf_context_object, exn) Stdlib.result
include Meta_parser.META_PARSER with type state := state
include Common_parser.COMMON_PARSER with type state := state
include Parser_combinator.PARSER with type state := state
include Support.Monad.MONAD
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Functor.FUNCTOR
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Apply.APPLY
with type 'a t := state -> state * ('a, exn) Stdlib.result
val schema : state -> state * (Synprs.schema_object, exn) Stdlib.result
val meta_type : state -> state * (Synprs.meta_thing, exn) Stdlib.result
val meta_object : state -> state * (Synprs.meta_thing, exn) Stdlib.result
val meta_pattern : state -> state * (Synprs.meta_thing, exn) Stdlib.result
val meta_context :
state ->
state * (Synprs.meta_context_object, exn) Stdlib.result
include Comp_parser.COMP_PARSER with type state := state
include Common_parser.COMMON_PARSER with type state := state
include Parser_combinator.PARSER with type state := state
include Support.Monad.MONAD
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Functor.FUNCTOR
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Apply.APPLY
with type 'a t := state -> state * ('a, exn) Stdlib.result
val comp_kind : state -> state * (Synprs.comp_sort_object, exn) Stdlib.result
val comp_typ : state -> state * (Synprs.comp_sort_object, exn) Stdlib.result
val comp_pattern :
state ->
state * (Synprs.comp_pattern_object, exn) Stdlib.result
val comp_copattern :
state ->
state * (Synprs.comp_copattern_object, exn) Stdlib.result
val comp_expression :
state ->
state * (Synprs.comp_expression_object, exn) Stdlib.result
val comp_context :
state ->
state * (Synprs.comp_context_object, exn) Stdlib.result
include Harpoon_parser.HARPOON_PARSER with type state := state
include Common_parser.COMMON_PARSER with type state := state
include Parser_combinator.PARSER with type state := state
include Support.Monad.MONAD
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Functor.FUNCTOR
with type 'a t := state -> state * ('a, exn) Stdlib.result
include Support.Apply.APPLY
with type 'a t := state -> state * ('a, exn) Stdlib.result
val harpoon_proof : state -> state * (Synprs.harpoon_proof, exn) Stdlib.result
val interactive_harpoon_command :
state ->
state * (Synprs.harpoon_repl_command, exn) Stdlib.result
include Signature_parser.SIGNATURE_PARSER
with type state := state
with type token = Parser_state.token
with type location = Parser_state.location
include Common_parser.COMMON_PARSER
with type state := state
with type token = Parser_state.token
with type location = Parser_state.location
include Parser_combinator.PARSER
with type state := state
with type token = Parser_state.token
with type location = Parser_state.location
type token = Parser_state.token
type location = Parser_state.location
type 'a parser = 'a t
include Support.Monad.MONAD with type 'a t := 'a t
val return : 'a -> 'a t
return a
injects a
into the monadic type.
bind f a
is the sequential composition of two actions, passing any value produced by a
as argument to f
.
compose g f
is the Kleisli composition of f
and g
, passing the input to f
, then binding the output to g
.
( let* ) ma f
is bind f ma
. This is a binding operator, and it is used as let* a = ma in f a
.
include Support.Functor.FUNCTOR with type 'a t := 'a t
map f
is the function that maps values of t
by f
. The order of arguments is for use in function pipelines as fb = fa |> map (fun a -> (* ... *))
.
( let+ ) ma f
is map f a
. This is a binding operator, and is used as let+ a = ma in f a
include Support.Apply.APPLY with type 'a t := 'a t
ap fa fab
applies argument fa
to fab
under the abstract datatype t
.
ap_first second first
combines actions first
and second
but keeps only first
. That is, ap_first second first = first
. The order of arguments is for use in function pipelines as first = first |> ap_first second
.
ap_second second first
combines actions first
and second
but keeps only second
. That is, ap_second second first = second
. The order of arguments is for use in function pipelines as second = first |> ap_second second
.
seq2 fa1 fa2
sequentially executes actions fa1
and fa2
, and keeps their outputs under the abstract datatype t
.
seq3 fa1 fa2 fa3
sequentially executes actions fa1
, fa2
and fa3
, and keeps their outputs under the abstract datatype t
.
seq4 fa1 fa2 fa3 fa4
sequentially executes actions fa1
, fa2
, fa3
and fa4
, and keeps their outputs under the abstract datatype t
.
seq5 fa1 fa2 fa3 fa4 fa5
sequentially executes actions fa1
, fa2
, fa3
, fa4
and fa5
, and keeps their outputs under the abstract datatype t
.
lift2 f ma1 ma2
sequentially executes actions ma1
, ma2
and passes their outputs to f
.
lift3 f ma1 ma2 ma3
sequentially executes actions ma1
, ma2
, ma3
and passes their outputs to f
.
lift4 f ma1 ma2 ma3 ma4
sequentially executes actions ma1
, ma2
, ma3
, ma4
and passes their outputs to f
.
run p state
is (state', result)
where result
is the output of parsing p
using state
.
This is only meant to be used internally, or when defining new parser combinators, since it does not prune the parser exception when result = Result.Error exn
.
The end user of a parser should use run_exn
.
run_exn p state
is (state', parsed)
where parsed
is the object parsed from state
using p
. A pruned exception is raised if p
fails.
catch p
runs p
and invokes the given handler to modify the outcome. Despite being called "catch", this parser is actually a kind of map
, and is used to implement "low-level" parser transformations.
val fail : exn -> 'a t
fail exn
is the parser that always fails with exn
.
val fail_at_location : Beluga_syntax.Location.t -> exn -> 'a t
fail_at_location location exn
is the parser that always fails with exn
annotated with location
.
val fail_at_next_location : exn -> 'a t
fail_at_next_location exn
is the parser that always fails with exn
annotated with the next location in the input stream.
val fail_at_previous_location : exn -> 'a t
fail_at_previous_location exn
is the parser that always fails with exn
annotated with the previous location in the input stream.
labelled label p
is p'
such that when p
fails, p'
annotates the exception with a label for error-reporting.
span p
is the parser p'
that additionally returns the source file location that spans the tokens consumed by p
.
only p
is the parser p'
that expects p
to succeed, then the end of input to be reached immediately after.
maybe p
is the parser p'
that may fail, in which case it returns Option.none
.
many p
is the parser p'
that parses tokens following the EBNF grammar p*
, meaning that it parses 0
or more occurrences of p
.
val some : 'a t -> 'a Support.List1.t t
some p
is the parser p'
that parses tokens following the EBNF grammar p+
, meaning that it parses 1
or more occurrences of p
.
sep_by0 ~sep p
is the parser p'
that parses tokens following the EBNF grammar [p (sep p)*]
, meaning that it parses 0
or more occurrences of p
separated by sep
.
val sep_by1 : sep:unit t -> 'a t -> 'a Support.List1.t t
sep_by1 ~sep p
is the parser p'
that parses tokens following the EBNF grammar p (sep p)+
, meaning that it parses 1
or more occurrences of p
separated by sep
.
trying p
is the parser p'
that enables backtracking in the parser state if p
fails, which signals that the erroneous state can be backtracked out of irrespective of the number of consumed tokens.
val choice : 'a t Support.List.t -> 'a t
choice ps
is the parser p
that sequentially tries the parsers in p
.
For instance, choice [p1; p2]
first runs p1
. If it fails, p2
is run if one of the following is true.
p1
failed without consuming any input.p1
failed with backtracking enabled. Backtracking is enabled by the trying
combinator.satisfy f
is the basic parser p
that performs an action based on whether the next token in the stream satisfies the predicate f
. The parser is advanced only if the next token satisfies f
. If the next token does not satisfy the predicate, then the error is annotated with the next token's location.
val eoi : unit t
eoi
is the parser that expects the end of input to be reached. This is either the end of the input string, token stream, or file input channel.
val keyword : string -> unit t
keyword s
parses the string s
either as a keyword token, or an identifier matching s
.
val integer : int t
integer
parses an integer.
val dot_integer : int t
dot_integer
parses a dot followed by an integer.
val pragma : string -> unit t
pragma s
parses --s
.
val string_literal : string t
string_literal
parses an escaped string literal between double quotes.
val dot : unit t
dot
parses `.'
.
val dots : unit t
dots
parses `..'
.
val comma : unit t
comma
parses `,'
.
val colon : unit t
colon
parses `:'
.
val semicolon : unit t
semicolon
parses `;'
.
val slash : unit t
slash
pases `/'
.
val equals : unit t
equals
parses `='
.
val lambda : unit t
lambda
parses `\'
.
val hat : unit t
hat
parses `^'
.
val underscore : unit t
underscore
parses `_'
.
val pipe : unit t
pipe
parses `|'
.
val forward_arrow : unit t
forward_arrow
parses `->'
.
val backward_arrow : unit t
backward_arrow
parses `<-'
.
val thick_forward_arrow : unit t
thick_forward_arrow
parses `=>'
.
val plus : unit t
plus
parses `+'
.
val star : unit t
star
parses `*'
.
val hash : unit t
hash
parses `#'
. For identifiers prefixed by a hash symbol, see hash_identifier
and omittable_hash_identifier
.
val double_colon : unit t
double_colon
parses `::'
.
val turnstile : unit t
turnstile
parses `|-'
.
val turnstile_hash : unit t
turnstile_hash
parses `|-#'
.
val left_parenthesis : unit t
left_parenthesis
parses `('
.
val right_parenthesis : unit t
right_parenthesis
parses `)'
.
val left_brace : unit t
left_brace
parses `{'
.
val right_brace : unit t
right_brace
parses `}'
.
val left_brack : unit t
left_brack
parses `['
.
val right_brack : unit t
right_brack
parses `]'
.
val left_angle : unit t
left_angle
parses `<'
.
val right_angle : unit t
right_angle
parses `>'
.
val identifier : Beluga_syntax.Identifier.t t
identifier
parses a plain identifier.
val dot_identifier : Beluga_syntax.Identifier.t t
dot_identifier
parses a dot followed by an identifier.
val hash_identifier : Beluga_syntax.Identifier.t t
hash_identifier
parses an identifier starting with `#'
. The prefix `#'
is included in the identifier.
val dollar_identifier : Beluga_syntax.Identifier.t t
dollar_identifier
parses an identifier starting with `$'
. The prefix `$'
is included in the identifier.
val omittable_identifier : Beluga_syntax.Identifier.t option t
omittable_identifier
parses `_' | <identifier>
.
val omittable_hash_identifier : Beluga_syntax.Identifier.t option t
omittable_hash_identifier
parses `#_' | <hash-identifier>
.
val omittable_dollar_identifier : Beluga_syntax.Identifier.t option t
omittable_dollar_identifier
parses `$_' | <dollar-identifier>
.
val qualified_identifier : Beluga_syntax.Qualified_identifier.t t
qualified_identifier
parses <identifier> (<dot-identifier>)*
.
val dot_qualified_identifier : Beluga_syntax.Qualified_identifier.t t
dot_qualified_identifier
parses a dot followed by a qualified identifier.
val qualified_or_plain_identifier :
[> `Plain of Beluga_syntax.Identifier.t
| `Qualified of Beluga_syntax.Qualified_identifier.t ]
t
qualified_or_plain_identifier
parses a plain identifier or a qualified identifier, whichever is the longest parse. That is, if qualified_or_plain_identifier
parses a qualified identifier, then it has at least one namespace.
val omittable_meta_object_identifier :
(Beluga_syntax.Identifier.t option * [> `Dollar | `Hash | `Plain ]) t
omittable_meta_object_identifier
parses `_' | `#_' | `$_' | <identifier> | <hash-identifier> | <dollar-identifier>
.
val meta_object_identifier :
(Beluga_syntax.Identifier.t * [> `Dollar | `Hash | `Plain ]) t
meta_object_identifier
parses <identifier> | <hash-identifier> | <dollar-identifier>
.
val hole : [> `Labelled of Beluga_syntax.Identifier.t | `Unlabelled ] parser
hole
parses `?' | `?'<identifier>
.
val block_comment : (Beluga_syntax.Location.t * string) t
block_comment
parses %{{ c }}%
.
val signature_file : Synprs.signature_file t
val signature_global_pragma : Synprs.signature_global_pragma t
val signature_entry : Synprs.signature_entry t
val signature_declaration : Synprs.signature_declaration t
val trust_totality_declaration : Synprs.signature_totality_declaration t
val named_totality_declaration : Synprs.signature_totality_declaration t
val numeric_totality_declaration : Synprs.signature_totality_declaration t
val totality_declaration : Synprs.signature_totality_declaration t