Module Cobol_common.Visitor

Tree Visitors

Tree visitors as implemented here are objects that define one method for each type of node to be visited. Such visitors are associated (and implemented using) a set of functions that are visiting starting points.

Conventions

In the module of a visitor V:

Note: a type Srcloc.with_loc is denoted with a prime ('). For instance, the starting point for a node of type t with_loc will be V.fold_t'. This convention also guides the naming of utilities below.

Types

The type of actions depends on the kind of visitor. For now, only a folding visitor is available:

type 'a folding_action =
  1. | SkipChildren of 'a
    (*

    do not visit any of the node's children, and continue with the given value

    *)
  2. | DoChildren of 'a
    (*

    visit the node's children

    *)
  3. | DoChildrenAndThen of 'a * 'a -> 'a
    (*

    visit the node's children, and then call the given function

    *)

Action to be performed when visiting a node:

Utilities for writing visitors

module INFIX : sig ... end
val in_testsuite : bool Stdlib.ref
val report : string -> pos:(string * int * int * int) -> func_name:string -> unit

Specific visitors

module Fold : sig ... end

Visitor that accumulates over the nodes of the tree.

module Fold_with_context : sig ... end

Folder that carries a context

include module type of struct include Fold end

Visitor that accumulates over the nodes of the tree.

type 'a action = 'a folding_action
type ('x, 'a) fold = 'x -> 'a -> 'a folding_action

Some combinators to write more readable folding visitors

val skip_children : 'a -> 'a folding_action
val skip : 'a -> 'a folding_action
val do_children : 'a -> 'a folding_action
val proceed : 'a -> 'a folding_action
val do_children_and_then : 'a -> ('a -> 'a) -> 'a folding_action
val proceed_and_then : 'a -> ('a -> 'a) -> 'a folding_action
val default : 'a -> 'b -> 'b folding_action

Action handling

val handle : ('a -> 'b -> 'b action) -> continue:('a -> 'b -> 'b) -> 'a -> 'b -> 'b

handle fold continue node acc first calls fold node acc, and then behaves according to the action returned.

val leaf : ('x -> 'a -> 'a action) -> 'x -> 'a -> 'a

leaf fold node acc calls fold node acc and returns immediately (after executing the post action, if fold returns DoChildrenAndThen).

class 'a folder : object ... end

Base folding visitor.

Entry points for folding over some basic types.

val fold_bool : 'a folder -> bool -> 'a -> 'a
val fold_char : 'a folder -> char -> 'a -> 'a
val fold_int : 'a folder -> int -> 'a -> 'a
val fold_string : 'a folder -> string -> 'a -> 'a

Generic entry points.

val fold_option : fold:('b folder as 'a -> 'c -> 'b -> 'b) -> 'd -> 'c option -> 'b -> 'b
val fold_list : fold:('b folder as 'a -> 'c -> 'b -> 'b) -> 'd -> 'c list -> 'b -> 'b
val fold_nel : fold:('b folder as 'a -> 'c -> 'b -> 'b) -> 'd -> 'c Basics.NEL.t -> 'b -> 'b
val fold' : fold:('b folder as 'a -> 'c -> 'b -> 'b) -> 'd -> 'c Srcloc.TYPES.with_loc -> 'b -> 'b
val fold_int' : 'a folder -> int Srcloc.TYPES.with_loc -> 'a -> 'a
val fold_int'_opt : 'a folder -> int Srcloc.TYPES.with_loc option -> 'a -> 'a
val fold_string' : 'a folder -> string Srcloc.TYPES.with_loc -> 'a -> 'a
val fold_string'_opt : 'a folder -> string Srcloc.TYPES.with_loc option -> 'a -> 'a
val fold_with_loc_list : fold:('b folder as 'a -> 'c -> 'b -> 'b) -> 'd -> 'c Srcloc.TYPES.with_loc list -> 'b -> 'b
val handle' : ('a Srcloc.TYPES.with_loc -> 'x -> 'x action) -> fold:('x folder as 'b -> 'a -> 'x -> 'x) -> 'b -> 'a Srcloc.TYPES.with_loc -> 'x -> 'x

Helper to shorten definitions for traversal of nodes with source locations

val leaf' : ('a Srcloc.TYPES.with_loc -> 'b -> 'b action) -> 'b folder -> 'a Srcloc.TYPES.with_loc -> 'b -> 'b
val todo : (string * int * int * int) -> string -> 'b -> 'a -> 'a

Reports a missing folding visitor implementation once. Use __POS__ as first argument

val partial : (string * int * int * int) -> string -> 'a -> 'a

Reports a partial folding visitor implementation once. Use __POS__ as first argument