1 /* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
30 let out t s = if !O.verbose_parser then prerr_endline ("-- " ^ t ^ " " ^ s)
32 let trim = HExtlib.trim_blanks
36 let cat x = String.concat " " x
38 let mk_vars local idents =
39 let kind = if local then T.Var else T.Con in
40 let map ident = T.Inline (local, kind, trim ident, "", None, []) in
44 String.sub s 1 (String.length s - 2)
47 String.sub s 1 (String.length s - 1)
50 [T.Coercion (false, str); T.Coercion (true, str)]
53 [T.Notation (false, str); T.Notation (true, str)]
57 | "Remark" -> Some `Remark
58 | "Lemma" -> Some `Lemma
59 | "Theorem" -> Some `Theorem
60 | "Definition" -> Some `Definition
61 | "Fixpoint" -> Some `Definition
62 | "CoFixpoint" -> Some `Definition
63 | "Let" -> Some `Definition
64 | "Scheme" -> Some `Theorem
68 let generate s = T.Inline (false, T.Con, ext ^ s, "", Some `Theorem, []) in
69 List.rev_map generate ["2"; "1"]
72 %token <string> SPC STR IDENT INT EXTRA AC OP CP OC CC OQ CQ DEF FS COE CN SC CM
73 %token <string> WITH ABBR IN LET TH PROOF QED VAR AX IND GEN SEC END UNX REQ XP SET NOT LOAD ID COERC MOR
77 %type <Types.items> items
79 /* SPACED TOKENS ************************************************************/
81 ident: xident spcs { $1 ^ $2 };
82 fs: FS spcs { $1 ^ $2 };
83 mor: MOR spcs { $1 ^ $2 };
84 th: TH spcs { $1 ^ $2 };
85 xlet: LET spcs { $1 ^ $2 };
86 proof: PROOF spcs { $1 ^ $2 };
87 qed: QED spcs { $1 ^ $2 };
88 gen: GEN spcs { $1 ^ $2 };
89 sec: SEC spcs { $1 ^ $2 };
90 xend: END spcs { $1 ^ $2 };
91 unx: UNX spcs { $1 ^ $2 };
92 def: DEF spcs { $1 ^ $2 };
93 op: OP spcs { $1 ^ $2 };
94 abbr: ABBR spcs { $1 ^ $2 };
95 var: VAR spcs { $1 ^ $2 };
96 ax: AX spcs { $1 ^ $2 };
97 req: REQ spcs { $1 ^ $2 };
98 load: LOAD spcs { $1 ^ $2 };
99 xp: XP spcs { $1 ^ $2 };
100 ind: IND spcs { $1 ^ $2 };
101 set: SET spcs { $1 ^ $2 };
102 notation: NOT spcs { $1 ^ $2 };
103 cn: CN spcs { $1 ^ $2 };
104 str: STR spcs { $1 ^ $2 };
105 id: ID spcs { $1 ^ $2 };
106 coerc: COERC spcs { $1 ^ $2 };
107 cm: CM spcs { $1 ^ $2 } | { "" }
108 wh: WITH spcs { $1 ^ $2 };
110 /* SPACES *******************************************************************/
113 | OQ verbatims CQ { $1 ^ $2 ^ $3 }
121 | spc spcs { $1 ^ $2 }
125 | SPC rspcs { $1 ^ $2 }
128 /* IDENTIFIERS **************************************************************/
153 | outer_keyword { $1 }
158 | qid AC { strip1 $2 :: $1 }
162 | ident cm idents { $1 :: $3 }
165 /* PLAIN SKIP ***************************************************************/
169 | inner_keyword { $1 }
182 | outer_keyword { $1 }
191 /* LEFT SKIP ****************************************************************/
194 | plain_skip { $1, [] }
195 | abbr li_skips IN { $1 ^ fst $2 ^ $3, snd $2 }
196 | op ocp_skips CP { $1 ^ fst $2 ^ $3, snd $2 }
201 | outer_keyword { $1, [] }
207 | xlskip spcs { fst $1 ^ $2, snd $1 }
208 | xlskip spcs xlskips { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
211 | rlskip spcs { fst $1 ^ $2, snd $1 }
212 | rlskip spcs xlskips { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
219 /* GENERAL SKIP *************************************************************/
226 | outer_keyword { $1, [] }
232 | xskip spcs { fst $1 ^ $2, snd $1 }
233 | xskip spcs xskips { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
236 | rskip spcs { fst $1 ^ $2, snd $1 }
237 | rskip spcs xskips { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
240 /* ABBREVIATION SKIP ********************************************************/
243 | inner_skip { $1, [] }
244 | abbr li_skips IN { $1 ^ fst $2 ^ $3, snd $2 }
245 | op ocp_skips CP { $1 ^ fst $2 ^ $3, snd $2 }
248 | li_skip spcs { fst $1 ^ $2, snd $1 }
249 | li_skip spcs li_skips { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
252 /* PARENTETIC SKIP **********************************************************/
255 | inner_skip { $1, [] }
256 | abbr li_skips IN { $1 ^ fst $2 ^ $3, snd $2 }
257 | op ocp_skips CP { $1 ^ fst $2 ^ $3, snd $2 }
261 | ocp_skip spcs { fst $1 ^ $2, snd $1 }
262 | ocp_skip spcs ocp_skips { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
265 /* VERBATIM SKIP ************************************************************/
278 | verbatim verbatims { $1 ^ $2 }
281 /* PROOF STEPS **************************************************************/
285 | skips FS { snd $1 }
289 | step spcs steps { $1 @ $3 }
293 | proof fs steps qed { $3 }
294 | proof skips { snd $2 }
295 | proof wh skips fs steps qed { snd $3 @ $5 }
298 | th ident opt_lskips def xskips FS
300 [T.Inline (false, T.Con, trim $2, "", mk_flavour $1, [])]
302 | th ident lskips fs just FS
304 $5 @ [T.Inline (false, T.Con, trim $2, "", mk_flavour $1, [])]
306 | gen ident def xskips FS
308 [T.Inline (false, T.Con, trim $2, "", mk_flavour $1, [])]
310 | mor ident cn ident fs just FS
311 { out "MOR" $4; $6 @ mk_morphism (trim $4) }
312 | xlet ident opt_lskips def xskips FS
314 [T.Inline (true, T.Con, trim $2, "", mk_flavour $1, [])]
316 | xlet ident lskips fs just FS
318 $5 @ [T.Inline (true, T.Con, trim $2, "", mk_flavour $1, [])]
320 | var idents cn xskips FS
321 { out "VAR" (cat $2); mk_vars true $2 }
322 | ax idents cn xskips FS
323 { out "AX" (cat $2); mk_vars false $2 }
326 T.Inline (false, T.Ind, trim $2, "", None, []) :: snd $3
329 { out "SEC" $2; [T.Section (true, trim $2, $1 ^ $2)] }
331 { out "END" $2; [T.Section (false, trim $2, $1 ^ $2)] }
333 { out "UNX" (fst $2); [T.Unexport ($1 ^ fst $2 ^ trim $3)] }
335 { out "REQ" $3; [T.Include (true, trim $3)] }
337 { out "REQ" $2; [T.Include (true, trim $2)] }
339 { out "REQ" $2; [T.Include (true, strip2 (trim $2))] }
340 | coerc qid spcs skips FS
341 { out "COERCE" (hd $2); coercion (hd $2) }
342 | id coerc qid spcs skips FS
343 { out "COERCE" (hd $3); coercion (hd $3) }
345 { out "ERROR" $2; failwith ("macro_step " ^ $2) }
347 { out "ERROR" $2; failwith ("macro_step " ^ $2) }
350 out "ERROR" vs; failwith ("macro_step " ^ vs) }
353 out "ERROR" vs; failwith ("macro_step " ^ vs) }
356 /* VERNACULAR ITEMS *********************************************************/
359 | OQ verbatims CQ { [T.Comment $2] }
361 | set xskips FS { [T.Unexport ($1 ^ fst $2 ^ trim $3)] }
362 | notation xskips FS { notation ($1 ^ fst $2 ^ trim $3) }
363 | error { out "ERROR" "item"; failwith "item" }
367 | rspcs item items { $2 @ $3 }
372 oc: OC spcs { $1 ^ $2 };
373 coe: COE spcs { $1 ^ $2 };
374 sc: SC spcs { $1 ^ $2 };
381 | cnot spcs { $1 ^ $2 }
382 | cnot spcs cnots { $1 ^ $2 ^ $3 }
395 | abbr extends0 IN { $1 ^ $2 ^ $3 }
396 | op extends1 CP { $1 ^ $2 ^ $3 }
415 | restrict spcs { $1 ^ $2 }
416 | restrict spcs restricts { $1 ^ $2 ^ $3 }
419 | xre spcs { $1 ^ $2 }
420 | xre spcs restricts { $1 ^ $2 ^ $3 }
427 | extend0 spcs { $1 ^ $2 }
428 | extend0 spcs extends0 { $1 ^ $2 ^ $3 }
436 | extend1 spcs { $1 ^ $2 }
437 | extend1 spcs extends1 { $1 ^ $2 ^ $3 }
458 | unexport_rr { $1, [] }
459 | oc fields CC { $1 ^ fst $2 ^ $3, snd $2 }
460 | oc unexport_ff CC { $1, [] }
463 | unexport_r spcs { fst $1 ^ $2, snd $1 }
464 | unexport_r spcs unexports_r { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
467 | ident cn unexports_r
468 { $1 ^ $2 ^ fst $3, snd $3 }
469 | ident def unexports_r
470 { $1 ^ $2 ^ fst $3, snd $3 }
471 | ident coe unexports_r
472 { $1 ^ $2 ^ fst $3, snd $3 @ coercion (trim $1) }
476 | field sc fields { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
478 | error { out "ERROR" "fields"; failwith "fields" }
490 | unexport spcs { fst $1 ^ $2, snd $1 }
491 | unexport spcs unexports { fst $1 ^ $2 ^ fst $3, snd $1 @ snd $3 }
495 | reserved_ident { $1 }
507 | verbatim verbatims { $1 ^ $2 }
511 | restricts FS { [] }
515 | step spcs steps { $1 @ $3 }
519 | th ident restricts fs proof FS steps qed FS
521 $7 @ [T.Inline (false, T.Con, trim $2, "", mk_flavour $1)]
523 | th ident restricts fs proof restricts FS
525 [T.Inline (false, T.Con, trim $2, "", mk_flavour $1)]
527 | th ident restricts fs steps qed FS
529 $5 @ [T.Inline (false, T.Con, trim $2, "", mk_flavour $1)]
531 | th ident restricts def restricts FS
533 [T.Inline (false, T.Con, trim $2, "", mk_flavour $1)]
535 | th ident def restricts FS
537 [T.Inline (false, T.Con, trim $2, "", mk_flavour $1)]
539 | gen ident def restricts FS
541 [T.Inline (false, T.Con, trim $2, "", mk_flavour $1)]
543 | xlet ident restricts fs proof FS steps qed FS
545 $7 @ [T.Inline (true, T.Con, trim $2, "", mk_flavour $1)]
547 | xlet ident restricts fs proof restricts FS
549 [T.Inline (true, T.Con, trim $2, "", mk_flavour $1)]
551 | xlet ident restricts fs steps qed FS
553 $5 @ [T.Inline (true, T.Con, trim $2, "", mk_flavour $1)]
555 | xlet ident restricts def restricts FS
557 [T.Inline (true, T.Con, trim $2, "", mk_flavour $1)]
559 | xlet ident def restricts FS
561 [T.Inline (true, T.Con, trim $2, "", mk_flavour $1)]
564 { out "VAR" (cat $2); mk_vars true $2 }
566 { out "AX" (cat $2); mk_vars false $2 }
567 | ind ident unexports FS
569 T.Inline (false, T.Ind, trim $2, "", None) :: snd $3
572 { out "SEC" $2; [T.Section (true, trim $2, $1 ^ $2)] }
574 { out "END" $2; [T.Section (false, trim $2, $1 ^ $2)] }
576 { out "UNX" (fst $2); [T.Unexport ($1 ^ fst $2 ^ trim $3)] }
578 { out "REQ" $3; [T.Include (trim $3)] }
580 { out "REQ" $3; [T.Include (trim $3)] }
582 { out "REQ" $2; [T.Include (trim $2)] }
584 { out "REQ" $2; [T.Include (strip2 (trim $2))] }
585 | coerc qid spcs cn unexports FS
586 { out "COERCE" (hd $2); coercion (hd $2) }
587 | id coerc qid spcs cn unexports FS
588 { out "COERCE" (hd $3); coercion (hd $3) }
590 { out "ERROR" $2; failwith ("macro_step " ^ $2) }
592 { out "ERROR" $2; failwith ("macro_step " ^ $2) }
595 out "ERROR" vs; failwith ("macro_step " ^ vs) }
598 out "ERROR" vs; failwith ("macro_step " ^ vs) }
601 | OQ verbatims CQ { [T.Comment $2] }
603 | set unexports FS { [T.Unexport ($1 ^ fst $2 ^ trim $3)] }
604 | notation unexports FS { notation ($1 ^ fst $2 ^ trim $3) }
605 | error { out "ERROR" "item"; failwith "item" }
609 | rspcs item items { $2 @ $3 }