Debian Codegraph @

tools/coqdep/lib/lexer.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 {
12
13 open Filename
14 open Lexing
15
16 type qualid = string list
17
18 type load = Logical of string | Physical of string
19
20 type coq_token =
21 | Require of qualid option * qualid list
22 | Declare of string list
23 | Load of load
24 | External of qualid * string
25
26 exception Fin_fichier
27 exception Syntax_error of int*int
28
29 let unquote_string s =
30 String.sub s 1 (String.length s - 2)
31
32 let unquote_vfile_string s =
33 let f = unquote_string s in
34 if check_suffix f ".v" then chop_suffix f ".v" else f
35
36 let backtrack lexbuf = lexbuf.lex_curr_pos <- lexbuf.lex_start_pos;
37 lexbuf.lex_curr_p <- lexbuf.lex_start_p
38
39 let syntax_error lexbuf =
40 raise (Syntax_error (Lexing.lexeme_start lexbuf, Lexing.lexeme_end lexbuf))
41
42 let check_valid lexbuf s =
43 match Unicode.ident_refutation s with
44 | None -> s
45 | Some _ -> syntax_error lexbuf
46
47 let get_ident lexbuf =
48 let s = Lexing.lexeme lexbuf in check_valid lexbuf s
49
50 let get_field_name lexbuf =
51 let s = Lexing.lexeme lexbuf in
52 check_valid lexbuf (String.sub s 1 (String.length s - 1))
53
54 }
55
56 let space = [' ' '\t' '\n' '\r']
57 let lowercase = ['a'-'z']
58 let uppercase = ['A'-'Z']
59 let identchar = ['A'-'Z' 'a'-'z' '_' '\'' '0'-'9']
60 let caml_up_ident = uppercase identchar*
61 let caml_low_ident = lowercase identchar*
62
63 (* This is an overapproximation, we check correctness afterwards *)
64 let coq_ident = ['A'-'Z' 'a'-'z' '_' '\128'-'\255'] ['A'-'Z' 'a'-'z' '_' '\'' '0'-'9' '\128'-'\255']*
65 let coq_field = '.' coq_ident
66 let coq_qual_id_rex = coq_ident coq_field+
67
68 let dot = '.' ( space+ | eof)
69
70 rule coq_action = parse
71 | "Require" space+
72 { require_modifiers None lexbuf }
73 | "Local" space+ "Declare" space+ "ML" space+ "Module" space+
74 { modules [] lexbuf }
75 | "Declare" space+ "ML" space+ "Module" space+
76 { modules [] lexbuf }
77 | "Load" space+
78 { load_file lexbuf }
79 | "Time" space+
80 { coq_action lexbuf }
81 | "Timeout" space+ ['0'-'9']+ space+
82 { coq_action lexbuf }
83 | "From" space+
84 { from_rule false lexbuf }
85 | "Comments" space+ "From" space+
86 { from_rule true lexbuf }
87 | "#["
88 { skip_attribute lexbuf; coq_action lexbuf }
89 | space+
90 { coq_action lexbuf }
91 | "(*"
92 { comment lexbuf; coq_action lexbuf }
93 | eof
94 { raise Fin_fichier}
95 | _
96 { skip_to_dot lexbuf; coq_action lexbuf }
97
98 and skip_attribute = parse
99 | "(*"
100 { comment lexbuf; skip_attribute lexbuf }
101 | "]"
102 { () }
103 | '"' [^ '"']* '"'
104 { skip_attribute lexbuf }
105 | _
106 { skip_attribute lexbuf }
107
108 and from_rule only_extra_dep = parse
109 | "(*"
110 { comment lexbuf; from_rule only_extra_dep lexbuf }
111 | space+
112 { from_rule only_extra_dep lexbuf }
113 | coq_ident
114 { let from = coq_qual_id_tail [get_ident lexbuf] lexbuf in
115 consume_require_or_extradeps only_extra_dep (Some from) lexbuf }
116 | eof
117 { syntax_error lexbuf }
118 | _
119 { syntax_error lexbuf }
120
121 and extra_dep_rule from = parse
122 | "(*"
123 { comment lexbuf; extra_dep_rule from lexbuf }
124 | space+
125 { extra_dep_rule from lexbuf }
126 | eof
127 { syntax_error lexbuf }
128 | '"' ([^ '"']+ as f) '"' (*'"'*)
129 { skip_to_dot lexbuf;
130 External (from,f) }
131
132 and require_modifiers from = parse
133 | "(*"
134 { comment lexbuf; require_modifiers from lexbuf }
135 | ("Import" | "Export") space* ("-" space*)? ((space | "(") as p)
136 { if p = '(' then skip_parenthesized lexbuf;
137 require_file from lexbuf }
138 | space+
139 { require_modifiers from lexbuf }
140 | eof
141 { syntax_error lexbuf }
142 | _
143 { backtrack lexbuf ; require_file from lexbuf }
144
145 and consume_require_or_extradeps only_extra_dep from = parse
146 | "(*"
147 { comment lexbuf; consume_require_or_extradeps only_extra_dep from lexbuf }
148 | space+
149 { consume_require_or_extradeps only_extra_dep from lexbuf }
150 | "Require" space+
151 { if only_extra_dep then syntax_error lexbuf; require_modifiers from lexbuf }
152 | "Extra" space+ "Dependency" space+
153 { match from with
154 | None -> syntax_error lexbuf (* Extra Dependency requires From *)
155 | Some from -> extra_dep_rule from lexbuf }
156 | _
157 { syntax_error lexbuf }
158
159 and comment = parse
160 | "(*"
161 { comment lexbuf; comment lexbuf }
162 | "*)"
163 { () }
164 | "'" [^ '\\' '\''] "'"
165 { comment lexbuf }
166 | "'" '\\' ['\\' '\'' 'n' 't' 'b' 'r'] "'"
167 { comment lexbuf }
168 | "'" '\\' ['0'-'9'] ['0'-'9'] ['0'-'9'] "'"
169 { comment lexbuf }
170 | eof
171 { raise Fin_fichier }
172 | _
173 { comment lexbuf }
174
175 and skip_parenthesized = parse
176 | "(*"
177 { comment lexbuf; skip_parenthesized lexbuf }
178 | "("
179 { skip_parenthesized lexbuf; skip_parenthesized lexbuf }
180 | ")"
181 { () }
182 | eof
183 { raise Fin_fichier }
184 | _
185 { skip_parenthesized lexbuf }
186
187 and load_file = parse
188 | '"' [^ '"']* '"' (*'"'*)
189 { let s = lexeme lexbuf in
190 parse_dot lexbuf;
191 Load (Physical (unquote_vfile_string s)) }
192 | coq_ident
193 { let s = get_ident lexbuf in skip_to_dot lexbuf; Load (Logical s) }
194 | eof
195 { syntax_error lexbuf }
196 | _
197 { syntax_error lexbuf }
198
199 and require_file from = parse
200 | "(*"
201 { comment lexbuf; require_file from lexbuf }
202 | space+
203 { require_file from lexbuf }
204 | coq_ident
205 { let name = coq_qual_id_tail [get_ident lexbuf] lexbuf in
206 let qid = coq_qual_id_list [name] lexbuf in
207 parse_dot lexbuf;
208 Require (from, qid) }
209 | eof
210 { syntax_error lexbuf }
211 | _
212 { syntax_error lexbuf }
213
214 and skip_to_dot = parse
215 | "(*"
216 { comment lexbuf; skip_to_dot lexbuf }
217 | dot { () }
218 | eof { syntax_error lexbuf }
219 | _ { skip_to_dot lexbuf }
220
221 and parse_dot = parse
222 | dot { () }
223 | eof { syntax_error lexbuf }
224 | _ { syntax_error lexbuf }
225
226 and coq_qual_id_tail module_name = parse
227 | "(*"
228 { comment lexbuf; coq_qual_id_tail module_name lexbuf }
229 | space+
230 { coq_qual_id_tail module_name lexbuf }
231 | coq_field
232 { coq_qual_id_tail (get_field_name lexbuf :: module_name) lexbuf }
233 | eof
234 { syntax_error lexbuf }
235 | _
236 { backtrack lexbuf;
237 List.rev module_name }
238
239 and coq_qual_id_list module_names = parse
240 | "(*"
241 { comment lexbuf; coq_qual_id_list module_names lexbuf }
242 | "("
243 { skip_parenthesized lexbuf; coq_qual_id_list module_names lexbuf }
244 | space+
245 { coq_qual_id_list module_names lexbuf }
246 | coq_ident
247 { let name = coq_qual_id_tail [get_ident lexbuf] lexbuf in
248 coq_qual_id_list (name :: module_names) lexbuf
249 }
250 | eof
251 { syntax_error lexbuf }
252 | _
253 { backtrack lexbuf;
254 List.rev module_names }
255
256 and modules mllist = parse
257 | space+
258 { modules mllist lexbuf }
259 | "(*"
260 { comment lexbuf; modules mllist lexbuf }
261 | '"' [^'"']* '"'
262 { let lex = (Lexing.lexeme lexbuf) in
263 let str = String.sub lex 1 (String.length lex - 2) in
264 modules (str :: mllist) lexbuf}
265 | eof
266 { syntax_error lexbuf }
267 | _
268 { Declare (List.rev mllist) }