cmly_api.ml 4.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
(******************************************************************************)
(*                                                                            *)
(*                                   Menhir                                   *)
(*                                                                            *)
(*                       François Pottier, Inria Paris                        *)
(*              Yann Régis-Gianas, PPS, Université Paris Diderot              *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU General Public License version 2, as described in the    *)
(*  file LICENSE.                                                             *)
(*                                                                            *)
(******************************************************************************)

14 15 16 17 18
(* The following signatures describe the API offered by the functor
   [Cfmly_read.Read]. This functor reads in a .cmly file and gives
   access to the description of the grammar and automaton contained
   in this file. *)

POTTIER Francois's avatar
POTTIER Francois committed
19 20 21
(* This API is currently entirely self-contained, except for a reference
   to the module [Keyword], which is also part of [MenhirSdk]. *)

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
(* The module type [INDEXED] describes a type [t] whose elements are
   in a bijection with an integer interval of the form [0..count). *)

module type INDEXED = sig
  type t
  val count : int
  val of_int : int -> t
  val to_int : t -> int
  val iter : (t -> unit) -> unit
  val fold : (t -> 'a -> 'a) -> 'a -> 'a
  val tabulate : (t -> 'a) -> t -> 'a
end

(* The module type [GRAMMAR] describes the grammar and automaton. *)

module type GRAMMAR = sig

  type terminal    = private int
  type nonterminal = private int
  type production  = private int
  type lr0         = private int
  type lr1         = private int
  type item        = production * int
45
  type ocamltype   = string
46
  type ocamlexpr   = string
47

48 49 50 51 52 53
  module Range : sig
    type t
    val startp: t -> Lexing.position
    val endp: t -> Lexing.position
  end

54 55 56 57 58
  module Attribute : sig
    type t
    val label        : t -> string
    val has_label    : string -> t -> bool
    val payload      : t -> string
59
    val position     : t -> Range.t
60
  end
61 62

  module Grammar : sig
63
    val basename     : string
64 65
    val preludes     : string list
    val postludes    : string list
66 67
    val parameters   : string list
    val entry_points : (nonterminal * production * lr1) list
68
    val attributes   : Attribute.t list
69 70 71 72 73 74
  end

  module Terminal : sig
    include INDEXED with type t = terminal
    val name         : t -> string
    val kind         : t -> [`REGULAR | `ERROR | `EOF | `PSEUDO]
75 76
    val typ          : t -> ocamltype option
    val attributes   : t -> Attribute.t list
77 78 79 80 81 82 83
  end

  module Nonterminal : sig
    include INDEXED with type t = nonterminal
    val name         : t -> string
    val mangled_name : t -> string
    val kind         : t -> [`REGULAR | `START]
84
    val typ          : t -> ocamltype option
85
    val positions    : t -> Range.t list
86
    val nullable     : t -> bool
87
    val first        : t -> terminal list
88
    val attributes   : t -> Attribute.t list
89 90 91 92 93 94 95 96 97 98 99 100
  end

  type symbol =
    | T of terminal
    | N of nonterminal

  val symbol_name : ?mangled:bool -> symbol -> string

  type identifier = string

  module Action : sig
    type t
101
    val expr         : t -> ocamlexpr
102 103 104 105 106 107 108
    val keywords     : t -> Keyword.keyword list
  end

  module Production : sig
    include INDEXED with type t = production
    val kind         : t -> [`REGULAR | `START]
    val lhs          : t -> nonterminal
109
    val rhs          : t -> (symbol * identifier * Attribute.t list) array
110
    val positions    : t -> Range.t list
111
    val action       : t -> Action.t option
112
    val attributes   : t -> Attribute.t list
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  end

  module Lr0 : sig
    include INDEXED with type t = lr0
    val incoming     : t -> symbol option
    val items        : t -> item list
  end

  module Lr1 : sig
    include INDEXED with type t = lr1
    val lr0          : t -> lr0
    val transitions  : t -> (symbol * t) list
    val reductions   : t -> (terminal * production list) list
  end

  module Print : sig
    open Format
    val terminal            : formatter -> terminal -> unit
    val nonterminal         : formatter -> nonterminal -> unit
    val symbol              : formatter -> symbol -> unit
    val mangled_nonterminal : formatter -> nonterminal -> unit
    val mangled_symbol      : formatter -> symbol -> unit
    val production          : formatter -> production -> unit
    val item                : formatter -> item -> unit
    val itemset             : formatter -> item list -> unit
    val annot_item          : string      list -> formatter -> item      -> unit
    val annot_itemset       : string list list -> formatter -> item list -> unit
  end

end