6aa3056dac912160d3edd4111be84a6dfbdcee45
[bpt/coccinelle.git] / bundles / menhirLib / menhir-20120123 / src / keyword.ml
1 (**************************************************************************)
2 (* *)
3 (* Menhir *)
4 (* *)
5 (* François Pottier, INRIA Rocquencourt *)
6 (* Yann Régis-Gianas, PPS, Université Paris Diderot *)
7 (* *)
8 (* Copyright 2005-2008 Institut National de Recherche en Informatique *)
9 (* et en Automatique. All rights reserved. This file is distributed *)
10 (* under the terms of the Q Public License version 1.0, with the change *)
11 (* described in file LICENSE. *)
12 (* *)
13 (**************************************************************************)
14
15 (* This module provides some type and function definitions
16 that help deal with the keywords that we recognize within
17 semantic actions. *)
18
19 (* ------------------------------------------------------------------------- *)
20 (* Types. *)
21
22 (* The user can request position information either at type
23 [int] (a simple offset) or at type [Lexing.position]. *)
24
25 type flavor =
26 | FlavorOffset
27 | FlavorPosition
28
29 (* The user can request position information about the
30 start or end of a symbol. *)
31
32 type where =
33 | WhereStart
34 | WhereEnd
35
36 (* The user can request position information about a production's
37 left-hand side or about one of the symbols in its right-hand
38 side, which he can refer to by position or by name. *)
39
40 type subject =
41 | Left
42 | RightDollar of int
43 | RightNamed of string
44
45 (* Keywords inside semantic actions. They allow access to semantic
46 values or to position information. *)
47
48 type keyword =
49 | Dollar of int
50 | Position of subject * where * flavor
51 | PreviousError
52 | SyntaxError
53
54 (* ------------------------------------------------------------------------- *)
55 (* These auxiliary functions help map a [Position] keyword to the
56 name of the variable that the keyword is replaced with. *)
57
58 let where = function
59 | WhereStart ->
60 "start"
61 | WhereEnd ->
62 "end"
63
64 let subject = function
65 | Left ->
66 ""
67 | RightDollar i ->
68 Printf.sprintf "__%d_" i
69 | RightNamed id ->
70 Printf.sprintf "_%s_" id
71
72 let flavor = function
73 | FlavorPosition ->
74 "pos"
75 | FlavorOffset ->
76 "ofs"
77
78 let posvar s w f =
79 Printf.sprintf "_%s%s%s" (where w) (flavor f) (subject s)
80
81 (* ------------------------------------------------------------------------- *)
82 (* Sets of keywords. *)
83
84 module KeywordSet =
85 struct
86 include Set.Make (struct
87 type t = keyword
88 let compare = compare
89 end)
90
91 (* This converts a list of keywords with positions into a set of keywords. *)
92 let from_list keywords =
93 List.fold_right add keywords empty
94
95 end
96