| 1 |
(* |
| 2 |
RE - A regular expression library |
| 3 |
|
| 4 |
Copyright (C) 2001 Jerome Vouillon |
| 5 |
email: [email protected] |
| 6 |
|
| 7 |
This library is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU Lesser General Public |
| 9 |
License as published by the Free Software Foundation, with |
| 10 |
linking exception; either version 2.1 of the License, or (at |
| 11 |
your option) any later version. |
| 12 |
|
| 13 |
This library is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
Lesser General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU Lesser General Public |
| 19 |
License along with this library; if not, write to the Free Software |
| 20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
*) |
| 22 |
|
| 23 |
module Re = Core |
| 24 |
|
| 25 |
exception Parse_error = Parse_buffer.Parse_error |
| 26 |
exception Not_supported |
| 27 |
|
| 28 |
let acc_digits = |
| 29 |
let rec loop base digits acc i = |
| 30 |
match digits with |
| 31 |
| [] -> acc |
| 32 |
| d :: digits -> |
| 33 |
let acc = acc + (d * i) in |
| 34 |
let i = i * i in |
| 35 |
loop base digits acc i |
| 36 |
in |
| 37 |
fun ~base ~digits -> loop base digits 0 1 |
| 38 |
;; |
| 39 |
|
| 40 |
let char_of_int x = |
| 41 |
match char_of_int x with |
| 42 |
| x -> x |
| 43 |
| exception _ -> raise Parse_error |
| 44 |
;; |
| 45 |
|
| 46 |
let parse multiline dollar_endonly dotall ungreedy s = |
| 47 |
let buf = Parse_buffer.create s in |
| 48 |
let accept = Parse_buffer.accept buf in |
| 49 |
let eos () = Parse_buffer.eos buf in |
| 50 |
let test c = Parse_buffer.test buf c in |
| 51 |
let unget () = Parse_buffer.unget buf in |
| 52 |
let get () = Parse_buffer.get buf in |
| 53 |
let greedy_mod r = |
| 54 |
let gr = accept '?' in |
| 55 |
let gr = if ungreedy then not gr else gr in |
| 56 |
if gr then Re.non_greedy r else Re.greedy r |
| 57 |
in |
| 58 |
let rec regexp () = regexp' (branch ()) |
| 59 |
and regexp' left = if accept '|' then regexp' (Re.alt [ left; branch () ]) else left |
| 60 |
and branch () = branch' [] |
| 61 |
and branch' left = |
| 62 |
if eos () || test '|' || test ')' |
| 63 |
then Re.seq (List.rev left) |
| 64 |
else branch' (piece () :: left) |
| 65 |
and in_brace ~f ~init = |
| 66 |
match accept '{' with |
| 67 |
| false -> None |
| 68 |
| true -> |
| 69 |
let rec loop acc = |
| 70 |
if accept '}' |
| 71 |
then acc |
| 72 |
else ( |
| 73 |
let acc = f acc in |
| 74 |
loop acc) |
| 75 |
in |
| 76 |
Some (loop init) |
| 77 |
and piece () = |
| 78 |
let r = atom () in |
| 79 |
if accept '*' |
| 80 |
then greedy_mod (Re.rep r) |
| 81 |
else if accept '+' |
| 82 |
then greedy_mod (Re.rep1 r) |
| 83 |
else if accept '?' |
| 84 |
then greedy_mod (Re.opt r) |
| 85 |
else if accept '{' |
| 86 |
then ( |
| 87 |
match Parse_buffer.integer buf with |
| 88 |
| Some i -> |
| 89 |
let j = if accept ',' then Parse_buffer.integer buf else Some i in |
| 90 |
if not (accept '}') then raise Parse_error; |
| 91 |
(match j with |
| 92 |
| Some j when j < i -> raise Parse_error |
| 93 |
| _ -> ()); |
| 94 |
greedy_mod (Re.repn r i j) |
| 95 |
| None -> |
| 96 |
unget (); |
| 97 |
r) |
| 98 |
else r |
| 99 |
and atom () = |
| 100 |
if accept '.' |
| 101 |
then if dotall then Re.any else Re.notnl |
| 102 |
else if accept '(' |
| 103 |
then |
| 104 |
if accept '?' |
| 105 |
then |
| 106 |
if accept ':' |
| 107 |
then ( |
| 108 |
let r = regexp () in |
| 109 |
if not (accept ')') then raise Parse_error; |
| 110 |
r) |
| 111 |
else if accept '#' |
| 112 |
then comment () |
| 113 |
else if accept '<' |
| 114 |
then ( |
| 115 |
let name = name () in |
| 116 |
let r = regexp () in |
| 117 |
if not (accept ')') then raise Parse_error; |
| 118 |
Re.group ~name r) |
| 119 |
else raise Parse_error |
| 120 |
else ( |
| 121 |
let r = regexp () in |
| 122 |
if not (accept ')') then raise Parse_error; |
| 123 |
Re.group r) |
| 124 |
else if accept '^' |
| 125 |
then if multiline then Re.bol else Re.bos |
| 126 |
else if accept '$' |
| 127 |
then if multiline then Re.eol else if dollar_endonly then Re.leol else Re.eos |
| 128 |
else if accept '[' |
| 129 |
then if accept '^' then Re.compl (bracket []) else Re.alt (bracket []) |
| 130 |
else if accept '\\' |
| 131 |
then ( |
| 132 |
(* XXX |
| 133 |
- Back-references |
| 134 |
- \cx (control-x), \ddd |
| 135 |
*) |
| 136 |
if eos () then raise Parse_error; |
| 137 |
match get () with |
| 138 |
| 'w' -> Re.alt [ Re.alnum; Re.char '_' ] |
| 139 |
| 'W' -> Re.compl [ Re.alnum; Re.char '_' ] |
| 140 |
| 's' -> Re.space |
| 141 |
| 'S' -> Re.compl [ Re.space ] |
| 142 |
| 'd' -> Re.digit |
| 143 |
| 'D' -> Re.compl [ Re.digit ] |
| 144 |
| 'b' -> Re.alt [ Re.bow; Re.eow ] |
| 145 |
| 'B' -> Re.not_boundary |
| 146 |
| 'A' -> Re.bos |
| 147 |
| 'Z' -> Re.leol |
| 148 |
| 'z' -> Re.eos |
| 149 |
| 'G' -> Re.start |
| 150 |
| 'e' -> Re.char '\x1b' |
| 151 |
| 'f' -> Re.char '\x0c' |
| 152 |
| 'n' -> Re.char '\n' |
| 153 |
| 'r' -> Re.char '\r' |
| 154 |
| 't' -> Re.char '\t' |
| 155 |
| 'Q' -> quote (Buffer.create 12) |
| 156 |
| 'E' -> raise Parse_error |
| 157 |
| 'x' -> |
| 158 |
let c1, c2 = |
| 159 |
match in_brace ~init:[] ~f:(fun acc -> hexdigit () :: acc) with |
| 160 |
| Some [ c1; c2 ] -> c1, c2 |
| 161 |
| Some [ c2 ] -> 0, c2 |
| 162 |
| Some _ -> raise Parse_error |
| 163 |
| None -> |
| 164 |
let c1 = hexdigit () in |
| 165 |
let c2 = hexdigit () in |
| 166 |
c1, c2 |
| 167 |
in |
| 168 |
let code = (c1 * 16) + c2 in |
| 169 |
Re.char (char_of_int code) |
| 170 |
| 'o' -> |
| 171 |
(match |
| 172 |
in_brace ~init:[] ~f:(fun acc -> |
| 173 |
match maybe_octaldigit () with |
| 174 |
| None -> raise Parse_error |
| 175 |
| Some p -> p :: acc) |
| 176 |
with |
| 177 |
| None -> raise Parse_error |
| 178 |
| Some digits -> Re.char (char_of_int (acc_digits ~base:8 ~digits))) |
| 179 |
| 'a' .. 'z' | 'A' .. 'Z' -> raise Parse_error |
| 180 |
| '0' .. '7' as n1 -> |
| 181 |
let n2 = maybe_octaldigit () in |
| 182 |
let n3 = maybe_octaldigit () in |
| 183 |
(match n2, n3 with |
| 184 |
| Some n2, Some n3 -> |
| 185 |
let n1 = Char.code n1 - Char.code '0' in |
| 186 |
Re.char (char_of_int ((n1 * (8 * 8)) + (n2 * 8) + n3)) |
| 187 |
| _, _ -> raise Not_supported) |
| 188 |
| '8' .. '9' -> raise Not_supported |
| 189 |
| c -> Re.char c) |
| 190 |
else ( |
| 191 |
if eos () then raise Parse_error; |
| 192 |
match get () with |
| 193 |
| '*' | '+' | '?' | '{' | '\\' -> raise Parse_error |
| 194 |
| c -> Re.char c) |
| 195 |
and quote buf = |
| 196 |
if accept '\\' |
| 197 |
then ( |
| 198 |
if eos () then raise Parse_error; |
| 199 |
match get () with |
| 200 |
| 'E' -> Re.str (Buffer.contents buf) |
| 201 |
| c -> |
| 202 |
Buffer.add_char buf '\\'; |
| 203 |
Buffer.add_char buf c; |
| 204 |
quote buf) |
| 205 |
else ( |
| 206 |
if eos () then raise Parse_error; |
| 207 |
Buffer.add_char buf (get ()); |
| 208 |
quote buf) |
| 209 |
and hexdigit () = |
| 210 |
if eos () then raise Parse_error; |
| 211 |
match get () with |
| 212 |
| '0' .. '9' as d -> Char.code d - Char.code '0' |
| 213 |
| 'a' .. 'f' as d -> Char.code d - Char.code 'a' + 10 |
| 214 |
| 'A' .. 'F' as d -> Char.code d - Char.code 'A' + 10 |
| 215 |
| _ -> raise Parse_error |
| 216 |
and maybe_octaldigit () = |
| 217 |
if eos () |
| 218 |
then None |
| 219 |
else ( |
| 220 |
match get () with |
| 221 |
| '0' .. '7' as d -> Some (Char.code d - Char.code '0') |
| 222 |
| _ -> None) |
| 223 |
and name () = |
| 224 |
if eos () |
| 225 |
then raise Parse_error |
| 226 |
else ( |
| 227 |
match get () with |
| 228 |
| ('_' | 'a' .. 'z' | 'A' .. 'Z') as c -> |
| 229 |
let b = Buffer.create 32 in |
| 230 |
Buffer.add_char b c; |
| 231 |
name' b |
| 232 |
| _ -> raise Parse_error) |
| 233 |
and name' b = |
| 234 |
if eos () |
| 235 |
then raise Parse_error |
| 236 |
else ( |
| 237 |
match get () with |
| 238 |
| ('_' | 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9') as c -> |
| 239 |
Buffer.add_char b c; |
| 240 |
name' b |
| 241 |
| '>' -> Buffer.contents b |
| 242 |
| _ -> raise Parse_error) |
| 243 |
and bracket s = |
| 244 |
if s <> [] && accept ']' |
| 245 |
then s |
| 246 |
else ( |
| 247 |
match char () with |
| 248 |
| `Set st -> bracket (st :: s) |
| 249 |
| `Char c -> |
| 250 |
if accept '-' |
| 251 |
then |
| 252 |
if accept ']' |
| 253 |
then Re.char c :: Re.char '-' :: s |
| 254 |
else |
| 255 |
bracket |
| 256 |
(match char () with |
| 257 |
| `Char c' -> Re.rg c c' :: s |
| 258 |
| `Set st' -> Re.char c :: Re.char '-' :: st' :: s) |
| 259 |
else bracket (Re.char c :: s)) |
| 260 |
and char () = |
| 261 |
if eos () then raise Parse_error; |
| 262 |
let c = get () in |
| 263 |
if c = '[' |
| 264 |
then ( |
| 265 |
if accept '=' then raise Not_supported; |
| 266 |
match Posix_class.parse buf with |
| 267 |
| Some set -> `Set set |
| 268 |
| None -> |
| 269 |
if accept '.' |
| 270 |
then ( |
| 271 |
if eos () then raise Parse_error; |
| 272 |
let c = get () in |
| 273 |
if not (accept '.') then raise Not_supported; |
| 274 |
if not (accept ']') then raise Parse_error; |
| 275 |
`Char c) |
| 276 |
else `Char c) |
| 277 |
else if c = '\\' |
| 278 |
then ( |
| 279 |
if eos () then raise Parse_error; |
| 280 |
let c = get () in |
| 281 |
(* XXX |
| 282 |
\127, ... |
| 283 |
*) |
| 284 |
match c with |
| 285 |
| 'b' -> `Char '\008' |
| 286 |
| 'n' -> `Char '\n' (*XXX*) |
| 287 |
| 'r' -> `Char '\r' (*XXX*) |
| 288 |
| 't' -> `Char '\t' (*XXX*) |
| 289 |
| 'w' -> `Set (Re.alt [ Re.alnum; Re.char '_' ]) |
| 290 |
| 'W' -> `Set (Re.compl [ Re.alnum; Re.char '_' ]) |
| 291 |
| 's' -> `Set Re.space |
| 292 |
| 'S' -> `Set (Re.compl [ Re.space ]) |
| 293 |
| 'd' -> `Set Re.digit |
| 294 |
| 'D' -> `Set (Re.compl [ Re.digit ]) |
| 295 |
| 'a' .. 'z' | 'A' .. 'Z' -> raise Parse_error |
| 296 |
| '0' .. '9' -> raise Not_supported |
| 297 |
| _ -> `Char c) |
| 298 |
else `Char c |
| 299 |
and comment () = |
| 300 |
if eos () then raise Parse_error; |
| 301 |
if accept ')' |
| 302 |
then Re.epsilon |
| 303 |
else ( |
| 304 |
Parse_buffer.junk buf; |
| 305 |
comment ()) |
| 306 |
in |
| 307 |
let res = regexp () in |
| 308 |
if not (eos ()) then raise Parse_error; |
| 309 |
res |
| 310 |
;; |
| 311 |
|
| 312 |
type opt = |
| 313 |
[ `Ungreedy |
| 314 |
| `Dotall |
| 315 |
| `Dollar_endonly |
| 316 |
| `Multiline |
| 317 |
| `Anchored |
| 318 |
| `Caseless |
| 319 |
] |
| 320 |
|
| 321 |
let re ?(opts = []) s = |
| 322 |
let r = |
| 323 |
parse |
| 324 |
(List.memq `Multiline opts) |
| 325 |
(List.memq `Dollar_endonly opts) |
| 326 |
(List.memq `Dotall opts) |
| 327 |
(List.memq `Ungreedy opts) |
| 328 |
s |
| 329 |
in |
| 330 |
let r = if List.memq `Anchored opts then Re.seq [ Re.start; r ] else r in |
| 331 |
let r = if List.memq `Caseless opts then Re.no_case r else r in |
| 332 |
r |
| 333 |
;; |
| 334 |
|
| 335 |
let compile = Re.compile |
| 336 |
let compile_pat ?(opts = []) s = compile (re ~opts s) |