| 1 |
{ |
| 2 |
(* raise when the format is unrecognized *) |
| 3 |
exception Unknown_format |
| 4 |
|
| 5 |
type lines = |
| 6 |
| Single of int |
| 7 |
| Range of int * int |
| 8 |
|
| 9 |
type code = |
| 10 |
{ code : int |
| 11 |
; name : string |
| 12 |
} |
| 13 |
|
| 14 |
type source = |
| 15 |
| Code of code |
| 16 |
| Alert of string |
| 17 |
|
| 18 |
type severity = |
| 19 |
| Error of source option |
| 20 |
| Warning of code |
| 21 |
| Alert of { name : string ; source : string } |
| 22 |
|
| 23 |
type loc = |
| 24 |
{ chars : (int * int) option |
| 25 |
; lines : lines |
| 26 |
; path : string |
| 27 |
} |
| 28 |
|
| 29 |
type line = { indent : int ; contents : string } |
| 30 |
|
| 31 |
type token = |
| 32 |
| Loc of { indent : int ; loc : loc ; message : string } |
| 33 |
| Line of line |
| 34 |
| Eof |
| 35 |
|
| 36 |
let parse_range s = |
| 37 |
match String.split_on_char '-' s with |
| 38 |
| [x; y] -> (int_of_string x, int_of_string y) |
| 39 |
| _ -> assert false |
| 40 |
} |
| 41 |
|
| 42 |
let blank = [' ' '\t' '\r']* |
| 43 |
|
| 44 |
let newline = '\n' |
| 45 |
|
| 46 |
let quoted_string = '"' [^ '"']+ '"' |
| 47 |
|
| 48 |
let digits = ['0' - '9']+ |
| 49 |
|
| 50 |
let range = digits "-" digits |
| 51 |
|
| 52 |
let any = _ * |
| 53 |
|
| 54 |
let alert_name = ['a' - 'z'] ['A' - 'Z' 'a' - 'z' '0' - '9' '_']* |
| 55 |
|
| 56 |
rule skip_excerpt_head = parse |
| 57 |
| blank digits " | " [^ '\n']* [ '.' ]* "\n"? |
| 58 |
{ `Continue } |
| 59 |
| eof { `Stop } |
| 60 |
| "" { `Stop } |
| 61 |
|
| 62 |
and skip_excerpt_tail = parse |
| 63 |
| "..." '\r'? '\n'? { `Continue } |
| 64 |
| blank digits " | " [^ '\n']* "\n"? |
| 65 |
{ `Continue } |
| 66 |
| blank '^'+ blank "\n"? |
| 67 |
{ `Continue } |
| 68 |
| eof { `Stop } |
| 69 |
| "" { `Stop } |
| 70 |
|
| 71 |
and severity = parse |
| 72 |
| "Error:" |
| 73 |
(blank any as rest) |
| 74 |
{ Some (Error None, rest) } |
| 75 |
| "Warning" blank (digits as code) blank "[" ([^ ']']+ as name) "]:" |
| 76 |
(blank any as rest) |
| 77 |
{ Some (Warning { code = int_of_string code ; name }, rest) |
| 78 |
} |
| 79 |
| "Error" blank |
| 80 |
"(warning" blank (digits as code) blank "[" ([^ ']']+ as name) "]):" |
| 81 |
(blank any as rest) |
| 82 |
{ Some (Error (Some (Code { code = int_of_string code ; name })), rest) |
| 83 |
} |
| 84 |
| "Alert " blank (alert_name as name) ":" blank (any as source) |
| 85 |
{ Some (Alert { name ; source }, "") |
| 86 |
} |
| 87 |
| (("Error" | "Warning") as kind) " (alert " ([^ ')']+ as alert) "):" |
| 88 |
(blank any as rest) |
| 89 |
{ let res = |
| 90 |
match kind with |
| 91 |
| "Error" -> Error (Some (Alert alert)) |
| 92 |
| "Warning" -> Alert { name = alert ; source = "" } |
| 93 |
| _ -> assert false |
| 94 |
in |
| 95 |
Some (res, rest) |
| 96 |
} |
| 97 |
| "" { None } |
| 98 |
|
| 99 |
and line = parse |
| 100 |
| (blank as prefix) ([^ '\n']* as contents) blank newline? |
| 101 |
{ Line { indent = String.length prefix ; contents } |
| 102 |
} |
| 103 |
| eof { Eof } |
| 104 |
|
| 105 |
and token = parse |
| 106 |
| (blank as indent) "File \"" ([^ '"']* as path) "\", " blank |
| 107 |
(("line " (digits as line) | "lines " (range as lines))) |
| 108 |
("," blank "characters" blank (range as chars))? |
| 109 |
":" blank ([^ '\n']* as message) newline? |
| 110 |
{ let lines = |
| 111 |
match line, lines with |
| 112 |
| Some line, None -> Single (int_of_string line) |
| 113 |
| None, Some range -> |
| 114 |
let start, finish = (parse_range range) in |
| 115 |
Range (start, finish) |
| 116 |
| None, None |
| 117 |
| Some _, Some _ -> assert false |
| 118 |
in |
| 119 |
let chars = |
| 120 |
match chars with |
| 121 |
| None -> None |
| 122 |
| Some chars -> |
| 123 |
let start, finish = parse_range chars in |
| 124 |
Some (start, finish) |
| 125 |
in |
| 126 |
let indent = String.length indent in |
| 127 |
let loc = { lines ; path ; chars } in |
| 128 |
Loc { loc ; indent ; message } |
| 129 |
} |
| 130 |
| eof { Eof } |
| 131 |
| "" { line lexbuf } |