Debian Codegraph @

parsing/notgram_ops.ml

raw symbols SCIP
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 open Pp
12 open CErrors
13 open Notation
14
15 (* Register the grammar of notation for parsing and printing
16 (also register the parsing rule if not onlyprinting) *)
17
18 let notation_grammar_map = Summary.ref ~stage:Summary.Stage.Synterp ~name:"notation_grammar_map" NotationMap.empty
19
20 let declare_notation_grammar ntn rule =
21 try
22 let _ = NotationMap.find ntn !notation_grammar_map in
23 anomaly (str "Notation " ++ pr_notation ntn ++ str " is already assigned a grammar.")
24 with Not_found ->
25 notation_grammar_map := NotationMap.add ntn rule !notation_grammar_map
26
27 let grammar_of_notation ntn =
28 NotationMap.find ntn !notation_grammar_map
29
30 let list_prefixes ntn =
31 let rec aux inrec = function
32 | [] -> []
33 | s :: symbols ->
34 let nt, inrec = match s with
35 | NonTerminal _ -> (if inrec then 0 else 1), false
36 | Terminal s when String.equal s (Names.Id.to_string Notation_ops.ldots_var) -> 0, true
37 (* the above is horribly hackish but we plan to rewrite recursive notations soon anyway *)
38 | Terminal _ | SProdList _ | Break _ -> 0, inrec in
39 let q = List.map (fun (l, k) -> s :: l, k + nt) (aux inrec symbols) in
40 match s, symbols with
41 (* successive Terminal are considered at once by the parser,
42 don't list "'T1'" as prefix of "'T1' 'T2'" *)
43 | Terminal _, Terminal _ :: _ -> q
44 | _ -> ([s], nt) :: q in
45 let entry, symbols = decompose_notation_key ntn in
46 let symbols = match symbols with
47 (* don't consider notations "{ _ ..." that have a special treatment *)
48 | Terminal "{" :: NonTerminal _ :: _ -> [] | _ -> symbols in
49 aux false symbols
50 (* considering "_" as a prefix would make all infix notations incompatible *)
51 |> List.filter (function [NonTerminal _], _ -> false | _ -> true)
52 |> List.map (fun (l, k) -> make_notation_key entry l, k)
53
54 let prefixes_map = Summary.ref ~stage:Summary.Stage.Synterp ~name:"notation_prefixes_map" NotationMap.empty
55
56 let declare_prefixes ntn =
57 let register_prefix (pref, _) =
58 if not (NotationMap.mem pref !prefixes_map) then
59 prefixes_map := NotationMap.add pref ntn !prefixes_map in
60 List.iter register_prefix (list_prefixes ntn)
61
62 let notation_non_terminals_map = Summary.ref ~stage:Summary.Stage.Synterp ~name:"notation_non_terminals_map" NotationMap.empty
63
64 let declare_notation_non_terminals ntn entries =
65 try
66 let _ = NotationMap.find ntn !notation_grammar_map in
67 anomaly (str "Notation " ++ pr_notation ntn ++ str " is already assigned a grammar.")
68 with Not_found ->
69 notation_non_terminals_map := NotationMap.add ntn entries !notation_non_terminals_map;
70 declare_prefixes ntn
71
72 let non_terminals_of_notation ntn =
73 NotationMap.find ntn !notation_non_terminals_map
74
75 let longest_common_prefix ntn =
76 CList.find_map
77 (fun (pref, k) ->
78 NotationMap.find_opt pref !prefixes_map
79 |> Option.map (fun ntn -> ntn, k))
80 (List.rev (list_prefixes ntn))
81
82 let get_defined_notations () =
83 NotationSet.elements @@ NotationMap.domain !notation_grammar_map