1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

# 11 "parsing/g_prim.mlg"
 

open Names
open Libnames

open Pcoq.Prim

let prim_kw = ["{"; "}"; "["; "]"; "("; ")"; "'"; "%"; "|"]
let _ = List.iter CLexer.add_keyword prim_kw


let local_make_qualid loc l id = make_qualid ~loc (DirPath.make l) id

let check_int loc = function
  | { NumTok.int = i; frac = ""; exp = "" } -> i
  | _ -> CErrors.user_err ~loc (Pp.str "This number is not an integer.")

let my_int_of_string loc s =
  try
    int_of_string s
  with Failure _ ->
    CErrors.user_err ~loc (Pp.str "This number is too large.")

let rec contiguous tok n m =
    n == m
  ||
    let (_, ep) = Loc.unloc (tok n) in
    let (bp, _) = Loc.unloc (tok (n + 1)) in
    Int.equal ep bp && contiguous tok (succ n) m

let rec lookahead_kwds strm n = function
  | [] -> ()
  | x :: xs ->
      let toks = Stream.npeek (n+1) strm in
      match List.nth toks n with
      | Tok.KEYWORD y ->
          if String.equal x y then lookahead_kwds strm (succ n) xs
          else raise Stream.Failure
      | _ -> raise Stream.Failure
      | exception (Failure _) -> raise Stream.Failure

(* [test_nospace m] fails if the next m tokens are not contiguous keywords *)
let test_nospace m = assert(m <> []); Pcoq.Entry.of_parser "test_nospace"
   (fun tok strm ->
      let n = Stream.count strm in
      lookahead_kwds strm 0 m;
      if contiguous tok n (n + List.length m - 1) then ()
      else raise Stream.Failure)

let test_nospace_pipe_closedcurly =
  test_nospace ["|"; "}"]




let _ = let field = Pcoq.Entry.create "field"
        and fields = Pcoq.Entry.create "fields"
        and basequalid = Pcoq.Entry.create "basequalid"
        in
        let () =
        Pcoq.grammar_extend preident None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop,
                              (Extend.Atoken (Tok.PIDENT (None)))),
                 (fun s loc -> 
# 73 "parsing/g_prim.mlg"
                       s 
                               ))])])
        in let () =
        Pcoq.grammar_extend ident None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop,
                              (Extend.Atoken (Tok.PIDENT (None)))),
                 (fun s loc -> 
# 76 "parsing/g_prim.mlg"
                       Id.of_string s 
                               ))])])
        in let () =
        Pcoq.grammar_extend pattern_ident None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Next (Extend.Stop,
                                           (Extend.Atoken (Tok.PLEFTQMARK))),
                              (Extend.Aentry ident)),
                 (fun id _ loc -> 
# 79 "parsing/g_prim.mlg"
                                   id 
                                  ))])])
        in let () =
        Pcoq.grammar_extend pattern_identref None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry pattern_ident)),
                 (fun id loc -> 
# 82 "parsing/g_prim.mlg"
                                CAst.make ~loc id 
                                ))])])
        in let () =
        Pcoq.grammar_extend var None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry ident)),
                 (fun id loc -> 
# 85 "parsing/g_prim.mlg"
                        CAst.make ~loc id 
                                ))])])
        in let () =
        Pcoq.grammar_extend identref None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry ident)),
                 (fun id loc -> 
# 88 "parsing/g_prim.mlg"
                        CAst.make ~loc id 
                                ))])])
        in let () =
        Pcoq.grammar_extend field None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop,
                              (Extend.Atoken (Tok.PFIELD (None)))),
                 (fun s loc -> 
# 91 "parsing/g_prim.mlg"
                       Id.of_string s 
                               ))])])
        in let () =
        Pcoq.grammar_extend fields None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry field)),
                 (fun id loc -> 
# 95 "parsing/g_prim.mlg"
                        ([],id) 
                                ));
                Extend.Rule
                (Extend.Next (Extend.Next (Extend.Stop,
                                          (Extend.Aentry field)),
                             (Extend.Aentry fields)),
                (fun f id loc -> 
# 94 "parsing/g_prim.mlg"
                                    let (l,id') = f in (l@[id],id') 
                                 ))])])
        in let () =
        Pcoq.grammar_extend fullyqualid None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry ident)),
                 (fun id loc -> 
# 100 "parsing/g_prim.mlg"
                        CAst.make ~loc [id] 
                                ));
                Extend.Rule
                (Extend.Next (Extend.Next (Extend.Stop,
                                          (Extend.Aentry ident)),
                             (Extend.Aentry fields)),
                (fun f id loc -> 
# 99 "parsing/g_prim.mlg"
                                  let (l,id') = f in CAst.make ~loc @@ id::List.rev (id'::l) 
                                 ))])])
        in let () =
        Pcoq.grammar_extend basequalid None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry ident)),
                 (fun id loc -> 
# 105 "parsing/g_prim.mlg"
                        qualid_of_ident ~loc id 
                                ));
                Extend.Rule
                (Extend.Next (Extend.Next (Extend.Stop,
                                          (Extend.Aentry ident)),
                             (Extend.Aentry fields)),
                (fun f id loc -> 
# 104 "parsing/g_prim.mlg"
                                  let (l,id') = f in  local_make_qualid loc (l@[id]) id' 
                                 ))])])
        in let () =
        Pcoq.grammar_extend name None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry ident)),
                 (fun id loc -> 
# 110 "parsing/g_prim.mlg"
                        CAst.make ~loc @@ Name id 
                                ));
                Extend.Rule
                (Extend.Next (Extend.Stop, (Extend.Atoken (Tok.PIDENT (Some
                             ("_"))))),
                (fun _ loc -> 
# 109 "parsing/g_prim.mlg"
                        CAst.make ~loc Anonymous 
                              ))])])
        in let () =
        Pcoq.grammar_extend reference None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry ident)),
                 (fun id loc -> 
# 116 "parsing/g_prim.mlg"
                        local_make_qualid loc [] id 
                                ));
                Extend.Rule
                (Extend.Next (Extend.Next (Extend.Stop,
                                          (Extend.Aentry ident)),
                             (Extend.Aentry fields)),
                (fun f id loc -> 
# 113 "parsing/g_prim.mlg"
                                   
        let (l,id') = f in
        local_make_qualid loc (l@[id]) id' 
                                 ))])])
        in let () =
        Pcoq.grammar_extend by_notation None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Next (Extend.Stop,
                                           (Extend.Aentry ne_string)),
                              (Extend.Aopt (Extend.Arules [Extend.Rules 
                                                          (Extend.NextNoRec 
                                                          (Extend.NextNoRec 
                                                          (Extend.Stop,
                                                          (Extend.Atoken (Tok.PKEYWORD ("%")))),
                                                          (Extend.Atoken (Tok.PIDENT (None)))),
                                                          (fun key _ loc ->
                                                          
# 120 "parsing/g_prim.mlg"
                                                       key 
                                                          ))]))),
                 (fun sc s loc -> 
# 120 "parsing/g_prim.mlg"
                                                                    (s, sc) 
                                  ))])])
        in let () =
        Pcoq.grammar_extend smart_global None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry by_notation)),
                 (fun ntn loc -> 
# 124 "parsing/g_prim.mlg"
                               CAst.make ~loc @@ Constrexpr.ByNotation ntn 
                                 ));
                Extend.Rule
                (Extend.Next (Extend.Stop, (Extend.Aentry reference)),
                (fun c loc -> 
# 123 "parsing/g_prim.mlg"
                           CAst.make ~loc @@ Constrexpr.AN c 
                              ))])])
        in let () =
        Pcoq.grammar_extend qualid None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry basequalid)),
                 (fun qid loc -> 
# 127 "parsing/g_prim.mlg"
                              qid 
                                 ))])])
        in let () =
        Pcoq.grammar_extend ne_string None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop,
                              (Extend.Atoken (Tok.PSTRING (None)))),
                 (fun s loc -> 
# 131 "parsing/g_prim.mlg"
          if s="" then CErrors.user_err ~loc (Pp.str"Empty string."); s 
                               ))])])
        in let () =
        Pcoq.grammar_extend ne_lstring None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry ne_string)),
                 (fun s loc -> 
# 135 "parsing/g_prim.mlg"
                           CAst.make ~loc s 
                               ))])])
        in let () =
        Pcoq.grammar_extend dirpath None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Next (Extend.Stop,
                                           (Extend.Aentry ident)),
                              (Extend.Alist0 (Extend.Aentry field))),
                 (fun l id loc -> 
# 139 "parsing/g_prim.mlg"
          DirPath.make (List.rev (id::l)) 
                                  ))])])
        in let () =
        Pcoq.grammar_extend string None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop,
                              (Extend.Atoken (Tok.PSTRING (None)))),
                 (fun s loc -> 
# 142 "parsing/g_prim.mlg"
                        s 
                               ))])])
        in let () =
        Pcoq.grammar_extend lstring None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop, (Extend.Aentry string)),
                 (fun s loc -> 
# 145 "parsing/g_prim.mlg"
                        CAst.make ~loc s 
                               ))])])
        in let () =
        Pcoq.grammar_extend integer None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Next (Extend.Stop,
                                           (Extend.Atoken (Tok.PKEYWORD ("-")))),
                              (Extend.Atoken (Tok.PNUMERAL None))),
                 (fun i _ loc -> 
# 149 "parsing/g_prim.mlg"
                              - my_int_of_string loc (check_int loc i) 
                                 ));
                Extend.Rule
                (Extend.Next (Extend.Stop,
                             (Extend.Atoken (Tok.PNUMERAL None))),
                (fun i loc -> 
# 148 "parsing/g_prim.mlg"
                              my_int_of_string loc (check_int loc i) 
                              ))])])
        in let () =
        Pcoq.grammar_extend natural None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop,
                              (Extend.Atoken (Tok.PNUMERAL None))),
                 (fun i loc -> 
# 152 "parsing/g_prim.mlg"
                         my_int_of_string loc (check_int loc i) 
                               ))])])
        in let () =
        Pcoq.grammar_extend bigint None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Stop,
                              (Extend.Atoken (Tok.PNUMERAL None))),
                 (fun i loc -> 
# 155 "parsing/g_prim.mlg"
                         check_int loc i 
                               ))])])
        in let () =
        Pcoq.grammar_extend bar_cbrace None
        (None, [(None, None,
                [Extend.Rule
                 (Extend.Next (Extend.Next (Extend.Next (Extend.Stop,
                                                        (Extend.Aentry test_nospace_pipe_closedcurly)),
                                           (Extend.Atoken (Tok.PKEYWORD ("|")))),
                              (Extend.Atoken (Tok.PKEYWORD ("}")))),
                 (fun _ _ _ loc -> 
# 158 "parsing/g_prim.mlg"
                                                     () 
                                   ))])])
        in ()