Debian Codegraph @

tools/coqdoc/cpretty.mll

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (*s Utility functions for the scanners *)
12
13 {
14 open Printf
15 open Lexing
16 open Common
17
18 (* A list function we need *)
19 let rec take n ls =
20 if n = 0 then [] else
21 match ls with
22 | [] -> []
23 | (l :: ls) -> l :: (take (n-1) ls)
24
25 (* count the number of spaces at the beginning of a string *)
26 let count_spaces s =
27 let n = String.length s in
28 let rec count c i =
29 if i == n then c,i else match s.[i] with
30 | '\t' -> count (c + (8 - (c mod 8))) (i + 1)
31 | ' ' -> count (c + 1) (i + 1)
32 | _ -> c,i
33 in
34 count 0 0
35
36 let count_newlines s =
37 let len = String.length s in
38 let n = ref 0 in
39 String.iteri (fun i c ->
40 match c with (* skip "\r\n" *)
41 | '\r' when i + 1 = len || s.[i+1] = '\n' -> incr n
42 | '\n' -> incr n
43 | _ -> ()) s;
44 !n
45
46 (* Whether a string starts with a newline (used on strings that might match the [nl] regexp) *)
47 let is_nl s = String.length s = 0 || let c = s.[0] in c = '\n' || c = '\r'
48
49 let remove_newline s =
50 let n = String.length s in
51 let rec count i = if i == n || s.[i] <> '\n' then i else count (i + 1) in
52 let i = count 0 in
53 i, String.sub s i (n - i)
54
55 let count_dashes s =
56 let c = ref 0 in
57 for i = 0 to String.length s - 1 do if s.[i] = '-' then incr c done;
58 !c
59
60 let cut_head_tail_spaces s =
61 let n = String.length s in
62 let rec look_up i = if i == n || s.[i] <> ' ' then i else look_up (i+1) in
63 let rec look_dn i = if i == -1 || s.[i] <> ' ' then i else look_dn (i-1) in
64 let l = look_up 0 in
65 let r = look_dn (n-1) in
66 if l <= r then String.sub s l (r-l+1) else s
67
68 let sec_title s =
69 let rec count lev i =
70 if s.[i] = '*' then
71 count (succ lev) (succ i)
72 else
73 let t = String.sub s i (String.length s - i) in
74 lev, cut_head_tail_spaces t
75 in
76 count 0 (String.index s '*')
77
78 let strip_eol s =
79 let eol = s.[String.length s - 1] = '\n' in
80 (eol, if eol then String.sub s 1 (String.length s - 1) else s)
81
82 let is_none x =
83 match x with
84 | None -> true
85 | Some _ -> false
86
87 let formatted : position option ref = ref None
88 let brackets = ref 0
89 let comment_level = ref 0
90 let in_proof = ref None
91
92 let in_env start stop =
93 let r = ref false in
94 let start_env () = r := true; start () in
95 let stop_env () = if !r then stop (); r := false in
96 (fun x -> !r), start_env, stop_env
97
98 let _, start_emph, stop_emph = in_env Output.start_emph Output.stop_emph
99 let in_quote, start_quote, stop_quote = in_env Output.start_quote Output.stop_quote
100
101 let url_buffer = Buffer.create 40
102 let url_name_buffer = Buffer.create 40
103
104 let backtrack lexbuf = lexbuf.lex_curr_pos <- lexbuf.lex_start_pos;
105 lexbuf.lex_curr_p <- lexbuf.lex_start_p
106
107 let backtrack_past_newline lexbuf =
108 let buf = lexeme lexbuf in
109 let splits = Str.bounded_split_delim (Str.regexp "['\n']") buf 2 in
110 match splits with
111 | [] -> ()
112 | (_ :: []) -> ()
113 | (s1 :: rest :: _) ->
114 let length_skip = 1 + String.length s1 in
115 lexbuf.lex_curr_pos <- lexbuf.lex_start_pos + length_skip
116
117 (* saving/restoring the PP state *)
118
119 type state = {
120 st_gallina : bool;
121 st_light : bool
122 }
123
124 let state_stack = Stack.create ()
125
126 let save_state () =
127 Stack.push { st_gallina = !prefs.gallina; st_light = !prefs.light } state_stack
128
129 let restore_state () =
130 let s = Stack.pop state_stack in
131 prefs := { !prefs with gallina = s.st_gallina };
132 prefs := { !prefs with light = s.st_light }
133
134 let begin_show () = save_state ();
135 prefs := { { !prefs with gallina = false } with light = false }
136 let end_show () = restore_state ()
137
138 let begin_details s =
139 save_state ();
140 prefs := { { !prefs with gallina = false } with light = false };
141 Output.start_details s
142 let end_details () = Output.stop_details (); restore_state ()
143
144 (* Reset the globals *)
145
146 let reset () =
147 formatted := None;
148 brackets := 0;
149 comment_level := 0
150
151 (* erasing of Section/End *)
152
153 let section_re = Str.regexp "[ \t]*Section"
154 let end_re = Str.regexp "[ \t]*End"
155 let is_section s = Str.string_match section_re s 0
156 let is_end s = Str.string_match end_re s 0
157
158 let sections_to_close = ref 0
159
160 let section_or_end s =
161 if is_section s then begin
162 incr sections_to_close; true
163 end else if is_end s then begin
164 if !sections_to_close > 0 then begin
165 decr sections_to_close; true
166 end else
167 false
168 end else
169 true
170
171 (* for item lists *)
172 type list_compare =
173 | Before
174 | StartLevel of int
175 | InLevel of int * bool
176
177 (* Before : we're before any levels
178 StartLevel : at the same column as the dash in a level
179 InLevel : after the dash of this level, but before any deeper dashes.
180 bool is true if this is the last level *)
181 let find_level levels cur_indent =
182 match levels with
183 | [] -> Before
184 | (l::ls) ->
185 if cur_indent < l then Before
186 else
187 (* cur_indent will never be less than the head of the list *)
188 let rec findind ls n =
189 match ls with
190 | [] -> InLevel (n,true)
191 | (l :: []) -> if cur_indent = l then StartLevel n
192 else InLevel (n,true)
193 | (l1 :: l2 :: ls) ->
194 if cur_indent = l1 then StartLevel n
195 else if cur_indent < l2 then InLevel (n,false)
196 else findind (l2 :: ls) (n+1)
197 in
198 findind (l::ls) 1
199
200 type is_start_list =
201 | Rule
202 | List of int
203 | Neither
204
205 let check_start_list str =
206 let n_dashes = count_dashes str in
207 let (n_spaces,_) = count_spaces str in
208 if n_dashes >= 4 && not !prefs.plain_comments then
209 Rule
210 else
211 if n_dashes = 1 && not !prefs.plain_comments then
212 List n_spaces
213 else
214 Neither
215
216 (* examine a string for subtitleness *)
217 let subtitle m s =
218 match Str.split_delim (Str.regexp ":") s with
219 | [] -> false
220 | (name::_) ->
221 if (cut_head_tail_spaces name) = m then
222 true
223 else
224 false
225
226
227 (* tokens pretty-print *)
228
229 let token_buffer = Buffer.create 1024
230
231 let token_re =
232 Str.regexp "[ \t]*(\\*\\*[ \t]+printing[ \t]+\\([^ \t]+\\)"
233 let printing_token_re =
234 Str.regexp
235 "[ \t]*\\(\\(%\\([^%]*\\)%\\)\\|\\(\\$[^$]*\\$\\)\\)?[ \t]*\\(#\\(\\(&#\\|[^#]\\)*\\)#\\)?"
236
237 let add_printing_token toks pps =
238 try
239 if Str.string_match token_re toks 0 then
240 let tok = Str.matched_group 1 toks in
241 if Str.string_match printing_token_re pps 0 then
242 let pp =
243 (try Some (Str.matched_group 3 pps) with _ ->
244 try Some (Str.matched_group 4 pps) with _ -> None),
245 (try Some (Str.matched_group 6 pps) with _ -> None)
246 in
247 Output.add_printing_token tok pp
248 with _ ->
249 ()
250
251 let remove_token_re =
252 Str.regexp
253 "[ \t]*(\\*\\*[ \t]+remove[ \t]+printing[ \t]+\\([^ \t]+\\)[ \t]*\\*)"
254
255 let remove_printing_token toks =
256 try
257 if Str.string_match remove_token_re toks 0 then
258 let tok = Str.matched_group 1 toks in
259 Output.remove_printing_token tok
260 with _ ->
261 ()
262
263 let output_indented_keyword s lexbuf =
264 let nbsp,isp = count_spaces s in
265 Output.indentation nbsp;
266 let s = String.sub s isp (String.length s - isp) in
267 Output.keyword s (lexeme_start lexbuf + isp)
268
269 let only_gallina () =
270 !prefs.gallina && !in_proof <> None
271
272 let parse_comments () =
273 !prefs.parse_comments && not (only_gallina ())
274
275 (* Advance lexbuf by n lines. Equivalent to calling [Lexing.new_line lexbuf] n times *)
276 let new_lines n lexbuf =
277 let lcp = lexbuf.lex_curr_p in
278 if lcp != dummy_pos then
279 lexbuf.lex_curr_p <-
280 { lcp with
281 pos_lnum = lcp.pos_lnum + n;
282 pos_bol = lcp.pos_cnum }
283
284 let print_position_p chan p =
285 Printf.fprintf chan "%s%d, character %d"
286 (if p.pos_fname = "" then "Line " else "File \"" ^ p.pos_fname ^ "\", line ")
287 p.pos_lnum (p.pos_cnum - p.pos_bol)
288
289 let print_position chan {lex_start_p = p} = print_position_p chan p
290
291 let warn msg lexbuf =
292 eprintf "%a, warning: %s\n" print_position lexbuf msg;
293 flush stderr
294
295 exception MismatchPreformatted of position
296
297 (* let debug lexbuf msg = Printf.printf "%a %s\n" print_position lexbuf.lex_start_p msg *)
298 }
299
300 (*s Regular expressions *)
301
302 let space = [' ' '\t']
303 let nl = "\r\n" | '\n' | '\r'
304 let space_nl = space | nl
305
306 let firstchar =
307 ['A'-'Z' 'a'-'z' '_'] |
308 (* superscript 1 *)
309 '\194' '\185' |
310 (* utf-8 latin 1 supplement *)
311 '\195' ['\128'-'\150'] |
312 '\195' ['\152'-'\182'] |
313 '\195' ['\184'-'\191'] |
314 (* utf-8 letterlike symbols *)
315 '\206' (['\145'-'\161'] | ['\163'-'\191']) |
316 '\207' (['\145'-'\191']) |
317 '\226' ('\130' [ '\128'-'\137' ] (* subscripts *)
318 | '\129' [ '\176'-'\187' ] (* superscripts *)
319 | '\132' ['\128'-'\191'] | '\133' ['\128'-'\143'])
320 let identchar =
321 firstchar | ['\'' '0'-'9' '@' ]
322 let id = firstchar identchar*
323 let pfx_id = (id '.')*
324 let identifier =
325 id | pfx_id id
326
327 (* This misses unicode stuff, and it adds "[" and "]". It's only an
328 approximation of idents - used for detecting whether an underscore
329 is part of an identifier or meant to indicate emphasis *)
330 let nonidentchar = [^ 'A'-'Z' 'a'-'z' '_' '[' ']' '\'' '0'-'9' '@' '\"' '\'' '`']
331
332 let printing_token = [^ ' ' '\t']*
333
334 let thm_token =
335 "Theorem"
336 | "Lemma"
337 | "Fact"
338 | "Remark"
339 | "Corollary"
340 | "Proposition"
341 | "Property"
342 | "Goal"
343
344 let prf_token =
345 "Next" space+ "Obligation"
346 | "Proof" (space* "." | space+ "with" | space+ "using")
347
348 let immediate_prf_token =
349 (* Approximation of a proof term, if not in the prf_token case *)
350 (* To be checked after prf_token *)
351 "Proof" space* [^ '.' 'w' 'u']
352
353 let def_token =
354 "Definition"
355 | "Let"
356 | "Let" space* "Fixpoint"
357 | "Let" space* "CoFixpoint"
358 | "Class"
359 | "SubClass"
360 | "Example"
361 | "Fixpoint"
362 | "Function"
363 | "Boxed"
364 | "CoFixpoint"
365 | "Record"
366 | "Variant"
367 | "Structure"
368 | "Scheme"
369 | "Combined" space+ "Scheme"
370 | "Inductive"
371 | "CoInductive"
372 | "Equations"
373 | "Instance"
374 | "Existing" space+ ("Instance" | "Instances" | "Class")
375 | "Declare" space+ "Instance"
376 | "Functional" space+ "Scheme"
377
378 let decl_token =
379 "Hypothesis"
380 | "Hypotheses"
381 | "Parameter" 's'?
382 | "Axiom" 's'?
383 | "Conjecture"
384 | "Primitive"
385 | "Constraint"
386 | "Universe"
387 | "Universes"
388 | "Register"
389
390 let gallina_ext =
391 "Module"
392 | "Include" space+ "Type"
393 | "Include"
394 | "Declare" space+ "Module"
395 | "Transparent"
396 | "Opaque"
397 | "Typeclasses" space+ "Transparent"
398 | "Typeclasses" space+ "Opaque"
399 | "Canonical"
400 | "Coercion"
401 | "Identity"
402 | "Implicit"
403 | "Tactic" space+ "Notation"
404 | "Section"
405 | "Context"
406 | "Variable" 's'?
407 | ("Hypothesis" | "Hypotheses")
408 | "End"
409
410 let notation_kw =
411 "Notation"
412 | "Infix"
413 | "Reserved" space+ "Notation"
414 | "Reserved" space+ "Infix"
415 | "Number" space+ "Notation"
416 | "String" space+ "Notation"
417 | "Enable" space+ "Notation"
418 | "Disable" space+ "Notation"
419
420 let commands =
421 "Pwd"
422 | "Cd"
423 | "Drop"
424 | "ProtectedLoop"
425 | "Quit"
426 | "Restart"
427 | "Load"
428 | "Add"
429 | "Remove" space+ "Loadpath"
430 | "Print"
431 | "Inspect"
432 | "About"
433 | "SearchAbout"
434 | "SearchPattern"
435 | "SearchRewrite"
436 | "Search"
437 | "Locate"
438 | "Eval"
439 | "Reset"
440 | "Check"
441 | "Type"
442
443 | "Section"
444 | "Chapter"
445 | "Variable" 's'?
446 | ("Hypothesis" | "Hypotheses")
447 | "End"
448
449 let end_kw =
450 immediate_prf_token | "Qed" | "Defined" | "Save" | "Admitted" | "Abort"
451
452 let extraction =
453 "Extraction"
454 | "Recursive" space+ "Extraction"
455 | "Extract"
456
457 let gallina_kw = thm_token | def_token | decl_token | gallina_ext | commands | extraction
458
459 let legacy_attr_kw =
460 "Local"
461 | "Global"
462 | "Polymorphic"
463 | "Monomorphic"
464 | "Cumulative"
465 | "NonCumulative"
466 | "Private"
467
468 let prog_kw =
469 "Program" space+ (legacy_attr_kw space+)* gallina_kw
470 | "Obligation"
471 | "Obligations"
472 | "Solve"
473
474 let hint_kw =
475 "Extern" | "Rewrite" | "Resolve" | "Immediate" | "Transparent" | "Opaque" | "Unfold" | "Constructors"
476
477 let set_kw =
478 "Printing" space+ ("Coercions" | "Universes" | "All")
479 | "Implicit" space+ "Arguments"
480
481
482 let gallina_kw_to_hide =
483 "Implicit" space+ "Arguments"
484 | "Arguments"
485 | "Ltac"
486 | "Ltac2"
487 | "From"
488 | "Require"
489 | "Import"
490 | "Export"
491 | "Load"
492 | "Hint" space+ hint_kw
493 | "Create" space+ "HintDb"
494 | "Removed" space+ "Hints"
495 | "Open"
496 | "Close"
497 | "Delimit"
498 | "Undelimit"
499 | "Declare" space+ "Scope"
500 | "Bind" space+ "Scope"
501 | "Format"
502 | "Transparent"
503 | "Opaque"
504 | "Strategy"
505 | "Derive"
506 | "Generalizable" space+ ("All" space+ "Variables" | "No" space+ "Variables" | "Variable" | "Variables")
507 | ("Declare" space+ ("Morphism" | "Step") )
508 | ("Set" | "Unset") space+ set_kw
509 | "Declare" space+ ("Left" | "Right") space+ "Step"
510 | "Debug" space+ ("On" | "Off")
511 | "Collection"
512
513
514 let section = "*" | "**" | "***" | "****"
515
516 let item_space = " "
517
518 let begin_hide = "(*" space* "begin" space+ "hide" space* "*)" space*
519 let end_hide = "(*" space* "end" space+ "hide" space* "*)" space*
520 let begin_show = "(*" space* "begin" space+ "show" space* "*)" space*
521 let end_show = "(*" space* "end" space+ "show" space* "*)" space*
522 let begin_details = "(*" space* "begin" space+ "details" space*
523 let end_details = "(*" space* "end" space+ "details" space* "*)" space*
524 (*
525 let begin_verb = "(*" space* "begin" space+ "verb" space* "*)"
526 let end_verb = "(*" space* "end" space+ "verb" space* "*)"
527 *)
528
529 (*s Scanning Rocq, at beginning of line *)
530
531 rule coq_bol = parse
532 | space* (nl+ as s)
533 { new_lines (String.length s) lexbuf;
534 if not (!in_proof <> None && (!prefs.gallina || !prefs.light))
535 then Output.empty_line_of_code ();
536 coq_bol lexbuf }
537 | space* "(**" (space_nl as s)
538 { if is_nl s then new_lines 1 lexbuf;
539 Output.end_coq (); Output.start_doc ();
540 let eol = doc_bol lexbuf in
541 Output.end_doc (); Output.start_coq ();
542 if eol then coq_bol lexbuf else coq lexbuf }
543 | space* "Comments" (space_nl as s)
544 { if is_nl s then new_lines 1 lexbuf;
545 Output.end_coq (); Output.start_doc ();
546 comments lexbuf;
547 Output.end_doc (); Output.start_coq ();
548 coq lexbuf }
549 | space* begin_hide nl
550 { new_lines 1 lexbuf; skip_hide lexbuf; coq_bol lexbuf }
551 | space* begin_show nl
552 { new_lines 1 lexbuf; begin_show (); coq_bol lexbuf }
553 | space* end_show nl
554 { new_lines 1 lexbuf; end_show (); coq_bol lexbuf }
555 | space* begin_details (* At this point, the comment remains open,
556 and will be closed by [details_body] *)
557 { let s = details_body lexbuf in
558 Output.end_coq (); begin_details s; Output.start_coq (); coq_bol lexbuf }
559 | space* end_details nl
560 { new_lines 1 lexbuf;
561 Output.end_coq (); end_details (); Output.start_coq (); coq_bol lexbuf }
562 | space* (legacy_attr_kw space+)* gallina_kw_to_hide
563 { let s = lexeme lexbuf in
564 if !prefs.light && section_or_end s then
565 let eol = skip_to_dot lexbuf in
566 if eol then (coq_bol lexbuf) else coq lexbuf
567 else
568 begin
569 output_indented_keyword s lexbuf;
570 let eol = body lexbuf in
571 if eol then coq_bol lexbuf else coq lexbuf
572 end }
573 | space* (legacy_attr_kw space+)* thm_token
574 { let s = lexeme lexbuf in
575 output_indented_keyword s lexbuf;
576 let eol = body lexbuf in
577 in_proof := Some eol;
578 if eol then coq_bol lexbuf else coq lexbuf }
579 | space* prf_token
580 { in_proof := Some true;
581 let eol =
582 if not !prefs.gallina then
583 begin backtrack lexbuf; body_bol lexbuf end
584 else
585 let s = lexeme lexbuf in
586 if s.[String.length s - 1] = '.' then false
587 else skip_to_dot lexbuf
588 in if eol then coq_bol lexbuf else coq lexbuf }
589 | space* end_kw {
590 let eol =
591 if not (only_gallina ()) then
592 begin backtrack lexbuf; body_bol lexbuf end
593 else skip_to_dot lexbuf
594 in
595 in_proof := None;
596 if eol then coq_bol lexbuf else coq lexbuf }
597 | space* (legacy_attr_kw space+)* gallina_kw
598 {
599 in_proof := None;
600 let s = lexeme lexbuf in
601 output_indented_keyword s lexbuf;
602 let eol= body lexbuf in
603 if eol then coq_bol lexbuf else coq lexbuf }
604 | space* (legacy_attr_kw space+)* prog_kw
605 {
606 in_proof := None;
607 let s = lexeme lexbuf in
608 output_indented_keyword s lexbuf;
609 let eol= body lexbuf in
610 if eol then coq_bol lexbuf else coq lexbuf }
611 | space* (legacy_attr_kw space+)* notation_kw
612 { let s = lexeme lexbuf in
613 output_indented_keyword s lexbuf;
614 let eol= start_notation_string lexbuf in
615 if eol then coq_bol lexbuf else coq lexbuf }
616
617 | space* "(**" space+ "printing" space+ printing_token space+
618 { let tok = lexeme lexbuf in
619 let s = printing_token_body lexbuf in
620 add_printing_token tok s;
621 coq_bol lexbuf }
622 | space* "(**" space+ "printing" space+
623 { warn "bad 'printing' command" lexbuf;
624 comment_level := 1;
625 ignore (comment lexbuf);
626 coq_bol lexbuf }
627 | space* "(**" space+ "remove" space+ "printing" space+
628 printing_token space* "*)"
629 { remove_printing_token (lexeme lexbuf);
630 coq_bol lexbuf }
631 | space* "(**" space+ "remove" space+ "printing" space+
632 { warn "bad 'remove printing' command" lexbuf;
633 comment_level := 1;
634 ignore (comment lexbuf);
635 coq_bol lexbuf }
636 | space* "(*"
637 { comment_level := 1;
638 let eol =
639 if parse_comments () then begin
640 let s = lexeme lexbuf in
641 let nbsp, isp = count_spaces s in
642 Output.indentation nbsp;
643 Output.start_comment ();
644 comment lexbuf
645 end else skipped_comment lexbuf in
646 if eol then coq_bol lexbuf else coq lexbuf }
647 | space* "#[" {
648 let eol = begin backtrack lexbuf; body_bol lexbuf end
649 in if eol then coq_bol lexbuf else coq lexbuf }
650 | eof
651 { () }
652 | _
653 { let eol =
654 if not !prefs.gallina then
655 begin backtrack lexbuf; body_bol lexbuf end
656 else
657 skip_to_dot_or_brace lexbuf
658 in
659 if eol then coq_bol lexbuf else coq lexbuf }
660
661 (*s Scanning Rocq elsewhere *)
662
663 and coq = parse
664 | nl
665 { new_lines 1 lexbuf; if not (only_gallina ()) then Output.line_break(); coq_bol lexbuf }
666 | "(**" (space_nl as s)
667 { if is_nl s then new_lines 1 lexbuf;
668 Output.end_coq (); Output.start_doc ();
669 let eol = doc_bol lexbuf in
670 Output.end_doc (); Output.start_coq ();
671 if eol then coq_bol lexbuf else coq lexbuf }
672 | "(*"
673 { comment_level := 1;
674 let eol =
675 if parse_comments () then begin
676 Output.start_comment ();
677 comment lexbuf
678 end else skipped_comment lexbuf in
679 if eol then coq_bol lexbuf else coq lexbuf }
680 | (nl+ as s) space* "]]"
681 { new_lines (count_newlines s) lexbuf;
682 if is_none !formatted then
683 begin
684 (* Isn't this an anomaly *)
685 let s = lexeme lexbuf in
686 let nlsp,s = remove_newline s in
687 let nbsp,isp = count_spaces s in
688 Output.indentation nbsp;
689 let loc = lexeme_start lexbuf + isp + nlsp in
690 Output.sublexer ']' loc;
691 Output.sublexer ']' (loc+1);
692 coq lexbuf
693 end }
694 | eof
695 { () }
696 | (legacy_attr_kw space+)* gallina_kw_to_hide
697 { let s = lexeme lexbuf in
698 if !prefs.light && section_or_end s then
699 begin
700 let eol = skip_to_dot lexbuf in
701 if eol then coq_bol lexbuf else coq lexbuf end
702 else
703 begin
704 Output.ident s None;
705 let eol=body lexbuf in
706 if eol then coq_bol lexbuf else coq lexbuf end }
707 | prf_token
708 { let eol =
709 if not !prefs.gallina then
710 begin backtrack lexbuf; body lexbuf end
711 else
712 let s = lexeme lexbuf in
713 let eol =
714 if s.[String.length s - 1] = '.' then false
715 else skip_to_dot lexbuf
716 in
717 eol
718 in if eol then coq_bol lexbuf else coq lexbuf }
719 | end_kw {
720 let eol =
721 if not !prefs.gallina then
722 begin backtrack lexbuf; body lexbuf end
723 else
724 let eol = skip_to_dot lexbuf in
725 if !in_proof <> Some true && eol then
726 Output.line_break ();
727 eol
728 in
729 in_proof := None;
730 if eol then coq_bol lexbuf else coq lexbuf }
731 | (legacy_attr_kw space+)* gallina_kw
732 { let s = lexeme lexbuf in
733 Output.ident s None;
734 let eol = body lexbuf in
735 if eol then coq_bol lexbuf else coq lexbuf }
736 | (legacy_attr_kw space+)* notation_kw
737 { let s = lexeme lexbuf in
738 Output.ident s None;
739 let eol= start_notation_string lexbuf in
740 if eol then coq_bol lexbuf else coq lexbuf }
741 | (legacy_attr_kw space+)* prog_kw
742 { let s = lexeme lexbuf in
743 Output.ident s None;
744 let eol = body lexbuf in
745 if eol then coq_bol lexbuf else coq lexbuf }
746 | "#["
747 { ignore(lexeme lexbuf);
748 Output.char '#'; Output.char '[';
749 let eol = body lexbuf in
750 if eol then coq_bol lexbuf else coq lexbuf }
751 | space+ { Output.char ' '; coq lexbuf }
752 | eof
753 { () }
754 | _ { let eol =
755 if not !prefs.gallina then
756 begin backtrack lexbuf; body lexbuf end
757 else
758 skip_to_dot_or_brace lexbuf
759 in
760 if eol then coq_bol lexbuf else coq lexbuf}
761
762 (*s Scanning documentation, at beginning of line *)
763
764 and doc_bol = parse
765 | space* section space+ ([^'\n' '\r' '*'] | '*'+ [^'\n' '\r' ')' '*'])* ('*'+ (nl as s))?
766 { if not (is_none s) then new_lines 1 lexbuf;
767 let eol, lex = strip_eol (lexeme lexbuf) in
768 let lev, s = sec_title lex in
769 if (!prefs.lib_subtitles) &&
770 (subtitle (Output.get_module false) s) then
771 ()
772 else
773 Output.section lev (fun () -> ignore (doc None (from_string s)));
774 if eol then doc_bol lexbuf else doc None lexbuf }
775 | ((space_nl* nl)? as s) (space* '-'+ as line)
776 { let nl_count = count_newlines s in
777 match check_start_list line with
778 | Neither -> backtrack_past_newline lexbuf; new_lines 1 lexbuf; doc None lexbuf
779 | List n ->
780 new_lines nl_count lexbuf;
781 if nl_count > 0 then Output.paragraph ();
782 Output.item 1; doc (Some [n]) lexbuf
783 | Rule ->
784 new_lines nl_count lexbuf;
785 Output.rule (); doc None lexbuf
786 }
787 | (space_nl* nl) as s
788 { new_lines (count_newlines s) lexbuf; Output.paragraph (); doc_bol lexbuf }
789 | "<<" space* nl
790 { new_lines 1 lexbuf; Output.start_verbatim false; verbatim_block lexbuf; doc_bol lexbuf }
791 | "<<"
792 { Output.start_verbatim true; verbatim_inline lexbuf; doc None lexbuf }
793 | eof
794 { true }
795 | '_'
796 { if !prefs.plain_comments then Output.char '_' else start_emph ();
797 doc None lexbuf }
798 | "" { doc None lexbuf }
799
800 (*s Scanning lists - using whitespace *)
801 and doc_list_bol indents = parse
802 | space* '-'
803 { let (n_spaces,_) = count_spaces (lexeme lexbuf) in
804 match find_level indents n_spaces with
805 | Before -> backtrack lexbuf; doc_bol lexbuf
806 | StartLevel n -> Output.item n; doc (Some (take n indents)) lexbuf
807 | InLevel (n,true) ->
808 let items = List.length indents in
809 Output.item (items+1);
810 doc (Some (List.append indents [n_spaces])) lexbuf
811 | InLevel (_,false) ->
812 backtrack lexbuf; doc_bol lexbuf
813 }
814 | "<<" space* nl
815 { new_lines 1 lexbuf; Output.start_verbatim false;
816 verbatim_block lexbuf;
817 doc_list_bol indents lexbuf }
818 | "<<" space*
819 { Output.start_verbatim true;
820 verbatim_inline lexbuf;
821 doc (Some indents) lexbuf }
822 | "[[" nl
823 { new_lines 1 lexbuf; formatted := Some lexbuf.lex_start_p;
824 Output.start_inline_coq_block ();
825 ignore(body_bol lexbuf);
826 Output.end_inline_coq_block ();
827 formatted := None;
828 doc_list_bol indents lexbuf }
829 | "[[[" nl
830 { new_lines 1 lexbuf; inf_rules (Some indents) lexbuf }
831 | space* nl space* '-'
832 { (* Like in the doc_bol production, these two productions
833 exist only to deal properly with whitespace *)
834 new_lines 1 lexbuf;
835 Output.paragraph ();
836 backtrack_past_newline lexbuf;
837 doc_list_bol indents lexbuf }
838 | space* nl space* _
839 { new_lines 1 lexbuf;
840 let buf' = lexeme lexbuf in
841 let buf =
842 let bufs = Str.split_delim (Str.regexp "['\n']") buf' in
843 match bufs with
844 | (_ :: s :: []) -> s
845 | (_ :: _ :: s :: _) -> s
846 | _ -> eprintf "Internal error bad_split2 - please report\n";
847 exit 1
848 in
849 let (n_spaces,_) = count_spaces buf in
850 match find_level indents n_spaces with
851 | StartLevel 1 | Before ->
852 (* Here we were at the beginning of a line, and it was blank.
853 The next line started before any list items. So: insert
854 a paragraph for the empty line, rewind to whatever's just
855 after the newline, then toss over to doc_bol for whatever
856 comes next. *)
857 Output.stop_item ();
858 Output.paragraph ();
859 backtrack_past_newline lexbuf;
860 doc_bol lexbuf
861 | StartLevel _ | InLevel _ ->
862 Output.paragraph ();
863 backtrack_past_newline lexbuf;
864 doc_list_bol indents lexbuf
865
866 }
867 | space* _
868 { let (n_spaces,_) = count_spaces (lexeme lexbuf) in
869 match find_level indents n_spaces with
870 | Before -> Output.stop_item (); backtrack lexbuf;
871 doc_bol lexbuf
872 | StartLevel n ->
873 Output.reach_item_level (n-1);
874 backtrack lexbuf;
875 doc (Some (take (n-1) indents)) lexbuf
876 | InLevel (n,_) ->
877 Output.reach_item_level n;
878 backtrack lexbuf;
879 doc (Some (take n indents)) lexbuf
880 }
881
882 (*s Scanning documentation elsewhere *)
883 and doc indents = parse
884 | nl
885 { new_lines 1 lexbuf;
886 Output.char '\n';
887 match indents with
888 | Some ls -> doc_list_bol ls lexbuf
889 | None -> doc_bol lexbuf }
890 | "[[" nl
891 { new_lines 1 lexbuf;
892 if !prefs.plain_comments
893 then (Output.char '['; Output.char '['; doc indents lexbuf)
894 else (formatted := Some lexbuf.lex_start_p;
895 Output.start_inline_coq_block ();
896 let eol = body_bol lexbuf in
897 Output.end_inline_coq_block (); formatted := None;
898 if eol then
899 match indents with
900 | Some ls -> doc_list_bol ls lexbuf
901 | None -> doc_bol lexbuf
902 else doc indents lexbuf)}
903 | "[[[" nl
904 { new_lines 1 lexbuf; inf_rules indents lexbuf }
905 | "[]"
906 { Output.proofbox (); doc indents lexbuf }
907 | "{{" { url lexbuf; doc indents lexbuf }
908 | "["
909 { if !prefs.plain_comments then Output.char '['
910 else (brackets := 1; Output.start_inline_coq (); escaped_coq lexbuf;
911 Output.end_inline_coq ()); doc indents lexbuf }
912 | "(*"
913 { backtrack lexbuf ;
914 let bol_parse = match indents with
915 | Some is -> doc_list_bol is
916 | None -> doc_bol
917 in
918 let eol =
919 if !prefs.parse_comments then comment lexbuf
920 else skipped_comment lexbuf in
921 if eol then bol_parse lexbuf else doc indents lexbuf }
922 | '*'* "*)" (space_nl* as s) "(**"
923 { let nl_count = count_newlines s in
924 new_lines nl_count lexbuf;
925 (match indents with
926 | Some _ -> Output.stop_item ()
927 | None -> ());
928 (* this says - if there is a blank line between the two comments,
929 insert one in the output too *)
930 if nl_count > 1 then Output.paragraph ();
931 doc_bol lexbuf
932 }
933 | '*'* "*)" space* nl
934 { new_lines 1 lexbuf; Output.char '\n'; true }
935 | '*'* "*)"
936 { false }
937 | "$"
938 { if !prefs.plain_comments then Output.char '$'
939 else (Output.start_latex_math (); escaped_math_latex lexbuf);
940 doc indents lexbuf }
941 | "$$"
942 { if !prefs.plain_comments then Output.char '$';
943 Output.char '$'; doc indents lexbuf }
944 | "%"
945 { if !prefs.plain_comments then Output.char '%'
946 else escaped_latex lexbuf; doc indents lexbuf }
947 | "%%"
948 { if !prefs.plain_comments then Output.char '%';
949 Output.char '%'; doc indents lexbuf }
950 | "#"
951 { if !prefs.plain_comments then Output.char '#'
952 else escaped_html lexbuf; doc indents lexbuf }
953 | "##"
954 { if !prefs.plain_comments then Output.char '#';
955 Output.char '#'; doc indents lexbuf }
956 | nonidentchar '_' nonidentchar
957 { List.iter (fun x -> Output.char (lexeme_char lexbuf x)) [0;1;2];
958 doc indents lexbuf}
959 | nonidentchar '_'
960 { Output.char (lexeme_char lexbuf 0);
961 if !prefs.plain_comments then Output.char '_' else start_emph () ;
962 doc indents lexbuf }
963 | '_' nonidentchar
964 { if !prefs.plain_comments then Output.char '_' else stop_emph () ;
965 Output.char (lexeme_char lexbuf 1);
966 doc indents lexbuf }
967 | "<<" space*
968 { Output.start_verbatim true; verbatim_inline lexbuf; doc indents lexbuf }
969 | '"'
970 { if !prefs.plain_comments
971 then Output.char '"'
972 else if in_quote ()
973 then stop_quote ()
974 else start_quote ();
975 doc indents lexbuf }
976 | eof
977 { false }
978 | _
979 { Output.char (lexeme_char lexbuf 0); doc indents lexbuf }
980
981 (*s Various escapings *)
982
983 and escaped_math_latex = parse
984 | "$" { Output.stop_latex_math () }
985 | eof { Output.stop_latex_math () }
986 | "*)"
987 { Output.stop_latex_math (); backtrack lexbuf }
988 | _ { Output.latex_char (lexeme_char lexbuf 0); escaped_math_latex lexbuf }
989
990 and escaped_latex = parse
991 | "%" { () }
992 | eof { () }
993 | "*)"
994 { backtrack lexbuf }
995 | _ { Output.latex_char (lexeme_char lexbuf 0); escaped_latex lexbuf }
996
997 and escaped_html = parse
998 | "#" { () }
999 | "&#"
1000 { Output.html_char '&'; Output.html_char '#'; escaped_html lexbuf }
1001 | "##"
1002 { Output.html_char '#'; escaped_html lexbuf }
1003 | eof { () }
1004 | "*)"
1005 { backtrack lexbuf }
1006 | _ { Output.html_char (lexeme_char lexbuf 0); escaped_html lexbuf }
1007
1008 and verbatim_block = parse
1009 | nl ">>" space* nl { new_lines 2 lexbuf; Output.verbatim_char false '\n'; Output.stop_verbatim false }
1010 | nl ">>"
1011 { new_lines 1 lexbuf;
1012 warn "missing newline after \">>\" block" lexbuf;
1013 Output.verbatim_char false '\n';
1014 Output.stop_verbatim false }
1015 | eof { warn "unterminated \">>\" block" lexbuf; Output.stop_verbatim false }
1016 | nl { new_lines 1 lexbuf; Output.verbatim_char false (lexeme_char lexbuf 0); verbatim_block lexbuf }
1017 | _ { Output.verbatim_char false (lexeme_char lexbuf 0); verbatim_block lexbuf }
1018
1019 and verbatim_inline = parse
1020 | nl { new_lines 1 lexbuf;
1021 warn "unterminated inline \">>\"" lexbuf;
1022 Output.char '\n';
1023 Output.stop_verbatim true }
1024 | ">>" { Output.stop_verbatim true }
1025 | eof { warn "unterminated inline \">>\"" lexbuf; Output.stop_verbatim true }
1026 | _ { Output.verbatim_char true (lexeme_char lexbuf 0); verbatim_inline lexbuf }
1027
1028 and url = parse
1029 | "}}" { Output.url (Buffer.contents url_buffer) None; Buffer.clear url_buffer }
1030 | "}" { url_name lexbuf }
1031 | _ { Buffer.add_char url_buffer (lexeme_char lexbuf 0); url lexbuf }
1032
1033 and url_name = parse
1034 | "}" { Output.url (Buffer.contents url_buffer) (Some (Buffer.contents url_name_buffer));
1035 Buffer.clear url_buffer; Buffer.clear url_name_buffer }
1036 | _ { Buffer.add_char url_name_buffer (lexeme_char lexbuf 0); url_name lexbuf }
1037
1038 (*s Rocq, inside quotations *)
1039
1040 and escaped_coq = parse
1041 | "]"
1042 { decr brackets;
1043 if !brackets > 0 then
1044 (Output.sublexer_in_doc ']'; escaped_coq lexbuf)
1045 else Tokens.flush_sublexer () }
1046 | "["
1047 { incr brackets;
1048 Output.sublexer_in_doc '['; escaped_coq lexbuf }
1049 | "(*"
1050 { Tokens.flush_sublexer (); comment_level := 1;
1051 ignore (if !prefs.parse_comments then comment lexbuf
1052 else skipped_comment lexbuf);
1053 escaped_coq lexbuf }
1054 | "*)"
1055 { (* likely to be a syntax error *)
1056 warn "unterminated \"]\"" lexbuf; backtrack lexbuf }
1057 | eof
1058 { Tokens.flush_sublexer () }
1059 | identifier
1060 { Tokens.flush_sublexer();
1061 Output.ident (lexeme lexbuf) None;
1062 escaped_coq lexbuf }
1063 | space_nl*
1064 { let str = lexeme lexbuf in
1065 Tokens.flush_sublexer();
1066 (if !prefs.inline_notmono then ()
1067 else Output.end_inline_coq ());
1068 String.iter Output.char str;
1069 (if !prefs.inline_notmono then ()
1070 else Output.start_inline_coq ());
1071 escaped_coq lexbuf }
1072 | _
1073 { Output.sublexer_in_doc (lexeme_char lexbuf 0);
1074 escaped_coq lexbuf }
1075
1076 (*s Rocq "Comments" command. *)
1077
1078 and comments = parse
1079 | space_nl+
1080 { Output.char ' '; comments lexbuf }
1081 | '"' [^ '"']* '"'
1082 { let s = lexeme lexbuf in
1083 let s = String.sub s 1 (String.length s - 2) in
1084 ignore (doc None (from_string s)); comments lexbuf }
1085 | ([^ '.' '"'] | '.' [^ ' ' '\t' '\n'])+
1086 { escaped_coq (from_string (lexeme lexbuf)); comments lexbuf }
1087 | "." (space_nl | eof)
1088 { () }
1089 | eof
1090 { () }
1091 | _
1092 { Output.char (lexeme_char lexbuf 0); comments lexbuf }
1093
1094 and skipped_comment = parse
1095 | "(*"
1096 { incr comment_level;
1097 skipped_comment lexbuf }
1098 | "*)" space* nl
1099 { new_lines 1 lexbuf;
1100 decr comment_level;
1101 if !comment_level > 0 then skipped_comment lexbuf else true }
1102 | "*)"
1103 { decr comment_level;
1104 if !comment_level > 0 then skipped_comment lexbuf else false }
1105 | eof { false }
1106 | _ { skipped_comment lexbuf }
1107
1108 and comment = parse
1109 | "(*"
1110 { incr comment_level;
1111 Output.start_comment ();
1112 comment lexbuf }
1113 | "*)" space* nl
1114 { new_lines 1 lexbuf;
1115 Output.end_comment ();
1116 Output.line_break ();
1117 decr comment_level;
1118 if !comment_level > 0 then comment lexbuf else true }
1119 | "*)"
1120 { Output.end_comment ();
1121 decr comment_level;
1122 if !comment_level > 0 then comment lexbuf else false }
1123 | "["
1124 { if !prefs.plain_comments then Output.char '['
1125 else (brackets := 1; Output.start_inline_coq ();
1126 escaped_coq lexbuf; Output.end_inline_coq ());
1127 comment lexbuf }
1128 | "[[" nl
1129 { new_lines 1 lexbuf;
1130 if !prefs.plain_comments then (Output.char '['; Output.char '[')
1131 else (formatted := Some lexbuf.lex_start_p;
1132 Output.start_inline_coq_block ();
1133 let _ = body_bol lexbuf in
1134 Output.end_inline_coq_block (); formatted := None);
1135 comment lexbuf }
1136 | "$"
1137 { if !prefs.plain_comments then Output.char '$'
1138 else (Output.start_latex_math (); escaped_math_latex lexbuf);
1139 comment lexbuf }
1140 | "$$"
1141 { if !prefs.plain_comments then Output.char '$';
1142 Output.char '$';
1143 comment lexbuf }
1144 | "%"
1145 { if !prefs.plain_comments then Output.char '%'
1146 else escaped_latex lexbuf;
1147 comment lexbuf }
1148 | "%%"
1149 { if !prefs.plain_comments then Output.char '%';
1150 Output.char '%';
1151 comment lexbuf }
1152 | "#"
1153 { if !prefs.plain_comments then Output.char '#'
1154 else escaped_html lexbuf;
1155 comment lexbuf }
1156 | "##"
1157 { if !prefs.plain_comments then Output.char '#';
1158 Output.char '#';
1159 comment lexbuf }
1160 | eof { false }
1161 | space+
1162 { Output.indentation (fst (count_spaces (lexeme lexbuf)));
1163 comment lexbuf }
1164 | nl
1165 { new_lines 1 lexbuf;
1166 Output.line_break ();
1167 comment lexbuf }
1168 | _ { Output.char (lexeme_char lexbuf 0);
1169 comment lexbuf }
1170
1171 and skip_to_dot = parse
1172 | '.' space* nl { new_lines 1 lexbuf; true }
1173 | eof | '.' space+ { false }
1174 | "(*"
1175 { comment_level := 1;
1176 ignore (skipped_comment lexbuf);
1177 skip_to_dot lexbuf }
1178 | _ { skip_to_dot lexbuf }
1179
1180 and skip_to_dot_or_brace = parse
1181 | '.' space* nl { new_lines 1 lexbuf; true }
1182 | eof | '.' space+ { false }
1183 | "(*"
1184 { comment_level := 1;
1185 ignore (skipped_comment lexbuf);
1186 skip_to_dot_or_brace lexbuf }
1187 | "}" space* nl
1188 { new_lines 1 lexbuf; true }
1189 | "}"
1190 { false }
1191 | space*
1192 { skip_to_dot_or_brace lexbuf }
1193 | _ { skip_to_dot lexbuf }
1194
1195 and body_bol = parse
1196 | space+
1197 { Output.indentation (fst (count_spaces (lexeme lexbuf))); body lexbuf }
1198 | "" { Output.indentation 0; body lexbuf }
1199
1200 and body = parse
1201 | nl { Tokens.flush_sublexer(); Output.line_break(); new_lines 1 lexbuf; body_bol lexbuf}
1202 | (nl+ as s) space* "]]" space* nl
1203 { new_lines (count_newlines s + 1) lexbuf;
1204 Tokens.flush_sublexer();
1205 if is_none !formatted then
1206 begin
1207 let s = lexeme lexbuf in
1208 let nlsp,s = remove_newline s in
1209 let _,isp = count_spaces s in
1210 let loc = lexeme_start lexbuf + nlsp + isp in
1211 Output.sublexer ']' loc;
1212 Output.sublexer ']' (loc+1);
1213 Tokens.flush_sublexer();
1214 body lexbuf
1215 end
1216 else
1217 begin
1218 Output.paragraph ();
1219 true
1220 end }
1221 | "]]" space* nl
1222 { Tokens.flush_sublexer();
1223 new_lines 1 lexbuf;
1224 if is_none !formatted then
1225 begin
1226 let loc = lexeme_start lexbuf in
1227 Output.sublexer ']' loc;
1228 Output.sublexer ']' (loc+1);
1229 Tokens.flush_sublexer();
1230 Output.line_break();
1231 body lexbuf
1232 end
1233 else
1234 begin
1235 Output.paragraph ();
1236 true
1237 end }
1238 | eof
1239 { Tokens.flush_sublexer();
1240 match !formatted with
1241 | None -> false
1242 | Some p -> raise (MismatchPreformatted p) }
1243 | '.' space* (nl as s | eof)
1244 { if not (is_none s) then new_line lexbuf;
1245 Tokens.flush_sublexer(); Output.char '.'; Output.line_break();
1246 if is_none !formatted then true else body_bol lexbuf }
1247 | '.' space* nl "]]" space* nl
1248 { new_lines 2 lexbuf;
1249 Tokens.flush_sublexer(); Output.char '.';
1250 if is_none !formatted then
1251 begin
1252 eprintf "Error: stray ]] at %d\n" (lexeme_start lexbuf);
1253 flush stderr;
1254 exit 1
1255 end
1256 else
1257 begin
1258 Output.paragraph ();
1259 true
1260 end
1261 }
1262 | '.' space+
1263 { Tokens.flush_sublexer(); Output.char '.'; Output.char ' ';
1264 if is_none !formatted then false else body lexbuf }
1265 | "(**" (space_nl as s)
1266 { if is_nl s then new_line lexbuf;
1267 Tokens.flush_sublexer(); Output.end_coq (); Output.start_doc ();
1268 let eol = doc_bol lexbuf in
1269 Output.end_doc (); Output.start_coq ();
1270 if eol then body_bol lexbuf else body lexbuf }
1271 | "(*"
1272 { Tokens.flush_sublexer(); comment_level := 1;
1273 let eol =
1274 if parse_comments () then begin
1275 Output.start_comment ();
1276 comment lexbuf
1277 end else begin
1278 let eol = skipped_comment lexbuf in
1279 if eol then Output.line_break();
1280 eol
1281 end in
1282 if eol then body_bol lexbuf else body lexbuf }
1283 | "where"
1284 { Tokens.flush_sublexer();
1285 Output.ident (lexeme lexbuf) None;
1286 start_notation_string lexbuf }
1287 | identifier
1288 { Tokens.flush_sublexer();
1289 Output.ident (lexeme lexbuf) (Some (lexeme_start lexbuf));
1290 body lexbuf }
1291 | ".."
1292 { Tokens.flush_sublexer(); Output.char '.'; Output.char '.';
1293 body lexbuf }
1294 | '"'
1295 { Tokens.flush_sublexer(); Output.char '"';
1296 string lexbuf;
1297 body lexbuf }
1298 | space
1299 { Tokens.flush_sublexer(); Output.char (lexeme_char lexbuf 0);
1300 body lexbuf }
1301
1302 | _ { let c = lexeme_char lexbuf 0 in
1303 Output.sublexer c (lexeme_start lexbuf);
1304 body lexbuf }
1305
1306 and start_notation_string = parse
1307 | space { Tokens.flush_sublexer(); Output.char (lexeme_char lexbuf 0);
1308 start_notation_string lexbuf }
1309 | '"' (* a true notation *)
1310 { Output.sublexer '"' (lexeme_start lexbuf);
1311 notation_string lexbuf;
1312 body lexbuf }
1313 | _ (* an abbreviation *)
1314 { backtrack lexbuf; body lexbuf }
1315
1316 and notation_string = parse
1317 | "\"\""
1318 { Output.char '"'; Output.char '"'; (* Unlikely! *)
1319 notation_string lexbuf }
1320 | '"'
1321 { Tokens.flush_sublexer(); Output.char '"' }
1322 | _ { let c = lexeme_char lexbuf 0 in
1323 Output.sublexer c (lexeme_start lexbuf);
1324 notation_string lexbuf }
1325
1326 and string = parse
1327 | "\"\"" { Output.char '"'; Output.char '"'; string lexbuf }
1328 | '"' { Output.char '"' }
1329 | _ { let c = lexeme_char lexbuf 0 in Output.char c; string lexbuf }
1330
1331 and skip_hide = parse
1332 | eof | end_hide nl { new_lines 1 lexbuf; () }
1333 | _ { skip_hide lexbuf }
1334
1335 (*s Reading token pretty-print *)
1336
1337 and printing_token_body = parse
1338 | "*)" (nl as s)? | eof
1339 { if not (is_none s) then new_lines 1 lexbuf;
1340 let s = Buffer.contents token_buffer in
1341 Buffer.clear token_buffer;
1342 s }
1343 | (nl | _) as s
1344 { if is_nl s then new_lines 1 lexbuf;
1345 Buffer.add_string token_buffer (lexeme lexbuf);
1346 printing_token_body lexbuf }
1347
1348 and details_body = parse
1349 | "*)" space* (nl as s)? | eof
1350 { if not (is_none s) then new_lines 1 lexbuf;
1351 None }
1352 | ":" space* { details_body_rec lexbuf }
1353
1354 and details_body_rec = parse
1355 | "*)" space* (nl as s)? | eof
1356 { if not (is_none s) then new_lines 1 lexbuf;
1357 let s = Buffer.contents token_buffer in
1358 Buffer.clear token_buffer;
1359 Some s }
1360 | _ { Buffer.add_string token_buffer (lexeme lexbuf);
1361 details_body_rec lexbuf }
1362
1363 (*s These handle inference rules, parsing the body segments of things
1364 enclosed in [[[ ]]] brackets *)
1365 and inf_rules indents = parse
1366 | space* nl (* blank line, before or between definitions *)
1367 { new_lines 1 lexbuf; inf_rules indents lexbuf }
1368 | "]]]" nl (* end of the inference rules block *)
1369 { new_lines 1 lexbuf;
1370 match indents with
1371 | Some ls -> doc_list_bol ls lexbuf
1372 | None -> doc_bol lexbuf }
1373 | _
1374 { backtrack lexbuf; (* anything else must be the first line in a rule *)
1375 inf_rules_assumptions indents [] lexbuf}
1376
1377 (* The inference rule parsing just collects the inference rule and then
1378 calls the output function once, instead of doing things incrementally
1379 like the rest of the lexer. If only there were a real parsing phase...
1380 *)
1381 and inf_rules_assumptions indents assumptions = parse
1382 | space* "---" '-'* [^ '\n']* nl (* hit the horizontal line *)
1383 { new_lines 1 lexbuf;
1384 let line = lexeme lexbuf in
1385 let (spaces,_) = count_spaces line in
1386 let dashes_and_name =
1387 cut_head_tail_spaces (String.sub line 0 (String.length line - 1))
1388 in
1389 let ldn = String.length dashes_and_name in
1390 let (dashes,name) =
1391 try (let i = String.index dashes_and_name ' ' in
1392 let d = String.sub dashes_and_name 0 i in
1393 let n = cut_head_tail_spaces
1394 (String.sub dashes_and_name (i+1) (ldn-i-1))
1395 in
1396 (d, Some n))
1397 with _ -> (dashes_and_name, None)
1398
1399 in
1400 inf_rules_conclusion indents (List.rev assumptions)
1401 (spaces, dashes, name) [] lexbuf }
1402 | [^ '\n']* nl (* if it's not the horizontal line, it's an assumption *)
1403 { new_lines 1 lexbuf;
1404 let line = lexeme lexbuf in
1405 let (spaces,_) = count_spaces line in
1406 let assumption = cut_head_tail_spaces
1407 (String.sub line 0 (String.length line - 1))
1408 in
1409 inf_rules_assumptions indents ((spaces,assumption)::assumptions)
1410 lexbuf }
1411
1412 (*s The conclusion is required to come immediately after the
1413 horizontal bar. It is allowed to contain multiple lines of
1414 text, like the assumptions. The conclusion ends when we spot a
1415 blank line or a ']]]'. *)
1416 and inf_rules_conclusion indents assumptions middle conclusions = parse
1417 | space* nl | space* "]]]" nl (* end of conclusions. *)
1418 { new_lines 2 lexbuf; backtrack lexbuf;
1419 Output.inf_rule assumptions middle (List.rev conclusions);
1420 inf_rules indents lexbuf }
1421 | space* [^ '\n']+ nl (* this is a line in the conclusion *)
1422 { new_lines 1 lexbuf;
1423 let line = lexeme lexbuf in
1424 let (spaces,_) = count_spaces line in
1425 let conc = cut_head_tail_spaces (String.sub line 0
1426 (String.length line - 1))
1427 in
1428 inf_rules_conclusion indents assumptions middle
1429 ((spaces,conc) :: conclusions) lexbuf
1430 }
1431
1432 (*s A small scanner to support the chapter subtitle feature *)
1433 and st_start m = parse
1434 | "(*" "*"+ space+ "*" space+
1435 { st_modname m lexbuf }
1436 | _
1437 { None }
1438
1439 and st_modname m = parse
1440 | identifier space* ":" space*
1441 { if subtitle m (lexeme lexbuf) then
1442 st_subtitle lexbuf
1443 else
1444 None
1445 }
1446 | _
1447 { None }
1448
1449 and st_subtitle = parse
1450 | [^ '\n']* '\n'
1451 { let st = lexeme lexbuf in
1452 let i = try Str.search_forward (Str.regexp "\\**)") st 0 with
1453 Not_found ->
1454 (eprintf "unterminated comment at beginning of file\n";
1455 exit 1)
1456 in
1457 Some (cut_head_tail_spaces (String.sub st 0 i))
1458 }
1459 | _
1460 { None }
1461 (*s Applying the scanners to files *)
1462
1463 {
1464 (* coq_bol with error handling *)
1465 let coq_bol' f lb =
1466 try coq_bol lb with
1467 | MismatchPreformatted p ->
1468 Printf.eprintf "%a: mismatched \"[[\"\n" print_position_p p;
1469 exit 1
1470
1471 let coq_file f m =
1472 reset ();
1473 let c = open_in f in
1474 let lb = from_channel c in
1475 let lb = { lb with lex_curr_p = { lb.lex_curr_p with pos_fname = f };
1476 lex_start_p = { lb.lex_start_p with pos_fname = f } } in
1477 (Index.current_library := m;
1478 Output.initialize ();
1479 Output.start_module ();
1480 Output.start_coq (); coq_bol' f lb; Output.end_coq ();
1481 close_in c)
1482
1483 let detect_subtitle f m =
1484 let c = open_in f in
1485 let lb = from_channel c in
1486 let sub = st_start m lb in
1487 close_in c;
1488 sub
1489 }