Debian Codegraph @

vendor/re/src/emacs.ml

raw
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
26 exception Not_supported
27
28 let parse s =
29 let buf = Parse_buffer.create s in
30 let accept = Parse_buffer.accept buf in
31 let accept2 = Parse_buffer.accept2 buf in
32 let eos () = Parse_buffer.eos buf in
33 let test2 = Parse_buffer.test2 buf in
34 let get () = Parse_buffer.get buf in
35 let rec regexp () = regexp' (branch ())
36 and regexp' left =
37 if accept2 '\\' '|' then regexp' (Re.alt [ left; branch () ]) else left
38 and branch () = branch' []
39 and branch' left =
40 if eos () || test2 '\\' '|' || test2 '\\' ')'
41 then Re.seq (List.rev left)
42 else branch' (piece () :: left)
43 and piece () =
44 let r = atom () in
45 if accept '*'
46 then Re.rep r
47 else if accept '+'
48 then Re.rep1 r
49 else if accept '?'
50 then Re.opt r
51 else r
52 and atom () =
53 if accept '.'
54 then Re.notnl
55 else if accept '^'
56 then Re.bol
57 else if accept '$'
58 then Re.eol
59 else if accept '['
60 then if accept '^' then Re.compl (bracket []) else Re.alt (bracket [])
61 else if accept '\\'
62 then
63 if accept '('
64 then (
65 let r = regexp () in
66 if not (accept2 '\\' ')') then raise Parse_error;
67 Re.group r)
68 else if accept '`'
69 then Re.bos
70 else if accept '\''
71 then Re.eos
72 else if accept '='
73 then Re.start
74 else if accept 'b'
75 then Re.alt [ Re.bow; Re.eow ]
76 else if accept 'B'
77 then Re.not_boundary
78 else if accept '<'
79 then Re.bow
80 else if accept '>'
81 then Re.eow
82 else if accept 'w'
83 then Re.alt [ Re.alnum; Re.char '_' ]
84 else if accept 'W'
85 then Re.compl [ Re.alnum; Re.char '_' ]
86 else (
87 if eos () then raise Parse_error;
88 match get () with
89 | ('*' | '+' | '?' | '[' | ']' | '.' | '^' | '$' | '\\') as c -> Re.char c
90 | '0' .. '9' -> raise Not_supported
91 | _ -> raise Parse_error)
92 else (
93 if eos () then raise Parse_error;
94 match get () with
95 | '*' | '+' | '?' -> raise Parse_error
96 | c -> Re.char c)
97 and bracket s =
98 if s <> [] && accept ']'
99 then s
100 else (
101 let c = char () in
102 if accept '-'
103 then
104 if accept ']'
105 then Re.char c :: Re.char '-' :: s
106 else (
107 let c' = char () in
108 bracket (Re.rg c c' :: s))
109 else bracket (Re.char c :: s))
110 and char () =
111 if eos () then raise Parse_error;
112 get ()
113 in
114 let res = regexp () in
115 if not (eos ()) then raise Parse_error;
116 res
117 ;;
118
119 let re ?(case = true) s =
120 let r = parse s in
121 if case then r else Re.no_case r
122 ;;
123
124 let compile = Re.compile
125 let compile_pat ?(case = true) s = compile (re ~case s)