/src/lib/regular_expression/regular_expression_builder.e
Specman e | 291 lines | 190 code | 28 blank | 73 comment | 6 complexity | f0d7298fb11dcb83d088b25da02b6c68 MD5 | raw file
1-- This file is part of a Liberty Eiffel library. 2-- See the full copyright at the end. 3-- 4expanded class REGULAR_EXPRESSION_BUILDER 5 -- 6 -- Regular expression building from various dialects. 7 -- Some REGULAR_EXPRESSION object is build from a string 8 -- describing a pattern. A regular expression is a pattern 9 -- that is matched against a subject string. 10 -- 11 -- See tutorial/regular_expression for usage. 12 -- 13 14feature {ANY} -- Building REGULAR_EXPRESSION 15 convert_posix_pattern (p: STRING): REGULAR_EXPRESSION 16 -- Create some REGULAR_EXPRESSION from the pattern `p' according to POSIX syntax. 17 -- If `p' is not a valid regular expression according to POSIX syntax, then `Result' is Void 18 -- and `last_error_message' and `last_error_position' are set. 19 require 20 p /= Void 21 do 22 Result := convert_backtracking_pattern(p, posix_builder) 23 ensure 24 Result /= Void xor last_error_message /= Void 25 initialized: Result /= Void implies not Result.last_match_succeeded 26 substitution_cleared: Result /= Void implies not Result.substitution_pattern_ready 27 end 28 29 convert_perl_pattern (p: STRING): REGULAR_EXPRESSION 30 -- Create some REGULAR_EXPRESSION from the pattern `p' according to Perl syntax. 31 -- If `p' is not a valid regular expression according to Perl syntax, then `Result' is Void 32 -- and `last_error_message' and `last_error_position' are set. 33 require 34 p /= Void 35 do 36 if has_extended_legibility then 37 perl5_builder.set_extended_legibility 38 else 39 perl5_builder.set_no_extended_legibility 40 end 41 Result := convert_backtracking_pattern(p, perl5_builder) 42 ensure 43 Result /= Void xor last_error_message /= Void 44 initialized: Result /= Void implies not Result.last_match_succeeded 45 substitution_cleared: Result /= Void implies not Result.substitution_pattern_ready 46 end 47 48 convert_python_pattern (p: STRING): REGULAR_EXPRESSION 49 -- Create some REGULAR_EXPRESSION from the pattern `p' according to Python syntax. 50 -- If `p' is not a valid regular expression according to Python syntax, then `Result' is Void 51 -- and `last_error_message' and `last_error_position' are set. 52 require 53 p /= Void 54 do 55 if has_extended_legibility then 56 python_builder.set_extended_legibility 57 else 58 python_builder.set_no_extended_legibility 59 end 60 Result := convert_backtracking_pattern(p, python_builder) 61 ensure 62 Result /= Void xor last_error_message /= Void 63 initialized: Result /= Void implies not Result.last_match_succeeded 64 substitution_cleared: Result /= Void implies not Result.substitution_pattern_ready 65 end 66 67feature {ANY} -- options 68 is_case_insensitive: BOOLEAN 69 -- Is the match case insensitive? 70 -- Default is False 71 72 is_case_sensitive: BOOLEAN 73 -- Is the match case sensitive? 74 -- Default is True 75 do 76 Result := not is_case_insensitive 77 end 78 79 set_case_sensitive 80 -- Set the match as case sensitive. 81 do 82 is_case_insensitive := False 83 ensure 84 definition: is_case_insensitive = False and is_case_sensitive = True 85 end 86 87 set_case_insensitive 88 -- Set the match as case insensitive. 89 do 90 is_case_insensitive := True 91 ensure 92 definition: is_case_insensitive = True and is_case_sensitive = False 93 end 94 95 does_any_match_newline: BOOLEAN 96 -- Does the "any character" mark match a newline? 97 -- Default is False 98 99 set_any_match_newline 100 -- The "any character" mark will match a newline. 101 do 102 does_any_match_newline := True 103 ensure 104 definition: does_any_match_newline = True 105 end 106 107 set_any_dont_match_newline 108 -- The "any character" mark will not match a newline. 109 do 110 does_any_match_newline := False 111 ensure 112 definition: does_any_match_newline = False 113 end 114 115 does_match_line_boundary: BOOLEAN 116 -- Does the begin/end marks match line boundary? 117 -- Default is False 118 119 does_match_text_boundary: BOOLEAN 120 -- Does the begin/end marks match text boundary? 121 -- Default is True 122 do 123 Result := not does_match_line_boundary 124 ensure 125 definition: Result = not does_match_line_boundary 126 end 127 128 set_match_line_boundary 129 -- The begin/end marks will match line boundary. 130 do 131 does_match_line_boundary := True 132 ensure 133 definition: does_match_line_boundary = True and does_match_text_boundary = False 134 end 135 136 set_match_text_boundary 137 -- The begin/end marks will match text boundary. 138 do 139 does_match_line_boundary := False 140 ensure 141 definition: does_match_line_boundary = False and does_match_text_boundary = True 142 end 143 144 has_extended_legibility: BOOLEAN 145 -- Is the extended legibility active? 146 147 has_extended_ligibility: BOOLEAN 148 obsolete 149 "Use `has_extended_legibility' instead." 150 do 151 Result := has_extended_legibility 152 end 153 154 set_extended_legibility 155 -- Activate extended legibility. 156 do 157 has_extended_legibility := True 158 ensure 159 definition: has_extended_legibility = True 160 end 161 162 set_extended_ligibility 163 obsolete 164 "Use `set_extended_legibility' instead." 165 do 166 set_extended_legibility 167 ensure 168 definition: has_extended_legibility = True 169 end 170 171 set_no_extended_legibility 172 -- Deactivate extended legibility. 173 do 174 has_extended_legibility := False 175 ensure 176 definition: has_extended_legibility = False 177 end 178 179 set_no_extended_ligibility 180 obsolete 181 "Use `set_no_extended_legibility' instead." 182 do 183 set_no_extended_legibility 184 ensure 185 definition: has_extended_legibility = False 186 end 187 188 set_default_options 189 -- Set the default options 190 do 191 set_case_sensitive 192 set_any_dont_match_newline 193 set_match_text_boundary 194 set_no_extended_legibility 195 ensure 196 is_case_sensitive 197 not does_any_match_newline 198 does_match_text_boundary 199 not has_extended_legibility 200 end 201 202feature {ANY} -- Error informations 203 last_error_message: STRING 204 -- Used to report error during last creation attempt. 205 -- 206 -- See also `convert_perl_pattern', `convert_posix_pattern'. 207 208 last_error_position: INTEGER 209 -- Used to report error position during last creation attempt. 210 -- 211 -- See also `convert_perl_pattern', `convert_posix_pattern'. 212 213feature {} -- Internal 214 posix_builder: POSIX_REGULAR_EXPRESSION_BUILDER 215 -- The builder for the POSIX syntax 216 once 217 create Result.make 218 end 219 220 perl5_builder: PERL5_REGULAR_EXPRESSION_BUILDER 221 -- The builder for the PERL5 syntax 222 once 223 create Result.make 224 end 225 226 python_builder: PYTHON_REGULAR_EXPRESSION_BUILDER 227 -- The builder for the Python syntax 228 once 229 create Result.make 230 end 231 232 convert_backtracking_pattern (p: STRING; builder: BACKTRACKING_REGULAR_EXPRESSION_BUILDER): BACKTRACKING_REGULAR_EXPRESSION 233 -- Create some BACKTRACKING_REGULAR_EXPRESSION from the pattern `p' according to the syntax 234 -- passed by the given 'builder'. 235 -- If `p' is not a valid regular expression according the said syntax, then `Result' is Void 236 -- and `last_error_message' and `last_error_message' are set. 237 require 238 p /= Void and builder /= Void 239 do 240 if is_case_insensitive then 241 builder.set_case_insensitive 242 else 243 builder.set_case_sensitive 244 end 245 if does_any_match_newline then 246 builder.set_any_match_newline 247 else 248 builder.set_any_dont_match_newline 249 end 250 if does_match_line_boundary then 251 builder.set_match_line_boundary 252 else 253 builder.set_match_text_boundary 254 end 255 builder.set_expression(p) 256 builder.parse 257 if builder.has_result then 258 create {BACKTRACKING_REGULAR_EXPRESSION} Result.make 259 Result.set_pattern(builder.last_pattern) 260 last_error_message := Void 261 else 262 last_error_position := builder.position 263 last_error_message := builder.last_error.twin 264 end 265 ensure 266 Result /= Void xor last_error_message /= Void 267 initialized: Result /= Void implies not Result.last_match_succeeded 268 substitution_cleared: Result /= Void implies not Result.substitution_pattern_ready 269 end 270 271end -- class REGULAR_EXPRESSION_BUILDER 272-- 273-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file. 274-- 275-- Permission is hereby granted, free of charge, to any person obtaining a copy 276-- of this software and associated documentation files (the "Software"), to deal 277-- in the Software without restriction, including without limitation the rights 278-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 279-- copies of the Software, and to permit persons to whom the Software is 280-- furnished to do so, subject to the following conditions: 281-- 282-- The above copyright notice and this permission notice shall be included in 283-- all copies or substantial portions of the Software. 284-- 285-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 286-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 287-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 288-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 289-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 290-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 291-- THE SOFTWARE.