TOML 1.1 codecs for OCaml

ocaml-tomlt: fix E001 high cyclomatic complexity in string parsers

Split single loop with multiline branch into separate loop_ml/loop_sl
functions in parse_basic_string and parse_literal_string.

+65 -65
+65 -65
lib_bytesrw/tomlt_bytesrw.ml
··· 390 390 true 391 391 | _ -> false 392 392 in 393 - let rec loop () = 393 + let rec loop_ml () = 394 394 if is_eof l then failwith "Unterminated string"; 395 395 let c = get_current l in 396 - if multiline then begin 397 - if c = '"' then handle_multiline_quotes l buf '"' loop 398 - else if c = '\\' then handle_multiline_basic_backslash l buf loop 396 + if c = '"' then handle_multiline_quotes l buf '"' loop_ml 397 + else if c = '\\' then handle_multiline_basic_backslash l buf loop_ml 398 + else begin 399 + if c = '\r' then begin 400 + advance l; 401 + if peek l = Some '\n' then ( 402 + Buffer.add_char buf '\n'; 403 + advance l) 404 + else failwith "Bare carriage return not allowed in string" 405 + end 406 + else if Char.code c >= 0x80 then validate_and_add_utf8_to_buffer l buf 399 407 else begin 400 - if c = '\r' then begin 401 - advance l; 402 - if peek l = Some '\n' then ( 403 - Buffer.add_char buf '\n'; 404 - advance l) 405 - else failwith "Bare carriage return not allowed in string" 406 - end 407 - else if Char.code c >= 0x80 then validate_and_add_utf8_to_buffer l buf 408 - else begin 409 - validate_string_char l c true; 410 - Buffer.add_char buf c; 411 - advance l 412 - end; 413 - loop () 414 - end 408 + validate_string_char l c true; 409 + Buffer.add_char buf c; 410 + advance l 411 + end; 412 + loop_ml () 415 413 end 414 + in 415 + let rec loop_sl () = 416 + if is_eof l then failwith "Unterminated string"; 417 + let c = get_current l in 418 + if c = '"' then advance l 419 + else if c = '\\' then ( 420 + Buffer.add_string buf (parse_escape l); 421 + loop_sl ()) 422 + else if c = '\n' || c = '\r' then 423 + failwith "Newline not allowed in basic string" 416 424 else begin 417 - if c = '"' then advance l 418 - else if c = '\\' then ( 419 - Buffer.add_string buf (parse_escape l); 420 - loop ()) 421 - else if c = '\n' || c = '\r' then 422 - failwith "Newline not allowed in basic string" 425 + if Char.code c >= 0x80 then validate_and_add_utf8_to_buffer l buf 423 426 else begin 424 - if Char.code c >= 0x80 then validate_and_add_utf8_to_buffer l buf 425 - else begin 426 - validate_string_char l c false; 427 - Buffer.add_char buf c; 428 - advance l 429 - end; 430 - loop () 431 - end 427 + validate_string_char l c false; 428 + Buffer.add_char buf c; 429 + advance l 430 + end; 431 + loop_sl () 432 432 end 433 433 in 434 - loop (); 434 + if multiline then loop_ml () else loop_sl (); 435 435 (Buffer.contents buf, multiline) 436 436 437 437 let validate_literal_ctrl l c code ~multiline = ··· 462 462 true 463 463 | _ -> false 464 464 in 465 - let rec loop () = 465 + let rec loop_ml () = 466 466 if is_eof l then failwith "Unterminated literal string"; 467 467 let c = get_current l in 468 - if multiline then begin 469 - if c = '\'' then handle_multiline_quotes l buf '\'' loop 468 + if c = '\'' then handle_multiline_quotes l buf '\'' loop_ml 469 + else begin 470 + if c = '\r' then begin 471 + advance l; 472 + if peek l = Some '\n' then ( 473 + Buffer.add_char buf '\n'; 474 + advance l) 475 + else failwith "Bare carriage return not allowed in literal string" 476 + end 477 + else if Char.code c >= 0x80 then validate_and_add_utf8_to_buffer l buf 470 478 else begin 471 - if c = '\r' then begin 472 - advance l; 473 - if peek l = Some '\n' then ( 474 - Buffer.add_char buf '\n'; 475 - advance l) 476 - else failwith "Bare carriage return not allowed in literal string" 477 - end 478 - else if Char.code c >= 0x80 then validate_and_add_utf8_to_buffer l buf 479 - else begin 480 - validate_literal_ctrl l c (Char.code c) ~multiline:true; 481 - Buffer.add_char buf c; 482 - advance l 483 - end; 484 - loop () 485 - end 479 + validate_literal_ctrl l c (Char.code c) ~multiline:true; 480 + Buffer.add_char buf c; 481 + advance l 482 + end; 483 + loop_ml () 486 484 end 485 + in 486 + let rec loop_sl () = 487 + if is_eof l then failwith "Unterminated literal string"; 488 + let c = get_current l in 489 + if c = '\'' then advance l 490 + else if c = '\n' || c = '\r' then 491 + failwith "Newline not allowed in literal string" 487 492 else begin 488 - if c = '\'' then advance l 489 - else if c = '\n' || c = '\r' then 490 - failwith "Newline not allowed in literal string" 493 + let code = Char.code c in 494 + if code >= 0x80 then validate_and_add_utf8_to_buffer l buf 491 495 else begin 492 - let code = Char.code c in 493 - if code >= 0x80 then validate_and_add_utf8_to_buffer l buf 494 - else begin 495 - validate_literal_ctrl l c code ~multiline:false; 496 - Buffer.add_char buf c; 497 - advance l 498 - end; 499 - loop () 500 - end 496 + validate_literal_ctrl l c code ~multiline:false; 497 + Buffer.add_char buf c; 498 + advance l 499 + end; 500 + loop_sl () 501 501 end 502 502 in 503 - loop (); 503 + if multiline then loop_ml () else loop_sl (); 504 504 (Buffer.contents buf, multiline) 505 505 506 506 let parse_number l =