Debian Codegraph @

coqpp/coqpp_lex.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 Lexing
14 open Coqpp_parse
15
16 type mode =
17 | OCaml
18 | Extend
19
20 exception Lex_error of Coqpp_ast.loc * string
21
22 let loc lexbuf = {
23 Coqpp_ast.loc_start = lexeme_start_p lexbuf;
24 Coqpp_ast.loc_end = lexeme_end_p lexbuf;
25 }
26
27 let newline lexbuf =
28 let pos = lexbuf.lex_curr_p in
29 let pos = { pos with pos_lnum = pos.pos_lnum + 1; pos_bol = pos.pos_cnum } in
30 lexbuf.lex_curr_p <- pos
31
32 let num_comments = ref 0
33 let num_braces = ref 0
34 let ocaml_start_pos = ref Lexing.dummy_pos
35
36 let mode () = if !num_braces = 0 then Extend else OCaml
37
38 let comment_buf = Buffer.create 512
39 let ocaml_buf = Buffer.create 512
40 let string_buf = Buffer.create 512
41
42 let lex_error lexbuf msg =
43 raise (Lex_error (loc lexbuf, msg))
44
45 let lex_unexpected_eof lexbuf where =
46 lex_error lexbuf (Printf.sprintf "Unexpected end of file in %s" where)
47
48 let start_comment _ =
49 let () = incr num_comments in
50 Buffer.add_string comment_buf "(*"
51
52 let end_comment lexbuf =
53 let () = decr num_comments in
54 let () = Buffer.add_string comment_buf "*)" in
55 if !num_comments < 0 then lex_error lexbuf "Unexpected end of comment"
56 else if !num_comments = 0 then
57 let s = Buffer.contents comment_buf in
58 let () = Buffer.reset comment_buf in
59 Some (COMMENT s)
60 else
61 None
62
63 let start_ocaml lexbuf =
64 let () = match mode () with
65 | OCaml -> Buffer.add_string ocaml_buf "{"
66 | Extend -> ocaml_start_pos := lexeme_start_p lexbuf
67 in
68 incr num_braces
69
70 let end_ocaml lexbuf =
71 let () = decr num_braces in
72 if !num_braces < 0 then lex_error lexbuf "Unexpected end of OCaml code"
73 else if !num_braces = 0 then
74 let s = Buffer.contents ocaml_buf in
75 let () = Buffer.reset ocaml_buf in
76 let loc = Some {
77 Coqpp_ast.loc_start = !ocaml_start_pos;
78 Coqpp_ast.loc_end = lexeme_end_p lexbuf
79 } in
80 Some (CODE { Coqpp_ast.code = s; loc })
81 else
82 let () = Buffer.add_string ocaml_buf "}" in
83 None
84
85 }
86
87 let letter = ['a'-'z' 'A'-'Z']
88 let letterlike = ['_' 'a'-'z' 'A'-'Z']
89 let alphanum = ['_' 'a'-'z' 'A'-'Z' '0'-'9' '\'']
90 let ident = letterlike alphanum*
91 let qualid = ident ('.' ident)*
92 let space = [' ' '\t' '\r']
93 let number = [ '0'-'9' ]
94
95 rule extend = parse
96 | "(*" { start_comment (); comment lexbuf }
97 | "{" { start_ocaml lexbuf; ocaml lexbuf }
98 | "GRAMMAR" { GRAMMAR }
99 | "VERNAC" { VERNAC }
100 | "COMMAND" { COMMAND }
101 | "TACTIC" { TACTIC }
102 | "EXTEND" { EXTEND }
103 | "DOC_GRAMMAR" { DOC_GRAMMAR }
104 | "END" { END }
105 | "DECLARE" { DECLARE }
106 | "PLUGIN" { PLUGIN }
107 | "DEPRECATED" { DEPRECATED }
108 | "CLASSIFIED" { CLASSIFIED }
109 | "STATE" { STATE }
110 | "PRINTED" { PRINTED }
111 | "TYPED" { TYPED }
112 | "INTERPRETED" { INTERPRETED }
113 | "GLOBALIZED" { GLOBALIZED }
114 | "SUBSTITUTED" { SUBSTITUTED }
115 | "ARGUMENT" { ARGUMENT }
116 | "RAW_PRINTED" { RAW_PRINTED }
117 | "GLOB_PRINTED" { GLOB_PRINTED }
118 | "SYNTERP" { SYNTERP }
119 | "BY" { BY }
120 | "AS" { AS }
121 (** Camlp5 specific keywords *)
122 | "GLOBAL" { GLOBAL }
123 | "TOP" { TOP }
124 | "FIRST" { FIRST }
125 | "LAST" { LAST }
126 | "BEFORE" { BEFORE }
127 | "AFTER" { AFTER }
128 | "LEVEL" { LEVEL }
129 | "LEFTA" { LEFTA }
130 | "RIGHTA" { RIGHTA }
131 | "NONA" { NONA }
132 | "BOTHA" { BOTHA }
133 | "IGNORE" { IGNORE }
134 | "KEYWORDS" { KEYWORDS }
135 (** Standard *)
136 | ident { IDENT (Lexing.lexeme lexbuf) }
137 | qualid { QUALID (Lexing.lexeme lexbuf) }
138 | number { INT (int_of_string (Lexing.lexeme lexbuf)) }
139 | space { extend lexbuf }
140 | '\"' { string lexbuf }
141 | '\n' { newline lexbuf; extend lexbuf }
142 | "![" { BANGBRACKET }
143 | "#[" { HASHBRACKET }
144 | '[' { LBRACKET }
145 | ']' { RBRACKET }
146 | '|' { PIPE }
147 | "->" { ARROW }
148 | "=>" { FUN }
149 | ',' { COMMA }
150 | ':' { COLON }
151 | ';' { SEMICOLON }
152 | '(' { LPAREN }
153 | ')' { RPAREN }
154 | '=' { EQUAL }
155 | '*' { STAR }
156 | _ { lex_error lexbuf "syntax error" }
157 | eof { EOF }
158
159 and ocaml = parse
160 | "{" { start_ocaml lexbuf; ocaml lexbuf }
161 | "}" { match end_ocaml lexbuf with Some tk -> tk | None -> ocaml lexbuf }
162 | '\n' { newline lexbuf; Buffer.add_char ocaml_buf '\n'; ocaml lexbuf }
163 | '\"' { Buffer.add_char ocaml_buf '\"'; ocaml_string lexbuf }
164 | (_ as c) { Buffer.add_char ocaml_buf c; ocaml lexbuf }
165 | eof { lex_unexpected_eof lexbuf "OCaml code" }
166
167 and comment = parse
168 | "*)" { match end_comment lexbuf with Some _ -> extend lexbuf | None -> comment lexbuf }
169 | "(*" { start_comment lexbuf; comment lexbuf }
170 | '\n' { newline lexbuf; Buffer.add_char comment_buf '\n'; comment lexbuf }
171 | (_ as c) { Buffer.add_char comment_buf c; comment lexbuf }
172 | eof { lex_unexpected_eof lexbuf "comment" }
173
174 and string = parse
175 | '\"'
176 {
177 let s = Buffer.contents string_buf in
178 let () = Buffer.reset string_buf in
179 STRING s
180 }
181 | "\\\"" { Buffer.add_char string_buf '\"'; string lexbuf }
182 | '\n' { newline lexbuf; Buffer.add_char string_buf '\n'; string lexbuf }
183 | (_ as c) { Buffer.add_char string_buf c; string lexbuf }
184 | eof { lex_unexpected_eof lexbuf "string" }
185
186 and ocaml_string = parse
187 | "\\\"" { Buffer.add_string ocaml_buf "\\\""; ocaml_string lexbuf }
188 | '\"' { Buffer.add_char ocaml_buf '\"'; ocaml lexbuf }
189 | (_ as c) { Buffer.add_char ocaml_buf c; ocaml_string lexbuf }
190 | eof { lex_unexpected_eof lexbuf "OCaml string" }
191
192 {
193
194 let token lexbuf = match mode () with
195 | OCaml -> ocaml lexbuf
196 | Extend -> extend lexbuf
197
198 }