| 1 |
/*------------------------------------------------------------------------* |
| 2 |
* static const char CVSID[] = "$Id: regularExp.c,v 1.30 2006/08/13 21:47:45 yooden Exp $"; |
| 3 |
* `CompileRE', `ExecRE', and `substituteRE' -- regular expression parsing |
| 4 |
* |
| 5 |
* This is a HIGHLY ALTERED VERSION of Henry Spencer's `regcomp' and |
| 6 |
* `regexec' code adapted for NEdit. |
| 7 |
* |
| 8 |
* .-------------------------------------------------------------------. |
| 9 |
* | ORIGINAL COPYRIGHT NOTICE: | |
| 10 |
* | | |
| 11 |
* | Copyright (c) 1986 by University of Toronto. | |
| 12 |
* | Written by Henry Spencer. Not derived from licensed software. | |
| 13 |
* | | |
| 14 |
* | Permission is granted to anyone to use this software for any | |
| 15 |
* | purpose on any computer system, and to redistribute it freely, | |
| 16 |
* | subject to the following restrictions: | |
| 17 |
* | | |
| 18 |
* | 1. The author is not responsible for the consequences of use of | |
| 19 |
* | this software, no matter how awful, even if they arise | |
| 20 |
* | from defects in it. | |
| 21 |
* | | |
| 22 |
* | 2. The origin of this software must not be misrepresented, either | |
| 23 |
* | by explicit claim or by omission. | |
| 24 |
* | | |
| 25 |
* | 3. Altered versions should be plainly marked as such, and must not | |
| 26 |
* | be misrepresented as being the original software. | |
| 27 |
* `-------------------------------------------------------------------' |
| 28 |
* |
| 29 |
* This is free software; you can redistribute it and/or modify it under the |
| 30 |
* terms of the GNU General Public License as published by the Free Software |
| 31 |
* Foundation; either version 2 of the License, or (at your option) any later |
| 32 |
* version. In addition, you may distribute version of this program linked to |
| 33 |
* Motif or Open Motif. See README for details. |
| 34 |
* |
| 35 |
* This software is distributed in the hope that it will be useful, but WITHOUT |
| 36 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 37 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 38 |
* for more details. |
| 39 |
* |
| 40 |
* You should have received a copy of the GNU General Public License along with |
| 41 |
* software; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 42 |
* Place, Suite 330, Boston, MA 02111-1307 USA |
| 43 |
* |
| 44 |
* |
| 45 |
* BEWARE that some of this code is subtly aware of the way operator |
| 46 |
* precedence is structured in regular expressions. Serious changes in |
| 47 |
* regular-expression syntax might require a total rethink. |
| 48 |
* -- Henry Spencer |
| 49 |
* (Yes, it did!) -- Christopher Conrad, Dec. 1999 |
| 50 |
* |
| 51 |
* January, 1994, Mark Edel |
| 52 |
* Consolidated files, changed names of external functions to avoid |
| 53 |
* potential conflicts with native regcomp and regexec functions, changed |
| 54 |
* error reporting to NEdit form, added multi-line and reverse searching, |
| 55 |
* and added \n \t \u \U \l \L. |
| 56 |
* |
| 57 |
* June, 1996, Mark Edel |
| 58 |
* Bug in NEXT macro, didn't work for expressions which compiled to over |
| 59 |
* 256 bytes. |
| 60 |
* |
| 61 |
* December, 1999, Christopher Conrad |
| 62 |
* Reformatted code for readability, improved error output, added octal and |
| 63 |
* hexadecimal escapes, added back-references (\1-\9), added positive look |
| 64 |
* ahead: (?=...), added negative lookahead: (?!...), added non-capturing |
| 65 |
* parentheses: (?:...), added case insensitive constructs (?i...) and |
| 66 |
* (?I...), added newline matching constructs (?n...) and (?N...), added |
| 67 |
* regex comments: (?#...), added shortcut escapes: \d\D\l\L\s\S\w\W\y\Y. |
| 68 |
* Added "not a word boundary" anchor \B. |
| 69 |
* |
| 70 |
* July, 2002, Eddy De Greef |
| 71 |
* Added look behind, both positive (?<=...) and negative (?<!...) for |
| 72 |
* bounded-length patterns. |
| 73 |
* |
| 74 |
* November, 2004, Eddy De Greef |
| 75 |
* Added constrained matching (allowing specification of the logical end |
| 76 |
* of the string iso. matching till \0), and fixed several (probably |
| 77 |
* very old) string overrun errors that could easily result in crashes, |
| 78 |
* especially in client code. |
| 79 |
* |
| 80 |
* June, 2008, David Weenink |
| 81 |
* Adaptation for use in the Praat program. |
| 82 |
* Changed reg_error to call Melder_error. |
| 83 |
* Extra parameter for SubstituteRE to allow error differentiation |
| 84 |
* (not enough memory can be repaired upstream). |
| 85 |
* |
| 86 |
* April, 2018, Paul Boersma |
| 87 |
* Added Unicode-based definitions of letters, digits, spaces and word boundaries. |
| 88 |
* Thereby removed shortcut escapes from classes. |
| 89 |
*/ |
| 90 |
|
| 91 |
#include <limits.h> |
| 92 |
#include "melder.h" |
| 93 |
#include "../kar/wctype_portable.h" |
| 94 |
|
| 95 |
/* The first byte of the regexp internal `program' is a magic number to help |
| 96 |
guard against corrupted data; the compiled regex code really begins in the |
| 97 |
second byte. */ |
| 98 |
|
| 99 |
#define MAGIC 0234 |
| 100 |
|
| 101 |
/* The "internal use only" fields in `regexp.h' are present to pass info from |
| 102 |
* `CompileRE' to `ExecRE' which permits the execute phase to run lots faster on |
| 103 |
* simple cases. They are: |
| 104 |
* |
| 105 |
* match_start Character that must begin a match; '\0' if none obvious. |
| 106 |
* anchor Is the match anchored (at beginning-of-line only)? |
| 107 |
* |
| 108 |
* `match_start' and `anchor' permit very fast decisions on suitable starting |
| 109 |
* points for a match, considerably reducing the work done by ExecRE. */ |
| 110 |
|
| 111 |
/* STRUCTURE FOR A REGULAR EXPRESSION (regex) `PROGRAM'. |
| 112 |
* |
| 113 |
* This is essentially a linear encoding of a nondeterministic finite-state |
| 114 |
* machine or NFA (aka syntax charts or `railroad normal form' in parsing |
| 115 |
* technology). Each node is an opcode plus a NEXT pointer, possibly |
| 116 |
* followed by operands. NEXT pointers of all nodes except BRANCH implement |
| 117 |
* concatenation; a NEXT pointer with a BRANCH on both ends of it is |
| 118 |
* connecting two alternatives. (Here we have one of the subtle syntax |
| 119 |
* dependencies: an individual BRANCH (as opposed to a collection of them) is |
| 120 |
* never concatenated with anything because of operator precedence.) The |
| 121 |
* operand of some types of nodes is a literal string; for others, it is a node |
| 122 |
* leading into a sub-FSM. In particular, the operand of a BRANCH node is the |
| 123 |
* first node of the branch. (NB this is _NOT_ a tree structure: the tail of |
| 124 |
* the branch connects to the thing following the set of BRANCHes.) |
| 125 |
* |
| 126 |
* The opcodes are: */ |
| 127 |
|
| 128 |
/* DEFINITION VALUE MEANING */ |
| 129 |
|
| 130 |
#define END 1 /* End of program. */ |
| 131 |
|
| 132 |
/* Zero width positional assertions. */ |
| 133 |
|
| 134 |
#define BOL 2 /* Match position at beginning of line. */ |
| 135 |
#define EOL 3 /* Match position at end of line. */ |
| 136 |
#define BOWORD 4 /* Match "" representing word delimiter or BOL */ |
| 137 |
#define EOWORD 5 /* Match "" representing word delimiter or EOL */ |
| 138 |
#define NOT_BOUNDARY 6 /* Not word boundary (\B, opposite of < and >) */ |
| 139 |
|
| 140 |
/* Op codes with null terminated string operands. */ |
| 141 |
|
| 142 |
#define EXACTLY 7 /* Match this string. */ |
| 143 |
#define SIMILAR 8 /* Match this case insensitive string */ |
| 144 |
#define ANY_OF 9 /* Match any character in the set. */ |
| 145 |
#define ANY_BUT 10 /* Match any character not in the set. */ |
| 146 |
|
| 147 |
/* Op codes to match any character. */ |
| 148 |
|
| 149 |
#define ANY 11 /* Match any one character (implements '.') */ |
| 150 |
#define EVERY 12 /* Same as ANY but matches newline. */ |
| 151 |
|
| 152 |
/* Shortcut escapes, \d, \D, \l, \L, \s, \S, \w, \W, \y, \Y. */ |
| 153 |
|
| 154 |
#define DIGIT 13 /* Match any digit, i.e. [0123456789] */ |
| 155 |
#define NOT_DIGIT 14 /* Match any non-digit, i.e. [^0123456789] */ |
| 156 |
#define LETTER 15 /* Match any letter character [a-zA-Z] */ |
| 157 |
#define NOT_LETTER 16 /* Match any non-letter character [^a-zA-Z] */ |
| 158 |
#define SPACE 17 /* Match any whitespace character EXCEPT \n */ |
| 159 |
#define SPACE_NL 18 /* Match any whitespace character INCLUDING \n */ |
| 160 |
#define NOT_SPACE 19 /* Match any non-whitespace character */ |
| 161 |
#define NOT_SPACE_NL 20 /* Same as NOT_SPACE but matches newline. */ |
| 162 |
#define WORD_CHAR 21 /* Match any word character [a-zA-Z0-9_] */ |
| 163 |
#define NOT_WORD_CHAR 22 /* Match any non-word character [^a-zA-Z0-9_] */ |
| 164 |
#define IS_DELIM 23 /* Match any character that's a word delimiter */ |
| 165 |
#define NOT_DELIM 24 /* Match any character NOT a word delimiter */ |
| 166 |
|
| 167 |
/* Quantifier nodes. (Only applied to SIMPLE nodes. Quantifiers applied to non |
| 168 |
SIMPLE nodes or larger atoms are implemented using complex constructs.)*/ |
| 169 |
|
| 170 |
#define STAR 25 /* Match this (simple) thing 0 or more times. */ |
| 171 |
#define LAZY_STAR 26 /* Minimal matching STAR */ |
| 172 |
#define QUESTION 27 /* Match this (simple) thing 0 or 1 times. */ |
| 173 |
#define LAZY_QUESTION 28 /* Minimal matching QUESTION */ |
| 174 |
#define PLUS 29 /* Match this (simple) thing 1 or more times. */ |
| 175 |
#define LAZY_PLUS 30 /* Minimal matching PLUS */ |
| 176 |
#define BRACE 31 /* Match this (simple) thing m to n times. */ |
| 177 |
#define LAZY_BRACE 32 /* Minimal matching BRACE */ |
| 178 |
|
| 179 |
/* Nodes used to build complex constructs. */ |
| 180 |
|
| 181 |
#define NOTHING 33 /* Match empty string (always matches) */ |
| 182 |
#define BRANCH 34 /* Match this alternative, or the next... */ |
| 183 |
#define BACK 35 /* Always matches, NEXT ptr points backward. */ |
| 184 |
#define INIT_COUNT 36 /* Initialize {m,n} counter to zero */ |
| 185 |
#define INC_COUNT 37 /* Increment {m,n} counter by one */ |
| 186 |
#define TEST_COUNT 38 /* Test {m,n} counter against operand */ |
| 187 |
|
| 188 |
/* Back Reference nodes. */ |
| 189 |
|
| 190 |
#define BACK_REF 39 /* Match latest matched parenthesized text */ |
| 191 |
#define BACK_REF_CI 40 /* Case insensitive version of BACK_REF */ |
| 192 |
#define X_REGEX_BR 41 /* Cross-Regex Back-Ref for syntax highlighting */ |
| 193 |
#define X_REGEX_BR_CI 42 /* Case insensitive version of X_REGEX_BR_CI */ |
| 194 |
|
| 195 |
/* Various nodes used to implement parenthetical constructs. */ |
| 196 |
|
| 197 |
#define POS_AHEAD_OPEN 43 /* Begin positive look ahead */ |
| 198 |
#define NEG_AHEAD_OPEN 44 /* Begin negative look ahead */ |
| 199 |
#define LOOK_AHEAD_CLOSE 45 /* End positive or negative look ahead */ |
| 200 |
|
| 201 |
#define POS_BEHIND_OPEN 46 /* Begin positive look behind */ |
| 202 |
#define NEG_BEHIND_OPEN 47 /* Begin negative look behind */ |
| 203 |
#define LOOK_BEHIND_CLOSE 48 /* Close look behind */ |
| 204 |
|
| 205 |
#define OPEN 49 /* Open for capturing parentheses. */ |
| 206 |
|
| 207 |
/* OPEN+1 is number 1, etc. */ |
| 208 |
#define CLOSE (OPEN + NSUBEXP) /* Close for capturing parentheses. */ |
| 209 |
|
| 210 |
#define LAST_PAREN (CLOSE + NSUBEXP) |
| 211 |
|
| 212 |
#if (LAST_PAREN > UCHAR_MAX) |
| 213 |
#error "Too many parentheses for storage in an unsigned char (LAST_PAREN too big.)" |
| 214 |
#endif |
| 215 |
|
| 216 |
/* The next_ptr () function can consume up to 30% of the time during matching |
| 217 |
because it is called an immense number of times (an average of 25 |
| 218 |
next_ptr() calls per match() call was witnessed for Perl syntax |
| 219 |
highlighting). Therefore it is well worth removing some of the function |
| 220 |
call overhead by selectively inlining the next_ptr() calls. Moreover, |
| 221 |
the inlined code can be simplified for matching because one of the tests, |
| 222 |
only necessary during compilation, can be left out. |
| 223 |
The net result of using this inlined version at two critical places is |
| 224 |
a 25% speedup (again, witnesses on Perl syntax highlighting). */ |
| 225 |
|
| 226 |
#define NEXT_PTR(in_ptr, out_ptr)\ |
| 227 |
next_ptr_offset = GET_OFFSET (in_ptr);\ |
| 228 |
if (next_ptr_offset == 0)\ |
| 229 |
out_ptr = NULL;\ |
| 230 |
else {\ |
| 231 |
if (GET_OP_CODE (in_ptr) == BACK)\ |
| 232 |
out_ptr = in_ptr - next_ptr_offset;\ |
| 233 |
else \ |
| 234 |
out_ptr = in_ptr + next_ptr_offset;\ |
| 235 |
} |
| 236 |
|
| 237 |
/* OPCODE NOTES: |
| 238 |
------------ |
| 239 |
|
| 240 |
All nodes consist of an 8 bit op code followed by 2 bytes that make up a 16 |
| 241 |
bit NEXT pointer. Some nodes have a null terminated character string operand |
| 242 |
following the NEXT pointer. Other nodes may have an 8 bit index operand. |
| 243 |
The TEST_COUNT node has an index operand followed by a 16 bit test value. |
| 244 |
The BRACE and LAZY_BRACE nodes have two 16 bit values for min and max but no |
| 245 |
index value. |
| 246 |
|
| 247 |
SIMILAR |
| 248 |
Operand(s): null terminated string |
| 249 |
|
| 250 |
Implements a case insensitive match of a string. Mostly intended for use |
| 251 |
in syntax highlighting patterns for keywords of languages like FORTRAN |
| 252 |
and Ada that are case insensitive. The regex text in this node is |
| 253 |
converted to lower case during regex compile. |
| 254 |
|
| 255 |
DIGIT, NOT_DIGIT, LETTER, NOT_LETTER, SPACE, NOT_SPACE, WORD_CHAR, |
| 256 |
NOT_WORD_CHAR |
| 257 |
Operand(s): None |
| 258 |
|
| 259 |
Implements shortcut escapes \d, \D, \l, \L, \s, \S, \w, \W. |
| 260 |
The Unicode-aware functions Melder_isDecimalNumber(), Melder_isLetter(), |
| 261 |
Melder_isHorizontalSpace(), and Melder_isWordCharacter are |
| 262 |
used to implement these in the hopes of increasing portability. |
| 263 |
|
| 264 |
NOT_BOUNDARY |
| 265 |
Operand(s): None |
| 266 |
|
| 267 |
Implements \B as a zero width assertion that the current character is |
| 268 |
NOT on a word boundary. Word boundaries are defined to be the position |
| 269 |
between two characters where one of those characters is |
| 270 |
a Unicode word character and the other character is not. |
| 271 |
|
| 272 |
IS_DELIM |
| 273 |
Operand(s): None |
| 274 |
|
| 275 |
Implements \y as any character that is a Unicode word delimiter. |
| 276 |
|
| 277 |
NOT_DELIM |
| 278 |
Operand(s): None |
| 279 |
|
| 280 |
Implements \Y as any character that is NOT a Unicode word delimiter. |
| 281 |
|
| 282 |
STAR, PLUS, QUESTION, and complex '*', '+', and '?' |
| 283 |
Operand(s): None (Note: NEXT pointer is usually zero. The code that |
| 284 |
processes this node skips over it.) |
| 285 |
|
| 286 |
Complex (parenthesized) versions implemented as circular BRANCH |
| 287 |
structures using BACK. SIMPLE versions (one character per match) are |
| 288 |
implemented separately for speed and to minimize recursion. |
| 289 |
|
| 290 |
BRACE, LAZY_BRACE |
| 291 |
Operand(s): minimum value (2 bytes), maximum value (2 bytes) |
| 292 |
|
| 293 |
Implements the {m,n} construct for atoms that are SIMPLE. |
| 294 |
|
| 295 |
BRANCH |
| 296 |
Operand(s): None |
| 297 |
|
| 298 |
The set of branches constituting a single choice are hooked together |
| 299 |
with their NEXT pointers, since precedence prevents anything being |
| 300 |
concatenated to any individual branch. The NEXT pointer of the last |
| 301 |
BRANCH in a choice points to the thing following the whole choice. This |
| 302 |
is also where the final NEXT pointer of each individual branch points; |
| 303 |
each branch starts with the operand node of a BRANCH node. |
| 304 |
|
| 305 |
BACK |
| 306 |
Operand(s): None |
| 307 |
|
| 308 |
Normal NEXT pointers all implicitly point forward. Back implicitly |
| 309 |
points backward. BACK exists to make loop structures possible. |
| 310 |
|
| 311 |
INIT_COUNT |
| 312 |
Operand(s): index (1 byte) |
| 313 |
|
| 314 |
Initializes the count array element referenced by the index operand. |
| 315 |
This node is used to build general (i.e. parenthesized) {m,n} constructs. |
| 316 |
|
| 317 |
INC_COUNT |
| 318 |
Operand(s): index (1 byte) |
| 319 |
|
| 320 |
Increments the count array element referenced by the index operand. |
| 321 |
This node is used to build general (i.e. parenthesized) {m,n} constructs. |
| 322 |
|
| 323 |
TEST_COUNT |
| 324 |
Operand(s): index (1 byte), test value (2 bytes) |
| 325 |
|
| 326 |
Tests the current value of the count array element specified by the |
| 327 |
index operand against the test value. If the current value is less than |
| 328 |
the test value, control passes to the node after that TEST_COUNT node. |
| 329 |
Otherwise control passes to the node referenced by the NEXT pointer for |
| 330 |
the TEST_COUNT node. This node is used to build general (i.e. |
| 331 |
parenthesized) {m,n} constructs. |
| 332 |
|
| 333 |
BACK_REF, BACK_REF_CI |
| 334 |
Operand(s): index (1 byte, value 1-9) |
| 335 |
|
| 336 |
Implements back references. This node will attempt to match whatever text |
| 337 |
was most recently captured by the index'th set of parentheses. |
| 338 |
BACK_REF_CI is case insensitive version. |
| 339 |
|
| 340 |
X_REGEX_BR, X_REGEX_BR_CI |
| 341 |
(NOT IMPLEMENTED YET) |
| 342 |
|
| 343 |
Operand(s): index (1 byte, value 1-9) |
| 344 |
|
| 345 |
Implements back references into a previously matched but separate regular |
| 346 |
expression. This is used by syntax highlighting patterns. This node will |
| 347 |
attempt to match whatever text was most captured by the index'th set of |
| 348 |
parentheses of the separate regex passed to ExecRE. X_REGEX_BR_CI is case |
| 349 |
insensitive version. |
| 350 |
|
| 351 |
POS_AHEAD_OPEN, NEG_AHEAD_OPEN, LOOK_AHEAD_CLOSE |
| 352 |
|
| 353 |
Operand(s): None |
| 354 |
|
| 355 |
Implements positive and negative look ahead. Look ahead is an assertion |
| 356 |
that something is either there or not there. Once this is determined the |
| 357 |
regex engine backtracks to where it was just before the look ahead was |
| 358 |
encountered, i.e. look ahead is a zero width assertion. |
| 359 |
|
| 360 |
POS_BEHIND_OPEN, NEG_BEHIND_OPEN, LOOK_BEHIND_CLOSE |
| 361 |
|
| 362 |
Operand(s): 2x2 bytes for OPEN (match boundaries), None for CLOSE |
| 363 |
|
| 364 |
Implements positive and negative look behind. Look behind is an assertion |
| 365 |
that something is either there or not there in front of the current |
| 366 |
position. Look behind is a zero width assertion, with the additional |
| 367 |
constraint that it must have a bounded length (for complexity and |
| 368 |
efficiency reasons; note that most other implementation even impose |
| 369 |
fixed length). |
| 370 |
|
| 371 |
OPEN, CLOSE |
| 372 |
|
| 373 |
Operand(s): None |
| 374 |
|
| 375 |
OPEN + n = Start of parenthesis 'n', CLOSE + n = Close of parenthesis |
| 376 |
'n', and are numbered at compile time. |
| 377 |
*/ |
| 378 |
|
| 379 |
/* A node is one char of opcode followed by two chars of NEXT pointer plus |
| 380 |
* any operands. NEXT pointers are stored as two 8-bit pieces, high order |
| 381 |
* first. The value is a positive offset from the opcode of the node |
| 382 |
* containing it. An operand, if any, simply follows the node. (Note that |
| 383 |
* much of the code generation knows about this implicit relationship.) |
| 384 |
* |
| 385 |
* Using two bytes for NEXT_PTR_SIZE is vast overkill for most things, |
| 386 |
* but allows patterns to get big without disasters. */ |
| 387 |
|
| 388 |
#define OP_CODE_SIZE 1 |
| 389 |
#define NEXT_PTR_SIZE 2 |
| 390 |
#define INDEX_SIZE 1 |
| 391 |
#define LENGTH_SIZE 4 |
| 392 |
#define NODE_SIZE (NEXT_PTR_SIZE + OP_CODE_SIZE) |
| 393 |
|
| 394 |
#define GET_OP_CODE(p) (*(char32 *)(p)) |
| 395 |
#define OPERAND(p) ((p) + NODE_SIZE) |
| 396 |
#define GET_OFFSET(p) ((( *((p) + 1) & 0377) << 8) + (( *((p) + 2)) & 0377)) |
| 397 |
#define PUT_OFFSET_L(v) (char32)(((v) >> 8) & 0377) |
| 398 |
#define PUT_OFFSET_R(v) (char32) ((v) & 0377) |
| 399 |
#define GET_LOWER(p) ((( *((p) + NODE_SIZE) & 0377) << 8) + \ |
| 400 |
(( *((p) + NODE_SIZE+1)) & 0377)) |
| 401 |
#define GET_UPPER(p) ((( *((p) + NODE_SIZE+2) & 0377) << 8) + \ |
| 402 |
(( *((p) + NODE_SIZE+3)) & 0377)) |
| 403 |
|
| 404 |
/* Utility definitions. */ |
| 405 |
|
| 406 |
#define REG_FAIL(m) {*Error_Ptr = (m); return (NULL);} |
| 407 |
#define IS_QUANTIFIER(c) ((c) == '*' || (c) == '+' || \ |
| 408 |
(c) == '?' || (c) == Brace_Char) |
| 409 |
#define SET_BIT(i,n) ((i) |= (1 << ((n) - 1))) |
| 410 |
#define TEST_BIT(i,n) ((i) & (1 << ((n) - 1))) |
| 411 |
#define U_CHAR_AT(p) ((unsigned int) *(char32 *)(p)) |
| 412 |
|
| 413 |
/* Flags to be passed up and down via function parameters during compile. */ |
| 414 |
|
| 415 |
#define WORST 0 /* Worst case. No assumptions can be made.*/ |
| 416 |
#define HAS_WIDTH 1 /* Known never to match null string. */ |
| 417 |
#define SIMPLE 2 /* Simple enough to be STAR/PLUS operand. */ |
| 418 |
|
| 419 |
#define NO_PAREN 0 /* Only set by initial call to "chunk". */ |
| 420 |
#define PAREN 1 /* Used for normal capturing parentheses. */ |
| 421 |
#define NO_CAPTURE 2 /* Non-capturing parentheses (grouping only). */ |
| 422 |
#define INSENSITIVE 3 /* Case insensitive parenthetical construct */ |
| 423 |
#define SENSITIVE 4 /* Case sensitive parenthetical construct */ |
| 424 |
#define NEWLINE 5 /* Construct to match newlines in most cases */ |
| 425 |
#define NO_NEWLINE 6 /* Construct to match newlines normally */ |
| 426 |
|
| 427 |
#define REG_INFINITY 0UL |
| 428 |
#define REG_ZERO 0UL |
| 429 |
#define REG_ONE 1UL |
| 430 |
|
| 431 |
/* Flags for function shortcut_escape() */ |
| 432 |
|
| 433 |
#define CHECK_ESCAPE 0 /* Check an escape sequence for validity only. */ |
| 434 |
#define CHECK_CLASS_ESCAPE 1 /* Check the validity of an escape within a character class */ |
| 435 |
#define EMIT_NODE 2 /* Emit the appropriate node. */ |
| 436 |
|
| 437 |
/* Number of bytes to offset from the beginning of the regex program to the start |
| 438 |
of the actual compiled regex code, i.e. skipping over the MAGIC number and |
| 439 |
the two counters at the front. */ |
| 440 |
|
| 441 |
#define REGEX_START_OFFSET 3 |
| 442 |
|
| 443 |
#define MAX_COMPILED_SIZE 32767UL /* Largest size a compiled regex can be. |
| 444 |
Probably could be 65535UL. */ |
| 445 |
|
| 446 |
/* Global work variables for `CompileRE'. */ |
| 447 |
|
| 448 |
static char32 *Reg_Parse; /* Input scan ptr (scans user's regex) */ |
| 449 |
static int Total_Paren; /* Parentheses, (), counter. */ |
| 450 |
static int Num_Braces; /* Number of general {m,n} constructs. |
| 451 |
{m,n} quantifiers of SIMPLE atoms are |
| 452 |
not included in this count. */ |
| 453 |
static int Closed_Parens; /* Bit flags indicating () closure. */ |
| 454 |
static int Paren_Has_Width; /* Bit flags indicating ()'s that are |
| 455 |
known to not match the empty string */ |
| 456 |
static char32 Compute_Size; /* Address of this used as flag. */ |
| 457 |
static char32 *Code_Emit_Ptr; /* When Code_Emit_Ptr is set to |
| 458 |
&Compute_Size no code is emitted. |
| 459 |
Instead, the size of code that WOULD |
| 460 |
have been generated is accumulated in |
| 461 |
Reg_Size. Otherwise, Code_Emit_Ptr |
| 462 |
points to where compiled regex code is |
| 463 |
to be written. */ |
| 464 |
static unsigned long Reg_Size; /* Size of compiled regex code. */ |
| 465 |
static const char32 **Error_Ptr; /* Place to store error messages so |
| 466 |
they can be returned by `CompileRE' */ |
| 467 |
static char32 Error_Text [128];/* Sting to build error messages in. */ |
| 468 |
|
| 469 |
static int Is_Case_Insensitive; |
| 470 |
static int Match_Newline; |
| 471 |
|
| 472 |
static int Enable_Counting_Quantifier = 1; |
| 473 |
static char32 Brace_Char; |
| 474 |
static char32 Default_Meta_Char [] = { '{', '.', '*', '+', '?', '[', '(', '|', ')', '^', '<', '>', '$', '\0' }; |
| 475 |
static char32 *Meta_Char; |
| 476 |
|
| 477 |
typedef struct { |
| 478 |
long lower; |
| 479 |
long upper; |
| 480 |
} len_range; |
| 481 |
|
| 482 |
/* Forward declarations for functions used by `CompileRE'. */ |
| 483 |
|
| 484 |
static char32 *alternative (int *flag_param, len_range *range_param); |
| 485 |
static char32 *back_ref (char32 *c, int *flag_param, int emit); |
| 486 |
static char32 *chunk (int paren, int *flag_param, len_range *range_param); |
| 487 |
static void emit_byte (char32 c); |
| 488 |
static void emit_class_byte (char32 c); |
| 489 |
static char32 *emit_node (int op_code); |
| 490 |
static char32 *emit_special (char32 op_code, unsigned long test_val, int index); |
| 491 |
static char32 literal_escape (char32 c); |
| 492 |
static char32 numeric_escape (char32 c, char32 **parse); |
| 493 |
static char32 *atom (int *flag_param, len_range *range_param); |
| 494 |
static void reg_error (const char32_t *str); |
| 495 |
static char32 *insert (char32 op, char32 *opnd, long min, long max, int index); |
| 496 |
static char32 *next_ptr (char32 *ptr); |
| 497 |
static void offset_tail (char32 *ptr, int offset, char32 *val); |
| 498 |
static void branch_tail (char32 *ptr, int offset, char32 *val); |
| 499 |
static char32 *piece (int *flag_param, len_range *range_param); |
| 500 |
static void tail (char32 *search_from, char32 *point_t); |
| 501 |
static char32 *shortcut_escape (char32 c, int *flag_param, int emit); |
| 502 |
|
| 503 |
/*----------------------------------------------------------------------* |
| 504 |
* CompileRE |
| 505 |
* |
| 506 |
* Compiles a regular expression into the internal format used by |
| 507 |
* `ExecRE'. |
| 508 |
* |
| 509 |
* The default behaviour wrt. case sensitivity and newline matching can |
| 510 |
* be controlled through the defaultFlags argument (Markus Schwarzenberg). |
| 511 |
* Future extensions are possible by using other flag bits. |
| 512 |
* Note that currently only the case sensitivity flag is effectively used. |
| 513 |
* |
| 514 |
* Beware that the optimization and preparation code in here knows about |
| 515 |
* some of the structure of the compiled regexp. |
| 516 |
*----------------------------------------------------------------------*/ |
| 517 |
|
| 518 |
regexp *CompileRE_throwable (conststring32 expression, int defaultFlags) { |
| 519 |
conststring32 compileMessage; |
| 520 |
regexp *compiledRE = CompileRE (expression, & compileMessage, defaultFlags); |
| 521 |
if (compiledRE == NULL) |
| 522 |
Melder_throw (U"Regular expression: ", compileMessage, U" (", expression, U")."); |
| 523 |
return compiledRE; |
| 524 |
} |
| 525 |
|
| 526 |
regexp *CompileRE (conststring32 exp, conststring32 *errorText, int defaultFlags) { |
| 527 |
|
| 528 |
regexp *comp_regex = NULL; |
| 529 |
char32 *scan; |
| 530 |
int flags_local, pass; |
| 531 |
len_range range_local; |
| 532 |
|
| 533 |
if (Enable_Counting_Quantifier) { |
| 534 |
Brace_Char = '{'; |
| 535 |
Meta_Char = &Default_Meta_Char [0]; |
| 536 |
} else { |
| 537 |
Brace_Char = '*'; /* Bypass the '{' in */ |
| 538 |
Meta_Char = &Default_Meta_Char [1]; /* Default_Meta_Char */ |
| 539 |
} |
| 540 |
|
| 541 |
/* Set up errorText to receive failure reports. */ |
| 542 |
|
| 543 |
Error_Ptr = errorText; |
| 544 |
*Error_Ptr = U""; |
| 545 |
|
| 546 |
if (exp == NULL) { |
| 547 |
REG_FAIL (U"NULL argument, `CompileRE\'"); |
| 548 |
} |
| 549 |
|
| 550 |
Code_Emit_Ptr = &Compute_Size; |
| 551 |
Reg_Size = 0UL; |
| 552 |
|
| 553 |
/* We can't allocate space until we know how big the compiled form will be, |
| 554 |
but we can't compile it (and thus know how big it is) until we've got a |
| 555 |
place to put the code. So we cheat: we compile it twice, once with code |
| 556 |
generation turned off and size counting turned on, and once "for real". |
| 557 |
This also means that we don't allocate space until we are sure that the |
| 558 |
thing really will compile successfully, and we never have to move the |
| 559 |
code and thus invalidate pointers into it. (Note that it has to be in |
| 560 |
one piece because free() should be able to free it all.) */ |
| 561 |
|
| 562 |
for (pass = 1; pass <= 2; pass++) { |
| 563 |
/*-------------------------------------------* |
| 564 |
* FIRST PASS: Determine size and legality. * |
| 565 |
* SECOND PASS: Emit code. * |
| 566 |
*-------------------------------------------*/ |
| 567 |
|
| 568 |
/* Schwarzenberg: |
| 569 |
* If defaultFlags = 0 use standard defaults: |
| 570 |
* Is_Case_Insensitive: Case sensitive is the default |
| 571 |
* Match_Newline: Newlines are NOT matched by default |
| 572 |
* in character classes |
| 573 |
*/ |
| 574 |
Is_Case_Insensitive = ( (defaultFlags & REDFLT_CASE_INSENSITIVE) ? 1 : 0); |
| 575 |
Match_Newline = 0; /* ((defaultFlags & REDFLT_MATCH_NEWLINE) ? 1 : 0); |
| 576 |
Currently not used. Uncomment if needed. */ |
| 577 |
|
| 578 |
Reg_Parse = (char32 *) exp; |
| 579 |
Total_Paren = 1; |
| 580 |
Num_Braces = 0; |
| 581 |
Closed_Parens = 0; |
| 582 |
Paren_Has_Width = 0; |
| 583 |
|
| 584 |
emit_byte (MAGIC); |
| 585 |
emit_byte ('%'); /* Placeholder for num of capturing parentheses. */ |
| 586 |
emit_byte ('%'); /* Placeholder for num of general {m,n} constructs. */ |
| 587 |
|
| 588 |
if (chunk (NO_PAREN, &flags_local, &range_local) == NULL) { |
| 589 |
return (NULL); /* Something went wrong */ |
| 590 |
} |
| 591 |
if (pass == 1) { |
| 592 |
if (Reg_Size >= MAX_COMPILED_SIZE) { |
| 593 |
/* Too big for NEXT pointers NEXT_PTR_SIZE bytes long to span. |
| 594 |
This is a real issue since the first BRANCH node usually points |
| 595 |
to the end of the compiled regex code. */ |
| 596 |
|
| 597 |
Melder_sprint (Error_Text,128, U"regexp > ", MAX_COMPILED_SIZE, U" bytes"); |
| 598 |
REG_FAIL (Error_Text); |
| 599 |
} |
| 600 |
|
| 601 |
/* Allocate memory. */ |
| 602 |
|
| 603 |
comp_regex = (regexp *) malloc (sizeof (regexp) + Reg_Size * sizeof (char32)); |
| 604 |
|
| 605 |
if (comp_regex == NULL) { |
| 606 |
REG_FAIL (U"out of memory in `CompileRE\'"); |
| 607 |
} |
| 608 |
|
| 609 |
Code_Emit_Ptr = (char32 *) comp_regex->program; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
comp_regex->program [1] = (char32) Total_Paren - 1; |
| 614 |
comp_regex->program [2] = (char32) Num_Braces; |
| 615 |
|
| 616 |
/*----------------------------------------* |
| 617 |
* Dig out information for optimizations. * |
| 618 |
*----------------------------------------*/ |
| 619 |
|
| 620 |
comp_regex->match_start = '\0'; /* Worst-case defaults. */ |
| 621 |
comp_regex->anchor = 0; |
| 622 |
|
| 623 |
/* First BRANCH. */ |
| 624 |
|
| 625 |
scan = (char32 *) (comp_regex->program + REGEX_START_OFFSET); |
| 626 |
|
| 627 |
if (GET_OP_CODE (next_ptr (scan)) == END) { /* Only one top-level choice. */ |
| 628 |
scan = OPERAND (scan); |
| 629 |
|
| 630 |
/* Starting-point info. */ |
| 631 |
|
| 632 |
if (GET_OP_CODE (scan) == EXACTLY) { |
| 633 |
comp_regex->match_start = *OPERAND (scan); |
| 634 |
|
| 635 |
} else if (PLUS <= GET_OP_CODE (scan) && |
| 636 |
GET_OP_CODE (scan) <= LAZY_PLUS) { |
| 637 |
|
| 638 |
/* Allow x+ or x+? at the start of the regex to be |
| 639 |
optimized. */ |
| 640 |
|
| 641 |
if (GET_OP_CODE (scan + NODE_SIZE) == EXACTLY) { |
| 642 |
comp_regex->match_start = *OPERAND (scan + NODE_SIZE); |
| 643 |
} |
| 644 |
} else if (GET_OP_CODE (scan) == BOL) { |
| 645 |
comp_regex->anchor++; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
return (comp_regex); |
| 650 |
} |
| 651 |
|
| 652 |
/*----------------------------------------------------------------------* |
| 653 |
* chunk * |
| 654 |
* * |
| 655 |
* Process main body of regex or process a parenthesized "thing". * |
| 656 |
* * |
| 657 |
* Caller must absorb opening parenthesis. * |
| 658 |
* * |
| 659 |
* Combining parenthesis handling with the base level of regular * |
| 660 |
* expression is a trifle forced, but the need to tie the tails of the * |
| 661 |
* branches to what follows makes it hard to avoid. * |
| 662 |
*----------------------------------------------------------------------*/ |
| 663 |
|
| 664 |
static char32 *chunk (int paren, int *flag_param, len_range *range_param) { |
| 665 |
|
| 666 |
char32 *ret_val = NULL; |
| 667 |
char32 *this_branch; |
| 668 |
char32 *ender = NULL; |
| 669 |
int this_paren = 0; |
| 670 |
int flags_local, first = 1, zero_width, i; |
| 671 |
int old_sensitive = Is_Case_Insensitive; |
| 672 |
int old_newline = Match_Newline; |
| 673 |
len_range range_local; |
| 674 |
int look_only = 0; |
| 675 |
char32 *emit_look_behind_bounds = NULL; |
| 676 |
|
| 677 |
|
| 678 |
*flag_param = HAS_WIDTH; /* Tentatively. */ |
| 679 |
range_param->lower = 0; /* Idem */ |
| 680 |
range_param->upper = 0; |
| 681 |
|
| 682 |
/* Make an OPEN node, if parenthesized. */ |
| 683 |
|
| 684 |
if (paren == PAREN) { |
| 685 |
if (Total_Paren >= NSUBEXP) { |
| 686 |
Melder_sprint (Error_Text,128, U"number of ()'s > ", NSUBEXP); |
| 687 |
REG_FAIL (Error_Text) |
| 688 |
} |
| 689 |
|
| 690 |
this_paren = Total_Paren; Total_Paren++; |
| 691 |
ret_val = emit_node (OPEN + this_paren); |
| 692 |
} else if (paren == POS_AHEAD_OPEN || paren == NEG_AHEAD_OPEN) { |
| 693 |
*flag_param = WORST; /* Look ahead is zero width. */ |
| 694 |
look_only = 1; |
| 695 |
ret_val = emit_node (paren); |
| 696 |
} else if (paren == POS_BEHIND_OPEN || paren == NEG_BEHIND_OPEN) { |
| 697 |
*flag_param = WORST; /* Look behind is zero width. */ |
| 698 |
look_only = 1; |
| 699 |
/* We'll overwrite the zero length later on, so we save the ptr */ |
| 700 |
ret_val = emit_special (paren, 0, 0); |
| 701 |
emit_look_behind_bounds = ret_val + NODE_SIZE; |
| 702 |
} else if (paren == INSENSITIVE) { |
| 703 |
Is_Case_Insensitive = 1; |
| 704 |
} else if (paren == SENSITIVE) { |
| 705 |
Is_Case_Insensitive = 0; |
| 706 |
} else if (paren == NEWLINE) { |
| 707 |
Match_Newline = 1; |
| 708 |
} else if (paren == NO_NEWLINE) { |
| 709 |
Match_Newline = 0; |
| 710 |
} |
| 711 |
|
| 712 |
/* Pick up the branches, linking them together. */ |
| 713 |
|
| 714 |
do { |
| 715 |
this_branch = alternative (&flags_local, &range_local); |
| 716 |
|
| 717 |
if (this_branch == NULL) { |
| 718 |
return (NULL); |
| 719 |
} |
| 720 |
|
| 721 |
if (first) { |
| 722 |
first = 0; |
| 723 |
*range_param = range_local; |
| 724 |
if (ret_val == NULL) { |
| 725 |
ret_val = this_branch; |
| 726 |
} |
| 727 |
} else if (range_param->lower >= 0) { |
| 728 |
if (range_local.lower >= 0) { |
| 729 |
if (range_local.lower < range_param->lower) { |
| 730 |
range_param->lower = range_local.lower; |
| 731 |
} |
| 732 |
if (range_local.upper > range_param->upper) { |
| 733 |
range_param->upper = range_local.upper; |
| 734 |
} |
| 735 |
} else { |
| 736 |
range_param->lower = -1; /* Branches have different lengths */ |
| 737 |
range_param->upper = -1; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
tail (ret_val, this_branch); /* Connect BRANCH -> BRANCH. */ |
| 742 |
|
| 743 |
/* If any alternative could be zero width, consider the whole |
| 744 |
parenthisized thing to be zero width. */ |
| 745 |
|
| 746 |
if (! (flags_local & HAS_WIDTH)) { |
| 747 |
*flag_param &= ~HAS_WIDTH; |
| 748 |
} |
| 749 |
|
| 750 |
/* Are there more alternatives to process? */ |
| 751 |
|
| 752 |
if (*Reg_Parse != '|') { |
| 753 |
break; |
| 754 |
} |
| 755 |
|
| 756 |
Reg_Parse++; |
| 757 |
} while (1); |
| 758 |
|
| 759 |
/* Make a closing node, and hook it on the end. */ |
| 760 |
|
| 761 |
if (paren == PAREN) { |
| 762 |
ender = emit_node (CLOSE + this_paren); |
| 763 |
|
| 764 |
} else if (paren == NO_PAREN) { |
| 765 |
ender = emit_node (END); |
| 766 |
|
| 767 |
} else if (paren == POS_AHEAD_OPEN || paren == NEG_AHEAD_OPEN) { |
| 768 |
ender = emit_node (LOOK_AHEAD_CLOSE); |
| 769 |
|
| 770 |
} else if (paren == POS_BEHIND_OPEN || paren == NEG_BEHIND_OPEN) { |
| 771 |
ender = emit_node (LOOK_BEHIND_CLOSE); |
| 772 |
|
| 773 |
} else { |
| 774 |
ender = emit_node (NOTHING); |
| 775 |
} |
| 776 |
|
| 777 |
tail (ret_val, ender); |
| 778 |
|
| 779 |
/* Hook the tails of the branch alternatives to the closing node. */ |
| 780 |
|
| 781 |
for (this_branch = ret_val; this_branch != NULL;) { |
| 782 |
branch_tail (this_branch, NODE_SIZE, ender); |
| 783 |
this_branch = next_ptr (this_branch); |
| 784 |
} |
| 785 |
|
| 786 |
/* Check for proper termination. */ |
| 787 |
|
| 788 |
if (paren != NO_PAREN && *Reg_Parse++ != ')') { |
| 789 |
REG_FAIL (U"missing right parenthesis \')\'"); |
| 790 |
} else if (paren == NO_PAREN && *Reg_Parse != '\0') { |
| 791 |
if (*Reg_Parse == ')') { |
| 792 |
REG_FAIL (U"missing left parenthesis \'(\'"); |
| 793 |
} else { |
| 794 |
REG_FAIL (U"junk on end"); /* "Can't happen" - NOTREACHED */ |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
/* Check whether look behind has a fixed size */ |
| 799 |
|
| 800 |
if (emit_look_behind_bounds) { |
| 801 |
if (range_param->lower < 0) { |
| 802 |
REG_FAIL (U"look-behind does not have a bounded size"); |
| 803 |
} |
| 804 |
if (range_param->upper > 65535L) { |
| 805 |
REG_FAIL (U"max. look-behind size is too large (>65535)") |
| 806 |
} |
| 807 |
if (Code_Emit_Ptr != &Compute_Size) { |
| 808 |
*emit_look_behind_bounds++ = PUT_OFFSET_L (range_param->lower); |
| 809 |
*emit_look_behind_bounds++ = PUT_OFFSET_R (range_param->lower); |
| 810 |
*emit_look_behind_bounds++ = PUT_OFFSET_L (range_param->upper); |
| 811 |
*emit_look_behind_bounds = PUT_OFFSET_R (range_param->upper); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
/* For look ahead/behind, the length should be set to zero again */ |
| 816 |
if (look_only) { |
| 817 |
range_param->lower = 0; |
| 818 |
range_param->upper = 0; |
| 819 |
} |
| 820 |
|
| 821 |
zero_width = 0; |
| 822 |
|
| 823 |
/* Set a bit in Closed_Parens to let future calls to function `back_ref' |
| 824 |
know that we have closed this set of parentheses. */ |
| 825 |
|
| 826 |
if (paren == PAREN && this_paren <= (int) sizeof (Closed_Parens) * CHAR_BIT) { |
| 827 |
SET_BIT (Closed_Parens, this_paren); |
| 828 |
|
| 829 |
/* Determine if a parenthesized expression is modified by a quantifier |
| 830 |
that can have zero width. */ |
| 831 |
|
| 832 |
if (* (Reg_Parse) == '?' || * (Reg_Parse) == '*') { |
| 833 |
zero_width++; |
| 834 |
} else if (* (Reg_Parse) == '{' && Brace_Char == '{') { |
| 835 |
if (* (Reg_Parse + 1) == ',' || * (Reg_Parse + 1) == '}') { |
| 836 |
zero_width++; |
| 837 |
} else if (* (Reg_Parse + 1) == '0') { |
| 838 |
i = 2; |
| 839 |
|
| 840 |
while (* (Reg_Parse + i) == '0') { |
| 841 |
i++; |
| 842 |
} |
| 843 |
|
| 844 |
if (* (Reg_Parse + i) == ',') { |
| 845 |
zero_width++; |
| 846 |
} |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
/* If this set of parentheses is known to never match the empty string, set |
| 852 |
a bit in Paren_Has_Width to let future calls to function back_ref know |
| 853 |
that this set of parentheses has non-zero width. This will allow star |
| 854 |
(*) or question (?) quantifiers to be aplied to a back-reference that |
| 855 |
refers to this set of parentheses. */ |
| 856 |
|
| 857 |
if ( (*flag_param & HAS_WIDTH) && |
| 858 |
paren == PAREN && |
| 859 |
!zero_width && |
| 860 |
this_paren <= (int) (sizeof (Paren_Has_Width) * CHAR_BIT)) { |
| 861 |
|
| 862 |
SET_BIT (Paren_Has_Width, this_paren); |
| 863 |
} |
| 864 |
|
| 865 |
Is_Case_Insensitive = old_sensitive; |
| 866 |
Match_Newline = old_newline; |
| 867 |
|
| 868 |
return (ret_val); |
| 869 |
} |
| 870 |
|
| 871 |
/*----------------------------------------------------------------------* |
| 872 |
* alternative |
| 873 |
* |
| 874 |
* Processes one alternative of an '|' operator. Connects the NEXT |
| 875 |
* pointers of each regex atom together sequentialy. |
| 876 |
*----------------------------------------------------------------------*/ |
| 877 |
|
| 878 |
static char32 *alternative (int *flag_param, len_range *range_param) { |
| 879 |
|
| 880 |
char32 *ret_val; |
| 881 |
char32 *chain; |
| 882 |
char32 *latest; |
| 883 |
int flags_local; |
| 884 |
len_range range_local; |
| 885 |
|
| 886 |
*flag_param = WORST; /* Tentatively. */ |
| 887 |
range_param->lower = 0; /* Idem */ |
| 888 |
range_param->upper = 0; |
| 889 |
|
| 890 |
ret_val = emit_node (BRANCH); |
| 891 |
chain = NULL; |
| 892 |
|
| 893 |
/* Loop until we hit the start of the next alternative, the end of this set |
| 894 |
of alternatives (end of parentheses), or the end of the regex. */ |
| 895 |
|
| 896 |
while (*Reg_Parse != '|' && *Reg_Parse != ')' && *Reg_Parse != '\0') { |
| 897 |
latest = piece (&flags_local, &range_local); |
| 898 |
|
| 899 |
if (latest == NULL) { |
| 900 |
return (NULL); /* Something went wrong. */ |
| 901 |
} |
| 902 |
|
| 903 |
*flag_param |= flags_local & HAS_WIDTH; |
| 904 |
if (range_local.lower < 0) { |
| 905 |
/* Not a fixed length */ |
| 906 |
range_param->lower = -1; |
| 907 |
range_param->upper = -1; |
| 908 |
} else if (range_param->lower >= 0) { |
| 909 |
range_param->lower += range_local.lower; |
| 910 |
range_param->upper += range_local.upper; |
| 911 |
} |
| 912 |
|
| 913 |
if (chain != NULL) { /* Connect the regex atoms together sequentialy. */ |
| 914 |
tail (chain, latest); |
| 915 |
} |
| 916 |
|
| 917 |
chain = latest; |
| 918 |
} |
| 919 |
|
| 920 |
if (chain == NULL) { /* Loop ran zero times. */ |
| 921 |
(void) emit_node (NOTHING); |
| 922 |
} |
| 923 |
|
| 924 |
return (ret_val); |
| 925 |
} |
| 926 |
|
| 927 |
/*----------------------------------------------------------------------* |
| 928 |
* piece - something followed by possible '*', '+', '?', or "{m,n}" |
| 929 |
* |
| 930 |
* Note that the branching code sequences used for the general cases of |
| 931 |
* *, +. ?, and {m,n} are somewhat optimized: they use the same |
| 932 |
* NOTHING node as both the endmarker for their branch list and the |
| 933 |
* body of the last branch. It might seem that this node could be |
| 934 |
* dispensed with entirely, but the endmarker role is not redundant. |
| 935 |
*----------------------------------------------------------------------*/ |
| 936 |
|
| 937 |
static char32 *piece (int *flag_param, len_range *range_param) { |
| 938 |
|
| 939 |
char32 *ret_val; |
| 940 |
char32 *next; |
| 941 |
char32 op_code; |
| 942 |
unsigned long min_max [2] = {REG_ZERO, REG_INFINITY}; |
| 943 |
int flags_local, i, brace_present = 0; |
| 944 |
int lazy = 0, comma_present = 0; |
| 945 |
int digit_present [2] = {0, 0}; |
| 946 |
len_range range_local; |
| 947 |
|
| 948 |
ret_val = atom (&flags_local, &range_local); |
| 949 |
|
| 950 |
if (ret_val == NULL) { |
| 951 |
return (NULL); /* Something went wrong. */ |
| 952 |
} |
| 953 |
|
| 954 |
op_code = *Reg_Parse; |
| 955 |
|
| 956 |
if (!IS_QUANTIFIER (op_code)) { |
| 957 |
*flag_param = flags_local; |
| 958 |
*range_param = range_local; |
| 959 |
return (ret_val); |
| 960 |
} else if (op_code == '{') { /* {n,m} quantifier present */ |
| 961 |
brace_present++; |
| 962 |
Reg_Parse++; |
| 963 |
|
| 964 |
/* This code will allow specifying a counting range in any of the |
| 965 |
following forms: |
| 966 |
|
| 967 |
{m,n} between m and n. |
| 968 |
{,n} same as {0,n} or between 0 and infinity. |
| 969 |
{m,} same as {m,0} or between m and infinity. |
| 970 |
{m} same as {m,m} or exactly m. |
| 971 |
{,} same as {0,0} or between 0 and infinity or just '*'. |
| 972 |
{} same as {0,0} or between 0 and infinity or just '*'. |
| 973 |
|
| 974 |
Note that specifying a max of zero, {m,0} is not allowed in the regex |
| 975 |
itself, but it is implemented internally that way to support '*', '+', |
| 976 |
and {min,} constructs and signals an unlimited number. */ |
| 977 |
|
| 978 |
for (i = 0; i < 2; i++) { |
| 979 |
/* Look for digits of number and convert as we go. The numeric maximum |
| 980 |
value for max and min of 65,535 is due to using 2 bytes to store |
| 981 |
each value in the compiled regex code. */ |
| 982 |
|
| 983 |
while (Melder_isAsciiDecimalNumber (*Reg_Parse)) { |
| 984 |
/* (6553 * 10 + 6) > 65535 (16 bit max) */ |
| 985 |
|
| 986 |
if ( (min_max [i] == 6553UL && (*Reg_Parse - '0') <= 5) || |
| 987 |
(min_max [i] <= 6552UL)) { |
| 988 |
|
| 989 |
min_max [i] = (min_max [i] * 10UL) + |
| 990 |
(unsigned long) (*Reg_Parse - '0'); |
| 991 |
Reg_Parse++; |
| 992 |
|
| 993 |
digit_present [i]++; |
| 994 |
} else { |
| 995 |
if (i == 0) { |
| 996 |
Melder_sprint (Error_Text,128, U"min operand of {", min_max [0], *Reg_Parse, U",???} > 65535"); |
| 997 |
} else { |
| 998 |
Melder_sprint (Error_Text,128, U"max operand of {", min_max [0], U",", min_max [1], *Reg_Parse, U"} > 65535"); |
| 999 |
} |
| 1000 |
|
| 1001 |
REG_FAIL (Error_Text); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (!comma_present && *Reg_Parse == ',') { |
| 1006 |
comma_present++; |
| 1007 |
Reg_Parse++; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
/* A max of zero cannot be specified directly in the regex since it would |
| 1012 |
signal a max of infinity. This code specifically disallows `{0,0}', |
| 1013 |
`{,0}', and `{0}' which really means nothing to humans but would be |
| 1014 |
interpreted as `{0,infinity}' or `*' if we didn't make this check. */ |
| 1015 |
|
| 1016 |
if (digit_present [0] && (min_max [0] == REG_ZERO) && !comma_present) { |
| 1017 |
|
| 1018 |
REG_FAIL (U"{0} is an invalid range"); |
| 1019 |
} else if (digit_present [0] && (min_max [0] == REG_ZERO) && |
| 1020 |
digit_present [1] && (min_max [1] == REG_ZERO)) { |
| 1021 |
|
| 1022 |
REG_FAIL (U"{0,0} is an invalid range"); |
| 1023 |
} else if (digit_present [1] && (min_max [1] == REG_ZERO)) { |
| 1024 |
if (digit_present [0]) { |
| 1025 |
Melder_sprint (Error_Text,128, U"{", min_max [0], U",0} is an invalid range"); |
| 1026 |
REG_FAIL (Error_Text); |
| 1027 |
} else { |
| 1028 |
REG_FAIL (U"{,0} is an invalid range"); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
if (!comma_present) { |
| 1033 |
min_max [1] = min_max [0]; |
| 1034 |
} /* {x} means {x,x} */ |
| 1035 |
|
| 1036 |
if (*Reg_Parse != '}') { |
| 1037 |
REG_FAIL (U"{m,n} specification missing right \'}\'"); |
| 1038 |
|
| 1039 |
} else if (min_max [1] != REG_INFINITY && min_max [0] > min_max [1]) { |
| 1040 |
/* Disallow a backward range. */ |
| 1041 |
|
| 1042 |
Melder_sprint (Error_Text,128, U"{", min_max [0], U",", min_max [1], U"} is an invalid range"); |
| 1043 |
REG_FAIL (Error_Text); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
Reg_Parse++; |
| 1048 |
|
| 1049 |
/* Check for a minimal matching (non-greedy or "lazy") specification. */ |
| 1050 |
|
| 1051 |
if (*Reg_Parse == '?') { |
| 1052 |
lazy = 1; |
| 1053 |
Reg_Parse++; |
| 1054 |
} |
| 1055 |
|
| 1056 |
/* Avoid overhead of counting if possible */ |
| 1057 |
|
| 1058 |
if (op_code == '{') { |
| 1059 |
if (min_max [0] == REG_ZERO && min_max [1] == REG_INFINITY) { |
| 1060 |
op_code = '*'; |
| 1061 |
} else if (min_max [0] == REG_ONE && min_max [1] == REG_INFINITY) { |
| 1062 |
op_code = '+'; |
| 1063 |
} else if (min_max [0] == REG_ZERO && min_max [1] == REG_ONE) { |
| 1064 |
op_code = '?'; |
| 1065 |
} else if (min_max [0] == REG_ONE && min_max [1] == REG_ONE) { |
| 1066 |
/* "x{1,1}" is the same as "x". No need to pollute the compiled |
| 1067 |
regex with such nonsense. */ |
| 1068 |
|
| 1069 |
*flag_param = flags_local; |
| 1070 |
*range_param = range_local; |
| 1071 |
return (ret_val); |
| 1072 |
} else if (Num_Braces > (int) UCHAR_MAX) { |
| 1073 |
Melder_sprint (Error_Text,128, U"number of {m,n} constructs > ", UCHAR_MAX); |
| 1074 |
REG_FAIL (Error_Text); |
| 1075 |
} |
| 1076 |
} |
| 1077 |
|
| 1078 |
if (op_code == '+') { |
| 1079 |
min_max [0] = REG_ONE; |
| 1080 |
} |
| 1081 |
if (op_code == '?') { |
| 1082 |
min_max [1] = REG_ONE; |
| 1083 |
} |
| 1084 |
|
| 1085 |
/* It is dangerous to apply certain quantifiers to a possibly zero width |
| 1086 |
item. */ |
| 1087 |
|
| 1088 |
if (! (flags_local & HAS_WIDTH)) { |
| 1089 |
if (brace_present) { |
| 1090 |
Melder_sprint (Error_Text,128, U"{", min_max [0], U",", min_max [1], U"} operand could be empty"); |
| 1091 |
} else { |
| 1092 |
Melder_sprint (Error_Text,128, op_code, U" operand could be empty"); |
| 1093 |
} |
| 1094 |
|
| 1095 |
REG_FAIL (Error_Text); |
| 1096 |
} |
| 1097 |
|
| 1098 |
*flag_param = (min_max [0] > REG_ZERO) ? (WORST | HAS_WIDTH) : WORST; |
| 1099 |
if (range_local.lower >= 0) { |
| 1100 |
if (min_max[1] != REG_INFINITY) { |
| 1101 |
range_param->lower = range_local.lower * min_max[0]; |
| 1102 |
range_param->upper = range_local.upper * min_max[1]; |
| 1103 |
} else { |
| 1104 |
range_param->lower = -1; /* Not a fixed-size length */ |
| 1105 |
range_param->upper = -1; |
| 1106 |
} |
| 1107 |
} else { |
| 1108 |
range_param->lower = -1; /* Not a fixed-size length */ |
| 1109 |
range_param->upper = -1; |
| 1110 |
} |
| 1111 |
|
| 1112 |
/*---------------------------------------------------------------------* |
| 1113 |
* Symbol Legend For Node Structure Diagrams |
| 1114 |
*---------------------------------------------------------------------* |
| 1115 |
* (...) = general grouped thing |
| 1116 |
* B = (B)ranch, K = bac(K), N = (N)othing |
| 1117 |
* I = (I)nitialize count, C = Increment (C)ount |
| 1118 |
* T~m = (T)est against mini(m)um- go to NEXT pointer if >= operand |
| 1119 |
* T~x = (T)est against ma(x)imum- go to NEXT pointer if >= operand |
| 1120 |
* '~' = NEXT pointer, \___| = forward pointer, |___/ = Backward pointer |
| 1121 |
*---------------------------------------------------------------------*/ |
| 1122 |
|
| 1123 |
if (op_code == '*' && (flags_local & SIMPLE)) { |
| 1124 |
insert ( (lazy ? LAZY_STAR : STAR), ret_val, 0UL, 0UL, 0); |
| 1125 |
|
| 1126 |
} else if (op_code == '+' && (flags_local & SIMPLE)) { |
| 1127 |
insert (lazy ? LAZY_PLUS : PLUS, ret_val, 0UL, 0UL, 0); |
| 1128 |
|
| 1129 |
} else if (op_code == '?' && (flags_local & SIMPLE)) { |
| 1130 |
insert (lazy ? LAZY_QUESTION : QUESTION, ret_val, 0UL, 0UL, 0); |
| 1131 |
|
| 1132 |
} else if (op_code == '{' && (flags_local & SIMPLE)) { |
| 1133 |
insert (lazy ? LAZY_BRACE : BRACE, ret_val, min_max [0], min_max [1], 0); |
| 1134 |
|
| 1135 |
} else if ( (op_code == '*' || op_code == '+') && lazy) { |
| 1136 |
/* Node structure for (x)*? Node structure for (x)+? construct. |
| 1137 |
* construct. (Same as (x)*? except for initial |
| 1138 |
* forward jump into parenthesis.) |
| 1139 |
* |
| 1140 |
* ___6____ |
| 1141 |
* _______5_______ /________|______ |
| 1142 |
* | _4__ 1_\ /| ____ | _\ |
| 1143 |
* |/ | / |\ / |/ | | / |\ |
| 1144 |
* B~ N~ B~ (...)~ K~ N~ N~ B~ N~ B~ (...)~ K~ N~ |
| 1145 |
* \ \___2_______| \ \___________| |
| 1146 |
* \_____3_______| \_____________| |
| 1147 |
* |
| 1148 |
*/ |
| 1149 |
|
| 1150 |
tail (ret_val, emit_node (BACK)); /* 1 */ |
| 1151 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 2,4 */ |
| 1152 |
(void) insert (NOTHING, ret_val, 0UL, 0UL, 0); /* 3 */ |
| 1153 |
|
| 1154 |
next = emit_node (NOTHING); /* 2,3 */ |
| 1155 |
|
| 1156 |
offset_tail (ret_val, NODE_SIZE, next); /* 2 */ |
| 1157 |
tail (ret_val, next); /* 3 */ |
| 1158 |
insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 4,5 */ |
| 1159 |
tail (ret_val, ret_val + (2 * NODE_SIZE)); /* 4 */ |
| 1160 |
offset_tail (ret_val, 3 * NODE_SIZE, ret_val); /* 5 */ |
| 1161 |
|
| 1162 |
if (op_code == '+') { |
| 1163 |
insert (NOTHING, ret_val, 0UL, 0UL, 0); /* 6 */ |
| 1164 |
tail (ret_val, ret_val + (4 * NODE_SIZE)); /* 6 */ |
| 1165 |
} |
| 1166 |
} else if (op_code == '*') { |
| 1167 |
/* Node structure for (x)* construct. |
| 1168 |
* ____1_____ |
| 1169 |
* | \ |
| 1170 |
* B~ (...)~ K~ B~ N~ |
| 1171 |
* \ \_|2 |\_| |
| 1172 |
* \__3_______| 4 |
| 1173 |
*/ |
| 1174 |
|
| 1175 |
insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 1,3 */ |
| 1176 |
offset_tail (ret_val, NODE_SIZE, emit_node (BACK)); /* 2 */ |
| 1177 |
offset_tail (ret_val, NODE_SIZE, ret_val); /* 1 */ |
| 1178 |
tail (ret_val, emit_node (BRANCH)); /* 3 */ |
| 1179 |
tail (ret_val, emit_node (NOTHING)); /* 4 */ |
| 1180 |
} else if (op_code == '+') { |
| 1181 |
/* Node structure for (x)+ construct. |
| 1182 |
* |
| 1183 |
* ____2_____ |
| 1184 |
* | \ |
| 1185 |
* (...)~ B~ K~ B~ N~ |
| 1186 |
* \_|\____|\_| |
| 1187 |
* 1 3 4 |
| 1188 |
*/ |
| 1189 |
|
| 1190 |
next = emit_node (BRANCH); /* 1 */ |
| 1191 |
|
| 1192 |
tail (ret_val, next); /* 1 */ |
| 1193 |
tail (emit_node (BACK), ret_val); /* 2 */ |
| 1194 |
tail (next, emit_node (BRANCH)); /* 3 */ |
| 1195 |
tail (ret_val, emit_node (NOTHING)); /* 4 */ |
| 1196 |
} else if (op_code == '?' && lazy) { |
| 1197 |
/* Node structure for (x)?? construct. |
| 1198 |
* _4__ 1_ |
| 1199 |
* / | / | |
| 1200 |
* B~ N~ B~ (...)~ N~ |
| 1201 |
* \ \___2____| |
| 1202 |
* \_____3____| |
| 1203 |
*/ |
| 1204 |
|
| 1205 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 2,4 */ |
| 1206 |
(void) insert (NOTHING, ret_val, 0UL, 0UL, 0); /* 3 */ |
| 1207 |
|
| 1208 |
next = emit_node (NOTHING); /* 1,2,3 */ |
| 1209 |
|
| 1210 |
offset_tail (ret_val, 2 * NODE_SIZE, next); /* 1 */ |
| 1211 |
offset_tail (ret_val, NODE_SIZE, next); /* 2 */ |
| 1212 |
tail (ret_val, next); /* 3 */ |
| 1213 |
insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 4 */ |
| 1214 |
tail (ret_val, (ret_val + (2 * NODE_SIZE))); /* 4 */ |
| 1215 |
|
| 1216 |
} else if (op_code == '?') { |
| 1217 |
/* Node structure for (x)? construct. |
| 1218 |
* ___1____ _2 |
| 1219 |
* / |/ | |
| 1220 |
* B~ (...)~ B~ N~ |
| 1221 |
* \__3_| |
| 1222 |
*/ |
| 1223 |
|
| 1224 |
insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 1 */ |
| 1225 |
tail (ret_val, emit_node (BRANCH)); /* 1 */ |
| 1226 |
|
| 1227 |
next = emit_node (NOTHING); /* 2,3 */ |
| 1228 |
|
| 1229 |
tail (ret_val, next); /* 2 */ |
| 1230 |
offset_tail (ret_val, NODE_SIZE, next); /* 3 */ |
| 1231 |
} else if (op_code == '{' && min_max [0] == min_max [1]) { |
| 1232 |
/* Node structure for (x){m}, (x){m}?, (x){m,m}, or (x){m,m}? constructs. |
| 1233 |
* Note that minimal and maximal matching mean the same thing when we |
| 1234 |
* specify the minimum and maximum to be the same value. |
| 1235 |
* _______3_____ |
| 1236 |
* | 1_ _2 \ |
| 1237 |
* | / |/ | \ |
| 1238 |
* I~ (...)~ C~ T~m K~ N~ |
| 1239 |
* \_| \_____| |
| 1240 |
* 5 4 |
| 1241 |
*/ |
| 1242 |
|
| 1243 |
tail (ret_val, emit_special (INC_COUNT, 0UL, Num_Braces)); /* 1 */ |
| 1244 |
tail (ret_val, emit_special (TEST_COUNT, min_max [0], Num_Braces));/* 2 */ |
| 1245 |
tail (emit_node (BACK), ret_val); /* 3 */ |
| 1246 |
tail (ret_val, emit_node (NOTHING)); /* 4 */ |
| 1247 |
|
| 1248 |
next = insert (INIT_COUNT, ret_val, 0UL, 0UL, Num_Braces); /* 5 */ |
| 1249 |
|
| 1250 |
tail (ret_val, next); /* 5 */ |
| 1251 |
|
| 1252 |
Num_Braces++; |
| 1253 |
} else if (op_code == '{' && lazy) { |
| 1254 |
if (min_max [0] == REG_ZERO && min_max [1] != REG_INFINITY) { |
| 1255 |
/* Node structure for (x){0,n}? or {,n}? construct. |
| 1256 |
* _________3____________ |
| 1257 |
* 8_| _4__ 1_ _2 \ |
| 1258 |
* / |/ | / |/ | \ |
| 1259 |
* I~ B~ N~ B~ (...)~ C~ T~x K~ N~ |
| 1260 |
* \ \ \__7__| |
| 1261 |
* \ \_________6_______| |
| 1262 |
* \______5____________| |
| 1263 |
*/ |
| 1264 |
|
| 1265 |
tail (ret_val, emit_special (INC_COUNT, 0UL, Num_Braces)); /* 1 */ |
| 1266 |
|
| 1267 |
next = emit_special (TEST_COUNT, min_max [0], Num_Braces); /* 2,7 */ |
| 1268 |
|
| 1269 |
tail (ret_val, next); /* 2 */ |
| 1270 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, Num_Braces); /* 4,6 */ |
| 1271 |
(void) insert (NOTHING, ret_val, 0UL, 0UL, Num_Braces); /* 5 */ |
| 1272 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, Num_Braces); /* 3,4,8 */ |
| 1273 |
tail (emit_node (BACK), ret_val); /* 3 */ |
| 1274 |
tail (ret_val, ret_val + (2 * NODE_SIZE)); /* 4 */ |
| 1275 |
|
| 1276 |
next = emit_node (NOTHING); /* 5,6,7 */ |
| 1277 |
|
| 1278 |
offset_tail (ret_val, NODE_SIZE, next); /* 5 */ |
| 1279 |
offset_tail (ret_val, 2 * NODE_SIZE, next); /* 6 */ |
| 1280 |
offset_tail (ret_val, 3 * NODE_SIZE, next); /* 7 */ |
| 1281 |
|
| 1282 |
next = insert (INIT_COUNT, ret_val, 0UL, 0UL, Num_Braces); /* 8 */ |
| 1283 |
|
| 1284 |
tail (ret_val, next); /* 8 */ |
| 1285 |
|
| 1286 |
} else if (min_max [0] > REG_ZERO && min_max [1] == REG_INFINITY) { |
| 1287 |
/* Node structure for (x){m,}? construct. |
| 1288 |
* ______8_________________ |
| 1289 |
* | _______3_____ \ |
| 1290 |
* | _7__ | 1_ _2 \ \ |
| 1291 |
* |/ | | / |/ | \ \ |
| 1292 |
* I~ B~ N~ B~ (...)~ C~ T~m K~ K~ N~ |
| 1293 |
* \_____\__\_| \_4___| | |
| 1294 |
* 9 \ \_________5__________| |
| 1295 |
* \_______6______________| |
| 1296 |
*/ |
| 1297 |
|
| 1298 |
tail (ret_val, emit_special (INC_COUNT, 0UL, Num_Braces)); /* 1 */ |
| 1299 |
|
| 1300 |
next = emit_special (TEST_COUNT, min_max [0], Num_Braces); /* 2,4 */ |
| 1301 |
|
| 1302 |
tail (ret_val, next); /* 2 */ |
| 1303 |
tail (emit_node (BACK), ret_val); /* 3 */ |
| 1304 |
tail (ret_val, emit_node (BACK)); /* 4 */ |
| 1305 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 5,7 */ |
| 1306 |
(void) insert (NOTHING, ret_val, 0UL, 0UL, 0); /* 6 */ |
| 1307 |
|
| 1308 |
next = emit_node (NOTHING); /* 5,6 */ |
| 1309 |
|
| 1310 |
offset_tail (ret_val, NODE_SIZE, next); /* 5 */ |
| 1311 |
tail (ret_val, next); /* 6 */ |
| 1312 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 7,8 */ |
| 1313 |
tail (ret_val, ret_val + (2 * NODE_SIZE)); /* 7 */ |
| 1314 |
offset_tail (ret_val, 3 * NODE_SIZE, ret_val); /* 8 */ |
| 1315 |
(void) insert (INIT_COUNT, ret_val, 0UL, 0UL, Num_Braces); /* 9 */ |
| 1316 |
tail (ret_val, ret_val + INDEX_SIZE + (4 * NODE_SIZE)); /* 9 */ |
| 1317 |
|
| 1318 |
} else { |
| 1319 |
/* Node structure for (x){m,n}? construct. |
| 1320 |
* ______9_____________________ |
| 1321 |
* | _____________3___ \ |
| 1322 |
* | __8_ | 1_ _2 \ \ |
| 1323 |
* |/ | | / |/ | \ \ |
| 1324 |
* I~ B~ N~ B~ (...)~ C~ T~x T~m K~ K~ N~ |
| 1325 |
* \_____\__\_| \ \__4__| | |
| 1326 |
* 10 \ \ \_7_________| |
| 1327 |
* \ \_________6_____________| |
| 1328 |
* \_______5_________________| |
| 1329 |
*/ |
| 1330 |
|
| 1331 |
tail (ret_val, emit_special (INC_COUNT, 0UL, Num_Braces)); /* 1 */ |
| 1332 |
|
| 1333 |
next = emit_special (TEST_COUNT, min_max [1], Num_Braces); /* 2,7 */ |
| 1334 |
|
| 1335 |
tail (ret_val, next); /* 2 */ |
| 1336 |
|
| 1337 |
next = emit_special (TEST_COUNT, min_max [0], Num_Braces); /* 4 */ |
| 1338 |
|
| 1339 |
tail (emit_node (BACK), ret_val); /* 3 */ |
| 1340 |
tail (next, emit_node (BACK)); /* 4 */ |
| 1341 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 6,8 */ |
| 1342 |
(void) insert (NOTHING, ret_val, 0UL, 0UL, 0); /* 5 */ |
| 1343 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 8,9 */ |
| 1344 |
|
| 1345 |
next = emit_node (NOTHING); /* 5,6,7 */ |
| 1346 |
|
| 1347 |
offset_tail (ret_val, NODE_SIZE, next); /* 5 */ |
| 1348 |
offset_tail (ret_val, 2 * NODE_SIZE, next); /* 6 */ |
| 1349 |
offset_tail (ret_val, 3 * NODE_SIZE, next); /* 7 */ |
| 1350 |
tail (ret_val, ret_val + (2 * NODE_SIZE)); /* 8 */ |
| 1351 |
offset_tail (next, -NODE_SIZE, ret_val); /* 9 */ |
| 1352 |
insert (INIT_COUNT, ret_val, 0UL, 0UL, Num_Braces); /* 10 */ |
| 1353 |
tail (ret_val, ret_val + INDEX_SIZE + (4 * NODE_SIZE)); /* 10 */ |
| 1354 |
} |
| 1355 |
|
| 1356 |
Num_Braces++; |
| 1357 |
} else if (op_code == '{') { |
| 1358 |
if (min_max [0] == REG_ZERO && min_max [1] != REG_INFINITY) { |
| 1359 |
/* Node structure for (x){0,n} or (x){,n} construct. |
| 1360 |
* |
| 1361 |
* ___3____________ |
| 1362 |
* | 1_ _2 \ 5_ |
| 1363 |
* | / |/ | \ / | |
| 1364 |
* I~ B~ (...)~ C~ T~x K~ B~ N~ |
| 1365 |
* \_|\ \_6___|__| |
| 1366 |
* 7 \________4________| |
| 1367 |
*/ |
| 1368 |
|
| 1369 |
tail (ret_val, emit_special (INC_COUNT, 0UL, Num_Braces)); /* 1 */ |
| 1370 |
|
| 1371 |
next = emit_special (TEST_COUNT, min_max [1], Num_Braces); /* 2,6 */ |
| 1372 |
|
| 1373 |
tail (ret_val, next); /* 2 */ |
| 1374 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 3,4,7 */ |
| 1375 |
tail (emit_node (BACK), ret_val); /* 3 */ |
| 1376 |
|
| 1377 |
next = emit_node (BRANCH); /* 4,5 */ |
| 1378 |
|
| 1379 |
tail (ret_val, next); /* 4 */ |
| 1380 |
tail (next, emit_node (NOTHING)); /* 5,6 */ |
| 1381 |
offset_tail (ret_val, NODE_SIZE, next); /* 6 */ |
| 1382 |
|
| 1383 |
next = insert (INIT_COUNT, ret_val, 0UL, 0UL, Num_Braces); /* 7 */ |
| 1384 |
|
| 1385 |
tail (ret_val, next); /* 7 */ |
| 1386 |
|
| 1387 |
} else if (min_max [0] > REG_ZERO && min_max [1] == REG_INFINITY) { |
| 1388 |
/* Node structure for (x){m,} construct. |
| 1389 |
* __________4________ |
| 1390 |
* | __3__________ \ |
| 1391 |
* _|___| 1_ _2 \ \ _7 |
| 1392 |
* / | 8 | / |/ | \ \ / | |
| 1393 |
* I~ B~ (...)~ C~ T~m K~ K~ B~ N~ |
| 1394 |
* \ \_5___| | |
| 1395 |
* \__________6__________| |
| 1396 |
*/ |
| 1397 |
|
| 1398 |
tail (ret_val, emit_special (INC_COUNT, 0UL, Num_Braces)); /* 1 */ |
| 1399 |
|
| 1400 |
next = emit_special (TEST_COUNT, min_max [0], Num_Braces); /* 2 */ |
| 1401 |
|
| 1402 |
tail (ret_val, next); /* 2 */ |
| 1403 |
tail (emit_node (BACK), ret_val); /* 3 */ |
| 1404 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 4,6 */ |
| 1405 |
|
| 1406 |
next = emit_node (BACK); /* 4 */ |
| 1407 |
|
| 1408 |
tail (next, ret_val); /* 4 */ |
| 1409 |
offset_tail (ret_val, NODE_SIZE, next); /* 5 */ |
| 1410 |
tail (ret_val, emit_node (BRANCH)); /* 6 */ |
| 1411 |
tail (ret_val, emit_node (NOTHING)); /* 7 */ |
| 1412 |
|
| 1413 |
insert (INIT_COUNT, ret_val, 0UL, 0UL, Num_Braces); /* 8 */ |
| 1414 |
|
| 1415 |
tail (ret_val, ret_val + INDEX_SIZE + (2 * NODE_SIZE)); /* 8 */ |
| 1416 |
|
| 1417 |
} else { |
| 1418 |
/* Node structure for (x){m,n} construct. |
| 1419 |
* _____6________________ |
| 1420 |
* | _____________3___ \ |
| 1421 |
* 9_|__| 1_ _2 \ \ _8 |
| 1422 |
* / | | / |/ | \ \ / | |
| 1423 |
* I~ B~ (...)~ C~ T~x T~m K~ K~ B~ N~ |
| 1424 |
* \ \ \__4__| | | |
| 1425 |
* \ \_7_________|__| |
| 1426 |
* \_________5_____________| |
| 1427 |
*/ |
| 1428 |
|
| 1429 |
tail (ret_val, emit_special (INC_COUNT, 0UL, Num_Braces)); /* 1 */ |
| 1430 |
|
| 1431 |
next = emit_special (TEST_COUNT, min_max [1], Num_Braces); /* 2,4 */ |
| 1432 |
|
| 1433 |
tail (ret_val, next); /* 2 */ |
| 1434 |
|
| 1435 |
next = emit_special (TEST_COUNT, min_max [0], Num_Braces); /* 4 */ |
| 1436 |
|
| 1437 |
tail (emit_node (BACK), ret_val); /* 3 */ |
| 1438 |
tail (next, emit_node (BACK)); /* 4 */ |
| 1439 |
(void) insert (BRANCH, ret_val, 0UL, 0UL, 0); /* 5,6 */ |
| 1440 |
|
| 1441 |
next = emit_node (BRANCH); /* 5,8 */ |
| 1442 |
|
| 1443 |
tail (ret_val, next); /* 5 */ |
| 1444 |
offset_tail (next, -NODE_SIZE, ret_val); /* 6 */ |
| 1445 |
|
| 1446 |
next = emit_node (NOTHING); /* 7,8 */ |
| 1447 |
|
| 1448 |
offset_tail (ret_val, NODE_SIZE, next); /* 7 */ |
| 1449 |
|
| 1450 |
offset_tail (next, -NODE_SIZE, next); /* 8 */ |
| 1451 |
(void) insert (INIT_COUNT, ret_val, 0UL, 0UL, Num_Braces); /* 9 */ |
| 1452 |
tail (ret_val, ret_val + INDEX_SIZE + (2 * NODE_SIZE)); /* 9 */ |
| 1453 |
} |
| 1454 |
|
| 1455 |
Num_Braces++; |
| 1456 |
} else { |
| 1457 |
/* We get here if the IS_QUANTIFIER macro is not coordinated properly |
| 1458 |
with this function. */ |
| 1459 |
|
| 1460 |
REG_FAIL (U"internal error #2, `piece\'"); |
| 1461 |
} |
| 1462 |
|
| 1463 |
if (IS_QUANTIFIER (*Reg_Parse)) { |
| 1464 |
if (op_code == '{') { |
| 1465 |
Melder_sprint (Error_Text,128, U"nested quantifiers, {m,n}", *Reg_Parse); |
| 1466 |
} else { |
| 1467 |
Melder_sprint (Error_Text,128, U"nested quantifiers, ", op_code, *Reg_Parse); |
| 1468 |
} |
| 1469 |
|
| 1470 |
REG_FAIL (Error_Text); |
| 1471 |
} |
| 1472 |
|
| 1473 |
return (ret_val); |
| 1474 |
} |
| 1475 |
|
| 1476 |
/*----------------------------------------------------------------------* |
| 1477 |
* atom |
| 1478 |
* |
| 1479 |
* Process one regex item at the lowest level |
| 1480 |
* |
| 1481 |
* OPTIMIZATION: Lumps a continuous sequence of ordinary characters |
| 1482 |
* together so that it can turn them into a single EXACTLY node, which |
| 1483 |
* is smaller to store and faster to run. |
| 1484 |
*----------------------------------------------------------------------*/ |
| 1485 |
|
| 1486 |
static char32 *atom (int *flag_param, len_range *range_param) { |
| 1487 |
|
| 1488 |
char32 *ret_val; |
| 1489 |
char32 test; |
| 1490 |
int flags_local; |
| 1491 |
len_range range_local; |
| 1492 |
|
| 1493 |
*flag_param = WORST; /* Tentatively. */ |
| 1494 |
range_param->lower = 0; /* Idem */ |
| 1495 |
range_param->upper = 0; |
| 1496 |
|
| 1497 |
/* Process any regex comments, e.g. `(?# match next token->)'. The |
| 1498 |
terminating right parenthesis cannot be escaped. The comment stops at |
| 1499 |
the first right parenthesis encountered (or the end of the regex |
| 1500 |
string)... period. Handles multiple sequential comments, |
| 1501 |
e.g. `(?# one)(?# two)...' */ |
| 1502 |
|
| 1503 |
while (*Reg_Parse == '(' && |
| 1504 |
* (Reg_Parse + 1) == '?' && |
| 1505 |
* (Reg_Parse + 2) == '#') { |
| 1506 |
|
| 1507 |
Reg_Parse += 3; |
| 1508 |
|
| 1509 |
while (*Reg_Parse != ')' && *Reg_Parse != '\0') { |
| 1510 |
Reg_Parse++; |
| 1511 |
} |
| 1512 |
|
| 1513 |
if (*Reg_Parse == ')') { |
| 1514 |
Reg_Parse++; |
| 1515 |
} |
| 1516 |
|
| 1517 |
if (*Reg_Parse == ')' || *Reg_Parse == '|' || *Reg_Parse == '\0') { |
| 1518 |
/* Hit end of regex string or end of parenthesized regex; have to |
| 1519 |
return "something" (i.e. a NOTHING node) to avoid generating an |
| 1520 |
error. */ |
| 1521 |
|
| 1522 |
ret_val = emit_node (NOTHING); |
| 1523 |
|
| 1524 |
return (ret_val); |
| 1525 |
} |
| 1526 |
} |
| 1527 |
|
| 1528 |
switch (*Reg_Parse ++) { |
| 1529 |
case U'^': |
| 1530 |
ret_val = emit_node (BOL); |
| 1531 |
break; |
| 1532 |
case U'$': |
| 1533 |
ret_val = emit_node (EOL); |
| 1534 |
break; |
| 1535 |
case U'<': |
| 1536 |
ret_val = emit_node (BOWORD); |
| 1537 |
break; |
| 1538 |
case U'>': |
| 1539 |
ret_val = emit_node (EOWORD); |
| 1540 |
break; |
| 1541 |
case U'.': |
| 1542 |
if (Match_Newline) { |
| 1543 |
ret_val = emit_node (EVERY); |
| 1544 |
} else { |
| 1545 |
ret_val = emit_node (ANY); |
| 1546 |
} |
| 1547 |
|
| 1548 |
*flag_param |= (HAS_WIDTH | SIMPLE); |
| 1549 |
range_param->lower = 1; |
| 1550 |
range_param->upper = 1; |
| 1551 |
break; |
| 1552 |
case U'(': |
| 1553 |
if (*Reg_Parse == U'?') { /* Special parenthetical expression */ |
| 1554 |
Reg_Parse++; |
| 1555 |
range_local.lower = 0; /* Make sure it is always used */ |
| 1556 |
range_local.upper = 0; |
| 1557 |
|
| 1558 |
if (*Reg_Parse == U':') { |
| 1559 |
Reg_Parse ++; |
| 1560 |
ret_val = chunk (NO_CAPTURE, &flags_local, &range_local); |
| 1561 |
} else if (*Reg_Parse == U'=') { |
| 1562 |
Reg_Parse ++; |
| 1563 |
ret_val = chunk (POS_AHEAD_OPEN, &flags_local, &range_local); |
| 1564 |
} else if (*Reg_Parse == U'!') { |
| 1565 |
Reg_Parse ++; |
| 1566 |
ret_val = chunk (NEG_AHEAD_OPEN, &flags_local, &range_local); |
| 1567 |
} else if (*Reg_Parse == U'i') { |
| 1568 |
Reg_Parse ++; |
| 1569 |
ret_val = chunk (INSENSITIVE, &flags_local, &range_local); |
| 1570 |
} else if (*Reg_Parse == U'I') { |
| 1571 |
Reg_Parse ++; |
| 1572 |
ret_val = chunk (SENSITIVE, &flags_local, &range_local); |
| 1573 |
} else if (*Reg_Parse == U'n') { |
| 1574 |
Reg_Parse ++; |
| 1575 |
ret_val = chunk (NEWLINE, &flags_local, &range_local); |
| 1576 |
} else if (*Reg_Parse == U'N') { |
| 1577 |
Reg_Parse ++; |
| 1578 |
ret_val = chunk (NO_NEWLINE, &flags_local, &range_local); |
| 1579 |
} else if (*Reg_Parse == U'<') { |
| 1580 |
Reg_Parse ++; |
| 1581 |
if (*Reg_Parse == U'=') { |
| 1582 |
Reg_Parse ++; |
| 1583 |
ret_val = chunk (POS_BEHIND_OPEN, &flags_local, &range_local); |
| 1584 |
} else if (*Reg_Parse == U'!') { |
| 1585 |
Reg_Parse ++; |
| 1586 |
ret_val = chunk (NEG_BEHIND_OPEN, &flags_local, &range_local); |
| 1587 |
} else { |
| 1588 |
Melder_sprint (Error_Text,128, U"invalid look-behind syntax, \"(?<", *Reg_Parse, U"...)\""); |
| 1589 |
REG_FAIL (Error_Text) |
| 1590 |
} |
| 1591 |
} else { |
| 1592 |
Melder_sprint (Error_Text,128, U"invalid grouping syntax, \"(?", *Reg_Parse, U"...)\""); |
| 1593 |
REG_FAIL (Error_Text) |
| 1594 |
} |
| 1595 |
} else { /* Normal capturing parentheses */ |
| 1596 |
ret_val = chunk (PAREN, & flags_local, & range_local); |
| 1597 |
} |
| 1598 |
if (! ret_val) |
| 1599 |
return nullptr; // something went wrong |
| 1600 |
|
| 1601 |
/* Add HAS_WIDTH flag if it was set by call to chunk. */ |
| 1602 |
|
| 1603 |
*flag_param |= flags_local & HAS_WIDTH; |
| 1604 |
*range_param = range_local; |
| 1605 |
|
| 1606 |
break; |
| 1607 |
|
| 1608 |
case U'\0': |
| 1609 |
case U'|': |
| 1610 |
case U')': |
| 1611 |
REG_FAIL (U"internal error #3, `atom\'") // supposed to be caught earlier |
| 1612 |
case U'?': |
| 1613 |
case U'+': |
| 1614 |
case U'*': |
| 1615 |
Melder_sprint (Error_Text,128, * (Reg_Parse - 1), U" follows nothing"); |
| 1616 |
REG_FAIL (Error_Text) |
| 1617 |
|
| 1618 |
case U'{': |
| 1619 |
if (Enable_Counting_Quantifier) { |
| 1620 |
REG_FAIL (U"{m,n} follows nothing") |
| 1621 |
} else { |
| 1622 |
ret_val = emit_node (EXACTLY); /* Treat braces as literals. */ |
| 1623 |
emit_byte (U'{'); |
| 1624 |
emit_byte (U'\0'); |
| 1625 |
range_param->lower = 1; |
| 1626 |
range_param->upper = 1; |
| 1627 |
} |
| 1628 |
|
| 1629 |
break; |
| 1630 |
|
| 1631 |
case U'[': { |
| 1632 |
char32 last_emit = U'\0'; |
| 1633 |
|
| 1634 |
/* Handle characters that can only occur at the start of a class. */ |
| 1635 |
|
| 1636 |
if (*Reg_Parse == U'^') { /* Complement of range. */ |
| 1637 |
ret_val = emit_node (ANY_BUT); |
| 1638 |
Reg_Parse ++; |
| 1639 |
|
| 1640 |
/* All negated classes include newline unless escaped with |
| 1641 |
a "(?n)" switch. */ |
| 1642 |
|
| 1643 |
if (!Match_Newline) { |
| 1644 |
emit_byte (U'\n'); |
| 1645 |
} |
| 1646 |
} else { |
| 1647 |
ret_val = emit_node (ANY_OF); |
| 1648 |
} |
| 1649 |
|
| 1650 |
if (*Reg_Parse == U']' || *Reg_Parse == U'-') { |
| 1651 |
/* If '-' or ']' is the first character in a class, |
| 1652 |
it is a literal character in the class. */ |
| 1653 |
|
| 1654 |
last_emit = *Reg_Parse; |
| 1655 |
emit_byte (*Reg_Parse); |
| 1656 |
Reg_Parse ++; |
| 1657 |
} |
| 1658 |
|
| 1659 |
/* Handle the rest of the class characters. */ |
| 1660 |
|
| 1661 |
while (*Reg_Parse != U'\0' && *Reg_Parse != U']') { |
| 1662 |
if (*Reg_Parse == U'-') { /* Process a range, e.g [a-z]. */ |
| 1663 |
Reg_Parse ++; |
| 1664 |
|
| 1665 |
if (*Reg_Parse == U']' || *Reg_Parse == U'\0') { |
| 1666 |
/* If '-' is the last character in a class it is a literal |
| 1667 |
character. If `Reg_Parse' points to the end of the |
| 1668 |
regex string, an error will be generated later. */ |
| 1669 |
|
| 1670 |
emit_byte (U'-'); |
| 1671 |
last_emit = U'-'; |
| 1672 |
} else { |
| 1673 |
/* We must get the range starting character value from the |
| 1674 |
emitted code since it may have been an escaped |
| 1675 |
character. `second_value' is set one larger than the |
| 1676 |
just emitted character value. This is done since |
| 1677 |
`second_value' is used as the start value for the loop |
| 1678 |
that emits the values in the range. Since we have |
| 1679 |
already emitted the first character of the class, we do |
| 1680 |
not want to emit it again. */ |
| 1681 |
|
| 1682 |
char32 second_value = last_emit + 1, last_value; |
| 1683 |
|
| 1684 |
if (*Reg_Parse == U'\\') { |
| 1685 |
/* Handle escaped characters within a class range. |
| 1686 |
Specifically disallow shortcut escapes as the end of |
| 1687 |
a class range. To allow this would be ambiguous |
| 1688 |
since shortcut escapes represent a set of characters, |
| 1689 |
and it would not be clear which character of the |
| 1690 |
class should be treated as the "last" character. */ |
| 1691 |
|
| 1692 |
Reg_Parse++; |
| 1693 |
|
| 1694 |
if ((test = numeric_escape (*Reg_Parse, &Reg_Parse)) != U'\0') { |
| 1695 |
last_value = test; |
| 1696 |
} else if ((test = literal_escape (*Reg_Parse)) != U'\0') { |
| 1697 |
last_value = test; |
| 1698 |
} else if (shortcut_escape (*Reg_Parse, NULL, CHECK_CLASS_ESCAPE)) { |
| 1699 |
Melder_sprint (Error_Text, 128, U"\\", *Reg_Parse, U" is not allowed as range operand"); |
| 1700 |
REG_FAIL (Error_Text) |
| 1701 |
} else { |
| 1702 |
Melder_sprint (Error_Text, 128, U"\\", *Reg_Parse, U" is an invalid char class escape sequence"); |
| 1703 |
REG_FAIL (Error_Text) |
| 1704 |
} |
| 1705 |
} else { |
| 1706 |
last_value = U_CHAR_AT (Reg_Parse); |
| 1707 |
} |
| 1708 |
|
| 1709 |
if (Is_Case_Insensitive) { |
| 1710 |
second_value = Melder_toLowerCase (second_value); // TODO: this should do casefolding |
| 1711 |
last_value = Melder_toLowerCase (last_value); |
| 1712 |
} |
| 1713 |
|
| 1714 |
/* For case insensitive, something like [A-_] will |
| 1715 |
generate an error here since ranges are converted to |
| 1716 |
lower case. */ |
| 1717 |
|
| 1718 |
if (second_value - 1 > last_value) |
| 1719 |
REG_FAIL (U"invalid [] range") |
| 1720 |
|
| 1721 |
/* If only one character in range (e.g [a-a]) then this |
| 1722 |
loop is not run since the first character of any range |
| 1723 |
was emitted by the previous iteration of while loop. */ |
| 1724 |
|
| 1725 |
for (; second_value <= last_value; second_value++) |
| 1726 |
emit_class_byte (second_value); |
| 1727 |
last_emit = (char32) last_value; |
| 1728 |
Reg_Parse ++; |
| 1729 |
} /* End class character range code. */ |
| 1730 |
} else if (*Reg_Parse == '\\') { |
| 1731 |
Reg_Parse++; |
| 1732 |
|
| 1733 |
if ( (test = numeric_escape (*Reg_Parse, &Reg_Parse)) != '\0') { |
| 1734 |
emit_class_byte (test); |
| 1735 |
last_emit = test; |
| 1736 |
} else if ( (test = literal_escape (*Reg_Parse)) != '\0') { |
| 1737 |
emit_byte (test); |
| 1738 |
last_emit = test; |
| 1739 |
} else if (shortcut_escape (*Reg_Parse, |
| 1740 |
NULL, |
| 1741 |
CHECK_CLASS_ESCAPE)) { |
| 1742 |
|
| 1743 |
if (* (Reg_Parse + 1) == '-') { |
| 1744 |
/* Specifically disallow shortcut escapes as the start |
| 1745 |
of a character class range (see comment above.) */ |
| 1746 |
|
| 1747 |
Melder_sprint (Error_Text,128, U"\\", *Reg_Parse, U" not allowed as range operand"); |
| 1748 |
|
| 1749 |
REG_FAIL (Error_Text); |
| 1750 |
} else { |
| 1751 |
/* Emit the bytes that are part of the shortcut |
| 1752 |
escape sequence's range (e.g. \d = 0123456789) */ |
| 1753 |
|
| 1754 |
Melder_sprint (Error_Text,128, U"Cannot have escape sequences such as \\", *Reg_Parse, U" inside a class"); |
| 1755 |
} |
| 1756 |
} else { |
| 1757 |
Melder_sprint (Error_Text,128, U"\\", *Reg_Parse, U" is an invalid char class escape sequence"); |
| 1758 |
|
| 1759 |
REG_FAIL (Error_Text); |
| 1760 |
} |
| 1761 |
|
| 1762 |
Reg_Parse++; |
| 1763 |
|
| 1764 |
/* End of class escaped sequence code */ |
| 1765 |
} else { |
| 1766 |
emit_class_byte (*Reg_Parse); /* Ordinary class character. */ |
| 1767 |
|
| 1768 |
last_emit = *Reg_Parse; |
| 1769 |
Reg_Parse++; |
| 1770 |
} |
| 1771 |
} /* End of while (*Reg_Parse != U'\0' && *Reg_Parse != U']') */ |
| 1772 |
|
| 1773 |
if (*Reg_Parse != U']') { |
| 1774 |
REG_FAIL (U"missing right \']\'"); |
| 1775 |
} |
| 1776 |
|
| 1777 |
emit_byte (U'\0'); |
| 1778 |
|
| 1779 |
/* NOTE: it is impossible to specify an empty class. This is |
| 1780 |
because [] would be interpreted as "begin character class" |
| 1781 |
followed by a literal ']' character and no "end character class" |
| 1782 |
delimiter (']'). Because of this, it is always safe to assume |
| 1783 |
that a class HAS_WIDTH. */ |
| 1784 |
|
| 1785 |
Reg_Parse ++; |
| 1786 |
*flag_param |= HAS_WIDTH | SIMPLE; |
| 1787 |
range_param->lower = 1; |
| 1788 |
range_param->upper = 1; |
| 1789 |
} |
| 1790 |
|
| 1791 |
break; /* End of character class code. */ |
| 1792 |
|
| 1793 |
case U'\\': |
| 1794 |
/* Force Error_Text to have a length of zero. This way we can tell if |
| 1795 |
either of the calls to shortcut_escape() or back_ref() fill |
| 1796 |
Error_Text with an error message. */ |
| 1797 |
|
| 1798 |
Error_Text [0] = U'\0'; |
| 1799 |
|
| 1800 |
if ( (ret_val = shortcut_escape (*Reg_Parse, flag_param, EMIT_NODE))) { |
| 1801 |
Reg_Parse ++; |
| 1802 |
range_param->lower = 1; |
| 1803 |
range_param->upper = 1; |
| 1804 |
break; |
| 1805 |
} else if ( (ret_val = back_ref (Reg_Parse, flag_param, EMIT_NODE))) { |
| 1806 |
/* Can't make any assumptions about a back-reference as to SIMPLE |
| 1807 |
or HAS_WIDTH. For example (^|<) is neither simple nor has |
| 1808 |
width. So we don't flip bits in flag_param here. */ |
| 1809 |
|
| 1810 |
Reg_Parse++; |
| 1811 |
/* Back-references always have an unknown length */ |
| 1812 |
range_param->lower = -1; |
| 1813 |
range_param->upper = -1; |
| 1814 |
break; |
| 1815 |
} |
| 1816 |
if (Error_Text [0] != U'\0') |
| 1817 |
REG_FAIL (Error_Text) |
| 1818 |
|
| 1819 |
/* At this point it is apparent that the escaped character is not a |
| 1820 |
shortcut escape or back-reference. Back up one character to allow |
| 1821 |
the default code to include it as an ordinary character. */ |
| 1822 |
|
| 1823 |
/* Fall through to Default case to handle literal escapes and numeric |
| 1824 |
escapes. */ |
| 1825 |
|
| 1826 |
default: |
| 1827 |
Reg_Parse --; /* If we fell through from the above code, we are now |
| 1828 |
pointing at the back slash (\) character. */ |
| 1829 |
{ |
| 1830 |
char32 *parse_save; |
| 1831 |
int len = 0; |
| 1832 |
|
| 1833 |
if (Is_Case_Insensitive) { |
| 1834 |
ret_val = emit_node (SIMILAR); |
| 1835 |
} else { |
| 1836 |
ret_val = emit_node (EXACTLY); |
| 1837 |
} |
| 1838 |
|
| 1839 |
/* Loop until we find a meta character, shortcut escape, back |
| 1840 |
reference, or end of regex string. */ |
| 1841 |
|
| 1842 |
for (; *Reg_Parse != '\0' && ! str32chr (Meta_Char, *Reg_Parse); len++) { |
| 1843 |
|
| 1844 |
/* Save where we are in case we have to back |
| 1845 |
this character out. */ |
| 1846 |
|
| 1847 |
parse_save = Reg_Parse; |
| 1848 |
|
| 1849 |
if (*Reg_Parse == U'\\') { |
| 1850 |
Reg_Parse ++; // point to escaped character |
| 1851 |
|
| 1852 |
Error_Text [0] = U'\0'; // see comment above |
| 1853 |
|
| 1854 |
if ( (test = numeric_escape (*Reg_Parse, &Reg_Parse))) { |
| 1855 |
if (Is_Case_Insensitive) { |
| 1856 |
emit_byte (Melder_toLowerCase (test)); |
| 1857 |
} else { |
| 1858 |
emit_byte (test); |
| 1859 |
} |
| 1860 |
} else if ( (test = literal_escape (*Reg_Parse))) { |
| 1861 |
emit_byte (test); |
| 1862 |
} else if (back_ref (Reg_Parse, NULL, CHECK_ESCAPE)) { |
| 1863 |
/* Leave back reference for next `atom' call */ |
| 1864 |
|
| 1865 |
Reg_Parse --; break; |
| 1866 |
} else if (shortcut_escape (*Reg_Parse, NULL, CHECK_ESCAPE)) { |
| 1867 |
/* Leave shortcut escape for next `atom' call */ |
| 1868 |
|
| 1869 |
Reg_Parse --; break; |
| 1870 |
} else { |
| 1871 |
if (Error_Text [0] != U'\0') { |
| 1872 |
/* None of the above calls generated an error message |
| 1873 |
so generate our own here. */ |
| 1874 |
|
| 1875 |
Melder_sprint (Error_Text,128, U"\\", *Reg_Parse, U" is an invalid escape sequence"); |
| 1876 |
} |
| 1877 |
REG_FAIL (Error_Text) |
| 1878 |
} |
| 1879 |
Reg_Parse ++; |
| 1880 |
} else { |
| 1881 |
/* Ordinary character */ |
| 1882 |
|
| 1883 |
if (Is_Case_Insensitive) { |
| 1884 |
emit_byte (Melder_toLowerCase (*Reg_Parse)); |
| 1885 |
} else { |
| 1886 |
emit_byte (*Reg_Parse); |
| 1887 |
} |
| 1888 |
Reg_Parse ++; |
| 1889 |
} |
| 1890 |
|
| 1891 |
/* If next regex token is a quantifier (?, +. *, or {m,n}) and |
| 1892 |
our EXACTLY node so far is more than one character, leave the |
| 1893 |
last character to be made into an EXACTLY node one character |
| 1894 |
wide for the multiplier to act on. For example 'abcd* would |
| 1895 |
have an EXACTLY node with an 'abc' operand followed by a STAR |
| 1896 |
node followed by another EXACTLY node with a 'd' operand. */ |
| 1897 |
|
| 1898 |
if (IS_QUANTIFIER (*Reg_Parse) && len > 0) { |
| 1899 |
Reg_Parse = parse_save; /* Point to previous regex token. */ |
| 1900 |
if (Code_Emit_Ptr == &Compute_Size) { |
| 1901 |
Reg_Size--; |
| 1902 |
} else { |
| 1903 |
Code_Emit_Ptr--; /* Write over previously emitted byte. */ |
| 1904 |
} |
| 1905 |
break; |
| 1906 |
} |
| 1907 |
} |
| 1908 |
if (len <= 0) |
| 1909 |
REG_FAIL (U"internal error #4, `atom\'") |
| 1910 |
|
| 1911 |
*flag_param |= HAS_WIDTH; |
| 1912 |
|
| 1913 |
if (len == 1) |
| 1914 |
*flag_param |= SIMPLE; |
| 1915 |
|
| 1916 |
range_param->lower = len; |
| 1917 |
range_param->upper = len; |
| 1918 |
|
| 1919 |
emit_byte (U'\0'); |
| 1920 |
} |
| 1921 |
} /* END switch (*Reg_Parse++) */ |
| 1922 |
|
| 1923 |
return (ret_val); |
| 1924 |
} |
| 1925 |
|
| 1926 |
/*----------------------------------------------------------------------* |
| 1927 |
* emit_node |
| 1928 |
* |
| 1929 |
* Emit (if appropriate) the op code for a regex node atom. |
| 1930 |
* |
| 1931 |
* The NEXT pointer is initialized to NULL. |
| 1932 |
* |
| 1933 |
* Returns a pointer to the START of the emitted node. |
| 1934 |
*----------------------------------------------------------------------*/ |
| 1935 |
|
| 1936 |
static char32 *emit_node (int op_code) { |
| 1937 |
char32 *ret_val = Code_Emit_Ptr; /* Return address of start of node */ |
| 1938 |
if (ret_val == & Compute_Size) { |
| 1939 |
Reg_Size += NODE_SIZE; |
| 1940 |
} else { |
| 1941 |
char32 *ptr = ret_val; |
| 1942 |
*ptr ++ = op_code; |
| 1943 |
*ptr ++ = U'\0'; // null "NEXT" pointer |
| 1944 |
*ptr ++ = U'\0'; |
| 1945 |
Code_Emit_Ptr = ptr; |
| 1946 |
} |
| 1947 |
return ret_val; |
| 1948 |
} |
| 1949 |
|
| 1950 |
/*----------------------------------------------------------------------* |
| 1951 |
* emit_byte |
| 1952 |
* |
| 1953 |
* Emit (if appropriate) a byte of code (usually part of an operand.) |
| 1954 |
*----------------------------------------------------------------------*/ |
| 1955 |
|
| 1956 |
static void emit_byte (char32 c) { |
| 1957 |
if (Code_Emit_Ptr == & Compute_Size) { |
| 1958 |
Reg_Size ++; |
| 1959 |
} else { |
| 1960 |
*Code_Emit_Ptr ++ = c; |
| 1961 |
} |
| 1962 |
} |
| 1963 |
|
| 1964 |
/*----------------------------------------------------------------------* |
| 1965 |
* emit_class_byte |
| 1966 |
* |
| 1967 |
* Emit (if appropriate) a byte of code (usually part of a character |
| 1968 |
* class operand.) |
| 1969 |
*----------------------------------------------------------------------*/ |
| 1970 |
|
| 1971 |
static void emit_class_byte (char32 c) { |
| 1972 |
if (Code_Emit_Ptr == & Compute_Size) { |
| 1973 |
Reg_Size ++; |
| 1974 |
if (Is_Case_Insensitive && Melder_isLetter (c)) { |
| 1975 |
Reg_Size ++; |
| 1976 |
} |
| 1977 |
} else if (Is_Case_Insensitive && Melder_isLetter (c)) { |
| 1978 |
/* For case-insensitive character classes, emit both upper and lower case |
| 1979 |
versions of alphabetical characters. */ |
| 1980 |
|
| 1981 |
*Code_Emit_Ptr ++ = Melder_toLowerCase (c); |
| 1982 |
*Code_Emit_Ptr ++ = Melder_toUpperCase (c); |
| 1983 |
} else { |
| 1984 |
*Code_Emit_Ptr ++ = c; |
| 1985 |
} |
| 1986 |
} |
| 1987 |
|
| 1988 |
/*----------------------------------------------------------------------* |
| 1989 |
* emit_special |
| 1990 |
* |
| 1991 |
* Emit nodes that need special processing. |
| 1992 |
*----------------------------------------------------------------------*/ |
| 1993 |
|
| 1994 |
static char32 *emit_special ( |
| 1995 |
char32 op_code, |
| 1996 |
unsigned long test_val, |
| 1997 |
int index) |
| 1998 |
{ |
| 1999 |
char32 *ret_val = & Compute_Size; |
| 2000 |
char32 *ptr; |
| 2001 |
|
| 2002 |
if (Code_Emit_Ptr == & Compute_Size) { |
| 2003 |
switch (op_code) { |
| 2004 |
case POS_BEHIND_OPEN: |
| 2005 |
case NEG_BEHIND_OPEN: |
| 2006 |
Reg_Size += LENGTH_SIZE; /* Length of the look-behind match */ |
| 2007 |
Reg_Size += NODE_SIZE; /* Make room for the node */ |
| 2008 |
break; |
| 2009 |
case TEST_COUNT: |
| 2010 |
Reg_Size += NEXT_PTR_SIZE; /* Make room for a test value. */ |
| 2011 |
// ppgb FALLTHROUGH (both test and inc require index) |
| 2012 |
case INC_COUNT: |
| 2013 |
Reg_Size += INDEX_SIZE; /* Make room for an index value. */ |
| 2014 |
// ppgb FALLTHROUGH (everything requires node) |
| 2015 |
default: |
| 2016 |
Reg_Size += NODE_SIZE; /* Make room for the node. */ |
| 2017 |
} |
| 2018 |
} else { |
| 2019 |
ret_val = emit_node (op_code); /* Return the address for start of node. */ |
| 2020 |
ptr = Code_Emit_Ptr; |
| 2021 |
if (op_code == INC_COUNT || op_code == TEST_COUNT) { |
| 2022 |
*ptr ++ = (char32) index; |
| 2023 |
if (op_code == TEST_COUNT) { |
| 2024 |
*ptr ++ = PUT_OFFSET_L (test_val); |
| 2025 |
*ptr ++ = PUT_OFFSET_R (test_val); |
| 2026 |
} |
| 2027 |
} else if (op_code == POS_BEHIND_OPEN || op_code == NEG_BEHIND_OPEN) { |
| 2028 |
*ptr ++ = PUT_OFFSET_L (test_val); |
| 2029 |
*ptr ++ = PUT_OFFSET_R (test_val); |
| 2030 |
*ptr ++ = PUT_OFFSET_L (test_val); |
| 2031 |
*ptr ++ = PUT_OFFSET_R (test_val); |
| 2032 |
} |
| 2033 |
Code_Emit_Ptr = ptr; |
| 2034 |
} |
| 2035 |
return ret_val; |
| 2036 |
} |
| 2037 |
|
| 2038 |
/*----------------------------------------------------------------------* |
| 2039 |
* insert |
| 2040 |
* |
| 2041 |
* Insert a node in front of already emitted node(s). Means relocating |
| 2042 |
* the operand. Code_Emit_Ptr points one byte past the just emitted |
| 2043 |
* node and operand. The parameter `insert_pos' points to the location |
| 2044 |
* where the new node is to be inserted. |
| 2045 |
*----------------------------------------------------------------------*/ |
| 2046 |
|
| 2047 |
static char32 *insert ( |
| 2048 |
char32 op, |
| 2049 |
char32 *insert_pos, |
| 2050 |
long min, |
| 2051 |
long max, |
| 2052 |
int index) { |
| 2053 |
|
| 2054 |
char32 *src; |
| 2055 |
char32 *dst; |
| 2056 |
char32 *place; |
| 2057 |
int insert_size = NODE_SIZE; |
| 2058 |
|
| 2059 |
if (op == BRACE || op == LAZY_BRACE) { |
| 2060 |
/* Make room for the min and max values. */ |
| 2061 |
|
| 2062 |
insert_size += (2 * NEXT_PTR_SIZE); |
| 2063 |
} else if (op == INIT_COUNT) { |
| 2064 |
/* Make room for an index value . */ |
| 2065 |
|
| 2066 |
insert_size += INDEX_SIZE; |
| 2067 |
} |
| 2068 |
|
| 2069 |
if (Code_Emit_Ptr == &Compute_Size) { |
| 2070 |
Reg_Size += insert_size; |
| 2071 |
return &Compute_Size; |
| 2072 |
} |
| 2073 |
|
| 2074 |
src = Code_Emit_Ptr; |
| 2075 |
Code_Emit_Ptr += insert_size; |
| 2076 |
dst = Code_Emit_Ptr; |
| 2077 |
|
| 2078 |
/* Relocate the existing emitted code to make room for the new node. */ |
| 2079 |
|
| 2080 |
while (src > insert_pos) { |
| 2081 |
*--dst = *--src; |
| 2082 |
} |
| 2083 |
|
| 2084 |
place = insert_pos; /* Where operand used to be. */ |
| 2085 |
*place++ = op; /* Inserted operand. */ |
| 2086 |
*place++ = '\0'; /* NEXT pointer for inserted operand. */ |
| 2087 |
*place++ = '\0'; |
| 2088 |
|
| 2089 |
if (op == BRACE || op == LAZY_BRACE) { |
| 2090 |
*place++ = PUT_OFFSET_L (min); |
| 2091 |
*place++ = PUT_OFFSET_R (min); |
| 2092 |
|
| 2093 |
*place++ = PUT_OFFSET_L (max); |
| 2094 |
*place++ = PUT_OFFSET_R (max); |
| 2095 |
} else if (op == INIT_COUNT) { |
| 2096 |
*place++ = (char32) index; |
| 2097 |
} |
| 2098 |
|
| 2099 |
return place; /* Return a pointer to the start of the code moved. */ |
| 2100 |
} |
| 2101 |
|
| 2102 |
/*----------------------------------------------------------------------* |
| 2103 |
* tail - Set the next-pointer at the end of a node chain. |
| 2104 |
*----------------------------------------------------------------------*/ |
| 2105 |
|
| 2106 |
static void tail (char32 *search_from, char32 *point_to) { |
| 2107 |
|
| 2108 |
char32 *scan; |
| 2109 |
char32 *next; |
| 2110 |
int offset; |
| 2111 |
|
| 2112 |
if (search_from == &Compute_Size) { |
| 2113 |
return; |
| 2114 |
} |
| 2115 |
|
| 2116 |
/* Find the last node in the chain (node with a null NEXT pointer) */ |
| 2117 |
|
| 2118 |
scan = search_from; |
| 2119 |
|
| 2120 |
for (;;) { |
| 2121 |
next = next_ptr (scan); |
| 2122 |
|
| 2123 |
if (!next) { |
| 2124 |
break; |
| 2125 |
} |
| 2126 |
|
| 2127 |
scan = next; |
| 2128 |
} |
| 2129 |
|
| 2130 |
if (GET_OP_CODE (scan) == BACK) { |
| 2131 |
offset = scan - point_to; |
| 2132 |
} else { |
| 2133 |
offset = point_to - scan; |
| 2134 |
} |
| 2135 |
|
| 2136 |
/* Set NEXT pointer */ |
| 2137 |
|
| 2138 |
* (scan + 1) = PUT_OFFSET_L (offset); |
| 2139 |
* (scan + 2) = PUT_OFFSET_R (offset); |
| 2140 |
} |
| 2141 |
|
| 2142 |
/*--------------------------------------------------------------------* |
| 2143 |
* offset_tail |
| 2144 |
* |
| 2145 |
* Perform a tail operation on (ptr + offset). |
| 2146 |
*--------------------------------------------------------------------*/ |
| 2147 |
|
| 2148 |
static void offset_tail (char32 *ptr, int offset, char32 *val) { |
| 2149 |
if (ptr == & Compute_Size || ! ptr) |
| 2150 |
return; |
| 2151 |
tail (ptr + offset, val); |
| 2152 |
} |
| 2153 |
|
| 2154 |
/*--------------------------------------------------------------------* |
| 2155 |
* branch_tail |
| 2156 |
* |
| 2157 |
* Perform a tail operation on (ptr + offset) but only if `ptr' is a |
| 2158 |
* BRANCH node. |
| 2159 |
*--------------------------------------------------------------------*/ |
| 2160 |
|
| 2161 |
static void branch_tail (char32 *ptr, int offset, char32 *val) { |
| 2162 |
if (ptr == & Compute_Size || ! ptr || GET_OP_CODE (ptr) != BRANCH) |
| 2163 |
return; |
| 2164 |
tail (ptr + offset, val); |
| 2165 |
} |
| 2166 |
|
| 2167 |
/*--------------------------------------------------------------------* |
| 2168 |
* shortcut_escape |
| 2169 |
* |
| 2170 |
* Implements convenient escape sequences that represent entire |
| 2171 |
* character classes or special location assertions (similar to escapes |
| 2172 |
* supported by Perl) |
| 2173 |
* _ |
| 2174 |
* \d Digits [0-9] | |
| 2175 |
* \D NOT a digit [^0-9] | (Examples |
| 2176 |
* \l Letters [a-zA-Z] | at left |
| 2177 |
* \L NOT a Letter [^a-zA-Z] | are |
| 2178 |
* \s Whitespace [ \t\n\r\f\v] | for |
| 2179 |
* \S NOT Whitespace [^ \t\n\r\f\v] | ASCII) |
| 2180 |
* \w "Word" character [a-zA-Z0-9_] | |
| 2181 |
* \W NOT a "Word" character [^a-zA-Z0-9_] _| |
| 2182 |
* |
| 2183 |
* \B Matches any character that is NOT a word-delimiter |
| 2184 |
* |
| 2185 |
* Codes for the "emit" parameter: |
| 2186 |
* |
| 2187 |
* EMIT_NODE |
| 2188 |
* Emit a shortcut node. Shortcut nodes have an implied set of |
| 2189 |
* class characters. This helps keep the compiled regex string |
| 2190 |
* small. |
| 2191 |
* |
| 2192 |
* CHECK_ESCAPE |
| 2193 |
* Only verify that this is a valid shortcut escape. |
| 2194 |
* |
| 2195 |
* CHECK_CLASS_ESCAPE |
| 2196 |
* Same as CHECK_ESCAPE but only allows characters valid within |
| 2197 |
* a klas. |
| 2198 |
* |
| 2199 |
*--------------------------------------------------------------------*/ |
| 2200 |
|
| 2201 |
static char32 *shortcut_escape ( |
| 2202 |
char32 c, |
| 2203 |
int *flag_param, |
| 2204 |
int emit) { |
| 2205 |
|
| 2206 |
char32 *klas = NULL; |
| 2207 |
static const conststring32 codes = U"ByYwWdDlLsS"; |
| 2208 |
char32 *ret_val = (char32 *) 1; // assume success |
| 2209 |
conststring32 valid_codes; |
| 2210 |
|
| 2211 |
if (emit == CHECK_CLASS_ESCAPE) { |
| 2212 |
valid_codes = codes + 5; /* \B, \y, \Y, \w and \W are not allowed in classes */ |
| 2213 |
} else { |
| 2214 |
valid_codes = codes; |
| 2215 |
} |
| 2216 |
|
| 2217 |
if (! str32chr (valid_codes, c)) { |
| 2218 |
return nullptr; // not a valid shortcut escape sequence |
| 2219 |
} else if (emit == CHECK_ESCAPE || emit == CHECK_CLASS_ESCAPE) { |
| 2220 |
return ret_val; // just checking if this is a valid shortcut escape |
| 2221 |
} |
| 2222 |
|
| 2223 |
switch (c) { |
| 2224 |
case U'd': |
| 2225 |
case U'D': |
| 2226 |
if (emit == EMIT_NODE) |
| 2227 |
ret_val = (iswlower_portable ((int) c) ? emit_node (DIGIT) : emit_node (NOT_DIGIT)); |
| 2228 |
break; |
| 2229 |
case U'l': |
| 2230 |
case U'L': |
| 2231 |
if (emit == EMIT_NODE) |
| 2232 |
ret_val = (iswlower_portable ((int) c) ? emit_node (LETTER) : emit_node (NOT_LETTER)); |
| 2233 |
break; |
| 2234 |
case U's': |
| 2235 |
case U'S': |
| 2236 |
if (emit == EMIT_NODE) { |
| 2237 |
if (Match_Newline) { |
| 2238 |
ret_val = (iswlower_portable ((int) c) ? emit_node (SPACE_NL) : emit_node (NOT_SPACE_NL)); |
| 2239 |
} else { |
| 2240 |
ret_val = (iswlower_portable ((int) c) ? emit_node (SPACE) : emit_node (NOT_SPACE)); |
| 2241 |
} |
| 2242 |
} |
| 2243 |
break; |
| 2244 |
case U'w': |
| 2245 |
case U'W': |
| 2246 |
if (emit == EMIT_NODE) { |
| 2247 |
ret_val = (iswlower_portable ((int) c) ? emit_node (WORD_CHAR) : emit_node (NOT_WORD_CHAR)); |
| 2248 |
} else { |
| 2249 |
REG_FAIL (U"internal error #105 `shortcut_escape\'"); |
| 2250 |
} |
| 2251 |
break; |
| 2252 |
/* |
| 2253 |
Since the delimiter table is not available at regex compile time, |
| 2254 |
\B, \y and \Y can only generate a node. At run time, the delimiter table |
| 2255 |
will be available for these nodes to use. |
| 2256 |
*/ |
| 2257 |
case 'y': |
| 2258 |
if (emit == EMIT_NODE) { |
| 2259 |
ret_val = emit_node (IS_DELIM); |
| 2260 |
} else { |
| 2261 |
REG_FAIL (U"internal error #5 `shortcut_escape\'"); |
| 2262 |
} |
| 2263 |
break; |
| 2264 |
case 'Y': |
| 2265 |
if (emit == EMIT_NODE) { |
| 2266 |
ret_val = emit_node (NOT_DELIM); |
| 2267 |
} else { |
| 2268 |
REG_FAIL (U"internal error #6 `shortcut_escape\'"); |
| 2269 |
} |
| 2270 |
break; |
| 2271 |
case 'B': |
| 2272 |
if (emit == EMIT_NODE) { |
| 2273 |
ret_val = emit_node (NOT_BOUNDARY); |
| 2274 |
} else { |
| 2275 |
REG_FAIL (U"internal error #7 `shortcut_escape\'"); |
| 2276 |
} |
| 2277 |
break; |
| 2278 |
default: |
| 2279 |
/* |
| 2280 |
We get here if there isn't a case for every character in the string "codes". |
| 2281 |
*/ |
| 2282 |
REG_FAIL (U"internal error #8 `shortcut_escape\'"); |
| 2283 |
} |
| 2284 |
|
| 2285 |
if (emit == EMIT_NODE && c != 'B') { |
| 2286 |
*flag_param |= (HAS_WIDTH | SIMPLE); |
| 2287 |
} |
| 2288 |
|
| 2289 |
if (klas) { |
| 2290 |
/* |
| 2291 |
Emit bytes within a character class operand. |
| 2292 |
*/ |
| 2293 |
while (*klas != U'\0') |
| 2294 |
emit_byte (*klas ++); |
| 2295 |
} |
| 2296 |
|
| 2297 |
return ret_val; |
| 2298 |
} |
| 2299 |
|
| 2300 |
/*--------------------------------------------------------------------* |
| 2301 |
* numeric_escape |
| 2302 |
* |
| 2303 |
* Implements hex and octal numeric escape sequence syntax. |
| 2304 |
* |
| 2305 |
* Hexadecimal Escape: \x## Max of two digits Must have leading 'x'. |
| 2306 |
* Octal Escape: \0### Max of three digits and not greater |
| 2307 |
* than 377 octal. Must have leading zero. |
| 2308 |
* |
| 2309 |
* Returns the actual character value or NULL if not a valid hex or |
| 2310 |
* octal escape. REG_FAIL is called if \x0, \x00, \0, \00, \000, or |
| 2311 |
* \0000 is specified. |
| 2312 |
*--------------------------------------------------------------------*/ |
| 2313 |
|
| 2314 |
static char32 numeric_escape ( |
| 2315 |
char32 c, |
| 2316 |
char32 **parse) { |
| 2317 |
|
| 2318 |
static char32 digits [] = { 'f', 'e', 'd', 'c', 'b', 'a', 'F', 'E', 'D', 'C', 'B', 'A', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0', '\0' }; |
| 2319 |
|
| 2320 |
static unsigned int digit_val [] = { |
| 2321 |
15, 14, 13, 12, 11, 10, /* Lower case Hex digits */ |
| 2322 |
15, 14, 13, 12, 11, 10, /* Upper case Hex digits */ |
| 2323 |
9, 8, 7, 6, 5, 4, 3, 2, 1, 0 |
| 2324 |
}; /* Decimal Digits */ |
| 2325 |
|
| 2326 |
char32 *scan; |
| 2327 |
char32 *pos_ptr; |
| 2328 |
char32 *digit_str; |
| 2329 |
unsigned int value = 0; |
| 2330 |
unsigned int radix = 8; |
| 2331 |
int width = 3; /* Can not be bigger than \0377 */ |
| 2332 |
int pos_delta = 14; |
| 2333 |
int i, pos; |
| 2334 |
|
| 2335 |
switch (c) { |
| 2336 |
case U'0': |
| 2337 |
digit_str = digits + pos_delta; /* Only use Octal digits, i.e. 0-7. */ |
| 2338 |
break; |
| 2339 |
|
| 2340 |
case U'x': |
| 2341 |
case U'X': |
| 2342 |
width = 2; /* Can not be bigger than \0377 */ |
| 2343 |
radix = 16; |
| 2344 |
pos_delta = 0; |
| 2345 |
digit_str = digits; /* Use all of the digit characters. */ |
| 2346 |
break; |
| 2347 |
|
| 2348 |
default: |
| 2349 |
return U'\0'; /* Not a numeric escape */ |
| 2350 |
} |
| 2351 |
|
| 2352 |
scan = *parse; scan ++; /* Only change *parse on success. */ |
| 2353 |
|
| 2354 |
pos_ptr = str32chr (digit_str, *scan); |
| 2355 |
|
| 2356 |
for (i = 0; pos_ptr && i < width; i++) { |
| 2357 |
pos = (pos_ptr - digit_str) + pos_delta; |
| 2358 |
value = (value * radix) + digit_val [pos]; |
| 2359 |
|
| 2360 |
/* If this digit makes the value over 255, treat this digit as a literal |
| 2361 |
character instead of part of the numeric escape. For example, \0777 |
| 2362 |
will be processed as \077 (an 'M') and a literal '7' character, NOT |
| 2363 |
511 decimal which is > 255. */ |
| 2364 |
|
| 2365 |
if (value > 255) { |
| 2366 |
/* Back out calculations for last digit processed. */ |
| 2367 |
|
| 2368 |
value -= digit_val [pos]; |
| 2369 |
value /= radix; |
| 2370 |
|
| 2371 |
break; /* Note that scan will not be incremented and still points to |
| 2372 |
the digit that caused overflow. It will be decremented by |
| 2373 |
the "else" below to point to the last character that is |
| 2374 |
considered to be part of the octal escape. */ |
| 2375 |
} |
| 2376 |
|
| 2377 |
scan ++; |
| 2378 |
pos_ptr = str32chr (digit_str, *scan); |
| 2379 |
} |
| 2380 |
|
| 2381 |
/* Handle the case of "\0" i.e. trying to specify a NULL character. */ |
| 2382 |
|
| 2383 |
if (value == 0) { |
| 2384 |
if (c == U'0') { |
| 2385 |
Melder_sprint (Error_Text,128, U"\\00 is an invalid octal escape"); |
| 2386 |
} else { |
| 2387 |
Melder_sprint (Error_Text,128, U"\\", c, U"0 is an invalid hexadecimal escape"); |
| 2388 |
} |
| 2389 |
} else { |
| 2390 |
/* Point to the last character of the number on success. */ |
| 2391 |
|
| 2392 |
scan --; |
| 2393 |
*parse = scan; |
| 2394 |
} |
| 2395 |
|
| 2396 |
return value; |
| 2397 |
} |
| 2398 |
|
| 2399 |
/*--------------------------------------------------------------------* |
| 2400 |
* literal_escape |
| 2401 |
* |
| 2402 |
* Recognize escaped literal characters (prefixed with backslash), |
| 2403 |
* and translate them into the corresponding character. |
| 2404 |
* |
| 2405 |
* Returns the proper character value or NULL if not a valid literal |
| 2406 |
* escape. |
| 2407 |
*--------------------------------------------------------------------*/ |
| 2408 |
|
| 2409 |
static char32 literal_escape (char32 c) { |
| 2410 |
|
| 2411 |
static char32 valid_escape [] = { |
| 2412 |
'a', 'b', |
| 2413 |
'e', |
| 2414 |
'f', 'n', 'r', 't', 'v', '(', ')', '-', '[', ']', |
| 2415 |
'<', '>', '{', '}', '.', '\\', '|', '^', '$', '*', |
| 2416 |
'+', '?', '&', '\0' |
| 2417 |
}; |
| 2418 |
|
| 2419 |
static char32 value [] = { |
| 2420 |
'\a', '\b', |
| 2421 |
0x1B, /* Escape character in Unicode character set. */ |
| 2422 |
'\f', '\n', '\r', '\t', '\v', '(', ')', '-', '[', ']', |
| 2423 |
'<', '>', '{', '}', '.', '\\', '|', '^', '$', '*', |
| 2424 |
'+', '?', '&', '\0' |
| 2425 |
}; |
| 2426 |
|
| 2427 |
for (int i = 0; valid_escape [i] != '\0'; i++) { |
| 2428 |
if (c == valid_escape [i]) { |
| 2429 |
return value [i]; |
| 2430 |
} |
| 2431 |
} |
| 2432 |
|
| 2433 |
return '\0'; |
| 2434 |
} |
| 2435 |
|
| 2436 |
/*--------------------------------------------------------------------* |
| 2437 |
* back_ref |
| 2438 |
* |
| 2439 |
* Process a request to match a previous parenthesized thing. |
| 2440 |
* Parenthetical entities are numbered beginning at 1 by counting |
| 2441 |
* opening parentheses from left to to right. \0 would represent |
| 2442 |
* whole match, but would confuse numeric_escape as an octal escape, |
| 2443 |
* so it is forbidden. |
| 2444 |
* |
| 2445 |
* Constructs of the form \~1, \~2, etc. are cross-regex back |
| 2446 |
* references and are used in syntax highlighting patterns to match |
| 2447 |
* text previously matched by another regex. *** IMPLEMENT LATER *** |
| 2448 |
*--------------------------------------------------------------------*/ |
| 2449 |
|
| 2450 |
static char32 *back_ref ( |
| 2451 |
char32 *c, |
| 2452 |
int *flag_param, |
| 2453 |
int emit) { |
| 2454 |
|
| 2455 |
int paren_no, c_offset = 0, is_cross_regex = 0; |
| 2456 |
|
| 2457 |
char32 *ret_val; |
| 2458 |
|
| 2459 |
/* Implement cross regex backreferences later. */ |
| 2460 |
|
| 2461 |
/* if (*c == (regularExp_CHAR) ('~')) { |
| 2462 |
c_offset++; |
| 2463 |
is_cross_regex++; |
| 2464 |
} */ |
| 2465 |
|
| 2466 |
paren_no = (int) (* (c + c_offset) - (char32) ('0')); |
| 2467 |
|
| 2468 |
if (! Melder_isAsciiDecimalNumber (* (c + c_offset)) || /* Only \1, \2, ... \9 are supported. */ |
| 2469 |
paren_no == 0) { /* Should be caught by numeric_escape. */ |
| 2470 |
|
| 2471 |
return NULL; |
| 2472 |
} |
| 2473 |
|
| 2474 |
/* Make sure parentheses for requested back-reference are complete. */ |
| 2475 |
|
| 2476 |
if (! is_cross_regex && ! TEST_BIT (Closed_Parens, paren_no)) { |
| 2477 |
Melder_sprint (Error_Text,128, U"\\", paren_no, U" is an illegal back reference"); |
| 2478 |
return NULL; |
| 2479 |
} |
| 2480 |
|
| 2481 |
if (emit == EMIT_NODE) { |
| 2482 |
if (is_cross_regex) { |
| 2483 |
Reg_Parse ++; /* Skip past the '~' in a cross regex back reference. |
| 2484 |
We only do this if we are emitting code. */ |
| 2485 |
|
| 2486 |
if (Is_Case_Insensitive) { |
| 2487 |
ret_val = emit_node (X_REGEX_BR_CI); |
| 2488 |
} else { |
| 2489 |
ret_val = emit_node (X_REGEX_BR); |
| 2490 |
} |
| 2491 |
} else { |
| 2492 |
if (Is_Case_Insensitive) { |
| 2493 |
ret_val = emit_node (BACK_REF_CI); |
| 2494 |
} else { |
| 2495 |
ret_val = emit_node (BACK_REF); |
| 2496 |
} |
| 2497 |
} |
| 2498 |
|
| 2499 |
emit_byte ((char32) paren_no); |
| 2500 |
|
| 2501 |
if (is_cross_regex || TEST_BIT (Paren_Has_Width, paren_no)) { |
| 2502 |
*flag_param |= HAS_WIDTH; |
| 2503 |
} |
| 2504 |
} else if (emit == CHECK_ESCAPE) { |
| 2505 |
ret_val = (char32 *) 1; |
| 2506 |
} else { |
| 2507 |
ret_val = nullptr; |
| 2508 |
} |
| 2509 |
return ret_val; |
| 2510 |
} |
| 2511 |
|
| 2512 |
/*======================================================================* |
| 2513 |
* Regex execution related code |
| 2514 |
*======================================================================*/ |
| 2515 |
|
| 2516 |
/* Global work variables for `ExecRE'. */ |
| 2517 |
|
| 2518 |
static char32 *Reg_Input; /* String-input pointer. */ |
| 2519 |
static const char32 *Start_Of_String; /* Beginning of input, for ^ */ |
| 2520 |
/* and < checks. */ |
| 2521 |
static const char32 *End_Of_String; /* Logical end of input (if supplied, till \0 otherwise) */ |
| 2522 |
static const char32 *Look_Behind_To; /* Position till were look behind can safely check back */ |
| 2523 |
static char32 **Start_Ptr_Ptr; /* Pointer to `startp' array. */ |
| 2524 |
static char32 **End_Ptr_Ptr; /* Ditto for `endp'. */ |
| 2525 |
static char32 *Extent_Ptr_FW; /* Forward extent pointer */ |
| 2526 |
static char32 *Extent_Ptr_BW; /* Backward extent pointer */ |
| 2527 |
static char32 *Back_Ref_Start [10]; /* Back_Ref_Start [0] and */ |
| 2528 |
static char32 *Back_Ref_End [10]; /* Back_Ref_End [0] are not */ |
| 2529 |
/* used. This simplifies */ |
| 2530 |
/* indexing. */ |
| 2531 |
/* |
| 2532 |
* Measured recursion limits: |
| 2533 |
* Linux: +/- 40 000 (up to 110 000) |
| 2534 |
* Solaris: +/- 85 000 |
| 2535 |
* HP-UX 11: +/- 325 000 |
| 2536 |
* |
| 2537 |
* So 10 000 ought to be safe. |
| 2538 |
*/ |
| 2539 |
#define REGEX_RECURSION_LIMIT 10000 |
| 2540 |
static int Recursion_Count; /* Recursion counter */ |
| 2541 |
static int Recursion_Limit_Exceeded; /* Recursion limit exceeded flag */ |
| 2542 |
|
| 2543 |
#define AT_END_OF_STRING(X) (*(X) == U'\0' || (End_Of_String != NULL && (X) >= End_Of_String)) |
| 2544 |
|
| 2545 |
/* static regexp *Cross_Regex_Backref; */ |
| 2546 |
|
| 2547 |
static bool Prev_Is_BOL; |
| 2548 |
static bool Succ_Is_EOL; |
| 2549 |
static bool Prev_Is_Delim; |
| 2550 |
static bool Succ_Is_Delim; |
| 2551 |
|
| 2552 |
/* Define a pointer to an array to hold general (...){m,n} counts. */ |
| 2553 |
|
| 2554 |
typedef struct brace_counts { |
| 2555 |
unsigned long count [1]; /* More unwarranted chumminess with compiler. */ |
| 2556 |
} brace_counts; |
| 2557 |
|
| 2558 |
static struct brace_counts *Brace; |
| 2559 |
|
| 2560 |
/* Forward declarations of functions used by `ExecRE' */ |
| 2561 |
|
| 2562 |
static int attempt (regexp *, char32 *); |
| 2563 |
static int match (char32 *, int *); |
| 2564 |
static unsigned long greedy (char32 *, long); |
| 2565 |
static void adjustcase (char32 *, int, char32); |
| 2566 |
|
| 2567 |
/* |
| 2568 |
* ExecRE - match a `regexp' structure against a string |
| 2569 |
* |
| 2570 |
* If `end' is non-NULL, matches may not BEGIN past end, but may extend past |
| 2571 |
* it. If reverse is true, `end' should be specified, and searching begins at |
| 2572 |
* `end'. "isbol" should be set to true if the beginning of the string is the |
| 2573 |
* actual beginning of a line (since `ExecRE' can't look backwards from the |
| 2574 |
* beginning to find whether there was a newline before). Likewise, "isbow" |
| 2575 |
* asks whether the string is preceded by a word delimiter. End of string is |
| 2576 |
* always treated as a word and line boundary (there may be cases where it |
| 2577 |
* shouldn't be, in which case, this should be changed). |
| 2578 |
* Look_behind_to indicates the position till where it is safe to |
| 2579 |
* perform look-behind matches. If set, it should be smaller than or equal |
| 2580 |
* to the start position of the search (pointed at by string). If it is NULL, |
| 2581 |
* it defaults to the start position. |
| 2582 |
* Finally, match_to indicates the logical end of the string, till where |
| 2583 |
* matches are allowed to extend. Note that look-ahead patterns may look |
| 2584 |
* past that boundary. If match_to is set to NULL, the terminating \0 is |
| 2585 |
* assumed to correspond to the logical boundary. Match_to, if set, should be |
| 2586 |
* larger than or equal to end, if set. |
| 2587 |
*/ |
| 2588 |
|
| 2589 |
int ExecRE ( |
| 2590 |
regexp *prog, |
| 2591 |
regexp *cross_regex_backref, |
| 2592 |
conststring32 string, |
| 2593 |
const char32 *end, |
| 2594 |
int reverse, |
| 2595 |
char32 prev_char, |
| 2596 |
char32 succ_char, |
| 2597 |
conststring32 look_behind_to, |
| 2598 |
conststring32 match_to) { |
| 2599 |
|
| 2600 |
char32 *str; |
| 2601 |
char32 **s_ptr; |
| 2602 |
char32 **e_ptr; |
| 2603 |
int ret_val = 0; |
| 2604 |
int i; |
| 2605 |
(void) cross_regex_backref; |
| 2606 |
|
| 2607 |
s_ptr = (char32 **) prog->startp; |
| 2608 |
e_ptr = (char32 **) prog->endp; |
| 2609 |
|
| 2610 |
/* Check for valid parameters. */ |
| 2611 |
|
| 2612 |
if (! prog || ! string) { |
| 2613 |
reg_error (U"NULL parameter to `ExecRE\'"); |
| 2614 |
goto SINGLE_RETURN; |
| 2615 |
} |
| 2616 |
|
| 2617 |
/* Check validity of program. */ |
| 2618 |
|
| 2619 |
if (U_CHAR_AT (prog->program) != MAGIC) { |
| 2620 |
reg_error (U"corrupted program"); |
| 2621 |
goto SINGLE_RETURN; |
| 2622 |
} |
| 2623 |
|
| 2624 |
/* Remember the logical end of the string. */ |
| 2625 |
|
| 2626 |
End_Of_String = match_to; |
| 2627 |
|
| 2628 |
if (reverse && ! end) { |
| 2629 |
for (end = string; ! AT_END_OF_STRING (end); end ++) { } |
| 2630 |
succ_char = U'\n'; |
| 2631 |
} else if (! end) { |
| 2632 |
succ_char = U'\n'; |
| 2633 |
} |
| 2634 |
|
| 2635 |
/* Remember the beginning of the string for matching BOL */ |
| 2636 |
|
| 2637 |
Start_Of_String = string; |
| 2638 |
Look_Behind_To = look_behind_to ? look_behind_to : string; |
| 2639 |
|
| 2640 |
Prev_Is_BOL = ( prev_char == U'\n' || prev_char == U'\0' ); |
| 2641 |
Succ_Is_EOL = ( succ_char == U'\n' || succ_char == U'\0' ); |
| 2642 |
Prev_Is_Delim = ! Melder_isWordCharacter (prev_char); |
| 2643 |
Succ_Is_Delim = ! Melder_isWordCharacter (succ_char); |
| 2644 |
|
| 2645 |
Total_Paren = (int) (prog->program [1]); |
| 2646 |
Num_Braces = (int) (prog->program [2]); |
| 2647 |
|
| 2648 |
/* Reset the recursion detection flag */ |
| 2649 |
Recursion_Limit_Exceeded = 0; |
| 2650 |
|
| 2651 |
/* Cross_Regex_Backref = cross_regex_backref; */ |
| 2652 |
|
| 2653 |
/* Allocate memory for {m,n} construct counting variables if need be. */ |
| 2654 |
|
| 2655 |
if (Num_Braces > 0) { |
| 2656 |
Brace = |
| 2657 |
(brace_counts *) malloc (sizeof (brace_counts) * (size_t) Num_Braces); |
| 2658 |
|
| 2659 |
if (Brace == NULL) { |
| 2660 |
reg_error (U"out of memory in `ExecRE\'"); |
| 2661 |
goto SINGLE_RETURN; |
| 2662 |
} |
| 2663 |
} else { |
| 2664 |
Brace = NULL; |
| 2665 |
} |
| 2666 |
|
| 2667 |
/* Initialize the first nine (9) capturing parentheses start and end |
| 2668 |
pointers to point to the start of the search string. This is to prevent |
| 2669 |
crashes when later trying to reference captured parens that do not exist |
| 2670 |
in the compiled regex. We only need to do the first nine since users |
| 2671 |
can only specify \1, \2, ... \9. */ |
| 2672 |
|
| 2673 |
for (i = 9; i > 0; i--) { |
| 2674 |
*s_ptr++ = (char32 *) string; |
| 2675 |
*e_ptr++ = (char32 *) string; |
| 2676 |
} |
| 2677 |
|
| 2678 |
if (!reverse) { /* Forward Search */ |
| 2679 |
if (prog->anchor) { |
| 2680 |
/* Search is anchored at BOL */ |
| 2681 |
|
| 2682 |
if (attempt (prog, (char32 *) string)) { |
| 2683 |
ret_val = 1; |
| 2684 |
goto SINGLE_RETURN; |
| 2685 |
} |
| 2686 |
|
| 2687 |
for (str = (char32 *) string; |
| 2688 |
!AT_END_OF_STRING (str) && str != (char32 *) end && !Recursion_Limit_Exceeded; |
| 2689 |
str++) { |
| 2690 |
|
| 2691 |
if (*str == '\n') { |
| 2692 |
if (attempt (prog, str + 1)) { |
| 2693 |
ret_val = 1; |
| 2694 |
break; |
| 2695 |
} |
| 2696 |
} |
| 2697 |
} |
| 2698 |
|
| 2699 |
goto SINGLE_RETURN; |
| 2700 |
|
| 2701 |
} else if (prog->match_start != '\0') { |
| 2702 |
/* We know what char match must start with. */ |
| 2703 |
|
| 2704 |
for (str = (char32 *) string; |
| 2705 |
!AT_END_OF_STRING (str) && str != (char32 *) end && !Recursion_Limit_Exceeded; |
| 2706 |
str++) { |
| 2707 |
|
| 2708 |
if (*str == (char32) prog->match_start) { |
| 2709 |
if (attempt (prog, str)) { |
| 2710 |
ret_val = 1; |
| 2711 |
break; |
| 2712 |
} |
| 2713 |
} |
| 2714 |
} |
| 2715 |
|
| 2716 |
goto SINGLE_RETURN; |
| 2717 |
} else { |
| 2718 |
/* General case */ |
| 2719 |
|
| 2720 |
for (str = (char32 *) string; |
| 2721 |
!AT_END_OF_STRING (str) && str != (char32 *) end && !Recursion_Limit_Exceeded; |
| 2722 |
str++) { |
| 2723 |
|
| 2724 |
if (attempt (prog, str)) { |
| 2725 |
ret_val = 1; |
| 2726 |
break; |
| 2727 |
} |
| 2728 |
} |
| 2729 |
|
| 2730 |
/* Beware of a single $ matching \0 */ |
| 2731 |
if (!Recursion_Limit_Exceeded && !ret_val && AT_END_OF_STRING (str) && str != (char32 *) end) { |
| 2732 |
if (attempt (prog, str)) { |
| 2733 |
ret_val = 1; |
| 2734 |
} |
| 2735 |
} |
| 2736 |
|
| 2737 |
goto SINGLE_RETURN; |
| 2738 |
} |
| 2739 |
} else { /* Search reverse, same as forward, but loops run backward */ |
| 2740 |
|
| 2741 |
/* Make sure that we don't start matching beyond the logical end */ |
| 2742 |
if (End_Of_String != NULL && (char32 *) end > End_Of_String) { |
| 2743 |
end = (const char32 *) End_Of_String; |
| 2744 |
} |
| 2745 |
|
| 2746 |
if (prog->anchor) { |
| 2747 |
/* Search is anchored at BOL */ |
| 2748 |
|
| 2749 |
for (str = (char32 *) (end - 1); str >= (char32 *) string && !Recursion_Limit_Exceeded; str--) { |
| 2750 |
|
| 2751 |
if (*str == '\n') { |
| 2752 |
if (attempt (prog, str + 1)) { |
| 2753 |
ret_val = 1; |
| 2754 |
goto SINGLE_RETURN; |
| 2755 |
} |
| 2756 |
} |
| 2757 |
} |
| 2758 |
|
| 2759 |
if (!Recursion_Limit_Exceeded && attempt (prog, (char32 *) string)) { |
| 2760 |
ret_val = 1; |
| 2761 |
goto SINGLE_RETURN; |
| 2762 |
} |
| 2763 |
|
| 2764 |
goto SINGLE_RETURN; |
| 2765 |
} else if (prog->match_start != '\0') { |
| 2766 |
/* We know what char match must start with. */ |
| 2767 |
|
| 2768 |
for (str = (char32 *) end; str >= (char32 *) string && !Recursion_Limit_Exceeded; str--) { |
| 2769 |
|
| 2770 |
if (*str == (char32) prog->match_start) { |
| 2771 |
if (attempt (prog, str)) { |
| 2772 |
ret_val = 1; |
| 2773 |
break; |
| 2774 |
} |
| 2775 |
} |
| 2776 |
} |
| 2777 |
|
| 2778 |
goto SINGLE_RETURN; |
| 2779 |
} else { |
| 2780 |
/* General case */ |
| 2781 |
|
| 2782 |
for (str = (char32 *) end; str >= (char32 *) string && !Recursion_Limit_Exceeded; str--) { |
| 2783 |
|
| 2784 |
if (attempt (prog, str)) { |
| 2785 |
ret_val = 1; |
| 2786 |
break; |
| 2787 |
} |
| 2788 |
} |
| 2789 |
} |
| 2790 |
} |
| 2791 |
|
| 2792 |
SINGLE_RETURN: if (Brace) { |
| 2793 |
free (Brace); |
| 2794 |
} |
| 2795 |
|
| 2796 |
if (Recursion_Limit_Exceeded) { |
| 2797 |
return (0); |
| 2798 |
} |
| 2799 |
|
| 2800 |
return (ret_val); |
| 2801 |
} |
| 2802 |
|
| 2803 |
static int attempt (regexp *prog, char32 *string) { |
| 2804 |
|
| 2805 |
int i; |
| 2806 |
char32 **s_ptr; |
| 2807 |
char32 **e_ptr; |
| 2808 |
int branch_index = 0; /* Must be set to zero ! */ |
| 2809 |
|
| 2810 |
Reg_Input = string; |
| 2811 |
Start_Ptr_Ptr = (char32 **) prog->startp; |
| 2812 |
End_Ptr_Ptr = (char32 **) prog->endp; |
| 2813 |
s_ptr = (char32 **) prog->startp; |
| 2814 |
e_ptr = (char32 **) prog->endp; |
| 2815 |
|
| 2816 |
/* Reset the recursion counter. */ |
| 2817 |
Recursion_Count = 0; |
| 2818 |
|
| 2819 |
/* Overhead due to capturing parentheses. */ |
| 2820 |
|
| 2821 |
Extent_Ptr_BW = string; |
| 2822 |
Extent_Ptr_FW = nullptr; |
| 2823 |
|
| 2824 |
for (i = Total_Paren + 1; i > 0; i--) { |
| 2825 |
*s_ptr ++ = nullptr; |
| 2826 |
*e_ptr ++ = nullptr; |
| 2827 |
} |
| 2828 |
|
| 2829 |
if (match ( (char32 *) (prog->program + REGEX_START_OFFSET), |
| 2830 |
&branch_index)) { |
| 2831 |
prog->startp [0] = (char32 *) string; |
| 2832 |
prog->endp [0] = (char32 *) Reg_Input; /* <-- One char AFTER */ |
| 2833 |
prog->extentpBW = (char32 *) Extent_Ptr_BW; /* matched string! */ |
| 2834 |
prog->extentpFW = (char32 *) Extent_Ptr_FW; |
| 2835 |
prog->top_branch = branch_index; |
| 2836 |
|
| 2837 |
return 1; |
| 2838 |
} else { |
| 2839 |
return 0; |
| 2840 |
} |
| 2841 |
} |
| 2842 |
|
| 2843 |
/*----------------------------------------------------------------------* |
| 2844 |
* match - main matching routine |
| 2845 |
* |
| 2846 |
* Conceptually the strategy is simple: check to see whether the |
| 2847 |
* current node matches, call self recursively to see whether the rest |
| 2848 |
* matches, and then act accordingly. In practice we make some effort |
| 2849 |
* to avoid recursion, in particular by going through "ordinary" nodes |
| 2850 |
* (that don't need to know whether the rest of the match failed) by a |
| 2851 |
* loop instead of by recursion. Returns 0 failure, 1 success. |
| 2852 |
*----------------------------------------------------------------------*/ |
| 2853 |
#define MATCH_RETURN(X)\ |
| 2854 |
{ --Recursion_Count; return (X); } |
| 2855 |
#define CHECK_RECURSION_LIMIT\ |
| 2856 |
if (Recursion_Limit_Exceeded) MATCH_RETURN (0); |
| 2857 |
|
| 2858 |
static int match (char32 *prog, int *branch_index_param) { |
| 2859 |
|
| 2860 |
char32 *scan; /* Current node. */ |
| 2861 |
char32 *next; /* Next node. */ |
| 2862 |
int next_ptr_offset; /* Used by the NEXT_PTR () macro */ |
| 2863 |
|
| 2864 |
if (++ Recursion_Count > REGEX_RECURSION_LIMIT) { |
| 2865 |
if (! Recursion_Limit_Exceeded) { /* Prevent duplicate errors */ |
| 2866 |
reg_error (U"recursion limit exceeded, please respecify expression"); |
| 2867 |
} |
| 2868 |
Recursion_Limit_Exceeded = 1; |
| 2869 |
MATCH_RETURN (0) |
| 2870 |
} |
| 2871 |
|
| 2872 |
|
| 2873 |
scan = prog; |
| 2874 |
|
| 2875 |
while (scan) { |
| 2876 |
NEXT_PTR (scan, next); |
| 2877 |
|
| 2878 |
switch (GET_OP_CODE (scan)) { |
| 2879 |
case BRANCH: { |
| 2880 |
char32 *save; |
| 2881 |
int branch_index_local = 0; |
| 2882 |
|
| 2883 |
if (GET_OP_CODE (next) != BRANCH) { /* No choice. */ |
| 2884 |
next = OPERAND (scan); /* Avoid recursion. */ |
| 2885 |
} else { |
| 2886 |
do { |
| 2887 |
save = Reg_Input; |
| 2888 |
|
| 2889 |
if (match (OPERAND (scan), NULL)) { |
| 2890 |
if (branch_index_param) |
| 2891 |
*branch_index_param = branch_index_local; |
| 2892 |
MATCH_RETURN (1) |
| 2893 |
} |
| 2894 |
|
| 2895 |
CHECK_RECURSION_LIMIT |
| 2896 |
|
| 2897 |
++branch_index_local; |
| 2898 |
|
| 2899 |
Reg_Input = save; /* Backtrack. */ |
| 2900 |
NEXT_PTR (scan, scan); |
| 2901 |
} while (scan != NULL && GET_OP_CODE (scan) == BRANCH); |
| 2902 |
|
| 2903 |
MATCH_RETURN (0) // NOT REACHED |
| 2904 |
} |
| 2905 |
} |
| 2906 |
|
| 2907 |
break; |
| 2908 |
|
| 2909 |
case EXACTLY: { |
| 2910 |
char32 *opnd = OPERAND (scan); |
| 2911 |
|
| 2912 |
/* Inline the first character, for speed. */ |
| 2913 |
|
| 2914 |
if (*opnd != *Reg_Input) |
| 2915 |
MATCH_RETURN (0) |
| 2916 |
int len = Melder_length (opnd); |
| 2917 |
if (End_Of_String != NULL && Reg_Input + len > End_Of_String) |
| 2918 |
MATCH_RETURN (0) |
| 2919 |
if (len > 1 && str32ncmp (opnd, Reg_Input, len) != 0) |
| 2920 |
MATCH_RETURN (0) |
| 2921 |
Reg_Input += len; |
| 2922 |
} |
| 2923 |
|
| 2924 |
break; |
| 2925 |
|
| 2926 |
case SIMILAR: { |
| 2927 |
char32 *opnd = OPERAND (scan); |
| 2928 |
char32 test; |
| 2929 |
/* Note: the SIMILAR operand was converted to lower case during |
| 2930 |
regex compile. */ |
| 2931 |
|
| 2932 |
while ((test = *opnd++) != U'\0') { |
| 2933 |
if (AT_END_OF_STRING (Reg_Input) || Melder_toLowerCase (*Reg_Input++) != test) |
| 2934 |
MATCH_RETURN (0) |
| 2935 |
} |
| 2936 |
} |
| 2937 |
|
| 2938 |
break; |
| 2939 |
|
| 2940 |
case BOL: /* `^' (beginning of line anchor) */ |
| 2941 |
if (Reg_Input == Start_Of_String) { |
| 2942 |
if (Prev_Is_BOL) |
| 2943 |
break; |
| 2944 |
} else if (* (Reg_Input - 1) == U'\n') { |
| 2945 |
break; |
| 2946 |
} |
| 2947 |
MATCH_RETURN (0) |
| 2948 |
|
| 2949 |
case EOL: /* `$' anchor matches end of line and end of string */ |
| 2950 |
if (*Reg_Input == U'\n' || (AT_END_OF_STRING (Reg_Input) && Succ_Is_EOL)) |
| 2951 |
break; |
| 2952 |
MATCH_RETURN (0) |
| 2953 |
|
| 2954 |
case BOWORD: /* `<' (beginning of word anchor) */ |
| 2955 |
/* Check to see if the current character is not a delimiter |
| 2956 |
and the preceding character is. */ |
| 2957 |
{ |
| 2958 |
bool prev_is_delim; |
| 2959 |
if (Reg_Input == Start_Of_String) { |
| 2960 |
prev_is_delim = Prev_Is_Delim; |
| 2961 |
} else { |
| 2962 |
prev_is_delim = ! Melder_isWordCharacter (*(Reg_Input - 1)); |
| 2963 |
} |
| 2964 |
if (prev_is_delim) { |
| 2965 |
bool current_is_delim; |
| 2966 |
if (AT_END_OF_STRING (Reg_Input)) { |
| 2967 |
current_is_delim = Succ_Is_Delim; |
| 2968 |
} else { |
| 2969 |
current_is_delim = ! Melder_isWordCharacter (*Reg_Input); |
| 2970 |
} |
| 2971 |
if (! current_is_delim) |
| 2972 |
break; |
| 2973 |
} |
| 2974 |
} |
| 2975 |
MATCH_RETURN (0) |
| 2976 |
|
| 2977 |
case EOWORD: /* `>' (end of word anchor) */ |
| 2978 |
/* Check to see if the current character is a delimiter |
| 2979 |
and the preceding character is not. */ |
| 2980 |
{ |
| 2981 |
bool prev_is_delim; |
| 2982 |
if (Reg_Input == Start_Of_String) { |
| 2983 |
prev_is_delim = Prev_Is_Delim; |
| 2984 |
} else { |
| 2985 |
prev_is_delim = ! Melder_isWordCharacter (*(Reg_Input - 1)); |
| 2986 |
} |
| 2987 |
if (! prev_is_delim) { |
| 2988 |
bool current_is_delim; |
| 2989 |
if (AT_END_OF_STRING (Reg_Input)) { |
| 2990 |
current_is_delim = Succ_Is_Delim; |
| 2991 |
} else { |
| 2992 |
current_is_delim = ! Melder_isWordCharacter (*Reg_Input); |
| 2993 |
} |
| 2994 |
if (current_is_delim) |
| 2995 |
break; |
| 2996 |
} |
| 2997 |
} |
| 2998 |
MATCH_RETURN (0) |
| 2999 |
|
| 3000 |
case NOT_BOUNDARY: { /* \B (NOT a word boundary) */ |
| 3001 |
bool prev_is_delim; |
| 3002 |
bool current_is_delim; |
| 3003 |
if (Reg_Input == Start_Of_String) { |
| 3004 |
prev_is_delim = Prev_Is_Delim; |
| 3005 |
} else { |
| 3006 |
prev_is_delim = ! Melder_isWordCharacter (*(Reg_Input - 1)); |
| 3007 |
} |
| 3008 |
if (AT_END_OF_STRING (Reg_Input)) { |
| 3009 |
current_is_delim = Succ_Is_Delim; |
| 3010 |
} else { |
| 3011 |
current_is_delim = ! Melder_isWordCharacter (*Reg_Input); |
| 3012 |
} |
| 3013 |
if (! (prev_is_delim ^ current_is_delim)) |
| 3014 |
break; |
| 3015 |
} |
| 3016 |
MATCH_RETURN (0) |
| 3017 |
case IS_DELIM: /* \y (A word delimiter character.) */ |
| 3018 |
if (! Melder_isWordCharacter (*Reg_Input) && ! AT_END_OF_STRING (Reg_Input)) { |
| 3019 |
Reg_Input++; break; |
| 3020 |
} |
| 3021 |
MATCH_RETURN (0) |
| 3022 |
case NOT_DELIM: /* \Y (NOT a word delimiter character.) */ |
| 3023 |
if (Melder_isWordCharacter (*Reg_Input) && ! AT_END_OF_STRING (Reg_Input)) { |
| 3024 |
Reg_Input ++; |
| 3025 |
break; |
| 3026 |
} |
| 3027 |
MATCH_RETURN (0) |
| 3028 |
case WORD_CHAR: /* \w (word character; alpha-numeric or underscore) */ |
| 3029 |
if (Melder_isWordCharacter (*Reg_Input) && ! AT_END_OF_STRING (Reg_Input)) { |
| 3030 |
Reg_Input ++; |
| 3031 |
break; |
| 3032 |
} |
| 3033 |
MATCH_RETURN (0) |
| 3034 |
case NOT_WORD_CHAR:/* \W (NOT a word character) */ |
| 3035 |
if (Melder_isWordCharacter (*Reg_Input) || *Reg_Input == U'\n' || AT_END_OF_STRING (Reg_Input)) |
| 3036 |
MATCH_RETURN (0) |
| 3037 |
Reg_Input ++; |
| 3038 |
break; |
| 3039 |
case ANY: /* `.' (matches any character EXCEPT newline) */ |
| 3040 |
if (AT_END_OF_STRING (Reg_Input) || *Reg_Input == U'\n') |
| 3041 |
MATCH_RETURN (0) |
| 3042 |
Reg_Input ++; |
| 3043 |
break; |
| 3044 |
case EVERY: /* `.' (matches any character INCLUDING newline) */ |
| 3045 |
if (AT_END_OF_STRING (Reg_Input)) |
| 3046 |
MATCH_RETURN (0) |
| 3047 |
Reg_Input ++; |
| 3048 |
break; |
| 3049 |
case DIGIT: /* \d; for ASCII, use [0-9] */ |
| 3050 |
if (! Melder_isDecimalNumber (*Reg_Input) || AT_END_OF_STRING (Reg_Input)) |
| 3051 |
MATCH_RETURN (0) |
| 3052 |
Reg_Input ++; |
| 3053 |
break; |
| 3054 |
case NOT_DIGIT: /* \D; for ASCII, use [^0-9] */ |
| 3055 |
if (Melder_isDecimalNumber (*Reg_Input) || *Reg_Input == U'\n' || AT_END_OF_STRING (Reg_Input)) |
| 3056 |
MATCH_RETURN (0) |
| 3057 |
Reg_Input ++; |
| 3058 |
break; |
| 3059 |
case LETTER: /* \l; for ASCII, use [a-zA-Z] */ |
| 3060 |
if (! Melder_isLetter (*Reg_Input) || AT_END_OF_STRING (Reg_Input)) |
| 3061 |
MATCH_RETURN (0) |
| 3062 |
Reg_Input ++; |
| 3063 |
break; |
| 3064 |
case NOT_LETTER: /* \L; for ASCII, use [^a-zA-Z] */ |
| 3065 |
if (Melder_isLetter (*Reg_Input) || *Reg_Input == '\n' || AT_END_OF_STRING (Reg_Input)) |
| 3066 |
MATCH_RETURN (0) |
| 3067 |
Reg_Input ++; |
| 3068 |
break; |
| 3069 |
case SPACE: /* \s; for ASCII, use [ \t] */ |
| 3070 |
if (! Melder_isHorizontalSpace (*Reg_Input) || AT_END_OF_STRING (Reg_Input)) |
| 3071 |
MATCH_RETURN (0) |
| 3072 |
Reg_Input ++; |
| 3073 |
break; |
| 3074 |
case SPACE_NL: /* \s; for ASCII, use [\n \t\r\f\v] */ |
| 3075 |
if (! Melder_isHorizontalOrVerticalSpace (*Reg_Input) || AT_END_OF_STRING (Reg_Input)) |
| 3076 |
MATCH_RETURN (0) |
| 3077 |
Reg_Input ++; |
| 3078 |
break; |
| 3079 |
case NOT_SPACE: /* \S; for ASCII, use [^\n \t\r\f\v] */ |
| 3080 |
if (Melder_isHorizontalOrVerticalSpace (*Reg_Input) || AT_END_OF_STRING (Reg_Input)) |
| 3081 |
MATCH_RETURN (0) |
| 3082 |
Reg_Input ++; |
| 3083 |
break; |
| 3084 |
case NOT_SPACE_NL: /* \S; for ASCII, use [^ \t\r\f\v] */ |
| 3085 |
if (Melder_isHorizontalSpace (*Reg_Input) || AT_END_OF_STRING (Reg_Input)) |
| 3086 |
MATCH_RETURN (0) |
| 3087 |
Reg_Input ++; |
| 3088 |
break; |
| 3089 |
case ANY_OF: /* [...] character class. */ |
| 3090 |
if (AT_END_OF_STRING (Reg_Input)) |
| 3091 |
MATCH_RETURN (0) |
| 3092 |
/* Needed because strchr () |
| 3093 |
considers \0 as a member |
| 3094 |
of the character set. */ |
| 3095 |
|
| 3096 |
if (! str32chr (OPERAND (scan), *Reg_Input)) |
| 3097 |
MATCH_RETURN (0) |
| 3098 |
Reg_Input ++; |
| 3099 |
break; |
| 3100 |
case ANY_BUT: /* [^...] Negated character class-- does NOT normally |
| 3101 |
match newline (\n added usually to operand at compile |
| 3102 |
time.) */ |
| 3103 |
|
| 3104 |
if (AT_END_OF_STRING (Reg_Input)) { |
| 3105 |
MATCH_RETURN (0); /* See comment for ANY_OF. */ |
| 3106 |
} |
| 3107 |
if (str32chr (OPERAND (scan), *Reg_Input) != nullptr) |
| 3108 |
MATCH_RETURN (0) |
| 3109 |
Reg_Input ++; |
| 3110 |
break; |
| 3111 |
case NOTHING: |
| 3112 |
case BACK: |
| 3113 |
break; |
| 3114 |
case STAR: |
| 3115 |
case PLUS: |
| 3116 |
case QUESTION: |
| 3117 |
case BRACE: |
| 3118 |
|
| 3119 |
case LAZY_STAR: |
| 3120 |
case LAZY_PLUS: |
| 3121 |
case LAZY_QUESTION: |
| 3122 |
case LAZY_BRACE: { |
| 3123 |
unsigned long num_matched = REG_ZERO; |
| 3124 |
unsigned long min = ULONG_MAX, max = REG_ZERO; |
| 3125 |
char32 *save; |
| 3126 |
char32 next_char; |
| 3127 |
char32 *next_op; |
| 3128 |
int lazy = 0; |
| 3129 |
|
| 3130 |
/* Lookahead (when possible) to avoid useless match attempts |
| 3131 |
when we know what character comes next. */ |
| 3132 |
|
| 3133 |
if (GET_OP_CODE (next) == EXACTLY) { |
| 3134 |
next_char = *OPERAND (next); |
| 3135 |
} else { |
| 3136 |
next_char = U'\0';/* i.e. Don't know what next character is. */ |
| 3137 |
} |
| 3138 |
|
| 3139 |
next_op = OPERAND (scan); |
| 3140 |
|
| 3141 |
switch (GET_OP_CODE (scan)) { |
| 3142 |
case LAZY_STAR: |
| 3143 |
lazy = 1; |
| 3144 |
case STAR: |
| 3145 |
min = REG_ZERO; |
| 3146 |
max = ULONG_MAX; |
| 3147 |
break; |
| 3148 |
|
| 3149 |
case LAZY_PLUS: |
| 3150 |
lazy = 1; |
| 3151 |
case PLUS: |
| 3152 |
min = REG_ONE; |
| 3153 |
max = ULONG_MAX; |
| 3154 |
break; |
| 3155 |
|
| 3156 |
case LAZY_QUESTION: |
| 3157 |
lazy = 1; |
| 3158 |
case QUESTION: |
| 3159 |
min = REG_ZERO; |
| 3160 |
max = REG_ONE; |
| 3161 |
break; |
| 3162 |
|
| 3163 |
case LAZY_BRACE: |
| 3164 |
lazy = 1; |
| 3165 |
case BRACE: |
| 3166 |
min = (unsigned long) |
| 3167 |
GET_OFFSET (scan + NEXT_PTR_SIZE); |
| 3168 |
|
| 3169 |
max = (unsigned long) |
| 3170 |
GET_OFFSET (scan + (2 * NEXT_PTR_SIZE)); |
| 3171 |
|
| 3172 |
if (max <= REG_INFINITY) { |
| 3173 |
max = ULONG_MAX; |
| 3174 |
} |
| 3175 |
|
| 3176 |
next_op = OPERAND (scan + (2 * NEXT_PTR_SIZE)); |
| 3177 |
} |
| 3178 |
|
| 3179 |
save = Reg_Input; |
| 3180 |
|
| 3181 |
if (lazy) { |
| 3182 |
if (min > REG_ZERO) { |
| 3183 |
num_matched = greedy (next_op, min); |
| 3184 |
} |
| 3185 |
} else { |
| 3186 |
num_matched = greedy (next_op, max); |
| 3187 |
} |
| 3188 |
|
| 3189 |
while (min <= num_matched && num_matched <= max) { |
| 3190 |
if (next_char == U'\0' || next_char == *Reg_Input) { |
| 3191 |
if (match (next, NULL)) { |
| 3192 |
MATCH_RETURN (1); |
| 3193 |
} |
| 3194 |
|
| 3195 |
CHECK_RECURSION_LIMIT |
| 3196 |
} |
| 3197 |
|
| 3198 |
/* Couldn't or didn't match. */ |
| 3199 |
|
| 3200 |
if (lazy) { |
| 3201 |
if (!greedy (next_op, 1)) { |
| 3202 |
MATCH_RETURN (0); |
| 3203 |
} |
| 3204 |
|
| 3205 |
num_matched ++; /* Inch forward. */ |
| 3206 |
} else if (num_matched > REG_ZERO) { |
| 3207 |
num_matched --; /* Back up. */ |
| 3208 |
} else if (min == REG_ZERO && num_matched == REG_ZERO) { |
| 3209 |
break; |
| 3210 |
} |
| 3211 |
|
| 3212 |
Reg_Input = save + num_matched; |
| 3213 |
} |
| 3214 |
|
| 3215 |
MATCH_RETURN (0); |
| 3216 |
} |
| 3217 |
|
| 3218 |
break; |
| 3219 |
|
| 3220 |
case END: |
| 3221 |
if (Extent_Ptr_FW == NULL || (Reg_Input - Extent_Ptr_FW) > 0) { |
| 3222 |
Extent_Ptr_FW = Reg_Input; |
| 3223 |
} |
| 3224 |
|
| 3225 |
MATCH_RETURN (1) // Success! |
| 3226 |
|
| 3227 |
break; |
| 3228 |
|
| 3229 |
case INIT_COUNT: |
| 3230 |
Brace->count [*OPERAND (scan)] = REG_ZERO; |
| 3231 |
|
| 3232 |
break; |
| 3233 |
|
| 3234 |
case INC_COUNT: |
| 3235 |
Brace->count [*OPERAND (scan)] ++; |
| 3236 |
|
| 3237 |
break; |
| 3238 |
|
| 3239 |
case TEST_COUNT: |
| 3240 |
if (Brace->count [*OPERAND (scan)] < |
| 3241 |
(unsigned long) GET_OFFSET (scan + NEXT_PTR_SIZE + INDEX_SIZE)) { |
| 3242 |
|
| 3243 |
next = scan + NODE_SIZE + INDEX_SIZE + NEXT_PTR_SIZE; |
| 3244 |
} |
| 3245 |
|
| 3246 |
break; |
| 3247 |
|
| 3248 |
case BACK_REF: |
| 3249 |
case BACK_REF_CI: |
| 3250 |
/* case X_REGEX_BR: */ |
| 3251 |
/* case X_REGEX_BR_CI: *** IMPLEMENT LATER */ |
| 3252 |
{ |
| 3253 |
char32 *captured, *finish; |
| 3254 |
int paren_no; |
| 3255 |
|
| 3256 |
paren_no = (int) * OPERAND (scan); |
| 3257 |
|
| 3258 |
/* if (GET_OP_CODE (scan) == X_REGEX_BR || |
| 3259 |
GET_OP_CODE (scan) == X_REGEX_BR_CI) { |
| 3260 |
|
| 3261 |
if (Cross_Regex_Backref == NULL) MATCH_RETURN (0); |
| 3262 |
|
| 3263 |
captured = |
| 3264 |
(regularExp_CHAR *) Cross_Regex_Backref->startp [paren_no]; |
| 3265 |
|
| 3266 |
finish = |
| 3267 |
(regularExp_CHAR *) Cross_Regex_Backref->endp [paren_no]; |
| 3268 |
} else { */ |
| 3269 |
captured = Back_Ref_Start [paren_no]; |
| 3270 |
finish = Back_Ref_End [paren_no]; |
| 3271 |
/* } */ |
| 3272 |
|
| 3273 |
if (captured && finish) { |
| 3274 |
if (captured > finish) { |
| 3275 |
MATCH_RETURN (0); |
| 3276 |
} |
| 3277 |
|
| 3278 |
if (GET_OP_CODE (scan) == BACK_REF_CI /* || |
| 3279 |
GET_OP_CODE (scan) == X_REGEX_BR_CI*/) { |
| 3280 |
|
| 3281 |
while (captured < finish) { |
| 3282 |
if (AT_END_OF_STRING (Reg_Input) || Melder_toLowerCase (*captured++) != Melder_toLowerCase (*Reg_Input++)) // TODO: should casefold |
| 3283 |
MATCH_RETURN (0) |
| 3284 |
} |
| 3285 |
} else { |
| 3286 |
while (captured < finish) { |
| 3287 |
if (AT_END_OF_STRING (Reg_Input) || *captured++ != *Reg_Input++) |
| 3288 |
MATCH_RETURN (0) |
| 3289 |
} |
| 3290 |
} |
| 3291 |
break; |
| 3292 |
} else { |
| 3293 |
MATCH_RETURN (0) |
| 3294 |
} |
| 3295 |
} |
| 3296 |
|
| 3297 |
case POS_AHEAD_OPEN: |
| 3298 |
case NEG_AHEAD_OPEN: |
| 3299 |
{ |
| 3300 |
char32 *save = Reg_Input; |
| 3301 |
|
| 3302 |
/* Temporarily ignore the logical end of the string, to allow |
| 3303 |
lookahead past the end. */ |
| 3304 |
const char32 *saved_end = End_Of_String; |
| 3305 |
End_Of_String = NULL; |
| 3306 |
|
| 3307 |
int answer = match (next, NULL); /* Does the look-ahead regex match? */ |
| 3308 |
|
| 3309 |
CHECK_RECURSION_LIMIT |
| 3310 |
|
| 3311 |
if ( (GET_OP_CODE (scan) == POS_AHEAD_OPEN) ? answer : ! answer) { |
| 3312 |
/* Remember the last (most to the right) character position |
| 3313 |
that we consume in the input for a successful match. This |
| 3314 |
is info that may be needed should an attempt be made to |
| 3315 |
match the exact same text at the exact same place. Since |
| 3316 |
look-aheads backtrack, a regex with a trailing look-ahead |
| 3317 |
may need more text than it matches to accomplish a |
| 3318 |
re-match. */ |
| 3319 |
|
| 3320 |
if (Extent_Ptr_FW == NULL || (Reg_Input - Extent_Ptr_FW) > 0) { |
| 3321 |
Extent_Ptr_FW = Reg_Input; |
| 3322 |
} |
| 3323 |
|
| 3324 |
Reg_Input = save; /* Backtrack to look-ahead start. */ |
| 3325 |
End_Of_String = saved_end; /* Restore logical end. */ |
| 3326 |
|
| 3327 |
/* Jump to the node just after the (?=...) or (?!...) |
| 3328 |
Construct. */ |
| 3329 |
|
| 3330 |
next = next_ptr (OPERAND (scan)); /* Skip 1st branch */ |
| 3331 |
/* Skip the chain of branches inside the look-ahead */ |
| 3332 |
while (GET_OP_CODE (next) == BRANCH) { |
| 3333 |
next = next_ptr (next); |
| 3334 |
} |
| 3335 |
next = next_ptr (next); /* Skip the LOOK_AHEAD_CLOSE */ |
| 3336 |
} else { |
| 3337 |
Reg_Input = save; /* Backtrack to look-ahead start. */ |
| 3338 |
End_Of_String = saved_end; /* Restore logical end. */ |
| 3339 |
|
| 3340 |
MATCH_RETURN (0) |
| 3341 |
} |
| 3342 |
} |
| 3343 |
|
| 3344 |
break; |
| 3345 |
|
| 3346 |
case POS_BEHIND_OPEN: |
| 3347 |
case NEG_BEHIND_OPEN: { |
| 3348 |
char32 *save; |
| 3349 |
int answer; |
| 3350 |
int offset, upper; |
| 3351 |
int lower; |
| 3352 |
int found = 0; |
| 3353 |
const char32 *saved_end; |
| 3354 |
|
| 3355 |
save = Reg_Input; |
| 3356 |
saved_end = End_Of_String; |
| 3357 |
|
| 3358 |
/* Prevent overshoot (greedy matching could end past the |
| 3359 |
current position) by tightening the matching boundary. |
| 3360 |
Lookahead inside lookbehind can still cross that boundary. */ |
| 3361 |
End_Of_String = Reg_Input; |
| 3362 |
|
| 3363 |
lower = GET_LOWER (scan); |
| 3364 |
upper = GET_UPPER (scan); |
| 3365 |
|
| 3366 |
/* Start with the shortest match first. This is the most |
| 3367 |
efficient direction in general. |
| 3368 |
Note! Negative look behind is _very_ tricky when the length |
| 3369 |
is not constant: we have to make sure the expression doesn't |
| 3370 |
match for _any_ of the starting positions. */ |
| 3371 |
for (offset = lower; offset <= upper; ++offset) { |
| 3372 |
Reg_Input = save - offset; |
| 3373 |
|
| 3374 |
if (Reg_Input < Look_Behind_To) { |
| 3375 |
/* No need to look any further */ |
| 3376 |
break; |
| 3377 |
} |
| 3378 |
|
| 3379 |
answer = match (next, NULL); /* Does the look-behind regex match? */ |
| 3380 |
|
| 3381 |
CHECK_RECURSION_LIMIT |
| 3382 |
|
| 3383 |
/* The match must have ended at the current position; |
| 3384 |
otherwise it is invalid */ |
| 3385 |
if (answer && Reg_Input == save) { |
| 3386 |
/* It matched, exactly far enough */ |
| 3387 |
found = 1; |
| 3388 |
|
| 3389 |
/* Remember the last (most to the left) character position |
| 3390 |
that we consume in the input for a successful match. |
| 3391 |
This is info that may be needed should an attempt be |
| 3392 |
made to match the exact same text at the exact same |
| 3393 |
place. Since look-behind backtracks, a regex with a |
| 3394 |
leading look-behind may need more text than it matches |
| 3395 |
to accomplish a re-match. */ |
| 3396 |
|
| 3397 |
if (Extent_Ptr_BW == NULL || |
| 3398 |
(Extent_Ptr_BW - (save - offset)) > 0) { |
| 3399 |
Extent_Ptr_BW = save - offset; |
| 3400 |
} |
| 3401 |
|
| 3402 |
break; |
| 3403 |
} |
| 3404 |
} |
| 3405 |
|
| 3406 |
/* Always restore the position and the logical string end. */ |
| 3407 |
Reg_Input = save; |
| 3408 |
End_Of_String = saved_end; |
| 3409 |
|
| 3410 |
if ( (GET_OP_CODE (scan) == POS_BEHIND_OPEN) ? found : !found) { |
| 3411 |
/* The look-behind matches, so we must jump to the next |
| 3412 |
node. The look-behind node is followed by a chain of |
| 3413 |
branches (contents of the look-behind expression), and |
| 3414 |
terminated by a look-behind-close node. */ |
| 3415 |
next = next_ptr (OPERAND (scan) + LENGTH_SIZE); /* 1st branch */ |
| 3416 |
/* Skip the chained branches inside the look-ahead */ |
| 3417 |
while (GET_OP_CODE (next) == BRANCH) { |
| 3418 |
next = next_ptr (next); |
| 3419 |
} |
| 3420 |
next = next_ptr (next); /* Skip LOOK_BEHIND_CLOSE */ |
| 3421 |
} else { |
| 3422 |
/* Not a match */ |
| 3423 |
MATCH_RETURN (0) |
| 3424 |
} |
| 3425 |
} |
| 3426 |
break; |
| 3427 |
|
| 3428 |
case LOOK_AHEAD_CLOSE: |
| 3429 |
case LOOK_BEHIND_CLOSE: |
| 3430 |
MATCH_RETURN (1); /* We have reached the end of the look-ahead or |
| 3431 |
look-behind which implies that we matched it, |
| 3432 |
so return `true`. */ |
| 3433 |
default: |
| 3434 |
if ( (GET_OP_CODE (scan) > OPEN) && |
| 3435 |
(GET_OP_CODE (scan) < OPEN + NSUBEXP)) { |
| 3436 |
|
| 3437 |
int no; |
| 3438 |
char32 *save; |
| 3439 |
|
| 3440 |
no = GET_OP_CODE (scan) - OPEN; |
| 3441 |
save = Reg_Input; |
| 3442 |
|
| 3443 |
if (no < 10) { |
| 3444 |
Back_Ref_Start [no] = save; |
| 3445 |
Back_Ref_End [no] = NULL; |
| 3446 |
} |
| 3447 |
|
| 3448 |
if (match (next, NULL)) { |
| 3449 |
/* Do not set `Start_Ptr_Ptr' if some later invocation (think |
| 3450 |
recursion) of the same parentheses already has. */ |
| 3451 |
|
| 3452 |
if (Start_Ptr_Ptr [no] == NULL) { |
| 3453 |
Start_Ptr_Ptr [no] = save; |
| 3454 |
} |
| 3455 |
|
| 3456 |
MATCH_RETURN (1) |
| 3457 |
} else { |
| 3458 |
MATCH_RETURN (0) |
| 3459 |
} |
| 3460 |
} else if ( (GET_OP_CODE (scan) > CLOSE) && |
| 3461 |
(GET_OP_CODE (scan) < CLOSE + NSUBEXP)) { |
| 3462 |
|
| 3463 |
int no; |
| 3464 |
char32 *save; |
| 3465 |
|
| 3466 |
no = GET_OP_CODE (scan) - CLOSE; |
| 3467 |
save = Reg_Input; |
| 3468 |
|
| 3469 |
if (no < 10) { |
| 3470 |
Back_Ref_End [no] = save; |
| 3471 |
} |
| 3472 |
|
| 3473 |
if (match (next, NULL)) { |
| 3474 |
/* Do not set `End_Ptr_Ptr' if some later invocation of the |
| 3475 |
same parentheses already has. */ |
| 3476 |
|
| 3477 |
if (End_Ptr_Ptr [no] == NULL) { |
| 3478 |
End_Ptr_Ptr [no] = save; |
| 3479 |
} |
| 3480 |
MATCH_RETURN (1) |
| 3481 |
} else { |
| 3482 |
MATCH_RETURN (0) |
| 3483 |
} |
| 3484 |
} else { |
| 3485 |
reg_error (U"memory corruption, `match\'"); |
| 3486 |
MATCH_RETURN (0) |
| 3487 |
} |
| 3488 |
|
| 3489 |
break; |
| 3490 |
} |
| 3491 |
|
| 3492 |
scan = next; |
| 3493 |
} |
| 3494 |
|
| 3495 |
/* We get here only if there's trouble -- normally "case END" is |
| 3496 |
the terminating point. */ |
| 3497 |
|
| 3498 |
reg_error (U"corrupted pointers, `match\'"); |
| 3499 |
MATCH_RETURN (0) |
| 3500 |
} |
| 3501 |
|
| 3502 |
/*----------------------------------------------------------------------* |
| 3503 |
* greedy |
| 3504 |
* |
| 3505 |
* Repeatedly match something simple up to "max" times. If max <= 0 |
| 3506 |
* then match as much as possible (max = infinity). Uses unsigned long |
| 3507 |
* variables to maximize the amount of text matchable for unbounded |
| 3508 |
* qualifiers like '*' and '+'. This will allow at least 4,294,967,295 |
| 3509 |
* matches (4 Gig!) for an ANSI C compliant compiler. If you are |
| 3510 |
* applying a regex to something bigger than that, you shouldn't be |
| 3511 |
* using NEdit! |
| 3512 |
* |
| 3513 |
* Returns the actual number of matches. |
| 3514 |
*----------------------------------------------------------------------*/ |
| 3515 |
|
| 3516 |
static unsigned long greedy (char32 *p, long max) { |
| 3517 |
|
| 3518 |
char32 *input_str; |
| 3519 |
char32 *operand; |
| 3520 |
unsigned long count = REG_ZERO; |
| 3521 |
unsigned long max_cmp; |
| 3522 |
|
| 3523 |
input_str = Reg_Input; |
| 3524 |
operand = OPERAND (p); /* Literal char or start of class characters. */ |
| 3525 |
max_cmp = (max > 0) ? (unsigned long) max : ULONG_MAX; |
| 3526 |
|
| 3527 |
switch (GET_OP_CODE (p)) |
| 3528 |
{ |
| 3529 |
case ANY: /* Race to the end of the line or string. Dot DOESN'T match newline. */ |
| 3530 |
while (count < max_cmp && *input_str != '\n' && ! AT_END_OF_STRING (input_str)) { |
| 3531 |
count ++; |
| 3532 |
input_str ++; |
| 3533 |
} |
| 3534 |
break; |
| 3535 |
|
| 3536 |
case EVERY: /* Race to the end of the line or string. Dot DOES match newline. */ |
| 3537 |
while (count < max_cmp && ! AT_END_OF_STRING (input_str)) { |
| 3538 |
count ++; |
| 3539 |
input_str ++; |
| 3540 |
} |
| 3541 |
break; |
| 3542 |
|
| 3543 |
case EXACTLY: /* Count occurrences of single character operand. */ |
| 3544 |
while (count < max_cmp && *operand == *input_str && ! AT_END_OF_STRING (input_str)) { |
| 3545 |
count ++; |
| 3546 |
input_str ++; |
| 3547 |
} |
| 3548 |
break; |
| 3549 |
|
| 3550 |
case SIMILAR: /* Case-insensitive version of EXACTLY */ |
| 3551 |
while (count < max_cmp && *operand == Melder_toLowerCase (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3552 |
count ++; |
| 3553 |
input_str ++; |
| 3554 |
} |
| 3555 |
break; |
| 3556 |
|
| 3557 |
case ANY_OF: /* [...] character class. */ |
| 3558 |
while (count < max_cmp && str32chr (operand, *input_str) != NULL && ! AT_END_OF_STRING (input_str)) { |
| 3559 |
count ++; |
| 3560 |
input_str ++; |
| 3561 |
} |
| 3562 |
break; |
| 3563 |
|
| 3564 |
case ANY_BUT: /* [^...] Negated character class- does NOT normally |
| 3565 |
match newline (\n added usually to operand at compile |
| 3566 |
time.) */ |
| 3567 |
|
| 3568 |
while (count < max_cmp && str32chr (operand, *input_str) == NULL && ! AT_END_OF_STRING (input_str)) { |
| 3569 |
count ++; |
| 3570 |
input_str ++; |
| 3571 |
} |
| 3572 |
break; |
| 3573 |
|
| 3574 |
case IS_DELIM: /* \y (not a word delimiter char) |
| 3575 |
NOTE: '\n' and '\0' are always word delimiters. */ |
| 3576 |
|
| 3577 |
while (count < max_cmp && ! Melder_isWordCharacter (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3578 |
count ++; |
| 3579 |
input_str ++; |
| 3580 |
} |
| 3581 |
break; |
| 3582 |
|
| 3583 |
case NOT_DELIM: /* \Y (not a word delimiter char) |
| 3584 |
NOTE: '\n' and '\0' are always word delimiters. */ |
| 3585 |
|
| 3586 |
while (count < max_cmp && Melder_isWordCharacter (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3587 |
count ++; |
| 3588 |
input_str ++; |
| 3589 |
} |
| 3590 |
break; |
| 3591 |
|
| 3592 |
case WORD_CHAR: /* \w (a word character: letter, number, mark or connector punctuation) */ |
| 3593 |
while (count < max_cmp && Melder_isWordCharacter (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3594 |
count ++; |
| 3595 |
input_str ++; |
| 3596 |
} |
| 3597 |
break; |
| 3598 |
|
| 3599 |
case NOT_WORD_CHAR: /* \W (NOT a word character) */ |
| 3600 |
while (count < max_cmp && ! Melder_isWordCharacter (*input_str) && *input_str != U'\n' && ! AT_END_OF_STRING (input_str)) { |
| 3601 |
count ++; |
| 3602 |
input_str ++; |
| 3603 |
} |
| 3604 |
break; |
| 3605 |
|
| 3606 |
case DIGIT: /* for ASCII, use [0-9] */ |
| 3607 |
while (count < max_cmp && Melder_isDecimalNumber (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3608 |
count ++; |
| 3609 |
input_str ++; |
| 3610 |
} |
| 3611 |
break; |
| 3612 |
|
| 3613 |
case NOT_DIGIT: /* for ASCII, use [^0-9] */ |
| 3614 |
while (count < max_cmp && ! Melder_isDecimalNumber (*input_str) && *input_str != U'\n' && ! AT_END_OF_STRING (input_str)) { |
| 3615 |
count ++; |
| 3616 |
input_str ++; |
| 3617 |
} |
| 3618 |
break; |
| 3619 |
|
| 3620 |
case SPACE: /* for ASCII, use [ \t\r\f\v]-- doesn't match newline. */ |
| 3621 |
while (count < max_cmp && Melder_isHorizontalSpace (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3622 |
count ++; |
| 3623 |
input_str ++; |
| 3624 |
} |
| 3625 |
break; |
| 3626 |
|
| 3627 |
case SPACE_NL: /* for ASCII, use [\n \t\r\f\v]-- matches newline. */ |
| 3628 |
while (count < max_cmp && Melder_isHorizontalOrVerticalSpace (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3629 |
count ++; |
| 3630 |
input_str ++; |
| 3631 |
} |
| 3632 |
break; |
| 3633 |
|
| 3634 |
case NOT_SPACE: /* for ASCII, use [^\n \t\r\f\v]-- doesn't match newline. */ |
| 3635 |
while (count < max_cmp && ! Melder_isHorizontalOrVerticalSpace (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3636 |
count ++; |
| 3637 |
input_str ++; |
| 3638 |
} |
| 3639 |
break; |
| 3640 |
|
| 3641 |
case NOT_SPACE_NL: /* for ASCII, use [^ \t\r\f\v]-- matches newline. */ |
| 3642 |
while (count < max_cmp && ! Melder_isHorizontalSpace (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3643 |
count ++; |
| 3644 |
input_str ++; |
| 3645 |
} |
| 3646 |
break; |
| 3647 |
|
| 3648 |
case LETTER: /* for ASCII, use [a-zA-Z] */ |
| 3649 |
while (count < max_cmp && Melder_isLetter (*input_str) && ! AT_END_OF_STRING (input_str)) { |
| 3650 |
count ++; |
| 3651 |
input_str ++; |
| 3652 |
} |
| 3653 |
break; |
| 3654 |
|
| 3655 |
case NOT_LETTER: /* for ASCII, use [^a-zA-Z] */ |
| 3656 |
while (count < max_cmp && ! Melder_isLetter (*input_str) && *input_str != '\n' && ! AT_END_OF_STRING (input_str)) { |
| 3657 |
count ++; |
| 3658 |
input_str ++; |
| 3659 |
} |
| 3660 |
break; |
| 3661 |
|
| 3662 |
default: |
| 3663 |
/* Called inappropriately. Only atoms that are SIMPLE should |
| 3664 |
generate a call to greedy. The above cases should cover |
| 3665 |
all the atoms that are SIMPLE. */ |
| 3666 |
|
| 3667 |
reg_error (U"internal error #10 `greedy\'"); |
| 3668 |
count = 0U; /* Best we can do. */ |
| 3669 |
} |
| 3670 |
|
| 3671 |
/* Point to character just after last matched character. */ |
| 3672 |
|
| 3673 |
Reg_Input = input_str; |
| 3674 |
|
| 3675 |
return (count); |
| 3676 |
} |
| 3677 |
|
| 3678 |
/*----------------------------------------------------------------------* |
| 3679 |
* next_ptr - compute the address of a node's "NEXT" pointer. |
| 3680 |
* Note: a simplified inline version is available via the NEXT_PTR() macro, |
| 3681 |
* but that one is only to be used at time-critical places (see the |
| 3682 |
* description of the macro). |
| 3683 |
*----------------------------------------------------------------------*/ |
| 3684 |
|
| 3685 |
static char32 *next_ptr (char32 *ptr) { |
| 3686 |
if (ptr == & Compute_Size) |
| 3687 |
return nullptr; |
| 3688 |
int offset = GET_OFFSET (ptr); |
| 3689 |
if (offset == 0) |
| 3690 |
return nullptr; |
| 3691 |
if (GET_OP_CODE (ptr) == BACK) { |
| 3692 |
return ptr - offset; |
| 3693 |
} else { |
| 3694 |
return ptr + offset; |
| 3695 |
} |
| 3696 |
} |
| 3697 |
|
| 3698 |
/* |
| 3699 |
** SubstituteRE - Perform substitutions after a `regexp' match. |
| 3700 |
** |
| 3701 |
** This function cleanly shortens results of more than max length to max. |
| 3702 |
** To give the caller a chance to react to this the function returns False |
| 3703 |
** on any error. The substitution will still be executed. |
| 3704 |
*/ |
| 3705 |
int SubstituteRE (const regexp *prog, conststring32 source, mutablestring32 dest, int max, int *errorType) { |
| 3706 |
|
| 3707 |
const char32 *src; |
| 3708 |
const char32 *src_alias; |
| 3709 |
char32 *dst; |
| 3710 |
char32 c; |
| 3711 |
char32 test; |
| 3712 |
int paren_no; |
| 3713 |
int len; |
| 3714 |
char32 chgcase; |
| 3715 |
bool anyWarnings = false; |
| 3716 |
|
| 3717 |
*errorType = 0; |
| 3718 |
if (! prog || ! source || ! dest) { |
| 3719 |
reg_error (U"NULL parm to `SubstituteRE\'"); |
| 3720 |
*errorType = 2; |
| 3721 |
return false; |
| 3722 |
} |
| 3723 |
|
| 3724 |
if (U_CHAR_AT (prog->program) != MAGIC) { |
| 3725 |
*errorType = 3; |
| 3726 |
reg_error (U"damaged regexp passed to `SubstituteRE\'"); |
| 3727 |
return false; |
| 3728 |
} |
| 3729 |
|
| 3730 |
src = source; |
| 3731 |
dst = dest; |
| 3732 |
|
| 3733 |
while ( (c = *src++) != U'\0') { |
| 3734 |
chgcase = U'\0'; |
| 3735 |
paren_no = -1; |
| 3736 |
|
| 3737 |
if (c == U'\\') { |
| 3738 |
/* Process any case-altering tokens, i.e \u, \U, \l, \L. */ |
| 3739 |
|
| 3740 |
if (*src == U'u' || *src == U'U' || *src == U'l' || *src == U'L') { |
| 3741 |
chgcase = *src; |
| 3742 |
src ++; |
| 3743 |
c = *src ++; |
| 3744 |
if (c == U'\0') |
| 3745 |
break; |
| 3746 |
} |
| 3747 |
} |
| 3748 |
|
| 3749 |
if (c == U'&') { |
| 3750 |
paren_no = 0; |
| 3751 |
} else if (c == U'\\') { |
| 3752 |
/* Can not pass register variable `&src' to function `numeric_escape' |
| 3753 |
so make a non-register copy that we can take the address of. */ |
| 3754 |
|
| 3755 |
src_alias = src; |
| 3756 |
|
| 3757 |
if (U'1' <= *src && *src <= U'9') { |
| 3758 |
paren_no = (int) * src++ - (int) '0'; |
| 3759 |
|
| 3760 |
} else if ( (test = literal_escape (*src)) != U'\0') { |
| 3761 |
c = test; src++; |
| 3762 |
|
| 3763 |
} else if ( (test = numeric_escape (*src, (char32 **) &src_alias)) != U'\0') { |
| 3764 |
c = test; |
| 3765 |
src = src_alias; |
| 3766 |
src ++; |
| 3767 |
|
| 3768 |
/* NOTE: if an octal escape for zero is attempted (e.g. \000), it |
| 3769 |
will be treated as a literal string. */ |
| 3770 |
} else if (*src == U'\0') { |
| 3771 |
/* If '\' is the last character of the replacement string, it is |
| 3772 |
interpreted as a literal backslash. */ |
| 3773 |
|
| 3774 |
c = U'\\'; |
| 3775 |
} else { |
| 3776 |
c = *src ++; /* Allow any escape sequence (This is */ |
| 3777 |
} /* INCONSISTENT with the `CompileRE' */ |
| 3778 |
} /* mind set of issuing an error! */ |
| 3779 |
|
| 3780 |
if (paren_no < 0) { /* Ordinary character. */ |
| 3781 |
if ( (dst - dest) >= (max - 1)) { |
| 3782 |
*errorType = 1; |
| 3783 |
reg_error (U"replacing expression in `SubstituteRE\' too long; truncating"); |
| 3784 |
anyWarnings = true; |
| 3785 |
break; |
| 3786 |
} else { |
| 3787 |
*dst++ = c; |
| 3788 |
} |
| 3789 |
} else if (prog->startp [paren_no] != NULL && |
| 3790 |
prog->endp [paren_no] != NULL) { |
| 3791 |
|
| 3792 |
len = prog->endp [paren_no] - prog->startp [paren_no]; |
| 3793 |
|
| 3794 |
if ( (dst + len - dest) >= max - 1) { |
| 3795 |
*errorType = 1; |
| 3796 |
reg_error (U"replacing expression in `SubstituteRE\' too long; truncating"); |
| 3797 |
anyWarnings = true; |
| 3798 |
len = max - (dst - dest) - 1; |
| 3799 |
} |
| 3800 |
|
| 3801 |
(void) str32ncpy (dst, prog->startp [paren_no], len); |
| 3802 |
|
| 3803 |
if (chgcase != U'\0') { |
| 3804 |
adjustcase (dst, len, chgcase); |
| 3805 |
} |
| 3806 |
|
| 3807 |
dst += len; |
| 3808 |
|
| 3809 |
if (len != 0 && * (dst - 1) == U'\0') { /* strncpy hit NUL. */ |
| 3810 |
*errorType = 3; |
| 3811 |
reg_error (U"damaged match string in `SubstituteRE\'"); |
| 3812 |
anyWarnings = true; |
| 3813 |
} |
| 3814 |
} |
| 3815 |
} |
| 3816 |
|
| 3817 |
*dst = U'\0'; |
| 3818 |
|
| 3819 |
return ! anyWarnings; |
| 3820 |
} |
| 3821 |
|
| 3822 |
static void adjustcase (char32 *str, int len, char32 chgcase) { |
| 3823 |
|
| 3824 |
char32 *string = str; |
| 3825 |
|
| 3826 |
/* The tokens \u and \l only modify the first character while the tokens |
| 3827 |
\U and \L modify the entire string. */ |
| 3828 |
|
| 3829 |
if (iswlower_portable ((int) chgcase) && len > 0) |
| 3830 |
len = 1; |
| 3831 |
switch (chgcase) { |
| 3832 |
case U'u': |
| 3833 |
case U'U': |
| 3834 |
for (integer i = 0; i < len; i ++) |
| 3835 |
* (string + i) = Melder_toUpperCase (* (string + i)); |
| 3836 |
break; |
| 3837 |
case U'l': |
| 3838 |
case U'L': |
| 3839 |
for (integer i = 0; i < len; i ++) |
| 3840 |
* (string + i) = Melder_toLowerCase (* (string + i)); |
| 3841 |
break; |
| 3842 |
} |
| 3843 |
} |
| 3844 |
|
| 3845 |
/*----------------------------------------------------------------------* |
| 3846 |
* reg_error |
| 3847 |
*----------------------------------------------------------------------*/ |
| 3848 |
|
| 3849 |
static void reg_error (conststring32 str) { |
| 3850 |
Melder_appendError (U"Internal error processing regular expression: ", str); |
| 3851 |
} |
| 3852 |
|
| 3853 |
void EnableCountingQuantifier (int is_enabled) { |
| 3854 |
Enable_Counting_Quantifier = is_enabled; |
| 3855 |
} |
| 3856 |
|
| 3857 |
/* End of file regularExp.cpp */ |