| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open CErrors |
| 13 |
open Notation |
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
|
| 42 |
|
| 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 |
|
| 48 |
| Terminal "{" :: NonTerminal _ :: _ -> [] | _ -> symbols in |
| 49 |
aux false symbols |
| 50 |
|
| 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 |