| 1 |
(* |
| 2 |
* This file is part of Coccinelle, lincensed 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 |
(* Lexer/parser for spgen config file *) |
| 8 |
|
| 9 |
{ |
| 10 |
|
| 11 |
(* ------------------------------------------------------------------------- *) |
| 12 |
(* TYPES *) |
| 13 |
|
| 14 |
(* the config syntax has pretty low complexity and is very lenient, so we just |
| 15 |
* interpret the tokens directly and skip the lexical analysis. *) |
| 16 |
|
| 17 |
type attribute = |
| 18 |
| Org of string * string list | Report of string * string list |
| 19 |
|
| 20 |
type part = |
| 21 |
| Description of string |
| 22 |
| Limitations of string list |
| 23 |
| Keywords of string |
| 24 |
| Confidence of string |
| 25 |
| Comments of string |
| 26 |
| Options of string |
| 27 |
| Authors of string list (* author, affiliation, license in one *) |
| 28 |
| Url of string |
| 29 |
| Rule of (string * string option) * attribute list |
| 30 |
|
| 31 |
(* the table is for line numbers to produce more meaningful error messages *) |
| 32 |
let table = ref (Array.make 0 (0,0)) |
| 33 |
exception Eof |
| 34 |
|
| 35 |
let get_start_line lexbuf = |
| 36 |
match !table.(Lexing.lexeme_start lexbuf) with (l,_) -> l |
| 37 |
|
| 38 |
let get_column lexbuf = |
| 39 |
match !table.(Lexing.lexeme_start lexbuf) with (_,c) -> c |
| 40 |
|
| 41 |
let get_position_str lexbuf = |
| 42 |
"around line " ^ (string_of_int (get_start_line lexbuf)) |
| 43 |
^ ", column " ^ (string_of_int (get_column lexbuf)) |
| 44 |
|
| 45 |
let error prob lexbuf = |
| 46 |
let pos = get_position_str lexbuf in failwith ("Config error: " ^ prob ^ pos) |
| 47 |
|
| 48 |
let illegal = error "Illegal syntax " |
| 49 |
|
| 50 |
let split_list delim = Str.split (Str.regexp (" *"^delim^" *")) |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
(* ------------------------------------------------------------------------- *) |
| 55 |
(* LEXING RULES *) |
| 56 |
|
| 57 |
let sp = [' ' '\t'] |
| 58 |
let ws = ['\n' '\r' '\012' '\013' ' ' '\t']* |
| 59 |
let notws = [^ '\n' '\r' '\012' '\013' ' ' '\t']* |
| 60 |
let notnl = [^ '\n' '\r' '\012' '\013']* |
| 61 |
let equal = (sp* "=" sp*) |
| 62 |
let colon = (sp* ":" sp*) |
| 63 |
let percent = (sp* "%" sp*) |
| 64 |
let letter = ['A'-'Z' 'a'-'z' '_'] |
| 65 |
let number = ['0'-'9']* |
| 66 |
let cname = letter (letter | number)* |
| 67 |
|
| 68 |
rule token = parse |
| 69 |
| ("description"|"d") equal (notnl as d) { Description d } |
| 70 |
| ("keywords" |"k") equal (notnl as v) { Keywords v } |
| 71 |
| ("confidence" |"c") equal (notws as c) { Confidence c } |
| 72 |
| ("comments" |"m") equal (notnl as m) { Comments m } |
| 73 |
| ("options" |"o") equal (notnl as o) { Options o } |
| 74 |
| ("url" |"u") equal (notnl as u) { Url u } |
| 75 |
| ("limitations"|"l") equal (notnl as l) { Limitations (split_list "|" l) } |
| 76 |
| ("author"|"authors"|"a") equal (notnl as a) { Authors (split_list "|" a) } |
| 77 |
|
| 78 |
(* for naming unnamed rules. e.g. 8:name = ... for the rule on line 8. *) |
| 79 |
| (number as oldrule) colon (cname as newrule) equal { |
| 80 |
Rule ((oldrule, Some newrule), cocci_rule lexbuf) |
| 81 |
} |
| 82 |
|
| 83 |
(* standard rules with org and report messages *) |
| 84 |
| (cname as rulenm) equal { |
| 85 |
Rule ((rulenm, None), cocci_rule lexbuf) |
| 86 |
} |
| 87 |
|
| 88 |
(* comments and whitespace are skipped *) |
| 89 |
| ws | "//" [^ '\n']* { token lexbuf } |
| 90 |
| "/*" { comment lexbuf } |
| 91 |
| eof { raise Eof} |
| 92 |
| _ { illegal lexbuf } |
| 93 |
|
| 94 |
and comment = parse |
| 95 |
| "*/" {token lexbuf} |
| 96 |
| eof { error "unclosed comment, needs \"*/\"! " lexbuf} |
| 97 |
| _ {comment lexbuf } |
| 98 |
|
| 99 |
and cocci_rule = parse |
| 100 |
| ws* "org" colon { |
| 101 |
let (a,b) = format_string lexbuf in Org (a,b) :: (cocci_rule lexbuf) |
| 102 |
} |
| 103 |
| ws* "report" colon { |
| 104 |
let (a,b) = format_string lexbuf in Report (a,b) :: (cocci_rule lexbuf) |
| 105 |
} |
| 106 |
| _ { [] } |
| 107 |
|
| 108 |
(* possibly formatted org and report messages *) |
| 109 |
and format_string = parse |
| 110 |
| "\"" (notnl as msg) "\"" percent "(" (notnl as lst) ")" { |
| 111 |
(msg, split_list "," lst) |
| 112 |
} |
| 113 |
| "\"" (notnl as msg) "\"" percent (notnl as lst) { (msg,[lst]) } |
| 114 |
| "\"" (notnl as msg) "\"" { (msg, []) } |
| 115 |
| _ { illegal lexbuf } |