| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open Util |
| 13 |
open Tok |
| 14 |
|
| 15 |
module Stream = Gramlib.Stream |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
module CharOrd = struct type t = char let compare : char -> char -> int = compare end |
| 21 |
module CharMap = Map.Make (CharOrd) |
| 22 |
|
| 23 |
type starts_quotation = NoQuotation | Quotation |
| 24 |
|
| 25 |
type ttree = { |
| 26 |
node : (string * starts_quotation) option; |
| 27 |
branch : ttree CharMap.t; |
| 28 |
} |
| 29 |
|
| 30 |
let empty_keyword_state = { node = None; branch = CharMap.empty } |
| 31 |
|
| 32 |
let ttree_add ttree (str,quot) = |
| 33 |
let rec insert tt i = |
| 34 |
if i == String.length str then |
| 35 |
{node = Some (str,quot); branch = tt.branch} |
| 36 |
else |
| 37 |
let c = str.[i] in |
| 38 |
let br = |
| 39 |
match try Some (CharMap.find c tt.branch) with Not_found -> None with |
| 40 |
| Some tt' -> |
| 41 |
CharMap.add c (insert tt' (i + 1)) (CharMap.remove c tt.branch) |
| 42 |
| None -> |
| 43 |
let tt' = {node = None; branch = CharMap.empty} in |
| 44 |
CharMap.add c (insert tt' (i + 1)) tt.branch |
| 45 |
in |
| 46 |
{ node = tt.node; branch = br } |
| 47 |
in |
| 48 |
insert ttree 0 |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
let ttree_find ttree str = |
| 54 |
let rec proc_rec tt i = |
| 55 |
if i == String.length str then Some tt |
| 56 |
else match CharMap.find_opt str.[i] tt.branch with |
| 57 |
| Some tt -> proc_rec tt (i+1) |
| 58 |
| None -> None |
| 59 |
in |
| 60 |
proc_rec ttree 0 |
| 61 |
|
| 62 |
let ttree_elements ttree = |
| 63 |
let rec elts tt accu = |
| 64 |
let accu = match tt.node with |
| 65 |
| None -> accu |
| 66 |
| Some (s,_) -> CString.Set.add s accu |
| 67 |
in |
| 68 |
CharMap.fold (fun _ tt accu -> elts tt accu) tt.branch accu |
| 69 |
in |
| 70 |
elts ttree CString.Set.empty |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
module Error = struct |
| 75 |
|
| 76 |
type t = |
| 77 |
| Illegal_character |
| 78 |
| Unterminated_comment |
| 79 |
| Unterminated_string |
| 80 |
| Unterminated_quotation |
| 81 |
| Undefined_token |
| 82 |
| Bad_token of string |
| 83 |
|
| 84 |
exception E of t |
| 85 |
|
| 86 |
let to_string x = |
| 87 |
"Syntax Error: Lexer: " ^ |
| 88 |
(match x with |
| 89 |
| Illegal_character -> "Illegal character" |
| 90 |
| Unterminated_comment -> "Unterminated comment" |
| 91 |
| Unterminated_string -> "Unterminated string" |
| 92 |
| Unterminated_quotation -> "Unterminated quotation" |
| 93 |
| Undefined_token -> "Undefined token" |
| 94 |
| Bad_token tok -> Format.sprintf "Bad token %S" tok) |
| 95 |
|
| 96 |
end |
| 97 |
open Error |
| 98 |
|
| 99 |
let err loc str = Loc.raise ~loc (Error.E str) |
| 100 |
|
| 101 |
let bad_token str = raise (Error.E (Bad_token str)) |
| 102 |
|
| 103 |
|
| 104 |
let set_loc_pos loc bp ep = |
| 105 |
Loc.sub loc (bp - loc.Loc.bp) (ep - bp) |
| 106 |
|
| 107 |
|
| 108 |
let bump_loc_line loc bol_pos = |
| 109 |
Loc.{ loc with |
| 110 |
line_nb = loc.line_nb + 1; |
| 111 |
line_nb_last = loc.line_nb + 1; |
| 112 |
bol_pos; |
| 113 |
bol_pos_last = bol_pos; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
let bump_loc_line_last loc bol_pos = |
| 122 |
let open Loc in |
| 123 |
let loc' = { loc with |
| 124 |
line_nb = loc.line_nb_last + 1; |
| 125 |
line_nb_last = loc.line_nb_last + 1; |
| 126 |
bol_pos; |
| 127 |
bol_pos_last = bol_pos; |
| 128 |
bp = loc.bp + 1; |
| 129 |
ep = loc.ep + 1; |
| 130 |
} in |
| 131 |
Loc.merge loc loc' |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
let after loc = |
| 136 |
Loc.{ loc with |
| 137 |
line_nb = loc.line_nb_last; |
| 138 |
bol_pos = loc.bol_pos_last; |
| 139 |
bp = loc.ep; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
type token_kind = |
| 145 |
| Utf8Token of (Unicode.status * int) |
| 146 |
| EmptyStream |
| 147 |
|
| 148 |
let error_utf8 loc cs = |
| 149 |
let bp = Stream.count cs in |
| 150 |
Stream.junk () cs; |
| 151 |
let loc = set_loc_pos loc bp (bp+1) in |
| 152 |
err loc Illegal_character |
| 153 |
|
| 154 |
let check_utf8_trailing_byte loc cs c = |
| 155 |
if not (Int.equal (Char.code c land 0xC0) 0x80) then error_utf8 loc cs |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
let lookup_utf8_char loc nj cs = |
| 160 |
match Stream.nth () nj cs with |
| 161 |
| None -> [] |
| 162 |
| Some c1 -> |
| 163 |
match c1 with |
| 164 |
| '\x00'..'\x7F' -> [c1] |
| 165 |
| c1 -> |
| 166 |
let c1 = Char.code c1 in |
| 167 |
if Int.equal (c1 land 0x40) 0 || Int.equal (c1 land 0x38) 0x38 then error_utf8 loc cs else |
| 168 |
if Int.equal (c1 land 0x20) 0 then |
| 169 |
match List.skipn nj (Stream.npeek () (nj+2) cs) with |
| 170 |
| [_;c2] as l -> check_utf8_trailing_byte loc cs c2; l |
| 171 |
| _ -> error_utf8 loc cs |
| 172 |
else if Int.equal (c1 land 0x10) 0 then |
| 173 |
match List.skipn nj (Stream.npeek () (nj+3) cs) with |
| 174 |
| [_;c2;c3] as l -> |
| 175 |
check_utf8_trailing_byte loc cs c2; |
| 176 |
check_utf8_trailing_byte loc cs c3; |
| 177 |
l |
| 178 |
| _ -> error_utf8 loc cs |
| 179 |
else match List.skipn nj (Stream.npeek () (nj+4) cs) with |
| 180 |
| [_;c2;c3;c4] as l -> |
| 181 |
check_utf8_trailing_byte loc cs c2; |
| 182 |
check_utf8_trailing_byte loc cs c3; |
| 183 |
check_utf8_trailing_byte loc cs c4; |
| 184 |
l |
| 185 |
| _ -> error_utf8 loc cs |
| 186 |
|
| 187 |
let status_of_utf8 = function |
| 188 |
| [] -> EmptyStream |
| 189 |
| l -> |
| 190 |
let n, unicode = match l with |
| 191 |
| [c1] -> 1, Char.code c1 |
| 192 |
| [c1;c2] -> 2, (Char.code c1 land 0x1F) lsl 6 + (Char.code c2 land 0x3F) |
| 193 |
| [c1;c2;c3] -> |
| 194 |
3, (Char.code c1 land 0x0F) lsl 12 + (Char.code c2 land 0x3F) lsl 6 + |
| 195 |
(Char.code c3 land 0x3F) |
| 196 |
| [c1;c2;c3;c4] -> |
| 197 |
4, (Char.code c1 land 0x07) lsl 18 + (Char.code c2 land 0x3F) lsl 12 + |
| 198 |
(Char.code c3 land 0x3F) lsl 6 + (Char.code c4 land 0x3F) |
| 199 |
| _ -> assert false |
| 200 |
in |
| 201 |
Utf8Token (Unicode.classify unicode, n) |
| 202 |
|
| 203 |
let lookup_utf8 loc cs = |
| 204 |
status_of_utf8 (lookup_utf8_char loc 0 cs) |
| 205 |
|
| 206 |
let is_letter l = |
| 207 |
match status_of_utf8 l with |
| 208 |
| EmptyStream -> false |
| 209 |
| Utf8Token (st,_) -> Unicode.is_letter st |
| 210 |
|
| 211 |
let unlocated f x = |
| 212 |
let dummy_loc = Loc.(initial ToplevelInput) in |
| 213 |
f dummy_loc x |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
let check_keyword str = |
| 218 |
let rec loop_symb s = match Stream.peek () s with |
| 219 |
| Some (' ' | '\n' | '\r' | '\t') -> |
| 220 |
Stream.junk () s; |
| 221 |
bad_token str |
| 222 |
| _ -> |
| 223 |
match unlocated lookup_utf8 s with |
| 224 |
| Utf8Token (_,n) -> Stream.njunk () n s; loop_symb s |
| 225 |
| EmptyStream -> () |
| 226 |
in |
| 227 |
loop_symb (Stream.of_string str) |
| 228 |
|
| 229 |
let check_ident str = |
| 230 |
let rec loop_id intail s = |
| 231 |
match unlocated lookup_utf8 s with |
| 232 |
| Utf8Token (st, n) when not intail && Unicode.is_valid_ident_initial st -> |
| 233 |
Stream.njunk () n s; loop_id true s |
| 234 |
| Utf8Token (st, n) when intail && Unicode.is_valid_ident_trailing st -> |
| 235 |
Stream.njunk () n s; |
| 236 |
loop_id true s |
| 237 |
| EmptyStream -> () |
| 238 |
| Utf8Token _ -> bad_token str |
| 239 |
in |
| 240 |
loop_id false (Stream.of_string str) |
| 241 |
|
| 242 |
let is_ident str = |
| 243 |
try let _ = check_ident str in true with Error.E _ -> false |
| 244 |
|
| 245 |
let is_keyword ttree s = |
| 246 |
match ttree_find ttree s with |
| 247 |
| None -> false |
| 248 |
| Some { node = None } -> false |
| 249 |
| Some { node = Some _ } -> true |
| 250 |
|
| 251 |
let add_keyword ?(quotation=NoQuotation) ttree str = |
| 252 |
if not (is_keyword ttree str) then |
| 253 |
begin |
| 254 |
check_keyword str; |
| 255 |
ttree_add ttree (str,quotation) |
| 256 |
end |
| 257 |
else ttree |
| 258 |
|
| 259 |
let add_keyword_tok : type c. _ -> c Tok.p -> _ = fun ttree -> function |
| 260 |
| PKEYWORD s -> add_keyword ~quotation:NoQuotation ttree s |
| 261 |
| PQUOTATION s -> add_keyword ~quotation:Quotation ttree s |
| 262 |
| _ -> ttree |
| 263 |
|
| 264 |
let keywords = ttree_elements |
| 265 |
|
| 266 |
|
| 267 |
type keyword_state = ttree |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
let buff = ref (Bytes.create 80) |
| 272 |
|
| 273 |
let store len x = |
| 274 |
let open Bytes in |
| 275 |
if len >= length !buff then |
| 276 |
buff := cat !buff (create (length !buff)); |
| 277 |
set !buff len x; |
| 278 |
succ len |
| 279 |
|
| 280 |
|
| 281 |
let rec nstore n len cs = |
| 282 |
if n>0 then nstore (n-1) (store len (Option.get @@ Stream.next () cs)) cs else len |
| 283 |
|
| 284 |
let get_buff len = Bytes.sub_string !buff 0 len |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
let warn_unrecognized_unicode = |
| 289 |
CWarnings.create ~name:"unrecognized-unicode" ~category:CWarnings.CoreCategories.parsing |
| 290 |
(fun (u,id) -> |
| 291 |
strbrk (Printf.sprintf "Not considering unicode character \"%s\" of unknown \ |
| 292 |
lexical status as part of identifier \"%s\"." u id)) |
| 293 |
|
| 294 |
let rec ident_tail loc len s = |
| 295 |
match lookup_utf8 loc s with |
| 296 |
| Utf8Token (st, n) when Unicode.is_valid_ident_trailing st -> |
| 297 |
ident_tail loc (nstore n len s) s |
| 298 |
| Utf8Token (st, n) when Unicode.is_unknown st -> |
| 299 |
let id = get_buff len in |
| 300 |
let u = String.concat "" (List.map (String.make 1) (Stream.npeek () n s)) in |
| 301 |
warn_unrecognized_unicode ~loc (u,id); len |
| 302 |
| _ -> len |
| 303 |
|
| 304 |
let warn_comment_terminator_in_string = |
| 305 |
CWarnings.create ~name:"comment-terminator-in-string" ~category:CWarnings.CoreCategories.parsing |
| 306 |
(fun () -> |
| 307 |
(strbrk |
| 308 |
"Not interpreting \"*)\" as the end of current \ |
| 309 |
non-terminated comment because it occurs in a \ |
| 310 |
non-terminated string of the comment.")) |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
let rec string loc ~comm_level bp len s = match Stream.peek () s with |
| 315 |
| Some '"' -> |
| 316 |
Stream.junk () s; |
| 317 |
let esc = |
| 318 |
match Stream.peek () s with |
| 319 |
Some '"' -> Stream.junk () s; true |
| 320 |
| _ -> false |
| 321 |
in |
| 322 |
if esc then string loc ~comm_level bp (store len '"') s else (loc, len) |
| 323 |
| Some '(' -> |
| 324 |
Stream.junk () s; |
| 325 |
(fun s -> match Stream.peek () s with |
| 326 |
| Some '*' -> |
| 327 |
Stream.junk () s; |
| 328 |
let comm_level = Option.map succ comm_level in |
| 329 |
string loc ~comm_level |
| 330 |
bp (store (store len '(') '*') |
| 331 |
s |
| 332 |
| _ -> |
| 333 |
string loc ~comm_level bp (store len '(') s) s |
| 334 |
| Some '*' -> |
| 335 |
Stream.junk () s; |
| 336 |
(fun s -> match Stream.peek () s with |
| 337 |
| Some ')' -> |
| 338 |
Stream.junk () s; |
| 339 |
let () = match comm_level with |
| 340 |
| Some 0 -> |
| 341 |
warn_comment_terminator_in_string ~loc () |
| 342 |
| _ -> () |
| 343 |
in |
| 344 |
let comm_level = Option.map pred comm_level in |
| 345 |
string loc ~comm_level bp (store (store len '*') ')') s |
| 346 |
| _ -> |
| 347 |
string loc ~comm_level bp (store len '*') s) s |
| 348 |
| Some ('\n' as c) -> |
| 349 |
Stream.junk () s; |
| 350 |
let ep = Stream.count s in |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
let loc = |
| 355 |
if Option.has_some comm_level then bump_loc_line loc ep |
| 356 |
else bump_loc_line_last loc ep |
| 357 |
in |
| 358 |
string loc ~comm_level bp (store len c) s |
| 359 |
| Some c -> |
| 360 |
Stream.junk () s; |
| 361 |
string loc ~comm_level bp (store len c) s |
| 362 |
| None -> |
| 363 |
let ep = Stream.count s in |
| 364 |
let loc = set_loc_pos loc bp ep in |
| 365 |
err loc Unterminated_string |
| 366 |
|
| 367 |
|
| 368 |
let comment_begin = ref None |
| 369 |
let comm_loc bp = match !comment_begin with |
| 370 |
| None -> comment_begin := Some bp |
| 371 |
| _ -> () |
| 372 |
|
| 373 |
let comments = ref [] |
| 374 |
let current_comment = Buffer.create 8192 |
| 375 |
|
| 376 |
let real_push_char c = Buffer.add_char current_comment c |
| 377 |
|
| 378 |
let push_string s = Buffer.add_string current_comment s |
| 379 |
|
| 380 |
let dbg = CDebug.create ~name:"comment-lexing" () |
| 381 |
|
| 382 |
let record_comments = ref false |
| 383 |
|
| 384 |
let comment_stop ep = |
| 385 |
let current_s = Buffer.contents current_comment in |
| 386 |
(if !record_comments && Buffer.length current_comment > 0 then |
| 387 |
let bp = match !comment_begin with |
| 388 |
Some bp -> bp |
| 389 |
| None -> |
| 390 |
Feedback.msg_debug |
| 391 |
(str "No begin location for comment '" |
| 392 |
++ str current_s ++str"' ending at " |
| 393 |
++ int ep); |
| 394 |
ep-1 in |
| 395 |
dbg Pp.(fun () -> |
| 396 |
str "comment at chars " ++ int bp ++ str "-" ++ int ep ++ str ":" ++ fnl() ++ |
| 397 |
str current_s); |
| 398 |
comments := ((bp,ep),current_s) :: !comments); |
| 399 |
Buffer.clear current_comment; |
| 400 |
comment_begin := None |
| 401 |
|
| 402 |
let rec comment loc bp s = |
| 403 |
let bp2 = Stream.count s in |
| 404 |
match Stream.peek () s with |
| 405 |
| Some '(' -> |
| 406 |
Stream.junk () s; |
| 407 |
let loc = |
| 408 |
match Stream.peek () s with |
| 409 |
| Some '*' -> |
| 410 |
Stream.junk () s; |
| 411 |
push_string "(*"; comment loc bp s |
| 412 |
| _ -> push_string "("; loc |
| 413 |
in |
| 414 |
comment loc bp s |
| 415 |
| Some '*' -> |
| 416 |
Stream.junk () s; |
| 417 |
begin match Stream.peek () s with |
| 418 |
| Some ')' -> Stream.junk () s; push_string "*)"; loc |
| 419 |
| _ -> real_push_char '*'; comment loc bp s |
| 420 |
end |
| 421 |
| Some '"' -> |
| 422 |
Stream.junk () s; |
| 423 |
let loc, len = string loc ~comm_level:(Some 0) bp2 0 s in |
| 424 |
push_string "\""; push_string (get_buff len); push_string "\""; |
| 425 |
comment loc bp s |
| 426 |
| Some ('\n' as z) -> |
| 427 |
Stream.junk () s; |
| 428 |
let ep = Stream.count s in |
| 429 |
real_push_char z; comment (bump_loc_line loc ep) bp s |
| 430 |
| Some z -> |
| 431 |
Stream.junk () s; |
| 432 |
real_push_char z; comment loc bp s |
| 433 |
| None -> |
| 434 |
let ep = Stream.count s in |
| 435 |
let loc = set_loc_pos loc bp ep in |
| 436 |
err loc Unterminated_comment |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
let update_longest_valid_token last nj tt cs = |
| 446 |
match tt.node with |
| 447 |
| Some _ as last' -> |
| 448 |
|
| 449 |
|
| 450 |
Stream.njunk () nj cs; 0, last' |
| 451 |
| None -> |
| 452 |
|
| 453 |
nj, last |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
let rec progress_further loc last nj last_is_letter tt cs = |
| 458 |
match lookup_utf8_char loc nj cs with |
| 459 |
| [] -> snd (update_longest_valid_token last nj tt cs) |
| 460 |
| l -> progress_utf8 loc last nj last_is_letter tt cs l |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
and progress_utf8 loc last nj last_is_letter tt cs l = |
| 465 |
let is_letter' = is_letter l in |
| 466 |
|
| 467 |
|
| 468 |
let nj, last = if last_is_letter && is_letter' then nj, last else update_longest_valid_token last nj tt cs in |
| 469 |
try |
| 470 |
|
| 471 |
let tt = List.fold_left (fun tt c -> CharMap.find c tt.branch) tt l in |
| 472 |
progress_further loc last (nj + List.length l) is_letter' tt cs |
| 473 |
with Not_found -> |
| 474 |
last |
| 475 |
|
| 476 |
let blank_or_eof cs = |
| 477 |
match Stream.peek () cs with |
| 478 |
| None -> true |
| 479 |
| Some (' ' | '\t' | '\n' |'\r') -> true |
| 480 |
| _ -> false |
| 481 |
|
| 482 |
type marker = Delimited of int * char list * char list | ImmediateAsciiIdent |
| 483 |
|
| 484 |
let peek_marker_len b e s = |
| 485 |
let rec peek n = |
| 486 |
match Stream.nth () n s with |
| 487 |
| Some c -> if c = b then peek (n+1) else n, List.make n b, List.make n e |
| 488 |
| None -> n, List.make n b, List.make n e |
| 489 |
in |
| 490 |
let len, start, stop = peek 0 in |
| 491 |
if len = 0 then None |
| 492 |
else Some (Delimited (len, start, stop)) |
| 493 |
|
| 494 |
let peek_marker s = |
| 495 |
match Stream.peek () s with |
| 496 |
| Some '(' -> peek_marker_len '(' ')' s |
| 497 |
| Some '[' -> peek_marker_len '[' ']' s |
| 498 |
| Some '{' -> peek_marker_len '{' '}' s |
| 499 |
| Some ('a'..'z' | 'A'..'Z' | '_') -> Some ImmediateAsciiIdent |
| 500 |
| Some _ | None -> None |
| 501 |
|
| 502 |
let parse_quotation loc bp s = |
| 503 |
match peek_marker s with |
| 504 |
| Some ImmediateAsciiIdent -> |
| 505 |
let c = Option.get @@ Stream.next () s in |
| 506 |
let len = ident_tail loc (store 0 c) s in |
| 507 |
(get_buff len, set_loc_pos loc bp (Stream.count s)) |
| 508 |
| Some (Delimited (lenmarker, bmarker, emarker)) -> |
| 509 |
let dot_gobbling = |
| 510 |
|
| 511 |
match bmarker with |
| 512 |
| '{' :: '{' :: _ -> true |
| 513 |
| _ -> false in |
| 514 |
let b = Buffer.create 80 in |
| 515 |
let commit1 c = Buffer.add_char b c; Stream.junk () s in |
| 516 |
let commit l = List.iter commit1 l in |
| 517 |
let rec quotation loc depth = |
| 518 |
match Stream.npeek () lenmarker s with |
| 519 |
| l when l = bmarker -> |
| 520 |
commit l; |
| 521 |
quotation loc (depth + 1) |
| 522 |
| l when l = emarker -> |
| 523 |
commit l; |
| 524 |
if depth > 1 then quotation loc (depth - 1) else loc |
| 525 |
| '\n' :: cs -> |
| 526 |
commit1 '\n'; |
| 527 |
let loc = bump_loc_line_last loc (Stream.count s) in |
| 528 |
quotation loc depth |
| 529 |
| '.' :: _ -> |
| 530 |
commit1 '.'; |
| 531 |
if not dot_gobbling && blank_or_eof s then |
| 532 |
err (set_loc_pos loc bp (Stream.count s)) Unterminated_quotation |
| 533 |
else quotation loc depth |
| 534 |
| c :: cs -> |
| 535 |
commit1 c; |
| 536 |
quotation loc depth |
| 537 |
| [] -> err (set_loc_pos loc bp (Stream.count s)) Unterminated_quotation |
| 538 |
in |
| 539 |
let loc = quotation loc 0 in |
| 540 |
(Buffer.contents b, set_loc_pos loc bp (Stream.count s)) |
| 541 |
| None -> err (set_loc_pos loc bp (Stream.count s)) Unterminated_quotation |
| 542 |
|
| 543 |
let peek_string v s = |
| 544 |
let l = String.length v in |
| 545 |
let rec aux i = |
| 546 |
if Int.equal i l then true |
| 547 |
else |
| 548 |
let l' = Stream.npeek () (i + 1) s in |
| 549 |
match List.nth_opt l' i with |
| 550 |
| Some c -> Char.equal c v.[i] && aux (i + 1) |
| 551 |
| None -> false in |
| 552 |
aux 0 |
| 553 |
|
| 554 |
let find_keyword ttree loc notkw id bp s = |
| 555 |
if peek_string ":{{" s then |
| 556 |
begin |
| 557 |
|
| 558 |
Stream.junk () s; |
| 559 |
let txt, loc = parse_quotation loc bp s in |
| 560 |
(QUOTATION (id ^ ":", txt), loc) |
| 561 |
end |
| 562 |
else |
| 563 |
let tt = ttree_find ttree id in |
| 564 |
let further = Option.bind tt (fun tt -> progress_further loc tt.node 0 true tt s) in |
| 565 |
match further with |
| 566 |
| None -> |
| 567 |
let ep = Stream.count s in |
| 568 |
(notkw, set_loc_pos loc bp ep) |
| 569 |
| Some (c,NoQuotation) -> |
| 570 |
let ep = Stream.count s in |
| 571 |
(KEYWORD c, set_loc_pos loc bp ep) |
| 572 |
| Some (c,Quotation) -> |
| 573 |
let txt, loc = parse_quotation loc bp s in |
| 574 |
(QUOTATION(c, txt), loc) |
| 575 |
|
| 576 |
let process_sequence loc bp c cs = |
| 577 |
let rec aux n cs = |
| 578 |
match Stream.peek () cs with |
| 579 |
| Some c' when c == c' -> Stream.junk () cs; aux (n+1) cs |
| 580 |
| _ -> BULLET (String.make n c), set_loc_pos loc bp (Stream.count cs) |
| 581 |
in |
| 582 |
aux 1 cs |
| 583 |
|
| 584 |
|
| 585 |
let process_chars ~diff_mode ttree loc bp l cs = |
| 586 |
let t = progress_utf8 loc None (- (List.length l)) false ttree cs l in |
| 587 |
let ep = Stream.count cs in |
| 588 |
match t with |
| 589 |
| Some (t,NoQuotation) -> (KEYWORD t, set_loc_pos loc bp ep) |
| 590 |
| Some (c,Quotation) -> |
| 591 |
let txt, loc = parse_quotation loc bp cs in |
| 592 |
(QUOTATION(c, txt), loc) |
| 593 |
| None -> |
| 594 |
if diff_mode then begin |
| 595 |
let s = String.concat "" (List.map (String.make 1) l) in |
| 596 |
(IDENT s, set_loc_pos loc bp ep) |
| 597 |
end else begin |
| 598 |
let loc = set_loc_pos loc bp ep in |
| 599 |
err loc Undefined_token |
| 600 |
end |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
let parse_after_dot ~diff_mode ttree loc c bp s = |
| 605 |
match lookup_utf8 loc s with |
| 606 |
| Utf8Token (st, n) when Unicode.is_valid_ident_initial st -> |
| 607 |
let len = ident_tail loc (nstore n 0 s) s in |
| 608 |
let field = get_buff len in |
| 609 |
find_keyword ttree loc (FIELD field) ("."^field) bp s |
| 610 |
| Utf8Token _ | EmptyStream -> |
| 611 |
process_chars ~diff_mode ttree loc bp [c] s |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
let parse_after_qmark ~diff_mode ttree loc bp s = |
| 616 |
match lookup_utf8 loc s with |
| 617 |
| Utf8Token (st, _) when Unicode.is_valid_ident_initial st -> LEFTQMARK |
| 618 |
| EmptyStream -> KEYWORD "?" |
| 619 |
| Utf8Token _ -> fst (process_chars ~diff_mode ttree loc bp ['?'] s) |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
let between_commands = ref true |
| 625 |
|
| 626 |
let rec next_token ~diff_mode ttree loc s = |
| 627 |
let bp = Stream.count s in |
| 628 |
match Stream.peek () s with |
| 629 |
| Some '\n' -> |
| 630 |
Stream.junk () s; |
| 631 |
let ep = Stream.count s in |
| 632 |
next_token ~diff_mode ttree (bump_loc_line loc ep) s |
| 633 |
| Some (' ' | '\t' | '\r') -> |
| 634 |
Stream.junk () s; |
| 635 |
next_token ~diff_mode ttree loc s |
| 636 |
| Some ('.' as c) -> |
| 637 |
Stream.junk () s; |
| 638 |
let t, newloc = parse_after_dot ~diff_mode ttree loc c bp s in |
| 639 |
between_commands := false; |
| 640 |
|
| 641 |
|
| 642 |
let () = match t with |
| 643 |
| KEYWORD ("." | "...") -> |
| 644 |
if not (blank_or_eof s) then begin |
| 645 |
let ep = Stream.count s in |
| 646 |
err (set_loc_pos loc bp (ep+1)) Undefined_token |
| 647 |
end; |
| 648 |
between_commands := true; |
| 649 |
| _ -> () |
| 650 |
in |
| 651 |
t, newloc |
| 652 |
| Some ('-' | '+' | '*' as c) -> |
| 653 |
Stream.junk () s; |
| 654 |
let t,new_between_commands = |
| 655 |
if !between_commands then process_sequence loc bp c s, true |
| 656 |
else process_chars ~diff_mode ttree loc bp [c] s,false |
| 657 |
in |
| 658 |
between_commands := new_between_commands; t |
| 659 |
| Some '?' -> |
| 660 |
Stream.junk () s; |
| 661 |
let ep = Stream.count s in |
| 662 |
let t = parse_after_qmark ~diff_mode ttree loc bp s in |
| 663 |
between_commands := false; |
| 664 |
(t, set_loc_pos loc bp ep) |
| 665 |
| Some ('a'..'z' | 'A'..'Z' | '_' as c) -> |
| 666 |
Stream.junk () s; |
| 667 |
let len = ident_tail loc (store 0 c) s in |
| 668 |
let id = get_buff len in |
| 669 |
between_commands := false; |
| 670 |
find_keyword ttree loc (IDENT id) id bp s |
| 671 |
| Some ('0'..'9') -> |
| 672 |
let n = NumTok.Unsigned.parse s in |
| 673 |
between_commands := false; |
| 674 |
find_keyword ttree loc (NUMBER n) (NumTok.Unsigned.sprint n) bp s |
| 675 |
| Some '\"' -> |
| 676 |
Stream.junk () s; |
| 677 |
let (loc, len) = string loc ~comm_level:None bp 0 s in |
| 678 |
between_commands := false; |
| 679 |
let str = get_buff len in |
| 680 |
find_keyword ttree loc (STRING str) (CString.quote_coq_string str) bp s |
| 681 |
| Some ('(' as c) -> |
| 682 |
Stream.junk () s; |
| 683 |
begin |
| 684 |
match Stream.peek () s with |
| 685 |
| Some '*' when diff_mode -> |
| 686 |
Stream.junk () s; |
| 687 |
let ep = Stream.count s in |
| 688 |
(IDENT "(*", set_loc_pos loc bp ep) |
| 689 |
| Some '*' -> |
| 690 |
Stream.junk () s; |
| 691 |
comm_loc bp; |
| 692 |
push_string "(*"; |
| 693 |
let loc = comment loc bp s in |
| 694 |
comment_stop (Stream.count s); |
| 695 |
next_token ~diff_mode ttree loc s |
| 696 |
| _ -> |
| 697 |
let t = process_chars ~diff_mode ttree loc bp [c] s in |
| 698 |
between_commands := false; |
| 699 |
t |
| 700 |
end |
| 701 |
| Some ('{' | '}' as c) -> |
| 702 |
Stream.junk () s; |
| 703 |
let ep = Stream.count s in |
| 704 |
let t,new_between_commands = |
| 705 |
if !between_commands then (KEYWORD (String.make 1 c), set_loc_pos loc bp ep), true |
| 706 |
else process_chars ~diff_mode ttree loc bp [c] s, false |
| 707 |
in |
| 708 |
between_commands := new_between_commands; t |
| 709 |
| _ -> |
| 710 |
let l = lookup_utf8_char loc 0 s in |
| 711 |
match status_of_utf8 l with |
| 712 |
| Utf8Token (st, n) when Unicode.is_valid_ident_initial st -> |
| 713 |
let len = ident_tail loc (nstore n 0 s) s in |
| 714 |
let id = get_buff len in |
| 715 |
between_commands := false; |
| 716 |
find_keyword ttree loc (IDENT id) id bp s |
| 717 |
| Utf8Token (_, n) -> |
| 718 |
Stream.njunk () n s; |
| 719 |
let t = process_chars ~diff_mode ttree loc bp l s in |
| 720 |
between_commands := false; |
| 721 |
t |
| 722 |
| EmptyStream -> |
| 723 |
between_commands := false; |
| 724 |
(EOI, set_loc_pos loc bp (bp+1)) |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
let next_token ~diff_mode ttree loc s : (Tok.t * Loc.t, Exninfo.iexn) Result.t = |
| 729 |
let f (diff_mode, ttree, loc, s) = next_token ~diff_mode ttree loc s in |
| 730 |
CErrors.to_result ~f (diff_mode, ttree, loc, s) |
| 731 |
|
| 732 |
let pattern_strings : type c. _ -> c p -> string * string option = |
| 733 |
fun kwstate -> function |
| 734 |
| PKEYWORD s -> "", Some s |
| 735 |
| PIDENT (Some s as os) -> |
| 736 |
let kind = if is_keyword kwstate s then "" else "IDENT" in |
| 737 |
kind, os |
| 738 |
| PIDENT None -> "IDENT", None |
| 739 |
| PFIELD s -> "FIELD", s |
| 740 |
| PNUMBER None -> "NUMBER", None |
| 741 |
| PNUMBER (Some n) -> "NUMBER", Some (NumTok.Unsigned.sprint n) |
| 742 |
| PSTRING s -> "STRING", s |
| 743 |
| PLEFTQMARK -> "LEFTQMARK", None |
| 744 |
| PBULLET s -> "BULLET", s |
| 745 |
| PQUOTATION lbl -> "QUOTATION", Some lbl |
| 746 |
| PEOI -> "EOI", None |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
module MakeLexer (Diff : sig val mode : bool end) |
| 751 |
: Gramlib.Plexing.S |
| 752 |
with type keyword_state = keyword_state |
| 753 |
and type te = Tok.t |
| 754 |
and type 'c pattern = 'c Tok.p |
| 755 |
= struct |
| 756 |
type nonrec keyword_state = keyword_state |
| 757 |
type te = Tok.t |
| 758 |
type 'c pattern = 'c Tok.p |
| 759 |
let tok_pattern_eq = Tok.equal_p |
| 760 |
let tok_pattern_exact = Tok.pattern_exact |
| 761 |
let tok_pattern_strings = pattern_strings |
| 762 |
let tok_func ?(loc=Loc.(initial ToplevelInput)) cs = |
| 763 |
let cur_loc = ref loc in |
| 764 |
Gramlib.LStream.from ~loc |
| 765 |
(fun ttree -> |
| 766 |
match next_token ~diff_mode:Diff.mode ttree !cur_loc cs with |
| 767 |
| Ok (tok, loc) -> |
| 768 |
cur_loc := after loc; |
| 769 |
Some (tok,loc) |
| 770 |
| Error (exn, info) -> |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
let loc = Loc.get_loc info in |
| 777 |
Option.iter (fun loc -> cur_loc := after loc) loc; |
| 778 |
Exninfo.iraise (exn, info)) |
| 779 |
let tok_match = Tok.match_pattern |
| 780 |
let tok_text = Tok.token_text |
| 781 |
|
| 782 |
|
| 783 |
module State = struct |
| 784 |
|
| 785 |
type t = int option * string * bool * ((int * int) * string) list |
| 786 |
|
| 787 |
let init () = (None,"",true,[]) |
| 788 |
let set (o,s,b,c) = |
| 789 |
comment_begin := o; |
| 790 |
Buffer.clear current_comment; Buffer.add_string current_comment s; |
| 791 |
between_commands := b; |
| 792 |
comments := c |
| 793 |
let get () = |
| 794 |
(!comment_begin, Buffer.contents current_comment, !between_commands, !comments) |
| 795 |
let drop () = set (init ()) |
| 796 |
let get_comments (_,_,_,c) = c |
| 797 |
|
| 798 |
end |
| 799 |
end |
| 800 |
|
| 801 |
module Lexer = MakeLexer (struct let mode = false end) |
| 802 |
|
| 803 |
module LexerDiff = MakeLexer (struct let mode = true end) |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
let strip s = |
| 808 |
let len = |
| 809 |
let rec loop i len = |
| 810 |
if Int.equal i (String.length s) then len |
| 811 |
else if s.[i] == ' ' then loop (i + 1) len |
| 812 |
else loop (i + 1) (len + 1) |
| 813 |
in |
| 814 |
loop 0 0 |
| 815 |
in |
| 816 |
if len == String.length s then s |
| 817 |
else |
| 818 |
let s' = Bytes.create len in |
| 819 |
let rec loop i i' = |
| 820 |
if i == String.length s then s' |
| 821 |
else if s.[i] == ' ' then loop (i + 1) i' |
| 822 |
else begin Bytes.set s' i' s.[i]; loop (i + 1) (i' + 1) end |
| 823 |
in |
| 824 |
Bytes.to_string (loop 0 0) |
| 825 |
|
| 826 |
let terminal s = |
| 827 |
let s = strip s in |
| 828 |
let () = match s with "" -> failwith "empty token." | _ -> () in |
| 829 |
if is_ident s then PIDENT (Some s) |
| 830 |
else PKEYWORD s |
| 831 |
|
| 832 |
|
| 833 |
let terminal_number s = match NumTok.Unsigned.parse_string s with |
| 834 |
| Some n -> PNUMBER (Some n) |
| 835 |
| None -> failwith "number token expected." |