Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 52 lines (46 sloc) 1.883 kB
8d70150 @avsm Add an ABNF parser and a helper command-line utility to help visualize
authored
1 (** Copyright (c) 2008,2009 Anil Madhavapeddy <anil@recoil.org>
2 ** See the COPYING file included in this distribution for licensing details *)
3
4 open Abnf_syntaxtree
5 open Printf
6
7 exception Rule_error of string
8
9 let generate_rules rule_defs =
10 let s_rule_hash = Hashtbl.create 1 in
11 List.iter (fun rule_def ->
12 (* Put all the rules into the syntax hash table *)
13 if Hashtbl.mem s_rule_hash rule_def.s_name then
14 raise (Rule_error ("Duplicate rule: " ^ rule_def.s_name));
15 Hashtbl.add s_rule_hash rule_def.s_name rule_def.s_rule;
16 ) rule_defs;
17 s_rule_hash
18
19 (* Go through the rules hash resolving references and making sure they are valid *)
20 let rec check_rule rules_hash rule_name =
21 let cfn x = check_rule rules_hash rule_name x in
22 function
23 |S_terminal _ -> ()
24 |S_string _ -> ()
25 |S_concat (r1, r2) -> cfn r1; cfn r2
26 |S_alt (r1, r2) -> cfn r1; cfn r2
31821af @samoht Replace S_seq by S_bracket which is a less confusing name
samoht authored
27 |S_bracket r -> cfn r
8d70150 @avsm Add an ABNF parser and a helper command-line utility to help visualize
authored
28 |S_any_except (r1, r2) -> cfn r1; cfn r2
29 |S_reference r ->
30 if not (Hashtbl.mem rules_hash r) then
31 raise (Rule_error (sprintf "Unknown reference: %s in rule %s" r rule_name))
ddd4629 @avsm add support for HTTP ABNF constructs (mainly the list alternator and …
authored
32 |S_repetition (min, max, r)
33 |S_element_list (min, max, r) -> begin
8d70150 @avsm Add an ABNF parser and a helper command-line utility to help visualize
authored
34 cfn r;
35 match min, max with
36 |Some min, Some max ->
37 if min > max then raise (Rule_error ("min > max in " ^ rule_name));
38 if max < 0 then raise (Rule_error ("max < 0 in " ^ rule_name));
39 |Some min, None ->
40 if min < 0 then raise (Rule_error ("min < 0 in " ^ rule_name));
41 |None, Some max ->
42 if max < 0 then raise (Rule_error ("max < 0 in " ^ rule_name));
43 |None, None -> ()
44 end
45 |S_hex_range (f,t) ->
46 (* XXX put checks in *) ()
47
48 let check rules_hash =
49 Hashtbl.iter (fun rule_name rule ->
50 check_rule rules_hash rule_name rule
51 ) rules_hash
Something went wrong with that request. Please try again.