Skip to content

Commit

Permalink
refactor: remove polymorphic variants in perl re's (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrinberg authored Oct 11, 2024
1 parent 6e3f600 commit 83d0f60
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions lib/perl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ let char_of_int x =
| exception _ -> raise Parse_error
;;

type elem =
| Char of char
| Set of Ast.t

let parse multiline dollar_endonly dotall ungreedy s =
let buf = Parse_buffer.create s in
let accept = Parse_buffer.accept buf in
Expand Down Expand Up @@ -245,17 +249,17 @@ let parse multiline dollar_endonly dotall ungreedy s =
then s
else (
match char () with
| `Set st -> bracket (st :: s)
| `Char c ->
| Set st -> bracket (st :: s)
| Char c ->
if accept '-'
then
if accept ']'
then Re.char c :: Re.char '-' :: s
else
bracket
(match char () with
| `Char c' -> Re.rg c c' :: s
| `Set st' -> Re.char c :: Re.char '-' :: st' :: s)
| Char c' -> Re.rg c c' :: s
| Set st' -> Re.char c :: Re.char '-' :: st' :: s)
else bracket (Re.char c :: s))
and char () =
if eos () then raise Parse_error;
Expand All @@ -264,16 +268,16 @@ let parse multiline dollar_endonly dotall ungreedy s =
then (
if accept '=' then raise Not_supported;
match Posix_class.parse buf with
| Some set -> `Set set
| Some set -> Set set
| None ->
if accept '.'
then (
if eos () then raise Parse_error;
let c = get () in
if not (accept '.') then raise Not_supported;
if not (accept ']') then raise Parse_error;
`Char c)
else `Char c)
Char c)
else Char c)
else if c = '\\'
then (
if eos () then raise Parse_error;
Expand All @@ -282,20 +286,20 @@ let parse multiline dollar_endonly dotall ungreedy s =
\127, ...
*)
match c with
| 'b' -> `Char '\008'
| 'n' -> `Char '\n' (*XXX*)
| 'r' -> `Char '\r' (*XXX*)
| 't' -> `Char '\t' (*XXX*)
| 'w' -> `Set (Re.alt [ Re.alnum; Re.char '_' ])
| 'W' -> `Set (Re.compl [ Re.alnum; Re.char '_' ])
| 's' -> `Set Re.space
| 'S' -> `Set (Re.compl [ Re.space ])
| 'd' -> `Set Re.digit
| 'D' -> `Set (Re.compl [ Re.digit ])
| 'b' -> Char '\008'
| 'n' -> Char '\n' (*XXX*)
| 'r' -> Char '\r' (*XXX*)
| 't' -> Char '\t' (*XXX*)
| 'w' -> Set (Re.alt [ Re.alnum; Re.char '_' ])
| 'W' -> Set (Re.compl [ Re.alnum; Re.char '_' ])
| 's' -> Set Re.space
| 'S' -> Set (Re.compl [ Re.space ])
| 'd' -> Set Re.digit
| 'D' -> Set (Re.compl [ Re.digit ])
| 'a' .. 'z' | 'A' .. 'Z' -> raise Parse_error
| '0' .. '9' -> raise Not_supported
| _ -> `Char c)
else `Char c
| _ -> Char c)
else Char c
and comment () =
if eos () then raise Parse_error;
if accept ')'
Expand Down

0 comments on commit 83d0f60

Please sign in to comment.