github ↗
docs menu

Module Ops.Pattern_matcher

Pattern matchers.

An ordered list of rules, each a pattern and a function of the nodes it names. Matching a node tries the rules in order and is the first result. A matcher is typed by the context its rules read, 'ctx, and by its results, 'r. A rewriter's results are nodes; a fold's are anything else, such as verdicts or text.

#type node := t
#type ('ctx, 'r) rule

The type for rules.

#val rule : Upat.t -> ((string -> node) -> 'r option) -> ('ctx, 'r) rule

rule p f is f m for a node p matches, where m name is the node p names name. m raises Invalid_argument on a name the match did not bind.

#val rule_ctx : Upat.t -> ('ctx -> (string -> node) -> 'r option) -> ('ctx, 'r) rule

rule_ctx p f is rule, with the context passed to f.

#type ('ctx, 'r) t

The type for pattern matchers.

#val v : (unit -> ('ctx, node) rule list) -> ('ctx, node) t

v rules is the rewriter of the rules rules () returns, which it calls on its first rewrite. Domains that rewrite first at once may each call rules (); all of them use the one result the matcher keeps. A rule declines by returning None or the node it matched; the next rule is then tried.

The first rewrite raises Invalid_argument if a rule's pattern has no operation.

#val fold : (unit -> ('ctx, 'r) rule list) -> ('ctx, 'r) t

fold rules is the matcher of the rules rules () returns, which it calls on its first rewrite as v does, and whose results are not the nodes they match. A rule declines by returning None.

The first rewrite raises Invalid_argument if a rule's pattern has no operation.

#val append : ('ctx, 'r) t -> ('ctx, 'r) t -> ('ctx, 'r) t

append m0 m1 tries m0's rules, then m1's, each declining as its matcher says.

#val concat : ('ctx, 'r) t list -> ('ctx, 'r) t

concat ms tries the rules of ms in order.

#val with_ctx : (unit, 'r) t -> ('ctx, 'r) t

with_ctx m is m, ignoring the context, for appending to matchers that read one.

#val rewrite : ('ctx, 'r) t -> 'ctx -> node -> 'r option

rewrite m ctx u is the result of the first rule of m that matches u and does not decline, trying the namings of each pattern in Upat.match_'s order.