make sep/delim customisable

This commit is contained in:
atagen 2024-12-05 23:40:30 +11:00
parent 6d98e1639b
commit c5c240ca2e
8 changed files with 60 additions and 26 deletions

View file

@ -2,20 +2,20 @@ open Types
open Angstrom
open Parsers
let is_sep = function '\x20' | '\x09' -> true | _ -> false
let is_delim = function '\x0a' | '\x0d' -> true | _ -> false
let is_text c = not (is_sep c || is_delim c || c = '\x1b')
let sep = take_while is_sep >>| fun s -> Separator s
let delim = take_while is_delim >>| fun s -> Delimiter s
let text = take_while is_text >>| fun s -> Text s
let is_sep p c = Array.mem c p.separator
let is_delim p c = Array.mem c p.delimiter
let is_text p c = not (is_sep p c || is_delim p c || c = '\x1b')
let sep p = take_while (is_sep p) >>| fun s -> Separator s
let delim p = take_while (is_delim p) >>| fun s -> Delimiter s
let text p = take_while (is_text p) >>| fun s -> Text s
let ansi = Ansi.ansi_parse
let culr_parse =
let culr_parse p =
peek_char_fail >>= function
| n when is_sep n -> sep
| n when is_delim n -> delim
| n when is_sep p n -> sep p
| n when is_delim p n -> delim p
| n when n = '\x1b' -> ansi
| _n -> text
| _n -> text p
let conv_rgb = function [ r; g; b ] -> Types.RGB (r, g, b) | _ -> assert false
let hex_digit = take 2 >>| fun s -> int_of_string ("0x" ^ s)
@ -37,22 +37,23 @@ let parse_env_order s =
|> unpack |> Array.of_list
let%test "sep_parse" =
parse_string ~consume:Consume.All sep " " |> function
parse_string ~consume:Consume.All (sep (create_parser ())) " " |> function
| Ok (Separator " ") -> true
| _ -> false
let%test "delim_parse" =
parse_string ~consume:Consume.All delim "\n\n\n" |> function
parse_string ~consume:Consume.All (delim (create_parser ())) "\n\n\n" |> function
| Ok (Delimiter "\n\n\n") -> true
| _ -> false
let%test "text_parse" =
parse_string ~consume:Consume.All text "okokyeahok" |> function
parse_string ~consume:Consume.All (text (create_parser ())) "okokyeahok" |> function
| Ok (Text "okokyeahok") -> true
| _ -> false
let%test "culr_parse" =
parse_string ~consume:Consume.All (many1 culr_parse)
parse_string ~consume:Consume.All (many1 (culr_parse (create_parser ())))
"sometext \n\n\n \x1b[38;2;2m"
|> function
| Ok