keyword.mli 3.16 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
(* This module provides some type and function definitions
   that help deal with the keywords that we recognize within
   semantic actions. *)

POTTIER Francois's avatar
POTTIER Francois committed
18 19 20 21 22 23 24
(* The user can request position information either at several types:
   - a simple offset of type [int], e.g., via $startofs;
   - a position of type [Lexing.position], e.g., via $startpos;
   - a location, e.g., via $loc.
   A location is currently represented as a pair of positions, but
   this might change in the future; we may allow the user to choose
   a custom type of locations. *)
25 26 27 28

type flavor =
  | FlavorOffset
  | FlavorPosition
29
  | FlavorLocation
30

31 32 33
(* 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. *)
34 35

type where =
POTTIER Francois's avatar
POTTIER Francois committed
36 37 38
  | WhereSymbolStart
  | WhereStart
  | WhereEnd
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
42 43 44
   side, which he must refer to by name. (Referring to its symbol
   by its position, using [$i], is permitted in the concrete
   syntax, but the lexer eliminates this form.)
45

46
   We add a new subject, [Before], which corresponds to [$endpos($0)]
47
   in concrete syntax. We adopt the (slightly awkward) convention that
48 49
   when the subject is [Before], the [where] component must be [WhereEnd].

POTTIER Francois's avatar
POTTIER Francois committed
50 51 52 53 54
   If [flavor] is [FlavorLocation], then [where] must be [WhereSymbolStart] or
   [WhereStart]. In the former case, [subject] must be [Left]; this
   corresponds to $sloc in concrete syntax. In the latter case, [subject] must
   be [Left] or [RightNamed _]; this corresponds to $loc and $loc(x) in
   concrete syntax. *)
55

56
type subject =
57
  | Before
58 59 60 61
  | Left
  | RightNamed of string

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

64
type keyword =
65 66 67 68 69 70 71 72 73
  | Position of subject * where * flavor
  | SyntaxError

(* This maps a [Position] keyword to the name of the variable that the
   keyword is replaced with. *)

val posvar: subject -> where -> flavor -> string

(* Sets of keywords. *)
74

POTTIER Francois's avatar
POTTIER Francois committed
75
module KeywordSet : sig
76 77 78
  include Set.S with type elt = keyword
  val map: (keyword -> keyword) -> t -> t
end