Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Unreleased
---------------------------------

- Drop the `re` dependency from `graphql_parser` (#221)

0.14.0 2022-07-08
---------------------------------

Expand Down
1 change: 0 additions & 1 deletion graphql_parser.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ depends: [
"menhir" {build}
"alcotest" {with-test & >= "0.8.1"}
"fmt"
"re" {>= "1.5.0"}
]

synopsis: "Library for parsing GraphQL queries"
23 changes: 14 additions & 9 deletions graphql_parser/src/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,20 @@ module Pp = struct
let colon : unit Fmt.t = Fmt.(const string ":")

let quote_string s =
let open Re in
s
|> replace_string (compile (char '\\')) ~by:"\\\\"
|> replace_string (compile (char '"')) ~by:"\\\""
|> replace_string (compile (char '\b')) ~by:"\\b"
|> replace_string (compile (char '\012')) ~by:"\\f"
|> replace_string (compile (char '\n')) ~by:"\\n"
|> replace_string (compile (char '\r')) ~by:"\\r"
|> replace_string (compile (char '\t')) ~by:"\\t"
let b = Buffer.create (String.length s) in
String.iter
(fun c ->
match c with
| '\\' -> Buffer.add_string b "\\\\"
| '"' -> Buffer.add_string b "\\\""
| '\b' -> Buffer.add_string b "\\b"
| '\012' -> Buffer.add_string b "\\f"
| '\n' -> Buffer.add_string b "\\n"
| '\r' -> Buffer.add_string b "\\r"
| '\t' -> Buffer.add_string b "\\t"
| c -> Buffer.add_char b c)
s;
Buffer.contents b

let rec pp_value fmt = function
| `Null -> Fmt.string fmt "null"
Expand Down
26 changes: 18 additions & 8 deletions graphql_parser/src/block_string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ let get_block_string_indentation (s : string) : int =
done;
match !common_indent with None -> 0 | Some num -> num

let split_lines (s : string) : string list =
let lines = ref [] in
let b = Buffer.create 32 in
let i = ref 0 in
let n = String.length s in
while !i < n do
( match s.[!i] with
| ('\r' as c) | ('\n' as c) ->
if c == '\r' && try s.[!i + 1] == '\n' with _ -> false then incr i;
lines := Buffer.contents b :: !lines;
Buffer.clear b
| c -> Buffer.add_char b c );
incr i
done;
lines := Buffer.contents b :: !lines;
List.rev !lines

exception NotBlank

let is_blank (s : string) : bool =
Expand All @@ -33,14 +50,7 @@ let is_blank (s : string) : bool =
with NotBlank -> false

let dedent_block_string_value (s : string) : string =
let lines =
(let open Re in
split
(compile
(alt [ seq [ char '\r'; char '\n' ]; alt [ char '\n'; char '\r' ] ]))
s)
|> Array.of_list
in
let lines = split_lines s |> Array.of_list in
let start_line = ref 0 in
while !start_line < Array.length lines && is_blank lines.(!start_line) do
incr start_line
Expand Down
2 changes: 1 addition & 1 deletion graphql_parser/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(library
(name graphql_parser)
(public_name graphql_parser)
(libraries fmt re)
(libraries fmt)
(flags
(:standard -w -30)))