Debian Codegraph @

tools/spgen/source/spgen_config.ml

raw
1 (*
2 * This file is part of Coccinelle, licensed under the terms of the GPL v2.
3 * See copyright.txt in the Coccinelle source code for more information.
4 * The Coccinelle source code can be obtained at http://coccinelle.lip6.fr
5 *)
6
7 module UI = User_input
8 module Lex = Spgen_lexer
9
10 (* ------------------------------------------------------------------------- *)
11
12 (* Read config files for user-specified options *)
13
14 (* ------------------------------------------------------------------------- *)
15 (* LEXER FUNCTIONS *)
16
17 (* function to be applied continuously on the input coccinelle script *)
18 let lex_config lexbuf =
19 let rec aux ((d,l,k,c,m,o,a,u,r) as res) =
20 try
21 let result = Lex.token lexbuf in
22 match result with
23 | Lex.Description d -> aux (d,l,k,c,m,o,a,u,r)
24 | Lex.Limitations l -> aux (d,l,k,c,m,o,a,u,r)
25 | Lex.Keywords k -> aux (d,l,k,c,m,o,a,u,r)
26 | Lex.Confidence c -> aux (d,l,k,c,m,o,a,u,r)
27 | Lex.Comments m -> aux (d,l,k,c,m,o,a,u,r)
28 | Lex.Options o -> aux (d,l,k,c,m,o,a,u,r)
29 | Lex.Authors a -> aux (d,l,k,c,m,o,a,u,r)
30 | Lex.Url u -> aux (d,l,k,c,m,o,a,u,r)
31 | Lex.Rule (rn,attr) -> aux (d,l,k,c,m,o,a,u, (rn, attr) :: r)
32 with Lex.Eof -> res in
33 aux ("",[],"","","","",[],"",[])
34
35 (* open the input coccinelle script and lex/parse it.
36 * the table maps the absolute character position to its line and column number
37 *)
38 let parse filename =
39 let _ = Lex.table := Common.full_charpos_to_pos filename in
40 Common.with_open_infile filename
41 (fun channel ->
42 let lexbuf = Lexing.from_channel channel in
43 lex_config lexbuf)
44
45
46 (* ------------------------------------------------------------------------- *)
47 (* PARSER FUNCTIONS *)
48
49 (* parse org/report messages and associated metavars.
50 * if several org/report messages defined, take the last one
51 *)
52 let parse_msgs attributes =
53 let read ((om,ov),(rm,rv)) = function
54 | Lex.Org (om,ov) -> ((om,ov), (rm,rv))
55 | Lex.Report (rm,rv) -> ((om,ov), (rm,rv)) in
56 List.fold_left read (("",[]), ("",[])) attributes
57
58 (* add the rule to the User_input.t. *)
59 let add_rule rule_check t ((oldrnm,newnm),a) =
60 let oldrnm = rule_check oldrnm in
61 let (org,report) =
62 match parse_msgs a with
63 | ("",_),("",_) ->
64 failwith ("Config error: must specify at least org or report message.")
65 | ("",_),mgv | mgv,("",_) ->
66 (try
67 UI.check_format_string mgv;
68 (mgv,mgv)
69 with Failure msg ->
70 failwith ("Config error: " ^ msg))
71 | org, rep ->
72 try
73 UI.check_format_string org;
74 UI.check_format_string rep;
75 (org, rep)
76 with Failure msg ->
77 failwith ("Config error: " ^ msg) in
78 let rule_name = match newnm with | Some nm -> nm | None -> oldrnm in
79 let rule = UI.Rule.make ~rule_name ~org ~report in
80 UI.add_rule ~rule_name:oldrnm rule t
81
82 (*rule list is a list of (rulename, attribute list) *)
83 let add_rules rule_exists rule_list t =
84 List.fold_left (fun a b -> add_rule rule_exists a b) t rule_list
85
86 (* expanded constructor for User_input.t *)
87 let make desc limit keys conf comments options authors url =
88 let t =
89 try
90 UI.make ~description:desc ~confidence:(UI.Confidence.from_string conf)
91 with UI.Confidence.Not_confidence s ->
92 failwith
93 ("Config error: Confidence must be low, moderate, or high, not "^s) in
94 let t = UI.set_limits limit t in
95 let t = UI.set_keys keys t in
96 let t = UI.set_comments comments t in
97 let t = UI.set_options options t in
98 let t = UI.set_authors authors t in
99 UI.set_url url t
100
101 (* ------------------------------------------------------------------------- *)
102 (* ENTRY POINT *)
103
104 let parse_local ~rule_names ~config_name =
105 let rule_check x =
106 let x =
107 if Globals.starts_with_digit x then "rule starting on line "^ x else x in
108 if (List.mem x rule_names) then x
109 else failwith ("Config error: no */+/- rule called \"" ^ x ^ "\".") in
110 let (d,l,k,c,m,o,a,u,r) = parse config_name in
111 let t = make d l k c m o a u in
112 let t = add_rules rule_check r t in
113 t
114
115 let parse_default = UI.make
116 ~description:"No description."
117 ~confidence:UI.Confidence.Moderate
118
119 (* TODO: implement *)
120 let parse_global ~config_name =
121 (None, None, "rule_", "j", "found a match around here ...", 80)