Debian Codegraph @

vendor/re/src/posix.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 (*
24 What we could (should?) do:
25 - a* ==> longest ((shortest (no_group a)* ), a | ()) (!!!)
26 - abc understood as (ab)c
27 - "((a?)|b)" against "ab" should not bind the first subpattern to anything
28
29 Note that it should be possible to handle "(((ab)c)d)e" efficiently
30 *)
31 module Re = Core
32
33 exception Parse_error = Parse_buffer.Parse_error
34 exception Not_supported
35
36 let parse newline s =
37 let buf = Parse_buffer.create s in
38 let accept = Parse_buffer.accept buf in
39 let eos () = Parse_buffer.eos buf in
40 let test c = Parse_buffer.test buf c in
41 let unget () = Parse_buffer.unget buf in
42 let get () = Parse_buffer.get buf in
43 let rec regexp () = regexp' (branch ())
44 and regexp' left = if accept '|' then regexp' (Re.alt [ left; branch () ]) else left
45 and branch () = branch' []
46 and branch' left =
47 if eos () || test '|' || test ')'
48 then Re.seq (List.rev left)
49 else branch' (piece () :: left)
50 and piece () =
51 let r = atom () in
52 if accept '*'
53 then Re.rep (Re.nest r)
54 else if accept '+'
55 then Re.rep1 (Re.nest r)
56 else if accept '?'
57 then Re.opt r
58 else if accept '{'
59 then (
60 match Parse_buffer.integer buf with
61 | Some i ->
62 let j = if accept ',' then Parse_buffer.integer buf else Some i in
63 if not (accept '}') then raise Parse_error;
64 (match j with
65 | Some j when j < i -> raise Parse_error
66 | _ -> ());
67 Re.repn (Re.nest r) i j
68 | None ->
69 unget ();
70 r)
71 else r
72 and atom () =
73 if accept '.'
74 then if newline then Re.notnl else Re.any
75 else if accept '('
76 then (
77 let r = regexp () in
78 if not (accept ')') then raise Parse_error;
79 Re.group r)
80 else if accept '^'
81 then if newline then Re.bol else Re.bos
82 else if accept '$'
83 then if newline then Re.eol else Re.eos
84 else if accept '['
85 then
86 if accept '^'
87 then Re.diff (Re.compl (bracket [])) (Re.char '\n')
88 else Re.alt (bracket [])
89 else if accept '\\'
90 then (
91 if eos () then raise Parse_error;
92 match get () with
93 | ('|' | '(' | ')' | '*' | '+' | '?' | '[' | '.' | '^' | '$' | '{' | '\\') as c ->
94 Re.char c
95 | _ -> raise Parse_error)
96 else (
97 if eos () then raise Parse_error;
98 match get () with
99 | '*' | '+' | '?' | '{' | '\\' -> raise Parse_error
100 | c -> Re.char c)
101 and bracket s =
102 if s <> [] && accept ']'
103 then s
104 else (
105 match char () with
106 | `Set st -> bracket (st :: s)
107 | `Char c ->
108 if accept '-'
109 then
110 if accept ']'
111 then Re.char c :: Re.char '-' :: s
112 else
113 bracket
114 (match char () with
115 | `Char c' -> Re.rg c c' :: s
116 | `Set st' -> Re.char c :: Re.char '-' :: st' :: s)
117 else bracket (Re.char c :: s))
118 and char () =
119 if eos () then raise Parse_error;
120 let c = get () in
121 if c = '['
122 then (
123 match Posix_class.parse buf with
124 | Some set -> `Set set
125 | None ->
126 if accept '.'
127 then (
128 if eos () then raise Parse_error;
129 let c = get () in
130 if not (accept '.') then raise Not_supported;
131 if not (accept ']') then raise Parse_error;
132 `Char c)
133 else `Char c)
134 else `Char c
135 in
136 let res = regexp () in
137 if not (eos ()) then raise Parse_error;
138 res
139 ;;
140
141 type opt =
142 [ `ICase
143 | `NoSub
144 | `Newline
145 ]
146
147 let re ?(opts = []) s =
148 let r = parse (List.memq `Newline opts) s in
149 let r = if List.memq `ICase opts then Re.no_case r else r in
150 let r = if List.memq `NoSub opts then Re.no_group r else r in
151 r
152 ;;
153
154 let compile re = Re.compile (Re.longest re)
155 let compile_pat ?(opts = []) s = compile (re ~opts s)