Debian Codegraph @

tools/coqdoc/tokens.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 (* Application of printing rules based on a dictionary specific to the
12 target language *)
13
14 (*s Dictionaries: trees annotated with string options, each node being a map
15 from chars to dictionaries (the subtrees). A trie, in other words.
16
17 (code copied from parsing/lexer.ml4 for the use of coqdoc, Apr 2010)
18 *)
19
20 module CharMap = Map.Make (struct
21 type t = char
22 let compare (x : t) (y : t) = compare x y
23 end)
24
25 type ttree = {
26 node : string option;
27 branch : ttree CharMap.t }
28
29 let empty_ttree = { node = None; branch = CharMap.empty }
30
31 let ttree_add ttree str translated =
32 let rec insert tt i =
33 if i == String.length str then
34 {node = Some translated; branch = tt.branch}
35 else
36 let c = str.[i] in
37 let br =
38 match try Some (CharMap.find c tt.branch) with Not_found -> None with
39 | Some tt' ->
40 CharMap.add c (insert tt' (i + 1)) (CharMap.remove c tt.branch)
41 | None ->
42 let tt' = {node = None; branch = CharMap.empty} in
43 CharMap.add c (insert tt' (i + 1)) tt.branch
44 in
45 { node = tt.node; branch = br }
46 in
47 insert ttree 0
48
49 (* Removes a string from a dictionary: returns an equal dictionary
50 if the word not present. *)
51 let ttree_remove ttree str =
52 let rec remove tt i =
53 if i == String.length str then
54 {node = None; branch = tt.branch}
55 else
56 let c = str.[i] in
57 let br =
58 match try Some (CharMap.find c tt.branch) with Not_found -> None with
59 | Some tt' ->
60 CharMap.add c (remove tt' (i + 1)) (CharMap.remove c tt.branch)
61 | None -> tt.branch
62 in
63 { node = tt.node; branch = br }
64 in
65 remove ttree 0
66
67 let ttree_descend ttree c = CharMap.find c ttree.branch
68
69 let ttree_find ttree str =
70 let rec proc_rec tt i =
71 if i == String.length str then tt
72 else proc_rec (CharMap.find str.[i] tt.branch) (i+1)
73 in
74 proc_rec ttree 0
75
76 (*s Parameters of the translation automaton *)
77
78 type out_function = bool -> bool -> Index.index_entry option -> string -> unit
79
80 let token_tree = ref (ref empty_ttree)
81 let outfun = ref (fun _ _ _ _ -> failwith "outfun not initialized")
82
83 (*s Translation automaton *)
84
85 let buff = Buffer.create 4
86
87 let flush_buffer was_symbolchar tag tok =
88 let hastr = String.length tok <> 0 in
89 if hastr then !outfun false was_symbolchar tag tok;
90 if Buffer.length buff <> 0 then
91 !outfun true (if hastr then not was_symbolchar else was_symbolchar)
92 tag (Buffer.contents buff);
93 Buffer.clear buff
94
95 type sublexer_state =
96 | Neutral
97 | Buffering of bool * Index.index_entry option * string * ttree
98
99 let translation_state = ref Neutral
100
101 let buffer_char is_symbolchar ctag c =
102 let rec aux = function
103 | Neutral ->
104 restart_buffering ()
105 | Buffering (was_symbolchar,tag,translated,tt) ->
106 if tag <> ctag then
107 (* A strong tag comes from Rocq; if different Rocq tags *)
108 (* hence, we don't try to see the chars as part of a single token *)
109 let translated =
110 match tt.node with
111 | Some tok -> Buffer.clear buff; tok
112 | None -> translated in
113 flush_buffer was_symbolchar tag translated;
114 restart_buffering ()
115 else
116 begin
117 (* If we change the category of characters (symbol vs ident) *)
118 (* we accept this as a possible token cut point and remember the *)
119 (* translated token up to that point *)
120 let translated =
121 if is_symbolchar <> was_symbolchar then
122 match tt.node with
123 | Some tok -> Buffer.clear buff; tok
124 | None -> translated
125 else translated in
126 (* We try to make a significant token from the current *)
127 (* buffer and the new character *)
128 try
129 let tt = ttree_descend tt c in
130 Buffer.add_char buff c;
131 Buffering (is_symbolchar,ctag,translated,tt)
132 with Not_found ->
133 (* No existing translation for the given set of chars *)
134 if is_symbolchar <> was_symbolchar then
135 (* If we changed the category of character read, we accept it *)
136 (* as a possible cut point and restart looking for a translation *)
137 (flush_buffer was_symbolchar tag translated;
138 restart_buffering ())
139 else
140 (* If we did not change the category of character read, we do *)
141 (* not want to cut arbitrarily in the middle of the sequence of *)
142 (* symbol characters or identifier characters *)
143 (Buffer.add_char buff c;
144 Buffering (is_symbolchar,tag,translated,empty_ttree))
145 end
146
147 and restart_buffering () =
148 let tt = try ttree_descend !(!token_tree) c with Not_found -> empty_ttree in
149 Buffer.add_char buff c;
150 Buffering (is_symbolchar,ctag,"",tt)
151
152 in
153 translation_state := aux !translation_state
154
155 let output_tagged_ident_string s =
156 for i = 0 to String.length s - 1 do buffer_char false None s.[i] done
157
158 let output_tagged_symbol_char tag c =
159 buffer_char true tag c
160
161 let flush_sublexer () =
162 match !translation_state with
163 | Neutral -> ()
164 | Buffering (was_symbolchar,tag,translated,tt) ->
165 let translated =
166 match tt.node with
167 | Some tok -> Buffer.clear buff; tok
168 | None -> translated in
169 flush_buffer was_symbolchar tag translated;
170 translation_state := Neutral
171
172 (* Translation not using the automaton *)
173 let translate s =
174 try (ttree_find !(!token_tree) s).node with Not_found -> None