-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ml
318 lines (305 loc) · 7.35 KB
/
parser.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
(* Parser *)
(* Copyright (C)2005-2006 Berke Durak *)
(* Released under the GNU General Public License, version 2 or later. *)
(* Hand-crafted on 2006-05-24 *)
open Bool;;
open Target;;
open Util;;
exception Error of string;;
(*** comment_killer_line_counter *)
let comment_killer_line_counter lc s =
let rec state0 = parser
| [< 'c; s >] ->
(match c with
| '#' -> state1 s
| '\n' -> incr lc; [< 'c; state0 s >]
| _ -> [< 'c; state0 s >])
| [< >] -> [< >]
and state1 = parser
| [< 'c; s >] ->
if c = '\n' then
begin
incr lc;
[< '' '; state0 s >]
end
else
state1 s
| [< >] -> [< >]
in
state0 s
;;
(* ***)
type token =
| AND
| CHECK
| COMMA
| COMMAND
| CONSECUTIVE
| ECHO
| EXECUTE
| FAILURE
| FAILURES
| FALSE
| HAS
| INT of int
| MAIL
| MONITOR
| MORE
| NOT
| ON
| OR
| OUTPUTS
| REGEXP of string * char list
| STRING of string
| SUBJECT
| THAN
| THANKS
| THAT
| THEN
| TO
| TRUE
| URL
| WITH
| LPAREN
| RPAREN
;;
let string_of_token = function
| AND -> "AND"
| CHECK -> "CHECK"
| COMMA -> "COMMA"
| COMMAND -> "COMMAND"
| CONSECUTIVE -> "CONSECUTIVE"
| ECHO -> "ECHO"
| EXECUTE -> "EXECUTE"
| FAILURE -> "FAILURE"
| FAILURES -> "FAILURES"
| FALSE -> "FALSE"
| HAS -> "HAS"
| INT i -> "INT("^(string_of_int i)^")"
| MAIL -> "MAIL"
| MONITOR -> "MONITOR"
| MORE -> "MORE"
| NOT -> "NOT"
| ON -> "ON"
| OR -> "OR"
| OUTPUTS -> "OUTPUTS"
| REGEXP(r,o) -> "REGEXP("^r^",["^(String.concat ";" (List.map (String.make 1) o))^"])"
| STRING s -> "STRING("^s^")"
| SUBJECT -> "SUBJECT"
| THAN -> "THAN"
| THANKS -> "THANKS"
| THAT -> "THAT"
| THEN -> "THEN"
| TO -> "TO"
| TRUE -> "TRUE"
| URL -> "URL"
| WITH -> "WITH"
| LPAREN -> "LPAREN"
| RPAREN -> "RPAREN"
;;
let keywords_list =
[
"and", AND;
"check", CHECK;
"comma", COMMA;
"command", COMMAND;
"consecutive", CONSECUTIVE;
"echo", ECHO;
"execute", EXECUTE;
"failure", FAILURE;
"failures", FAILURES;
"false", FALSE;
"has", HAS;
"mail", MAIL;
"monitor", MONITOR;
"more", MORE;
"not", NOT;
"on", ON;
"or", OR;
"outputs", OUTPUTS;
"subject", SUBJECT;
"than", THAN;
"thanks", THANKS;
"that", THAT;
"then", THEN;
"to", TO;
"true", TRUE;
"url", URL;
"with", WITH;
]
;;
(*** lex *)
let lex s =
let b = Buffer.create 16 in
let rec lex0 = parser
| [< ''/'; s >] -> lex_regexp0 s
| [< ''('; s >] -> [< 'LPAREN; lex0 s >]
| [< '')'; s >] -> [< 'RPAREN; lex0 s >]
| [< '','; s >] -> [< 'COMMA; lex0 s >]
| [< ''"'; s >] -> lex_string0 s
| [< '( '0'..'9' as c); s >] -> lex_int0 ((Char.code c) land 15) s
| [< ' ('a'..'z' as c); s >] ->
Buffer.add_char b c;
lex_keyword0 s
| [< ' (' '|'\t'|'\n'|'\r'); s >] -> lex0 s
| [< s >] -> Stream.empty s; [< >]
and lex_escapable = parser
| [< ''\\'; s >] ->
begin
match s with parser
| [< ''t' >] -> '\t'
| [< ''n' >] -> '\n'
| [< ''r' >] -> '\r'
| [< ''\\' >] -> '\\'
| [< ''/' >] -> '/'
| [< ''"' >] -> '"'
end
| [< 'c; s >] -> c
and lex_string0 = parser
| [< ''"'; s >] ->
let w = Buffer.contents b in
Buffer.clear b;
[< '(STRING w); lex0 s >]
| [< c = lex_escapable; s >] ->
Buffer.add_char b c;
lex_string0 s
and lex_regexp0 = parser
| [< ''/'; s >] ->
let w = Buffer.contents b in
Buffer.clear b;
lex_regexp1 w [] s
| [< c = lex_escapable; s >] ->
Buffer.add_char b c;
lex_regexp0 s
and lex_regexp1 w o = parser
| [< ' ('a'..'z' as c); s >] -> lex_regexp1 w (c :: o) s
| [< s >] -> [< '(REGEXP(w,o)); lex0 s >]
and lex_keyword0 = parser
| [< ' ('a'..'z' as c); s >] ->
Buffer.add_char b c;
lex_keyword0 s
| [< s >] ->
let w = Buffer.contents b in
Buffer.clear b;
match
try
Some(List.assoc w keywords_list)
with
| Not_found -> None
with
| Some k -> [< 'k; lex0 s >]
| None -> raise (Error(sf "Unknown token %S" w))
and lex_int0 q = parser
| [< '( '0'..'9' as c); s >] -> lex_int0 (10 * q + ((Char.code c) land 15)) s
| [< s >] -> [< '(INT q); lex0 s >]
in
lex0 s
;;
(* ***)
let parse s =
let rec do_program = parser
| [< e = do_entry; s >] -> e :: do_program s
| [< >] -> []
and do_entry = parser
| [< 'TO; 'MONITOR; '(STRING w); cl = do_checklist; a = do_actions; 'THANKS >] -> (w, cl, a)
and do_checklist = parser
| [< ck = do_check; s >] ->
match s with parser
| [< 'THEN; s >] -> ck :: (do_checklist s)
| [< >] -> [ck]
and do_check = parser
| [< 'CHECK; 'THAT; src = do_source; '(HAS|OUTPUTS); x = do_expression >] -> (src, x)
and do_source = parser
| [< 'URL; '(STRING w) >] -> Url w
| [< 'COMMAND; '(STRING w) >] -> Cmd w
and do_actions = parser
| [< fc = do_failure_condition; al = do_action_list >] -> (fc, al)
and do_failure_condition = parser
| [< 'ON; s >] ->
match s with parser
| [< 'MORE; 'THAN; '(INT i); 'CONSECUTIVE; '(FAILURE|FAILURES) >] -> i
| [< 'FAILURE >] -> 0
and do_action_list = parser
| [< a = do_action; s >] ->
match s with parser
| [< 'THEN; s >] -> a :: (do_action_list s)
| [< >] -> [a]
and do_action ?(subject="No subject") = parser
| [< 'WITH; 'SUBJECT; '(STRING subject); s >] -> do_action ~subject s
| [< 'MAIL; rl = do_recipient_list >] -> Mail(subject, rl)
| [< 'ECHO; '(STRING w) >] -> Echo w
| [< 'EXECUTE; '(STRING w) >] -> Exec w
and do_recipient_list = parser
| [< '(STRING u); s >] ->
match s with parser
| [< 'AND; '(STRING v) >] -> [u;v]
| [< 'COMMA; rl = do_recipient_list >] -> u :: rl
| [< >] -> [u]
and do_expression x = parse_s x
and atomizer continuation = parser
| [< 'NOT; rest >] ->
atomizer
begin fun x rest ->
continuation (Not x) rest
end
rest
| [< 'REGEXP(r,o); rest >] ->
let flags =
List.map
begin function
| 'i' -> `CASELESS
| c -> raise (Error(sf "Bad regexp option char %C" c))
end
o
in
let re = Pcre.regexp ~study:true ~flags r in
let a = Atom(r,re) in
continuation a rest
| [< 'LPAREN; y = parse_s; 'RPAREN; rest >] -> continuation y rest
and parse_s1 x = parser
| [< 'OR; y = parse_s >] -> Or[x;y]
| [< 'AND; rest >] -> parse_t x rest
| [< >] -> x
and parse_t1 x y = parser
| [< 'OR; z = parse_s >] -> Or[And[x;y]; z]
| [< 'AND; rest >] -> parse_t (And[x;y]) rest
| [< >] -> And[x;y]
and parse_s x = atomizer parse_s1 x
and parse_t x y = atomizer (parse_t1 x) y
in
do_program s
;;
exception At_line of int * exn;;
let dump line x =
Stream.iter (fun a -> Printf.printf "%d %s\n" (1 + !line) (string_of_token a)) x
;;
let dump fn =
let ic = open_in fn in
let s0 = Stream.of_channel ic in
let line = ref 0 in
let s1 = comment_killer_line_counter line s0 in
try
let s2 = lex s1 in
dump line s2;
close_in ic
with
| x ->
close_in ic;
raise (At_line(!line, x))
;;
let load fn =
let ic = open_in fn in
let s0 = Stream.of_channel ic in
let line = ref 0 in
let s1 = comment_killer_line_counter line s0 in
try
let s2 = lex s1 in
let p = parse s2 in
close_in ic;
p
with
| x ->
close_in ic;
raise (At_line(!line, x))
;;
let parse x = parse (lex (Stream.of_string x));;