-
Notifications
You must be signed in to change notification settings - Fork 2
/
message.ml
77 lines (66 loc) · 2.04 KB
/
message.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(*
* Copyright (c) 2015 Alexander Færøy. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*)
type t =
{
prefix : string option;
command : string;
arguments : string list;
}
let prefix { prefix; _ } =
prefix
let command { command; _ } =
command
let arguments { arguments; _ } =
arguments
exception ParseError of string
let rec parse_one_argument s l =
match (Utils.prefix s ':') with
| true ->
(String.sub s 1 (String.length s - 1)) :: l
| false ->
match (String.contains s ' ') with
| true ->
let argument, rest = Utils.split s ' ' in
parse_one_argument rest (argument :: l)
| false ->
s :: l
let parse_arguments s =
List.rev (parse_one_argument s [])
let parse_command prefix s =
match (String.contains s ' ') with
| true ->
let command, rest = Utils.split s ' ' in
{
prefix = prefix;
command = String.uppercase command;
arguments = parse_arguments rest;
}
| false ->
{
prefix = prefix;
command = String.uppercase s;
arguments = [];
}
let parse_prefix s =
match (String.contains s ' ') with
| true ->
let prefix, rest = Utils.split s ' ' in
parse_command (Some prefix) rest
| false ->
raise (ParseError s)
let parse s =
try
match (Utils.prefix s ':') with
| true -> parse_prefix (String.sub s 1 ((String.length s) - 1))
| false -> parse_command None s
with _ ->
raise (ParseError s)
let to_string { prefix; command; arguments; } =
let p =
match prefix with
| Some v -> (String.concat "" ["'"; v; "'"])
| None -> "N/A" in
Printf.sprintf "Prefix: %s, Command: '%s', Arguments: '%s'" p command (String.concat "', '" arguments)