Debian Codegraph @

gramlib/grammar.mli

raw symbols SCIP
1 (* camlp5r *)
2 (* grammar.mli,v *)
3 (* Copyright (c) INRIA 2007-2017 *)
4
5 (** Extensible grammars.
6
7 This module implements the Camlp5 extensible grammars system.
8 Grammars entries can be extended using the [EXTEND] statement,
9 added by loading the Camlp5 [pa_extend.cmo] file. *)
10
11 exception ParseError of string
12 (** Raised by parsers when the first component of a stream pattern is
13 accepted, but one of the following components is rejected. *)
14
15 (** {6 Functorial interface} *)
16
17 (** Alternative for grammars use. Grammars are no more Ocaml values:
18 there is no type for them. Modules generated preserve the
19 rule "an entry cannot call an entry of another grammar" by
20 normal OCaml typing. *)
21
22 (** The input signature for the functor [Grammar.GMake]: [te] is the
23 type of the tokens. *)
24
25 type norec
26 type mayrec
27
28 module type S = sig
29 type keyword_state
30 type te
31 type 'c pattern
32 type ty_pattern = TPattern : 'a pattern -> ty_pattern
33
34 type peek_error = unit
35
36 type 'a parser_v = ('a, peek_error) result
37 (** Recoverable parsing errors are signaled use [Error]. To be
38 correctly recovered we must not have consumed any tokens since
39 the last choice point, ie we only peeked at the stream.
40
41 Other errors are signaled using the [ParseError] exception or
42 even arbitrary exceptions. *)
43
44 (** Type combinators to factor the module type between explicit
45 state passing in Grammar and global state in Procq *)
46
47 type 'a with_gstate
48 (** Reader of grammar state *)
49
50 type 'a with_kwstate
51 (** Read keyword state *)
52
53 type 'a with_estate
54 (** Read entry state *)
55
56 type 'a mod_estate
57 (** Read/write entry state *)
58
59 module Parsable : sig
60 type t
61 val make : ?loc:Loc.t -> (unit,char) Stream.t -> t
62 val comments : t -> ((int * int) * string) list
63 val loc : t -> Loc.t
64 val consume : t -> int -> unit with_kwstate
65 end
66
67 module Entry : sig
68 type 'a t
69 val make : string -> 'a t mod_estate
70 val parse : 'a t -> Parsable.t -> 'a with_gstate
71 val name : 'a t -> string
72 type 'a parser_fun = { parser_fun : keyword_state -> (keyword_state,te) LStream.t -> 'a parser_v }
73 val of_parser : string -> 'a parser_fun -> 'a t mod_estate
74 val parse_token_stream : 'a t -> (keyword_state,te) LStream.t -> 'a parser_v with_gstate
75 val print : Format.formatter -> 'a t -> unit with_kwstate with_estate
76 val is_empty : 'a t -> bool with_estate
77
78 type any_t = Any : 'a t -> any_t
79 val accumulate_in : any_t list -> any_t list CString.Map.t with_estate
80 val all_in : unit -> any_t list CString.Map.t with_estate
81 end
82
83 module rec Symbol : sig
84
85 type ('self, 'trec, 'a) t
86 val nterm : 'a Entry.t -> ('self, norec, 'a) t
87 val nterml : 'a Entry.t -> string -> ('self, norec, 'a) t
88 val list0 : ('self, 'trec, 'a) t -> ('self, 'trec, 'a list) t
89 val list0sep :
90 ('self, 'trec, 'a) t -> ('self, norec, unit) t ->
91 ('self, 'trec, 'a list) t
92 val list1 : ('self, 'trec, 'a) t -> ('self, 'trec, 'a list) t
93 val list1sep :
94 ('self, 'trec, 'a) t -> ('self, norec, unit) t ->
95 ('self, 'trec, 'a list) t
96 val opt : ('self, 'trec, 'a) t -> ('self, 'trec, 'a option) t
97 val self : ('self, mayrec, 'self) t
98 val next : ('self, mayrec, 'self) t
99 val token : 'c pattern -> ('self, norec, 'c) t
100 val tokens : ty_pattern list -> ('self, norec, unit) t
101 val rules : 'a Rules.t list -> ('self, norec, 'a) t
102
103 end and Rule : sig
104
105 type ('self, 'trec, 'f, 'r) t
106
107 val stop : ('self, norec, 'r, 'r) t
108 val next :
109 ('self, _, 'a, 'r) t -> ('self, _, 'b) Symbol.t ->
110 ('self, mayrec, 'b -> 'a, 'r) t
111 val next_norec :
112 ('self, norec, 'a, 'r) Rule.t -> ('self, norec, 'b) Symbol.t ->
113 ('self, norec, 'b -> 'a, 'r) t
114
115 end and Rules : sig
116
117 type 'a t
118 val make : (_, norec, 'f, Loc.t -> 'a) Rule.t -> 'f -> 'a t
119
120 end
121
122 module Production : sig
123 type 'a t
124 val make : ('a, _, 'f, Loc.t -> 'a) Rule.t -> 'f -> 'a t
125 end
126
127 type 'a single_extend_statement =
128 string option * Gramext.g_assoc option * 'a Production.t list
129
130 type 'a extend_statement =
131 | Reuse of string option * 'a Production.t list
132 (** Extend an existing level by its optional given name. If None, picks the topmost level. *)
133 | Fresh of Gramext.position * 'a single_extend_statement list
134 (** Create a level at the given position. *)
135
136 val generalize_symbol : ('a, 'tr, 'c) Symbol.t -> ('b, norec, 'c) Symbol.t option
137
138 (* Used in custom entries, should tweak? *)
139 val level_of_nonterm : ('a, norec, 'c) Symbol.t -> string option
140
141 end
142
143 (* Interface private to clients *)
144 module type ExtS = sig
145
146 type keyword_state
147
148 module EState : sig
149 type t
150 val empty : t
151 end
152 module GState : sig
153 type t = {
154 estate : EState.t;
155 kwstate : keyword_state;
156 recover : bool;
157 has_non_assoc : bool;
158 }
159 end
160
161 include S
162 with type keyword_state := keyword_state
163 and type 'a with_gstate := GState.t -> 'a
164 and type 'a with_kwstate := keyword_state -> 'a
165 and type 'a with_estate := EState.t -> 'a
166 and type 'a mod_estate := EState.t -> EState.t * 'a
167
168 type 's add_kw = { add_kw : 'c. 's -> 'c pattern -> 's }
169
170 val safe_extend : 's add_kw -> EState.t -> 's -> 'a Entry.t -> 'a extend_statement -> EState.t * 's
171
172 module Unsafe : sig
173 val existing_entry : EState.t -> 'a Entry.t -> EState.t
174 val existing_of_parser : EState.t -> 'a Entry.t -> 'a Entry.parser_fun -> EState.t
175 end
176
177 end
178
179 (** Signature type of the functor [Grammar.GMake]. The types and
180 functions are almost the same than in generic interface, but:
181 - Grammars are not values. Functions holding a grammar as parameter
182 do not have this parameter yet.
183 - The type [parsable] is used in function [parse] instead of
184 the char stream, avoiding the possible loss of tokens.
185 - The type of tokens (expressions and patterns) can be any
186 type (instead of (string * string)); the module parameter
187 must specify a way to show them as (string * string) *)
188
189 module GMake (L : Plexing.S) : ExtS
190 with type keyword_state := L.keyword_state
191 and type te := L.te
192 and type 'c pattern := 'c L.pattern