From d3246545a2e1d3a4efbc20c41128ee333b6b7e74 Mon Sep 17 00:00:00 2001 From: Guillaume Cluzel Date: Sat, 8 Jun 2024 21:47:57 +0200 Subject: [PATCH] Rename some functions and improve code quality --- lib/json5/lexer.ml | 19 +++++++++++-------- lib/json5/parser.ml | 28 ++++++++++++++-------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/json5/lexer.ml b/lib/json5/lexer.ml index 7eb93f3..1ea97ab 100644 --- a/lib/json5/lexer.ml +++ b/lib/json5/lexer.ml @@ -34,15 +34,18 @@ let pp_token ppf = function | STRING s -> Format.fprintf ppf "%S" s | EOF -> Format.pp_print_string ppf "eof" -let custom_error lexbuf = +let lexer_error lexbuf = let { Lexing.pos_fname = file; pos_lnum = line; _ }, _ = Sedlexing.lexing_positions lexbuf in let file_line = - if String.equal file "" then "Line" else Format.sprintf "File %s, line" file + if String.equal file "" then "Line" else Printf.sprintf "File %s, line" file in - Format.sprintf "%s %d: Unexpected character '%s'" file_line line - (Sedlexing.Utf8.lexeme lexbuf) + let msg = + Printf.sprintf "%s %d: Unexpected character '%s'" file_line line + (Sedlexing.Utf8.lexeme lexbuf) + in + Error msg let source_character = [%sedlex.regexp? any] let line_terminator = [%sedlex.regexp? 0x000A | 0x000D | 0x2028 | 0x2029] @@ -188,7 +191,7 @@ let string_lex_single lexbuf strbuf = | Sub (source_character, ('\'' | line_terminator)) -> Buffer.add_string strbuf (lexeme lexbuf); lex lexbuf strbuf - | _ -> Error (custom_error lexbuf) + | _ -> lexer_error lexbuf in lex lexbuf strbuf @@ -205,7 +208,7 @@ let string_lex_double lexbuf strbuf = | Sub (source_character, ('"' | line_terminator)) -> Buffer.add_string strbuf (lexeme lexbuf); lex lexbuf strbuf - | _ -> Error (custom_error lexbuf) + | _ -> lexer_error lexbuf in lex lexbuf strbuf @@ -213,7 +216,7 @@ let string_lex lexbuf quote = let strbuf = Buffer.create 200 in if quote = "'" then string_lex_single lexbuf strbuf else if quote = {|"|} then string_lex_double lexbuf strbuf - else Error (Format.sprintf "Invalid string quote %S" quote) + else Error (Printf.sprintf "Invalid string quote %S" quote) let rec lex tokens buf = let lexeme = Sedlexing.Utf8.lexeme in @@ -248,4 +251,4 @@ let rec lex tokens buf = let s = lexeme buf in lex ((STRING s, pos) :: tokens) buf | eof -> Ok (List.rev ((EOF, pos) :: tokens)) - | _ -> Error (custom_error buf) + | _ -> lexer_error buf diff --git a/lib/json5/parser.ml b/lib/json5/parser.ml index 78baa56..e30670e 100644 --- a/lib/json5/parser.ml +++ b/lib/json5/parser.ml @@ -1,22 +1,22 @@ open Let_syntax.Result -let custom_error pos error = +let parser_error pos error = let file_line = if String.equal pos.Lexing.pos_fname "" then "Line" - else Format.sprintf "File %s, line" pos.Lexing.pos_fname + else Printf.sprintf "File %s, line" pos.Lexing.pos_fname in - let msg = Format.sprintf "%s %d: %s" file_line pos.Lexing.pos_lnum error in + let msg = Printf.sprintf "%s %d: %s" file_line pos.Lexing.pos_lnum error in Error msg let rec parse_list acc = function | [] -> Error "Unexpected end of input" - | [ (Lexer.EOF, pos) ] -> custom_error pos "Unexpected end of input" + | [ (Lexer.EOF, pos) ] -> parser_error pos "Unexpected end of input" | (Lexer.CLOSE_BRACKET, _) :: xs -> Ok (acc, xs) | xs -> ( let* v, xs = parse xs in match xs with | [] -> Error "Unexpected end of input" - | [ (Lexer.EOF, pos) ] -> custom_error pos "Unexpected end of input" + | [ (Lexer.EOF, pos) ] -> parser_error pos "Unexpected end of input" | (Lexer.CLOSE_BRACKET, _) :: xs | (COMMA, _) :: (CLOSE_BRACKET, _) :: xs -> Ok (v :: acc, xs) @@ -25,22 +25,22 @@ let rec parse_list acc = function let s = Format.asprintf "Unexpected list token: %a" Lexer.pp_token x in - custom_error pos s) + parser_error pos s) and parse_assoc acc = function | [] -> Error "Unexpected end of input" - | [ (Lexer.EOF, pos) ] -> custom_error pos "Unexpected end of input" + | [ (Lexer.EOF, pos) ] -> parser_error pos "Unexpected end of input" | (CLOSE_BRACE, _) :: xs -> Ok (acc, xs) | (STRING k, _) :: xs -> ( match xs with | [] -> Error "Unexpected end of input" - | [ (Lexer.EOF, pos) ] -> custom_error pos "Unexpected end of input" + | [ (Lexer.EOF, pos) ] -> parser_error pos "Unexpected end of input" | (Lexer.COLON, _) :: xs -> ( let* v, xs = parse xs in let item = (k, v) in match xs with | [] -> Error "Unexpected end of input" - | [ (Lexer.EOF, pos) ] -> custom_error pos "Unexpected end of input" + | [ (Lexer.EOF, pos) ] -> parser_error pos "Unexpected end of input" | (CLOSE_BRACE, _) :: xs | (COMMA, _) :: (CLOSE_BRACE, _) :: xs -> Ok (item :: acc, xs) | (COMMA, _) :: xs -> parse_assoc (item :: acc) xs @@ -49,22 +49,22 @@ and parse_assoc acc = function Format.asprintf "Unexpected assoc list token: %a" Lexer.pp_token x in - custom_error pos s) + parser_error pos s) | (x, pos) :: _ -> let s = Format.asprintf "Expected ':' but found '%a'" Lexer.pp_token x in - custom_error pos s) + parser_error pos s) | (x, pos) :: _ -> let s = Format.asprintf "Expected string or identifier but found '%a'" Lexer.pp_token x in - custom_error pos s + parser_error pos s and parse = function | [] -> Error "Unexpected end of input" - | [ (Lexer.EOF, pos) ] -> custom_error pos "Unexpected end of input" + | [ (Lexer.EOF, pos) ] -> parser_error pos "Unexpected end of input" | (token, pos) :: xs -> ( match token with | TRUE -> Ok (Ast.Bool true, xs) @@ -82,7 +82,7 @@ and parse = function (Ast.Assoc (List.rev a), xs) | x -> let s = Format.asprintf "Unexpected token: %a" Lexer.pp_token x in - custom_error pos s) + parser_error pos s) let parse_from_lexbuf ?(fname = "") ?(lnum = 1) lexbuffer = Sedlexing.set_filename lexbuffer fname;