InspectionTableInterpreter.ml 9.6 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 Library General Public License version 2, with a         *)
(*  special exception on linking, as described in the file LICENSE.           *)
(*                                                                            *)
(******************************************************************************)

14 15 16 17 18 19 20 21 22 23 24 25 26
(* -------------------------------------------------------------------------- *)

(* The type functor. *)

module Symbols (T : sig

  type 'a terminal
  type 'a nonterminal

end) = struct

  open T

27 28 29
  (* This should be the only place in the whole library (and generator!)
     where these types are defined. *)

30 31 32 33
  type 'a symbol =
    | T : 'a terminal -> 'a symbol
    | N : 'a nonterminal -> 'a symbol

34
  type xsymbol =
35 36 37 38 39 40 41 42
    | X : 'a symbol -> xsymbol

end

(* -------------------------------------------------------------------------- *)

(* The code functor. *)

43
module Make
44
  (TT : TableFormat.TABLES)
45 46
  (IT : InspectionTableFormat.TABLES
        with type 'a lr1state = int)
47 48 49 50
  (ET : EngineTypes.TABLE
        with type terminal = int
         and type nonterminal = int
         and type semantic_value = Obj.t)
POTTIER Francois's avatar
POTTIER Francois committed
51 52 53
  (E : sig
     type 'a env = (ET.state, ET.semantic_value, ET.token) EngineTypes.env
   end)
54
= struct
55

56
  (* Including [IT] is an easy way of inheriting the definitions of the types
57
     [symbol] and [xsymbol]. *)
58

59
  include IT
60

61 62 63
  (* This auxiliary function decodes a packed linearized array, as created by
     [TableBackend.linearize_and_marshal1]. Here, we read a row all at once. *)

POTTIER Francois's avatar
POTTIER Francois committed
64 65 66
  let read_packed_linearized
    (data, entry : PackedIntArray.t * PackedIntArray.t) (i : int) : int list
  =
67 68 69 70 71
    LinearizedArray.read_row_via
      (PackedIntArray.get data)
      (PackedIntArray.get entry)
      i

72 73 74
  (* This auxiliary function decodes a symbol. The encoding was done by
     [encode_symbol] or [encode_symbol_option] in the table back-end. *)

75
  let decode_symbol (symbol : int) : IT.xsymbol =
76 77 78 79 80 81 82 83
    (* If [symbol] is 0, then we have no symbol. This could mean e.g.
       that the function [incoming_symbol] has been applied to an
       initial state. In principle, this cannot happen. *)
    assert (symbol > 0);
    (* The low-order bit distinguishes terminal and nonterminal symbols. *)
    let kind = symbol land 1 in
    let symbol = symbol lsr 1 in
    if kind = 0 then
84
      IT.terminal (symbol - 1)
85
    else
86
      IT.nonterminal symbol
87

88 89 90 91 92 93
  (* These auxiliary functions convert a symbol to its integer code. For speed
     and for convenience, we use an unsafe type cast. This relies on the fact
     that the data constructors of the [terminal] and [nonterminal] GADTs are
     declared in an order that reflects their internal code. In the case of
     nonterminal symbols, we add [start] to account for the presence of the
     start symbols. *)
94

95
  let n2i (nt : 'a IT.nonterminal) : int =
96
    let answer = TT.start + Obj.magic nt in
97
    assert (IT.nonterminal answer = X (N nt)); (* TEMPORARY roundtrip *)
98 99
    answer

100
  let t2i (t : 'a IT.terminal) : int =
101
    let answer = Obj.magic t in
102
    assert (IT.terminal answer = X (T t)); (* TEMPORARY roundtrip *)
103 104
    answer

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  (* Ordering functions. *)

  let compare_terminals t1 t2 =
    (* Subtraction is safe because overflow is impossible. *)
    t2i t1 - t2i t2

  let compare_nonterminals nt1 nt2 =
    (* Subtraction is safe because overflow is impossible. *)
    n2i nt1 - n2i nt2

  let compare_symbols symbol1 symbol2 =
    match symbol1, symbol2 with
    | X (T _), X (N _) ->
        -1
    | X (N _), X (T _) ->
        1
    | X (T t1), X (T t2) ->
        compare_terminals t1 t2
    | X (N nt1), X (N nt2) ->
        compare_nonterminals nt1 nt2

126 127 128 129 130 131 132 133 134
  let compare_productions prod1 prod2 =
    (* Subtraction is safe because overflow is impossible. *)
    prod1 - prod2

  let compare_items (prod1, index1) (prod2, index2) =
    let c = compare_productions prod1 prod2 in
    (* Subtraction is safe because overflow is impossible. *)
    if c <> 0 then c else index1 - index2

135 136
  (* The function [incoming_symbol] goes through the tables [IT.lr0_core] and
     [IT.lr0_incoming]. This yields a representation of type [xsymbol], out of
137 138 139 140
     which we strip the [X] quantifier, so as to get a naked symbol. This last
     step is ill-typed and potentially dangerous. It is safe only because this
     function is used at type ['a lr1state -> 'a symbol], which forces an
     appropriate choice of ['a]. *)
141

142 143 144
  let incoming_symbol (s : 'a IT.lr1state) : 'a IT.symbol =
    let core = PackedIntArray.get IT.lr0_core s in
    let symbol = decode_symbol (PackedIntArray.get IT.lr0_incoming core) in
145
    match symbol with
146
    | IT.X symbol ->
147 148
        Obj.magic symbol

149
  (* The function [lhs] reads the table [TT.lhs] and uses [IT.nonterminal]
150 151 152
     to decode the symbol. *)

  let lhs prod =
153
    IT.nonterminal (PackedIntArray.get TT.lhs prod)
154

155
  (* The function [rhs] reads the table [IT.rhs] and uses [decode_symbol]
156
     to decode the symbol. *)
157 158

  let rhs prod =
159
    List.map decode_symbol (read_packed_linearized IT.rhs prod)
160 161

  (* The function [items] maps the LR(1) state [s] to its LR(0) core,
162
     then uses [core] as an index into the table [IT.lr0_items]. The
163 164 165
     items are then decoded by the function [export] below, which is
     essentially a copy of [Item.export]. *)

166 167 168 169
  type item =
      int * int

  let export t : item =
170 171 172 173
    (t lsr 7, t mod 128)

  let items s =
    (* Map [s] to its LR(0) core. *)
174 175 176
    let core = PackedIntArray.get IT.lr0_core s in
    (* Now use [core] to look up the table [IT.lr0_items]. *)
    List.map export (read_packed_linearized IT.lr0_items core)
177

178
  (* The function [nullable] maps the nonterminal symbol [nt] to its
179
     integer code, which it uses to look up the array [IT.nullable].
180 181
     This yields 0 or 1, which we map back to a Boolean result. *)

182 183 184 185
  let decode_bool i =
    assert (i = 0 || i = 1);
    i = 1

186
  let nullable nt =
187
    decode_bool (PackedIntArray.get1 IT.nullable (n2i nt))
188 189

  (* The function [first] maps the symbols [nt] and [t] to their integer
190
     codes, which it uses to look up the matrix [IT.first]. *)
191 192

  let first nt t =
193
    decode_bool (PackedIntArray.unflatten1 IT.first (n2i nt) (t2i t))
194

195 196 197 198 199 200 201
  let xfirst symbol t =
    match symbol with
    | X (T t') ->
        compare_terminals t t' = 0
    | X (N nt) ->
        first nt t

202
  (* The function [foreach_terminal] exploits the fact that the
203
     first component of [TT.error] is [Terminal.n - 1], i.e., the
204 205 206 207 208 209 210 211 212
     number of terminal symbols, including [error] but not [#]. *)

  let rec foldij i j f accu =
    if i = j then
      accu
    else
      foldij (i + 1) j f (f i accu)

  let foreach_terminal f accu =
213
    let n, _ = TT.error in
214
    foldij 0 n (fun i accu ->
215
      f (IT.terminal i) accu
216 217 218
    ) accu

  let foreach_terminal_but_error f accu =
219
    let n, _ = TT.error in
220
    foldij 0 n (fun i accu ->
221
      if i = TT.error_terminal then
222 223
        accu
      else
224
        f (IT.terminal i) accu
225 226
    ) accu

227 228
  (* ------------------------------------------------------------------------ *)

POTTIER Francois's avatar
POTTIER Francois committed
229 230 231 232 233 234 235 236
  (* The following is the implementation of the function [feed]. This function
     is logically part of the LR engine, so it would be nice if it were placed
     in the module [Engine], but it must be placed here because, to ensure
     type safety, its arguments must be a symbol of type ['a symbol] and a
     semantic value of type ['a]. The type ['a symbol] is not available in
     [Engine]. It is available here. *)


237 238 239
  open ET
  open EngineTypes

240 241 242 243 244 245
  (* TEMPORARY potential danger:
     - attempting to take a transition that does not exist
       (checked at runtime; raises Invalid_argument)
     - supplying a semantic value of incorrect type (statically checked
       by correlating 'a nonterminal with 'a) *)

246 247
  let feed_failure () =
    invalid_arg "feed: outgoing transition does not exist"
248

249
  let feed_nonterminal nt startp semv endp (env : _ E.env) =
250
    let source : state = env.current in
251 252
    match ET.maybe_goto_nt source nt with
    | None ->
253 254
        feed_failure()
    | Some (target : state) ->
255 256 257 258 259 260 261
        (* Push a new cell onto the stack, containing the identity of the state
           that we are leaving. The semantic value [semv] and positions [startp]
           and [endp] contained in the new cell are provided by the caller. *)
        let stack = { state = source; semv; startp; endp; next = env.stack } in
        (* Move to the target state. *)
        { env with stack; current = target }

262
  let feed_terminal terminal startp semv endp env =
263 264 265 266 267
    ET.action
      env.current
      terminal
      semv
      (fun env _please_discard terminal semv s' ->
268 269
        if log then
          Log.shift terminal s';
270 271 272 273 274
        let stack = {
          state = env.current; semv; startp; endp; next = env.stack;
        } in
        { env with stack; current = s' }
      )
275 276
      (fun _env _prod -> feed_failure())
      (fun _env ->       feed_failure())
277 278
      env

279 280 281 282 283 284 285 286
  let feed symbol startp semv endp env =
    let semv : semantic_value = Obj.repr semv in
    match symbol with
    | N nt ->
        feed_nonterminal (n2i nt) startp semv endp env
    | T terminal ->
        feed_terminal (t2i terminal) startp semv endp env

287
end