Debian Codegraph @

ide/rocqide/rocq_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 exception Unterminated
13
14 let utf8_adjust = ref 0
15
16 let utf8_lexeme_start lexbuf =
17 Lexing.lexeme_start lexbuf - !utf8_adjust
18 }
19
20 let space = [' ' '\n' '\r' '\t' '\012'] (* '\012' is form-feed *)
21
22 let number = [ '0'-'9' ]+
23
24 let string = "\"" _+ "\""
25
26 let alpha = ['a'-'z' 'A'-'Z']
27
28 let ident = alpha (alpha | number | '_' | "'")*
29
30 let undotted_sep = ((number | '[' ident ']') space* ':' space*)? '{' | '}' | '-'+ | '+'+ | '*'+
31
32 let vernac_control = "Fail" | "Time" | "Redirect" space+ string | "Timeout" space+ number
33
34 let dot_sep = '.' (space | eof)
35
36 let utf8_extra_byte = [ '\x80' - '\xBF' ]
37
38 rule rocq_string = parse
39 | "\"\"" { rocq_string lexbuf }
40 | "\"" { () }
41 | eof { () }
42 | utf8_extra_byte { incr utf8_adjust; rocq_string lexbuf }
43 | _ { rocq_string lexbuf }
44
45 and comment = parse
46 | "(*" { let _ = comment lexbuf in comment lexbuf }
47 | "\"" { let () = rocq_string lexbuf in comment lexbuf }
48 | "*)" { Some (utf8_lexeme_start lexbuf) }
49 | eof { None }
50 | utf8_extra_byte { incr utf8_adjust; comment lexbuf }
51 | _ { comment lexbuf }
52
53 and quotation n l = parse
54 | eof { raise Unterminated }
55 | utf8_extra_byte { incr utf8_adjust; quotation n l lexbuf }
56 | "{" { quotation_nesting n l 1 lexbuf }
57 | "}" { quotation_closing n l 1 lexbuf }
58 | _ { quotation n l lexbuf }
59
60 and quotation_nesting n l v = parse
61 | eof { raise Unterminated }
62 | utf8_extra_byte { incr utf8_adjust; quotation n l lexbuf }
63 | "{" {
64 if n = v+1 then quotation n (l+1) lexbuf
65 else quotation_nesting n l (v+1) lexbuf
66 }
67 | "}" { quotation_closing n l 1 lexbuf }
68 | _ { quotation n l lexbuf }
69
70 and quotation_closing n l v = parse
71 | eof { raise Unterminated }
72 | utf8_extra_byte { incr utf8_adjust; quotation n l lexbuf }
73 | "}" {
74 if n = v+1 then
75 if l = 1 then ()
76 else quotation n (l-1) lexbuf
77 else quotation_closing n l (v+1) lexbuf
78 }
79 | "{" { quotation_nesting n l 1 lexbuf }
80 | _ { quotation n l lexbuf }
81
82 and quotation_start n = parse
83 | eof { raise Unterminated }
84 | utf8_extra_byte { incr utf8_adjust; quotation n 1 lexbuf }
85 | "{" { quotation_start (n+1) lexbuf }
86 | _ { quotation n 1 lexbuf }
87
88 (** NB : [mkiter] should be called on increasing offsets *)
89
90 and sentence initial stamp = parse
91 | "(*" {
92 match comment lexbuf with
93 | None -> raise Unterminated
94 | Some comm_last ->
95 stamp comm_last Tags.Script.comment;
96 sentence initial stamp lexbuf
97 }
98 | "\"" {
99 let () = rocq_string lexbuf in
100 sentence false stamp lexbuf
101 }
102 | ".." {
103 (* We must have a particular rule for parsing "..", where no dot
104 is a terminator, even if we have a blank afterwards
105 (cf. for instance the syntax for recursive notation).
106 This rule and the following one also allow to treat the "..."
107 special case, where the third dot is a terminator. *)
108 sentence false stamp lexbuf
109 }
110 | dot_sep {
111 (* The usual "." terminator *)
112 stamp (utf8_lexeme_start lexbuf) Tags.Script.sentence;
113 sentence true stamp lexbuf
114 }
115 | (vernac_control space+)* undotted_sep {
116 (* Separators like { or } and bullets * - + are only active
117 at the start of a sentence *)
118 if initial then stamp (utf8_lexeme_start lexbuf + String.length (Lexing.lexeme lexbuf) - 1) Tags.Script.sentence;
119 sentence initial stamp lexbuf
120 }
121 | ['a'-'z' 'A'-'Z'] ":{{" {
122 quotation_start 2 lexbuf;
123 sentence false stamp lexbuf
124 }
125 | space+ {
126 (* Parsing spaces is the only situation preserving initiality *)
127 sentence initial stamp lexbuf
128 }
129 | utf8_extra_byte { incr utf8_adjust; sentence false stamp lexbuf }
130 | eof { if initial then () else raise Unterminated }
131 | _ {
132 (* Any other characters *)
133 sentence false stamp lexbuf
134 }
135
136 {
137
138 (** Parse sentences in string [slice], tagging last characters
139 of sentences with the [stamp] function.
140 It will raise [Unterminated] if [slice] ends with an unfinished
141 sentence.
142 *)
143
144 let delimit_sentences stamp slice =
145 utf8_adjust := 0;
146 sentence true stamp (Lexing.from_string slice)
147
148 }