Skip to content

Commit

Permalink
_
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <[email protected]>
  • Loading branch information
rgrinberg committed Nov 26, 2023
1 parent 186a3e3 commit 55e80af
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions ocaml-lsp-server/src/prefix_parser.ml
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
open Re
open Import

(*Regex based parser*)
let white_space = set "\n\t "
include struct
open Re

let name_char =
Re.alt [ rg 'a' 'z'; rg 'A' 'Z'; rg '0' '9'; char '_'; char '\'' ]
(* Regex based parser *)
let white_space = set "\n\t "

let name_with_dot =
Re.seq [ name_char; white_space |> rep; char '.'; white_space |> rep ]
let name_char =
Re.alt [ rg 'a' 'z'; rg 'A' 'Z'; rg '0' '9'; char '_'; char '\'' ]

let core_operator_str = {|$&*+-/=>@^||}
let name_with_dot =
Re.seq [ name_char; white_space |> rep; char '.'; white_space |> rep ]

let operator = core_operator_str ^ {|~!?%<:.|}
let core_operator_str = {|$&*+-/=>@^||}

let infix = set (operator ^ "#")
let operator = core_operator_str ^ {|~!?%<:.|}

let name_or_label =
compile
(seq
[ alt [ set "~?``"; str "let%"; str "and%" ] |> opt
; alt [ name_char; name_with_dot ] |> rep1
; stop
])
let infix = set (operator ^ "#")

(** matches let%lwt and let* style expressions. See
here:https://v2.ocaml.org/manual/bindingops.html *)
let monadic_bind =
compile
(seq
[ alt [ str "let"; str "and" ]
; alt [ infix |> rep1; seq [ name_char |> rep1; char '%' ] ]
; stop
])
let name_or_label =
compile
(seq
[ alt [ set "~?``"; str "let%"; str "and%" ] |> opt
; alt [ name_char; name_with_dot ] |> rep1
; stop
])

let infix_operator = compile (seq [ infix |> rep1; stop ])
(** matches let%lwt and let* style expressions. See
here:https://v2.ocaml.org/manual/bindingops.html *)
let monadic_bind =
compile
(seq
[ alt [ str "let"; str "and" ]
; alt [ infix |> rep1; seq [ name_char |> rep1; char '%' ] ]
; stop
])

open Import
let infix_operator = compile (seq [ infix |> rep1; stop ])
end

let try_parse_with_regex ~pos ~len text =
(*Attempt to match each of our possible prefix types, the order is important
Expand All @@ -45,4 +47,4 @@ let try_parse_with_regex ~pos ~len text =
[ name_or_label; monadic_bind; infix_operator ]
~f:(fun regex -> Re.exec_opt ~pos ~len regex text)
in
matched |> Option.map ~f:(fun x -> Group.get x 0)
matched |> Option.map ~f:(fun x -> Re.Group.get x 0)

0 comments on commit 55e80af

Please sign in to comment.