keyword.ml 3.23 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 19 20 21 22 23 24 25 26
(* This module provides some type and function definitions
   that help deal with the keywords that we recognize within
   semantic actions. *)

(* ------------------------------------------------------------------------- *)
(* Types. *)

(* The user can request position information either at type
   [int] (a simple offset) or at type [Lexing.position]. *)

type flavor =
  | FlavorOffset
  | FlavorPosition
27
  | FlavorLocation
28

29 30 31
(* The user can request position information about the $start or $end
   of a symbol. Also, $symbolstart requests the computation of the
   start position of the first nonempty element in a production. *)
32 33

type where =
34 35 36
| WhereSymbolStart
| WhereStart
| WhereEnd
37 38 39 40 41

(* The user can request position information about a production's
   left-hand side or about one of the symbols in its right-hand
   side, which he can refer to by position or by name. *)

42
type subject =
43
  | Before
44 45 46 47 48 49
  | Left
  | RightNamed of string

(* Keywords inside semantic actions. They allow access to semantic
   values or to position information. *)

50
type keyword =
51 52 53 54 55 56 57 58
  | Position of subject * where * flavor
  | SyntaxError

(* ------------------------------------------------------------------------- *)
(* These auxiliary functions help map a [Position] keyword to the
   name of the variable that the keyword is replaced with. *)

let where = function
59 60
  | WhereSymbolStart ->
      "symbolstart"
61 62 63 64 65 66
  | WhereStart ->
      "start"
  | WhereEnd ->
      "end"

let subject = function
67
  | Before ->
68
      "__0_"
69 70 71 72 73 74 75 76 77 78
  | Left ->
      ""
  | RightNamed id ->
      Printf.sprintf "_%s_" id

let flavor = function
  | FlavorPosition ->
      "pos"
  | FlavorOffset ->
      "ofs"
79 80
  | FlavorLocation ->
      "loc"
81 82

let posvar s w f =
83 84 85 86 87 88 89 90 91
  match w, f with
  | _, (FlavorOffset | FlavorPosition) ->
      Printf.sprintf "_%s%s%s" (where w) (flavor f) (subject s)
  | WhereSymbolStart, FlavorLocation ->
      "_sloc"
  | WhereStart, FlavorLocation ->
      Printf.sprintf "_loc%s" (subject s)
  | _ ->
      assert false
92 93 94 95

(* ------------------------------------------------------------------------- *)
(* Sets of keywords. *)

96 97 98
module KeywordSet = struct

  include Set.Make (struct
99 100 101
    type t = keyword
    let compare = compare
  end)
102

103 104 105 106 107 108
  let map f keywords =
    fold (fun keyword accu ->
      add (f keyword) accu
    ) keywords empty

end