| 1 |
unit CastleRegexpr; |
| 2 |
|
| 3 |
{ |
| 4 |
TRegExpr class library |
| 5 |
Delphi Regular Expressions |
| 6 |
|
| 7 |
Copyright (c) 1999-2004 Andrey V. Sorokin, St.Petersburg, Russia |
| 8 |
|
| 9 |
You can choose to use this Pascal unit in one of the two following licenses: |
| 10 |
|
| 11 |
Option 1> |
| 12 |
|
| 13 |
You may use this software in any kind of development, |
| 14 |
including comercial, redistribute, and modify it freely, |
| 15 |
under the following restrictions : |
| 16 |
1. This software is provided as it is, without any kind of |
| 17 |
warranty given. Use it at Your own risk.The author is not |
| 18 |
responsible for any consequences of use of this software. |
| 19 |
2. The origin of this software may not be mispresented, You |
| 20 |
must not claim that You wrote the original software. If |
| 21 |
You use this software in any kind of product, it would be |
| 22 |
appreciated that there in a information box, or in the |
| 23 |
documentation would be an acknowledgement like |
| 24 |
|
| 25 |
Partial Copyright (c) 2004 Andrey V. Sorokin |
| 26 |
https://sorokin.engineer/ |
| 27 |
[email protected] |
| 28 |
|
| 29 |
3. You may not have any income from distributing this source |
| 30 |
(or altered version of it) to other developers. When You |
| 31 |
use this product in a comercial package, the source may |
| 32 |
not be charged seperatly. |
| 33 |
4. Altered versions must be plainly marked as such, and must |
| 34 |
not be misrepresented as being the original software. |
| 35 |
5. RegExp Studio application and all the visual components as |
| 36 |
well as documentation is not part of the TRegExpr library |
| 37 |
and is not free for usage. |
| 38 |
|
| 39 |
https://sorokin.engineer/ |
| 40 |
[email protected] |
| 41 |
|
| 42 |
Option 2> |
| 43 |
|
| 44 |
The same modified LGPL with static linking exception as the Free Pascal RTL |
| 45 |
} |
| 46 |
|
| 47 |
{ |
| 48 |
program is essentially a linear encoding |
| 49 |
of a nondeterministic finite-state machine (aka syntax charts or |
| 50 |
"railroad normal form" in parsing technology). Each node is an opcode |
| 51 |
plus a "next" pointer, possibly plus an operand. "Next" pointers of |
| 52 |
all nodes except BRANCH implement concatenation; a "next" pointer with |
| 53 |
a BRANCH on both ends of it connects two alternatives. (Here we |
| 54 |
have one of the subtle syntax dependencies: an individual BRANCH (as |
| 55 |
opposed to a collection of them) is never concatenated with anything |
| 56 |
because of operator precedence.) The operand of some types of node is |
| 57 |
a literal string; for others, it is a node leading into a sub-FSM. In |
| 58 |
particular, the operand of a BRANCH node is the first node of the branch. |
| 59 |
(NB this is *not* a tree structure: the tail of the branch connects |
| 60 |
to the thing following the set of BRANCHes.) |
| 61 |
} |
| 62 |
|
| 63 |
interface |
| 64 |
|
| 65 |
{ off $DEFINE DebugSynRegExpr } |
| 66 |
// ======== Determine compiler |
| 67 |
{$I regexpr_compilers.inc} |
| 68 |
// ======== Define base compiler options |
| 69 |
{$BOOLEVAL OFF} |
| 70 |
{$EXTENDEDSYNTAX ON} |
| 71 |
{$LONGSTRINGS ON} |
| 72 |
{$IFDEF FPC} |
| 73 |
{$MODE DELPHI} // Delphi-compatible mode in FreePascal |
| 74 |
{$INLINE ON} |
| 75 |
{$ENDIF} |
| 76 |
// ======== Define options for TRegExpr engine |
| 77 |
{$DEFINE UnicodeRE} // Use WideChar for characters and UnicodeString/WideString for strings |
| 78 |
{ off $DEFINE UnicodeEx} // Support Unicode >0xFFFF, e.g. emoji, e.g. "." must find 2 WideChars of 1 emoji |
| 79 |
{ off $DEFINE UseWordChars} // Use WordChars property, otherwise fixed list 'a'..'z','A'..'Z','0'..'9','_' |
| 80 |
{ off $DEFINE UseSpaceChars} // Use SpaceChars property, otherwise fixed list |
| 81 |
{ off $DEFINE UseLineSep} // Use LineSeparators property, otherwise fixed line-break chars |
| 82 |
{$IFDEF UNICODE} |
| 83 |
{$IFNDEF UnicodeRE} |
| 84 |
{$MESSAGE ERROR 'You cannot undefine UnicodeRE for Unicode Delphi versions'} |
| 85 |
{$ENDIF} |
| 86 |
{$ENDIF} |
| 87 |
{$IFDEF FPC} |
| 88 |
{$DEFINE FastUnicodeData} // Use arrays for UpperCase/LowerCase/IsWordChar, they take 320K more memory |
| 89 |
{$ENDIF} |
| 90 |
{ off $DEFINE RegExpWithStackOverflowCheck} // Check the recursion depth and abort matching before stack overflows (available only for some OS/CPU) |
| 91 |
{$DEFINE UseFirstCharSet} // Enable optimization, which finds possible first chars of input string |
| 92 |
{$DEFINE RegExpPCodeDump} // Enable method Dump() to show opcode as string |
| 93 |
{$IFNDEF FPC} // Not supported in FreePascal |
| 94 |
{$DEFINE reRealExceptionAddr} // Exceptions will point to appropriate source line, not to Error procedure |
| 95 |
{$ENDIF} |
| 96 |
{$DEFINE ComplexBraces} // Support braces in complex cases |
| 97 |
{$IFNDEF UnicodeRE} |
| 98 |
{$UNDEF UnicodeEx} |
| 99 |
{$UNDEF FastUnicodeData} |
| 100 |
{$ENDIF} |
| 101 |
{.$DEFINE Compat} // Enable compatability methods/properties for forked version in Free Pascal 3.0 |
| 102 |
// ======== Define Pascal-language options |
| 103 |
// Asserts used to catch 'strange bugs' in TRegExpr implementation (when something goes |
| 104 |
// completely wrong). You can swith asserts on/off with help of {$C+}/{$C-} compiler options. |
| 105 |
{$IFDEF D3} { $DEFINE WITH_REGEX_ASSERT} {$ENDIF} |
| 106 |
{$IFDEF FPC}{$IFOPT C+} {$DEFINE WITH_REGEX_ASSERT} {$ENDIF}{$ENDIF} // Only if compile with -Sa |
| 107 |
// Define 'use subroutine parameters default values' option (do not edit this definition). |
| 108 |
{$IFDEF D4} {$DEFINE DefParam} {$ENDIF} |
| 109 |
{$IFDEF FPC} {$DEFINE DefParam} {$ENDIF} |
| 110 |
// Define 'OverMeth' options, to use method overloading (do not edit this definitions). |
| 111 |
{$IFDEF D5} {$DEFINE OverMeth} {$ENDIF} |
| 112 |
{$IFDEF FPC} {$DEFINE OverMeth} {$ENDIF} |
| 113 |
// Define 'InlineFuncs' options, to use inline keyword (do not edit this definitions). |
| 114 |
{$IFDEF D8} {$DEFINE InlineFuncs} {$ENDIF} |
| 115 |
{$IFDEF FPC} {$DEFINE InlineFuncs} {$ENDIF} |
| 116 |
{$PointerMath on} |
| 117 |
|
| 118 |
{$IFDEF RegExpWithStackOverflowCheck} // Define the stack checking algorithm for the current platform/CPU |
| 119 |
{$IF defined(Linux) or defined(Windows)}{$IF defined(CPU386) or defined(CPUX86_64)} |
| 120 |
{$DEFINE RegExpWithStackOverflowCheck_DecStack_Frame} // Stack-pointer decrements // use getframe over Sptr() |
| 121 |
{$ENDIF}{$ENDIF} |
| 122 |
{$ENDIF} |
| 123 |
uses |
| 124 |
SysUtils, // Exception |
| 125 |
{$IFDEF D2009} |
| 126 |
{$IFDEF D_XE2} |
| 127 |
System.Character, |
| 128 |
{$ELSE} |
| 129 |
Character, |
| 130 |
{$ENDIF} |
| 131 |
{$ENDIF} |
| 132 |
Classes; // TStrings in Split method |
| 133 |
|
| 134 |
type |
| 135 |
{$IFNDEF FPC} |
| 136 |
// Delphi doesn't have PtrInt but has NativeInt |
| 137 |
// but unfortunately NativeInt is declared wrongly in several versions |
| 138 |
{$IF SizeOf(Pointer)=4} |
| 139 |
PtrInt = Integer; |
| 140 |
PtrUInt = Cardinal; |
| 141 |
{$ELSE} |
| 142 |
PtrInt = Int64; |
| 143 |
PtrUInt = UInt64; |
| 144 |
{$IFEND} |
| 145 |
{$ENDIF} |
| 146 |
|
| 147 |
{$IFDEF UnicodeRE} |
| 148 |
PRegExprChar = PWideChar; |
| 149 |
{$IFDEF FPC} |
| 150 |
RegExprString = UnicodeString; |
| 151 |
{$ELSE} |
| 152 |
{$IFDEF D2009} |
| 153 |
RegExprString = UnicodeString; |
| 154 |
{$ELSE} |
| 155 |
RegExprString = WideString; |
| 156 |
{$ENDIF} |
| 157 |
{$ENDIF} |
| 158 |
REChar = WideChar; |
| 159 |
{$ELSE} |
| 160 |
PRegExprChar = PAnsiChar; |
| 161 |
RegExprString = AnsiString; |
| 162 |
REChar = AnsiChar; |
| 163 |
{$ENDIF} |
| 164 |
TREOp = REChar; // internal opcode type |
| 165 |
PREOp = ^TREOp; |
| 166 |
|
| 167 |
type |
| 168 |
TRegExprCharset = set of Byte; |
| 169 |
|
| 170 |
const |
| 171 |
// Escape char ('\' in common r.e.) used for escaping metachars (\w, \d etc) |
| 172 |
EscChar = '\'; |
| 173 |
|
| 174 |
// Substitute method: prefix of group reference: $1 .. $9 and $<name> |
| 175 |
SubstituteGroupChar = '$'; |
| 176 |
|
| 177 |
RegExprModifierI: Boolean = False; // default value for ModifierI |
| 178 |
RegExprModifierR: Boolean = True; // default value for ModifierR |
| 179 |
RegExprModifierS: Boolean = True; // default value for ModifierS |
| 180 |
RegExprModifierG: Boolean = True; // default value for ModifierG |
| 181 |
RegExprModifierM: Boolean = False; // default value for ModifierM |
| 182 |
RegExprModifierX: Boolean = False; // default value for ModifierX |
| 183 |
|
| 184 |
{$IFDEF UseSpaceChars} |
| 185 |
// default value for SpaceChars |
| 186 |
RegExprSpaceChars: RegExprString = ' '#$9#$A#$D#$C; |
| 187 |
{$ENDIF} |
| 188 |
|
| 189 |
{$IFDEF UseWordChars} |
| 190 |
// default value for WordChars |
| 191 |
RegExprWordChars: RegExprString = '0123456789' |
| 192 |
+ 'abcdefghijklmnopqrstuvwxyz' |
| 193 |
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_'; |
| 194 |
{$ENDIF} |
| 195 |
|
| 196 |
{$IFDEF UseLineSep} |
| 197 |
// default value for LineSeparators |
| 198 |
RegExprLineSeparators: RegExprString = #$d#$a#$b#$c |
| 199 |
{$IFDEF UnicodeRE} |
| 200 |
+ #$2028#$2029#$85 |
| 201 |
{$ENDIF}; |
| 202 |
{$ENDIF} |
| 203 |
|
| 204 |
// Tab and Unicode category "Space Separator": |
| 205 |
// https://www.compart.com/en/unicode/category/Zs |
| 206 |
RegExprHorzSeparators: RegExprString = #9#$20#$A0 |
| 207 |
{$IFDEF UnicodeRE} |
| 208 |
+ #$1680#$2000#$2001#$2002#$2003#$2004#$2005#$2006#$2007#$2008#$2009#$200A#$202F#$205F#$3000 |
| 209 |
{$ENDIF}; |
| 210 |
|
| 211 |
RegExprUsePairedBreak: Boolean = True; |
| 212 |
RegExprReplaceLineBreak: RegExprString = sLineBreak; |
| 213 |
|
| 214 |
const |
| 215 |
// Increment/keep-capacity for the size of arrays holding 'Group' related data |
| 216 |
// e.g., GrpBounds, GrpOpCodes and GrpNames |
| 217 |
RegexGroupCountIncrement = 50; |
| 218 |
|
| 219 |
// Max possible amount of groups. |
| 220 |
// Don't change it! It's defined by internal TRegExpr design. |
| 221 |
RegexMaxMaxGroups = MaxInt div 16; |
| 222 |
|
| 223 |
// Max depth of recursion for (?R) and (?1)..(?9) |
| 224 |
RegexMaxRecursion = 20; |
| 225 |
|
| 226 |
type |
| 227 |
TRegExprModifiers = record |
| 228 |
I: Boolean; |
| 229 |
// Case-insensitive. |
| 230 |
R: Boolean; |
| 231 |
// Extended syntax for Russian ranges in []. |
| 232 |
// If True, then а-я additionally includes letter 'ё', |
| 233 |
// А-Я additionally includes 'Ё', and а-Я includes all Russian letters. |
| 234 |
// Turn it off if it interferes with your national alphabet. |
| 235 |
S: Boolean; |
| 236 |
// Dot '.' matches any char, otherwise only [^\n]. |
| 237 |
G: Boolean; |
| 238 |
// Greedy. Switching it off switches all operators to non-greedy style, |
| 239 |
// so if G=False, then '*' works like '*?', '+' works like '+?' and so on. |
| 240 |
M: Boolean; |
| 241 |
// Treat string as multiple lines. It changes `^' and `$' from |
| 242 |
// matching at only the very start/end of the string to the start/end |
| 243 |
// of any line anywhere within the string. |
| 244 |
X: Boolean; |
| 245 |
// Allow comments in regex using # char. |
| 246 |
end; |
| 247 |
|
| 248 |
function IsModifiersEqual(const A, B: TRegExprModifiers): Boolean; |
| 249 |
|
| 250 |
type |
| 251 |
TRegExpr = class; |
| 252 |
TRegExprReplaceFunction = function(ARegExpr: TRegExpr): RegExprString of object; |
| 253 |
TRegExprCharChecker = function(ch: REChar): Boolean of object; |
| 254 |
TRegExprCharCheckerArray = array[0 .. 30] of TRegExprCharChecker; |
| 255 |
TRegExprCharCheckerInfo = record |
| 256 |
CharBegin, CharEnd: REChar; |
| 257 |
CheckerIndex: Integer; |
| 258 |
end; |
| 259 |
TRegExprCharCheckerInfos = array of TRegExprCharCheckerInfo; |
| 260 |
|
| 261 |
TRegExprAnchor = ( |
| 262 |
raNone, // Not anchored |
| 263 |
raBOL, // Must start at BOL |
| 264 |
raEOL, // Must start at EOL (maybe look behind) |
| 265 |
raContinue, // Must start at continue pos \G |
| 266 |
raOnlyOnce // Starts with .* must match from the start pos only. Must not be tried from a later pos |
| 267 |
); |
| 268 |
|
| 269 |
TRegExprFindFixedLengthFlag = ( |
| 270 |
flfReturnAtNextNil, |
| 271 |
flfSkipLookAround |
| 272 |
); |
| 273 |
TRegExprFindFixedLengthFlags = set of TRegExprFindFixedLengthFlag; |
| 274 |
|
| 275 |
{$IFDEF Compat} |
| 276 |
TRegExprInvertCaseFunction = function(const Ch: REChar): REChar of object; |
| 277 |
{$ENDIF} |
| 278 |
|
| 279 |
{$IFDEF ComplexBraces} |
| 280 |
POpLoopInfo = ^TOpLoopInfo; |
| 281 |
TOpLoopInfo = record |
| 282 |
Count: Integer; |
| 283 |
CurrentRegInput: PRegExprChar; |
| 284 |
BackTrackingAsAtom: Boolean; |
| 285 |
OuterLoop: POpLoopInfo; // for nested loops |
| 286 |
end; |
| 287 |
{$ENDIF} |
| 288 |
|
| 289 |
|
| 290 |
PPRegExprChar = ^PRegExprChar; |
| 291 |
TRegExprBoundsPtr = record |
| 292 |
TmpStart: PPRegExprChar; // pointer start of not yet finished group start in InputString |
| 293 |
// OP_CLOSE not yet reached |
| 294 |
// does not need to be cleared |
| 295 |
GrpStart: PPRegExprChar; // pointer to group start in InputString |
| 296 |
GrpEnd: PPRegExprChar; // pointer to group end in InputString |
| 297 |
end; |
| 298 |
TRegExprBounds = record |
| 299 |
TmpStart: array of PRegExprChar; // pointer start of not yet finished group start in InputString |
| 300 |
// OP_CLOSE not yet reached |
| 301 |
// does not need to be cleared |
| 302 |
GrpStart: array of PRegExprChar; // pointer to group start in InputString |
| 303 |
GrpEnd: array of PRegExprChar; // pointer to group end in InputString |
| 304 |
end; |
| 305 |
TRegExprBoundsArray = array[0 .. RegexMaxRecursion] of TRegExprBounds; |
| 306 |
|
| 307 |
PRegExprLookAroundInfo = ^TRegExprLookAroundInfo; |
| 308 |
TRegExprLookAroundInfo = record |
| 309 |
InputPos: PRegExprChar; // pointer to start of look-around in the input string |
| 310 |
savedInputCurrentEnd: PRegExprChar; // pointer to start of look-around in the input string |
| 311 |
IsNegative, HasMatchedToEnd: Boolean; |
| 312 |
IsBackTracking: Boolean; |
| 313 |
OuterInfo: PRegExprLookAroundInfo; // for nested lookaround |
| 314 |
end; |
| 315 |
|
| 316 |
TRegExprGroupName = record |
| 317 |
Name: RegExprString; |
| 318 |
Index: Integer; |
| 319 |
end; |
| 320 |
|
| 321 |
{ TRegExprGroupNameList } |
| 322 |
|
| 323 |
TRegExprGroupNameList = object |
| 324 |
Names: array of TRegExprGroupName; |
| 325 |
NameCount: Integer; |
| 326 |
// get index of group (subexpression) by name, to support named groups |
| 327 |
// like in Python: (?P<name>regex) |
| 328 |
function MatchIndexFromName(const AName: RegExprString): Integer; |
| 329 |
procedure Clear; |
| 330 |
procedure Add(const AName: RegExprString; AnIndex: Integer); |
| 331 |
end; |
| 332 |
|
| 333 |
{ TRegExpr } |
| 334 |
|
| 335 |
TRegExpr = class |
| 336 |
private |
| 337 |
FAllowBraceWithoutMin: Boolean; |
| 338 |
FAllowUnsafeLookBehind: Boolean; |
| 339 |
FAllowLiteralBraceWithoutRange: Boolean; |
| 340 |
FMatchesCleared: Boolean; |
| 341 |
fRaiseForRuntimeError: Boolean; |
| 342 |
GrpBounds: TRegExprBoundsArray; |
| 343 |
CurrentGrpBounds: TRegExprBoundsPtr; |
| 344 |
GrpNames: TRegExprGroupNameList; // names of groups, if non-empty |
| 345 |
GrpBacktrackingAsAtom: array of Boolean; // close of group[i] has set IsBacktrackingGroupAsAtom |
| 346 |
IsBacktrackingGroupAsAtom: Boolean; // Backtracking an entire atomic group that had matched. |
| 347 |
// Once the group matched it should not try any alternative matches within the group |
| 348 |
// If the pattern after the group fails, then the group fails (regardless of any alternative match in the group) |
| 349 |
|
| 350 |
GrpOpCodes: array of PRegExprChar; // pointer to opcode of group[i] (used by OP_SUBCALL*) |
| 351 |
GrpCount, ParsedGrpCount: Integer; |
| 352 |
|
| 353 |
{$IFDEF ComplexBraces} |
| 354 |
CurrentLoopInfoListPtr: POpLoopInfo; |
| 355 |
{$ENDIF} |
| 356 |
|
| 357 |
// The "internal use only" fields to pass info from compile |
| 358 |
// to execute that permits the execute phase to run lots faster on |
| 359 |
// simple cases. |
| 360 |
|
| 361 |
regAnchored: TRegExprAnchor; // is the match anchored (at beginning-of-line only)? |
| 362 |
// regAnchored permits very fast decisions on suitable starting points |
| 363 |
// for a match, cutting down the work a lot. regMust permits fast rejection |
| 364 |
// of lines that cannot possibly match. The regMust tests are costly enough |
| 365 |
// that regcomp() supplies a regMust only if the r.e. contains something |
| 366 |
// potentially expensive (at present, the only such thing detected is * or + |
| 367 |
// at the start of the r.e., which can involve a lot of backup). regMustLen is |
| 368 |
// supplied because the test in regexec() needs it and regcomp() is computing |
| 369 |
// it anyway. |
| 370 |
|
| 371 |
regMust: PRegExprChar; // string (pointer into program) that match must include, or nil |
| 372 |
regMustLen: Integer; // length of regMust string |
| 373 |
regMustString: RegExprString; // string which must occur in match (got from regMust/regMustLen) |
| 374 |
LookAroundInfoList: PRegExprLookAroundInfo; |
| 375 |
//regNestedCalls: integer; // some attempt to prevent 'catastrophic backtracking' but not used |
| 376 |
CurrentSubCalled: Integer; |
| 377 |
|
| 378 |
FMinMatchLen: integer; |
| 379 |
{$IFDEF UseFirstCharSet} |
| 380 |
FirstCharSet: TRegExprCharset; |
| 381 |
FirstCharArray: array[Byte] of Boolean; |
| 382 |
{$ENDIF} |
| 383 |
|
| 384 |
// work variables for Exec routines - save stack in recursion |
| 385 |
regInput: PRegExprChar; // pointer to currently handling char of input string |
| 386 |
fInputStart: PRegExprChar; // pointer to first char of input string |
| 387 |
fInputContinue: PRegExprChar; // pointer to char specified with Exec(AOffset), or start pos of ExecNext |
| 388 |
fInputEnd: PRegExprChar; // pointer after last char of input string |
| 389 |
fInputCurrentEnd: PRegExprChar; // pointer after last char of the current visible part of input string (can be limited by look-behind) |
| 390 |
fRegexStart: PRegExprChar; // pointer to first char of regex |
| 391 |
fRegexEnd: PRegExprChar; // pointer after last char of regex |
| 392 |
regRecursion: Integer; // current level of recursion (?R) (?1); always 0 if no recursion is used |
| 393 |
hasRecursion: Boolean; |
| 394 |
|
| 395 |
// work variables for compiler's routines |
| 396 |
regParse: PRegExprChar; // pointer to currently handling char of regex |
| 397 |
regNumBrackets: Integer; // count of () brackets |
| 398 |
regNumAtomicBrackets: Integer; // count of (?>) brackets |
| 399 |
regDummy: array [0..8 div SizeOf(REChar)] of REChar; // dummy pointer, used to detect 1st/2nd pass of Compile |
| 400 |
// if p=@regDummy, it is pass-1: opcode memory is not yet allocated |
| 401 |
programm: PRegExprChar; // pointer to opcode, =nil in pass-1 |
| 402 |
regCode: PRegExprChar; // pointer to last emitted opcode; changing in pass-2, but =@regDummy in pass-1 |
| 403 |
regCodeSize: Integer; // total opcode size in REChars |
| 404 |
regCodeWork: PRegExprChar; // pointer to opcode, to first code after MAGIC |
| 405 |
regExactlyLen: PLongInt; // pointer to length of substring of OP_EXACTLY* inside opcode |
| 406 |
fSecondPass: Boolean; // true inside pass-2 of Compile |
| 407 |
|
| 408 |
fExpression: RegExprString; // regex string |
| 409 |
fInputString: RegExprString; // input string |
| 410 |
fLastError: Integer; // Error call sets code of LastError |
| 411 |
fLastErrorOpcode: TREOp; |
| 412 |
fLastErrorSymbol: REChar; |
| 413 |
|
| 414 |
fModifiers: TRegExprModifiers; // regex modifiers |
| 415 |
fCompModifiers: TRegExprModifiers; // compiler's copy of modifiers |
| 416 |
fProgModifiers: TRegExprModifiers; // modifiers values from last programm compilation |
| 417 |
|
| 418 |
{$IFDEF UseSpaceChars} |
| 419 |
fSpaceChars: RegExprString; |
| 420 |
{$ENDIF} |
| 421 |
{$IFDEF UseWordChars} |
| 422 |
fWordChars: RegExprString; |
| 423 |
{$ENDIF} |
| 424 |
|
| 425 |
{$IFDEF UseLineSep} |
| 426 |
fLineSeparators: RegExprString; |
| 427 |
{$ENDIF} |
| 428 |
|
| 429 |
fUsePairedBreak: Boolean; |
| 430 |
fReplaceLineEnd: RegExprString; // string to use for "\n" in Substitute method |
| 431 |
|
| 432 |
fSlowChecksSizeMax: Integer; |
| 433 |
// Exec() param ASlowChecks is set to True, when Length(InputString)<SlowChecksSizeMax |
| 434 |
// This ASlowChecks enables to use regMustString optimization |
| 435 |
|
| 436 |
{$IFDEF UseLineSep} |
| 437 |
{$IFNDEF UnicodeRE} |
| 438 |
fLineSepArray: array[Byte] of Boolean; |
| 439 |
{$ENDIF} |
| 440 |
{$ENDIF} |
| 441 |
|
| 442 |
CharCheckers: TRegExprCharCheckerArray; |
| 443 |
CharCheckerInfos: TRegExprCharCheckerInfos; |
| 444 |
CheckerIndex_Word: Byte; |
| 445 |
CheckerIndex_NotWord: Byte; |
| 446 |
CheckerIndex_Digit: Byte; |
| 447 |
CheckerIndex_NotDigit: Byte; |
| 448 |
CheckerIndex_Space: Byte; |
| 449 |
CheckerIndex_NotSpace: Byte; |
| 450 |
CheckerIndex_HorzSep: Byte; |
| 451 |
CheckerIndex_NotHorzSep: Byte; |
| 452 |
CheckerIndex_VertSep: Byte; |
| 453 |
CheckerIndex_NotVertSep: Byte; |
| 454 |
CheckerIndex_LowerAZ: Byte; |
| 455 |
CheckerIndex_UpperAZ: Byte; |
| 456 |
CheckerIndex_AnyLineBreak: Byte; |
| 457 |
{$IFDEF RegExpWithStackOverflowCheck_DecStack_Frame} |
| 458 |
StackLimit: Pointer; |
| 459 |
{$ENDIF} |
| 460 |
|
| 461 |
{$IFDEF Compat} |
| 462 |
fUseUnicodeWordDetection: Boolean; |
| 463 |
fInvertCase: TRegExprInvertCaseFunction; |
| 464 |
fEmptyInputRaisesError: Boolean; |
| 465 |
fUseOsLineEndOnReplace: Boolean; |
| 466 |
function OldInvertCase(const Ch: REChar): REChar; |
| 467 |
function GetLinePairedSeparator: RegExprString; |
| 468 |
procedure SetLinePairedSeparator(const AValue: RegExprString); |
| 469 |
procedure SetUseOsLineEndOnReplace(AValue: Boolean); |
| 470 |
{$ENDIF} |
| 471 |
|
| 472 |
procedure InitCharCheckers; |
| 473 |
function CharChecker_Word(ch: REChar): Boolean; |
| 474 |
function CharChecker_NotWord(ch: REChar): Boolean; |
| 475 |
function CharChecker_Space(ch: REChar): Boolean; |
| 476 |
function CharChecker_NotSpace(ch: REChar): Boolean; |
| 477 |
function CharChecker_Digit(ch: REChar): Boolean; |
| 478 |
function CharChecker_NotDigit(ch: REChar): Boolean; |
| 479 |
function CharChecker_HorzSep(ch: REChar): Boolean; |
| 480 |
function CharChecker_NotHorzSep(ch: REChar): Boolean; |
| 481 |
function CharChecker_VertSep(ch: REChar): Boolean; |
| 482 |
function CharChecker_NotVertSep(ch: REChar): Boolean; |
| 483 |
function CharChecker_AnyLineBreak(ch: REChar): Boolean; |
| 484 |
function CharChecker_LowerAZ(ch: REChar): Boolean; |
| 485 |
function CharChecker_UpperAZ(ch: REChar): Boolean; |
| 486 |
function DumpCheckerIndex(N: Byte): RegExprString; |
| 487 |
function DumpCategoryChars(ch, ch2: REChar; Positive: Boolean): RegExprString; |
| 488 |
|
| 489 |
procedure ClearMatches; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 490 |
procedure ClearInternalExecData; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 491 |
procedure InitInternalGroupData; |
| 492 |
function FindInCharClass(ABuffer: PRegExprChar; AChar: REChar): Boolean; |
| 493 |
procedure GetCharSetFromCharClass(ABuffer: PRegExprChar; AIgnoreCase: Boolean; var ARes: TRegExprCharset); |
| 494 |
procedure GetCharSetFromSpaceChars(var ARes: TRegExprCharset); {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 495 |
procedure GetCharSetFromWordChars(var ARes: TRegExprCharSet); {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 496 |
function IsWordChar(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 497 |
function IsSpaceChar(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 498 |
function IsCustomLineSeparator(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 499 |
{$IFDEF UseLineSep} |
| 500 |
procedure InitLineSepArray; |
| 501 |
{$ENDIF} |
| 502 |
procedure FindGroupName(APtr, AEndPtr: PRegExprChar; AEndChar: REChar; var AName: RegExprString); |
| 503 |
|
| 504 |
// Mark programm as having to be [re]compiled |
| 505 |
procedure InvalidateProgramm; |
| 506 |
|
| 507 |
// Check if we can use compiled regex, compile it if something changed |
| 508 |
function IsProgrammOk: Boolean; |
| 509 |
|
| 510 |
procedure SetExpression(const AStr: RegExprString); |
| 511 |
|
| 512 |
function GetModifierStr: RegExprString; |
| 513 |
procedure SetModifierStr(const AStr: RegExprString); |
| 514 |
function GetModifierG: Boolean; |
| 515 |
function GetModifierI: Boolean; |
| 516 |
function GetModifierM: Boolean; |
| 517 |
function GetModifierR: Boolean; |
| 518 |
function GetModifierS: Boolean; |
| 519 |
function GetModifierX: Boolean; |
| 520 |
procedure SetModifierG(AValue: Boolean); |
| 521 |
procedure SetModifierI(AValue: Boolean); |
| 522 |
procedure SetModifierM(AValue: Boolean); |
| 523 |
procedure SetModifierR(AValue: Boolean); |
| 524 |
procedure SetModifierS(AValue: Boolean); |
| 525 |
procedure SetModifierX(AValue: Boolean); |
| 526 |
|
| 527 |
{ ==================== Compiler section =================== } |
| 528 |
// compile a regular expression into internal code |
| 529 |
function CompileRegExpr(ARegExp: PRegExprChar): Boolean; |
| 530 |
|
| 531 |
// set the next-pointer at the end of a node chain |
| 532 |
procedure Tail(p: PRegExprChar; val: PRegExprChar); |
| 533 |
|
| 534 |
// regoptail - regtail on operand of first argument; nop if operandless |
| 535 |
procedure OpTail(p: PRegExprChar; val: PRegExprChar); |
| 536 |
|
| 537 |
// regnode - emit a node, return location |
| 538 |
function EmitNode(op: TREOp): PRegExprChar; |
| 539 |
|
| 540 |
// emit OP_BRANCH (and fillchars) |
| 541 |
function EmitBranch: PRegExprChar; {$IFDEF FPC}inline;{$ENDIF} |
| 542 |
|
| 543 |
// emit (if appropriate) a byte of code |
| 544 |
procedure EmitC(ch: REChar); {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 545 |
|
| 546 |
// emit LongInt value |
| 547 |
procedure EmitInt(AValue: LongInt); {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 548 |
|
| 549 |
// for groups |
| 550 |
function EmitNodeWithGroupIndex(op: TREOp; AIndex: Integer): PRegExprChar; |
| 551 |
|
| 552 |
// emit back-reference to group |
| 553 |
function EmitGroupRef(AIndex: Integer; AIgnoreCase: Boolean): PRegExprChar; |
| 554 |
|
| 555 |
{$IFDEF FastUnicodeData} |
| 556 |
procedure FindCategoryName(var scan: PRegExprChar; var ch1, ch2: REChar); |
| 557 |
function EmitCategoryMain(APositive: Boolean): PRegExprChar; |
| 558 |
{$ENDIF} |
| 559 |
|
| 560 |
// insert an operator in front of already-emitted operand |
| 561 |
// Means relocating the operand. |
| 562 |
procedure InsertOperator(op: TREOp; opnd: PRegExprChar; sz: Integer); |
| 563 |
procedure RemoveOperator(opnd: PRegExprChar; sz: Integer); |
| 564 |
|
| 565 |
// regular expression, i.e. main body or parenthesized thing |
| 566 |
function ParseReg(var FlagParse: Integer): PRegExprChar; |
| 567 |
function DoParseReg(InBrackets: Boolean; BracketCounter: PInteger; var FlagParse: Integer; BeginGroupOp, EndGroupOP: TReOp): PRegExprChar; |
| 568 |
|
| 569 |
// one alternative of an | operator |
| 570 |
function ParseBranch(var FlagParse: Integer): PRegExprChar; |
| 571 |
|
| 572 |
procedure MaybeGuardBranchPiece(piece: PRegExprChar); |
| 573 |
|
| 574 |
// something followed by possible [*+?] |
| 575 |
function ParsePiece(var FlagParse: Integer): PRegExprChar; |
| 576 |
|
| 577 |
function HexDig(Ch: REChar): Integer; |
| 578 |
|
| 579 |
function UnQuoteChar(var APtr, AEnd: PRegExprChar): REChar; |
| 580 |
|
| 581 |
// the lowest level |
| 582 |
function ParseAtom(var FlagParse: Integer): PRegExprChar; |
| 583 |
|
| 584 |
// current pos in r.e. - for error hanling |
| 585 |
function GetCompilerErrorPos: PtrInt; |
| 586 |
|
| 587 |
{$IFDEF UseFirstCharSet} |
| 588 |
procedure FillFirstCharSet(prog: PRegExprChar); |
| 589 |
{$ENDIF} |
| 590 |
|
| 591 |
function IsPartFixedLength(var prog: PRegExprChar; var op: TREOp; var AMinLen, AMaxLen: integer; StopAt: TREOp; StopMaxProg: PRegExprChar; Flags: TRegExprFindFixedLengthFlags): boolean; |
| 592 |
|
| 593 |
{ ===================== Matching section =================== } |
| 594 |
// repeatedly match something simple, report how many |
| 595 |
function FindRepeated(p: PRegExprChar; AMax: Integer): Integer; |
| 596 |
|
| 597 |
// dig the "next" pointer out of a node |
| 598 |
function regNext(p: PRegExprChar): PRegExprChar; |
| 599 |
function regNextQuick(p: PRegExprChar): PRegExprChar; {$IFDEF FPC}inline;{$ENDIF} |
| 600 |
|
| 601 |
// dig the "last" pointer out of a chain of node |
| 602 |
function regLast(p: PRegExprChar): PRegExprChar; |
| 603 |
|
| 604 |
// recursively matching routine |
| 605 |
function MatchPrim(prog: PRegExprChar): Boolean; |
| 606 |
|
| 607 |
// match at specific position only, called from ExecPrim |
| 608 |
function MatchAtOnePos(APos: PRegExprChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 609 |
|
| 610 |
// Exec for stored InputString |
| 611 |
function ExecPrim(AOffset: Integer; ASlowChecks, ABackward: Boolean; ATryMatchOnlyStartingBefore: Integer): Boolean; |
| 612 |
function ExecPrimProtected(AOffset: Integer; ASlowChecks, ABackward: Boolean; ATryMatchOnlyStartingBefore: Integer): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 613 |
|
| 614 |
function GetSubExprCount: Integer; |
| 615 |
function GetMatchPos(Idx: Integer): PtrInt; |
| 616 |
function GetMatchLen(Idx: Integer): PtrInt; |
| 617 |
function GetMatch(Idx: Integer): RegExprString; |
| 618 |
|
| 619 |
procedure SetInputString(const AInputString: RegExprString); |
| 620 |
procedure SetInputRange(AStart, AEnd, AContinueAnchor: PRegExprChar); |
| 621 |
|
| 622 |
{$IFDEF UseLineSep} |
| 623 |
procedure SetLineSeparators(const AStr: RegExprString); |
| 624 |
{$ENDIF} |
| 625 |
procedure SetUsePairedBreak(AValue: Boolean); |
| 626 |
|
| 627 |
protected |
| 628 |
// Default handler raises exception ERegExpr with |
| 629 |
// Message = ErrorMsg (AErrorID), ErrorCode = AErrorID |
| 630 |
// and CompilerErrorPos = value of property CompilerErrorPos. |
| 631 |
procedure Error(AErrorID: Integer); virtual; // error handler. |
| 632 |
|
| 633 |
public |
| 634 |
constructor Create; {$IFDEF OverMeth} overload; |
| 635 |
constructor Create(const AExpression: RegExprString); overload; |
| 636 |
{$ENDIF} |
| 637 |
destructor Destroy; override; |
| 638 |
|
| 639 |
class function VersionMajor: Integer; |
| 640 |
class function VersionMinor: Integer; |
| 641 |
|
| 642 |
// match a programm against a string AInputString |
| 643 |
// Exec stores AInputString into InputString property |
| 644 |
// For Delphi 5 and higher overloaded versions are available: first without |
| 645 |
// parameter (uses already assigned InputString property value) |
| 646 |
// and second has int parameter, same as for ExecPos |
| 647 |
function Exec(const AInputString: RegExprString): Boolean; |
| 648 |
{$IFDEF OverMeth}overload;{$endif} {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 649 |
|
| 650 |
{$IFDEF OverMeth} |
| 651 |
function Exec: Boolean; overload; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 652 |
function Exec(AOffset: Integer): Boolean; overload; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 653 |
{$ENDIF} |
| 654 |
|
| 655 |
// find next match: |
| 656 |
// ExecNext; |
| 657 |
// works the same as |
| 658 |
// if MatchLen [0] = 0 then ExecPos (MatchPos [0] + 1) |
| 659 |
// else ExecPos (MatchPos [0] + MatchLen [0]); |
| 660 |
// but it's more simpler ! |
| 661 |
// Raises exception if used without preceeding SUCCESSFUL call to |
| 662 |
// Exec* (Exec, ExecPos, ExecNext). So You always must use something like |
| 663 |
// if Exec (InputString) then repeat { proceed results} until not ExecNext; |
| 664 |
function ExecNext(ABackward: Boolean {$IFDEF DefParam} = False{$ENDIF}): Boolean; |
| 665 |
|
| 666 |
// find match for InputString starting from AOffset position |
| 667 |
// (AOffset=1 - first char of InputString) |
| 668 |
function ExecPos(AOffset: Integer {$IFDEF DefParam} = 1{$ENDIF}): Boolean; |
| 669 |
{$IFDEF OverMeth}overload;{$endif} {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 670 |
|
| 671 |
{$IFDEF OverMeth} |
| 672 |
// find match for InputString at AOffset. |
| 673 |
// if ATryOnce=True then only match exactly at AOffset (like anchor \G) |
| 674 |
// if ATryMatchOnlyStartingBefore then only when the match can start before |
| 675 |
// that position: Result := MatchPos[0] < ATryMatchOnlyStartingBefore; |
| 676 |
function ExecPos(AOffset: Integer; ATryOnce, ABackward: Boolean): Boolean; overload; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 677 |
function ExecPos(AOffset, ATryMatchOnlyStartingBefore: Integer): Boolean; overload; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 678 |
{$ENDIF} |
| 679 |
|
| 680 |
// Returns ATemplate with '$&' or '$0' replaced by whole r.e. |
| 681 |
// occurence and '$1'...'$nn' replaced by subexpression with given index. |
| 682 |
// Symbol '$' is used instead of '\' (for future extensions |
| 683 |
// and for more Perl-compatibility) and accepts more than one digit. |
| 684 |
// If you want to place into template raw '$' or '\', use prefix '\'. |
| 685 |
// Example: '1\$ is $2\\rub\\' -> '1$ is <Match[2]>\rub\' |
| 686 |
// If you want to place any number after '$' you must enclose it |
| 687 |
// with curly braces: '${12}'. |
| 688 |
// Example: 'a$12bc' -> 'a<Match[12]>bc' |
| 689 |
// 'a${1}2bc' -> 'a<Match[1]>2bc'. |
| 690 |
function Substitute(const ATemplate: RegExprString): RegExprString; |
| 691 |
|
| 692 |
// Splits AInputStr to list by positions of all r.e. occurencies. |
| 693 |
// Internally calls Exec, ExecNext. |
| 694 |
procedure Split(const AInputStr: RegExprString; APieces: TStrings); |
| 695 |
|
| 696 |
function Replace(const AInputStr: RegExprString; |
| 697 |
const AReplaceStr: RegExprString; |
| 698 |
AUseSubstitution: Boolean{$IFDEF DefParam} = False{$ENDIF}) |
| 699 |
: RegExprString; {$IFDEF OverMeth} overload; |
| 700 |
function Replace(const AInputStr: RegExprString; |
| 701 |
AReplaceFunc: TRegExprReplaceFunction): RegExprString; overload; |
| 702 |
{$ENDIF} |
| 703 |
// Returns AInputStr with r.e. occurencies replaced by AReplaceStr. |
| 704 |
// If AUseSubstitution is true, then AReplaceStr will be used |
| 705 |
// as template for Substitution methods. |
| 706 |
// For example: |
| 707 |
// Expression := '({-i}block|var)\s*\(\s*([^ ]*)\s*\)\s*'; |
| 708 |
// Replace ('BLOCK( test1)', 'def "$1" value "$2"', True); |
| 709 |
// will return: def 'BLOCK' value 'test1' |
| 710 |
// Replace ('BLOCK( test1)', 'def "$1" value "$2"') |
| 711 |
// will return: def "$1" value "$2" |
| 712 |
// Internally calls Exec, ExecNext. |
| 713 |
// Overloaded version and ReplaceEx operate with callback function, |
| 714 |
// so you can implement really complex functionality. |
| 715 |
function ReplaceEx(const AInputStr: RegExprString; |
| 716 |
AReplaceFunc: TRegExprReplaceFunction): RegExprString; |
| 717 |
|
| 718 |
{$IFDEF Compat} |
| 719 |
function ExecPos(AOffset: Integer; ATryOnce: Boolean): Boolean; overload; deprecated 'Use modern form of ExecPos()'; |
| 720 |
class function InvertCaseFunction(const Ch: REChar): REChar; deprecated 'This has no effect now'; |
| 721 |
property InvertCase: TRegExprInvertCaseFunction read fInvertCase write fInvertCase; deprecated 'This has no effect now'; |
| 722 |
property UseUnicodeWordDetection: Boolean read fUseUnicodeWordDetection write fUseUnicodeWordDetection; deprecated 'This has no effect, use {$DEFINE UnicodeRE} instead'; |
| 723 |
property LinePairedSeparator: RegExprString read GetLinePairedSeparator write SetLinePairedSeparator; deprecated 'This has no effect now'; |
| 724 |
property EmptyInputRaisesError: Boolean read fEmptyInputRaisesError write fEmptyInputRaisesError; deprecated 'This has no effect now'; |
| 725 |
property UseOsLineEndOnReplace: Boolean read fUseOsLineEndOnReplace write SetUseOsLineEndOnReplace; deprecated 'Use property ReplaceLineEnd instead'; |
| 726 |
{$ENDIF} |
| 727 |
|
| 728 |
// Returns ID of last error, 0 if no errors (unusable if |
| 729 |
// Error method raises exception) and clear internal status |
| 730 |
// into 0 (no errors). |
| 731 |
function LastError: Integer; |
| 732 |
|
| 733 |
// Returns Error message for error with ID = AErrorID. |
| 734 |
function ErrorMsg(AErrorID: Integer): RegExprString; virtual; |
| 735 |
|
| 736 |
// Re-compile regex |
| 737 |
procedure Compile; |
| 738 |
|
| 739 |
{$IFDEF RegExpPCodeDump} |
| 740 |
// Show compiled regex in textual form |
| 741 |
function Dump(Indent: Integer = 0): RegExprString; |
| 742 |
// Show single opcode in textual form |
| 743 |
function DumpOp(op: TREOp): RegExprString; |
| 744 |
{$ENDIF} |
| 745 |
|
| 746 |
function IsCompiled: Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 747 |
|
| 748 |
// Opcode contains only operations for fixed match length: EXACTLY*, ANY*, etc |
| 749 |
function IsFixedLength(var op: TREOp; var ALen: Integer): Boolean; |
| 750 |
function IsFixedLengthEx(var op: TREOp; var AMinLen, AMaxLen: integer): boolean; |
| 751 |
|
| 752 |
// Regular expression. |
| 753 |
// For optimization, TRegExpr will automatically compiles it into 'P-code' |
| 754 |
// (You can see it with help of Dump method) and stores in internal |
| 755 |
// structures. Real [re]compilation occures only when it really needed - |
| 756 |
// while calling Exec, ExecNext, Substitute, Dump, etc |
| 757 |
// and only if Expression or other P-code affected properties was changed |
| 758 |
// after last [re]compilation. |
| 759 |
// If any errors while [re]compilation occures, Error method is called |
| 760 |
// (by default Error raises exception - see below) |
| 761 |
property Expression: RegExprString read fExpression write SetExpression; |
| 762 |
|
| 763 |
// Set/get default values of r.e.syntax modifiers. Modifiers in |
| 764 |
// r.e. (?ismx-ismx) will replace this default values. |
| 765 |
// If you try to set unsupported modifier, Error will be called |
| 766 |
// (by defaul Error raises exception ERegExpr). |
| 767 |
property ModifierStr: RegExprString read GetModifierStr write SetModifierStr; |
| 768 |
|
| 769 |
property ModifierI: Boolean read GetModifierI write SetModifierI; |
| 770 |
property ModifierR: Boolean read GetModifierR write SetModifierR; |
| 771 |
property ModifierS: Boolean read GetModifierS write SetModifierS; |
| 772 |
property ModifierG: Boolean read GetModifierG write SetModifierG; |
| 773 |
property ModifierM: Boolean read GetModifierM write SetModifierM; |
| 774 |
property ModifierX: Boolean read GetModifierX write SetModifierX; |
| 775 |
|
| 776 |
// returns current input string (from last Exec call or last assign |
| 777 |
// to this property). |
| 778 |
// Any assignment to this property clear Match* properties ! |
| 779 |
property InputString: RegExprString read fInputString write SetInputString; |
| 780 |
// SetInputSubString |
| 781 |
// Only looks at copy(AInputString, AInputStartPos, AInputLen) |
| 782 |
procedure SetInputSubString(const AInputString: RegExprString; AInputStartPos, AInputLen: Integer); |
| 783 |
|
| 784 |
// Number of subexpressions has been found in last Exec* call. |
| 785 |
// If there are no subexpr. but whole expr was found (Exec* returned True), |
| 786 |
// then SubExprMatchCount=0, if no subexpressions nor whole |
| 787 |
// r.e. found (Exec* returned false) then SubExprMatchCount=-1. |
| 788 |
// Note, that some subexpr. may be not found and for such |
| 789 |
// subexpr. MathPos=MatchLen=-1 and Match=''. |
| 790 |
// For example: Expression := '(1)?2(3)?'; |
| 791 |
// Exec ('123'): SubExprMatchCount=2, Match[0]='123', [1]='1', [2]='3' |
| 792 |
// Exec ('12'): SubExprMatchCount=1, Match[0]='12', [1]='1' |
| 793 |
// Exec ('23'): SubExprMatchCount=2, Match[0]='23', [1]='', [2]='3' |
| 794 |
// Exec ('2'): SubExprMatchCount=0, Match[0]='2' |
| 795 |
// Exec ('7') - return False: SubExprMatchCount=-1 |
| 796 |
property SubExprMatchCount: Integer read GetSubExprCount; |
| 797 |
|
| 798 |
// pos of entrance subexpr. #Idx into tested in last Exec* |
| 799 |
// string. First subexpr. has Idx=1, last - MatchCount, |
| 800 |
// whole r.e. has Idx=0. |
| 801 |
// Returns -1 if in r.e. no such subexpr. or this subexpr. |
| 802 |
// not found in input string. |
| 803 |
property MatchPos[Idx: Integer]: PtrInt read GetMatchPos; |
| 804 |
|
| 805 |
// len of entrance subexpr. #Idx r.e. into tested in last Exec* |
| 806 |
// string. First subexpr. has Idx=1, last - MatchCount, |
| 807 |
// whole r.e. has Idx=0. |
| 808 |
// Returns -1 if in r.e. no such subexpr. or this subexpr. |
| 809 |
// not found in input string. |
| 810 |
// Remember - MatchLen may be 0 (if r.e. match empty string) ! |
| 811 |
property MatchLen[Idx: Integer]: PtrInt read GetMatchLen; |
| 812 |
|
| 813 |
// == copy (InputString, MatchPos [Idx], MatchLen [Idx]) |
| 814 |
// Returns '' if in r.e. no such subexpr. or this subexpr. |
| 815 |
// not found in input string. |
| 816 |
property Match[Idx: Integer]: RegExprString read GetMatch; |
| 817 |
|
| 818 |
// get index of group (subexpression) by name, to support named groups |
| 819 |
// like in Python: (?P<name>regex) |
| 820 |
function MatchIndexFromName(const AName: RegExprString): Integer; |
| 821 |
|
| 822 |
function MatchFromName(const AName: RegExprString): RegExprString; |
| 823 |
|
| 824 |
// Returns position in r.e. where compiler stopped. |
| 825 |
// Useful for error diagnostics |
| 826 |
property CompilerErrorPos: PtrInt read GetCompilerErrorPos; |
| 827 |
|
| 828 |
{$IFDEF UseSpaceChars} |
| 829 |
// Contains chars, treated as /s (initially filled with RegExprSpaceChars |
| 830 |
// global constant) |
| 831 |
property SpaceChars: RegExprString read fSpaceChars write fSpaceChars; |
| 832 |
{$ENDIF} |
| 833 |
|
| 834 |
{$IFDEF UseWordChars} |
| 835 |
// Contains chars, treated as /w (initially filled with RegExprWordChars |
| 836 |
// global constant) |
| 837 |
property WordChars: RegExprString read fWordChars write fWordChars; |
| 838 |
{$ENDIF} |
| 839 |
|
| 840 |
{$IFDEF UseLineSep} |
| 841 |
// line separators (like \n in Unix) |
| 842 |
property LineSeparators: RegExprString read fLineSeparators write SetLineSeparators; |
| 843 |
{$ENDIF} |
| 844 |
|
| 845 |
// support paired line-break CR LF |
| 846 |
property UseLinePairedBreak: Boolean read fUsePairedBreak write SetUsePairedBreak; |
| 847 |
|
| 848 |
property ReplaceLineEnd: RegExprString read fReplaceLineEnd write fReplaceLineEnd; |
| 849 |
|
| 850 |
property SlowChecksSizeMax: Integer read fSlowChecksSizeMax write fSlowChecksSizeMax; |
| 851 |
|
| 852 |
// Errors during Exec() return false and set LastError. This option allows |
| 853 |
// them to raise an Exception |
| 854 |
property RaiseForRuntimeError: Boolean read fRaiseForRuntimeError write fRaiseForRuntimeError; |
| 855 |
|
| 856 |
property AllowUnsafeLookBehind: Boolean read FAllowUnsafeLookBehind write FAllowUnsafeLookBehind; |
| 857 |
|
| 858 |
// Make sure a { always is a range / don't allow unescaped literal usage |
| 859 |
property AllowLiteralBraceWithoutRange: Boolean read FAllowLiteralBraceWithoutRange write FAllowLiteralBraceWithoutRange; |
| 860 |
// support {,123} defaulting the min-matches to 0 |
| 861 |
property AllowBraceWithoutMin: Boolean read FAllowBraceWithoutMin write FAllowBraceWithoutMin; |
| 862 |
end; |
| 863 |
|
| 864 |
type |
| 865 |
ERegExpr = class(Exception) |
| 866 |
public |
| 867 |
ErrorCode: Integer; |
| 868 |
CompilerErrorPos: PtrInt; |
| 869 |
end; |
| 870 |
|
| 871 |
// true if string AInputString match regular expression ARegExpr |
| 872 |
// ! will raise exeption if syntax errors in ARegExpr |
| 873 |
function ExecRegExpr(const ARegExpr, AInputStr: RegExprString): Boolean; |
| 874 |
|
| 875 |
// Split AInputStr into APieces by r.e. ARegExpr occurencies |
| 876 |
procedure SplitRegExpr(const ARegExpr, AInputStr: RegExprString; |
| 877 |
APieces: TStrings); |
| 878 |
|
| 879 |
// Returns AInputStr with r.e. occurencies replaced by AReplaceStr |
| 880 |
// If AUseSubstitution is true, then AReplaceStr will be used |
| 881 |
// as template for Substitution methods. |
| 882 |
// For example: |
| 883 |
// ReplaceRegExpr ('({-i}block|var)\s*\(\s*([^ ]*)\s*\)\s*', |
| 884 |
// 'BLOCK( test1)', 'def "$1" value "$2"', True) |
| 885 |
// will return: def 'BLOCK' value 'test1' |
| 886 |
// ReplaceRegExpr ('({-i}block|var)\s*\(\s*([^ ]*)\s*\)\s*', |
| 887 |
// 'BLOCK( test1)', 'def "$1" value "$2"') |
| 888 |
// will return: def "$1" value "$2" |
| 889 |
function ReplaceRegExpr(const ARegExpr, AInputStr, AReplaceStr: RegExprString; |
| 890 |
AUseSubstitution: Boolean{$IFDEF DefParam} = False{$ENDIF}): RegExprString; |
| 891 |
{$IFDEF OverMeth}overload; |
| 892 |
|
| 893 |
// Alternate form allowing to set more parameters. |
| 894 |
|
| 895 |
type |
| 896 |
TRegexReplaceOption = ( |
| 897 |
rroModifierI, |
| 898 |
rroModifierR, |
| 899 |
rroModifierS, |
| 900 |
rroModifierG, |
| 901 |
rroModifierM, |
| 902 |
rroModifierX, |
| 903 |
rroUseSubstitution, |
| 904 |
rroUseOsLineEnd |
| 905 |
); |
| 906 |
TRegexReplaceOptions = set of TRegexReplaceOption; |
| 907 |
|
| 908 |
function ReplaceRegExpr(const ARegExpr, AInputStr, AReplaceStr: RegExprString; |
| 909 |
Options: TRegexReplaceOptions): RegExprString; overload; |
| 910 |
{$ENDIF} |
| 911 |
// Replace all metachars with its safe representation, |
| 912 |
// for example 'abc$cd.(' converts into 'abc\$cd\.\(' |
| 913 |
// This function useful for r.e. autogeneration from |
| 914 |
// user input |
| 915 |
function QuoteRegExprMetaChars(const AStr: RegExprString): RegExprString; |
| 916 |
// Makes list of subexpressions found in ARegExpr r.e. |
| 917 |
// In ASubExps every item represent subexpression, |
| 918 |
// from first to last, in format: |
| 919 |
// String - subexpression text (without '()') |
| 920 |
// low word of Object - starting position in ARegExpr, including '(' |
| 921 |
// if exists! (first position is 1) |
| 922 |
// high word of Object - length, including starting '(' and ending ')' |
| 923 |
// if exist! |
| 924 |
// AExtendedSyntax - must be True if modifier /m will be On while |
| 925 |
// using the r.e. |
| 926 |
// Useful for GUI editors of r.e. etc (You can find example of using |
| 927 |
// in TestRExp.dpr project) |
| 928 |
// Returns |
| 929 |
// 0 Success. No unbalanced brackets was found; |
| 930 |
// -1 There are not enough closing brackets ')'; |
| 931 |
// -(n+1) At position n was found opening '[' without |
| 932 |
// corresponding closing ']'; |
| 933 |
// n At position n was found closing bracket ')' without |
| 934 |
// corresponding opening '('. |
| 935 |
// If Result <> 0, then ASubExpr can contain empty items or illegal ones |
| 936 |
function RegExprSubExpressions(const ARegExpr: RegExprString; ASubExprs: TStrings; |
| 937 |
AExtendedSyntax: Boolean{$IFDEF DefParam} = False{$ENDIF}): Integer; |
| 938 |
|
| 939 |
implementation |
| 940 |
|
| 941 |
{$IFDEF FastUnicodeData} |
| 942 |
uses |
| 943 |
regexpr_unicodedata; |
| 944 |
{$ENDIF} |
| 945 |
|
| 946 |
const |
| 947 |
// TRegExpr.VersionMajor/Minor return values of these constants: |
| 948 |
REVersionMajor = 1; |
| 949 |
REVersionMinor = 186; |
| 950 |
|
| 951 |
OpKind_End = REChar(1); |
| 952 |
OpKind_MetaClass = REChar(2); |
| 953 |
OpKind_Range = REChar(3); |
| 954 |
OpKind_Char = REChar(4); |
| 955 |
OpKind_CategoryYes = REChar(5); |
| 956 |
OpKind_CategoryNo = REChar(6); |
| 957 |
|
| 958 |
RegExprAllSet = [0 .. 255]; |
| 959 |
RegExprWordSet = [Ord('a') .. Ord('z'), Ord('A') .. Ord('Z'), Ord('0') .. Ord('9'), Ord('_')]; |
| 960 |
RegExprDigitSet = [Ord('0') .. Ord('9')]; |
| 961 |
RegExprLowerAzSet = [Ord('a') .. Ord('z')]; |
| 962 |
RegExprUpperAzSet = [Ord('A') .. Ord('Z')]; |
| 963 |
RegExprAllAzSet = RegExprLowerAzSet + RegExprUpperAzSet; |
| 964 |
RegExprSpaceSet = [Ord(' '), $9, $A, $D, $C]; |
| 965 |
RegExprLineSeparatorsSet = [$d, $a, $b, $c] {$IFDEF UnicodeRE} + [$85] {$ENDIF}; |
| 966 |
RegExprHorzSeparatorsSet = [9, $20, $A0]; |
| 967 |
|
| 968 |
MaxBracesArg = $7FFFFFFF - 1; // max value for {n,m} arguments |
| 969 |
|
| 970 |
type |
| 971 |
TRENextOff = PtrInt; |
| 972 |
// internal Next "pointer" (offset to current p-code) |
| 973 |
PRENextOff = ^TRENextOff; |
| 974 |
// used for extracting Next "pointers" from compiled r.e. |
| 975 |
TREBracesArg = Integer; // type of {m,n} arguments |
| 976 |
PREBracesArg = ^TREBracesArg; |
| 977 |
|
| 978 |
TREGroupKind = ( |
| 979 |
gkNormalGroup, |
| 980 |
gkNonCapturingGroup, |
| 981 |
gkAtomicGroup, |
| 982 |
gkNamedGroupReference, |
| 983 |
gkComment, |
| 984 |
gkModifierString, |
| 985 |
gkLookahead, |
| 986 |
gkLookaheadNeg, |
| 987 |
gkLookbehind, |
| 988 |
gkLookbehindNeg, |
| 989 |
gkRecursion, |
| 990 |
gkSubCall |
| 991 |
); |
| 992 |
|
| 993 |
TReOpLookBehindOptions = packed record |
| 994 |
MatchLenMin, MatchLenMax: TREBracesArg; |
| 995 |
IsGreedy: REChar; |
| 996 |
end; |
| 997 |
PReOpLookBehindOptions = ^TReOpLookBehindOptions; |
| 998 |
|
| 999 |
const |
| 1000 |
ReOpLookBehindOptionsSz = SizeOf(TReOpLookBehindOptions) div SizeOf(REChar); |
| 1001 |
OPT_LOOKBEHIND_NON_GREEDY = REChar(0); |
| 1002 |
OPT_LOOKBEHIND_GREEDY = REChar(1); |
| 1003 |
OPT_LOOKBEHIND_FIXED = REChar(2); |
| 1004 |
|
| 1005 |
// Alexey T.: handling of that define FPC_REQUIRES_PROPER_ALIGNMENT was present even 15 years ago, |
| 1006 |
// but with it, we have failing of some RegEx tests, on ARM64 CPU. |
| 1007 |
// If I undefine FPC_REQUIRES_PROPER_ALIGNMENT, all tests run OK on ARM64 again. |
| 1008 |
{$undef FPC_REQUIRES_PROPER_ALIGNMENT} |
| 1009 |
|
| 1010 |
const |
| 1011 |
REOpSz = SizeOf(TREOp) div SizeOf(REChar); |
| 1012 |
// size of OP_ command in REChars |
| 1013 |
{$IFDEF FPC_REQUIRES_PROPER_ALIGNMENT} |
| 1014 |
// add space for aligning pointer |
| 1015 |
// -1 is the correct max size but also needed for InsertOperator that needs a multiple of pointer size |
| 1016 |
RENextOffSz = (2 * SizeOf(TRENextOff) div SizeOf(REChar)) - 1; |
| 1017 |
REBracesArgSz = (2 * SizeOf(TREBracesArg) div SizeOf(REChar)); |
| 1018 |
// add space for aligning pointer |
| 1019 |
{$ELSE} |
| 1020 |
RENextOffSz = (SizeOf(TRENextOff) div SizeOf(REChar)); |
| 1021 |
// size of Next pointer in REChars |
| 1022 |
REBracesArgSz = SizeOf(TREBracesArg) div SizeOf(REChar); |
| 1023 |
// size of BRACES arguments in REChars |
| 1024 |
{$ENDIF} |
| 1025 |
RENumberSz = SizeOf(LongInt) div SizeOf(REChar); |
| 1026 |
|
| 1027 |
REBranchArgSz = 2; // 2 * (REChar div REChar) |
| 1028 |
|
| 1029 |
type |
| 1030 |
TReGroupIndex = LongInt; |
| 1031 |
PReGroupIndex = ^TReGroupIndex; |
| 1032 |
const |
| 1033 |
ReGroupIndexSz = SizeOf(TReGroupIndex) div SizeOf(REChar); |
| 1034 |
|
| 1035 |
type |
| 1036 |
PtrPair = {$IFDEF UnicodeRE} ^LongInt; {$ELSE} ^Word; {$ENDIF} |
| 1037 |
|
| 1038 |
function GroupDataArraySize(ARequired, ACurrent: Integer): Integer; |
| 1039 |
begin |
| 1040 |
Result := ARequired; |
| 1041 |
if Result > ACurrent then |
| 1042 |
Exit; |
| 1043 |
|
| 1044 |
// Keep some extra |
| 1045 |
if Result > ACurrent - RegexGroupCountIncrement then |
| 1046 |
Result := ACurrent; |
| 1047 |
end; |
| 1048 |
|
| 1049 |
function IsPairedBreak(p: PRegExprChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1050 |
const |
| 1051 |
cBreak = {$IFDEF UnicodeRE} $000D000A; {$ELSE} $0D0A; {$ENDIF} |
| 1052 |
begin |
| 1053 |
Result := PtrPair(p)^ = cBreak; |
| 1054 |
end; |
| 1055 |
|
| 1056 |
function IsAnyLineBreak(C: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1057 |
begin |
| 1058 |
case C of |
| 1059 |
#10, |
| 1060 |
#13, |
| 1061 |
#$0B, |
| 1062 |
#$0C |
| 1063 |
{$ifdef UnicodeRE} |
| 1064 |
, #$85 |
| 1065 |
, #$2028 |
| 1066 |
, #$2029 |
| 1067 |
{$endif}: |
| 1068 |
Result := True; |
| 1069 |
else |
| 1070 |
Result := False; |
| 1071 |
end; |
| 1072 |
end; |
| 1073 |
|
| 1074 |
function _FindCharInBuffer(SBegin, SEnd: PRegExprChar; Ch: REChar): PRegExprChar; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1075 |
begin |
| 1076 |
while SBegin < SEnd do |
| 1077 |
begin |
| 1078 |
if SBegin^ = Ch then |
| 1079 |
begin |
| 1080 |
Result := SBegin; |
| 1081 |
Exit; |
| 1082 |
end; |
| 1083 |
Inc(SBegin); |
| 1084 |
end; |
| 1085 |
Result := nil; |
| 1086 |
end; |
| 1087 |
|
| 1088 |
function IsIgnoredChar(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1089 |
begin |
| 1090 |
case AChar of |
| 1091 |
' ', #9, #$d, #$a: |
| 1092 |
Result := True |
| 1093 |
else |
| 1094 |
Result := False; |
| 1095 |
end; |
| 1096 |
end; |
| 1097 |
|
| 1098 |
function _IsMetaChar(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1099 |
begin |
| 1100 |
case AChar of |
| 1101 |
'd', 'D', |
| 1102 |
's', 'S', |
| 1103 |
'w', 'W', |
| 1104 |
'v', 'V', |
| 1105 |
'h', 'H', |
| 1106 |
'R': |
| 1107 |
Result := True |
| 1108 |
else |
| 1109 |
Result := False; |
| 1110 |
end; |
| 1111 |
end; |
| 1112 |
|
| 1113 |
function AlignToPtr(const p: Pointer): Pointer; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1114 |
begin |
| 1115 |
{$IFDEF FPC_REQUIRES_PROPER_ALIGNMENT} |
| 1116 |
Result := Align(p, SizeOf(Pointer)); |
| 1117 |
{$ELSE} |
| 1118 |
Result := p; |
| 1119 |
{$ENDIF} |
| 1120 |
end; |
| 1121 |
|
| 1122 |
function AlignToInt(const p: Pointer): Pointer; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1123 |
begin |
| 1124 |
{$IFDEF FPC_REQUIRES_PROPER_ALIGNMENT} |
| 1125 |
Result := Align(p, SizeOf(Integer)); |
| 1126 |
{$ELSE} |
| 1127 |
Result := p; |
| 1128 |
{$ENDIF} |
| 1129 |
end; |
| 1130 |
|
| 1131 |
function StrLScan(P: PRegExprChar; C: REChar; len: PtrInt): PRegExprChar; |
| 1132 |
Var |
| 1133 |
count: PtrInt; |
| 1134 |
Begin |
| 1135 |
count := 0; |
| 1136 |
{ Find first matching character of Ch in Str } |
| 1137 |
while (count < len) do |
| 1138 |
begin |
| 1139 |
if C = P[count] then |
| 1140 |
begin |
| 1141 |
StrLScan := @(P[count]); |
| 1142 |
exit; |
| 1143 |
end; |
| 1144 |
Inc(count); |
| 1145 |
end; |
| 1146 |
{ nothing found. } |
| 1147 |
StrLScan := nil; |
| 1148 |
end; |
| 1149 |
|
| 1150 |
function StrLComp(str1,str2 : PRegExprChar; len : PtrInt) : PtrInt; |
| 1151 |
var |
| 1152 |
counter: PtrInt; |
| 1153 |
c1, c2: REChar; |
| 1154 |
begin |
| 1155 |
if len = 0 then |
| 1156 |
begin |
| 1157 |
StrLComp := 0; |
| 1158 |
exit; |
| 1159 |
end; |
| 1160 |
counter:=0; |
| 1161 |
repeat |
| 1162 |
c1:=str1[counter]; |
| 1163 |
c2:=str2[counter]; |
| 1164 |
inc(counter); |
| 1165 |
until (c1<>c2) or (counter>=len) or (c1=#0) or (c2=#0); |
| 1166 |
StrLComp:=ord(c1)-ord(c2); |
| 1167 |
end; |
| 1168 |
|
| 1169 |
function StrLPos(str1,str2 : PRegExprChar; len1, len2: PtrInt) : PRegExprChar; |
| 1170 |
var |
| 1171 |
p : PRegExprChar; |
| 1172 |
begin |
| 1173 |
StrLPos := nil; |
| 1174 |
if (str1 = nil) or (str2 = nil) then |
| 1175 |
exit; |
| 1176 |
len1 := len1 - len2 + 1; |
| 1177 |
p := StrLScan(str1,str2^, len1); |
| 1178 |
while p <> nil do |
| 1179 |
begin |
| 1180 |
if StrLComp(p, str2, len2)=0 then |
| 1181 |
begin |
| 1182 |
StrLPos := p; |
| 1183 |
exit; |
| 1184 |
end; |
| 1185 |
inc(p); |
| 1186 |
p := StrLScan(p, str2^, len1 - (p-str1)); |
| 1187 |
end; |
| 1188 |
end; |
| 1189 |
|
| 1190 |
{$IFDEF FastUnicodeData} |
| 1191 |
function _UpperCase(Ch: REChar): REChar; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1192 |
begin |
| 1193 |
Result := CharUpperArray[Ord(Ch)]; |
| 1194 |
end; |
| 1195 |
|
| 1196 |
function _LowerCase(Ch: REChar): REChar; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1197 |
begin |
| 1198 |
Result := CharLowerArray[Ord(Ch)]; |
| 1199 |
end; |
| 1200 |
|
| 1201 |
{$ELSE} |
| 1202 |
function _UpperCase(Ch: REChar): REChar; |
| 1203 |
begin |
| 1204 |
Result := Ch; |
| 1205 |
if (Ch >= 'a') and (Ch <= 'z') then |
| 1206 |
begin |
| 1207 |
Dec(Result, 32); |
| 1208 |
Exit; |
| 1209 |
end; |
| 1210 |
if Ord(Ch) < 128 then |
| 1211 |
Exit; |
| 1212 |
|
| 1213 |
{$IFDEF FPC} |
| 1214 |
{$IFDEF UnicodeRE} |
| 1215 |
Result := UnicodeUpperCase(Ch)[1]; |
| 1216 |
{$ELSE} |
| 1217 |
Result := AnsiUpperCase(Ch)[1]; |
| 1218 |
{$ENDIF} |
| 1219 |
{$ELSE} |
| 1220 |
{$IFDEF UnicodeRE} |
| 1221 |
{$IFDEF D_XE4} |
| 1222 |
Result := Ch.ToUpper; |
| 1223 |
{$ELSE} |
| 1224 |
{$IFDEF D2009} |
| 1225 |
Result := TCharacter.ToUpper(Ch); |
| 1226 |
{$ENDIF} |
| 1227 |
{$ENDIF} |
| 1228 |
{$ELSE} |
| 1229 |
Result := AnsiUpperCase(Ch)[1]; |
| 1230 |
{$ENDIF} |
| 1231 |
{$ENDIF} |
| 1232 |
end; |
| 1233 |
|
| 1234 |
function _LowerCase(Ch: REChar): REChar; |
| 1235 |
begin |
| 1236 |
Result := Ch; |
| 1237 |
if (Ch >= 'A') and (Ch <= 'Z') then |
| 1238 |
begin |
| 1239 |
Inc(Result, 32); |
| 1240 |
Exit; |
| 1241 |
end; |
| 1242 |
if Ord(Ch) < 128 then |
| 1243 |
Exit; |
| 1244 |
|
| 1245 |
{$IFDEF FPC} |
| 1246 |
{$IFDEF UnicodeRE} |
| 1247 |
Result := UnicodeLowerCase(Ch)[1]; |
| 1248 |
{$ELSE} |
| 1249 |
Result := AnsiLowerCase(Ch)[1]; |
| 1250 |
{$ENDIF} |
| 1251 |
{$ELSE} |
| 1252 |
{$IFDEF UnicodeRE} |
| 1253 |
{$IFDEF D_XE4} |
| 1254 |
Result := Ch.ToLower; |
| 1255 |
{$ELSE} |
| 1256 |
{$IFDEF D2009} |
| 1257 |
Result := TCharacter.ToLower(Ch); |
| 1258 |
{$ENDIF} |
| 1259 |
{$ENDIF} |
| 1260 |
{$ELSE} |
| 1261 |
Result := AnsiLowerCase(Ch)[1]; |
| 1262 |
{$ENDIF} |
| 1263 |
{$ENDIF} |
| 1264 |
end; |
| 1265 |
{$ENDIF} |
| 1266 |
|
| 1267 |
function InvertCase(const Ch: REChar): REChar; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1268 |
begin |
| 1269 |
Result := _UpperCase(Ch); |
| 1270 |
if Result = Ch then |
| 1271 |
Result := _LowerCase(Ch); |
| 1272 |
end; |
| 1273 |
|
| 1274 |
function _FindClosingBracket(P, PEnd: PRegExprChar): PRegExprChar; |
| 1275 |
var |
| 1276 |
Level: Integer; |
| 1277 |
begin |
| 1278 |
Result := nil; |
| 1279 |
Level := 1; |
| 1280 |
repeat |
| 1281 |
if P >= PEnd then Exit; |
| 1282 |
case P^ of |
| 1283 |
EscChar: |
| 1284 |
Inc(P); |
| 1285 |
'(': |
| 1286 |
begin |
| 1287 |
Inc(Level); |
| 1288 |
end; |
| 1289 |
')': |
| 1290 |
begin |
| 1291 |
Dec(Level); |
| 1292 |
if Level = 0 then |
| 1293 |
begin |
| 1294 |
Result := P; |
| 1295 |
Exit; |
| 1296 |
end; |
| 1297 |
end; |
| 1298 |
end; |
| 1299 |
Inc(P); |
| 1300 |
until False; |
| 1301 |
end; |
| 1302 |
|
| 1303 |
{$IFDEF UNICODEEX} |
| 1304 |
procedure IncUnicode(var p: PRegExprChar); {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1305 |
// make additional increment if we are on low-surrogate char |
| 1306 |
// no need to check p<fInputEnd, at the end of string we have chr(0) |
| 1307 |
var |
| 1308 |
ch: REChar; |
| 1309 |
begin |
| 1310 |
Inc(p); |
| 1311 |
ch := p^; |
| 1312 |
if (Ord(ch) >= $DC00) and (Ord(ch) <= $DFFF) then |
| 1313 |
Inc(p); |
| 1314 |
end; |
| 1315 |
|
| 1316 |
procedure IncUnicode2(var p: PRegExprChar; var N: Integer); {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1317 |
var |
| 1318 |
ch: REChar; |
| 1319 |
begin |
| 1320 |
Inc(p); |
| 1321 |
Inc(N); |
| 1322 |
ch := p^; |
| 1323 |
if (Ord(ch) >= $DC00) and (Ord(ch) <= $DFFF) then |
| 1324 |
begin |
| 1325 |
Inc(p); |
| 1326 |
Inc(N); |
| 1327 |
end; |
| 1328 |
end; |
| 1329 |
{$ENDIF} |
| 1330 |
|
| 1331 |
{ ============================================================= } |
| 1332 |
{ ===================== Global functions ====================== } |
| 1333 |
{ ============================================================= } |
| 1334 |
|
| 1335 |
function IsModifiersEqual(const A, B: TRegExprModifiers): Boolean; |
| 1336 |
begin |
| 1337 |
Result := |
| 1338 |
(A.I = B.I) and |
| 1339 |
(A.G = B.G) and |
| 1340 |
(A.M = B.M) and |
| 1341 |
(A.S = B.S) and |
| 1342 |
(A.R = B.R) and |
| 1343 |
(A.X = B.X); |
| 1344 |
end; |
| 1345 |
|
| 1346 |
function ParseModifiers(const APtr: PRegExprChar; |
| 1347 |
ALen: Integer; |
| 1348 |
var AValue: TRegExprModifiers): Boolean; |
| 1349 |
// Parse string and set AValue if it's in format 'ismxrg-ismxrg' |
| 1350 |
var |
| 1351 |
IsOn: Boolean; |
| 1352 |
i: Integer; |
| 1353 |
begin |
| 1354 |
Result := True; |
| 1355 |
IsOn := True; |
| 1356 |
for i := 0 to ALen-1 do |
| 1357 |
case APtr[i] of |
| 1358 |
'-': |
| 1359 |
if IsOn then |
| 1360 |
begin |
| 1361 |
IsOn := False; |
| 1362 |
end |
| 1363 |
else |
| 1364 |
begin |
| 1365 |
Result := False; |
| 1366 |
Exit; |
| 1367 |
end; |
| 1368 |
'I', 'i': |
| 1369 |
AValue.I := IsOn; |
| 1370 |
'R', 'r': |
| 1371 |
AValue.R := IsOn; |
| 1372 |
'S', 's': |
| 1373 |
AValue.S := IsOn; |
| 1374 |
'G', 'g': |
| 1375 |
AValue.G := IsOn; |
| 1376 |
'M', 'm': |
| 1377 |
AValue.M := IsOn; |
| 1378 |
'X', 'x': |
| 1379 |
AValue.X := IsOn; |
| 1380 |
else |
| 1381 |
Result := False; |
| 1382 |
Exit; |
| 1383 |
end; |
| 1384 |
end; |
| 1385 |
|
| 1386 |
function ExecRegExpr(const ARegExpr, AInputStr: RegExprString): Boolean; |
| 1387 |
var |
| 1388 |
r: TRegExpr; |
| 1389 |
begin |
| 1390 |
r := TRegExpr.Create; |
| 1391 |
try |
| 1392 |
r.Expression := ARegExpr; |
| 1393 |
Result := r.Exec(AInputStr); |
| 1394 |
finally |
| 1395 |
r.Free; |
| 1396 |
end; |
| 1397 |
end; { of function ExecRegExpr |
| 1398 |
-------------------------------------------------------------- } |
| 1399 |
|
| 1400 |
procedure SplitRegExpr(const ARegExpr, AInputStr: RegExprString; |
| 1401 |
APieces: TStrings); |
| 1402 |
var |
| 1403 |
r: TRegExpr; |
| 1404 |
begin |
| 1405 |
APieces.Clear; |
| 1406 |
r := TRegExpr.Create; |
| 1407 |
try |
| 1408 |
r.Expression := ARegExpr; |
| 1409 |
r.Split(AInputStr, APieces); |
| 1410 |
finally |
| 1411 |
r.Free; |
| 1412 |
end; |
| 1413 |
end; { of procedure SplitRegExpr |
| 1414 |
-------------------------------------------------------------- } |
| 1415 |
|
| 1416 |
function ReplaceRegExpr(const ARegExpr, AInputStr, AReplaceStr: RegExprString; |
| 1417 |
AUseSubstitution: Boolean{$IFDEF DefParam} = False{$ENDIF}): RegExprString; |
| 1418 |
begin |
| 1419 |
with TRegExpr.Create do |
| 1420 |
try |
| 1421 |
Expression := ARegExpr; |
| 1422 |
Result := Replace(AInputStr, AReplaceStr, AUseSubstitution); |
| 1423 |
finally |
| 1424 |
Free; |
| 1425 |
end; |
| 1426 |
end; { of function ReplaceRegExpr |
| 1427 |
-------------------------------------------------------------- } |
| 1428 |
{$IFDEF OverMeth} |
| 1429 |
|
| 1430 |
function ReplaceRegExpr(const ARegExpr, AInputStr, AReplaceStr: RegExprString; |
| 1431 |
Options: TRegexReplaceOptions): RegExprString; overload; |
| 1432 |
|
| 1433 |
begin |
| 1434 |
with TRegExpr.Create do |
| 1435 |
try |
| 1436 |
ModifierI := (rroModifierI in Options); |
| 1437 |
ModifierR := (rroModifierR in Options); |
| 1438 |
ModifierS := (rroModifierS in Options); |
| 1439 |
ModifierG := (rroModifierG in Options); |
| 1440 |
ModifierM := (rroModifierM in Options); |
| 1441 |
ModifierX := (rroModifierX in Options); |
| 1442 |
// Set this after the above, if the regex contains modifiers, they will be applied. |
| 1443 |
Expression := ARegExpr; |
| 1444 |
if rroUseOsLineEnd in Options then |
| 1445 |
ReplaceLineEnd := sLineBreak |
| 1446 |
else |
| 1447 |
ReplaceLineEnd := #10; |
| 1448 |
Result := Replace(AInputStr, AReplaceStr, rroUseSubstitution in Options); |
| 1449 |
finally |
| 1450 |
Free; |
| 1451 |
end; |
| 1452 |
end; |
| 1453 |
{$ENDIF} |
| 1454 |
|
| 1455 |
(* |
| 1456 |
const |
| 1457 |
MetaChars_Init = '^$.[()|?+*' + EscChar + '{'; |
| 1458 |
MetaChars = MetaChars_Init; // not needed to be a variable, const is faster |
| 1459 |
MetaAll = MetaChars_Init + ']}'; // Very similar to MetaChars, but slighly changed. |
| 1460 |
*) |
| 1461 |
|
| 1462 |
function _IsMetaSymbol1(ch: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1463 |
begin |
| 1464 |
case ch of |
| 1465 |
'^', '$', '.', '[', '(', ')', '|', '?', '+', '*', EscChar, '{': |
| 1466 |
Result := True |
| 1467 |
else |
| 1468 |
Result := False |
| 1469 |
end; |
| 1470 |
end; |
| 1471 |
|
| 1472 |
function _IsMetaSymbol2(ch: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 1473 |
begin |
| 1474 |
case ch of |
| 1475 |
'^', '$', '.', '[', '(', ')', '|', '?', '+', '*', EscChar, '{', |
| 1476 |
']', '}': |
| 1477 |
Result := True |
| 1478 |
else |
| 1479 |
Result := False |
| 1480 |
end; |
| 1481 |
end; |
| 1482 |
|
| 1483 |
function QuoteRegExprMetaChars(const AStr: RegExprString): RegExprString; |
| 1484 |
var |
| 1485 |
i, i0, Len: Integer; |
| 1486 |
ch: REChar; |
| 1487 |
begin |
| 1488 |
Result := ''; |
| 1489 |
Len := Length(AStr); |
| 1490 |
i := 1; |
| 1491 |
i0 := i; |
| 1492 |
while i <= Len do |
| 1493 |
begin |
| 1494 |
ch := AStr[i]; |
| 1495 |
if _IsMetaSymbol2(ch) then |
| 1496 |
begin |
| 1497 |
Result := Result + System.Copy(AStr, i0, i - i0) + EscChar + ch; |
| 1498 |
i0 := i + 1; |
| 1499 |
end; |
| 1500 |
Inc(i); |
| 1501 |
end; |
| 1502 |
Result := Result + System.Copy(AStr, i0, MaxInt); // Tail |
| 1503 |
end; { of function QuoteRegExprMetaChars |
| 1504 |
-------------------------------------------------------------- } |
| 1505 |
|
| 1506 |
function RegExprSubExpressions(const ARegExpr: RegExprString; ASubExprs: TStrings; |
| 1507 |
AExtendedSyntax: Boolean{$IFDEF DefParam} = False{$ENDIF}): Integer; |
| 1508 |
type |
| 1509 |
TStackItemRec = record |
| 1510 |
SubExprIdx: Integer; |
| 1511 |
StartPos: PtrInt; |
| 1512 |
end; |
| 1513 |
|
| 1514 |
TStackArray = packed array [0 .. RegexMaxMaxGroups - 1] of TStackItemRec; |
| 1515 |
var |
| 1516 |
Len, SubExprLen: Integer; |
| 1517 |
i, i0: Integer; |
| 1518 |
Modif: TRegExprModifiers; |
| 1519 |
Stack: ^TStackArray; |
| 1520 |
StackIdx, StackSz: Integer; |
| 1521 |
begin |
| 1522 |
Result := 0; // no unbalanced brackets found at this very moment |
| 1523 |
FillChar(Modif, SizeOf(Modif), 0); |
| 1524 |
ASubExprs.Clear; // I don't think that adding to non empty list |
| 1525 |
// can be useful, so I simplified algorithm to work only with empty list |
| 1526 |
|
| 1527 |
Len := Length(ARegExpr); // some optimization tricks |
| 1528 |
|
| 1529 |
// first we have to calculate number of subexpression to reserve |
| 1530 |
// space in Stack array (may be we'll reserve more than needed, but |
| 1531 |
// it's faster then memory reallocation during parsing) |
| 1532 |
StackSz := 1; // add 1 for entire r.e. |
| 1533 |
for i := 1 to Len do |
| 1534 |
if ARegExpr[i] = '(' then |
| 1535 |
Inc(StackSz); |
| 1536 |
// SetLength (Stack, StackSz); |
| 1537 |
GetMem(Stack, SizeOf(TStackItemRec) * StackSz); |
| 1538 |
|
| 1539 |
try |
| 1540 |
StackIdx := 0; |
| 1541 |
i := 1; |
| 1542 |
while (i <= Len) do |
| 1543 |
begin |
| 1544 |
case ARegExpr[i] of |
| 1545 |
'(': |
| 1546 |
begin |
| 1547 |
if (i < Len) and (ARegExpr[i + 1] = '?') then |
| 1548 |
begin |
| 1549 |
// this is not subexpression, but comment or other |
| 1550 |
// Perl extension. We must check is it (?ismxrg-ismxrg) |
| 1551 |
// and change AExtendedSyntax if /x is changed. |
| 1552 |
Inc(i, 2); // skip '(?' |
| 1553 |
i0 := i; |
| 1554 |
while (i <= Len) and (ARegExpr[i] <> ')') do |
| 1555 |
Inc(i); |
| 1556 |
if i > Len then |
| 1557 |
Result := -1 // unbalansed '(' |
| 1558 |
else |
| 1559 |
if ParseModifiers(@ARegExpr[i0], i - i0, Modif) then |
| 1560 |
// Alexey-T: original code had copy from i, not from i0 |
| 1561 |
AExtendedSyntax := Modif.X; |
| 1562 |
end |
| 1563 |
else |
| 1564 |
begin // subexpression starts |
| 1565 |
ASubExprs.Add(''); // just reserve space |
| 1566 |
with Stack[StackIdx] do |
| 1567 |
begin |
| 1568 |
SubExprIdx := ASubExprs.Count - 1; |
| 1569 |
StartPos := i; |
| 1570 |
end; |
| 1571 |
Inc(StackIdx); |
| 1572 |
end; |
| 1573 |
end; |
| 1574 |
')': |
| 1575 |
begin |
| 1576 |
if StackIdx = 0 then |
| 1577 |
Result := i // unbalanced ')' |
| 1578 |
else |
| 1579 |
begin |
| 1580 |
Dec(StackIdx); |
| 1581 |
with Stack[StackIdx] do |
| 1582 |
begin |
| 1583 |
SubExprLen := i - StartPos + 1; |
| 1584 |
ASubExprs.Objects[SubExprIdx] := |
| 1585 |
TObject(StartPos or (SubExprLen ShL 16)); |
| 1586 |
ASubExprs[SubExprIdx] := System.Copy(ARegExpr, StartPos + 1, |
| 1587 |
SubExprLen - 2); // add without brackets |
| 1588 |
end; |
| 1589 |
end; |
| 1590 |
end; |
| 1591 |
EscChar: |
| 1592 |
Inc(i); // skip quoted symbol |
| 1593 |
'[': |
| 1594 |
begin |
| 1595 |
// we have to skip character ranges at once, because they can |
| 1596 |
// contain '#', and '#' in it must NOT be recognized as eXtended |
| 1597 |
// comment beginning! |
| 1598 |
i0 := i; |
| 1599 |
Inc(i); |
| 1600 |
if ARegExpr[i] = ']' // first ']' inside [] treated as simple char, no need to check '[' |
| 1601 |
then |
| 1602 |
Inc(i); |
| 1603 |
while (i <= Len) and (ARegExpr[i] <> ']') do |
| 1604 |
if ARegExpr[i] = EscChar |
| 1605 |
then |
| 1606 |
Inc(i, 2) // skip 'escaped' char to prevent stopping at '\]' |
| 1607 |
else |
| 1608 |
Inc(i); |
| 1609 |
if (i > Len) or (ARegExpr[i] <> ']') |
| 1610 |
then |
| 1611 |
Result := -(i0 + 1); // unbalanced '[' |
| 1612 |
end; |
| 1613 |
'#': |
| 1614 |
if AExtendedSyntax then |
| 1615 |
begin |
| 1616 |
// skip eXtended comments |
| 1617 |
while (i <= Len) and (ARegExpr[i] <> #$d) and (ARegExpr[i] <> #$a) |
| 1618 |
// do not use [#$d, #$a] due to Unicode compatibility |
| 1619 |
do |
| 1620 |
Inc(i); |
| 1621 |
while (i + 1 <= Len) and |
| 1622 |
((ARegExpr[i + 1] = #$d) or (ARegExpr[i + 1] = #$a)) do |
| 1623 |
Inc(i); // attempt to work with different kinds of line separators |
| 1624 |
// now we are at the line separator that must be skipped. |
| 1625 |
end; |
| 1626 |
// here is no 'else' clause - we simply skip ordinary chars |
| 1627 |
end; // of case |
| 1628 |
Inc(i); // skip scanned char |
| 1629 |
// ! can move after Len due to skipping quoted symbol |
| 1630 |
end; |
| 1631 |
|
| 1632 |
// check brackets balance |
| 1633 |
if StackIdx <> 0 then |
| 1634 |
Result := -1; // unbalansed '(' |
| 1635 |
|
| 1636 |
// check if entire r.e. added |
| 1637 |
if (ASubExprs.Count = 0) or ((PtrInt(ASubExprs.Objects[0]) and $FFFF) <> 1) |
| 1638 |
or (((PtrInt(ASubExprs.Objects[0]) ShR 16) and $FFFF) <> Len) |
| 1639 |
// whole r.e. wasn't added because it isn't bracketed |
| 1640 |
// well, we add it now: |
| 1641 |
then |
| 1642 |
ASubExprs.InsertObject(0, ARegExpr, TObject((Len ShL 16) or 1)); |
| 1643 |
|
| 1644 |
finally |
| 1645 |
FreeMem(Stack); |
| 1646 |
end; |
| 1647 |
end; { of function RegExprSubExpressions |
| 1648 |
-------------------------------------------------------------- } |
| 1649 |
|
| 1650 |
const |
| 1651 |
OP_MAGIC = TREOp(216); // programm signature |
| 1652 |
|
| 1653 |
OP_EEND = TREOp(0); // End of program |
| 1654 |
OP_BOL = TREOp(1); // Empty match at beginning of line |
| 1655 |
OP_EOL = TREOp(2); // Empty match at end of line |
| 1656 |
OP_ANY = TREOp(3); // Match any one character |
| 1657 |
OP_ANYOF = TREOp(4); // Match any character in string |
| 1658 |
OP_ANYBUT = TREOp(5); // Match any character not in string |
| 1659 |
OP_BRANCH = TREOp(6); // Match this alternative, or the next |
| 1660 |
OP_BACK = TREOp(7); // Jump backward (Next < 0) |
| 1661 |
OP_EXACTLY = TREOp(8); // Match string exactly |
| 1662 |
OP_NOTHING = TREOp(9); // Match empty string |
| 1663 |
OP_STAR = TREOp(10); // Match this (simple) thing 0 or more times |
| 1664 |
OP_PLUS = TREOp(11); // Match this (simple) thing 1 or more times |
| 1665 |
OP_ANYDIGIT = TREOp(12); // Match any digit (equiv [0-9]) |
| 1666 |
OP_NOTDIGIT = TREOp(13); // Match not digit (equiv [0-9]) |
| 1667 |
OP_ANYLETTER = TREOp(14); // Match any 'word' char |
| 1668 |
OP_NOTLETTER = TREOp(15); // Match any 'non-word' char |
| 1669 |
OP_ANYSPACE = TREOp(16); // Match any 'space' char |
| 1670 |
OP_NOTSPACE = TREOp(17); // Match 'not space' char |
| 1671 |
OP_BRACES = TREOp(18); |
| 1672 |
// Node,Min,Max Match this (simple) thing from Min to Max times. |
| 1673 |
// Min and Max are TREBracesArg |
| 1674 |
OP_COMMENT = TREOp(19); // Comment |
| 1675 |
OP_EXACTLY_CI = TREOp(20); // Match string, case insensitive |
| 1676 |
OP_ANYOF_CI = TREOp(21); // Match any character in string, case insensitive |
| 1677 |
OP_ANYBUT_CI = TREOp(22); // Match any char not in string, case insensitive |
| 1678 |
OP_LOOPENTRY = TREOp(23); // Start of loop (Node - LOOP for this loop) |
| 1679 |
OP_LOOP = TREOp(24); // Back jump for LOOPENTRY |
| 1680 |
// Min and Max are TREBracesArg |
| 1681 |
// Node - next node in sequence, |
| 1682 |
// LoopEntryJmp - associated LOOPENTRY node addr |
| 1683 |
OP_EOL2 = TReOp(25); // like OP_EOL, but also matches before final line-break |
| 1684 |
OP_CONTINUE_POS = TReOp(26); // \G, where offset is from last match end or from Exec(AOffset) |
| 1685 |
OP_ANYLINEBREAK = TReOp(27); // \R |
| 1686 |
OP_BSUBEXP = TREOp(28); // Match previously matched subexpression #Idx (stored as REChar) |
| 1687 |
OP_BSUBEXP_CI = TREOp(29); // -"- in case-insensitive mode |
| 1688 |
|
| 1689 |
// Non-greedy ops |
| 1690 |
OP_STAR_NG = TREOp(30); // Same as OP_START but in non-greedy mode |
| 1691 |
OP_PLUS_NG = TREOp(31); // Same as OP_PLUS but in non-greedy mode |
| 1692 |
OP_BRACES_NG = TREOp(32); // Same as OP_BRACES but in non-greedy mode |
| 1693 |
OP_LOOP_NG = TREOp(33); // Same as OP_LOOP but in non-greedy mode |
| 1694 |
|
| 1695 |
// Multiline mode \m |
| 1696 |
OP_BOL_ML = TREOp(34); // Match "" at beginning of line |
| 1697 |
OP_EOL_ML = TREOp(35); // Match "" at end of line |
| 1698 |
OP_ANY_ML = TREOp(36); // Match any one character |
| 1699 |
|
| 1700 |
// Word boundary |
| 1701 |
OP_BOUND = TREOp(37); // Match "" between word char and non-word char |
| 1702 |
OP_NOTBOUND = TREOp(38); // Opposite to OP_BOUND |
| 1703 |
|
| 1704 |
OP_ANYHORZSEP = TREOp(39); // Any horizontal whitespace \h |
| 1705 |
OP_NOTHORZSEP = TREOp(40); // Not horizontal whitespace \H |
| 1706 |
OP_ANYVERTSEP = TREOp(41); // Any vertical whitespace \v |
| 1707 |
OP_NOTVERTSEP = TREOp(42); // Not vertical whitespace \V |
| 1708 |
|
| 1709 |
OP_ANYCATEGORY = TREOp(43); // \p{L} |
| 1710 |
OP_NOTCATEGORY = TREOp(44); // \P{L} |
| 1711 |
|
| 1712 |
// Possessive quantifiers |
| 1713 |
OP_STAR_POSS = TReOp(45); |
| 1714 |
OP_PLUS_POSS = TReOp(46); |
| 1715 |
OP_BRACES_POSS = TReOp(47); |
| 1716 |
|
| 1717 |
OP_RECUR = TReOp(48); |
| 1718 |
|
| 1719 |
OP_OPEN = TREOp(50); // Opening of group |
| 1720 |
OP_CLOSE = TREOp(51); // Closing of group |
| 1721 |
OP_OPEN_ATOMIC = TREOp(52); // Opening of group |
| 1722 |
OP_CLOSE_ATOMIC = TREOp(53); // Closing of group |
| 1723 |
|
| 1724 |
OP_LOOKAHEAD = TREOp(55); |
| 1725 |
OP_LOOKAHEAD_NEG = TREOp(56); |
| 1726 |
OP_LOOKAHEAD_END = TREOp(57); |
| 1727 |
OP_LOOKBEHIND = TREOp(58); |
| 1728 |
OP_LOOKBEHIND_NEG = TREOp(59); |
| 1729 |
OP_LOOKBEHIND_END = TREOp(60); |
| 1730 |
|
| 1731 |
OP_SUBCALL = TREOp(65); // Call of subroutine; OP_SUBCALL+i is for group i |
| 1732 |
OP_LOOP_POSS = TREOp(66); // Same as OP_LOOP but in non-greedy mode |
| 1733 |
|
| 1734 |
// Guarded branch |
| 1735 |
// If a branch is know to begin with a specific letter (starts with OP_EXACTLY[_CI]) |
| 1736 |
// then that letter can be tested before recursively calling MatchPrim. (guarded from non-match entering) |
| 1737 |
OP_GBRANCH = TREOp(67); |
| 1738 |
OP_GBRANCH_EX = TREOp(68); |
| 1739 |
OP_GBRANCH_EX_CI = TREOp(69); |
| 1740 |
|
| 1741 |
OP_RESET_MATCHPOS = TReOp(70); |
| 1742 |
|
| 1743 |
OP_NONE = High(TREOp); |
| 1744 |
|
| 1745 |
// We work with p-code through pointers, compatible with PRegExprChar. |
| 1746 |
// Note: all code components (TRENextOff, TREOp, TREBracesArg, etc) |
| 1747 |
// must have lengths that can be divided by SizeOf (REChar) ! |
| 1748 |
// A node is TREOp of opcode followed Next "pointer" of TRENextOff type. |
| 1749 |
// The Next is a offset from the opcode of the node containing it. |
| 1750 |
// An operand, if any, simply follows the node. (Note that much of |
| 1751 |
// the code generation knows about this implicit relationship!) |
| 1752 |
// Using TRENextOff=PtrInt speed up p-code processing. |
| 1753 |
|
| 1754 |
// Opcodes description: |
| 1755 |
// |
| 1756 |
// BRANCH The set of branches constituting a single choice are hooked |
| 1757 |
// together with their "next" pointers, since precedence prevents |
| 1758 |
// anything being concatenated to any individual branch. The |
| 1759 |
// "next" pointer of the last BRANCH in a choice points to the |
| 1760 |
// thing following the whole choice. This is also where the |
| 1761 |
// final "next" pointer of each individual branch points; each |
| 1762 |
// branch starts with the operand node of a BRANCH node. |
| 1763 |
// BACK Normal "next" pointers all implicitly point forward; BACK |
| 1764 |
// exists to make loop structures possible. |
| 1765 |
// STAR,PLUS,BRACES '?', and complex '*' and '+', are implemented as |
| 1766 |
// circular BRANCH structures using BACK. Complex '{min,max}' |
| 1767 |
// - as pair LOOPENTRY-LOOP (see below). Simple cases (one |
| 1768 |
// character per match) are implemented with STAR, PLUS and |
| 1769 |
// BRACES for speed and to minimize recursive plunges. |
| 1770 |
// LOOPENTRY,LOOP {min,max} are implemented as special pair |
| 1771 |
// LOOPENTRY-LOOP. Each LOOPENTRY initialize loopstack for |
| 1772 |
// current level. |
| 1773 |
// OPEN,CLOSE are numbered at compile time. |
| 1774 |
|
| 1775 |
{ ============================================================= } |
| 1776 |
{ ================== Error handling section =================== } |
| 1777 |
{ ============================================================= } |
| 1778 |
|
| 1779 |
const |
| 1780 |
reeOk = 0; |
| 1781 |
reeCompNullArgument = 100; |
| 1782 |
reeUnknownMetaSymbol = 101; |
| 1783 |
reeCompParseRegTooManyBrackets = 102; |
| 1784 |
reeCompParseRegUnmatchedBrackets = 103; |
| 1785 |
reeCompParseRegUnmatchedBrackets2 = 104; |
| 1786 |
reeCompParseRegJunkOnEnd = 105; |
| 1787 |
reeNotQuantifiable = 106; |
| 1788 |
reeNestedQuantif = 107; |
| 1789 |
reeBadHexDigit = 108; |
| 1790 |
reeInvalidRange = 109; |
| 1791 |
reeParseAtomTrailingBackSlash = 110; |
| 1792 |
reeNoHexCodeAfterBSlashX = 111; |
| 1793 |
reeHexCodeAfterBSlashXTooBig = 112; |
| 1794 |
reeUnmatchedSqBrackets = 113; |
| 1795 |
reeInternalUrp = 114; |
| 1796 |
reeQuantifFollowsNothing = 115; |
| 1797 |
reeTrailingBackSlash = 116; |
| 1798 |
reeNoLetterAfterBSlashC = 117; |
| 1799 |
reeMetaCharAfterMinusInRange = 118; |
| 1800 |
reeRarseAtomInternalDisaster = 119; |
| 1801 |
reeIncorrectSpecialBrackets = 120; |
| 1802 |
reeIncorrectBraces = 121; |
| 1803 |
reeBRACESArgTooBig = 122; |
| 1804 |
reeUnknownOpcodeInFillFirst = 123; |
| 1805 |
reeBracesMinParamGreaterMax = 124; |
| 1806 |
reeUnclosedComment = 125; |
| 1807 |
reeComplexBracesNotImplemented = 126; |
| 1808 |
reeUnrecognizedModifier = 127; |
| 1809 |
reeBadLinePairedSeparator = 128; |
| 1810 |
reeBadUnicodeCategory = 129; |
| 1811 |
reeTooSmallCheckersArray = 130; |
| 1812 |
reeBadRecursion = 132; |
| 1813 |
reeBadSubCall = 133; |
| 1814 |
reeNamedGroupBad = 140; |
| 1815 |
reeNamedGroupBadName = 141; |
| 1816 |
reeNamedGroupBadRef = 142; |
| 1817 |
reeNamedGroupDupName = 143; |
| 1818 |
reeLookaheadBad = 150; |
| 1819 |
reeLookbehindBad = 152; |
| 1820 |
reeLookaroundNotSafe = 153; |
| 1821 |
reeBadReference = 154; |
| 1822 |
// Runtime errors must be >= reeFirstRuntimeCode |
| 1823 |
reeFirstRuntimeCode = 1000; |
| 1824 |
reeRegRepeatCalledInappropriately = 1000; |
| 1825 |
reeMatchPrimMemoryCorruption = 1001; |
| 1826 |
reeNoExpression = 1003; |
| 1827 |
reeCorruptedProgram = 1004; |
| 1828 |
reeOffsetMustBePositive = 1006; |
| 1829 |
reeExecNextWithoutExec = 1007; |
| 1830 |
reeBadOpcodeInCharClass = 1008; |
| 1831 |
reeDumpCorruptedOpcode = 1011; |
| 1832 |
reeLoopStackExceeded = 1014; |
| 1833 |
reeLoopWithoutEntry = 1015; |
| 1834 |
reeUnknown = 1016; |
| 1835 |
|
| 1836 |
function TRegExpr.ErrorMsg(AErrorID: Integer): RegExprString; |
| 1837 |
begin |
| 1838 |
case AErrorID of |
| 1839 |
reeOk: |
| 1840 |
Result := 'No errors'; |
| 1841 |
reeCompNullArgument: |
| 1842 |
Result := 'TRegExpr compile: null argument'; |
| 1843 |
reeUnknownMetaSymbol: |
| 1844 |
Result := 'TRegExpr compile: unknown meta-character: \' + fLastErrorSymbol; |
| 1845 |
reeCompParseRegTooManyBrackets: |
| 1846 |
Result := 'TRegExpr compile: ParseReg: too many ()'; |
| 1847 |
reeCompParseRegUnmatchedBrackets: |
| 1848 |
Result := 'TRegExpr compile: ParseReg: unmatched ()'; |
| 1849 |
reeCompParseRegUnmatchedBrackets2: |
| 1850 |
Result := 'TRegExpr compile: ParseReg: unmatched ()'; |
| 1851 |
reeCompParseRegJunkOnEnd: |
| 1852 |
Result := 'TRegExpr compile: ParseReg: junk at end'; |
| 1853 |
reeNotQuantifiable: |
| 1854 |
Result := 'TRegExpr compile: Token before *+ operand is not quantifiable'; |
| 1855 |
reeNestedQuantif: |
| 1856 |
Result := 'TRegExpr compile: nested quantifier *?+'; |
| 1857 |
reeBadHexDigit: |
| 1858 |
Result := 'TRegExpr compile: bad hex digit'; |
| 1859 |
reeInvalidRange: |
| 1860 |
Result := 'TRegExpr compile: invalid [] range'; |
| 1861 |
reeParseAtomTrailingBackSlash: |
| 1862 |
Result := 'TRegExpr compile: parse atom trailing \'; |
| 1863 |
reeNoHexCodeAfterBSlashX: |
| 1864 |
Result := 'TRegExpr compile: no hex code after \x'; |
| 1865 |
reeNoLetterAfterBSlashC: |
| 1866 |
Result := 'TRegExpr compile: no letter "A".."Z" after \c'; |
| 1867 |
reeMetaCharAfterMinusInRange: |
| 1868 |
Result := 'TRegExpr compile: metachar after "-" in [] range'; |
| 1869 |
reeHexCodeAfterBSlashXTooBig: |
| 1870 |
Result := 'TRegExpr compile: hex code after \x is too big'; |
| 1871 |
reeUnmatchedSqBrackets: |
| 1872 |
Result := 'TRegExpr compile: unmatched []'; |
| 1873 |
reeInternalUrp: |
| 1874 |
Result := 'TRegExpr compile: internal fail on char "|", ")"'; |
| 1875 |
reeQuantifFollowsNothing: |
| 1876 |
Result := 'TRegExpr compile: quantifier ?+*{ follows nothing'; |
| 1877 |
reeTrailingBackSlash: |
| 1878 |
Result := 'TRegExpr compile: trailing \'; |
| 1879 |
reeRarseAtomInternalDisaster: |
| 1880 |
Result := 'TRegExpr compile: RarseAtom internal disaster'; |
| 1881 |
reeIncorrectSpecialBrackets: |
| 1882 |
Result := 'TRegExpr compile: incorrect expression in (?...) brackets'; |
| 1883 |
reeIncorrectBraces: |
| 1884 |
Result := 'TRegExpr compile: incorrect {} braces'; |
| 1885 |
reeBRACESArgTooBig: |
| 1886 |
Result := 'TRegExpr compile: braces {} argument too big'; |
| 1887 |
reeUnknownOpcodeInFillFirst: |
| 1888 |
Result := 'TRegExpr compile: unknown opcode in FillFirstCharSet ('+DumpOp(fLastErrorOpcode)+')'; |
| 1889 |
reeBracesMinParamGreaterMax: |
| 1890 |
Result := 'TRegExpr compile: braces {} min param greater then max'; |
| 1891 |
reeUnclosedComment: |
| 1892 |
Result := 'TRegExpr compile: unclosed (?#comment)'; |
| 1893 |
reeComplexBracesNotImplemented: |
| 1894 |
Result := 'TRegExpr compile: if you use braces {} and non-greedy ops *?, +?, ?? for complex cases, enable {$DEFINE ComplexBraces}'; |
| 1895 |
reeUnrecognizedModifier: |
| 1896 |
Result := 'TRegExpr compile: incorrect modifier'; |
| 1897 |
reeBadLinePairedSeparator: |
| 1898 |
Result := 'TRegExpr compile: LinePairedSeparator must countain two different chars or be empty'; |
| 1899 |
reeBadUnicodeCategory: |
| 1900 |
Result := 'TRegExpr compile: invalid category after \p or \P'; |
| 1901 |
reeTooSmallCheckersArray: |
| 1902 |
Result := 'TRegExpr compile: too small CharCheckers array'; |
| 1903 |
reeBadRecursion: |
| 1904 |
Result := 'TRegExpr compile: bad recursion (?R)'; |
| 1905 |
reeBadSubCall: |
| 1906 |
Result := 'TRegExpr compile: bad subroutine call'; |
| 1907 |
reeNamedGroupBad: |
| 1908 |
Result := 'TRegExpr compile: bad named group'; |
| 1909 |
reeNamedGroupBadName: |
| 1910 |
Result := 'TRegExpr compile: bad identifier in named group'; |
| 1911 |
reeNamedGroupBadRef: |
| 1912 |
Result := 'TRegExpr compile: bad back-reference to named group'; |
| 1913 |
reeNamedGroupDupName: |
| 1914 |
Result := 'TRegExpr compile: named group defined more than once'; |
| 1915 |
reeLookaheadBad: |
| 1916 |
Result := 'TRegExpr compile: bad lookahead'; |
| 1917 |
reeLookbehindBad: |
| 1918 |
Result := 'TRegExpr compile: bad lookbehind'; |
| 1919 |
reeLookaroundNotSafe: |
| 1920 |
Result := 'TRegExpr compile: lookbehind brackets with variable length do not support captures'; |
| 1921 |
reeBadReference: |
| 1922 |
Result := 'TRegExpr compile: invalid syntax for reference to capture group'; |
| 1923 |
|
| 1924 |
reeRegRepeatCalledInappropriately: |
| 1925 |
Result := 'TRegExpr exec: RegRepeat called inappropriately'; |
| 1926 |
reeMatchPrimMemoryCorruption: |
| 1927 |
Result := 'TRegExpr exec: MatchPrim memory corruption'; |
| 1928 |
reeNoExpression: |
| 1929 |
Result := 'TRegExpr exec: empty expression'; |
| 1930 |
reeCorruptedProgram: |
| 1931 |
Result := 'TRegExpr exec: corrupted opcode (no magic byte)'; |
| 1932 |
reeOffsetMustBePositive: |
| 1933 |
Result := 'TRegExpr exec: offset must be >0'; |
| 1934 |
reeExecNextWithoutExec: |
| 1935 |
Result := 'TRegExpr exec: ExecNext without Exec(Pos)'; |
| 1936 |
reeBadOpcodeInCharClass: |
| 1937 |
Result := 'TRegExpr exec: invalid opcode in char class'; |
| 1938 |
reeDumpCorruptedOpcode: |
| 1939 |
Result := 'TRegExpr dump: corrupted opcode'; |
| 1940 |
reeLoopStackExceeded: |
| 1941 |
Result := 'TRegExpr exec: loop stack exceeded'; |
| 1942 |
reeLoopWithoutEntry: |
| 1943 |
Result := 'TRegExpr exec: loop without loop entry'; |
| 1944 |
reeUnknown: |
| 1945 |
Result := 'TRegExpr exec: unknow error'; |
| 1946 |
else |
| 1947 |
Result := 'Unknown error'; |
| 1948 |
end; |
| 1949 |
end; { of procedure TRegExpr.Error |
| 1950 |
-------------------------------------------------------------- } |
| 1951 |
|
| 1952 |
function TRegExpr.LastError: Integer; |
| 1953 |
begin |
| 1954 |
Result := fLastError; |
| 1955 |
fLastError := reeOk; |
| 1956 |
end; { of function TRegExpr.LastError |
| 1957 |
-------------------------------------------------------------- } |
| 1958 |
|
| 1959 |
{ ============================================================= } |
| 1960 |
{ ===================== Common section ======================== } |
| 1961 |
{ ============================================================= } |
| 1962 |
|
| 1963 |
class function TRegExpr.VersionMajor: Integer; |
| 1964 |
begin |
| 1965 |
Result := REVersionMajor; |
| 1966 |
end; |
| 1967 |
|
| 1968 |
class function TRegExpr.VersionMinor: Integer; |
| 1969 |
begin |
| 1970 |
Result := REVersionMinor; |
| 1971 |
end; |
| 1972 |
|
| 1973 |
constructor TRegExpr.Create; |
| 1974 |
begin |
| 1975 |
inherited; |
| 1976 |
programm := nil; |
| 1977 |
fExpression := ''; |
| 1978 |
fInputString := ''; |
| 1979 |
|
| 1980 |
FillChar(fModifiers, SizeOf(fModifiers), 0); |
| 1981 |
fModifiers.I := RegExprModifierI; |
| 1982 |
fModifiers.R := RegExprModifierR; |
| 1983 |
fModifiers.S := RegExprModifierS; |
| 1984 |
fModifiers.G := RegExprModifierG; |
| 1985 |
fModifiers.M := RegExprModifierM; |
| 1986 |
fModifiers.X := RegExprModifierX; |
| 1987 |
|
| 1988 |
{$IFDEF UseSpaceChars} |
| 1989 |
SpaceChars := RegExprSpaceChars; |
| 1990 |
{$ENDIF} |
| 1991 |
{$IFDEF UseWordChars} |
| 1992 |
WordChars := RegExprWordChars; |
| 1993 |
{$ENDIF} |
| 1994 |
|
| 1995 |
{$IFDEF UseLineSep} |
| 1996 |
fLineSeparators := RegExprLineSeparators; |
| 1997 |
{$ENDIF} |
| 1998 |
|
| 1999 |
fUsePairedBreak := RegExprUsePairedBreak; |
| 2000 |
fReplaceLineEnd := RegExprReplaceLineBreak; |
| 2001 |
|
| 2002 |
fSlowChecksSizeMax := 2000; |
| 2003 |
FAllowUnsafeLookBehind := False; |
| 2004 |
fRaiseForRuntimeError := True; |
| 2005 |
|
| 2006 |
{$IFDEF UseLineSep} |
| 2007 |
InitLineSepArray; |
| 2008 |
{$ENDIF} |
| 2009 |
|
| 2010 |
InitCharCheckers; |
| 2011 |
|
| 2012 |
{$IFDEF Compat} |
| 2013 |
fInvertCase := OldInvertCase; |
| 2014 |
{$ENDIF} |
| 2015 |
end; { of constructor TRegExpr.Create |
| 2016 |
-------------------------------------------------------------- } |
| 2017 |
|
| 2018 |
{ TRegExprGroupNameList } |
| 2019 |
|
| 2020 |
function TRegExprGroupNameList.MatchIndexFromName(const AName: RegExprString |
| 2021 |
): Integer; |
| 2022 |
var |
| 2023 |
i: Integer; |
| 2024 |
begin |
| 2025 |
for i := 0 to NameCount - 1 do |
| 2026 |
if Names[i].Name = AName then |
| 2027 |
begin |
| 2028 |
Result := Names[i].Index; |
| 2029 |
Exit; |
| 2030 |
end; |
| 2031 |
Result := -1; |
| 2032 |
end; |
| 2033 |
|
| 2034 |
procedure TRegExprGroupNameList.Clear; |
| 2035 |
begin |
| 2036 |
NameCount := 0; |
| 2037 |
if Length(Names) > RegexGroupCountIncrement then |
| 2038 |
SetLength(Names, RegexGroupCountIncrement); |
| 2039 |
end; |
| 2040 |
|
| 2041 |
procedure TRegExprGroupNameList.Add(const AName: RegExprString; AnIndex: Integer |
| 2042 |
); |
| 2043 |
begin |
| 2044 |
if NameCount >= Length(Names) then |
| 2045 |
SetLength(Names, Length(Names) + 1 + RegexGroupCountIncrement); |
| 2046 |
Names[NameCount].Name := AName; |
| 2047 |
Names[NameCount].Index := AnIndex; |
| 2048 |
inc(NameCount); |
| 2049 |
end; |
| 2050 |
|
| 2051 |
{$IFDEF OverMeth} |
| 2052 |
constructor TRegExpr.Create(const AExpression: RegExprString); |
| 2053 |
begin |
| 2054 |
Create; |
| 2055 |
Expression := AExpression; |
| 2056 |
end; |
| 2057 |
{$ENDIF} |
| 2058 |
|
| 2059 |
destructor TRegExpr.Destroy; |
| 2060 |
begin |
| 2061 |
if programm <> nil then |
| 2062 |
begin |
| 2063 |
FreeMem(programm); |
| 2064 |
programm := nil; |
| 2065 |
end; |
| 2066 |
end; |
| 2067 |
|
| 2068 |
procedure TRegExpr.SetExpression(const AStr: RegExprString); |
| 2069 |
begin |
| 2070 |
if (AStr <> fExpression) or not IsCompiled then |
| 2071 |
begin |
| 2072 |
fExpression := AStr; |
| 2073 |
//UniqueString(fExpression); |
| 2074 |
fRegexStart := PRegExprChar(fExpression); |
| 2075 |
fRegexEnd := fRegexStart + Length(fExpression); |
| 2076 |
InvalidateProgramm; |
| 2077 |
end; |
| 2078 |
end; |
| 2079 |
|
| 2080 |
function TRegExpr.GetSubExprCount: Integer; |
| 2081 |
begin |
| 2082 |
Result := -1; |
| 2083 |
// if nothing found, we must return -1 per TRegExpr docs |
| 2084 |
if (GrpBounds[0].GrpStart[0] <> nil) then |
| 2085 |
Result := GrpCount; |
| 2086 |
end; |
| 2087 |
|
| 2088 |
function TRegExpr.GetMatchPos(Idx: Integer): PtrInt; |
| 2089 |
begin |
| 2090 |
Result := -1; |
| 2091 |
if (Idx < 0) or (Idx >= Length(GrpBounds[0].GrpStart)) then |
| 2092 |
Exit; |
| 2093 |
if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) then |
| 2094 |
Result := GrpBounds[0].GrpStart[Idx] - fInputStart + 1; |
| 2095 |
end; |
| 2096 |
|
| 2097 |
function TRegExpr.GetMatchLen(Idx: Integer): PtrInt; |
| 2098 |
begin |
| 2099 |
Result := -1; |
| 2100 |
if (Idx < 0) or (Idx >= Length(GrpBounds[0].GrpStart)) then |
| 2101 |
Exit; |
| 2102 |
if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) then |
| 2103 |
Result := GrpBounds[0].GrpEnd[Idx] - GrpBounds[0].GrpStart[Idx]; |
| 2104 |
end; |
| 2105 |
|
| 2106 |
function TRegExpr.GetMatch(Idx: Integer): RegExprString; |
| 2107 |
begin |
| 2108 |
Result := ''; |
| 2109 |
if (Idx < 0) or (Idx >= Length(GrpBounds[0].GrpStart)) then |
| 2110 |
Exit; |
| 2111 |
if (Idx >= 0) and (GrpBounds[0].GrpStart[Idx] <> nil) and |
| 2112 |
(GrpBounds[0].GrpEnd[Idx] > GrpBounds[0].GrpStart[Idx]) |
| 2113 |
then |
| 2114 |
SetString(Result, GrpBounds[0].GrpStart[Idx], GrpBounds[0].GrpEnd[Idx] - GrpBounds[0].GrpStart[Idx]); |
| 2115 |
end; |
| 2116 |
|
| 2117 |
function TRegExpr.MatchIndexFromName(const AName: RegExprString): Integer; |
| 2118 |
begin |
| 2119 |
Result := GrpNames.MatchIndexFromName(AName); |
| 2120 |
end; |
| 2121 |
|
| 2122 |
function TRegExpr.MatchFromName(const AName: RegExprString): RegExprString; |
| 2123 |
var |
| 2124 |
Idx: Integer; |
| 2125 |
begin |
| 2126 |
Result := ''; |
| 2127 |
Idx := GrpNames.MatchIndexFromName(AName); |
| 2128 |
if Idx >= 0 then |
| 2129 |
Result := GetMatch(Idx) |
| 2130 |
else |
| 2131 |
Result := ''; |
| 2132 |
end; |
| 2133 |
|
| 2134 |
function TRegExpr.GetModifierStr: RegExprString; |
| 2135 |
begin |
| 2136 |
Result := '-'; |
| 2137 |
|
| 2138 |
if ModifierI then |
| 2139 |
Result := 'i' + Result |
| 2140 |
else |
| 2141 |
Result := Result + 'i'; |
| 2142 |
if ModifierR then |
| 2143 |
Result := 'r' + Result |
| 2144 |
else |
| 2145 |
Result := Result + 'r'; |
| 2146 |
if ModifierS then |
| 2147 |
Result := 's' + Result |
| 2148 |
else |
| 2149 |
Result := Result + 's'; |
| 2150 |
if ModifierG then |
| 2151 |
Result := 'g' + Result |
| 2152 |
else |
| 2153 |
Result := Result + 'g'; |
| 2154 |
if ModifierM then |
| 2155 |
Result := 'm' + Result |
| 2156 |
else |
| 2157 |
Result := Result + 'm'; |
| 2158 |
if ModifierX then |
| 2159 |
Result := 'x' + Result |
| 2160 |
else |
| 2161 |
Result := Result + 'x'; |
| 2162 |
|
| 2163 |
if Result[Length(Result)] = '-' // remove '-' if all modifiers are 'On' |
| 2164 |
then |
| 2165 |
System.Delete(Result, Length(Result), 1); |
| 2166 |
end; { of function TRegExpr.GetModifierStr |
| 2167 |
-------------------------------------------------------------- } |
| 2168 |
|
| 2169 |
procedure TRegExpr.SetModifierG(AValue: Boolean); |
| 2170 |
begin |
| 2171 |
if fModifiers.G <> AValue then |
| 2172 |
begin |
| 2173 |
fModifiers.G := AValue; |
| 2174 |
InvalidateProgramm; |
| 2175 |
end; |
| 2176 |
end; |
| 2177 |
|
| 2178 |
procedure TRegExpr.SetModifierI(AValue: Boolean); |
| 2179 |
begin |
| 2180 |
if fModifiers.I <> AValue then |
| 2181 |
begin |
| 2182 |
fModifiers.I := AValue; |
| 2183 |
InvalidateProgramm; |
| 2184 |
end; |
| 2185 |
end; |
| 2186 |
|
| 2187 |
procedure TRegExpr.SetModifierM(AValue: Boolean); |
| 2188 |
begin |
| 2189 |
if fModifiers.M <> AValue then |
| 2190 |
begin |
| 2191 |
fModifiers.M := AValue; |
| 2192 |
InvalidateProgramm; |
| 2193 |
end; |
| 2194 |
end; |
| 2195 |
|
| 2196 |
procedure TRegExpr.SetModifierR(AValue: Boolean); |
| 2197 |
begin |
| 2198 |
if fModifiers.R <> AValue then |
| 2199 |
begin |
| 2200 |
fModifiers.R := AValue; |
| 2201 |
InvalidateProgramm; |
| 2202 |
end; |
| 2203 |
end; |
| 2204 |
|
| 2205 |
procedure TRegExpr.SetModifierS(AValue: Boolean); |
| 2206 |
begin |
| 2207 |
if fModifiers.S <> AValue then |
| 2208 |
begin |
| 2209 |
fModifiers.S := AValue; |
| 2210 |
InvalidateProgramm; |
| 2211 |
end; |
| 2212 |
end; |
| 2213 |
|
| 2214 |
procedure TRegExpr.SetModifierX(AValue: Boolean); |
| 2215 |
begin |
| 2216 |
if fModifiers.X <> AValue then |
| 2217 |
begin |
| 2218 |
fModifiers.X := AValue; |
| 2219 |
InvalidateProgramm; |
| 2220 |
end; |
| 2221 |
end; |
| 2222 |
|
| 2223 |
procedure TRegExpr.SetModifierStr(const AStr: RegExprString); |
| 2224 |
begin |
| 2225 |
if ParseModifiers(PRegExprChar(AStr), Length(AStr), fModifiers) then |
| 2226 |
InvalidateProgramm |
| 2227 |
else |
| 2228 |
Error(reeUnrecognizedModifier); |
| 2229 |
end; |
| 2230 |
|
| 2231 |
{ ============================================================= } |
| 2232 |
{ ==================== Compiler section ======================= } |
| 2233 |
{ ============================================================= } |
| 2234 |
|
| 2235 |
{$IFDEF FastUnicodeData} |
| 2236 |
function TRegExpr.IsWordChar(AChar: REChar): Boolean; |
| 2237 |
begin |
| 2238 |
// bit 7 in value: is word char |
| 2239 |
Result := CharCategoryArray[Ord(AChar)] and 128 <> 0; |
| 2240 |
end; |
| 2241 |
|
| 2242 |
(* |
| 2243 |
// Unicode General Category |
| 2244 |
UGC_UppercaseLetter = 0; Lu |
| 2245 |
UGC_LowercaseLetter = 1; Ll |
| 2246 |
UGC_TitlecaseLetter = 2; Lt |
| 2247 |
UGC_ModifierLetter = 3; Lm |
| 2248 |
UGC_OtherLetter = 4; Lo |
| 2249 |
|
| 2250 |
UGC_NonSpacingMark = 5; Mn |
| 2251 |
UGC_CombiningMark = 6; Mc |
| 2252 |
UGC_EnclosingMark = 7; Me |
| 2253 |
|
| 2254 |
UGC_DecimalNumber = 8; Nd |
| 2255 |
UGC_LetterNumber = 9; Nl |
| 2256 |
UGC_OtherNumber = 10; No |
| 2257 |
|
| 2258 |
UGC_ConnectPunctuation = 11; Pc |
| 2259 |
UGC_DashPunctuation = 12; Pd |
| 2260 |
UGC_OpenPunctuation = 13; Ps |
| 2261 |
UGC_ClosePunctuation = 14; Pe |
| 2262 |
UGC_InitialPunctuation = 15; Pi |
| 2263 |
UGC_FinalPunctuation = 16; Pf |
| 2264 |
UGC_OtherPunctuation = 17; Po |
| 2265 |
|
| 2266 |
UGC_MathSymbol = 18; Sm |
| 2267 |
UGC_CurrencySymbol = 19; Sc |
| 2268 |
UGC_ModifierSymbol = 20; Sk |
| 2269 |
UGC_OtherSymbol = 21; So |
| 2270 |
|
| 2271 |
UGC_SpaceSeparator = 22; Zs |
| 2272 |
UGC_LineSeparator = 23; Zl |
| 2273 |
UGC_ParagraphSeparator = 24; Zp |
| 2274 |
|
| 2275 |
UGC_Control = 25; Cc |
| 2276 |
UGC_Format = 26; Cf |
| 2277 |
UGC_Surrogate = 27; Cs |
| 2278 |
UGC_PrivateUse = 28; Co |
| 2279 |
UGC_Unassigned = 29; Cn |
| 2280 |
*) |
| 2281 |
|
| 2282 |
const |
| 2283 |
CategoryNames: array[0..29] of array[0..1] of REChar = ( |
| 2284 |
('L', 'u'), |
| 2285 |
('L', 'l'), |
| 2286 |
('L', 't'), |
| 2287 |
('L', 'm'), |
| 2288 |
('L', 'o'), |
| 2289 |
('M', 'n'), |
| 2290 |
('M', 'c'), |
| 2291 |
('M', 'e'), |
| 2292 |
('N', 'd'), |
| 2293 |
('N', 'l'), |
| 2294 |
('N', 'o'), |
| 2295 |
('P', 'c'), |
| 2296 |
('P', 'd'), |
| 2297 |
('P', 's'), |
| 2298 |
('P', 'e'), |
| 2299 |
('P', 'i'), |
| 2300 |
('P', 'f'), |
| 2301 |
('P', 'o'), |
| 2302 |
('S', 'm'), |
| 2303 |
('S', 'c'), |
| 2304 |
('S', 'k'), |
| 2305 |
('S', 'o'), |
| 2306 |
('Z', 's'), |
| 2307 |
('Z', 'l'), |
| 2308 |
('Z', 'p'), |
| 2309 |
('C', 'c'), |
| 2310 |
('C', 'f'), |
| 2311 |
('C', 's'), |
| 2312 |
('C', 'o'), |
| 2313 |
('C', 'n') |
| 2314 |
); |
| 2315 |
|
| 2316 |
function IsCategoryFirstChar(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 2317 |
begin |
| 2318 |
case AChar of |
| 2319 |
'L', 'M', 'N', 'P', 'S', 'C', 'Z': |
| 2320 |
Result := True; |
| 2321 |
else |
| 2322 |
Result := False; |
| 2323 |
end; |
| 2324 |
end; |
| 2325 |
|
| 2326 |
function IsCategoryChars(AChar, AChar2: REChar): Boolean; |
| 2327 |
var |
| 2328 |
i: Integer; |
| 2329 |
begin |
| 2330 |
for i := Low(CategoryNames) to High(CategoryNames) do |
| 2331 |
if (AChar = CategoryNames[i][0]) then |
| 2332 |
if (AChar2 = CategoryNames[i][1]) then |
| 2333 |
begin |
| 2334 |
Result := True; |
| 2335 |
Exit |
| 2336 |
end; |
| 2337 |
Result := False; |
| 2338 |
end; |
| 2339 |
|
| 2340 |
function CheckCharCategory(AChar: REChar; Ch0, Ch1: REChar): Boolean; |
| 2341 |
// AChar: check this char against opcode |
| 2342 |
// Ch0, Ch1: opcode operands after OP_*CATEGORY |
| 2343 |
var |
| 2344 |
N: Byte; |
| 2345 |
Name0, Name1: REChar; |
| 2346 |
begin |
| 2347 |
Result := False; |
| 2348 |
// bits 0..6 are category |
| 2349 |
N := CharCategoryArray[Ord(AChar)] and 127; |
| 2350 |
if N <= High(CategoryNames) then |
| 2351 |
begin |
| 2352 |
Name0 := CategoryNames[N][0]; |
| 2353 |
Name1 := CategoryNames[N][1]; |
| 2354 |
if Ch0 <> Name0 then Exit; |
| 2355 |
if Ch1 <> #0 then |
| 2356 |
if Ch1 <> Name1 then Exit; |
| 2357 |
Result := True; |
| 2358 |
end; |
| 2359 |
end; |
| 2360 |
|
| 2361 |
function MatchOneCharCategory(opnd, scan: PRegExprChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 2362 |
// opnd: points to opcode operands after OP_*CATEGORY |
| 2363 |
// scan: points into InputString |
| 2364 |
begin |
| 2365 |
Result := CheckCharCategory(scan^, opnd^, (opnd + 1)^); |
| 2366 |
end; |
| 2367 |
|
| 2368 |
{$ELSE} |
| 2369 |
function TRegExpr.IsWordChar(AChar: REChar): Boolean; |
| 2370 |
begin |
| 2371 |
{$IFDEF UseWordChars} |
| 2372 |
Result := Pos(AChar, fWordChars) > 0; |
| 2373 |
{$ELSE} |
| 2374 |
case AChar of |
| 2375 |
'a' .. 'z', |
| 2376 |
'A' .. 'Z', |
| 2377 |
'0' .. '9', '_': |
| 2378 |
Result := True |
| 2379 |
else |
| 2380 |
Result := False; |
| 2381 |
end; |
| 2382 |
{$ENDIF} |
| 2383 |
end; |
| 2384 |
{$ENDIF} |
| 2385 |
|
| 2386 |
function TRegExpr.IsSpaceChar(AChar: REChar): Boolean; |
| 2387 |
begin |
| 2388 |
{$IFDEF UseSpaceChars} |
| 2389 |
Result := Pos(AChar, fSpaceChars) > 0; |
| 2390 |
{$ELSE} |
| 2391 |
case AChar of |
| 2392 |
' ', #$9, #$A, #$D, #$C: |
| 2393 |
Result := True |
| 2394 |
else |
| 2395 |
Result := False; |
| 2396 |
end; |
| 2397 |
{$ENDIF} |
| 2398 |
end; |
| 2399 |
|
| 2400 |
function TRegExpr.IsCustomLineSeparator(AChar: REChar): Boolean; |
| 2401 |
begin |
| 2402 |
{$IFDEF UseLineSep} |
| 2403 |
{$IFDEF UnicodeRE} |
| 2404 |
Result := Pos(AChar, fLineSeparators) > 0; |
| 2405 |
{$ELSE} |
| 2406 |
Result := fLineSepArray[Byte(AChar)]; |
| 2407 |
{$ENDIF} |
| 2408 |
{$ELSE} |
| 2409 |
case AChar of |
| 2410 |
#$d, #$a, |
| 2411 |
{$IFDEF UnicodeRE} |
| 2412 |
#$85, #$2028, #$2029, |
| 2413 |
{$ENDIF} |
| 2414 |
#$b, #$c: |
| 2415 |
Result := True; |
| 2416 |
else |
| 2417 |
Result := False; |
| 2418 |
end; |
| 2419 |
{$ENDIF} |
| 2420 |
end; |
| 2421 |
|
| 2422 |
function IsDigitChar(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 2423 |
begin |
| 2424 |
case AChar of |
| 2425 |
'0' .. '9': |
| 2426 |
Result := True; |
| 2427 |
else |
| 2428 |
Result := False; |
| 2429 |
end; |
| 2430 |
end; |
| 2431 |
|
| 2432 |
function IsHorzSeparator(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 2433 |
begin |
| 2434 |
// Tab and Unicode categoty "Space Separator": https://www.compart.com/en/unicode/category/Zs |
| 2435 |
case AChar of |
| 2436 |
#9, #$20, #$A0: |
| 2437 |
Result := True; |
| 2438 |
{$IFDEF UnicodeRE} |
| 2439 |
#$1680, #$2000 .. #$200A, #$202F, #$205F, #$3000: |
| 2440 |
Result := True; |
| 2441 |
{$ENDIF} |
| 2442 |
else |
| 2443 |
Result := False; |
| 2444 |
end; |
| 2445 |
end; |
| 2446 |
|
| 2447 |
function IsVertLineSeparator(AChar: REChar): Boolean; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 2448 |
begin |
| 2449 |
case AChar of |
| 2450 |
#$d, #$a, #$b, #$c: |
| 2451 |
Result := True; |
| 2452 |
{$IFDEF UnicodeRE} |
| 2453 |
#$2028, #$2029, #$85: |
| 2454 |
Result := True; |
| 2455 |
{$ENDIF} |
| 2456 |
else |
| 2457 |
Result := False; |
| 2458 |
end; |
| 2459 |
end; |
| 2460 |
|
| 2461 |
procedure TRegExpr.InvalidateProgramm; |
| 2462 |
begin |
| 2463 |
if programm <> nil then |
| 2464 |
begin |
| 2465 |
FreeMem(programm); |
| 2466 |
programm := nil; |
| 2467 |
end; |
| 2468 |
end; { of procedure TRegExpr.InvalidateProgramm |
| 2469 |
-------------------------------------------------------------- } |
| 2470 |
|
| 2471 |
procedure TRegExpr.Compile; |
| 2472 |
begin |
| 2473 |
if fExpression = '' then |
| 2474 |
begin |
| 2475 |
Error(reeNoExpression); |
| 2476 |
Exit; |
| 2477 |
end; |
| 2478 |
|
| 2479 |
CompileRegExpr(fRegexStart); |
| 2480 |
end; { of procedure TRegExpr.Compile |
| 2481 |
-------------------------------------------------------------- } |
| 2482 |
|
| 2483 |
{$IFDEF UseLineSep} |
| 2484 |
procedure TRegExpr.InitLineSepArray; |
| 2485 |
{$IFNDEF UnicodeRE} |
| 2486 |
var |
| 2487 |
i: Integer; |
| 2488 |
{$ENDIF} |
| 2489 |
begin |
| 2490 |
{$IFNDEF UnicodeRE} |
| 2491 |
FillChar(fLineSepArray, SizeOf(fLineSepArray), 0); |
| 2492 |
for i := 1 to Length(fLineSeparators) do |
| 2493 |
fLineSepArray[Byte(fLineSeparators[i])] := True; |
| 2494 |
{$ENDIF} |
| 2495 |
end; |
| 2496 |
{$ENDIF} |
| 2497 |
|
| 2498 |
function TRegExpr.IsProgrammOk: Boolean; |
| 2499 |
begin |
| 2500 |
Result := False; |
| 2501 |
|
| 2502 |
// check modifiers |
| 2503 |
if not IsModifiersEqual(fModifiers, fProgModifiers) then |
| 2504 |
InvalidateProgramm; |
| 2505 |
|
| 2506 |
// compile if needed |
| 2507 |
if programm = nil then |
| 2508 |
begin |
| 2509 |
Compile; |
| 2510 |
// Check compiled programm |
| 2511 |
if programm = nil then |
| 2512 |
Exit; |
| 2513 |
end; |
| 2514 |
|
| 2515 |
if programm[0] <> OP_MAGIC then |
| 2516 |
Error(reeCorruptedProgram) |
| 2517 |
else |
| 2518 |
Result := True; |
| 2519 |
end; { of function TRegExpr.IsProgrammOk |
| 2520 |
-------------------------------------------------------------- } |
| 2521 |
|
| 2522 |
procedure TRegExpr.Tail(p: PRegExprChar; val: PRegExprChar); |
| 2523 |
// set the next-pointer at the end of a node chain |
| 2524 |
var |
| 2525 |
scan: PRegExprChar; |
| 2526 |
begin |
| 2527 |
if p = @regDummy then |
| 2528 |
Exit; |
| 2529 |
// Find last node. |
| 2530 |
scan := regLast(p); |
| 2531 |
// Set Next 'pointer' |
| 2532 |
if val < scan then |
| 2533 |
PRENextOff(AlignToPtr(scan + REOpSz))^ := -(scan - val) |
| 2534 |
// work around PWideChar subtraction bug (Delphi uses |
| 2535 |
// shr after subtraction to calculate widechar distance %-( ) |
| 2536 |
// so, if difference is negative we have .. the "feature" :( |
| 2537 |
// I could wrap it in $IFDEF UnicodeRE, but I didn't because |
| 2538 |
// "P – Q computes the difference between the address given |
| 2539 |
// by P (the higher address) and the address given by Q (the |
| 2540 |
// lower address)" - Delphi help quotation. |
| 2541 |
else |
| 2542 |
PRENextOff(AlignToPtr(scan + REOpSz))^ := val - scan; |
| 2543 |
end; { of procedure TRegExpr.Tail |
| 2544 |
-------------------------------------------------------------- } |
| 2545 |
|
| 2546 |
procedure TRegExpr.OpTail(p: PRegExprChar; val: PRegExprChar); |
| 2547 |
// regtail on operand of first argument; nop if operandless |
| 2548 |
begin |
| 2549 |
// "Operandless" and "op != OP_BRANCH" are synonymous in practice. |
| 2550 |
if (p = nil) or (p = @regDummy) or |
| 2551 |
(PREOp(p)^ <> OP_BRANCH) and (PREOp(p)^ <> OP_GBRANCH) and |
| 2552 |
(PREOp(p)^ <> OP_GBRANCH_EX) and (PREOp(p)^ <> OP_GBRANCH_EX_CI) |
| 2553 |
then |
| 2554 |
Exit; |
| 2555 |
Tail(p + REOpSz + RENextOffSz + REBranchArgSz, val); |
| 2556 |
end; { of procedure TRegExpr.OpTail |
| 2557 |
-------------------------------------------------------------- } |
| 2558 |
|
| 2559 |
function TRegExpr.EmitNode(op: TREOp): PRegExprChar; |
| 2560 |
// emit a node, return location |
| 2561 |
begin |
| 2562 |
Result := regCode; |
| 2563 |
if Result <> @regDummy then |
| 2564 |
begin |
| 2565 |
PREOp(regCode)^ := op; |
| 2566 |
Inc(regCode, REOpSz); |
| 2567 |
PRENextOff(AlignToPtr(regCode))^ := 0; // Next "pointer" := nil |
| 2568 |
Inc(regCode, RENextOffSz); |
| 2569 |
|
| 2570 |
if (op = OP_EXACTLY) or (op = OP_EXACTLY_CI) then |
| 2571 |
regExactlyLen := PLongInt(regCode) |
| 2572 |
else |
| 2573 |
regExactlyLen := nil; |
| 2574 |
|
| 2575 |
{$IFDEF DebugSynRegExpr} |
| 2576 |
if regcode - programm > regCodeSize then |
| 2577 |
raise Exception.Create('TRegExpr.EmitNode buffer overrun'); |
| 2578 |
{$ENDIF} |
| 2579 |
end |
| 2580 |
else |
| 2581 |
Inc(regCodeSize, REOpSz + RENextOffSz); |
| 2582 |
// compute code size without code generation |
| 2583 |
end; { of function TRegExpr.EmitNode |
| 2584 |
-------------------------------------------------------------- } |
| 2585 |
|
| 2586 |
function TRegExpr.EmitBranch: PRegExprChar; |
| 2587 |
begin |
| 2588 |
Result := EmitNode(OP_BRANCH); |
| 2589 |
EmitC(#0); |
| 2590 |
EmitC(#0); |
| 2591 |
end; |
| 2592 |
|
| 2593 |
procedure TRegExpr.EmitC(ch: REChar); |
| 2594 |
begin |
| 2595 |
if regCode <> @regDummy then |
| 2596 |
begin |
| 2597 |
regCode^ := ch; |
| 2598 |
Inc(regCode); |
| 2599 |
{$IFDEF DebugSynRegExpr} |
| 2600 |
if regcode - programm > regCodeSize then |
| 2601 |
raise Exception.Create('TRegExpr.EmitC buffer overrun'); |
| 2602 |
{$ENDIF} |
| 2603 |
end |
| 2604 |
else |
| 2605 |
Inc(regCodeSize, REOpSz); // Type of p-code pointer always is ^REChar |
| 2606 |
end; { of procedure TRegExpr.EmitC |
| 2607 |
-------------------------------------------------------------- } |
| 2608 |
|
| 2609 |
procedure TRegExpr.EmitInt(AValue: LongInt); |
| 2610 |
begin |
| 2611 |
if regCode <> @regDummy then |
| 2612 |
begin |
| 2613 |
PLongInt(regCode)^ := AValue; |
| 2614 |
Inc(regCode, RENumberSz); |
| 2615 |
{$IFDEF DebugSynRegExpr} |
| 2616 |
if regcode - programm > regCodeSize then |
| 2617 |
raise Exception.Create('TRegExpr.EmitInt buffer overrun'); |
| 2618 |
{$ENDIF} |
| 2619 |
end |
| 2620 |
else |
| 2621 |
Inc(regCodeSize, RENumberSz); |
| 2622 |
end; |
| 2623 |
|
| 2624 |
function TRegExpr.EmitNodeWithGroupIndex(op: TREOp; AIndex: Integer): PRegExprChar; |
| 2625 |
begin |
| 2626 |
Result := EmitNode(op); |
| 2627 |
EmitInt(AIndex); // TReGroupIndex = LongInt; |
| 2628 |
end; |
| 2629 |
|
| 2630 |
function TRegExpr.EmitGroupRef(AIndex: Integer; AIgnoreCase: Boolean): PRegExprChar; |
| 2631 |
begin |
| 2632 |
if AIgnoreCase then |
| 2633 |
Result := EmitNode(OP_BSUBEXP_CI) |
| 2634 |
else |
| 2635 |
Result := EmitNode(OP_BSUBEXP); |
| 2636 |
EmitInt(AIndex); // TReGroupIndex = LongInt; |
| 2637 |
end; |
| 2638 |
|
| 2639 |
{$IFDEF FastUnicodeData} |
| 2640 |
procedure TRegExpr.FindCategoryName(var scan: PRegExprChar; var ch1, ch2: REChar); |
| 2641 |
// scan: points into regex string after '\p', to find category name |
| 2642 |
// ch1, ch2: 2-char name of category; ch2 can be #0 |
| 2643 |
var |
| 2644 |
ch: REChar; |
| 2645 |
pos1, pos2, namePtr: PRegExprChar; |
| 2646 |
nameLen: Integer; |
| 2647 |
begin |
| 2648 |
ch1 := #0; |
| 2649 |
ch2 := #0; |
| 2650 |
ch := scan^; |
| 2651 |
if IsCategoryFirstChar(ch) then |
| 2652 |
begin |
| 2653 |
ch1 := ch; |
| 2654 |
Exit; |
| 2655 |
end; |
| 2656 |
if ch = '{' then |
| 2657 |
begin |
| 2658 |
pos1 := scan; |
| 2659 |
pos2 := pos1; |
| 2660 |
while (pos2 < fRegexEnd) and (pos2^ <> '}') do |
| 2661 |
Inc(pos2); |
| 2662 |
if pos2 >= fRegexEnd then |
| 2663 |
Error(reeIncorrectBraces); |
| 2664 |
|
| 2665 |
namePtr := pos1+1; |
| 2666 |
nameLen := pos2-pos1-1; |
| 2667 |
Inc(scan, nameLen+1); |
| 2668 |
|
| 2669 |
if nameLen<1 then |
| 2670 |
Error(reeBadUnicodeCategory); |
| 2671 |
if nameLen>2 then |
| 2672 |
Error(reeBadUnicodeCategory); |
| 2673 |
|
| 2674 |
if nameLen = 1 then |
| 2675 |
begin |
| 2676 |
ch1 := namePtr^; |
| 2677 |
ch2 := #0; |
| 2678 |
if not IsCategoryFirstChar(ch1) then |
| 2679 |
Error(reeBadUnicodeCategory); |
| 2680 |
Exit; |
| 2681 |
end; |
| 2682 |
|
| 2683 |
if nameLen = 2 then |
| 2684 |
begin |
| 2685 |
ch1 := namePtr^; |
| 2686 |
ch2 := (namePtr+1)^; |
| 2687 |
if not IsCategoryChars(ch1, ch2) then |
| 2688 |
Error(reeBadUnicodeCategory); |
| 2689 |
Exit; |
| 2690 |
end; |
| 2691 |
end |
| 2692 |
else |
| 2693 |
Error(reeBadUnicodeCategory); |
| 2694 |
end; |
| 2695 |
|
| 2696 |
function TRegExpr.EmitCategoryMain(APositive: Boolean): PRegExprChar; |
| 2697 |
var |
| 2698 |
ch, ch2: REChar; |
| 2699 |
begin |
| 2700 |
Inc(regParse); |
| 2701 |
if regParse >= fRegexEnd then |
| 2702 |
Error(reeBadUnicodeCategory); |
| 2703 |
FindCategoryName(regParse, ch, ch2); |
| 2704 |
if APositive then |
| 2705 |
Result := EmitNode(OP_ANYCATEGORY) |
| 2706 |
else |
| 2707 |
Result := EmitNode(OP_NOTCATEGORY); |
| 2708 |
EmitC(ch); |
| 2709 |
EmitC(ch2); |
| 2710 |
end; |
| 2711 |
{$ENDIF} |
| 2712 |
|
| 2713 |
procedure TRegExpr.InsertOperator(op: TREOp; opnd: PRegExprChar; sz: Integer); |
| 2714 |
// insert an operator in front of already-emitted operand |
| 2715 |
// Means relocating the operand. |
| 2716 |
var |
| 2717 |
src, dst, place: PRegExprChar; |
| 2718 |
i: Integer; |
| 2719 |
begin |
| 2720 |
if regCode = @regDummy then |
| 2721 |
begin |
| 2722 |
Inc(regCodeSize, sz); |
| 2723 |
Exit; |
| 2724 |
end; |
| 2725 |
// move code behind insert position |
| 2726 |
src := regCode; |
| 2727 |
Inc(regCode, sz); |
| 2728 |
{$IFDEF DebugSynRegExpr} |
| 2729 |
if regCode - programm > regCodeSize then |
| 2730 |
raise Exception.Create('TRegExpr.InsertOperator buffer overrun'); |
| 2731 |
if fSecondPass and ( (opnd<regCodeWork) or (opnd-regCodeWork>regCodeSize) ) then |
| 2732 |
raise Exception.Create('TRegExpr.InsertOperator invalid opnd'); |
| 2733 |
{$ENDIF} |
| 2734 |
dst := regCode; |
| 2735 |
while src > opnd do |
| 2736 |
begin |
| 2737 |
Dec(dst); |
| 2738 |
Dec(src); |
| 2739 |
dst^ := src^; |
| 2740 |
end; |
| 2741 |
place := opnd; // Op node, where operand used to be. |
| 2742 |
PREOp(place)^ := op; |
| 2743 |
Inc(place, REOpSz); |
| 2744 |
for i := 1 + REOpSz to sz do |
| 2745 |
begin |
| 2746 |
place^ := #0; |
| 2747 |
Inc(place); |
| 2748 |
end; |
| 2749 |
for i := 0 to regNumBrackets - 1 do |
| 2750 |
if (GrpOpCodes[i] <> nil) and (GrpOpCodes[i] >= opnd) then |
| 2751 |
GrpOpCodes[i] := GrpOpCodes[i] + sz; |
| 2752 |
end; { of procedure TRegExpr.InsertOperator |
| 2753 |
-------------------------------------------------------------- } |
| 2754 |
|
| 2755 |
procedure TRegExpr.RemoveOperator(opnd: PRegExprChar; sz: Integer); |
| 2756 |
// remove an operator in front of already-emitted operand |
| 2757 |
// Means relocating the operand. |
| 2758 |
var |
| 2759 |
src, dst: PRegExprChar; |
| 2760 |
i: Integer; |
| 2761 |
begin |
| 2762 |
if regCode = @regDummy then |
| 2763 |
begin |
| 2764 |
// Do not decrement regCodeSize => the fSecondPass may temporary fill the extra memory; |
| 2765 |
Exit; |
| 2766 |
end; |
| 2767 |
// move code behind insert position |
| 2768 |
{$IFDEF DebugSynRegExpr} |
| 2769 |
if fSecondPass and ( (opnd<regCodeWork) or (opnd>=regCodeWork+regCodeSize) ) then |
| 2770 |
raise Exception.Create('TRegExpr.RemoveOperator() invalid opnd'); |
| 2771 |
if (sz > regCodeSize-(opnd-regCodeWork)) then |
| 2772 |
raise Exception.Create('TRegExpr.RemoveOperator buffer underrun'); |
| 2773 |
{$ENDIF} |
| 2774 |
src := opnd + sz; |
| 2775 |
dst := opnd; |
| 2776 |
while src < regCode do |
| 2777 |
begin |
| 2778 |
dst^ := src^; |
| 2779 |
Inc(dst); |
| 2780 |
Inc(src); |
| 2781 |
end; |
| 2782 |
Dec(regCode, sz); |
| 2783 |
for i := 0 to regNumBrackets - 1 do |
| 2784 |
if (GrpOpCodes[i] <> nil) and (GrpOpCodes[i] > opnd) then |
| 2785 |
GrpOpCodes[i] := GrpOpCodes[i] - sz; |
| 2786 |
end; |
| 2787 |
|
| 2788 |
function FindSkippedMetaLen(PStart, PEnd: PRegExprChar): Integer; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 2789 |
// find length of initial segment of PStart string consisting |
| 2790 |
// entirely of characters not from IsMetaSymbol1. |
| 2791 |
begin |
| 2792 |
Result := 0; |
| 2793 |
while PStart < PEnd do |
| 2794 |
begin |
| 2795 |
if _IsMetaSymbol1(PStart^) then |
| 2796 |
Exit; |
| 2797 |
Inc(Result); |
| 2798 |
Inc(PStart) |
| 2799 |
end; |
| 2800 |
end; |
| 2801 |
|
| 2802 |
const |
| 2803 |
// Flags to be passed up and down. |
| 2804 |
FLAG_WORST = 0; // Worst case |
| 2805 |
FLAG_HASWIDTH = 1; // Cannot match empty string |
| 2806 |
FLAG_SIMPLE = 2; // Simple enough to be OP_STAR/OP_PLUS/OP_BRACES operand |
| 2807 |
FLAG_SPECSTART = 4; // Starts with * or + |
| 2808 |
FLAG_LOOP = 8; // Has eithe *, + or {,n} with n>=2 |
| 2809 |
FLAG_GREEDY = 16; // Has any greedy code |
| 2810 |
FLAG_NOT_QUANTIFIABLE = 64; // "Piece" (ParsePiece) is look-around |
| 2811 |
|
| 2812 |
{$IFDEF UnicodeRE} |
| 2813 |
RusRangeLoLow = #$430; // 'а' |
| 2814 |
RusRangeLoHigh = #$44F; // 'я' |
| 2815 |
RusRangeHiLow = #$410; // 'А' |
| 2816 |
RusRangeHiHigh = #$42F; // 'Я' |
| 2817 |
{$ELSE} |
| 2818 |
RusRangeLoLow = #$E0; // 'а' in cp1251 |
| 2819 |
RusRangeLoHigh = #$FF; // 'я' in cp1251 |
| 2820 |
RusRangeHiLow = #$C0; // 'А' in cp1251 |
| 2821 |
RusRangeHiHigh = #$DF; // 'Я' in cp1251 |
| 2822 |
{$ENDIF} |
| 2823 |
|
| 2824 |
function TRegExpr.FindInCharClass(ABuffer: PRegExprChar; AChar: REChar): Boolean; |
| 2825 |
// Buffer contains char pairs: (Kind, Data), where Kind is one of OpKind_ values, |
| 2826 |
// and Data depends on Kind |
| 2827 |
var |
| 2828 |
OpKind: REChar; |
| 2829 |
{$IFDEF FastUnicodeData} |
| 2830 |
ch, ch2: REChar; |
| 2831 |
{$ENDIF} |
| 2832 |
N: integer; |
| 2833 |
begin |
| 2834 |
repeat |
| 2835 |
OpKind := ABuffer^; |
| 2836 |
case OpKind of |
| 2837 |
OpKind_End: |
| 2838 |
begin |
| 2839 |
Result := False; |
| 2840 |
Exit; |
| 2841 |
end; |
| 2842 |
|
| 2843 |
OpKind_Range: |
| 2844 |
begin |
| 2845 |
Inc(ABuffer); |
| 2846 |
if (AChar >= ABuffer^) then |
| 2847 |
begin |
| 2848 |
Inc(ABuffer); |
| 2849 |
if (AChar <= ABuffer^) then |
| 2850 |
begin |
| 2851 |
Result := True; |
| 2852 |
Exit; |
| 2853 |
end; |
| 2854 |
Inc(ABuffer); |
| 2855 |
end |
| 2856 |
else |
| 2857 |
Inc(ABuffer, 2); |
| 2858 |
end; |
| 2859 |
|
| 2860 |
OpKind_MetaClass: |
| 2861 |
begin |
| 2862 |
Inc(ABuffer); |
| 2863 |
N := Ord(ABuffer^); |
| 2864 |
if CharCheckers[N](AChar) then |
| 2865 |
begin |
| 2866 |
Result := True; |
| 2867 |
Exit |
| 2868 |
end; |
| 2869 |
Inc(ABuffer); |
| 2870 |
end; |
| 2871 |
|
| 2872 |
OpKind_Char: |
| 2873 |
begin |
| 2874 |
Inc(ABuffer); |
| 2875 |
N := PLongInt(ABuffer)^; |
| 2876 |
Inc(ABuffer, RENumberSz); |
| 2877 |
repeat |
| 2878 |
if ABuffer^ = AChar then |
| 2879 |
begin |
| 2880 |
Result := True; |
| 2881 |
Exit; |
| 2882 |
end; |
| 2883 |
Inc(ABuffer); |
| 2884 |
dec(n); |
| 2885 |
until n = 0; |
| 2886 |
end; |
| 2887 |
|
| 2888 |
{$IFDEF FastUnicodeData} |
| 2889 |
OpKind_CategoryYes, |
| 2890 |
OpKind_CategoryNo: |
| 2891 |
begin |
| 2892 |
Inc(ABuffer); |
| 2893 |
ch := ABuffer^; |
| 2894 |
Inc(ABuffer); |
| 2895 |
ch2 := ABuffer^; |
| 2896 |
Inc(ABuffer); |
| 2897 |
Result := CheckCharCategory(AChar, ch, ch2); |
| 2898 |
if OpKind = OpKind_CategoryNo then |
| 2899 |
Result := not Result; |
| 2900 |
if Result then |
| 2901 |
Exit; |
| 2902 |
end; |
| 2903 |
{$ENDIF} |
| 2904 |
|
| 2905 |
{$IFDEF WITH_REGEX_ASSERT} |
| 2906 |
else |
| 2907 |
Error(reeBadOpcodeInCharClass); |
| 2908 |
{$ENDIF} |
| 2909 |
end; |
| 2910 |
until False; // assume that Buffer is ended correctly |
| 2911 |
end; |
| 2912 |
|
| 2913 |
|
| 2914 |
procedure TRegExpr.GetCharSetFromWordChars(var ARes: TRegExprCharSet); |
| 2915 |
{$IFDEF UseWordChars} |
| 2916 |
var |
| 2917 |
i: Integer; |
| 2918 |
ch: REChar; |
| 2919 |
{$ENDIF} |
| 2920 |
begin |
| 2921 |
{$IFDEF UseWordChars} |
| 2922 |
ARes := []; |
| 2923 |
for i := 1 to Length(fWordChars) do |
| 2924 |
begin |
| 2925 |
ch := fWordChars[i]; |
| 2926 |
{$IFDEF UnicodeRE} |
| 2927 |
if Ord(ch) <= $FF then |
| 2928 |
{$ENDIF} |
| 2929 |
Include(ARes, Byte(ch)); |
| 2930 |
end; |
| 2931 |
{$ELSE} |
| 2932 |
ARes := RegExprWordSet; |
| 2933 |
{$ENDIF} |
| 2934 |
end; |
| 2935 |
|
| 2936 |
procedure TRegExpr.GetCharSetFromSpaceChars(var ARes: TRegExprCharset); |
| 2937 |
{$IFDEF UseSpaceChars} |
| 2938 |
var |
| 2939 |
i: Integer; |
| 2940 |
ch: REChar; |
| 2941 |
{$ENDIF} |
| 2942 |
begin |
| 2943 |
{$IFDEF UseSpaceChars} |
| 2944 |
ARes := []; |
| 2945 |
for i := 1 to Length(fSpaceChars) do |
| 2946 |
begin |
| 2947 |
ch := fSpaceChars[i]; |
| 2948 |
{$IFDEF UnicodeRE} |
| 2949 |
if Ord(ch) <= $FF then |
| 2950 |
{$ENDIF} |
| 2951 |
Include(ARes, Byte(ch)); |
| 2952 |
end; |
| 2953 |
{$ELSE} |
| 2954 |
ARes := RegExprSpaceSet; |
| 2955 |
{$ENDIF} |
| 2956 |
end; |
| 2957 |
|
| 2958 |
procedure TRegExpr.GetCharSetFromCharClass(ABuffer: PRegExprChar; AIgnoreCase: Boolean; var ARes: TRegExprCharset); |
| 2959 |
var |
| 2960 |
ch, ch2: REChar; |
| 2961 |
TempSet: TRegExprCharSet; |
| 2962 |
N, i: Integer; |
| 2963 |
begin |
| 2964 |
ARes := []; |
| 2965 |
TempSet := []; |
| 2966 |
repeat |
| 2967 |
case ABuffer^ of |
| 2968 |
OpKind_End: |
| 2969 |
Exit; |
| 2970 |
|
| 2971 |
OpKind_Range: |
| 2972 |
begin |
| 2973 |
Inc(ABuffer); |
| 2974 |
ch := ABuffer^; |
| 2975 |
Inc(ABuffer); |
| 2976 |
ch2 := ABuffer^; |
| 2977 |
{$IFDEF UnicodeRE} |
| 2978 |
if Ord(ch2) > $FF then |
| 2979 |
ch2 := REChar($FF); |
| 2980 |
{$ENDIF} |
| 2981 |
Inc(ABuffer); |
| 2982 |
for i := Ord(ch) to Ord(ch2) do |
| 2983 |
begin |
| 2984 |
Include(ARes, Byte(i)); |
| 2985 |
if AIgnoreCase then |
| 2986 |
Include(ARes, Byte(InvertCase(REChar(i)))); |
| 2987 |
end; |
| 2988 |
end; |
| 2989 |
|
| 2990 |
OpKind_MetaClass: |
| 2991 |
begin |
| 2992 |
Inc(ABuffer); |
| 2993 |
N := Ord(ABuffer^); |
| 2994 |
Inc(ABuffer); |
| 2995 |
|
| 2996 |
if N = CheckerIndex_Word then |
| 2997 |
begin |
| 2998 |
GetCharSetFromWordChars(TempSet); |
| 2999 |
ARes := ARes + TempSet; |
| 3000 |
end |
| 3001 |
else |
| 3002 |
if N = CheckerIndex_NotWord then |
| 3003 |
begin |
| 3004 |
GetCharSetFromWordChars(TempSet); |
| 3005 |
ARes := ARes + (RegExprAllSet - TempSet); |
| 3006 |
end |
| 3007 |
else |
| 3008 |
if N = CheckerIndex_Space then |
| 3009 |
begin |
| 3010 |
GetCharSetFromSpaceChars(TempSet); |
| 3011 |
ARes := ARes + TempSet; |
| 3012 |
end |
| 3013 |
else |
| 3014 |
if N = CheckerIndex_NotSpace then |
| 3015 |
begin |
| 3016 |
GetCharSetFromSpaceChars(TempSet); |
| 3017 |
ARes := ARes + (RegExprAllSet - TempSet); |
| 3018 |
end |
| 3019 |
else |
| 3020 |
if N = CheckerIndex_Digit then |
| 3021 |
ARes := ARes + RegExprDigitSet |
| 3022 |
else |
| 3023 |
if N = CheckerIndex_NotDigit then |
| 3024 |
ARes := ARes + (RegExprAllSet - RegExprDigitSet) |
| 3025 |
else |
| 3026 |
if N = CheckerIndex_VertSep then |
| 3027 |
ARes := ARes + RegExprLineSeparatorsSet |
| 3028 |
else |
| 3029 |
if N = CheckerIndex_NotVertSep then |
| 3030 |
ARes := ARes + (RegExprAllSet - RegExprLineSeparatorsSet) |
| 3031 |
else |
| 3032 |
if N = CheckerIndex_HorzSep then |
| 3033 |
ARes := ARes + RegExprHorzSeparatorsSet |
| 3034 |
else |
| 3035 |
if N = CheckerIndex_NotHorzSep then |
| 3036 |
ARes := ARes + (RegExprAllSet - RegExprHorzSeparatorsSet) |
| 3037 |
else |
| 3038 |
if N = CheckerIndex_LowerAZ then |
| 3039 |
begin |
| 3040 |
if AIgnoreCase then |
| 3041 |
ARes := ARes + RegExprAllAzSet |
| 3042 |
else |
| 3043 |
ARes := ARes + RegExprLowerAzSet; |
| 3044 |
end |
| 3045 |
else |
| 3046 |
if N = CheckerIndex_UpperAZ then |
| 3047 |
begin |
| 3048 |
if AIgnoreCase then |
| 3049 |
ARes := ARes + RegExprAllAzSet |
| 3050 |
else |
| 3051 |
ARes := ARes + RegExprUpperAzSet; |
| 3052 |
end |
| 3053 |
else |
| 3054 |
if N = CheckerIndex_AnyLineBreak then |
| 3055 |
begin |
| 3056 |
ARes := ARes + RegExprLineSeparatorsSet; |
| 3057 |
//we miss U+2028 and U+2029 here |
| 3058 |
end |
| 3059 |
else |
| 3060 |
Error(reeBadOpcodeInCharClass); |
| 3061 |
end; |
| 3062 |
|
| 3063 |
OpKind_Char: |
| 3064 |
begin |
| 3065 |
Inc(ABuffer); |
| 3066 |
N := PLongInt(ABuffer)^; |
| 3067 |
Inc(ABuffer, RENumberSz); |
| 3068 |
for i := 1 to N do |
| 3069 |
begin |
| 3070 |
ch := ABuffer^; |
| 3071 |
Inc(ABuffer); |
| 3072 |
{$IFDEF UnicodeRE} |
| 3073 |
if Ord(ch) <= $FF then |
| 3074 |
{$ENDIF} |
| 3075 |
begin |
| 3076 |
Include(ARes, Byte(ch)); |
| 3077 |
if AIgnoreCase then |
| 3078 |
Include(ARes, Byte(InvertCase(ch))); |
| 3079 |
end; |
| 3080 |
end; |
| 3081 |
end; |
| 3082 |
|
| 3083 |
{$IFDEF FastUnicodeData} |
| 3084 |
OpKind_CategoryYes, |
| 3085 |
OpKind_CategoryNo: |
| 3086 |
begin |
| 3087 |
// usage of FirstCharSet makes no sense for regex with \p \P |
| 3088 |
ARes := RegExprAllSet; |
| 3089 |
Exit; |
| 3090 |
end; |
| 3091 |
{$ENDIF} |
| 3092 |
|
| 3093 |
{$IFDEF WITH_REGEX_ASSERT} |
| 3094 |
else |
| 3095 |
Error(reeBadOpcodeInCharClass); |
| 3096 |
{$ENDIF} |
| 3097 |
end; |
| 3098 |
until False; // assume that Buffer is ended correctly |
| 3099 |
end; |
| 3100 |
|
| 3101 |
|
| 3102 |
function TRegExpr.GetModifierG: Boolean; |
| 3103 |
begin |
| 3104 |
Result := fModifiers.G; |
| 3105 |
end; |
| 3106 |
|
| 3107 |
function TRegExpr.GetModifierI: Boolean; |
| 3108 |
begin |
| 3109 |
Result := fModifiers.I; |
| 3110 |
end; |
| 3111 |
|
| 3112 |
function TRegExpr.GetModifierM: Boolean; |
| 3113 |
begin |
| 3114 |
Result := fModifiers.M; |
| 3115 |
end; |
| 3116 |
|
| 3117 |
function TRegExpr.GetModifierR: Boolean; |
| 3118 |
begin |
| 3119 |
Result := fModifiers.R; |
| 3120 |
end; |
| 3121 |
|
| 3122 |
function TRegExpr.GetModifierS: Boolean; |
| 3123 |
begin |
| 3124 |
Result := fModifiers.S; |
| 3125 |
end; |
| 3126 |
|
| 3127 |
function TRegExpr.GetModifierX: Boolean; |
| 3128 |
begin |
| 3129 |
Result := fModifiers.X; |
| 3130 |
end; |
| 3131 |
|
| 3132 |
function TRegExpr.CompileRegExpr(ARegExp: PRegExprChar): Boolean; |
| 3133 |
// Compile a regular expression into internal code |
| 3134 |
// We can't allocate space until we know how big the compiled form will be, |
| 3135 |
// but we can't compile it (and thus know how big it is) until we've got a |
| 3136 |
// place to put the code. So we cheat: we compile it twice, once with code |
| 3137 |
// generation turned off and size counting turned on, and once "for real". |
| 3138 |
// This also means that we don't allocate space until we are sure that the |
| 3139 |
// thing really will compile successfully, and we never have to move the |
| 3140 |
// code and thus invalidate pointers into it. (Note that it has to be in |
| 3141 |
// one piece because free() must be able to free it all.) |
| 3142 |
// Beware that the optimization-preparation code in here knows about some |
| 3143 |
// of the structure of the compiled regexp. |
| 3144 |
var |
| 3145 |
scan, scanTemp, longest, longestTemp: PRegExprChar; |
| 3146 |
Len, LenTemp: Integer; |
| 3147 |
FlagTemp, MaxMatchLen: integer; |
| 3148 |
op: TREOp; |
| 3149 |
begin |
| 3150 |
Result := False; |
| 3151 |
FlagTemp := 0; |
| 3152 |
regParse := nil; // for correct error handling |
| 3153 |
regExactlyLen := nil; |
| 3154 |
|
| 3155 |
GrpCount := 0; |
| 3156 |
ParsedGrpCount := 0; |
| 3157 |
GrpNames.Clear; |
| 3158 |
fLastError := reeOk; |
| 3159 |
fLastErrorOpcode := TREOp(0); |
| 3160 |
hasRecursion := False; |
| 3161 |
|
| 3162 |
try |
| 3163 |
if programm <> nil then |
| 3164 |
begin |
| 3165 |
FreeMem(programm); |
| 3166 |
programm := nil; |
| 3167 |
end; |
| 3168 |
|
| 3169 |
if ARegExp = nil then |
| 3170 |
begin |
| 3171 |
Error(reeCompNullArgument); |
| 3172 |
Exit; |
| 3173 |
end; |
| 3174 |
|
| 3175 |
fProgModifiers := fModifiers; |
| 3176 |
// well, may it's paranoia. I'll check it later. |
| 3177 |
|
| 3178 |
// First pass: calculate opcode size, validate regex |
| 3179 |
fSecondPass := False; |
| 3180 |
fCompModifiers := fModifiers; |
| 3181 |
regParse := ARegExp; |
| 3182 |
regNumBrackets := 1; |
| 3183 |
regNumAtomicBrackets := 0; |
| 3184 |
regCodeSize := 0; |
| 3185 |
regCode := @regDummy; |
| 3186 |
regCodeWork := nil; |
| 3187 |
|
| 3188 |
EmitC(OP_MAGIC); |
| 3189 |
if ParseReg(FlagTemp) = nil then begin |
| 3190 |
regNumBrackets := 0; // Not calling InitInternalGroupData => array sizes not adjusted for FillChar |
| 3191 |
regNumAtomicBrackets := 0; |
| 3192 |
Exit; |
| 3193 |
end; |
| 3194 |
|
| 3195 |
// Allocate memory |
| 3196 |
GetMem(programm, regCodeSize * SizeOf(REChar)); |
| 3197 |
InitInternalGroupData; |
| 3198 |
|
| 3199 |
// Second pass: emit opcode |
| 3200 |
fSecondPass := True; |
| 3201 |
fCompModifiers := fModifiers; |
| 3202 |
regParse := ARegExp; |
| 3203 |
regNumBrackets := 1; |
| 3204 |
regNumAtomicBrackets := 0; |
| 3205 |
GrpCount := ParsedGrpCount; |
| 3206 |
ParsedGrpCount := 0; |
| 3207 |
regCode := programm; |
| 3208 |
regCodeWork := programm + REOpSz; |
| 3209 |
|
| 3210 |
EmitC(OP_MAGIC); |
| 3211 |
if ParseReg(FlagTemp) = nil then |
| 3212 |
Exit; |
| 3213 |
|
| 3214 |
// Dig out information for optimizations. |
| 3215 |
IsFixedLengthEx(op, FMinMatchLen, MaxMatchLen); |
| 3216 |
{$IFDEF UseFirstCharSet} |
| 3217 |
FirstCharSet := []; |
| 3218 |
FillFirstCharSet(regCodeWork); |
| 3219 |
for Len := 0 to 255 do |
| 3220 |
FirstCharArray[Len] := Byte(Len) in FirstCharSet; |
| 3221 |
{$ENDIF} |
| 3222 |
|
| 3223 |
regAnchored := raNone; |
| 3224 |
regMust := nil; |
| 3225 |
regMustLen := 0; |
| 3226 |
regMustString := ''; |
| 3227 |
|
| 3228 |
scan := regCodeWork; // First OP_BRANCH. |
| 3229 |
// Starting-point info. |
| 3230 |
if PREOp(scan)^ = OP_BOL then |
| 3231 |
regAnchored := raBOL |
| 3232 |
else |
| 3233 |
if PREOp(scan)^ = OP_EOL then |
| 3234 |
regAnchored := raEOL |
| 3235 |
else |
| 3236 |
if PREOp(scan)^ = OP_CONTINUE_POS then |
| 3237 |
regAnchored := raContinue |
| 3238 |
else |
| 3239 |
// ".*", ".*?", ".*+" at the very start of the pattern, only need to be |
| 3240 |
// tested from the start-pos of the InputString. |
| 3241 |
// If a pattern matches, then the ".*" will always go forward to where the |
| 3242 |
// rest of the pattern starts matching |
| 3243 |
// OP_ANY is "ModifierS=True" |
| 3244 |
if (PREOp(scan)^ = OP_STAR) or (PREOp(scan)^ = OP_STAR_NG) or (PREOp(scan)^ = OP_STAR_POSS) then begin |
| 3245 |
scanTemp := AlignToInt(scan + REOpSz + RENextOffSz); |
| 3246 |
if PREOp(scanTemp)^ = OP_ANY then |
| 3247 |
regAnchored := raOnlyOnce; |
| 3248 |
end |
| 3249 |
else |
| 3250 |
// "{0,} is the same as ".*". So the same optimization applies |
| 3251 |
if (PREOp(scan)^ = OP_BRACES) or (PREOp(scan)^ = OP_BRACES_NG) or (PREOp(scan)^ = OP_BRACES_POSS) then begin |
| 3252 |
scanTemp := AlignToInt(scan + REOpSz + RENextOffSz); |
| 3253 |
if (PREBracesArg(scanTemp)^ = 0) // BracesMinCount |
| 3254 |
and (PREBracesArg(scanTemp + REBracesArgSz)^ = MaxBracesArg) // BracesMaxCount |
| 3255 |
then begin |
| 3256 |
scanTemp := AlignToPtr(scanTemp + REBracesArgSz + REBracesArgSz); |
| 3257 |
if PREOp(scanTemp)^ = OP_ANY then |
| 3258 |
regAnchored := raOnlyOnce; |
| 3259 |
end; |
| 3260 |
end; |
| 3261 |
|
| 3262 |
// If there's something expensive in the r.e., find the longest |
| 3263 |
// literal string that must appear and make it the regMust. Resolve |
| 3264 |
// ties in favor of later strings, since the regstart check works |
| 3265 |
// with the beginning of the r.e. and avoiding duplication |
| 3266 |
// strengthens checking. Not a strong reason, but sufficient in the |
| 3267 |
// absence of others. |
| 3268 |
if (FlagTemp and FLAG_SPECSTART) <> 0 then |
| 3269 |
begin |
| 3270 |
longest := nil; |
| 3271 |
Len := 0; |
| 3272 |
while scan <> nil do |
| 3273 |
begin |
| 3274 |
if PREOp(scan)^ = OP_EXACTLY then |
| 3275 |
begin |
| 3276 |
longestTemp := scan + REOpSz + RENextOffSz + RENumberSz; |
| 3277 |
LenTemp := PLongInt(scan + REOpSz + RENextOffSz)^; |
| 3278 |
if LenTemp >= Len then |
| 3279 |
begin |
| 3280 |
longest := longestTemp; |
| 3281 |
Len := LenTemp; |
| 3282 |
end; |
| 3283 |
end; |
| 3284 |
scan := regNext(scan); |
| 3285 |
end; |
| 3286 |
regMust := longest; |
| 3287 |
regMustLen := Len; |
| 3288 |
if regMustLen > 1 then // don't use regMust if too short |
| 3289 |
SetString(regMustString, regMust, regMustLen); |
| 3290 |
end; |
| 3291 |
|
| 3292 |
Result := True; |
| 3293 |
|
| 3294 |
finally |
| 3295 |
begin |
| 3296 |
if not Result then |
| 3297 |
InvalidateProgramm; |
| 3298 |
end; |
| 3299 |
end; |
| 3300 |
|
| 3301 |
end; { of function TRegExpr.CompileRegExpr |
| 3302 |
-------------------------------------------------------------- } |
| 3303 |
|
| 3304 |
function TRegExpr.ParseReg(var FlagParse: Integer): PRegExprChar; |
| 3305 |
begin |
| 3306 |
Result := DoParseReg(False, nil, FlagParse, OP_NONE, OP_COMMENT); // can't use OP_NONE // The "ender" op will not be omitted anyway |
| 3307 |
end; |
| 3308 |
|
| 3309 |
function TRegExpr.DoParseReg(InBrackets: Boolean; BracketCounter: PInteger; |
| 3310 |
var FlagParse: Integer; BeginGroupOp, EndGroupOP: TReOp): PRegExprChar; |
| 3311 |
// regular expression, i.e. main body or parenthesized thing |
| 3312 |
// Caller must absorb opening parenthesis. |
| 3313 |
// Combining parenthesis handling with the base level of regular expression |
| 3314 |
// is a trifle forced, but the need to tie the tails of the branches to what |
| 3315 |
// follows makes it hard to avoid. |
| 3316 |
var |
| 3317 |
ret, br, ender, brStart: PRegExprChar; |
| 3318 |
NBrackets: Integer; |
| 3319 |
FlagTemp: Integer; |
| 3320 |
SavedModifiers: TRegExprModifiers; |
| 3321 |
HasGBranch, HasChoice: Boolean; |
| 3322 |
begin |
| 3323 |
Result := nil; |
| 3324 |
FlagTemp := 0; |
| 3325 |
FlagParse := FLAG_HASWIDTH; // Tentatively. |
| 3326 |
NBrackets := 0; |
| 3327 |
SavedModifiers := fCompModifiers; |
| 3328 |
|
| 3329 |
// Make an OP_OPEN node, if parenthesized. |
| 3330 |
ret := nil; |
| 3331 |
if InBrackets then |
| 3332 |
begin |
| 3333 |
if BracketCounter <> nil then begin |
| 3334 |
if BracketCounter^ >= RegexMaxMaxGroups then |
| 3335 |
begin |
| 3336 |
Error(reeCompParseRegTooManyBrackets); |
| 3337 |
Exit; |
| 3338 |
end; |
| 3339 |
NBrackets := BracketCounter^; |
| 3340 |
Inc(BracketCounter^); |
| 3341 |
if BeginGroupOp <> OP_NONE then |
| 3342 |
ret := EmitNodeWithGroupIndex(BeginGroupOp, NBrackets); |
| 3343 |
if fSecondPass and (BracketCounter = @regNumBrackets) then |
| 3344 |
GrpOpCodes[NBrackets] := ret; |
| 3345 |
end |
| 3346 |
else |
| 3347 |
if BeginGroupOp <> OP_NONE then |
| 3348 |
ret := EmitNode(BeginGroupOp); |
| 3349 |
end; |
| 3350 |
|
| 3351 |
// Pick up the branches, linking them together. |
| 3352 |
br := ParseBranch(FlagTemp); |
| 3353 |
brStart := br; |
| 3354 |
if br = nil then |
| 3355 |
begin |
| 3356 |
Result := nil; |
| 3357 |
Exit; |
| 3358 |
end; |
| 3359 |
if ret <> nil then |
| 3360 |
Tail(ret, br) // OP_OPEN -> first. |
| 3361 |
else |
| 3362 |
ret := br; |
| 3363 |
if (FlagTemp and FLAG_HASWIDTH) = 0 then |
| 3364 |
FlagParse := FlagParse and not FLAG_HASWIDTH; |
| 3365 |
FlagParse := FlagParse or FlagTemp and (FLAG_SPECSTART or FLAG_LOOP or FLAG_GREEDY); |
| 3366 |
HasGBranch := False; |
| 3367 |
HasChoice := regParse^ = '|'; |
| 3368 |
while (regParse^ = '|') do |
| 3369 |
begin |
| 3370 |
Inc(regParse); |
| 3371 |
br := ParseBranch(FlagTemp); |
| 3372 |
if br = nil then |
| 3373 |
begin |
| 3374 |
Result := nil; |
| 3375 |
Exit; |
| 3376 |
end; |
| 3377 |
if br^ <> OP_BRANCH then |
| 3378 |
HasGBranch := True; |
| 3379 |
Tail(ret, br); // OP_BRANCH -> OP_BRANCH. |
| 3380 |
if (FlagTemp and FLAG_HASWIDTH) = 0 then |
| 3381 |
FlagParse := FlagParse and not FLAG_HASWIDTH; |
| 3382 |
FlagParse := FlagParse or FlagTemp and (FLAG_SPECSTART or FLAG_LOOP or FLAG_GREEDY); |
| 3383 |
end; |
| 3384 |
if fSecondPass then begin |
| 3385 |
if HasGBranch then begin |
| 3386 |
if brStart^ = OP_BRANCH then |
| 3387 |
brStart^ := OP_GBRANCH; |
| 3388 |
end |
| 3389 |
else |
| 3390 |
if not HasChoice then |
| 3391 |
RemoveOperator(brStart, REOpSz + RENextOffSz + REBranchArgSz); |
| 3392 |
end; |
| 3393 |
|
| 3394 |
// Make a closing node, and hook it on the end. |
| 3395 |
if InBrackets and (EndGroupOP <> OP_NONE) then begin |
| 3396 |
if BracketCounter <> nil then |
| 3397 |
ender := EmitNodeWithGroupIndex(EndGroupOP, NBrackets) |
| 3398 |
else |
| 3399 |
ender := EmitNode(EndGroupOP); |
| 3400 |
end |
| 3401 |
else |
| 3402 |
if (EndGroupOP = OP_NONE) then begin |
| 3403 |
if HasChoice then |
| 3404 |
ender := EmitNode(OP_COMMENT) // need something to hook the branches' tails too |
| 3405 |
else |
| 3406 |
ender := nil; |
| 3407 |
end |
| 3408 |
else |
| 3409 |
ender := EmitNode(OP_EEND); |
| 3410 |
|
| 3411 |
if ender <> nil then begin |
| 3412 |
Tail(ret, ender); |
| 3413 |
|
| 3414 |
// Hook the tails of the branches to the closing node. |
| 3415 |
br := ret; |
| 3416 |
while br <> nil do |
| 3417 |
begin |
| 3418 |
OpTail(br, ender); |
| 3419 |
br := regNext(br); |
| 3420 |
end; |
| 3421 |
end; |
| 3422 |
|
| 3423 |
// Check for proper termination. |
| 3424 |
if InBrackets then |
| 3425 |
if regParse^ <> ')' then |
| 3426 |
begin |
| 3427 |
Error(reeCompParseRegUnmatchedBrackets); |
| 3428 |
Exit; |
| 3429 |
end |
| 3430 |
else |
| 3431 |
Inc(regParse); // skip trailing ')' |
| 3432 |
if (not InBrackets) and (regParse < fRegexEnd) then |
| 3433 |
begin |
| 3434 |
if regParse^ = ')' then |
| 3435 |
Error(reeCompParseRegUnmatchedBrackets2) |
| 3436 |
else |
| 3437 |
Error(reeCompParseRegJunkOnEnd); |
| 3438 |
Exit; |
| 3439 |
end; |
| 3440 |
fCompModifiers := SavedModifiers; // restore modifiers of parent |
| 3441 |
Result := ret; |
| 3442 |
end; { of function TRegExpr.ParseReg |
| 3443 |
-------------------------------------------------------------- } |
| 3444 |
|
| 3445 |
function TRegExpr.ParseBranch(var FlagParse: Integer): PRegExprChar; |
| 3446 |
// one alternative of an | operator |
| 3447 |
// Implements the concatenation operator. |
| 3448 |
var |
| 3449 |
ret, chain, latest: PRegExprChar; |
| 3450 |
FlagTemp: Integer; |
| 3451 |
begin |
| 3452 |
FlagTemp := 0; |
| 3453 |
FlagParse := FLAG_WORST; // Tentatively. |
| 3454 |
|
| 3455 |
ret := EmitBranch; |
| 3456 |
chain := nil; |
| 3457 |
while (regParse < fRegexEnd) and (regParse^ <> '|') and (regParse^ <> ')') do |
| 3458 |
begin |
| 3459 |
latest := ParsePiece(FlagTemp); |
| 3460 |
if latest = nil then |
| 3461 |
begin |
| 3462 |
Result := nil; |
| 3463 |
Exit; |
| 3464 |
end; |
| 3465 |
if fSecondPass and |
| 3466 |
(latest <> nil) and (latest^ = OP_COMMENT) and |
| 3467 |
( ((regParse < fRegexEnd) and (regParse^ <> '|') and (regParse^ <> ')')) or |
| 3468 |
(chain <> nil) |
| 3469 |
) |
| 3470 |
then begin |
| 3471 |
regCode := latest; |
| 3472 |
continue; |
| 3473 |
end; |
| 3474 |
|
| 3475 |
FlagParse := FlagParse or FlagTemp and (FLAG_HASWIDTH or FLAG_LOOP or FLAG_GREEDY); |
| 3476 |
if chain = nil // First piece. |
| 3477 |
then begin |
| 3478 |
FlagParse := FlagParse or FlagTemp and FLAG_SPECSTART; |
| 3479 |
MaybeGuardBranchPiece(ret); |
| 3480 |
end |
| 3481 |
else |
| 3482 |
Tail(chain, latest); |
| 3483 |
chain := latest; |
| 3484 |
end; |
| 3485 |
if chain = nil // Loop ran zero times. |
| 3486 |
then |
| 3487 |
EmitNode(OP_NOTHING); |
| 3488 |
Result := ret; |
| 3489 |
end; { of function TRegExpr.ParseBranch |
| 3490 |
-------------------------------------------------------------- } |
| 3491 |
|
| 3492 |
procedure TRegExpr.MaybeGuardBranchPiece(piece: PRegExprChar); |
| 3493 |
var |
| 3494 |
opnd: PRegExprChar; |
| 3495 |
ch: REChar; |
| 3496 |
begin |
| 3497 |
if not fSecondPass then |
| 3498 |
exit; |
| 3499 |
|
| 3500 |
opnd := piece + REOpSz + RENextOffSz + REBranchArgSz; |
| 3501 |
while opnd <> nil do begin |
| 3502 |
case opnd^ of |
| 3503 |
OP_OPEN, OP_OPEN_ATOMIC, OP_CLOSE, OP_CLOSE_ATOMIC, |
| 3504 |
OP_COMMENT, |
| 3505 |
OP_BOL, OP_CONTINUE_POS, OP_RESET_MATCHPOS, |
| 3506 |
OP_BOUND, OP_NOTBOUND, |
| 3507 |
OP_BACK: |
| 3508 |
opnd := regNext(opnd); |
| 3509 |
OP_PLUS, OP_PLUS_NG, OP_PLUS_POSS: |
| 3510 |
opnd := opnd + REOpSz + RENextOffSz; |
| 3511 |
OP_BRACES, OP_BRACES_NG, OP_BRACES_POSS: |
| 3512 |
begin |
| 3513 |
if PREBracesArg(AlignToPtr(opnd + REOpSz + RENextOffSz))^ >= 1 then |
| 3514 |
opnd := opnd + REOpSz + RENextOffSz + 2*REBracesArgSz; |
| 3515 |
break; |
| 3516 |
end; |
| 3517 |
OP_LOOPENTRY: |
| 3518 |
begin |
| 3519 |
if PREBracesArg(AlignToInt(regNext(opnd) + REOpSz + RENextOffSz))^ >= 1 then |
| 3520 |
opnd := opnd + REOpSz + RENextOffSz; |
| 3521 |
break; |
| 3522 |
end; |
| 3523 |
OP_LOOKAHEAD: // could contain OP_OPEN.... |
| 3524 |
begin |
| 3525 |
if ( ((opnd + 1 + RENextOffSz)^ = OP_EXACTLY) or |
| 3526 |
((opnd + 1 + RENextOffSz)^ = OP_EXACTLY_CI) |
| 3527 |
) |
| 3528 |
then begin |
| 3529 |
opnd := (opnd + 1 + RENextOffSz); |
| 3530 |
break; |
| 3531 |
end |
| 3532 |
else |
| 3533 |
opnd := regNext(regNext(opnd)); |
| 3534 |
end; |
| 3535 |
OP_LOOKAHEAD_NEG, OP_LOOKBEHIND, OP_LOOKBEHIND_NEG: |
| 3536 |
opnd := regNext(regNext(opnd)); |
| 3537 |
else |
| 3538 |
break; |
| 3539 |
end; |
| 3540 |
end; |
| 3541 |
if opnd <> nil then |
| 3542 |
case opnd^ of |
| 3543 |
OP_EXACTLY: begin |
| 3544 |
piece^ := OP_GBRANCH_EX; |
| 3545 |
ch := (opnd + REOpSz + RENextOffSz + RENumberSz)^; |
| 3546 |
(piece + REOpSz + RENextOffSz)^ := ch; |
| 3547 |
end; |
| 3548 |
OP_EXACTLY_CI: begin |
| 3549 |
piece^ := OP_GBRANCH_EX_CI; |
| 3550 |
ch := (opnd + REOpSz + RENextOffSz + RENumberSz)^; |
| 3551 |
(piece + REOpSz + RENextOffSz)^ := _UpperCase(ch); |
| 3552 |
(piece + REOpSz + RENextOffSz + 1)^ := _LowerCase(ch); |
| 3553 |
end; |
| 3554 |
end; |
| 3555 |
end; |
| 3556 |
|
| 3557 |
function TRegExpr.ParsePiece(var FlagParse: Integer): PRegExprChar; |
| 3558 |
// something followed by possible [*+?{] |
| 3559 |
// Note that the branching code sequences used for ? and the general cases |
| 3560 |
// of * and + and { are somewhat optimized: they use the same OP_NOTHING node as |
| 3561 |
// both the endmarker for their branch list and the body of the last branch. |
| 3562 |
// It might seem that this node could be dispensed with entirely, but the |
| 3563 |
// endmarker role is not redundant. |
| 3564 |
|
| 3565 |
function ParseNumber(AStart, AEnd: PRegExprChar): TREBracesArg; |
| 3566 |
begin |
| 3567 |
Result := 0; |
| 3568 |
if AEnd - AStart + 1 > 8 then |
| 3569 |
begin // prevent stupid scanning |
| 3570 |
Error(reeBRACESArgTooBig); |
| 3571 |
Exit; |
| 3572 |
end; |
| 3573 |
while AStart <= AEnd do |
| 3574 |
begin |
| 3575 |
Result := Result * 10 + (Ord(AStart^) - Ord('0')); |
| 3576 |
Inc(AStart); |
| 3577 |
end; |
| 3578 |
if (Result > MaxBracesArg) or (Result < 0) then |
| 3579 |
begin |
| 3580 |
Error(reeBRACESArgTooBig); |
| 3581 |
Exit; |
| 3582 |
end; |
| 3583 |
end; |
| 3584 |
|
| 3585 |
var |
| 3586 |
TheOp: TREOp; |
| 3587 |
NextNode: PRegExprChar; |
| 3588 |
|
| 3589 |
procedure EmitComplexBraces(ABracesMin, ABracesMax: TREBracesArg; ANonGreedyOp, APossesive: boolean); |
| 3590 |
{$IFDEF ComplexBraces} |
| 3591 |
var |
| 3592 |
off: TRENextOff; |
| 3593 |
{$ENDIF} |
| 3594 |
begin |
| 3595 |
{$IFNDEF ComplexBraces} |
| 3596 |
Error(reeComplexBracesNotImplemented); |
| 3597 |
{$ELSE} |
| 3598 |
if APossesive then |
| 3599 |
TheOp := OP_LOOP_POSS |
| 3600 |
else |
| 3601 |
if ANonGreedyOp then |
| 3602 |
TheOp := OP_LOOP_NG |
| 3603 |
else |
| 3604 |
TheOp := OP_LOOP; |
| 3605 |
InsertOperator(OP_LOOPENTRY, Result, REOpSz + RENextOffSz); |
| 3606 |
NextNode := EmitNode(TheOp); |
| 3607 |
if regCode <> @regDummy then |
| 3608 |
begin |
| 3609 |
off := (Result + REOpSz + RENextOffSz) - (regCode - REOpSz - RENextOffSz); |
| 3610 |
// back to Atom after OP_LOOPENTRY |
| 3611 |
PREBracesArg(AlignToInt(regCode))^ := ABracesMin; |
| 3612 |
Inc(regCode, REBracesArgSz); |
| 3613 |
PREBracesArg(AlignToInt(regCode))^ := ABracesMax; |
| 3614 |
Inc(regCode, REBracesArgSz); |
| 3615 |
PRENextOff(AlignToPtr(regCode))^ := off; |
| 3616 |
Inc(regCode, RENextOffSz); |
| 3617 |
{$IFDEF DebugSynRegExpr} |
| 3618 |
if regcode - programm > regCodeSize then |
| 3619 |
raise Exception.Create |
| 3620 |
('TRegExpr.ParsePiece.EmitComplexBraces buffer overrun'); |
| 3621 |
{$ENDIF} |
| 3622 |
end |
| 3623 |
else |
| 3624 |
Inc(regCodeSize, REBracesArgSz * 2 + RENextOffSz); |
| 3625 |
Tail(Result, NextNode); // OP_LOOPENTRY -> OP_LOOP |
| 3626 |
if regCode <> @regDummy then |
| 3627 |
Tail(Result + REOpSz + RENextOffSz, NextNode); // Atom -> OP_LOOP |
| 3628 |
{$ENDIF} |
| 3629 |
end; |
| 3630 |
|
| 3631 |
procedure EmitSimpleBraces(ABracesMin, ABracesMax: TREBracesArg; ANonGreedyOp, APossessive: Boolean); |
| 3632 |
begin |
| 3633 |
if APossessive then |
| 3634 |
TheOp := OP_BRACES_POSS |
| 3635 |
else |
| 3636 |
if ANonGreedyOp then |
| 3637 |
TheOp := OP_BRACES_NG |
| 3638 |
else |
| 3639 |
TheOp := OP_BRACES; |
| 3640 |
InsertOperator(TheOp, Result, REOpSz + RENextOffSz + REBracesArgSz * 2); |
| 3641 |
if regCode <> @regDummy then |
| 3642 |
begin |
| 3643 |
PREBracesArg(AlignToInt(Result + REOpSz + RENextOffSz))^ := ABracesMin; |
| 3644 |
PREBracesArg(AlignToInt(Result + REOpSz + RENextOffSz + REBracesArgSz))^ := ABracesMax; |
| 3645 |
end; |
| 3646 |
end; |
| 3647 |
|
| 3648 |
function DoParseBraceMinMax(var BMin, BMax: TREBracesArg): Boolean; |
| 3649 |
var |
| 3650 |
p: PRegExprChar; |
| 3651 |
begin |
| 3652 |
Result := False; |
| 3653 |
p := regParse; |
| 3654 |
while IsDigitChar(regParse^) do // <min> MUST appear |
| 3655 |
Inc(regParse); |
| 3656 |
if FAllowBraceWithoutMin and (regParse^ = ',') and (p = regParse) then |
| 3657 |
begin |
| 3658 |
if not (((regParse+1)^ >= '0') and ((regParse+1)^ <= '9')) then |
| 3659 |
Exit; |
| 3660 |
BMin := 0 |
| 3661 |
end |
| 3662 |
else |
| 3663 |
if (regParse^ <> '}') and (regParse^ <> ',') or (p = regParse) then |
| 3664 |
begin |
| 3665 |
if not FAllowLiteralBraceWithoutRange then |
| 3666 |
Error(reeIncorrectBraces); |
| 3667 |
Exit; |
| 3668 |
end |
| 3669 |
else |
| 3670 |
BMin := ParseNumber(p, regParse - 1); |
| 3671 |
if regParse^ = ',' then |
| 3672 |
begin |
| 3673 |
Inc(regParse); |
| 3674 |
p := regParse; |
| 3675 |
while IsDigitChar(regParse^) do |
| 3676 |
Inc(regParse); |
| 3677 |
if regParse^ <> '}' then |
| 3678 |
begin |
| 3679 |
if not FAllowLiteralBraceWithoutRange then |
| 3680 |
Error(reeIncorrectBraces); |
| 3681 |
Exit; |
| 3682 |
end; |
| 3683 |
if p = regParse then |
| 3684 |
BMax := MaxBracesArg |
| 3685 |
else |
| 3686 |
BMax := ParseNumber(p, regParse - 1); |
| 3687 |
end |
| 3688 |
else |
| 3689 |
BMax := BMin; // {n} == {n,n} |
| 3690 |
Result := True; |
| 3691 |
end; |
| 3692 |
|
| 3693 |
function ParseBraceMinMax(var BMin, BMax: TREBracesArg): Boolean; |
| 3694 |
begin |
| 3695 |
Result := DoParseBraceMinMax(BMin, BMax); |
| 3696 |
if Result and (BMin > BMax) then |
| 3697 |
begin |
| 3698 |
Error(reeBracesMinParamGreaterMax); |
| 3699 |
Exit; |
| 3700 |
end; |
| 3701 |
end; |
| 3702 |
|
| 3703 |
function CheckBraceIsLiteral: Boolean; |
| 3704 |
var |
| 3705 |
dummyBracesMin, dummyBracesMax: TREBracesArg; |
| 3706 |
savedRegParse: PRegExprChar; |
| 3707 |
begin |
| 3708 |
Result := False; |
| 3709 |
if not FAllowLiteralBraceWithoutRange then |
| 3710 |
exit; |
| 3711 |
savedRegParse := regParse; |
| 3712 |
Inc(regParse); |
| 3713 |
Result := not DoParseBraceMinMax(dummyBracesMin, dummyBracesMax); |
| 3714 |
regParse := savedRegParse; |
| 3715 |
end; |
| 3716 |
|
| 3717 |
var |
| 3718 |
op, nextch: REChar; |
| 3719 |
NonGreedyOp, NonGreedyCh, PossessiveCh: Boolean; |
| 3720 |
FlagTemp: Integer; |
| 3721 |
BracesMin, BracesMax: TREBracesArg; |
| 3722 |
savedRegParse: PRegExprChar; |
| 3723 |
begin |
| 3724 |
FlagTemp := 0; |
| 3725 |
Result := ParseAtom(FlagTemp); |
| 3726 |
if Result = nil then |
| 3727 |
Exit; |
| 3728 |
|
| 3729 |
op := regParse^; |
| 3730 |
if not ((op = '*') or (op = '+') or (op = '?') or (op = '{')) then |
| 3731 |
begin |
| 3732 |
FlagParse := FlagTemp; |
| 3733 |
Exit; |
| 3734 |
end; |
| 3735 |
|
| 3736 |
case op of |
| 3737 |
'*': |
| 3738 |
begin |
| 3739 |
if (FlagTemp and FLAG_NOT_QUANTIFIABLE) <> 0 then begin |
| 3740 |
Error(reeNotQuantifiable); |
| 3741 |
exit; |
| 3742 |
end; |
| 3743 |
FlagParse := FLAG_WORST or FLAG_SPECSTART or FLAG_LOOP; |
| 3744 |
nextch := (regParse + 1)^; |
| 3745 |
PossessiveCh := nextch = '+'; |
| 3746 |
if PossessiveCh then |
| 3747 |
begin |
| 3748 |
NonGreedyCh := False; |
| 3749 |
NonGreedyOp := False; |
| 3750 |
end |
| 3751 |
else |
| 3752 |
begin |
| 3753 |
NonGreedyCh := nextch = '?'; |
| 3754 |
NonGreedyOp := NonGreedyCh or not fCompModifiers.G; |
| 3755 |
end; |
| 3756 |
if not NonGreedyCh then |
| 3757 |
FlagParse := FlagParse or FLAG_GREEDY; |
| 3758 |
if (FlagTemp and (FLAG_SIMPLE or FLAG_HASWIDTH)) <> (FLAG_SIMPLE or FLAG_HASWIDTH) then |
| 3759 |
begin |
| 3760 |
if NonGreedyOp or PossessiveCh or ((FlagTemp and FLAG_HASWIDTH) = 0) then |
| 3761 |
EmitComplexBraces(0, MaxBracesArg, NonGreedyOp, PossessiveCh) |
| 3762 |
else |
| 3763 |
begin |
| 3764 |
// Too complex for OP_STAR. Write loop using OP_BRANCH and OP_BACK. |
| 3765 |
// 1: OP_BRANCH with 2 branches - to allow backtracking |
| 3766 |
// 1st choice: loop-content |
| 3767 |
// OP_BACK back to the branch |
| 3768 |
// execute another iteration of the branch, so each can backtrack |
| 3769 |
// 2nd choice: OP_NOTHING to exit |
| 3770 |
InsertOperator(OP_BRANCH, Result, REOpSz + RENextOffSz + REBranchArgSz); |
| 3771 |
OpTail(Result, EmitNode(OP_BACK)); |
| 3772 |
OpTail(Result, Result); |
| 3773 |
Tail(Result, EmitBranch); |
| 3774 |
Tail(Result, EmitNode(OP_NOTHING)); |
| 3775 |
MaybeGuardBranchPiece(Result); |
| 3776 |
end |
| 3777 |
end |
| 3778 |
else |
| 3779 |
begin // Simple AND has Width |
| 3780 |
if PossessiveCh then |
| 3781 |
TheOp := OP_STAR_POSS |
| 3782 |
else |
| 3783 |
if NonGreedyOp then |
| 3784 |
TheOp := OP_STAR_NG |
| 3785 |
else |
| 3786 |
TheOp := OP_STAR; |
| 3787 |
InsertOperator(TheOp, Result, REOpSz + RENextOffSz); |
| 3788 |
end; |
| 3789 |
if NonGreedyCh or PossessiveCh then |
| 3790 |
Inc(regParse); // Skip extra char ('?') |
| 3791 |
end; { of case '*' } |
| 3792 |
'+': |
| 3793 |
begin |
| 3794 |
if (FlagTemp and FLAG_NOT_QUANTIFIABLE) <> 0 then begin |
| 3795 |
Error(reeNotQuantifiable); |
| 3796 |
exit; |
| 3797 |
end; |
| 3798 |
FlagParse := FLAG_WORST or FLAG_SPECSTART or (FlagTemp and FLAG_HASWIDTH) or FLAG_LOOP; |
| 3799 |
nextch := (regParse + 1)^; |
| 3800 |
PossessiveCh := nextch = '+'; |
| 3801 |
if PossessiveCh then |
| 3802 |
begin |
| 3803 |
NonGreedyCh := False; |
| 3804 |
NonGreedyOp := False; |
| 3805 |
end |
| 3806 |
else |
| 3807 |
begin |
| 3808 |
NonGreedyCh := nextch = '?'; |
| 3809 |
NonGreedyOp := NonGreedyCh or not fCompModifiers.G; |
| 3810 |
end; |
| 3811 |
if not NonGreedyCh then |
| 3812 |
FlagParse := FlagParse or FLAG_GREEDY; |
| 3813 |
if (FlagTemp and (FLAG_SIMPLE or FLAG_HASWIDTH)) <> (FLAG_SIMPLE or FLAG_HASWIDTH) then |
| 3814 |
begin |
| 3815 |
if NonGreedyOp or PossessiveCh or ((FlagTemp and FLAG_HASWIDTH) = 0) then |
| 3816 |
EmitComplexBraces(1, MaxBracesArg, NonGreedyOp, PossessiveCh) |
| 3817 |
else |
| 3818 |
begin |
| 3819 |
// Too complex for OP_PLUS. Write loop using OP_BRANCH and OP_BACK. |
| 3820 |
// 1: loop-content |
| 3821 |
// 2: OP_BRANCH with 2 choices - to allow backtracking |
| 3822 |
// 2a: OP_BACK(1) to match the loop again (goto back, include another iteration of the branch in this choice) |
| 3823 |
// 2b: OP_NOTHING to exit, if the loop can match no more (branch 2a did not match) |
| 3824 |
NextNode := EmitBranch; |
| 3825 |
Tail(Result, NextNode); |
| 3826 |
Tail(EmitNode(OP_BACK), Result); |
| 3827 |
Tail(NextNode, EmitBranch); |
| 3828 |
Tail(Result, EmitNode(OP_NOTHING)); |
| 3829 |
MaybeGuardBranchPiece(NextNode); |
| 3830 |
end |
| 3831 |
end |
| 3832 |
else |
| 3833 |
begin // Simple |
| 3834 |
if PossessiveCh then |
| 3835 |
TheOp := OP_PLUS_POSS |
| 3836 |
else |
| 3837 |
if NonGreedyOp then |
| 3838 |
TheOp := OP_PLUS_NG |
| 3839 |
else |
| 3840 |
TheOp := OP_PLUS; |
| 3841 |
InsertOperator(TheOp, Result, REOpSz + RENextOffSz); |
| 3842 |
end; |
| 3843 |
if NonGreedyCh or PossessiveCh then |
| 3844 |
Inc(regParse); // Skip extra char ('?') |
| 3845 |
end; { of case '+' } |
| 3846 |
'?': |
| 3847 |
begin |
| 3848 |
FlagParse := FLAG_WORST; |
| 3849 |
nextch := (regParse + 1)^; |
| 3850 |
PossessiveCh := nextch = '+'; |
| 3851 |
if PossessiveCh then |
| 3852 |
begin |
| 3853 |
NonGreedyCh := False; |
| 3854 |
NonGreedyOp := False; |
| 3855 |
end |
| 3856 |
else |
| 3857 |
begin |
| 3858 |
NonGreedyCh := nextch = '?'; |
| 3859 |
NonGreedyOp := NonGreedyCh or not fCompModifiers.G; |
| 3860 |
end; |
| 3861 |
if not NonGreedyCh then |
| 3862 |
FlagParse := FlagParse or FLAG_GREEDY; |
| 3863 |
if NonGreedyOp or PossessiveCh then |
| 3864 |
begin // We emit x?? as x{0,1}? |
| 3865 |
if (FlagTemp and FLAG_SIMPLE) = 0 then |
| 3866 |
begin |
| 3867 |
EmitComplexBraces(0, 1, NonGreedyOp, PossessiveCh); |
| 3868 |
end |
| 3869 |
else |
| 3870 |
EmitSimpleBraces(0, 1, NonGreedyOp, PossessiveCh); |
| 3871 |
end |
| 3872 |
else |
| 3873 |
begin // greedy '?' |
| 3874 |
InsertOperator(OP_BRANCH, Result, REOpSz + RENextOffSz + REBranchArgSz); // Either x |
| 3875 |
Tail(Result, EmitBranch); // or |
| 3876 |
NextNode := EmitNode(OP_NOTHING); // nil. |
| 3877 |
Tail(Result, NextNode); |
| 3878 |
OpTail(Result, NextNode); |
| 3879 |
MaybeGuardBranchPiece(Result); |
| 3880 |
end; |
| 3881 |
if NonGreedyCh or PossessiveCh then |
| 3882 |
Inc(regParse); // Skip extra char ('?') |
| 3883 |
end; { of case '?' } |
| 3884 |
'{': |
| 3885 |
begin |
| 3886 |
savedRegParse := regParse; |
| 3887 |
Inc(regParse); |
| 3888 |
if not ParseBraceMinMax(BracesMin, BracesMax) then |
| 3889 |
begin |
| 3890 |
regParse := savedRegParse; |
| 3891 |
Exit; |
| 3892 |
end; |
| 3893 |
if (FlagTemp and FLAG_NOT_QUANTIFIABLE) <> 0 then begin |
| 3894 |
Error(reeNotQuantifiable); |
| 3895 |
exit; |
| 3896 |
end; |
| 3897 |
if BracesMin > 0 then |
| 3898 |
FlagParse := FLAG_WORST or (FlagTemp and FLAG_HASWIDTH); |
| 3899 |
if BracesMax > 0 then |
| 3900 |
FlagParse := FlagParse or FLAG_SPECSTART; |
| 3901 |
|
| 3902 |
nextch := (regParse + 1)^; |
| 3903 |
PossessiveCh := nextch = '+'; |
| 3904 |
if PossessiveCh then |
| 3905 |
begin |
| 3906 |
NonGreedyCh := False; |
| 3907 |
NonGreedyOp := False; |
| 3908 |
end |
| 3909 |
else |
| 3910 |
begin |
| 3911 |
NonGreedyCh := nextch = '?'; |
| 3912 |
NonGreedyOp := NonGreedyCh or not fCompModifiers.G; |
| 3913 |
end; |
| 3914 |
if not NonGreedyCh then |
| 3915 |
FlagParse := FlagParse or FLAG_GREEDY; |
| 3916 |
if BracesMax >= 2 then |
| 3917 |
FlagParse := FlagParse or FLAG_LOOP; |
| 3918 |
if (FlagTemp and (FLAG_SIMPLE or FLAG_HASWIDTH)) = (FLAG_SIMPLE or FLAG_HASWIDTH) then |
| 3919 |
EmitSimpleBraces(BracesMin, BracesMax, NonGreedyOp, PossessiveCh) |
| 3920 |
else |
| 3921 |
begin |
| 3922 |
EmitComplexBraces(BracesMin, BracesMax, NonGreedyOp, PossessiveCh); |
| 3923 |
end; |
| 3924 |
if NonGreedyCh or PossessiveCh then |
| 3925 |
Inc(regParse); // Skip extra char '?' |
| 3926 |
end; // of case '{' |
| 3927 |
// else // here we can't be |
| 3928 |
end; { of case op } |
| 3929 |
|
| 3930 |
FlagParse := FlagParse or FlagTemp and (FLAG_LOOP or FLAG_GREEDY); |
| 3931 |
Inc(regParse); |
| 3932 |
op := regParse^; |
| 3933 |
if (op = '*') or (op = '+') or (op = '?') or |
| 3934 |
( (op = '{') and not CheckBraceIsLiteral) |
| 3935 |
then |
| 3936 |
Error(reeNestedQuantif); |
| 3937 |
end; { of function TRegExpr.ParsePiece |
| 3938 |
-------------------------------------------------------------- } |
| 3939 |
|
| 3940 |
function TRegExpr.HexDig(Ch: REChar): Integer; |
| 3941 |
begin |
| 3942 |
case Ch of |
| 3943 |
'0' .. '9': |
| 3944 |
Result := Ord(Ch) - Ord('0'); |
| 3945 |
'a' .. 'f': |
| 3946 |
Result := Ord(Ch) - Ord('a') + 10; |
| 3947 |
'A' .. 'F': |
| 3948 |
Result := Ord(Ch) - Ord('A') + 10; |
| 3949 |
else |
| 3950 |
Result := 0; |
| 3951 |
Error(reeBadHexDigit); |
| 3952 |
end; |
| 3953 |
end; |
| 3954 |
|
| 3955 |
function TRegExpr.UnQuoteChar(var APtr, AEnd: PRegExprChar): REChar; |
| 3956 |
var |
| 3957 |
Ch: REChar; |
| 3958 |
begin |
| 3959 |
case APtr^ of |
| 3960 |
't': |
| 3961 |
Result := #$9; // \t => tab (HT/TAB) |
| 3962 |
'n': |
| 3963 |
Result := #$a; // \n => newline (NL) |
| 3964 |
'r': |
| 3965 |
Result := #$d; // \r => carriage return (CR) |
| 3966 |
'f': |
| 3967 |
Result := #$c; // \f => form feed (FF) |
| 3968 |
'a': |
| 3969 |
Result := #$7; // \a => alarm (bell) (BEL) |
| 3970 |
'e': |
| 3971 |
Result := #$1b; // \e => escape (ESC) |
| 3972 |
'c': |
| 3973 |
begin // \cK => code for Ctrl+K |
| 3974 |
Result := #0; |
| 3975 |
Inc(APtr); |
| 3976 |
if APtr >= AEnd then |
| 3977 |
Error(reeNoLetterAfterBSlashC); |
| 3978 |
Ch := APtr^; |
| 3979 |
case Ch of |
| 3980 |
'a' .. 'z': |
| 3981 |
Result := REChar(Ord(Ch) - Ord('a') + 1); |
| 3982 |
'A' .. 'Z': |
| 3983 |
Result := REChar(Ord(Ch) - Ord('A') + 1); |
| 3984 |
else |
| 3985 |
Error(reeNoLetterAfterBSlashC); |
| 3986 |
end; |
| 3987 |
end; |
| 3988 |
'x': |
| 3989 |
begin // \x: hex char |
| 3990 |
Result := #0; |
| 3991 |
Inc(APtr); |
| 3992 |
if APtr >= AEnd then |
| 3993 |
begin |
| 3994 |
Error(reeNoHexCodeAfterBSlashX); |
| 3995 |
Exit; |
| 3996 |
end; |
| 3997 |
if APtr^ = '{' then |
| 3998 |
begin // \x{nnnn} |
| 3999 |
repeat |
| 4000 |
Inc(APtr); |
| 4001 |
if APtr >= AEnd then |
| 4002 |
begin |
| 4003 |
Error(reeNoHexCodeAfterBSlashX); |
| 4004 |
Exit; |
| 4005 |
end; |
| 4006 |
if APtr^ <> '}' then |
| 4007 |
begin |
| 4008 |
if (Ord(Result) ShR (SizeOf(REChar) * 8 - 4)) and $F <> 0 then |
| 4009 |
begin |
| 4010 |
Error(reeHexCodeAfterBSlashXTooBig); |
| 4011 |
Exit; |
| 4012 |
end; |
| 4013 |
Result := REChar((Ord(Result) ShL 4) or HexDig(APtr^)); |
| 4014 |
// HexDig will cause Error if bad hex digit found |
| 4015 |
end |
| 4016 |
else |
| 4017 |
Break; |
| 4018 |
until False; |
| 4019 |
end |
| 4020 |
else |
| 4021 |
begin |
| 4022 |
Result := REChar(HexDig(APtr^)); |
| 4023 |
// HexDig will cause Error if bad hex digit found |
| 4024 |
Inc(APtr); |
| 4025 |
if APtr >= AEnd then |
| 4026 |
begin |
| 4027 |
Error(reeNoHexCodeAfterBSlashX); |
| 4028 |
Exit; |
| 4029 |
end; |
| 4030 |
Result := REChar((Ord(Result) ShL 4) or HexDig(APtr^)); |
| 4031 |
// HexDig will cause Error if bad hex digit found |
| 4032 |
end; |
| 4033 |
end; |
| 4034 |
else |
| 4035 |
Result := APtr^; |
| 4036 |
if (Result <> '_') and IsWordChar(Result) then |
| 4037 |
begin |
| 4038 |
fLastErrorSymbol := Result; |
| 4039 |
Error(reeUnknownMetaSymbol); |
| 4040 |
end; |
| 4041 |
end; |
| 4042 |
end; |
| 4043 |
|
| 4044 |
function TRegExpr.ParseAtom(var FlagParse: Integer): PRegExprChar; |
| 4045 |
// the lowest level |
| 4046 |
// Optimization: gobbles an entire sequence of ordinary characters so that |
| 4047 |
// it can turn them into a single node, which is smaller to store and |
| 4048 |
// faster to run. Backslashed characters are exceptions, each becoming a |
| 4049 |
// separate node; the code is simpler that way and it's not worth fixing. |
| 4050 |
var |
| 4051 |
ret, ret2, regLookBehindOption: PRegExprChar; |
| 4052 |
RangeBeg, RangeEnd: REChar; |
| 4053 |
CanBeRange: Boolean; |
| 4054 |
AddrOfLen: PLongInt; |
| 4055 |
HasCaseSenseChars: boolean; |
| 4056 |
|
| 4057 |
function ParseNumber(var AParsePos: PRegExprChar; out ANumber: Integer): Boolean; |
| 4058 |
begin |
| 4059 |
Result := False; |
| 4060 |
ANumber := 0; |
| 4061 |
while (AParsePos^ >= '0') and (AParsePos^ <= '9') do |
| 4062 |
begin |
| 4063 |
if ANumber > (High(ANumber)-10) div 10 then |
| 4064 |
exit; |
| 4065 |
ANumber := ANumber * 10 + (Ord(AParsePos^) - Ord('0')); |
| 4066 |
inc(AParsePos); |
| 4067 |
end; |
| 4068 |
Result := True; |
| 4069 |
end; |
| 4070 |
|
| 4071 |
procedure EmitExactly(Ch: REChar); |
| 4072 |
var |
| 4073 |
cs: Boolean; |
| 4074 |
begin |
| 4075 |
if fCompModifiers.I then |
| 4076 |
ret := EmitNode(OP_EXACTLY_CI) |
| 4077 |
else |
| 4078 |
ret := EmitNode(OP_EXACTLY); |
| 4079 |
EmitInt(1); |
| 4080 |
cs := False; |
| 4081 |
if fCompModifiers.I then begin |
| 4082 |
Ch := _UpperCase(Ch); |
| 4083 |
EmitC(Ch); |
| 4084 |
if Ch <> _LowerCase(Ch) then |
| 4085 |
cs := True; |
| 4086 |
end |
| 4087 |
else |
| 4088 |
EmitC(Ch); |
| 4089 |
if not cs then |
| 4090 |
PREOp(ret)^ := OP_EXACTLY; |
| 4091 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4092 |
end; |
| 4093 |
|
| 4094 |
procedure EmitRangeChar(Ch: REChar; AStartOfRange: Boolean); |
| 4095 |
begin |
| 4096 |
CanBeRange := AStartOfRange; |
| 4097 |
if fCompModifiers.I then begin |
| 4098 |
Ch := _UpperCase(Ch); |
| 4099 |
if Ch <> _LowerCase(Ch) then |
| 4100 |
HasCaseSenseChars := True; |
| 4101 |
end; |
| 4102 |
if AStartOfRange then |
| 4103 |
begin |
| 4104 |
AddrOfLen := nil; |
| 4105 |
RangeBeg := Ch; |
| 4106 |
end |
| 4107 |
else |
| 4108 |
begin |
| 4109 |
if AddrOfLen = nil then |
| 4110 |
begin |
| 4111 |
EmitC(OpKind_Char); |
| 4112 |
Pointer(AddrOfLen) := regCode; |
| 4113 |
EmitInt(0); |
| 4114 |
end; |
| 4115 |
Inc(AddrOfLen^); |
| 4116 |
EmitC(Ch); |
| 4117 |
end; |
| 4118 |
end; |
| 4119 |
|
| 4120 |
procedure EmitRangePacked(ch1, ch2: REChar); |
| 4121 |
var |
| 4122 |
ChkIndex: Integer; |
| 4123 |
begin |
| 4124 |
AddrOfLen := nil; |
| 4125 |
CanBeRange := False; |
| 4126 |
|
| 4127 |
if fCompModifiers.I then |
| 4128 |
begin |
| 4129 |
ch1 := _UpperCase(ch1); |
| 4130 |
ch2 := _UpperCase(ch2); |
| 4131 |
if (Ch1 <> _LowerCase(Ch1)) or (Ch2 <> _LowerCase(Ch2)) then |
| 4132 |
HasCaseSenseChars := True; |
| 4133 |
end; |
| 4134 |
|
| 4135 |
for ChkIndex := Low(CharCheckerInfos) to High(CharCheckerInfos) do |
| 4136 |
if (CharCheckerInfos[ChkIndex].CharBegin = ch1) and |
| 4137 |
(CharCheckerInfos[ChkIndex].CharEnd = ch2) then |
| 4138 |
begin |
| 4139 |
EmitC(OpKind_MetaClass); |
| 4140 |
EmitC(REChar(CharCheckerInfos[ChkIndex].CheckerIndex)); |
| 4141 |
Exit; |
| 4142 |
end; |
| 4143 |
|
| 4144 |
EmitC(OpKind_Range); |
| 4145 |
EmitC(ch1); |
| 4146 |
EmitC(ch2); |
| 4147 |
end; |
| 4148 |
|
| 4149 |
{$IFDEF FastUnicodeData} |
| 4150 |
procedure EmitCategoryInCharClass(APositive: Boolean); |
| 4151 |
var |
| 4152 |
ch, ch2: REChar; |
| 4153 |
begin |
| 4154 |
AddrOfLen := nil; |
| 4155 |
CanBeRange := False; |
| 4156 |
Inc(regParse); |
| 4157 |
FindCategoryName(regParse, ch, ch2); |
| 4158 |
if APositive then |
| 4159 |
EmitC(OpKind_CategoryYes) |
| 4160 |
else |
| 4161 |
EmitC(OpKind_CategoryNo); |
| 4162 |
EmitC(ch); |
| 4163 |
EmitC(ch2); |
| 4164 |
end; |
| 4165 |
{$ENDIF} |
| 4166 |
|
| 4167 |
var |
| 4168 |
FlagTemp: Integer; |
| 4169 |
Len: Integer; |
| 4170 |
SavedPtr: PRegExprChar; |
| 4171 |
EnderChar, TempChar: REChar; |
| 4172 |
DashForRange: Boolean; |
| 4173 |
GrpKind: TREGroupKind; |
| 4174 |
GrpName: RegExprString; |
| 4175 |
GrpIndex, ALen, RegGrpCountBefore, AMaxLen: integer; |
| 4176 |
NextCh: REChar; |
| 4177 |
op: TREOp; |
| 4178 |
SavedModifiers: TRegExprModifiers; |
| 4179 |
begin |
| 4180 |
Result := nil; |
| 4181 |
FlagTemp := 0; |
| 4182 |
FlagParse := FLAG_WORST; |
| 4183 |
AddrOfLen := nil; |
| 4184 |
GrpIndex := -1; |
| 4185 |
|
| 4186 |
Inc(regParse); |
| 4187 |
case (regParse - 1)^ of |
| 4188 |
'^': |
| 4189 |
begin |
| 4190 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4191 |
if not fCompModifiers.M |
| 4192 |
{$IFDEF UseLineSep} or (fLineSeparators = '') {$ENDIF} then |
| 4193 |
ret := EmitNode(OP_BOL) |
| 4194 |
else |
| 4195 |
ret := EmitNode(OP_BOL_ML); |
| 4196 |
end; |
| 4197 |
|
| 4198 |
'$': |
| 4199 |
begin |
| 4200 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4201 |
if not fCompModifiers.M |
| 4202 |
{$IFDEF UseLineSep} or (fLineSeparators = '') {$ENDIF} then |
| 4203 |
ret := EmitNode(OP_EOL) |
| 4204 |
else |
| 4205 |
ret := EmitNode(OP_EOL_ML); |
| 4206 |
end; |
| 4207 |
|
| 4208 |
'.': |
| 4209 |
begin |
| 4210 |
if fCompModifiers.S then |
| 4211 |
begin |
| 4212 |
ret := EmitNode(OP_ANY); |
| 4213 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4214 |
end |
| 4215 |
else |
| 4216 |
begin // not /s, so emit [^:LineSeparators:] |
| 4217 |
ret := EmitNode(OP_ANY_ML); |
| 4218 |
FlagParse := FlagParse or FLAG_HASWIDTH; // not so simple ;) |
| 4219 |
end; |
| 4220 |
end; |
| 4221 |
|
| 4222 |
'[': |
| 4223 |
begin |
| 4224 |
HasCaseSenseChars := False; |
| 4225 |
if regParse^ = '^' then |
| 4226 |
begin // Complement of range. |
| 4227 |
if fCompModifiers.I then |
| 4228 |
ret := EmitNode(OP_ANYBUT_CI) |
| 4229 |
else |
| 4230 |
ret := EmitNode(OP_ANYBUT); |
| 4231 |
Inc(regParse); |
| 4232 |
end |
| 4233 |
else if fCompModifiers.I then |
| 4234 |
ret := EmitNode(OP_ANYOF_CI) |
| 4235 |
else |
| 4236 |
ret := EmitNode(OP_ANYOF); |
| 4237 |
|
| 4238 |
CanBeRange := False; |
| 4239 |
|
| 4240 |
if regParse^ = ']' then |
| 4241 |
begin |
| 4242 |
// first ']' inside [] treated as simple char, no need to check '[' |
| 4243 |
EmitRangeChar(regParse^, (regParse + 1)^ = '-'); |
| 4244 |
Inc(regParse); |
| 4245 |
end; |
| 4246 |
|
| 4247 |
while (regParse < fRegexEnd) and (regParse^ <> ']') do |
| 4248 |
begin |
| 4249 |
// last '-' inside [] treated as simple dash |
| 4250 |
if (regParse^ = '-') and |
| 4251 |
((regParse + 1) < fRegexEnd) and |
| 4252 |
((regParse + 1)^ = ']') then |
| 4253 |
begin |
| 4254 |
EmitRangeChar('-', False); |
| 4255 |
Inc(regParse); |
| 4256 |
Break; |
| 4257 |
end; |
| 4258 |
|
| 4259 |
// char '-' which (maybe) makes a range |
| 4260 |
if (regParse^ = '-') and ((regParse + 1) < fRegexEnd) and CanBeRange then |
| 4261 |
begin |
| 4262 |
Inc(regParse); |
| 4263 |
RangeEnd := regParse^; |
| 4264 |
if RangeEnd = EscChar then |
| 4265 |
begin |
| 4266 |
if _IsMetaChar((regParse + 1)^) then |
| 4267 |
begin |
| 4268 |
Error(reeMetaCharAfterMinusInRange); |
| 4269 |
Exit; |
| 4270 |
end; |
| 4271 |
Inc(regParse); |
| 4272 |
RangeEnd := UnQuoteChar(regParse, fRegexEnd); |
| 4273 |
end; |
| 4274 |
|
| 4275 |
// special handling for Russian range a-YA, add 2 ranges: a-ya and A-YA |
| 4276 |
if fCompModifiers.R and |
| 4277 |
(RangeBeg = RusRangeLoLow) and (RangeEnd = RusRangeHiHigh) then |
| 4278 |
begin |
| 4279 |
EmitRangePacked(RusRangeLoLow, RusRangeLoHigh); |
| 4280 |
EmitRangePacked(RusRangeHiLow, RusRangeHiHigh); |
| 4281 |
end |
| 4282 |
else |
| 4283 |
begin // standard r.e. handling |
| 4284 |
if RangeBeg > RangeEnd then |
| 4285 |
begin |
| 4286 |
Error(reeInvalidRange); |
| 4287 |
Exit; |
| 4288 |
end; |
| 4289 |
EmitRangePacked(RangeBeg, RangeEnd); |
| 4290 |
end; |
| 4291 |
Inc(regParse); |
| 4292 |
end |
| 4293 |
else |
| 4294 |
begin |
| 4295 |
if regParse^ = EscChar then |
| 4296 |
begin |
| 4297 |
Inc(regParse); |
| 4298 |
if regParse >= fRegexEnd then |
| 4299 |
begin |
| 4300 |
Error(reeParseAtomTrailingBackSlash); |
| 4301 |
Exit; |
| 4302 |
end; |
| 4303 |
if _IsMetaChar(regParse^) then |
| 4304 |
begin |
| 4305 |
AddrOfLen := nil; |
| 4306 |
CanBeRange := False; |
| 4307 |
EmitC(OpKind_MetaClass); |
| 4308 |
case regParse^ of |
| 4309 |
'w': |
| 4310 |
EmitC(REChar(CheckerIndex_Word)); |
| 4311 |
'W': |
| 4312 |
EmitC(REChar(CheckerIndex_NotWord)); |
| 4313 |
's': |
| 4314 |
EmitC(REChar(CheckerIndex_Space)); |
| 4315 |
'S': |
| 4316 |
EmitC(REChar(CheckerIndex_NotSpace)); |
| 4317 |
'd': |
| 4318 |
EmitC(REChar(CheckerIndex_Digit)); |
| 4319 |
'D': |
| 4320 |
EmitC(REChar(CheckerIndex_NotDigit)); |
| 4321 |
'v': |
| 4322 |
EmitC(REChar(CheckerIndex_VertSep)); |
| 4323 |
'V': |
| 4324 |
EmitC(REChar(CheckerIndex_NotVertSep)); |
| 4325 |
'h': |
| 4326 |
EmitC(REChar(CheckerIndex_HorzSep)); |
| 4327 |
'H': |
| 4328 |
EmitC(REChar(CheckerIndex_NotHorzSep)); |
| 4329 |
'R': |
| 4330 |
EmitC(REChar(CheckerIndex_AnyLineBreak)); |
| 4331 |
else |
| 4332 |
Error(reeBadOpcodeInCharClass); |
| 4333 |
end; |
| 4334 |
end |
| 4335 |
else |
| 4336 |
{$IFDEF FastUnicodeData} |
| 4337 |
if regParse^ = 'p' then |
| 4338 |
EmitCategoryInCharClass(True) |
| 4339 |
else |
| 4340 |
if regParse^ = 'P' then |
| 4341 |
EmitCategoryInCharClass(False) |
| 4342 |
else |
| 4343 |
{$ENDIF} |
| 4344 |
begin |
| 4345 |
TempChar := UnQuoteChar(regParse, fRegexEnd); |
| 4346 |
// False if '-' is last char in [] |
| 4347 |
DashForRange := |
| 4348 |
(regParse + 2 < fRegexEnd) and |
| 4349 |
((regParse + 1)^ = '-') and |
| 4350 |
((regParse + 2)^ <> ']'); |
| 4351 |
EmitRangeChar(TempChar, DashForRange); |
| 4352 |
end; |
| 4353 |
end |
| 4354 |
else |
| 4355 |
begin |
| 4356 |
// False if '-' is last char in [] |
| 4357 |
DashForRange := |
| 4358 |
(regParse + 2 < fRegexEnd) and |
| 4359 |
((regParse + 1)^ = '-') and |
| 4360 |
((regParse + 2)^ <> ']'); |
| 4361 |
EmitRangeChar(regParse^, DashForRange); |
| 4362 |
end; |
| 4363 |
Inc(regParse); |
| 4364 |
end; |
| 4365 |
end; { of while } |
| 4366 |
AddrOfLen := nil; |
| 4367 |
CanBeRange := False; |
| 4368 |
EmitC(OpKind_End); |
| 4369 |
if fCompModifiers.I and not HasCaseSenseChars then begin |
| 4370 |
if PREOp(ret)^ = OP_ANYBUT_CI then |
| 4371 |
PREOp(ret)^ := OP_ANYBUT; |
| 4372 |
if PREOp(ret)^ = OP_ANYOF_CI then |
| 4373 |
PREOp(ret)^ := OP_ANYOF; |
| 4374 |
end; |
| 4375 |
if regParse^ <> ']' then |
| 4376 |
begin |
| 4377 |
Error(reeUnmatchedSqBrackets); |
| 4378 |
Exit; |
| 4379 |
end; |
| 4380 |
Inc(regParse); |
| 4381 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4382 |
end; |
| 4383 |
|
| 4384 |
'(': |
| 4385 |
begin |
| 4386 |
GrpKind := gkNormalGroup; |
| 4387 |
GrpName := ''; |
| 4388 |
|
| 4389 |
// A: detect kind of expression in brackets |
| 4390 |
if regParse^ = '?' then |
| 4391 |
begin |
| 4392 |
NextCh := (regParse + 1)^; |
| 4393 |
case NextCh of |
| 4394 |
':': |
| 4395 |
begin |
| 4396 |
// non-capturing group: (?:regex) |
| 4397 |
GrpKind := gkNonCapturingGroup; |
| 4398 |
Inc(regParse, 2); |
| 4399 |
end; |
| 4400 |
'>': |
| 4401 |
begin |
| 4402 |
// atomic group: (?>regex) |
| 4403 |
GrpKind := gkAtomicGroup; |
| 4404 |
Inc(regParse, 2); |
| 4405 |
end; |
| 4406 |
'P': |
| 4407 |
begin |
| 4408 |
if (regParse + 4 >= fRegexEnd) then |
| 4409 |
Error(reeNamedGroupBad); |
| 4410 |
case (regParse + 2)^ of |
| 4411 |
'<': |
| 4412 |
begin |
| 4413 |
// named group: (?P<name>regex) |
| 4414 |
GrpKind := gkNormalGroup; |
| 4415 |
FindGroupName(regParse + 3, fRegexEnd, '>', GrpName); |
| 4416 |
Inc(regParse, Length(GrpName) + 4); |
| 4417 |
end; |
| 4418 |
'=': |
| 4419 |
begin |
| 4420 |
// back-reference to named group: (?P=name) |
| 4421 |
GrpKind := gkNamedGroupReference; |
| 4422 |
FindGroupName(regParse + 3, fRegexEnd, ')', GrpName); |
| 4423 |
Inc(regParse, Length(GrpName) + 4); |
| 4424 |
end; |
| 4425 |
'>': |
| 4426 |
begin |
| 4427 |
// subroutine call to named group: (?P>name) |
| 4428 |
GrpKind := gkSubCall; |
| 4429 |
FindGroupName(regParse + 3, fRegexEnd, ')', GrpName); |
| 4430 |
Inc(regParse, Length(GrpName) + 4); |
| 4431 |
if fSecondPass then begin |
| 4432 |
GrpIndex := GrpNames.MatchIndexFromName(GrpName); |
| 4433 |
if GrpIndex < 1 then |
| 4434 |
Error(reeNamedGroupBadRef); |
| 4435 |
end; |
| 4436 |
end; |
| 4437 |
else |
| 4438 |
Error(reeNamedGroupBad); |
| 4439 |
end; |
| 4440 |
end; |
| 4441 |
'<': |
| 4442 |
begin |
| 4443 |
// lookbehind: (?<=foo)bar |
| 4444 |
case (regParse + 2)^ of |
| 4445 |
'=': |
| 4446 |
begin |
| 4447 |
if (regParse + 4 >= fRegexEnd) then |
| 4448 |
Error(reeLookbehindBad); |
| 4449 |
GrpKind := gkLookbehind; |
| 4450 |
Inc(regParse, 3); |
| 4451 |
end; |
| 4452 |
'!': |
| 4453 |
begin |
| 4454 |
if (regParse + 4 >= fRegexEnd) then |
| 4455 |
Error(reeLookbehindBad); |
| 4456 |
GrpKind := gkLookbehindNeg; |
| 4457 |
Inc(regParse, 3); |
| 4458 |
end; |
| 4459 |
'A'..'Z', 'a'..'z': |
| 4460 |
begin |
| 4461 |
// named group: (?<name>regex) |
| 4462 |
if (regParse + 4 >= fRegexEnd) then |
| 4463 |
Error(reeNamedGroupBad); |
| 4464 |
GrpKind := gkNormalGroup; |
| 4465 |
FindGroupName(regParse + 2, fRegexEnd, '>', GrpName); |
| 4466 |
Inc(regParse, Length(GrpName) + 3); |
| 4467 |
end; |
| 4468 |
else |
| 4469 |
Error(reeIncorrectSpecialBrackets); |
| 4470 |
end; |
| 4471 |
end; |
| 4472 |
'=', '!': |
| 4473 |
begin |
| 4474 |
// lookaheads: foo(?=bar) and foo(?!bar) |
| 4475 |
if (regParse + 3 >= fRegexEnd) then |
| 4476 |
Error(reeLookaheadBad); |
| 4477 |
if NextCh = '=' then |
| 4478 |
begin |
| 4479 |
GrpKind := gkLookahead; |
| 4480 |
end |
| 4481 |
else |
| 4482 |
begin |
| 4483 |
GrpKind := gkLookaheadNeg; |
| 4484 |
end; |
| 4485 |
Inc(regParse, 2); |
| 4486 |
end; |
| 4487 |
'#': |
| 4488 |
begin |
| 4489 |
// (?#comment) |
| 4490 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4491 |
GrpKind := gkComment; |
| 4492 |
Inc(regParse, 2); |
| 4493 |
end; |
| 4494 |
'a'..'z', '-': |
| 4495 |
begin |
| 4496 |
// modifiers string like (?mxr) |
| 4497 |
GrpKind := gkModifierString; |
| 4498 |
Inc(regParse); |
| 4499 |
end; |
| 4500 |
'R', '0': |
| 4501 |
begin |
| 4502 |
// recursion (?R), (?0) |
| 4503 |
GrpKind := gkRecursion; |
| 4504 |
Inc(regParse, 2); |
| 4505 |
if regParse^ <> ')' then |
| 4506 |
Error(reeBadRecursion); |
| 4507 |
Inc(regParse); |
| 4508 |
end; |
| 4509 |
'1'..'9': |
| 4510 |
begin |
| 4511 |
// subroutine call (?1)..(?99) |
| 4512 |
GrpKind := gkSubCall; |
| 4513 |
Inc(regParse, 1); |
| 4514 |
if not ParseNumber(regParse, GrpIndex) or (regParse^ <> ')') then |
| 4515 |
begin |
| 4516 |
Error(reeBadRecursion); |
| 4517 |
Exit; |
| 4518 |
end; |
| 4519 |
Inc(regParse, 1); |
| 4520 |
if fSecondPass and (GrpIndex > GrpCount) then |
| 4521 |
Error(reeBadSubCall); |
| 4522 |
end; |
| 4523 |
'''': |
| 4524 |
begin |
| 4525 |
// named group: (?'name'regex) |
| 4526 |
if (regParse + 4 >= fRegexEnd) then |
| 4527 |
Error(reeNamedGroupBad); |
| 4528 |
GrpKind := gkNormalGroup; |
| 4529 |
FindGroupName(regParse + 2, fRegexEnd, '''', GrpName); |
| 4530 |
Inc(regParse, Length(GrpName) + 3); |
| 4531 |
end; |
| 4532 |
'&': |
| 4533 |
begin |
| 4534 |
// subroutine call to named group: (?&name) |
| 4535 |
if (regParse + 2 >= fRegexEnd) then |
| 4536 |
Error(reeBadSubCall); |
| 4537 |
GrpKind := gkSubCall; |
| 4538 |
FindGroupName(regParse + 2, fRegexEnd, ')', GrpName); |
| 4539 |
Inc(regParse, Length(GrpName) + 3); |
| 4540 |
if fSecondPass then begin |
| 4541 |
GrpIndex := GrpNames.MatchIndexFromName(GrpName); |
| 4542 |
if GrpIndex < 1 then |
| 4543 |
Error(reeNamedGroupBadRef); |
| 4544 |
end; |
| 4545 |
end; |
| 4546 |
else |
| 4547 |
Error(reeIncorrectSpecialBrackets); |
| 4548 |
end; |
| 4549 |
end; |
| 4550 |
|
| 4551 |
// B: process found kind of brackets |
| 4552 |
case GrpKind of |
| 4553 |
gkNonCapturingGroup: |
| 4554 |
begin |
| 4555 |
ret := DoParseReg(True, nil, FlagTemp, OP_NONE, OP_NONE); |
| 4556 |
if ret = nil then |
| 4557 |
begin |
| 4558 |
Result := nil; |
| 4559 |
Exit; |
| 4560 |
end; |
| 4561 |
FlagParse := FlagParse or FlagTemp and (FLAG_HASWIDTH or FLAG_SPECSTART or FLAG_LOOP or FLAG_GREEDY); |
| 4562 |
end; |
| 4563 |
|
| 4564 |
gkNormalGroup, |
| 4565 |
gkAtomicGroup: |
| 4566 |
begin |
| 4567 |
// skip this block for one of passes, to not double groups count; |
| 4568 |
// must take first pass (we need GrpNames filled) |
| 4569 |
if (GrpKind = gkNormalGroup) then begin |
| 4570 |
Inc(ParsedGrpCount); |
| 4571 |
if (not fSecondPass) and (GrpName <> '') then |
| 4572 |
begin |
| 4573 |
// first pass |
| 4574 |
if GrpNames.MatchIndexFromName(GrpName) >= 0 then |
| 4575 |
Error(reeNamedGroupDupName); |
| 4576 |
GrpNames.Add(GrpName, ParsedGrpCount); |
| 4577 |
end; |
| 4578 |
end; |
| 4579 |
|
| 4580 |
if GrpKind = gkAtomicGroup then |
| 4581 |
ret := DoParseReg(True, @regNumAtomicBrackets, FlagTemp, OP_OPEN_ATOMIC, OP_CLOSE_ATOMIC) |
| 4582 |
else |
| 4583 |
ret := DoParseReg(True, @regNumBrackets, FlagTemp, OP_OPEN, OP_CLOSE); |
| 4584 |
if ret = nil then |
| 4585 |
begin |
| 4586 |
Result := nil; |
| 4587 |
Exit; |
| 4588 |
end; |
| 4589 |
FlagParse := FlagParse or FlagTemp and (FLAG_HASWIDTH or FLAG_SPECSTART or FLAG_LOOP or FLAG_GREEDY); |
| 4590 |
end; |
| 4591 |
|
| 4592 |
gkLookahead, |
| 4593 |
gkLookaheadNeg: |
| 4594 |
begin |
| 4595 |
case GrpKind of |
| 4596 |
gkLookahead: ret := EmitNode(OP_LOOKAHEAD); |
| 4597 |
gkLookaheadNeg: ret := EmitNode(OP_LOOKAHEAD_NEG); |
| 4598 |
end; |
| 4599 |
|
| 4600 |
Result := DoParseReg(True, nil, FlagTemp, OP_NONE, OP_LOOKAHEAD_END); |
| 4601 |
if Result = nil then |
| 4602 |
Exit; |
| 4603 |
|
| 4604 |
Tail(ret, regLast(Result)); |
| 4605 |
FlagParse := FlagParse and not FLAG_HASWIDTH; |
| 4606 |
end; |
| 4607 |
|
| 4608 |
gkLookbehind, |
| 4609 |
gkLookbehindNeg: |
| 4610 |
begin |
| 4611 |
case GrpKind of |
| 4612 |
gkLookbehind: ret := EmitNode(OP_LOOKBEHIND); |
| 4613 |
gkLookbehindNeg: ret := EmitNode(OP_LOOKBEHIND_NEG); |
| 4614 |
end; |
| 4615 |
regLookBehindOption := regCode; |
| 4616 |
if (regCode <> @regDummy) then |
| 4617 |
Inc(regCode, ReOpLookBehindOptionsSz) |
| 4618 |
else |
| 4619 |
Inc(regCodeSize, ReOpLookBehindOptionsSz); |
| 4620 |
|
| 4621 |
RegGrpCountBefore := ParsedGrpCount; |
| 4622 |
Result := DoParseReg(True, nil, FlagTemp, OP_NONE, OP_LOOKBEHIND_END); |
| 4623 |
if Result = nil then |
| 4624 |
Exit; |
| 4625 |
|
| 4626 |
Tail(ret, regLast(Result)); |
| 4627 |
|
| 4628 |
if (regCode <> @regDummy) then begin |
| 4629 |
ALen := 0; |
| 4630 |
ret2 := Result; |
| 4631 |
if IsPartFixedLength(ret2, op, ALen, AMaxLen, OP_LOOKBEHIND_END, regCode, [flfSkipLookAround]) then |
| 4632 |
PReOpLookBehindOptions(regLookBehindOption)^.IsGreedy := OPT_LOOKBEHIND_FIXED |
| 4633 |
else |
| 4634 |
if (ParsedGrpCount > RegGrpCountBefore) and (not FAllowUnsafeLookBehind) then |
| 4635 |
Error(reeLookaroundNotSafe) |
| 4636 |
else |
| 4637 |
if (FlagTemp and (FLAG_GREEDY)) = (FLAG_GREEDY) then |
| 4638 |
PReOpLookBehindOptions(regLookBehindOption)^.IsGreedy := OPT_LOOKBEHIND_GREEDY |
| 4639 |
else |
| 4640 |
PReOpLookBehindOptions(regLookBehindOption)^.IsGreedy := OPT_LOOKBEHIND_NON_GREEDY; |
| 4641 |
PReOpLookBehindOptions(regLookBehindOption)^.MatchLenMin := ALen; |
| 4642 |
PReOpLookBehindOptions(regLookBehindOption)^.MatchLenMax := AMaxLen; |
| 4643 |
end; |
| 4644 |
|
| 4645 |
FlagParse := FlagParse and not FLAG_HASWIDTH; |
| 4646 |
end; |
| 4647 |
|
| 4648 |
gkNamedGroupReference: |
| 4649 |
begin |
| 4650 |
Len := GrpNames.MatchIndexFromName(GrpName); |
| 4651 |
if fSecondPass and (Len < 0) then |
| 4652 |
Error(reeNamedGroupBadRef); |
| 4653 |
ret := EmitGroupRef(Len, fCompModifiers.I); |
| 4654 |
end; |
| 4655 |
|
| 4656 |
gkModifierString: |
| 4657 |
begin |
| 4658 |
SavedPtr := regParse; |
| 4659 |
while (regParse < fRegexEnd) and (regParse^ <> ')') and (regParse^ <> ':') do |
| 4660 |
Inc(regParse); |
| 4661 |
SavedModifiers := fCompModifiers; |
| 4662 |
if (regParse^ = ':') and ParseModifiers(SavedPtr, regParse - SavedPtr, fCompModifiers) then |
| 4663 |
begin |
| 4664 |
Inc(regParse); // skip ')' |
| 4665 |
ret := DoParseReg(True, nil, FlagTemp, OP_NONE, OP_COMMENT); // can't use OP_NONE // The "ender" op will not be omitted anyway |
| 4666 |
fCompModifiers := SavedModifiers; |
| 4667 |
if ret = nil then |
| 4668 |
begin |
| 4669 |
Result := nil; |
| 4670 |
Exit; |
| 4671 |
end; |
| 4672 |
FlagParse := FlagParse or FlagTemp and (FLAG_HASWIDTH or FLAG_SPECSTART or FLAG_LOOP or FLAG_GREEDY); |
| 4673 |
end |
| 4674 |
else |
| 4675 |
if (regParse^ = ')') and ParseModifiers(SavedPtr, regParse - SavedPtr, fCompModifiers) then |
| 4676 |
begin |
| 4677 |
Inc(regParse); // skip ')' |
| 4678 |
ret := EmitNode(OP_COMMENT); // comment |
| 4679 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4680 |
end |
| 4681 |
else |
| 4682 |
begin |
| 4683 |
Error(reeUnrecognizedModifier); |
| 4684 |
Exit; |
| 4685 |
end; |
| 4686 |
end; |
| 4687 |
|
| 4688 |
gkComment: |
| 4689 |
begin |
| 4690 |
while (regParse < fRegexEnd) and (regParse^ <> ')') do |
| 4691 |
Inc(regParse); |
| 4692 |
if regParse^ <> ')' then |
| 4693 |
begin |
| 4694 |
Error(reeUnclosedComment); |
| 4695 |
Exit; |
| 4696 |
end; |
| 4697 |
Inc(regParse); // skip ')' |
| 4698 |
ret := EmitNode(OP_COMMENT); // comment |
| 4699 |
end; |
| 4700 |
|
| 4701 |
gkRecursion: |
| 4702 |
begin |
| 4703 |
// set FLAG_HASWIDTH to allow compiling of such regex: b(?:m|(?R))*e |
| 4704 |
FlagParse := FlagParse or FLAG_HASWIDTH; |
| 4705 |
ret := EmitNode(OP_RECUR); |
| 4706 |
hasRecursion := True; |
| 4707 |
end; |
| 4708 |
|
| 4709 |
gkSubCall: |
| 4710 |
begin |
| 4711 |
// set FLAG_HASWIDTH like for (?R) |
| 4712 |
FlagParse := FlagParse or FLAG_HASWIDTH; |
| 4713 |
ret := EmitNodeWithGroupIndex(OP_SUBCALL, GrpIndex); |
| 4714 |
hasRecursion := True; |
| 4715 |
end; |
| 4716 |
end; // case GrpKind of |
| 4717 |
end; |
| 4718 |
|
| 4719 |
'|', ')': |
| 4720 |
begin // Supposed to be caught earlier. |
| 4721 |
Error(reeInternalUrp); |
| 4722 |
Exit; |
| 4723 |
end; |
| 4724 |
|
| 4725 |
'?', '+', '*': |
| 4726 |
begin |
| 4727 |
Error(reeQuantifFollowsNothing); |
| 4728 |
Exit; |
| 4729 |
end; |
| 4730 |
|
| 4731 |
EscChar: |
| 4732 |
begin |
| 4733 |
if regParse >= fRegexEnd then |
| 4734 |
begin |
| 4735 |
Error(reeTrailingBackSlash); |
| 4736 |
Exit; |
| 4737 |
end; |
| 4738 |
case regParse^ of |
| 4739 |
'b': |
| 4740 |
begin |
| 4741 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4742 |
ret := EmitNode(OP_BOUND); |
| 4743 |
end; |
| 4744 |
'B': |
| 4745 |
begin |
| 4746 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4747 |
ret := EmitNode(OP_NOTBOUND); |
| 4748 |
end; |
| 4749 |
'A': |
| 4750 |
begin |
| 4751 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4752 |
ret := EmitNode(OP_BOL); |
| 4753 |
end; |
| 4754 |
'z': |
| 4755 |
begin |
| 4756 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4757 |
ret := EmitNode(OP_EOL); |
| 4758 |
end; |
| 4759 |
'Z': |
| 4760 |
begin |
| 4761 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4762 |
ret := EmitNode(OP_EOL2); |
| 4763 |
end; |
| 4764 |
'G': |
| 4765 |
begin |
| 4766 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4767 |
ret := EmitNode(OP_CONTINUE_POS); |
| 4768 |
end; |
| 4769 |
'd': |
| 4770 |
begin // r.e.extension - any digit ('0' .. '9') |
| 4771 |
ret := EmitNode(OP_ANYDIGIT); |
| 4772 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4773 |
end; |
| 4774 |
'D': |
| 4775 |
begin // r.e.extension - not digit ('0' .. '9') |
| 4776 |
ret := EmitNode(OP_NOTDIGIT); |
| 4777 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4778 |
end; |
| 4779 |
's': |
| 4780 |
begin // r.e.extension - any space char |
| 4781 |
ret := EmitNode(OP_ANYSPACE); |
| 4782 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4783 |
end; |
| 4784 |
'S': |
| 4785 |
begin // r.e.extension - not space char |
| 4786 |
ret := EmitNode(OP_NOTSPACE); |
| 4787 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4788 |
end; |
| 4789 |
'w': |
| 4790 |
begin // r.e.extension - any english char / digit / '_' |
| 4791 |
ret := EmitNode(OP_ANYLETTER); |
| 4792 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4793 |
end; |
| 4794 |
'W': |
| 4795 |
begin // r.e.extension - not english char / digit / '_' |
| 4796 |
ret := EmitNode(OP_NOTLETTER); |
| 4797 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4798 |
end; |
| 4799 |
'v': |
| 4800 |
begin |
| 4801 |
ret := EmitNode(OP_ANYVERTSEP); |
| 4802 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4803 |
end; |
| 4804 |
'V': |
| 4805 |
begin |
| 4806 |
ret := EmitNode(OP_NOTVERTSEP); |
| 4807 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4808 |
end; |
| 4809 |
'h': |
| 4810 |
begin |
| 4811 |
ret := EmitNode(OP_ANYHORZSEP); |
| 4812 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4813 |
end; |
| 4814 |
'H': |
| 4815 |
begin |
| 4816 |
ret := EmitNode(OP_NOTHORZSEP); |
| 4817 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4818 |
end; |
| 4819 |
'1' .. '9': |
| 4820 |
begin |
| 4821 |
if fSecondPass and (Ord(regParse^) - Ord('0') > GrpCount) then |
| 4822 |
Error(reeBadReference); |
| 4823 |
ret := EmitGroupRef(Ord(regParse^) - Ord('0'), fCompModifiers.I); |
| 4824 |
end; |
| 4825 |
'g': |
| 4826 |
begin |
| 4827 |
case (regParse + 1)^ of |
| 4828 |
'<', '''': |
| 4829 |
begin |
| 4830 |
// subroutine call to named group |
| 4831 |
case (regParse + 1)^ of |
| 4832 |
'<': FindGroupName(regParse + 2, fRegexEnd, '>', GrpName); |
| 4833 |
'''': FindGroupName(regParse + 2, fRegexEnd, '''', GrpName); |
| 4834 |
end; |
| 4835 |
Inc(regParse, Length(GrpName) + 2); |
| 4836 |
GrpIndex := GrpNames.MatchIndexFromName(GrpName); |
| 4837 |
if fSecondPass and (GrpIndex < 1) then |
| 4838 |
Error(reeNamedGroupBadRef); |
| 4839 |
ret := EmitNodeWithGroupIndex(OP_SUBCALL, GrpIndex); |
| 4840 |
FlagParse := FlagParse or FLAG_HASWIDTH; |
| 4841 |
hasRecursion := True; |
| 4842 |
end; |
| 4843 |
'{': |
| 4844 |
begin |
| 4845 |
// back-reference to named group |
| 4846 |
FindGroupName(regParse + 2, fRegexEnd, '}', GrpName); |
| 4847 |
Inc(regParse, Length(GrpName) + 2); |
| 4848 |
GrpIndex := GrpNames.MatchIndexFromName(GrpName); |
| 4849 |
if fSecondPass and (GrpIndex < 1) then |
| 4850 |
Error(reeNamedGroupBadRef); |
| 4851 |
ret := EmitGroupRef(GrpIndex, fCompModifiers.I); |
| 4852 |
end; |
| 4853 |
'0'..'9': |
| 4854 |
begin |
| 4855 |
inc(regParse); |
| 4856 |
if not ParseNumber(regParse, GrpIndex) then begin |
| 4857 |
Error(reeBadReference); |
| 4858 |
Exit; |
| 4859 |
end; |
| 4860 |
dec(regParse); |
| 4861 |
if GrpIndex = 0 then |
| 4862 |
Error(reeBadReference); |
| 4863 |
if fSecondPass and (GrpIndex > GrpCount) then |
| 4864 |
Error(reeBadReference); |
| 4865 |
ret := EmitGroupRef(GrpIndex, fCompModifiers.I); |
| 4866 |
end; |
| 4867 |
else |
| 4868 |
Error(reeBadReference); |
| 4869 |
end; |
| 4870 |
end; |
| 4871 |
'k': |
| 4872 |
begin |
| 4873 |
// back-reference to named group |
| 4874 |
case (regParse + 1)^ of |
| 4875 |
'<': |
| 4876 |
FindGroupName(regParse + 2, fRegexEnd, '>', GrpName); |
| 4877 |
'''': |
| 4878 |
FindGroupName(regParse + 2, fRegexEnd, '''', GrpName); |
| 4879 |
'{': |
| 4880 |
FindGroupName(regParse + 2, fRegexEnd, '}', GrpName); |
| 4881 |
else |
| 4882 |
Error(reeBadReference); |
| 4883 |
end; |
| 4884 |
Inc(regParse, Length(GrpName) + 2); |
| 4885 |
GrpIndex := GrpNames.MatchIndexFromName(GrpName); |
| 4886 |
if fSecondPass and (GrpIndex < 1) then |
| 4887 |
Error(reeNamedGroupBadRef); |
| 4888 |
ret := EmitGroupRef(GrpIndex, fCompModifiers.I); |
| 4889 |
end; |
| 4890 |
'K': |
| 4891 |
begin |
| 4892 |
ret := EmitNode(OP_RESET_MATCHPOS); |
| 4893 |
FlagParse := FlagParse or FLAG_NOT_QUANTIFIABLE; |
| 4894 |
end; |
| 4895 |
{$IFDEF FastUnicodeData} |
| 4896 |
'p': |
| 4897 |
begin |
| 4898 |
ret := EmitCategoryMain(True); |
| 4899 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4900 |
end; |
| 4901 |
'P': |
| 4902 |
begin |
| 4903 |
ret := EmitCategoryMain(False); |
| 4904 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4905 |
end; |
| 4906 |
{$ENDIF} |
| 4907 |
'R': |
| 4908 |
begin |
| 4909 |
ret := EmitNode(OP_ANYLINEBREAK); |
| 4910 |
FlagParse := FlagParse or FLAG_HASWIDTH or FLAG_SIMPLE; |
| 4911 |
end; |
| 4912 |
else |
| 4913 |
EmitExactly(UnQuoteChar(regParse, fRegexEnd)); |
| 4914 |
end; { of case } |
| 4915 |
Inc(regParse); |
| 4916 |
end; |
| 4917 |
|
| 4918 |
else |
| 4919 |
begin |
| 4920 |
Dec(regParse); |
| 4921 |
if fCompModifiers.X and // check for eXtended syntax |
| 4922 |
((regParse^ = '#') or IsIgnoredChar(regParse^)) then |
| 4923 |
begin // \x |
| 4924 |
if regParse^ = '#' then |
| 4925 |
begin // Skip eXtended comment |
| 4926 |
// find comment terminator (group of \n and/or \r) |
| 4927 |
while (regParse < fRegexEnd) and (regParse^ <> #$d) and |
| 4928 |
(regParse^ <> #$a) do |
| 4929 |
Inc(regParse); |
| 4930 |
while (regParse^ = #$d) or (regParse^ = #$a) |
| 4931 |
// skip comment terminator |
| 4932 |
do |
| 4933 |
Inc(regParse); |
| 4934 |
// attempt to support different type of line separators |
| 4935 |
end |
| 4936 |
else |
| 4937 |
begin // Skip the blanks! |
| 4938 |
while IsIgnoredChar(regParse^) do |
| 4939 |
Inc(regParse); |
| 4940 |
end; |
| 4941 |
ret := EmitNode(OP_COMMENT); // comment |
| 4942 |
end |
| 4943 |
else |
| 4944 |
begin |
| 4945 |
Len := FindSkippedMetaLen(regParse, fRegexEnd); |
| 4946 |
if Len <= 0 then |
| 4947 |
if regParse^ <> '{' then |
| 4948 |
begin |
| 4949 |
Error(reeRarseAtomInternalDisaster); |
| 4950 |
Exit; |
| 4951 |
end |
| 4952 |
else |
| 4953 |
Len := FindSkippedMetaLen(regParse + 1, fRegexEnd) + 1; |
| 4954 |
// bad {n,m} - compile as EXACTLY |
| 4955 |
EnderChar := (regParse + Len)^; |
| 4956 |
if (Len > 1) and ((EnderChar = '*') or (EnderChar = '+') or (EnderChar = '?') or (EnderChar = '{')) then |
| 4957 |
Dec(Len); // back off clear of ?+*{ operand. |
| 4958 |
FlagParse := FlagParse or FLAG_HASWIDTH; |
| 4959 |
if Len = 1 then |
| 4960 |
FlagParse := FlagParse or FLAG_SIMPLE; |
| 4961 |
if fCompModifiers.I then |
| 4962 |
ret := EmitNode(OP_EXACTLY_CI) |
| 4963 |
else |
| 4964 |
ret := EmitNode(OP_EXACTLY); |
| 4965 |
EmitInt(0); |
| 4966 |
while (Len > 0) and ((not fCompModifiers.X) or (regParse^ <> '#')) do |
| 4967 |
begin |
| 4968 |
if not fCompModifiers.X or not IsIgnoredChar(regParse^) then |
| 4969 |
begin |
| 4970 |
if fCompModifiers.I then |
| 4971 |
EmitC(_UpperCase(regParse^)) |
| 4972 |
else |
| 4973 |
EmitC(regParse^); |
| 4974 |
if regCode <> @regDummy then |
| 4975 |
Inc(regExactlyLen^); |
| 4976 |
end; |
| 4977 |
Inc(regParse); |
| 4978 |
Dec(Len); |
| 4979 |
end; |
| 4980 |
end; { of if not comment } |
| 4981 |
end; { of case else } |
| 4982 |
end; { of case } |
| 4983 |
|
| 4984 |
Result := ret; |
| 4985 |
end; { of function TRegExpr.ParseAtom |
| 4986 |
-------------------------------------------------------------- } |
| 4987 |
|
| 4988 |
function TRegExpr.GetCompilerErrorPos: PtrInt; |
| 4989 |
begin |
| 4990 |
Result := 0; |
| 4991 |
if (fRegexStart = nil) or (regParse = nil) then |
| 4992 |
Exit; // not in compiling mode ? |
| 4993 |
Result := regParse - fRegexStart; |
| 4994 |
end; { of function TRegExpr.GetCompilerErrorPos |
| 4995 |
-------------------------------------------------------------- } |
| 4996 |
|
| 4997 |
{ ============================================================= } |
| 4998 |
{ ===================== Matching section ====================== } |
| 4999 |
{ ============================================================= } |
| 5000 |
|
| 5001 |
procedure TRegExpr.FindGroupName(APtr, AEndPtr: PRegExprChar; AEndChar: REChar; var AName: RegExprString); |
| 5002 |
// check that group name is valid identifier, started from non-digit |
| 5003 |
// this is to be like in Python regex |
| 5004 |
var |
| 5005 |
P: PRegExprChar; |
| 5006 |
begin |
| 5007 |
P := APtr; |
| 5008 |
if IsDigitChar(P^) or not IsWordChar(P^) then |
| 5009 |
Error(reeNamedGroupBadName); |
| 5010 |
|
| 5011 |
repeat |
| 5012 |
if P >= AEndPtr then |
| 5013 |
Error(reeNamedGroupBad); |
| 5014 |
if P^ = AEndChar then |
| 5015 |
Break; |
| 5016 |
if not (IsWordChar(P^) or (P^ = '_')) then |
| 5017 |
Error(reeNamedGroupBadName); |
| 5018 |
Inc(P); |
| 5019 |
until False; |
| 5020 |
|
| 5021 |
SetString(AName, APtr, P-APtr); |
| 5022 |
end; |
| 5023 |
|
| 5024 |
function TRegExpr.FindRepeated(p: PRegExprChar; AMax: Integer): Integer; |
| 5025 |
// repeatedly match something simple, report how many |
| 5026 |
// p: points to current opcode |
| 5027 |
var |
| 5028 |
scan: PRegExprChar; |
| 5029 |
opnd: PRegExprChar; |
| 5030 |
TheMax: PtrInt; // PtrInt, gets diff of 2 pointers |
| 5031 |
InvChar: REChar; |
| 5032 |
{$IFDEF UnicodeEx} |
| 5033 |
i: Integer; |
| 5034 |
{$ENDIF} |
| 5035 |
begin |
| 5036 |
Result := 0; |
| 5037 |
scan := regInput; // points into InputString |
| 5038 |
opnd := p + REOpSz + RENextOffSz; // points to operand of opcode (after OP_nnn code) |
| 5039 |
TheMax := fInputCurrentEnd - scan; |
| 5040 |
if TheMax > AMax then |
| 5041 |
TheMax := AMax; |
| 5042 |
case PREOp(p)^ of |
| 5043 |
OP_ANY: |
| 5044 |
begin |
| 5045 |
// note - OP_ANY_ML cannot be proceeded in FindRepeated because can skip |
| 5046 |
// more than one char at once |
| 5047 |
{$IFDEF UnicodeEx} |
| 5048 |
for i := 1 to TheMax do |
| 5049 |
IncUnicode2(scan, Result); |
| 5050 |
{$ELSE} |
| 5051 |
Result := TheMax; |
| 5052 |
Inc(scan, Result); |
| 5053 |
{$ENDIF} |
| 5054 |
end; |
| 5055 |
|
| 5056 |
OP_EXACTLY: |
| 5057 |
begin // in opnd can be only ONE char !!! |
| 5058 |
{ |
| 5059 |
// Alexey: commented because of https://github.com/andgineer/TRegExpr/issues/145 |
| 5060 |
NLen := PLongInt(opnd)^; |
| 5061 |
if TheMax > NLen then |
| 5062 |
TheMax := NLen; |
| 5063 |
} |
| 5064 |
Inc(opnd, RENumberSz); |
| 5065 |
while (Result < TheMax) and (opnd^ = scan^) do |
| 5066 |
begin |
| 5067 |
Inc(Result); |
| 5068 |
Inc(scan); |
| 5069 |
end; |
| 5070 |
end; |
| 5071 |
|
| 5072 |
OP_EXACTLY_CI: |
| 5073 |
begin // in opnd can be only ONE char !!! |
| 5074 |
{ |
| 5075 |
// Alexey: commented because of https://github.com/andgineer/TRegExpr/issues/145 |
| 5076 |
NLen := PLongInt(opnd)^; |
| 5077 |
if TheMax > NLen then |
| 5078 |
TheMax := NLen; |
| 5079 |
} |
| 5080 |
Inc(opnd, RENumberSz); |
| 5081 |
while (Result < TheMax) and (opnd^ = scan^) do |
| 5082 |
begin // prevent unneeded InvertCase |
| 5083 |
Inc(Result); |
| 5084 |
Inc(scan); |
| 5085 |
end; |
| 5086 |
if Result < TheMax then |
| 5087 |
begin |
| 5088 |
InvChar := _LowerCase(opnd^); // store in register |
| 5089 |
while (Result < TheMax) and ((opnd^ = scan^) or (InvChar = scan^)) do |
| 5090 |
begin |
| 5091 |
Inc(Result); |
| 5092 |
Inc(scan); |
| 5093 |
end; |
| 5094 |
end; |
| 5095 |
end; |
| 5096 |
|
| 5097 |
OP_ANYDIGIT: |
| 5098 |
while (Result < TheMax) and IsDigitChar(scan^) do |
| 5099 |
begin |
| 5100 |
Inc(Result); |
| 5101 |
Inc(scan); |
| 5102 |
end; |
| 5103 |
|
| 5104 |
OP_NOTDIGIT: |
| 5105 |
{$IFDEF UNICODEEX} |
| 5106 |
begin |
| 5107 |
i := 0; |
| 5108 |
while (i < TheMax) and not IsDigitChar(scan^) do |
| 5109 |
begin |
| 5110 |
Inc(i); |
| 5111 |
IncUnicode2(scan, Result); |
| 5112 |
end; |
| 5113 |
end; |
| 5114 |
{$ELSE} |
| 5115 |
while (Result < TheMax) and not IsDigitChar(scan^) do |
| 5116 |
begin |
| 5117 |
Inc(Result); |
| 5118 |
Inc(scan); |
| 5119 |
end; |
| 5120 |
{$ENDIF} |
| 5121 |
|
| 5122 |
OP_ANYLETTER: |
| 5123 |
while (Result < TheMax) and IsWordChar(scan^) do |
| 5124 |
begin |
| 5125 |
Inc(Result); |
| 5126 |
Inc(scan); |
| 5127 |
end; |
| 5128 |
|
| 5129 |
OP_NOTLETTER: |
| 5130 |
{$IFDEF UNICODEEX} |
| 5131 |
begin |
| 5132 |
i := 0; |
| 5133 |
while (i < TheMax) and not IsWordChar(scan^) do |
| 5134 |
begin |
| 5135 |
Inc(i); |
| 5136 |
IncUnicode2(scan, Result); |
| 5137 |
end; |
| 5138 |
end; |
| 5139 |
{$ELSE} |
| 5140 |
while (Result < TheMax) and not IsWordChar(scan^) do |
| 5141 |
begin |
| 5142 |
Inc(Result); |
| 5143 |
Inc(scan); |
| 5144 |
end; |
| 5145 |
{$ENDIF} |
| 5146 |
|
| 5147 |
OP_ANYSPACE: |
| 5148 |
while (Result < TheMax) and IsSpaceChar(scan^) do |
| 5149 |
begin |
| 5150 |
Inc(Result); |
| 5151 |
Inc(scan); |
| 5152 |
end; |
| 5153 |
|
| 5154 |
OP_NOTSPACE: |
| 5155 |
{$IFDEF UNICODEEX} |
| 5156 |
begin |
| 5157 |
i := 0; |
| 5158 |
while (i < TheMax) and not IsSpaceChar(scan^) do |
| 5159 |
begin |
| 5160 |
Inc(i); |
| 5161 |
IncUnicode2(scan, Result); |
| 5162 |
end; |
| 5163 |
end; |
| 5164 |
{$ELSE} |
| 5165 |
while (Result < TheMax) and not IsSpaceChar(scan^) do |
| 5166 |
begin |
| 5167 |
Inc(Result); |
| 5168 |
Inc(scan); |
| 5169 |
end; |
| 5170 |
{$ENDIF} |
| 5171 |
|
| 5172 |
OP_ANYVERTSEP: |
| 5173 |
while (Result < TheMax) and IsVertLineSeparator(scan^) do |
| 5174 |
begin |
| 5175 |
Inc(Result); |
| 5176 |
Inc(scan); |
| 5177 |
end; |
| 5178 |
|
| 5179 |
OP_NOTVERTSEP: |
| 5180 |
{$IFDEF UNICODEEX} |
| 5181 |
begin |
| 5182 |
i := 0; |
| 5183 |
while (i < TheMax) and not IsVertLineSeparator(scan^) do |
| 5184 |
begin |
| 5185 |
Inc(i); |
| 5186 |
IncUnicode2(scan, Result); |
| 5187 |
end; |
| 5188 |
end; |
| 5189 |
{$ELSE} |
| 5190 |
while (Result < TheMax) and not IsVertLineSeparator(scan^) do |
| 5191 |
begin |
| 5192 |
Inc(Result); |
| 5193 |
Inc(scan); |
| 5194 |
end; |
| 5195 |
{$ENDIF} |
| 5196 |
|
| 5197 |
OP_ANYHORZSEP: |
| 5198 |
while (Result < TheMax) and IsHorzSeparator(scan^) do |
| 5199 |
begin |
| 5200 |
Inc(Result); |
| 5201 |
Inc(scan); |
| 5202 |
end; |
| 5203 |
|
| 5204 |
OP_NOTHORZSEP: |
| 5205 |
{$IFDEF UNICODEEX} |
| 5206 |
begin |
| 5207 |
i := 0; |
| 5208 |
while (i < TheMax) and not IsHorzSeparator(scan^) do |
| 5209 |
begin |
| 5210 |
Inc(i); |
| 5211 |
IncUnicode2(scan, Result); |
| 5212 |
end; |
| 5213 |
end; |
| 5214 |
{$ELSE} |
| 5215 |
while (Result < TheMax) and not IsHorzSeparator(scan^) do |
| 5216 |
begin |
| 5217 |
Inc(Result); |
| 5218 |
Inc(scan); |
| 5219 |
end; |
| 5220 |
{$ENDIF} |
| 5221 |
|
| 5222 |
OP_ANYOF: |
| 5223 |
{$IFDEF UNICODEEX} |
| 5224 |
begin |
| 5225 |
i := 0; |
| 5226 |
while (i < TheMax) and FindInCharClass(opnd, scan^) do |
| 5227 |
begin |
| 5228 |
Inc(i); |
| 5229 |
IncUnicode2(scan, Result); |
| 5230 |
end; |
| 5231 |
end; |
| 5232 |
{$ELSE} |
| 5233 |
while (Result < TheMax) and FindInCharClass(opnd, scan^) do |
| 5234 |
begin |
| 5235 |
Inc(Result); |
| 5236 |
Inc(scan); |
| 5237 |
end; |
| 5238 |
{$ENDIF} |
| 5239 |
|
| 5240 |
OP_ANYBUT: |
| 5241 |
{$IFDEF UNICODEEX} |
| 5242 |
begin |
| 5243 |
i := 0; |
| 5244 |
while (i < TheMax) and not FindInCharClass(opnd, scan^) do |
| 5245 |
begin |
| 5246 |
Inc(i); |
| 5247 |
IncUnicode2(scan, Result); |
| 5248 |
end; |
| 5249 |
end; |
| 5250 |
{$ELSE} |
| 5251 |
while (Result < TheMax) and not FindInCharClass(opnd, scan^) do |
| 5252 |
begin |
| 5253 |
Inc(Result); |
| 5254 |
Inc(scan); |
| 5255 |
end; |
| 5256 |
{$ENDIF} |
| 5257 |
|
| 5258 |
OP_ANYOF_CI: |
| 5259 |
{$IFDEF UNICODEEX} |
| 5260 |
begin |
| 5261 |
i := 0; |
| 5262 |
while (i < TheMax) and FindInCharClass(opnd, _UpperCase(scan^)) do |
| 5263 |
begin |
| 5264 |
Inc(i); |
| 5265 |
IncUnicode2(scan, Result); |
| 5266 |
end; |
| 5267 |
end; |
| 5268 |
{$ELSE} |
| 5269 |
while (Result < TheMax) and FindInCharClass(opnd, _UpperCase(scan^)) do |
| 5270 |
begin |
| 5271 |
Inc(Result); |
| 5272 |
Inc(scan); |
| 5273 |
end; |
| 5274 |
{$ENDIF} |
| 5275 |
|
| 5276 |
OP_ANYBUT_CI: |
| 5277 |
{$IFDEF UNICODEEX} |
| 5278 |
begin |
| 5279 |
i := 0; |
| 5280 |
while (i < TheMax) and not FindInCharClass(opnd, _UpperCase(scan^)) do |
| 5281 |
begin |
| 5282 |
Inc(i); |
| 5283 |
IncUnicode2(scan, Result); |
| 5284 |
end; |
| 5285 |
end; |
| 5286 |
{$ELSE} |
| 5287 |
while (Result < TheMax) and not FindInCharClass(opnd, _UpperCase(scan^)) do |
| 5288 |
begin |
| 5289 |
Inc(Result); |
| 5290 |
Inc(scan); |
| 5291 |
end; |
| 5292 |
{$ENDIF} |
| 5293 |
|
| 5294 |
{$IFDEF FastUnicodeData} |
| 5295 |
OP_ANYCATEGORY: |
| 5296 |
{$IFDEF UNICODEEX} |
| 5297 |
begin |
| 5298 |
i := 0; |
| 5299 |
while (i < TheMax) and MatchOneCharCategory(opnd, scan) do |
| 5300 |
begin |
| 5301 |
Inc(i); |
| 5302 |
IncUnicode2(scan, Result); |
| 5303 |
end; |
| 5304 |
end; |
| 5305 |
{$ELSE} |
| 5306 |
while (Result < TheMax) and MatchOneCharCategory(opnd, scan) do |
| 5307 |
begin |
| 5308 |
Inc(Result); |
| 5309 |
Inc(scan); |
| 5310 |
end; |
| 5311 |
{$ENDIF} |
| 5312 |
|
| 5313 |
OP_NOTCATEGORY: |
| 5314 |
{$IFDEF UNICODEEX} |
| 5315 |
begin |
| 5316 |
i := 0; |
| 5317 |
while (i < TheMax) and not MatchOneCharCategory(opnd, scan) do |
| 5318 |
begin |
| 5319 |
Inc(i); |
| 5320 |
IncUnicode2(scan, Result); |
| 5321 |
end; |
| 5322 |
end; |
| 5323 |
{$ELSE} |
| 5324 |
while (Result < TheMax) and not MatchOneCharCategory(opnd, scan) do |
| 5325 |
begin |
| 5326 |
Inc(Result); |
| 5327 |
Inc(scan); |
| 5328 |
end; |
| 5329 |
{$ENDIF} |
| 5330 |
{$ENDIF} |
| 5331 |
|
| 5332 |
OP_ANYLINEBREAK: |
| 5333 |
while (Result < TheMax) and IsAnyLineBreak(scan^) do |
| 5334 |
begin |
| 5335 |
Inc(Result); |
| 5336 |
Inc(scan); |
| 5337 |
end; |
| 5338 |
|
| 5339 |
else |
| 5340 |
Result := 0; |
| 5341 |
Error(reeRegRepeatCalledInappropriately); |
| 5342 |
Exit; |
| 5343 |
end; { of case } |
| 5344 |
regInput := scan; |
| 5345 |
end; { of function TRegExpr.FindRepeated |
| 5346 |
-------------------------------------------------------------- } |
| 5347 |
|
| 5348 |
function TRegExpr.regNext(p: PRegExprChar): PRegExprChar; |
| 5349 |
// dig the "next" pointer out of a node |
| 5350 |
var |
| 5351 |
offset: TRENextOff; |
| 5352 |
begin |
| 5353 |
if p = @regDummy then |
| 5354 |
begin |
| 5355 |
Result := nil; |
| 5356 |
Exit; |
| 5357 |
end; |
| 5358 |
offset := PRENextOff(AlignToPtr(p + REOpSz))^; |
| 5359 |
if offset = 0 then |
| 5360 |
Result := nil |
| 5361 |
else |
| 5362 |
Result := p + offset; |
| 5363 |
end; |
| 5364 |
|
| 5365 |
function TRegExpr.regNextQuick(p: PRegExprChar): PRegExprChar; {$IFDEF FPC}inline;{$ENDIF} |
| 5366 |
{$IFDEF WITH_REGEX_ASSERT} |
| 5367 |
var |
| 5368 |
offset: TRENextOff; |
| 5369 |
{$ENDIF} |
| 5370 |
begin |
| 5371 |
// The inlined version is never called in the first pass. |
| 5372 |
Assert(fSecondPass); // fSecondPass will also be true in MatchPrim. |
| 5373 |
{$IFDEF WITH_REGEX_ASSERT} |
| 5374 |
offset := PRENextOff(AlignToPtr(p + REOpSz))^; |
| 5375 |
if offset = 0 then |
| 5376 |
Result := nil |
| 5377 |
else |
| 5378 |
begin |
| 5379 |
Result := p + offset; |
| 5380 |
assert((Result >= programm) and (Result < programm + regCodeSize * SizeOf(REChar))); |
| 5381 |
end; |
| 5382 |
{$ELSE} |
| 5383 |
Result := p + PRENextOff(AlignToPtr(p + REOpSz))^; |
| 5384 |
{$ENDIF} |
| 5385 |
end; |
| 5386 |
|
| 5387 |
function TRegExpr.regLast(p: PRegExprChar): PRegExprChar; |
| 5388 |
var |
| 5389 |
temp: PRegExprChar; |
| 5390 |
begin |
| 5391 |
Result := p; |
| 5392 |
if p = @regDummy then |
| 5393 |
Exit; |
| 5394 |
// Find last node. |
| 5395 |
repeat |
| 5396 |
temp := regNext(Result); |
| 5397 |
if temp = nil then |
| 5398 |
Break; |
| 5399 |
Result := temp; |
| 5400 |
until False; |
| 5401 |
end; |
| 5402 |
|
| 5403 |
type |
| 5404 |
TRegExprMatchPrimLocals = record |
| 5405 |
case TREOp of |
| 5406 |
{$IFDEF ComplexBraces} |
| 5407 |
OP_LOOPENTRY: ( |
| 5408 |
LoopInfo: TOpLoopInfo; |
| 5409 |
); |
| 5410 |
OP_LOOP: ( // and OP_LOOP_NG |
| 5411 |
LoopInfoListPtr: POpLoopInfo; |
| 5412 |
); |
| 5413 |
{$ENDIF} |
| 5414 |
OP_LOOKAHEAD, OP_LOOKBEHIND: ( |
| 5415 |
IsGreedy: REChar; |
| 5416 |
LookAroundInfo: TRegExprLookAroundInfo; |
| 5417 |
InpStart: PRegExprChar; // only OP_LOOKBEHIND |
| 5418 |
); |
| 5419 |
OP_LOOKAHEAD_END, OP_LOOKBEHIND_END: ( |
| 5420 |
LookAroundInfoPtr: PRegExprLookAroundInfo; |
| 5421 |
); |
| 5422 |
OP_SUBCALL: ( |
| 5423 |
savedCurrentSubCalled: Integer; |
| 5424 |
); |
| 5425 |
OP_STAR: ( |
| 5426 |
nextch: REChar; |
| 5427 |
); |
| 5428 |
end; |
| 5429 |
|
| 5430 |
function TRegExpr.MatchPrim(prog: PRegExprChar): Boolean; |
| 5431 |
// recursively matching routine |
| 5432 |
// Conceptually the strategy is simple: check to see whether the current |
| 5433 |
// node matches, call self recursively to see whether the rest matches, |
| 5434 |
// and then act accordingly. In practice we make some effort to avoid |
| 5435 |
// recursion, in particular by going through "ordinary" nodes (that don't |
| 5436 |
// need to know whether the rest of the match failed) by a loop instead of |
| 5437 |
// by recursion. |
| 5438 |
|
| 5439 |
var |
| 5440 |
scan: PRegExprChar; |
| 5441 |
next: PRegExprChar; // next node |
| 5442 |
opnd, save: PRegExprChar; |
| 5443 |
no: Integer; |
| 5444 |
LoopCnt: Integer; |
| 5445 |
Local: TRegExprMatchPrimLocals; |
| 5446 |
begin |
| 5447 |
Result := False; |
| 5448 |
{$IFDEF RegExpWithStackOverflowCheck_DecStack_Frame} |
| 5449 |
if get_frame < StackLimit then begin |
| 5450 |
error(reeLoopStackExceeded); |
| 5451 |
exit; |
| 5452 |
end; |
| 5453 |
{$ENDIF} |
| 5454 |
|
| 5455 |
|
| 5456 |
{ |
| 5457 |
// Alexey: not sure it's ok for long searches in big texts, so disabled |
| 5458 |
if regNestedCalls > MaxRegexBackTracking then |
| 5459 |
Exit; |
| 5460 |
Inc(regNestedCalls); |
| 5461 |
} |
| 5462 |
|
| 5463 |
scan := prog; |
| 5464 |
while True do |
| 5465 |
begin |
| 5466 |
Assert(scan <> nil); |
| 5467 |
next := regNextQuick(scan); |
| 5468 |
|
| 5469 |
case scan^ of |
| 5470 |
OP_BOUND: |
| 5471 |
begin |
| 5472 |
if ( (regInput = fInputStart) or not IsWordChar((regInput - 1)^) ) |
| 5473 |
= |
| 5474 |
( (regInput >= fInputEnd) or not IsWordChar(regInput^) ) |
| 5475 |
then |
| 5476 |
Exit; |
| 5477 |
end; |
| 5478 |
|
| 5479 |
OP_NOTBOUND: |
| 5480 |
begin |
| 5481 |
if ( (regInput = fInputStart) or not IsWordChar((regInput - 1)^) ) |
| 5482 |
<> |
| 5483 |
( (regInput >= fInputEnd) or not IsWordChar(regInput^) ) |
| 5484 |
then |
| 5485 |
Exit; |
| 5486 |
end; |
| 5487 |
|
| 5488 |
OP_BOL: |
| 5489 |
begin |
| 5490 |
if regInput <> fInputStart then |
| 5491 |
Exit; |
| 5492 |
end; |
| 5493 |
|
| 5494 |
OP_CONTINUE_POS: |
| 5495 |
begin |
| 5496 |
if regInput <> fInputContinue then |
| 5497 |
Exit; |
| 5498 |
end; |
| 5499 |
|
| 5500 |
OP_RESET_MATCHPOS: |
| 5501 |
begin |
| 5502 |
save := GrpBounds[0].GrpStart[0]; |
| 5503 |
GrpBounds[0].GrpStart[0] := regInput; |
| 5504 |
Result := MatchPrim(next); |
| 5505 |
if not Result then |
| 5506 |
GrpBounds[0].GrpStart[0] := save; |
| 5507 |
exit; |
| 5508 |
end; |
| 5509 |
|
| 5510 |
OP_EOL: |
| 5511 |
begin |
| 5512 |
// \z matches at the very end |
| 5513 |
if regInput < fInputEnd then |
| 5514 |
Exit; |
| 5515 |
end; |
| 5516 |
|
| 5517 |
OP_EOL2: |
| 5518 |
begin |
| 5519 |
// \Z matches at the very and + before the final line-break (LF and CR LF) |
| 5520 |
if regInput < fInputEnd then |
| 5521 |
begin |
| 5522 |
if (regInput = fInputEnd - 1) and (regInput^ = #10) then |
| 5523 |
begin end |
| 5524 |
else |
| 5525 |
if (regInput = fInputEnd - 2) and (regInput^ = #13) and ((regInput + 1) ^ = #10) then |
| 5526 |
begin end |
| 5527 |
else |
| 5528 |
Exit; |
| 5529 |
end; |
| 5530 |
end; |
| 5531 |
|
| 5532 |
OP_BOL_ML: |
| 5533 |
if regInput > fInputStart then |
| 5534 |
begin |
| 5535 |
if ((regInput - 1) <= fInputStart) or |
| 5536 |
not IsPairedBreak(regInput - 2) then |
| 5537 |
begin |
| 5538 |
// don't stop between paired separator |
| 5539 |
if IsPairedBreak(regInput - 1) then |
| 5540 |
Exit; |
| 5541 |
if not IsCustomLineSeparator((regInput - 1)^) then |
| 5542 |
Exit; |
| 5543 |
end; |
| 5544 |
end; |
| 5545 |
|
| 5546 |
OP_EOL_ML: |
| 5547 |
if regInput < fInputEnd then |
| 5548 |
begin |
| 5549 |
if not IsPairedBreak(regInput) then |
| 5550 |
begin |
| 5551 |
// don't stop between paired separator |
| 5552 |
if (regInput > fInputStart) and IsPairedBreak(regInput - 1) then |
| 5553 |
Exit; |
| 5554 |
if not IsCustomLineSeparator(regInput^) then |
| 5555 |
Exit; |
| 5556 |
end; |
| 5557 |
end; |
| 5558 |
|
| 5559 |
OP_ANY: |
| 5560 |
begin |
| 5561 |
if regInput >= fInputCurrentEnd then |
| 5562 |
Exit; |
| 5563 |
{$IFDEF UNICODEEX} |
| 5564 |
IncUnicode(regInput); |
| 5565 |
{$ELSE} |
| 5566 |
Inc(regInput); |
| 5567 |
{$ENDIF} |
| 5568 |
end; |
| 5569 |
|
| 5570 |
OP_ANY_ML: |
| 5571 |
begin |
| 5572 |
if (regInput >= fInputCurrentEnd) or |
| 5573 |
IsPairedBreak(regInput) or |
| 5574 |
IsCustomLineSeparator(regInput^) |
| 5575 |
then |
| 5576 |
Exit; |
| 5577 |
{$IFDEF UNICODEEX} |
| 5578 |
IncUnicode(regInput); |
| 5579 |
{$ELSE} |
| 5580 |
Inc(regInput); |
| 5581 |
{$ENDIF} |
| 5582 |
end; |
| 5583 |
|
| 5584 |
OP_ANYDIGIT: |
| 5585 |
begin |
| 5586 |
if (regInput >= fInputCurrentEnd) or not IsDigitChar(regInput^) then |
| 5587 |
Exit; |
| 5588 |
Inc(regInput); |
| 5589 |
end; |
| 5590 |
|
| 5591 |
OP_NOTDIGIT: |
| 5592 |
begin |
| 5593 |
if (regInput >= fInputCurrentEnd) or IsDigitChar(regInput^) then |
| 5594 |
Exit; |
| 5595 |
{$IFDEF UNICODEEX} |
| 5596 |
IncUnicode(regInput); |
| 5597 |
{$ELSE} |
| 5598 |
Inc(regInput); |
| 5599 |
{$ENDIF} |
| 5600 |
end; |
| 5601 |
|
| 5602 |
OP_ANYLETTER: |
| 5603 |
begin |
| 5604 |
if (regInput >= fInputCurrentEnd) or not IsWordChar(regInput^) then |
| 5605 |
Exit; |
| 5606 |
Inc(regInput); |
| 5607 |
end; |
| 5608 |
|
| 5609 |
OP_NOTLETTER: |
| 5610 |
begin |
| 5611 |
if (regInput >= fInputCurrentEnd) or IsWordChar(regInput^) then |
| 5612 |
Exit; |
| 5613 |
{$IFDEF UNICODEEX} |
| 5614 |
IncUnicode(regInput); |
| 5615 |
{$ELSE} |
| 5616 |
Inc(regInput); |
| 5617 |
{$ENDIF} |
| 5618 |
end; |
| 5619 |
|
| 5620 |
OP_ANYSPACE: |
| 5621 |
begin |
| 5622 |
if (regInput >= fInputCurrentEnd) or not IsSpaceChar(regInput^) then |
| 5623 |
Exit; |
| 5624 |
Inc(regInput); |
| 5625 |
end; |
| 5626 |
|
| 5627 |
OP_NOTSPACE: |
| 5628 |
begin |
| 5629 |
if (regInput >= fInputCurrentEnd) or IsSpaceChar(regInput^) then |
| 5630 |
Exit; |
| 5631 |
{$IFDEF UNICODEEX} |
| 5632 |
IncUnicode(regInput); |
| 5633 |
{$ELSE} |
| 5634 |
Inc(regInput); |
| 5635 |
{$ENDIF} |
| 5636 |
end; |
| 5637 |
|
| 5638 |
OP_ANYVERTSEP: |
| 5639 |
begin |
| 5640 |
if (regInput >= fInputCurrentEnd) or not IsVertLineSeparator(regInput^) then |
| 5641 |
Exit; |
| 5642 |
Inc(regInput); |
| 5643 |
end; |
| 5644 |
|
| 5645 |
OP_NOTVERTSEP: |
| 5646 |
begin |
| 5647 |
if (regInput >= fInputCurrentEnd) or IsVertLineSeparator(regInput^) then |
| 5648 |
Exit; |
| 5649 |
{$IFDEF UNICODEEX} |
| 5650 |
IncUnicode(regInput); |
| 5651 |
{$ELSE} |
| 5652 |
Inc(regInput); |
| 5653 |
{$ENDIF} |
| 5654 |
end; |
| 5655 |
|
| 5656 |
OP_ANYHORZSEP: |
| 5657 |
begin |
| 5658 |
if (regInput >= fInputCurrentEnd) or not IsHorzSeparator(regInput^) then |
| 5659 |
Exit; |
| 5660 |
Inc(regInput); |
| 5661 |
end; |
| 5662 |
|
| 5663 |
OP_NOTHORZSEP: |
| 5664 |
begin |
| 5665 |
if (regInput >= fInputCurrentEnd) or IsHorzSeparator(regInput^) then |
| 5666 |
Exit; |
| 5667 |
{$IFDEF UNICODEEX} |
| 5668 |
IncUnicode(regInput); |
| 5669 |
{$ELSE} |
| 5670 |
Inc(regInput); |
| 5671 |
{$ENDIF} |
| 5672 |
end; |
| 5673 |
|
| 5674 |
OP_EXACTLY_CI: |
| 5675 |
begin |
| 5676 |
opnd := scan + REOpSz + RENextOffSz; // OPERAND |
| 5677 |
no := PLongInt(opnd)^; |
| 5678 |
if (regInput + no > fInputCurrentEnd) then |
| 5679 |
Exit; |
| 5680 |
Inc(opnd, RENumberSz); |
| 5681 |
// Inline the first character, for speed. |
| 5682 |
if (opnd^ <> regInput^) and (_LowerCase(opnd^) <> regInput^) then |
| 5683 |
Exit; |
| 5684 |
save := regInput; |
| 5685 |
Inc(regInput, no); |
| 5686 |
while no > 1 do |
| 5687 |
begin |
| 5688 |
Inc(save); |
| 5689 |
Inc(opnd); |
| 5690 |
if (opnd^ <> save^) and (_LowerCase(opnd^) <> save^) then |
| 5691 |
Exit; |
| 5692 |
Dec(no); |
| 5693 |
end; |
| 5694 |
end; |
| 5695 |
|
| 5696 |
OP_EXACTLY: |
| 5697 |
begin |
| 5698 |
opnd := scan + REOpSz + RENextOffSz; // OPERAND |
| 5699 |
no := PLongInt(opnd)^; |
| 5700 |
if (regInput + no > fInputCurrentEnd) then |
| 5701 |
Exit; |
| 5702 |
Inc(opnd, RENumberSz); |
| 5703 |
// Inline the first character, for speed. |
| 5704 |
if opnd^ <> regInput^ then |
| 5705 |
Exit; |
| 5706 |
save := regInput; |
| 5707 |
Inc(regInput, no); |
| 5708 |
while no > 1 do |
| 5709 |
begin |
| 5710 |
Inc(save); |
| 5711 |
Inc(opnd); |
| 5712 |
if opnd^ <> save^ then |
| 5713 |
Exit; |
| 5714 |
Dec(no); |
| 5715 |
end; |
| 5716 |
end; |
| 5717 |
|
| 5718 |
OP_BSUBEXP: |
| 5719 |
begin |
| 5720 |
no := PReGroupIndex((scan + REOpSz + RENextOffSz))^; |
| 5721 |
if no < 0 then |
| 5722 |
Exit; |
| 5723 |
opnd := CurrentGrpBounds.GrpStart[no]; |
| 5724 |
if opnd = nil then |
| 5725 |
Exit; |
| 5726 |
save := CurrentGrpBounds.GrpEnd[no]; |
| 5727 |
if save = nil then |
| 5728 |
Exit; |
| 5729 |
no := save - opnd; |
| 5730 |
save := regInput; |
| 5731 |
if save + no - 1 >= fInputCurrentEnd then |
| 5732 |
Exit; |
| 5733 |
|
| 5734 |
while no > 0 do |
| 5735 |
begin |
| 5736 |
if (save^ <> opnd^) then |
| 5737 |
Exit; |
| 5738 |
Inc(save); |
| 5739 |
Inc(opnd); |
| 5740 |
Dec(no); |
| 5741 |
end; |
| 5742 |
regInput := save; |
| 5743 |
end; |
| 5744 |
|
| 5745 |
OP_BSUBEXP_CI: |
| 5746 |
begin |
| 5747 |
no := PReGroupIndex((scan + REOpSz + RENextOffSz))^; |
| 5748 |
if no < 0 then |
| 5749 |
Exit; |
| 5750 |
opnd := CurrentGrpBounds.GrpStart[no]; |
| 5751 |
if opnd = nil then |
| 5752 |
Exit; |
| 5753 |
save := CurrentGrpBounds.GrpEnd[no]; |
| 5754 |
if save = nil then |
| 5755 |
Exit; |
| 5756 |
no := save - opnd; |
| 5757 |
save := regInput; |
| 5758 |
if save + no - 1 >= fInputCurrentEnd then |
| 5759 |
Exit; |
| 5760 |
|
| 5761 |
while no > 0 do |
| 5762 |
begin |
| 5763 |
if ((save^ <> opnd^) and (save^ <> InvertCase(opnd^))) then |
| 5764 |
Exit; |
| 5765 |
Inc(save); |
| 5766 |
Inc(opnd); |
| 5767 |
Dec(no); |
| 5768 |
end; |
| 5769 |
regInput := save; |
| 5770 |
end; |
| 5771 |
|
| 5772 |
OP_ANYOF: |
| 5773 |
begin |
| 5774 |
if (regInput >= fInputCurrentEnd) or |
| 5775 |
not FindInCharClass(scan + REOpSz + RENextOffSz, regInput^) then |
| 5776 |
Exit; |
| 5777 |
{$IFDEF UNICODEEX} |
| 5778 |
IncUnicode(regInput); |
| 5779 |
{$ELSE} |
| 5780 |
Inc(regInput); |
| 5781 |
{$ENDIF} |
| 5782 |
end; |
| 5783 |
|
| 5784 |
OP_ANYBUT: |
| 5785 |
begin |
| 5786 |
if (regInput >= fInputCurrentEnd) or |
| 5787 |
FindInCharClass(scan + REOpSz + RENextOffSz, regInput^) then |
| 5788 |
Exit; |
| 5789 |
{$IFDEF UNICODEEX} |
| 5790 |
IncUnicode(regInput); |
| 5791 |
{$ELSE} |
| 5792 |
Inc(regInput); |
| 5793 |
{$ENDIF} |
| 5794 |
end; |
| 5795 |
|
| 5796 |
OP_ANYOF_CI: |
| 5797 |
begin |
| 5798 |
if (regInput >= fInputCurrentEnd) or |
| 5799 |
not FindInCharClass(scan + REOpSz + RENextOffSz, _UpperCase(regInput^)) then |
| 5800 |
Exit; |
| 5801 |
{$IFDEF UNICODEEX} |
| 5802 |
IncUnicode(regInput); |
| 5803 |
{$ELSE} |
| 5804 |
Inc(regInput); |
| 5805 |
{$ENDIF} |
| 5806 |
end; |
| 5807 |
|
| 5808 |
OP_ANYBUT_CI: |
| 5809 |
begin |
| 5810 |
if (regInput >= fInputCurrentEnd) or |
| 5811 |
FindInCharClass(scan + REOpSz + RENextOffSz, _UpperCase(regInput^)) then |
| 5812 |
Exit; |
| 5813 |
{$IFDEF UNICODEEX} |
| 5814 |
IncUnicode(regInput); |
| 5815 |
{$ELSE} |
| 5816 |
Inc(regInput); |
| 5817 |
{$ENDIF} |
| 5818 |
end; |
| 5819 |
|
| 5820 |
OP_NOTHING: |
| 5821 |
; |
| 5822 |
OP_COMMENT: |
| 5823 |
; |
| 5824 |
OP_BACK: |
| 5825 |
; |
| 5826 |
|
| 5827 |
OP_OPEN: |
| 5828 |
begin |
| 5829 |
no := PReGroupIndex((scan + REOpSz + RENextOffSz))^; |
| 5830 |
save := CurrentGrpBounds.TmpStart[no]; |
| 5831 |
CurrentGrpBounds.TmpStart[no] := regInput; |
| 5832 |
Result := MatchPrim(next); |
| 5833 |
CurrentGrpBounds.TmpStart[no] := save; |
| 5834 |
exit; |
| 5835 |
end; |
| 5836 |
|
| 5837 |
OP_OPEN_ATOMIC: |
| 5838 |
begin |
| 5839 |
no := PReGroupIndex((scan + REOpSz + RENextOffSz))^; |
| 5840 |
Result := MatchPrim(next); |
| 5841 |
if GrpBacktrackingAsAtom[no] then |
| 5842 |
IsBacktrackingGroupAsAtom := False; |
| 5843 |
GrpBacktrackingAsAtom[no] := False; |
| 5844 |
Exit; |
| 5845 |
end; |
| 5846 |
|
| 5847 |
OP_CLOSE: |
| 5848 |
begin |
| 5849 |
no := PReGroupIndex((scan + REOpSz + RENextOffSz))^; |
| 5850 |
save := CurrentGrpBounds.GrpStart[no]; |
| 5851 |
opnd := CurrentGrpBounds.GrpEnd[no]; // save2 |
| 5852 |
CurrentGrpBounds.GrpStart[no] := CurrentGrpBounds.TmpStart[no]; |
| 5853 |
CurrentGrpBounds.GrpEnd[no] := regInput; |
| 5854 |
|
| 5855 |
// if we are in OP_SUBCALL* call, it called OP_OPEN*, so we must return |
| 5856 |
// in OP_CLOSE, without going to next opcode |
| 5857 |
if CurrentSubCalled = no then |
| 5858 |
begin |
| 5859 |
Result := True; |
| 5860 |
Exit; |
| 5861 |
end; |
| 5862 |
|
| 5863 |
Result := MatchPrim(next); |
| 5864 |
if not Result then begin |
| 5865 |
CurrentGrpBounds.GrpStart[no] := save; |
| 5866 |
CurrentGrpBounds.GrpEnd[no] := opnd; |
| 5867 |
end; |
| 5868 |
|
| 5869 |
Exit; |
| 5870 |
end; |
| 5871 |
|
| 5872 |
OP_CLOSE_ATOMIC: |
| 5873 |
begin |
| 5874 |
no := PReGroupIndex((scan + REOpSz + RENextOffSz))^; |
| 5875 |
// handle atomic group, mark it as "done" |
| 5876 |
// (we are here because some OP_BRANCH is matched) |
| 5877 |
Result := MatchPrim(next); |
| 5878 |
if not Result then begin |
| 5879 |
if not IsBacktrackingGroupAsAtom then begin |
| 5880 |
GrpBacktrackingAsAtom[no] := True; |
| 5881 |
IsBacktrackingGroupAsAtom := True; |
| 5882 |
end; |
| 5883 |
end; |
| 5884 |
Exit; |
| 5885 |
end; |
| 5886 |
|
| 5887 |
OP_LOOKAHEAD, OP_LOOKAHEAD_NEG: |
| 5888 |
begin |
| 5889 |
Local.LookAroundInfo.InputPos := regInput; |
| 5890 |
Local.LookAroundInfo.IsNegative := (scan^ = OP_LOOKAHEAD_NEG); |
| 5891 |
Local.LookAroundInfo.HasMatchedToEnd := False; |
| 5892 |
Local.LookAroundInfo.IsBackTracking := False; |
| 5893 |
Local.LookAroundInfo.OuterInfo := LookAroundInfoList; |
| 5894 |
Local.LookAroundInfo.savedInputCurrentEnd := fInputCurrentEnd; |
| 5895 |
LookAroundInfoList := @Local.LookAroundInfo; |
| 5896 |
fInputCurrentEnd := fInputEnd; |
| 5897 |
|
| 5898 |
scan := PRegExprChar(AlignToPtr(scan + 1)) + RENextOffSz; |
| 5899 |
Result := MatchPrim(scan); |
| 5900 |
|
| 5901 |
if Local.LookAroundInfo.IsBackTracking then |
| 5902 |
IsBacktrackingGroupAsAtom := False; |
| 5903 |
LookAroundInfoList := Local.LookAroundInfo.OuterInfo; |
| 5904 |
fInputCurrentEnd := Local.LookAroundInfo.savedInputCurrentEnd; |
| 5905 |
|
| 5906 |
if Local.LookAroundInfo.IsNegative then begin |
| 5907 |
Result := (not Local.LookAroundInfo.HasMatchedToEnd); |
| 5908 |
if Result then begin |
| 5909 |
next := regNextQuick(next); // Next-Pointer of OP_LOOKAHEAD_END |
| 5910 |
regInput := Local.LookAroundInfo.InputPos; |
| 5911 |
Result := False; |
| 5912 |
scan := next; |
| 5913 |
continue; |
| 5914 |
end; |
| 5915 |
end; |
| 5916 |
|
| 5917 |
Exit; |
| 5918 |
end; |
| 5919 |
|
| 5920 |
OP_LOOKBEHIND, OP_LOOKBEHIND_NEG: |
| 5921 |
begin |
| 5922 |
Local.LookAroundInfo.InputPos := regInput; |
| 5923 |
Local.LookAroundInfo.IsNegative := (scan^ = OP_LOOKBEHIND_NEG); |
| 5924 |
Local.LookAroundInfo.HasMatchedToEnd := False; |
| 5925 |
Local.LookAroundInfo.IsBackTracking := False; |
| 5926 |
Local.LookAroundInfo.OuterInfo := LookAroundInfoList; |
| 5927 |
Local.LookAroundInfo.savedInputCurrentEnd := fInputCurrentEnd; |
| 5928 |
LookAroundInfoList := @Local.LookAroundInfo; |
| 5929 |
|
| 5930 |
scan := PRegExprChar(AlignToPtr(scan + 1)) + RENextOffSz; |
| 5931 |
Local.IsGreedy := PReOpLookBehindOptions(scan)^.IsGreedy; |
| 5932 |
|
| 5933 |
fInputCurrentEnd := regInput; |
| 5934 |
|
| 5935 |
Result := regInput - fInputStart >= PReOpLookBehindOptions(scan)^.MatchLenMin; |
| 5936 |
if Result then begin |
| 5937 |
if Local.IsGreedy = OPT_LOOKBEHIND_FIXED then begin |
| 5938 |
regInput := regInput - PReOpLookBehindOptions(scan)^.MatchLenMin; |
| 5939 |
inc(scan, ReOpLookBehindOptionsSz); |
| 5940 |
Result := MatchPrim(scan) |
| 5941 |
end |
| 5942 |
else |
| 5943 |
if Local.IsGreedy = OPT_LOOKBEHIND_NON_GREEDY then begin |
| 5944 |
Local.InpStart := regInput - PReOpLookBehindOptions(scan)^.MatchLenMin; |
| 5945 |
if regInput - fInputStart >= PReOpLookBehindOptions(scan)^.MatchLenMax then |
| 5946 |
save := regInput - PReOpLookBehindOptions(scan)^.MatchLenMax |
| 5947 |
else |
| 5948 |
save := fInputStart; |
| 5949 |
inc(scan, ReOpLookBehindOptionsSz); |
| 5950 |
repeat |
| 5951 |
regInput := Local.InpStart; |
| 5952 |
dec(Local.InpStart); |
| 5953 |
Result := MatchPrim(scan); |
| 5954 |
until Local.LookAroundInfo.HasMatchedToEnd or (Local.InpStart < save); |
| 5955 |
end |
| 5956 |
else begin |
| 5957 |
if regInput - fInputStart >= PReOpLookBehindOptions(scan)^.MatchLenMax then |
| 5958 |
Local.InpStart := regInput - PReOpLookBehindOptions(scan)^.MatchLenMax |
| 5959 |
else |
| 5960 |
Local.InpStart := fInputStart; |
| 5961 |
save := Local.LookAroundInfo.InputPos - PReOpLookBehindOptions(scan)^.MatchLenMin; |
| 5962 |
inc(scan, ReOpLookBehindOptionsSz); |
| 5963 |
repeat |
| 5964 |
regInput := Local.InpStart; |
| 5965 |
inc(Local.InpStart); |
| 5966 |
Result := MatchPrim(scan); |
| 5967 |
until Local.LookAroundInfo.HasMatchedToEnd or (Local.InpStart > save); |
| 5968 |
end; |
| 5969 |
end; |
| 5970 |
|
| 5971 |
if Local.LookAroundInfo.IsBackTracking then |
| 5972 |
IsBacktrackingGroupAsAtom := False; |
| 5973 |
LookAroundInfoList := Local.LookAroundInfo.OuterInfo; |
| 5974 |
fInputCurrentEnd := Local.LookAroundInfo.savedInputCurrentEnd; |
| 5975 |
|
| 5976 |
if Local.LookAroundInfo.IsNegative then begin |
| 5977 |
Result := not Local.LookAroundInfo.HasMatchedToEnd; |
| 5978 |
if Result then begin |
| 5979 |
next := regNextQuick(next); // Next-Pointer of OP_LOOKAHEAD_END |
| 5980 |
regInput := Local.LookAroundInfo.InputPos; |
| 5981 |
Result := False; |
| 5982 |
scan := next; |
| 5983 |
continue; |
| 5984 |
end; |
| 5985 |
end; |
| 5986 |
|
| 5987 |
Exit; |
| 5988 |
end; |
| 5989 |
|
| 5990 |
OP_LOOKAHEAD_END: |
| 5991 |
begin |
| 5992 |
if LookAroundInfoList = nil then |
| 5993 |
Exit; |
| 5994 |
Local.LookAroundInfoPtr := LookAroundInfoList; |
| 5995 |
Local.LookAroundInfoPtr.HasMatchedToEnd := True; |
| 5996 |
|
| 5997 |
if not Local.LookAroundInfoPtr^.IsNegative then begin |
| 5998 |
fInputCurrentEnd := Local.LookAroundInfoPtr^.savedInputCurrentEnd; |
| 5999 |
regInput := Local.LookAroundInfoPtr^.InputPos; |
| 6000 |
LookAroundInfoList := Local.LookAroundInfoPtr^.OuterInfo; |
| 6001 |
|
| 6002 |
Result := MatchPrim(next); |
| 6003 |
LookAroundInfoList := Local.LookAroundInfoPtr; |
| 6004 |
end; |
| 6005 |
|
| 6006 |
if (not Result) and not IsBacktrackingGroupAsAtom then begin |
| 6007 |
IsBacktrackingGroupAsAtom := True; |
| 6008 |
Local.LookAroundInfoPtr.IsBackTracking := True; |
| 6009 |
end; |
| 6010 |
Exit; |
| 6011 |
end; |
| 6012 |
|
| 6013 |
OP_LOOKBEHIND_END: |
| 6014 |
begin |
| 6015 |
if LookAroundInfoList = nil then |
| 6016 |
Exit; |
| 6017 |
|
| 6018 |
Local.LookAroundInfoPtr := LookAroundInfoList; |
| 6019 |
if not (Local.LookAroundInfoPtr^.InputPos = regInput) then |
| 6020 |
Exit; |
| 6021 |
|
| 6022 |
Local.LookAroundInfoPtr.HasMatchedToEnd := True; |
| 6023 |
|
| 6024 |
if not Local.LookAroundInfoPtr^.IsNegative then begin |
| 6025 |
regInput := Local.LookAroundInfoPtr^.InputPos; |
| 6026 |
fInputCurrentEnd := Local.LookAroundInfoPtr^.savedInputCurrentEnd; |
| 6027 |
LookAroundInfoList := Local.LookAroundInfoPtr^.OuterInfo; |
| 6028 |
|
| 6029 |
Result := MatchPrim(next); |
| 6030 |
LookAroundInfoList := Local.LookAroundInfoPtr; |
| 6031 |
end; |
| 6032 |
|
| 6033 |
if (not Result) and not IsBacktrackingGroupAsAtom then begin |
| 6034 |
IsBacktrackingGroupAsAtom := True; |
| 6035 |
Local.LookAroundInfoPtr.IsBackTracking := True; |
| 6036 |
end; |
| 6037 |
Exit; |
| 6038 |
end; |
| 6039 |
|
| 6040 |
OP_BRANCH: |
| 6041 |
begin |
| 6042 |
repeat |
| 6043 |
save := regInput; |
| 6044 |
Result := MatchPrim(scan + REOpSz + RENextOffSz + REBranchArgSz); |
| 6045 |
if Result then |
| 6046 |
Exit; |
| 6047 |
// if branch worked until OP_CLOSE, and marked atomic group as "done", then exit |
| 6048 |
regInput := save; |
| 6049 |
if IsBacktrackingGroupAsAtom then |
| 6050 |
Exit; |
| 6051 |
scan := next; |
| 6052 |
Assert(scan <> nil); |
| 6053 |
next := regNextQuick(scan); |
| 6054 |
if (next^ <> OP_BRANCH) then |
| 6055 |
break; |
| 6056 |
until False; |
| 6057 |
next := scan + REOpSz + RENextOffSz + REBranchArgSz; // Avoid recursion |
| 6058 |
end; |
| 6059 |
|
| 6060 |
OP_GBRANCH, OP_GBRANCH_EX, OP_GBRANCH_EX_CI: |
| 6061 |
begin |
| 6062 |
Assert((next^ = OP_BRANCH) or (next^ = OP_GBRANCH) or (next^ = OP_GBRANCH_EX) or (next^ = OP_GBRANCH_EX_CI)); |
| 6063 |
repeat |
| 6064 |
save := regInput; |
| 6065 |
case scan^ of |
| 6066 |
OP_GBRANCH, OP_BRANCH: |
| 6067 |
Result := MatchPrim(scan + REOpSz + RENextOffSz + REBranchArgSz); |
| 6068 |
OP_GBRANCH_EX: |
| 6069 |
if (regInput^ = (scan + REOpSz + RENextOffSz)^) then |
| 6070 |
Result := MatchPrim(scan + REOpSz + RENextOffSz + REBranchArgSz); |
| 6071 |
OP_GBRANCH_EX_CI: |
| 6072 |
if (regInput^ = (scan + REOpSz + RENextOffSz)^) or |
| 6073 |
(regInput^ = (scan + REOpSz + RENextOffSz + 1)^) |
| 6074 |
then |
| 6075 |
Result := MatchPrim(scan + REOpSz + RENextOffSz + REBranchArgSz); |
| 6076 |
end; |
| 6077 |
if Result then |
| 6078 |
Exit; |
| 6079 |
// if branch worked until OP_CLOSE, and marked atomic group as "done", then exit |
| 6080 |
regInput := save; |
| 6081 |
if IsBacktrackingGroupAsAtom then |
| 6082 |
Exit; |
| 6083 |
scan := next; |
| 6084 |
Assert(scan <> nil); |
| 6085 |
next := regNextQuick(scan); |
| 6086 |
if (next^ <> OP_BRANCH) and (next^ <> OP_GBRANCH) and (next^ <> OP_GBRANCH_EX) and (next^ <> OP_GBRANCH_EX_CI) then |
| 6087 |
break; |
| 6088 |
until False; |
| 6089 |
case scan^ of |
| 6090 |
OP_GBRANCH_EX: |
| 6091 |
if (regInput^ <> (scan + REOpSz + RENextOffSz)^) then |
| 6092 |
exit; |
| 6093 |
OP_GBRANCH_EX_CI: |
| 6094 |
if (regInput^ <> (scan + REOpSz + RENextOffSz)^) and |
| 6095 |
(regInput^ <> (scan + REOpSz + RENextOffSz + 1)^) |
| 6096 |
then |
| 6097 |
exit; |
| 6098 |
end; |
| 6099 |
next := scan + REOpSz + RENextOffSz + REBranchArgSz; // Avoid recursion |
| 6100 |
end; |
| 6101 |
|
| 6102 |
{$IFDEF ComplexBraces} |
| 6103 |
OP_LOOPENTRY: |
| 6104 |
begin |
| 6105 |
Local.LoopInfo.Count := 0; |
| 6106 |
Local.LoopInfo.BackTrackingAsAtom := False; |
| 6107 |
Local.LoopInfo.CurrentRegInput := nil; |
| 6108 |
Local.LoopInfo.OuterLoop := CurrentLoopInfoListPtr; |
| 6109 |
CurrentLoopInfoListPtr := @Local.LoopInfo; |
| 6110 |
save := regInput; |
| 6111 |
Result := MatchPrim(next); // execute loop |
| 6112 |
CurrentLoopInfoListPtr := Local.LoopInfo.OuterLoop; |
| 6113 |
if Local.LoopInfo.BackTrackingAsAtom then |
| 6114 |
IsBacktrackingGroupAsAtom := False; |
| 6115 |
if not Result then |
| 6116 |
regInput := save; |
| 6117 |
Exit; |
| 6118 |
end; |
| 6119 |
|
| 6120 |
OP_LOOP, OP_LOOP_POSS: |
| 6121 |
begin |
| 6122 |
if CurrentLoopInfoListPtr = nil then begin |
| 6123 |
Error(reeLoopWithoutEntry); |
| 6124 |
Exit; |
| 6125 |
end; |
| 6126 |
opnd := AlignToPtr(scan + REOpSz + RENextOffSz); |
| 6127 |
Local.LoopInfoListPtr := CurrentLoopInfoListPtr; |
| 6128 |
if Local.LoopInfoListPtr^.Count >= PREBracesArg(opnd)^ then // Min-Count |
| 6129 |
begin // Min alredy matched - we can work |
| 6130 |
LoopCnt := PREBracesArg(opnd + REBracesArgSz)^; // Max-Count |
| 6131 |
Result := (LoopCnt = MaxBracesArg) and // * or + |
| 6132 |
(Local.LoopInfoListPtr^.CurrentRegInput = regInput); |
| 6133 |
if Result then begin |
| 6134 |
CurrentLoopInfoListPtr := Local.LoopInfoListPtr^.OuterLoop; |
| 6135 |
Result := MatchPrim(next); |
| 6136 |
CurrentLoopInfoListPtr := Local.LoopInfoListPtr; |
| 6137 |
if (not Result) and (scan^ = OP_LOOP_POSS) then begin |
| 6138 |
Local.LoopInfoListPtr^.BackTrackingAsAtom := True; |
| 6139 |
IsBacktrackingGroupAsAtom := True; |
| 6140 |
end; |
| 6141 |
exit; |
| 6142 |
end; |
| 6143 |
|
| 6144 |
// greedy way - first try to max deep of greed ;) |
| 6145 |
if Local.LoopInfoListPtr^.Count < LoopCnt then |
| 6146 |
begin |
| 6147 |
save := regInput; |
| 6148 |
Local.LoopInfoListPtr^.CurrentRegInput := save; |
| 6149 |
Inc(Local.LoopInfoListPtr^.Count); |
| 6150 |
Result := MatchPrim(scan + PRENextOff(opnd + 2 * REBracesArgSz)^); |
| 6151 |
if Result or IsBacktrackingGroupAsAtom then |
| 6152 |
Exit; |
| 6153 |
Dec(Local.LoopInfoListPtr^.Count); |
| 6154 |
regInput := save; |
| 6155 |
end; |
| 6156 |
CurrentLoopInfoListPtr := Local.LoopInfoListPtr^.OuterLoop; |
| 6157 |
Result := MatchPrim(next); |
| 6158 |
CurrentLoopInfoListPtr := Local.LoopInfoListPtr; |
| 6159 |
|
| 6160 |
if Result or IsBacktrackingGroupAsAtom then |
| 6161 |
Exit; |
| 6162 |
if (scan^ = OP_LOOP_POSS) then begin |
| 6163 |
Local.LoopInfoListPtr^.BackTrackingAsAtom := True; |
| 6164 |
IsBacktrackingGroupAsAtom := True; |
| 6165 |
end; |
| 6166 |
Exit; |
| 6167 |
end |
| 6168 |
else |
| 6169 |
begin // first match a min_cnt times |
| 6170 |
Inc(Local.LoopInfoListPtr^.Count); |
| 6171 |
Local.LoopInfoListPtr^.CurrentRegInput := regInput; |
| 6172 |
Result := MatchPrim(scan + PRENextOff(opnd + 2 * REBracesArgSz)^); |
| 6173 |
if Result or IsBacktrackingGroupAsAtom then |
| 6174 |
Exit; |
| 6175 |
Dec(Local.LoopInfoListPtr^.Count); |
| 6176 |
Exit; |
| 6177 |
end; |
| 6178 |
end; |
| 6179 |
|
| 6180 |
OP_LOOP_NG: |
| 6181 |
begin |
| 6182 |
if CurrentLoopInfoListPtr = nil then begin |
| 6183 |
Error(reeLoopWithoutEntry); |
| 6184 |
Exit; |
| 6185 |
end; |
| 6186 |
opnd := AlignToPtr(scan + REOpSz + RENextOffSz); |
| 6187 |
Local.LoopInfoListPtr := CurrentLoopInfoListPtr; |
| 6188 |
if Local.LoopInfoListPtr^.Count >= PREBracesArg(opnd)^ then // Min-Count |
| 6189 |
begin // Min alredy matched - we can work |
| 6190 |
LoopCnt := PREBracesArg(opnd + REBracesArgSz)^; // Max-Count |
| 6191 |
Result := (LoopCnt = MaxBracesArg) and // * or + |
| 6192 |
(Local.LoopInfoListPtr^.CurrentRegInput = regInput); |
| 6193 |
if Result then begin |
| 6194 |
CurrentLoopInfoListPtr := Local.LoopInfoListPtr^.OuterLoop; |
| 6195 |
Result := MatchPrim(next); |
| 6196 |
CurrentLoopInfoListPtr := Local.LoopInfoListPtr; |
| 6197 |
exit; |
| 6198 |
end; |
| 6199 |
|
| 6200 |
save := regInput; |
| 6201 |
Local.LoopInfoListPtr^.CurrentRegInput := save; |
| 6202 |
// non-greedy - try just now |
| 6203 |
CurrentLoopInfoListPtr := Local.LoopInfoListPtr^.OuterLoop; |
| 6204 |
Result := MatchPrim(next); |
| 6205 |
CurrentLoopInfoListPtr := Local.LoopInfoListPtr; |
| 6206 |
if Result or IsBacktrackingGroupAsAtom then |
| 6207 |
Exit; |
| 6208 |
if Local.LoopInfoListPtr^.Count < LoopCnt then |
| 6209 |
begin |
| 6210 |
regInput := save; // failed - move next and try again |
| 6211 |
Inc(Local.LoopInfoListPtr^.Count); |
| 6212 |
Result := MatchPrim(scan + PRENextOff(opnd + 2 * REBracesArgSz)^); |
| 6213 |
if Result or IsBacktrackingGroupAsAtom then |
| 6214 |
Exit; |
| 6215 |
Dec(Local.LoopInfoListPtr^.Count); |
| 6216 |
end; |
| 6217 |
Exit; |
| 6218 |
end |
| 6219 |
else |
| 6220 |
begin // first match a min_cnt times |
| 6221 |
Inc(Local.LoopInfoListPtr^.Count); |
| 6222 |
Local.LoopInfoListPtr^.CurrentRegInput := regInput; |
| 6223 |
Result := MatchPrim(scan + PRENextOff(opnd + 2 * REBracesArgSz)^); |
| 6224 |
if Result or IsBacktrackingGroupAsAtom then |
| 6225 |
Exit; |
| 6226 |
Dec(Local.LoopInfoListPtr^.Count); |
| 6227 |
Exit; |
| 6228 |
end; |
| 6229 |
end; |
| 6230 |
{$ENDIF} |
| 6231 |
|
| 6232 |
OP_STAR, OP_PLUS, OP_BRACES: |
| 6233 |
begin |
| 6234 |
opnd := scan + REOpSz + RENextOffSz; |
| 6235 |
save := regInput; |
| 6236 |
case scan^ of |
| 6237 |
OP_STAR: |
| 6238 |
begin |
| 6239 |
no := FindRepeated(opnd, MaxInt); |
| 6240 |
LoopCnt := 0 // star |
| 6241 |
end; |
| 6242 |
OP_PLUS: |
| 6243 |
begin |
| 6244 |
no := FindRepeated(opnd, MaxInt); |
| 6245 |
if no < 1 then |
| 6246 |
Exit; |
| 6247 |
LoopCnt := 1 // star |
| 6248 |
end; |
| 6249 |
else |
| 6250 |
begin // braces |
| 6251 |
opnd := AlignToPtr(opnd); |
| 6252 |
no := FindRepeated(opnd + 2 * REBracesArgSz, PREBracesArg(opnd + REBracesArgSz)^); |
| 6253 |
LoopCnt := PREBracesArg(opnd)^; |
| 6254 |
if no < LoopCnt then |
| 6255 |
Exit; |
| 6256 |
end; |
| 6257 |
end; |
| 6258 |
|
| 6259 |
if next^ = OP_EXACTLY then begin |
| 6260 |
// Lookahead to avoid useless match attempts when we know |
| 6261 |
// what character comes next. |
| 6262 |
Local.nextch := (next + REOpSz + RENextOffSz + RENumberSz)^; |
| 6263 |
while no >= LoopCnt do |
| 6264 |
begin |
| 6265 |
// If it could work, try it. |
| 6266 |
if (Local.nextch = #0) or (regInput^ = Local.nextch) then |
| 6267 |
begin |
| 6268 |
if MatchPrim(next) then |
| 6269 |
begin |
| 6270 |
Result := True; |
| 6271 |
Exit; |
| 6272 |
end; |
| 6273 |
if IsBacktrackingGroupAsAtom then |
| 6274 |
Exit; |
| 6275 |
end; |
| 6276 |
Dec(no); // Couldn't or didn't - back up. |
| 6277 |
regInput := save + no; |
| 6278 |
end; { of while } |
| 6279 |
end |
| 6280 |
else begin |
| 6281 |
while no >= LoopCnt do |
| 6282 |
begin |
| 6283 |
if MatchPrim(next) then |
| 6284 |
begin |
| 6285 |
Result := True; |
| 6286 |
Exit; |
| 6287 |
end; |
| 6288 |
if IsBacktrackingGroupAsAtom then |
| 6289 |
Exit; |
| 6290 |
Dec(no); // Couldn't or didn't - back up. |
| 6291 |
regInput := save + no; |
| 6292 |
end; { of while } |
| 6293 |
end; |
| 6294 |
Exit; |
| 6295 |
end; |
| 6296 |
|
| 6297 |
OP_STAR_NG, OP_PLUS_NG, OP_BRACES_NG: |
| 6298 |
begin |
| 6299 |
opnd := scan + REOpSz + RENextOffSz; |
| 6300 |
save := regInput; |
| 6301 |
case scan^ of |
| 6302 |
OP_STAR_NG: |
| 6303 |
begin |
| 6304 |
no := FindRepeated(opnd, MaxInt); |
| 6305 |
LoopCnt := 0 // star |
| 6306 |
end; |
| 6307 |
OP_PLUS_NG: |
| 6308 |
begin |
| 6309 |
no := FindRepeated(opnd, MaxInt); |
| 6310 |
if no < 1 then |
| 6311 |
Exit; |
| 6312 |
LoopCnt := 1 // star |
| 6313 |
end; |
| 6314 |
else |
| 6315 |
begin // braces |
| 6316 |
opnd := AlignToPtr(opnd); |
| 6317 |
no := FindRepeated(opnd + 2 * REBracesArgSz, PREBracesArg(opnd + REBracesArgSz)^); |
| 6318 |
LoopCnt := PREBracesArg(opnd)^; |
| 6319 |
if no < LoopCnt then |
| 6320 |
Exit; |
| 6321 |
end; |
| 6322 |
end; |
| 6323 |
|
| 6324 |
|
| 6325 |
// non-greedy mode |
| 6326 |
// don't repeat more than "no" times |
| 6327 |
// Now we know real Max limit to move forward (for recursion 'back up') |
| 6328 |
// In some cases it can be faster to check only Min positions first, |
| 6329 |
// but after that we have to check every position separtely instead |
| 6330 |
// of fast scannig in loop. |
| 6331 |
if next^ = OP_EXACTLY then begin |
| 6332 |
// Lookahead to avoid useless match attempts when we know |
| 6333 |
// what character comes next. |
| 6334 |
Local.nextch := (next + REOpSz + RENextOffSz + RENumberSz)^; |
| 6335 |
while LoopCnt <= no do |
| 6336 |
begin |
| 6337 |
regInput := save + LoopCnt; |
| 6338 |
// If it could work, try it. |
| 6339 |
if (Local.nextch = #0) or (regInput^ = Local.nextch) then |
| 6340 |
begin |
| 6341 |
if MatchPrim(next) then |
| 6342 |
begin |
| 6343 |
Result := True; |
| 6344 |
Exit; |
| 6345 |
end; |
| 6346 |
if IsBacktrackingGroupAsAtom then |
| 6347 |
Exit; |
| 6348 |
end; |
| 6349 |
Inc(LoopCnt); // Couldn't or didn't - move forward. |
| 6350 |
end; { of while } |
| 6351 |
end |
| 6352 |
else begin |
| 6353 |
while LoopCnt <= no do |
| 6354 |
begin |
| 6355 |
regInput := save + LoopCnt; |
| 6356 |
if MatchPrim(next) then |
| 6357 |
begin |
| 6358 |
Result := True; |
| 6359 |
Exit; |
| 6360 |
end; |
| 6361 |
if IsBacktrackingGroupAsAtom then |
| 6362 |
Exit; |
| 6363 |
Inc(LoopCnt); // Couldn't or didn't - move forward. |
| 6364 |
end; { of while } |
| 6365 |
end; |
| 6366 |
Exit; |
| 6367 |
end; |
| 6368 |
|
| 6369 |
OP_STAR_POSS, OP_PLUS_POSS, OP_BRACES_POSS: |
| 6370 |
begin |
| 6371 |
opnd := scan + REOpSz + RENextOffSz; |
| 6372 |
case scan^ of |
| 6373 |
OP_STAR_POSS: |
| 6374 |
begin |
| 6375 |
FindRepeated(opnd, MaxInt); |
| 6376 |
end; |
| 6377 |
OP_PLUS_POSS: |
| 6378 |
begin |
| 6379 |
if FindRepeated(opnd, MaxInt) < 1 then |
| 6380 |
Exit; |
| 6381 |
end; |
| 6382 |
else |
| 6383 |
begin // braces |
| 6384 |
opnd := AlignToPtr(opnd); |
| 6385 |
if FindRepeated(opnd + 2 * REBracesArgSz, PREBracesArg(opnd + REBracesArgSz)^) |
| 6386 |
< PREBracesArg(opnd)^ |
| 6387 |
then |
| 6388 |
Exit; |
| 6389 |
end; |
| 6390 |
end; |
| 6391 |
end; |
| 6392 |
|
| 6393 |
OP_EEND: |
| 6394 |
begin |
| 6395 |
Result := True; // Success! |
| 6396 |
Exit; |
| 6397 |
end; |
| 6398 |
|
| 6399 |
{$IFDEF FastUnicodeData} |
| 6400 |
OP_ANYCATEGORY: |
| 6401 |
begin |
| 6402 |
if (regInput >= fInputCurrentEnd) then Exit; |
| 6403 |
if not MatchOneCharCategory(scan + REOpSz + RENextOffSz, regInput) then Exit; |
| 6404 |
{$IFDEF UNICODEEX} |
| 6405 |
IncUnicode(regInput); |
| 6406 |
{$ELSE} |
| 6407 |
Inc(regInput); |
| 6408 |
{$ENDIF} |
| 6409 |
end; |
| 6410 |
|
| 6411 |
OP_NOTCATEGORY: |
| 6412 |
begin |
| 6413 |
if (regInput >= fInputCurrentEnd) then Exit; |
| 6414 |
if MatchOneCharCategory(scan + REOpSz + RENextOffSz, regInput) then Exit; |
| 6415 |
{$IFDEF UNICODEEX} |
| 6416 |
IncUnicode(regInput); |
| 6417 |
{$ELSE} |
| 6418 |
Inc(regInput); |
| 6419 |
{$ENDIF} |
| 6420 |
end; |
| 6421 |
{$ENDIF} |
| 6422 |
|
| 6423 |
OP_RECUR: |
| 6424 |
begin |
| 6425 |
// call opcode start |
| 6426 |
if regRecursion < RegexMaxRecursion then |
| 6427 |
begin |
| 6428 |
Inc(regRecursion); |
| 6429 |
if regNumBrackets > 0 then begin |
| 6430 |
CurrentGrpBounds.TmpStart := @GrpBounds[regRecursion].TmpStart[0]; |
| 6431 |
CurrentGrpBounds.GrpStart := @GrpBounds[regRecursion].GrpStart[0]; |
| 6432 |
CurrentGrpBounds.GrpEnd := @GrpBounds[regRecursion].GrpEnd[0]; |
| 6433 |
FillChar(CurrentGrpBounds.GrpStart[0], SizeOf(CurrentGrpBounds.GrpStart[0])*regNumBrackets, 0); |
| 6434 |
end; |
| 6435 |
Result := MatchPrim(regCodeWork); |
| 6436 |
Dec(regRecursion); |
| 6437 |
if regNumBrackets > 0 then begin |
| 6438 |
CurrentGrpBounds.TmpStart := @GrpBounds[regRecursion].TmpStart[0]; |
| 6439 |
CurrentGrpBounds.GrpStart := @GrpBounds[regRecursion].GrpStart[0]; |
| 6440 |
CurrentGrpBounds.GrpEnd := @GrpBounds[regRecursion].GrpEnd[0]; |
| 6441 |
end; |
| 6442 |
if not Result then Exit; |
| 6443 |
Result := False; |
| 6444 |
end |
| 6445 |
else |
| 6446 |
Exit; |
| 6447 |
end; |
| 6448 |
|
| 6449 |
OP_SUBCALL: |
| 6450 |
begin |
| 6451 |
// call subroutine |
| 6452 |
no := PReGroupIndex((scan + REOpSz + RENextOffSz))^; |
| 6453 |
if no < 0 then Exit; |
| 6454 |
save := GrpOpCodes[no]; |
| 6455 |
if save = nil then Exit; |
| 6456 |
if regRecursion < RegexMaxRecursion then |
| 6457 |
begin |
| 6458 |
Local.savedCurrentSubCalled := CurrentSubCalled; |
| 6459 |
CurrentSubCalled := no; |
| 6460 |
Inc(regRecursion); |
| 6461 |
if regNumBrackets > 0 then begin |
| 6462 |
CurrentGrpBounds.TmpStart := @GrpBounds[regRecursion].TmpStart[0]; |
| 6463 |
CurrentGrpBounds.GrpStart := @GrpBounds[regRecursion].GrpStart[0]; |
| 6464 |
CurrentGrpBounds.GrpEnd := @GrpBounds[regRecursion].GrpEnd[0]; |
| 6465 |
FillChar(CurrentGrpBounds.GrpStart[0], SizeOf(CurrentGrpBounds.GrpStart[0])*regNumBrackets, 0); |
| 6466 |
end; |
| 6467 |
Result := MatchPrim(save); |
| 6468 |
Dec(regRecursion); |
| 6469 |
if regNumBrackets > 0 then begin |
| 6470 |
CurrentGrpBounds.TmpStart := @GrpBounds[regRecursion].TmpStart[0]; |
| 6471 |
CurrentGrpBounds.GrpStart := @GrpBounds[regRecursion].GrpStart[0]; |
| 6472 |
CurrentGrpBounds.GrpEnd := @GrpBounds[regRecursion].GrpEnd[0]; |
| 6473 |
end; |
| 6474 |
CurrentSubCalled := Local.savedCurrentSubCalled; |
| 6475 |
if not Result then Exit; |
| 6476 |
Result := False; |
| 6477 |
end |
| 6478 |
else |
| 6479 |
Exit; |
| 6480 |
end; |
| 6481 |
|
| 6482 |
OP_ANYLINEBREAK: |
| 6483 |
begin |
| 6484 |
if (regInput >= fInputCurrentEnd) or not IsAnyLineBreak(regInput^) then |
| 6485 |
Exit; |
| 6486 |
if regInput^ = #13 then begin |
| 6487 |
Inc(regInput); |
| 6488 |
if (regInput < fInputCurrentEnd) and (regInput^ = #10) then |
| 6489 |
Inc(regInput); |
| 6490 |
end |
| 6491 |
else |
| 6492 |
Inc(regInput); |
| 6493 |
end; |
| 6494 |
|
| 6495 |
{$IFDEF WITH_REGEX_ASSERT} |
| 6496 |
else |
| 6497 |
Error(reeMatchPrimMemoryCorruption); |
| 6498 |
Exit; |
| 6499 |
{$ENDIF} |
| 6500 |
end; { of case scan^ } |
| 6501 |
scan := next; |
| 6502 |
end; { of while scan <> nil } |
| 6503 |
end; { of function TRegExpr.MatchPrim |
| 6504 |
-------------------------------------------------------------- } |
| 6505 |
|
| 6506 |
function TRegExpr.Exec(const AInputString: RegExprString): Boolean; |
| 6507 |
begin |
| 6508 |
InputString := AInputString; |
| 6509 |
Result := ExecPrim(1, False, False, 0); |
| 6510 |
end; { of function TRegExpr.Exec |
| 6511 |
-------------------------------------------------------------- } |
| 6512 |
|
| 6513 |
{$IFDEF OverMeth} |
| 6514 |
function TRegExpr.Exec: Boolean; |
| 6515 |
var |
| 6516 |
SlowChecks: Boolean; |
| 6517 |
begin |
| 6518 |
SlowChecks := (fInputEnd - fInputStart < fSlowChecksSizeMax) and (regMustString <> ''); |
| 6519 |
Result := ExecPrim(1, SlowChecks, False, 0); |
| 6520 |
end; { of function TRegExpr.Exec |
| 6521 |
-------------------------------------------------------------- } |
| 6522 |
|
| 6523 |
function TRegExpr.Exec(AOffset: Integer): Boolean; |
| 6524 |
begin |
| 6525 |
// Check that the start position is not negative |
| 6526 |
if AOffset < 1 then |
| 6527 |
begin |
| 6528 |
ClearMatches; |
| 6529 |
Error(reeOffsetMustBePositive); |
| 6530 |
Result := False; |
| 6531 |
Exit; |
| 6532 |
end; |
| 6533 |
Result := ExecPrim(AOffset, False, False, 0); |
| 6534 |
end; { of function TRegExpr.Exec |
| 6535 |
-------------------------------------------------------------- } |
| 6536 |
{$ENDIF} |
| 6537 |
|
| 6538 |
function TRegExpr.ExecPos(AOffset: Integer {$IFDEF DefParam} = 1{$ENDIF}): Boolean; |
| 6539 |
begin |
| 6540 |
// Check that the start position is not negative |
| 6541 |
if AOffset < 1 then |
| 6542 |
begin |
| 6543 |
ClearMatches; |
| 6544 |
Error(reeOffsetMustBePositive); |
| 6545 |
Result := False; |
| 6546 |
Exit; |
| 6547 |
end; |
| 6548 |
Result := ExecPrim(AOffset, False, False, 0); |
| 6549 |
end; { of function TRegExpr.ExecPos |
| 6550 |
-------------------------------------------------------------- } |
| 6551 |
|
| 6552 |
{$IFDEF OverMeth} |
| 6553 |
function TRegExpr.ExecPos(AOffset: Integer; ATryOnce, ABackward: Boolean): Boolean; |
| 6554 |
begin |
| 6555 |
// Check that the start position is not negative |
| 6556 |
if AOffset < 1 then |
| 6557 |
begin |
| 6558 |
ClearMatches; |
| 6559 |
Error(reeOffsetMustBePositive); |
| 6560 |
Result := False; |
| 6561 |
Exit; |
| 6562 |
end; |
| 6563 |
if ATryOnce then |
| 6564 |
Result := ExecPrim(AOffset, False, ABackward, AOffset + 1) |
| 6565 |
else |
| 6566 |
Result := ExecPrim(AOffset, False, ABackward, 0); |
| 6567 |
end; |
| 6568 |
|
| 6569 |
function TRegExpr.ExecPos(AOffset, ATryMatchOnlyStartingBefore: Integer): Boolean; |
| 6570 |
begin |
| 6571 |
// Check that the start position is not negative |
| 6572 |
if AOffset < 1 then |
| 6573 |
begin |
| 6574 |
ClearMatches; |
| 6575 |
Error(reeOffsetMustBePositive); |
| 6576 |
Result := False; |
| 6577 |
Exit; |
| 6578 |
end; |
| 6579 |
if (ATryMatchOnlyStartingBefore > 0) and (AOffset >= ATryMatchOnlyStartingBefore) then begin |
| 6580 |
ClearMatches; |
| 6581 |
Result := False; |
| 6582 |
Exit; |
| 6583 |
end; |
| 6584 |
Result := ExecPrim(AOffset, False, False, ATryMatchOnlyStartingBefore); |
| 6585 |
end; |
| 6586 |
{$ENDIF} |
| 6587 |
|
| 6588 |
function TRegExpr.MatchAtOnePos(APos: PRegExprChar): Boolean; |
| 6589 |
begin |
| 6590 |
regInput := APos; |
| 6591 |
//regNestedCalls := 0; |
| 6592 |
fInputCurrentEnd := fInputEnd; |
| 6593 |
GrpBounds[0].GrpStart[0] := APos; |
| 6594 |
Result := MatchPrim(regCodeWork); |
| 6595 |
if Result then |
| 6596 |
Result := regInput >= GrpBounds[0].GrpStart[0]; |
| 6597 |
if Result then |
| 6598 |
GrpBounds[0].GrpEnd[0] := regInput |
| 6599 |
else |
| 6600 |
GrpBounds[0].GrpStart[0] := nil; |
| 6601 |
end; |
| 6602 |
|
| 6603 |
procedure TRegExpr.ClearMatches; |
| 6604 |
begin |
| 6605 |
if FMatchesCleared then |
| 6606 |
exit; |
| 6607 |
FMatchesCleared := True; |
| 6608 |
if Length(GrpBounds[0].GrpStart) > 0 then |
| 6609 |
FillChar(GrpBounds[0].GrpStart[0], SizeOf(GrpBounds[0].GrpStart[0])*regNumBrackets, 0); |
| 6610 |
end; |
| 6611 |
|
| 6612 |
procedure TRegExpr.ClearInternalExecData; |
| 6613 |
begin |
| 6614 |
fLastError := reeOk; |
| 6615 |
if Length(GrpBacktrackingAsAtom) > 0 then |
| 6616 |
FillChar(GrpBacktrackingAsAtom[0], SizeOf(GrpBacktrackingAsAtom[0])*regNumAtomicBrackets, 0); |
| 6617 |
IsBacktrackingGroupAsAtom := False; |
| 6618 |
{$IFDEF ComplexBraces} |
| 6619 |
// no loops started |
| 6620 |
CurrentLoopInfoListPtr := nil; |
| 6621 |
{$ENDIF} |
| 6622 |
LookAroundInfoList := nil; |
| 6623 |
CurrentSubCalled := -1; |
| 6624 |
regRecursion := 0; |
| 6625 |
if regNumBrackets > 0 then begin |
| 6626 |
CurrentGrpBounds.TmpStart := @GrpBounds[0].TmpStart[0]; |
| 6627 |
CurrentGrpBounds.GrpStart := @GrpBounds[0].GrpStart[0]; |
| 6628 |
CurrentGrpBounds.GrpEnd := @GrpBounds[0].GrpEnd[0]; |
| 6629 |
end; |
| 6630 |
end; |
| 6631 |
|
| 6632 |
procedure TRegExpr.InitInternalGroupData; |
| 6633 |
var |
| 6634 |
BndLen, i: Integer; |
| 6635 |
begin |
| 6636 |
BndLen := GroupDataArraySize(regNumBrackets, Length(GrpBounds[0].GrpStart)); |
| 6637 |
if hasRecursion then begin |
| 6638 |
for i := low(GrpBounds) to high(GrpBounds) do begin |
| 6639 |
SetLength(GrpBounds[i].TmpStart, BndLen); |
| 6640 |
SetLength(GrpBounds[i].GrpStart, BndLen); |
| 6641 |
SetLength(GrpBounds[i].GrpEnd, BndLen); |
| 6642 |
end; |
| 6643 |
end |
| 6644 |
else begin |
| 6645 |
SetLength(GrpBounds[0].TmpStart, BndLen); |
| 6646 |
SetLength(GrpBounds[0].GrpStart, BndLen); |
| 6647 |
SetLength(GrpBounds[0].GrpEnd, BndLen); |
| 6648 |
for i := low(GrpBounds) + 1 to high(GrpBounds) do begin |
| 6649 |
GrpBounds[i].TmpStart := nil; |
| 6650 |
GrpBounds[i].GrpStart := nil; |
| 6651 |
GrpBounds[i].GrpEnd := nil; |
| 6652 |
end; |
| 6653 |
end; |
| 6654 |
|
| 6655 |
SetLength(GrpOpCodes, GroupDataArraySize(regNumBrackets, Length(GrpOpCodes))); |
| 6656 |
SetLength(GrpBacktrackingAsAtom, GroupDataArraySize(regNumAtomicBrackets, Length(GrpBacktrackingAsAtom))); |
| 6657 |
|
| 6658 |
GrpOpCodes[0] := nil; |
| 6659 |
end; |
| 6660 |
|
| 6661 |
function TRegExpr.ExecPrim(AOffset: Integer; ASlowChecks, ABackward: Boolean; |
| 6662 |
ATryMatchOnlyStartingBefore: Integer): Boolean; |
| 6663 |
var |
| 6664 |
Len: Ptrint; |
| 6665 |
begin |
| 6666 |
Result := False; |
| 6667 |
|
| 6668 |
// Ensure that Match cleared either if optimization tricks or some error |
| 6669 |
// will lead to leaving ExecPrim without actual search. That is |
| 6670 |
// important for ExecNext logic and so on. |
| 6671 |
ClearMatches; |
| 6672 |
|
| 6673 |
// Don't check IsProgrammOk here! it causes big slowdown in test_benchmark! |
| 6674 |
if programm = nil then |
| 6675 |
begin |
| 6676 |
if fRaiseForRuntimeError then begin |
| 6677 |
Compile; |
| 6678 |
end |
| 6679 |
else begin |
| 6680 |
try |
| 6681 |
Compile; |
| 6682 |
except |
| 6683 |
on E: ERegExpr do begin |
| 6684 |
Result := False; |
| 6685 |
end; |
| 6686 |
else begin |
| 6687 |
Result := False; |
| 6688 |
fLastError := reeUnknown; |
| 6689 |
end; |
| 6690 |
end; |
| 6691 |
end; |
| 6692 |
if programm = nil then |
| 6693 |
Exit; |
| 6694 |
end; |
| 6695 |
|
| 6696 |
Len := fInputEnd - fInputStart; |
| 6697 |
if FMinMatchLen > Len then |
| 6698 |
Exit; |
| 6699 |
|
| 6700 |
// Check that the start position is not longer than the line |
| 6701 |
if (AOffset - 1) > Len - FMinMatchLen then |
| 6702 |
Exit; |
| 6703 |
|
| 6704 |
|
| 6705 |
// If there is a "must appear" string, look for it. |
| 6706 |
if ASlowChecks then |
| 6707 |
if regMustString <> '' then |
| 6708 |
if StrLPos(fInputStart, PRegExprChar(regMustString), Len, length(regMustString)) = nil then |
| 6709 |
exit; |
| 6710 |
|
| 6711 |
{$IFDEF RegExpWithStackOverflowCheck_DecStack_Frame} |
| 6712 |
StackLimit := StackBottom; |
| 6713 |
if StackLimit <> nil then |
| 6714 |
StackLimit := StackLimit + 36000; // Add for any calls within the current MatchPrim // FPC has "STACK_MARGIN = 16384;", but we need to call Error, ..., raise |
| 6715 |
{$ENDIF} |
| 6716 |
|
| 6717 |
ClearInternalExecData; |
| 6718 |
|
| 6719 |
if fRaiseForRuntimeError then begin |
| 6720 |
Result := ExecPrimProtected(AOffset, ASlowChecks, ABackward, ATryMatchOnlyStartingBefore); |
| 6721 |
end |
| 6722 |
else begin |
| 6723 |
try |
| 6724 |
Result := ExecPrimProtected(AOffset, ASlowChecks, ABackward, ATryMatchOnlyStartingBefore); |
| 6725 |
except |
| 6726 |
on E: EStackOverflow do begin |
| 6727 |
Result := False; |
| 6728 |
fLastError := reeLoopStackExceeded; |
| 6729 |
end; |
| 6730 |
on E: ERegExpr do begin |
| 6731 |
Result := False; |
| 6732 |
end; |
| 6733 |
else begin |
| 6734 |
Result := False; |
| 6735 |
fLastError := reeUnknown; |
| 6736 |
end; |
| 6737 |
end; |
| 6738 |
end; |
| 6739 |
end; |
| 6740 |
|
| 6741 |
function TRegExpr.ExecPrimProtected(AOffset: Integer; ASlowChecks, |
| 6742 |
ABackward: Boolean; ATryMatchOnlyStartingBefore: Integer): Boolean; |
| 6743 |
var |
| 6744 |
Ptr, SearchEnd: PRegExprChar; |
| 6745 |
begin |
| 6746 |
Result := False; |
| 6747 |
Ptr := fInputStart + AOffset - 1; |
| 6748 |
fInputContinue := Ptr; |
| 6749 |
FMatchesCleared := False; |
| 6750 |
// ATryOnce or anchored match (it needs to be tried only once). |
| 6751 |
if (ATryMatchOnlyStartingBefore = AOffset + 1) or (regAnchored in [raBOL, raOnlyOnce, raContinue]) then |
| 6752 |
begin |
| 6753 |
case regAnchored of |
| 6754 |
raBOL: if AOffset > 1 then Exit; // can't match the BOL |
| 6755 |
raEOL: Ptr := fInputEnd; |
| 6756 |
end; |
| 6757 |
{$IFDEF UseFirstCharSet} |
| 6758 |
if (Ptr < fInputEnd) |
| 6759 |
{$IFDEF UnicodeRE} and (Ord(Ptr^) <= $FF) {$ENDIF} |
| 6760 |
then |
| 6761 |
if not FirstCharArray[Byte(Ptr^)] then |
| 6762 |
Exit; |
| 6763 |
{$ENDIF} |
| 6764 |
|
| 6765 |
Result := MatchAtOnePos(Ptr); |
| 6766 |
Exit; |
| 6767 |
end; |
| 6768 |
|
| 6769 |
// Messy cases: unanchored match. |
| 6770 |
if ABackward then begin |
| 6771 |
Inc(Ptr, 2); |
| 6772 |
repeat |
| 6773 |
Dec(Ptr); |
| 6774 |
if Ptr < fInputStart then |
| 6775 |
Exit; |
| 6776 |
|
| 6777 |
{$IFDEF UseFirstCharSet} |
| 6778 |
{$IFDEF UnicodeRE} |
| 6779 |
if Ord(Ptr^) <= $FF then |
| 6780 |
{$ENDIF} |
| 6781 |
if not FirstCharArray[byte(Ptr^)] then |
| 6782 |
Continue; |
| 6783 |
{$ENDIF} |
| 6784 |
|
| 6785 |
Result := MatchAtOnePos(Ptr); |
| 6786 |
// Exit on a match or after testing the end-of-string |
| 6787 |
if Result then |
| 6788 |
Exit; |
| 6789 |
until False; |
| 6790 |
end |
| 6791 |
else begin |
| 6792 |
Dec(Ptr); |
| 6793 |
SearchEnd := fInputEnd - FMinMatchLen; |
| 6794 |
if (ATryMatchOnlyStartingBefore > 0) and (fInputStart + ATryMatchOnlyStartingBefore < SearchEnd) then |
| 6795 |
SearchEnd := fInputStart + ATryMatchOnlyStartingBefore - 2; |
| 6796 |
repeat |
| 6797 |
Inc(Ptr); |
| 6798 |
if Ptr > SearchEnd then |
| 6799 |
Break; |
| 6800 |
|
| 6801 |
{$IFDEF UseFirstCharSet} |
| 6802 |
{$IFDEF UnicodeRE} |
| 6803 |
if Ord(Ptr^) <= $FF then |
| 6804 |
{$ENDIF} |
| 6805 |
if not FirstCharArray[byte(Ptr^)] then |
| 6806 |
Continue; |
| 6807 |
{$ENDIF} |
| 6808 |
|
| 6809 |
Result := MatchAtOnePos(Ptr); |
| 6810 |
// Exit on a match or after testing the end-of-string |
| 6811 |
if Result then |
| 6812 |
Exit; |
| 6813 |
until False; |
| 6814 |
|
| 6815 |
{$IFDEF UseFirstCharSet} |
| 6816 |
if FirstCharArray[0] and (fInputEnd^ <> #0) then |
| 6817 |
Result := MatchAtOnePos(fInputEnd); |
| 6818 |
{$ENDIF} |
| 6819 |
end; |
| 6820 |
end; { of function TRegExpr.ExecPrim |
| 6821 |
-------------------------------------------------------------- } |
| 6822 |
|
| 6823 |
function TRegExpr.ExecNext(ABackward: Boolean {$IFDEF DefParam} = False{$ENDIF}): Boolean; |
| 6824 |
var |
| 6825 |
PtrBegin, PtrEnd: PRegExprChar; |
| 6826 |
Offset: PtrInt; |
| 6827 |
begin |
| 6828 |
PtrBegin := GrpBounds[0].GrpStart[0]; |
| 6829 |
PtrEnd := GrpBounds[0].GrpEnd[0]; |
| 6830 |
if (PtrBegin = nil) or (PtrEnd = nil) then |
| 6831 |
begin |
| 6832 |
Error(reeExecNextWithoutExec); |
| 6833 |
Result := False; |
| 6834 |
Exit; |
| 6835 |
end; |
| 6836 |
|
| 6837 |
Offset := PtrEnd - fInputStart + 1; |
| 6838 |
// prevent infinite looping if empty string matches r.e. |
| 6839 |
if PtrBegin = PtrEnd then |
| 6840 |
Inc(Offset); |
| 6841 |
|
| 6842 |
Result := ExecPrim(Offset, False, ABackward, 0); |
| 6843 |
end; { of function TRegExpr.ExecNext |
| 6844 |
-------------------------------------------------------------- } |
| 6845 |
|
| 6846 |
procedure TRegExpr.SetInputString(const AInputString: RegExprString); |
| 6847 |
begin |
| 6848 |
ClearMatches; |
| 6849 |
|
| 6850 |
fInputString := AInputString; |
| 6851 |
//UniqueString(fInputString); |
| 6852 |
|
| 6853 |
fInputStart := PRegExprChar(fInputString); |
| 6854 |
fInputEnd := fInputStart + Length(fInputString); |
| 6855 |
fInputContinue := fInputStart; |
| 6856 |
end; |
| 6857 |
|
| 6858 |
procedure TRegExpr.SetInputRange(AStart, AEnd, AContinueAnchor: PRegExprChar); |
| 6859 |
begin |
| 6860 |
ClearMatches; |
| 6861 |
fInputString := ''; |
| 6862 |
fInputStart := AStart; |
| 6863 |
fInputEnd := AEnd; |
| 6864 |
fInputContinue := AContinueAnchor; |
| 6865 |
end; |
| 6866 |
|
| 6867 |
{$IFDEF UseLineSep} |
| 6868 |
procedure TRegExpr.SetLineSeparators(const AStr: RegExprString); |
| 6869 |
begin |
| 6870 |
if AStr <> fLineSeparators then |
| 6871 |
begin |
| 6872 |
fLineSeparators := AStr; |
| 6873 |
InitLineSepArray; |
| 6874 |
InvalidateProgramm; |
| 6875 |
end; |
| 6876 |
end; { of procedure TRegExpr.SetLineSeparators |
| 6877 |
-------------------------------------------------------------- } |
| 6878 |
{$ENDIF} |
| 6879 |
|
| 6880 |
procedure TRegExpr.SetUsePairedBreak(AValue: Boolean); |
| 6881 |
begin |
| 6882 |
if AValue <> fUsePairedBreak then |
| 6883 |
begin |
| 6884 |
fUsePairedBreak := AValue; |
| 6885 |
InvalidateProgramm; |
| 6886 |
end; |
| 6887 |
end; |
| 6888 |
|
| 6889 |
function TRegExpr.Substitute(const ATemplate: RegExprString): RegExprString; |
| 6890 |
// perform substitutions after a regexp match |
| 6891 |
var |
| 6892 |
TemplateBeg, TemplateEnd: PRegExprChar; |
| 6893 |
|
| 6894 |
function ParseVarName(var APtr: PRegExprChar): Integer; |
| 6895 |
// extract name of variable: $1 or ${1} or ${name} |
| 6896 |
// from APtr^, uses TemplateEnd |
| 6897 |
var |
| 6898 |
p: PRegExprChar; |
| 6899 |
Delimited: Boolean; |
| 6900 |
GrpName: RegExprString; |
| 6901 |
begin |
| 6902 |
Result := 0; |
| 6903 |
GrpName := ''; |
| 6904 |
p := APtr; |
| 6905 |
Delimited := (p < TemplateEnd) and (p^ = '{'); |
| 6906 |
if Delimited then |
| 6907 |
Inc(p); // skip left curly brace |
| 6908 |
if (p < TemplateEnd) and (p^ = '&') then |
| 6909 |
Inc(p) // this is '$&' or '${&}' |
| 6910 |
else |
| 6911 |
begin |
| 6912 |
if IsDigitChar(p^) then |
| 6913 |
begin |
| 6914 |
while (p < TemplateEnd) and IsDigitChar(p^) do |
| 6915 |
begin |
| 6916 |
Result := Result * 10 + (Ord(p^) - Ord('0')); |
| 6917 |
Inc(p); |
| 6918 |
end |
| 6919 |
end |
| 6920 |
else |
| 6921 |
if Delimited then |
| 6922 |
begin |
| 6923 |
FindGroupName(p, TemplateEnd, '}', GrpName); |
| 6924 |
Result := GrpNames.MatchIndexFromName(GrpName); |
| 6925 |
Inc(p, Length(GrpName)); |
| 6926 |
end; |
| 6927 |
end; |
| 6928 |
if Delimited then |
| 6929 |
if (p < TemplateEnd) and (p^ = '}') then |
| 6930 |
Inc(p) // skip right curly brace |
| 6931 |
else |
| 6932 |
p := APtr; // isn't properly terminated |
| 6933 |
if p = APtr then |
| 6934 |
Result := -1; // no valid digits found or no right curly brace |
| 6935 |
APtr := p; |
| 6936 |
end; |
| 6937 |
|
| 6938 |
procedure FindSubstGroupIndex(var p: PRegExprChar; var Idx: Integer; var NumberFound: Boolean); |
| 6939 |
begin |
| 6940 |
Idx := ParseVarName(p); |
| 6941 |
NumberFound := Idx >= 0; |
| 6942 |
if NumberFound and (Idx > GrpCount) then |
| 6943 |
Idx := -1; |
| 6944 |
end; |
| 6945 |
|
| 6946 |
type |
| 6947 |
TSubstMode = (smodeNormal, smodeOneUpper, smodeOneLower, smodeAllUpper, smodeAllLower); |
| 6948 |
var |
| 6949 |
Mode: TSubstMode; |
| 6950 |
p, p0, p1, ResultPtr: PRegExprChar; |
| 6951 |
ResultLen, n: Integer; |
| 6952 |
Ch, QuotedChar: REChar; |
| 6953 |
GroupFound: Boolean; |
| 6954 |
begin |
| 6955 |
// Check programm and input string |
| 6956 |
if not IsProgrammOk then |
| 6957 |
Exit; |
| 6958 |
// Note: don't check for empty fInputString, it's valid case, |
| 6959 |
// e.g. user needs to replace regex "\b" to "_", it's zero match length |
| 6960 |
if ATemplate = '' then |
| 6961 |
begin |
| 6962 |
Result := ''; |
| 6963 |
Exit; |
| 6964 |
end; |
| 6965 |
TemplateBeg := PRegExprChar(ATemplate); |
| 6966 |
TemplateEnd := TemplateBeg + Length(ATemplate); |
| 6967 |
// Count result length for speed optimization. |
| 6968 |
ResultLen := 0; |
| 6969 |
p := TemplateBeg; |
| 6970 |
while p < TemplateEnd do |
| 6971 |
begin |
| 6972 |
Ch := p^; |
| 6973 |
Inc(p); |
| 6974 |
n := -1; |
| 6975 |
GroupFound := False; |
| 6976 |
if Ch = SubstituteGroupChar then |
| 6977 |
FindSubstGroupIndex(p, n, GroupFound); |
| 6978 |
if GroupFound then |
| 6979 |
begin |
| 6980 |
if (n >= 0) and (GrpBounds[0].GrpStart[n] <> nil) then |
| 6981 |
Inc(ResultLen, GrpBounds[0].GrpEnd[n] - GrpBounds[0].GrpStart[n]); |
| 6982 |
end |
| 6983 |
else |
| 6984 |
begin |
| 6985 |
if (Ch = EscChar) and (p < TemplateEnd) then |
| 6986 |
begin // quoted or special char followed |
| 6987 |
Ch := p^; |
| 6988 |
Inc(p); |
| 6989 |
case Ch of |
| 6990 |
'n': |
| 6991 |
Inc(ResultLen, Length(fReplaceLineEnd)); |
| 6992 |
'u', 'l', 'U', 'L': { nothing } |
| 6993 |
; |
| 6994 |
'x': |
| 6995 |
begin |
| 6996 |
Inc(ResultLen); |
| 6997 |
if (p^ = '{') then |
| 6998 |
begin // skip \x{....} |
| 6999 |
while ((p^ <> '}') and (p < TemplateEnd)) do |
| 7000 |
p := p + 1; |
| 7001 |
p := p + 1; |
| 7002 |
end |
| 7003 |
else |
| 7004 |
p := p + 2 // skip \x.. |
| 7005 |
end; |
| 7006 |
else |
| 7007 |
Inc(ResultLen); |
| 7008 |
end; |
| 7009 |
end |
| 7010 |
else |
| 7011 |
Inc(ResultLen); |
| 7012 |
end; |
| 7013 |
end; |
| 7014 |
// Get memory. We do it once and it significant speed up work ! |
| 7015 |
if ResultLen = 0 then |
| 7016 |
begin |
| 7017 |
Result := ''; |
| 7018 |
Exit; |
| 7019 |
end; |
| 7020 |
SetLength(Result, ResultLen); |
| 7021 |
// Fill Result |
| 7022 |
ResultPtr := PRegExprChar(Result); |
| 7023 |
p := TemplateBeg; |
| 7024 |
Mode := smodeNormal; |
| 7025 |
while p < TemplateEnd do |
| 7026 |
begin |
| 7027 |
Ch := p^; |
| 7028 |
p0 := p; |
| 7029 |
Inc(p); |
| 7030 |
p1 := p; |
| 7031 |
n := -1; |
| 7032 |
GroupFound := False; |
| 7033 |
if Ch = SubstituteGroupChar then |
| 7034 |
FindSubstGroupIndex(p, n, GroupFound); |
| 7035 |
if GroupFound then |
| 7036 |
begin |
| 7037 |
if n >= 0 then |
| 7038 |
begin |
| 7039 |
p0 := GrpBounds[0].GrpStart[n]; |
| 7040 |
if p0 = nil then |
| 7041 |
p1 := nil |
| 7042 |
else |
| 7043 |
p1 := GrpBounds[0].GrpEnd[n]; |
| 7044 |
end |
| 7045 |
else |
| 7046 |
p1 := p0; |
| 7047 |
end |
| 7048 |
else |
| 7049 |
begin |
| 7050 |
if (Ch = EscChar) and (p < TemplateEnd) then |
| 7051 |
begin // quoted or special char followed |
| 7052 |
Ch := p^; |
| 7053 |
Inc(p); |
| 7054 |
case Ch of |
| 7055 |
'n': |
| 7056 |
begin |
| 7057 |
p0 := PRegExprChar(fReplaceLineEnd); |
| 7058 |
p1 := p0 + Length(fReplaceLineEnd); |
| 7059 |
end; |
| 7060 |
'x', 't', 'r', 'f', 'a', 'e': |
| 7061 |
begin |
| 7062 |
p := p - 1; |
| 7063 |
// UnquoteChar expects the escaped char under the pointer |
| 7064 |
QuotedChar := UnQuoteChar(p, TemplateEnd); |
| 7065 |
p := p + 1; |
| 7066 |
// Skip after last part of the escaped sequence - UnquoteChar stops on the last symbol of it |
| 7067 |
p0 := @QuotedChar; |
| 7068 |
p1 := p0 + 1; |
| 7069 |
end; |
| 7070 |
'l': |
| 7071 |
begin |
| 7072 |
Mode := smodeOneLower; |
| 7073 |
p1 := p0; |
| 7074 |
end; |
| 7075 |
'L': |
| 7076 |
begin |
| 7077 |
Mode := smodeAllLower; |
| 7078 |
p1 := p0; |
| 7079 |
end; |
| 7080 |
'u': |
| 7081 |
begin |
| 7082 |
Mode := smodeOneUpper; |
| 7083 |
p1 := p0; |
| 7084 |
end; |
| 7085 |
'U': |
| 7086 |
begin |
| 7087 |
Mode := smodeAllUpper; |
| 7088 |
p1 := p0; |
| 7089 |
end; |
| 7090 |
else |
| 7091 |
Inc(p0); |
| 7092 |
Inc(p1); |
| 7093 |
end; |
| 7094 |
end |
| 7095 |
end; |
| 7096 |
if p0 < p1 then |
| 7097 |
begin |
| 7098 |
while p0 < p1 do |
| 7099 |
begin |
| 7100 |
case Mode of |
| 7101 |
smodeOneLower: |
| 7102 |
begin |
| 7103 |
ResultPtr^ := _LowerCase(p0^); |
| 7104 |
Mode := smodeNormal; |
| 7105 |
end; |
| 7106 |
smodeAllLower: |
| 7107 |
begin |
| 7108 |
ResultPtr^ := _LowerCase(p0^); |
| 7109 |
end; |
| 7110 |
smodeOneUpper: |
| 7111 |
begin |
| 7112 |
ResultPtr^ := _UpperCase(p0^); |
| 7113 |
Mode := smodeNormal; |
| 7114 |
end; |
| 7115 |
smodeAllUpper: |
| 7116 |
begin |
| 7117 |
ResultPtr^ := _UpperCase(p0^); |
| 7118 |
end; |
| 7119 |
else |
| 7120 |
ResultPtr^ := p0^; |
| 7121 |
end; |
| 7122 |
Inc(ResultPtr); |
| 7123 |
Inc(p0); |
| 7124 |
end; |
| 7125 |
Mode := smodeNormal; |
| 7126 |
end; |
| 7127 |
end; |
| 7128 |
end; { of function TRegExpr.Substitute |
| 7129 |
-------------------------------------------------------------- } |
| 7130 |
|
| 7131 |
procedure TRegExpr.Split(const AInputStr: RegExprString; APieces: TStrings); |
| 7132 |
var |
| 7133 |
PrevPos: PtrInt; |
| 7134 |
begin |
| 7135 |
PrevPos := 1; |
| 7136 |
if Exec(AInputStr) then |
| 7137 |
repeat |
| 7138 |
APieces.Add(System.Copy(AInputStr, PrevPos, MatchPos[0] - PrevPos)); |
| 7139 |
PrevPos := MatchPos[0] + MatchLen[0]; |
| 7140 |
until not ExecNext; |
| 7141 |
APieces.Add(System.Copy(AInputStr, PrevPos, MaxInt)); // Tail |
| 7142 |
end; { of procedure TRegExpr.Split |
| 7143 |
-------------------------------------------------------------- } |
| 7144 |
|
| 7145 |
function TRegExpr.Replace(const AInputStr: RegExprString; |
| 7146 |
const AReplaceStr: RegExprString; |
| 7147 |
AUseSubstitution: Boolean{$IFDEF DefParam} = False{$ENDIF}): RegExprString; |
| 7148 |
var |
| 7149 |
PrevPos: PtrInt; |
| 7150 |
begin |
| 7151 |
Result := ''; |
| 7152 |
PrevPos := 1; |
| 7153 |
if Exec(AInputStr) then |
| 7154 |
repeat |
| 7155 |
Result := Result + System.Copy(AInputStr, PrevPos, MatchPos[0] - PrevPos); |
| 7156 |
if AUseSubstitution |
| 7157 |
then |
| 7158 |
Result := Result + Substitute(AReplaceStr) |
| 7159 |
else |
| 7160 |
Result := Result + AReplaceStr; |
| 7161 |
PrevPos := MatchPos[0] + MatchLen[0]; |
| 7162 |
until not ExecNext; |
| 7163 |
Result := Result + System.Copy(AInputStr, PrevPos, MaxInt); // Tail |
| 7164 |
end; { of function TRegExpr.Replace |
| 7165 |
-------------------------------------------------------------- } |
| 7166 |
|
| 7167 |
function TRegExpr.ReplaceEx(const AInputStr: RegExprString; |
| 7168 |
AReplaceFunc: TRegExprReplaceFunction): RegExprString; |
| 7169 |
var |
| 7170 |
PrevPos: PtrInt; |
| 7171 |
begin |
| 7172 |
Result := ''; |
| 7173 |
PrevPos := 1; |
| 7174 |
if Exec(AInputStr) then |
| 7175 |
repeat |
| 7176 |
Result := Result + System.Copy(AInputStr, PrevPos, MatchPos[0] - PrevPos) |
| 7177 |
+ AReplaceFunc(Self); |
| 7178 |
PrevPos := MatchPos[0] + MatchLen[0]; |
| 7179 |
until not ExecNext; |
| 7180 |
Result := Result + System.Copy(AInputStr, PrevPos, MaxInt); // Tail |
| 7181 |
end; { of function TRegExpr.ReplaceEx |
| 7182 |
-------------------------------------------------------------- } |
| 7183 |
|
| 7184 |
{$IFDEF OverMeth} |
| 7185 |
function TRegExpr.Replace(const AInputStr: RegExprString; |
| 7186 |
AReplaceFunc: TRegExprReplaceFunction): RegExprString; |
| 7187 |
begin |
| 7188 |
Result := ReplaceEx(AInputStr, AReplaceFunc); |
| 7189 |
end; { of function TRegExpr.Replace |
| 7190 |
-------------------------------------------------------------- } |
| 7191 |
{$ENDIF} |
| 7192 |
{ ============================================================= } |
| 7193 |
{ ====================== Debug section ======================== } |
| 7194 |
{ ============================================================= } |
| 7195 |
|
| 7196 |
{$IFDEF UseFirstCharSet} |
| 7197 |
procedure TRegExpr.FillFirstCharSet(prog: PRegExprChar); |
| 7198 |
var |
| 7199 |
scan: PRegExprChar; // Current node. |
| 7200 |
Next: PRegExprChar; // Next node. |
| 7201 |
opnd: PRegExprChar; |
| 7202 |
Oper: TREOp; |
| 7203 |
ch: REChar; |
| 7204 |
min_cnt: Integer; |
| 7205 |
{$IFDEF UseLineSep} |
| 7206 |
i: Integer; |
| 7207 |
{$ENDIF} |
| 7208 |
TempSet, TmpFirstCharSet: TRegExprCharset; |
| 7209 |
begin |
| 7210 |
TempSet := []; |
| 7211 |
scan := prog; |
| 7212 |
while scan <> nil do |
| 7213 |
begin |
| 7214 |
Next := regNextQuick(scan); |
| 7215 |
Oper := PREOp(scan)^; |
| 7216 |
case Oper of |
| 7217 |
OP_BSUBEXP, |
| 7218 |
OP_BSUBEXP_CI: |
| 7219 |
begin |
| 7220 |
// we cannot optimize r.e. if it starts with back reference |
| 7221 |
FirstCharSet := RegExprAllSet; |
| 7222 |
Exit; |
| 7223 |
end; |
| 7224 |
|
| 7225 |
OP_BOL, |
| 7226 |
OP_BOL_ML, |
| 7227 |
OP_CONTINUE_POS, |
| 7228 |
OP_RESET_MATCHPOS: |
| 7229 |
; // Exit; |
| 7230 |
|
| 7231 |
OP_EOL, |
| 7232 |
OP_EOL2, |
| 7233 |
OP_EOL_ML: |
| 7234 |
begin |
| 7235 |
Include(FirstCharSet, 0); |
| 7236 |
if ModifierM then |
| 7237 |
begin |
| 7238 |
{$IFDEF UseLineSep} |
| 7239 |
for i := 1 to Length(LineSeparators) do |
| 7240 |
Include(FirstCharSet, Byte(LineSeparators[i])); |
| 7241 |
{$ELSE} |
| 7242 |
FirstCharSet := FirstCharSet + RegExprLineSeparatorsSet; |
| 7243 |
{$ENDIF} |
| 7244 |
end; |
| 7245 |
Exit; |
| 7246 |
end; |
| 7247 |
|
| 7248 |
OP_BOUND, |
| 7249 |
OP_NOTBOUND: |
| 7250 |
; |
| 7251 |
|
| 7252 |
OP_ANY, |
| 7253 |
OP_ANY_ML: |
| 7254 |
begin // we can better define ANYML |
| 7255 |
FirstCharSet := RegExprAllSet; |
| 7256 |
Exit; |
| 7257 |
end; |
| 7258 |
|
| 7259 |
OP_ANYDIGIT: |
| 7260 |
begin |
| 7261 |
FirstCharSet := FirstCharSet + RegExprDigitSet; |
| 7262 |
Exit; |
| 7263 |
end; |
| 7264 |
|
| 7265 |
OP_NOTDIGIT: |
| 7266 |
begin |
| 7267 |
FirstCharSet := FirstCharSet + (RegExprAllSet - RegExprDigitSet); |
| 7268 |
Exit; |
| 7269 |
end; |
| 7270 |
|
| 7271 |
OP_ANYLETTER: |
| 7272 |
begin |
| 7273 |
GetCharSetFromWordChars(TempSet); |
| 7274 |
FirstCharSet := FirstCharSet + TempSet; |
| 7275 |
Exit; |
| 7276 |
end; |
| 7277 |
|
| 7278 |
OP_NOTLETTER: |
| 7279 |
begin |
| 7280 |
GetCharSetFromWordChars(TempSet); |
| 7281 |
FirstCharSet := FirstCharSet + (RegExprAllSet - TempSet); |
| 7282 |
Exit; |
| 7283 |
end; |
| 7284 |
|
| 7285 |
OP_ANYSPACE: |
| 7286 |
begin |
| 7287 |
GetCharSetFromSpaceChars(TempSet); |
| 7288 |
FirstCharSet := FirstCharSet + TempSet; |
| 7289 |
Exit; |
| 7290 |
end; |
| 7291 |
|
| 7292 |
OP_NOTSPACE: |
| 7293 |
begin |
| 7294 |
GetCharSetFromSpaceChars(TempSet); |
| 7295 |
FirstCharSet := FirstCharSet + (RegExprAllSet - TempSet); |
| 7296 |
Exit; |
| 7297 |
end; |
| 7298 |
|
| 7299 |
OP_ANYVERTSEP: |
| 7300 |
begin |
| 7301 |
FirstCharSet := FirstCharSet + RegExprLineSeparatorsSet; |
| 7302 |
Exit; |
| 7303 |
end; |
| 7304 |
|
| 7305 |
OP_NOTVERTSEP: |
| 7306 |
begin |
| 7307 |
FirstCharSet := FirstCharSet + (RegExprAllSet - RegExprLineSeparatorsSet); |
| 7308 |
Exit; |
| 7309 |
end; |
| 7310 |
|
| 7311 |
OP_ANYHORZSEP: |
| 7312 |
begin |
| 7313 |
FirstCharSet := FirstCharSet + RegExprHorzSeparatorsSet; |
| 7314 |
Exit; |
| 7315 |
end; |
| 7316 |
|
| 7317 |
OP_NOTHORZSEP: |
| 7318 |
begin |
| 7319 |
FirstCharSet := FirstCharSet + (RegExprAllSet - RegExprHorzSeparatorsSet); |
| 7320 |
Exit; |
| 7321 |
end; |
| 7322 |
|
| 7323 |
OP_EXACTLY_CI: |
| 7324 |
begin |
| 7325 |
ch := (scan + REOpSz + RENextOffSz + RENumberSz)^; |
| 7326 |
{$IFDEF UnicodeRE} |
| 7327 |
if Ord(ch) <= $FF then |
| 7328 |
{$ENDIF} |
| 7329 |
begin |
| 7330 |
Include(FirstCharSet, Byte(ch)); |
| 7331 |
Include(FirstCharSet, Byte(InvertCase(ch))); |
| 7332 |
end; |
| 7333 |
Exit; |
| 7334 |
end; |
| 7335 |
|
| 7336 |
OP_EXACTLY: |
| 7337 |
begin |
| 7338 |
ch := (scan + REOpSz + RENextOffSz + RENumberSz)^; |
| 7339 |
{$IFDEF UnicodeRE} |
| 7340 |
if Ord(ch) <= $FF then |
| 7341 |
{$ENDIF} |
| 7342 |
Include(FirstCharSet, Byte(ch)); |
| 7343 |
Exit; |
| 7344 |
end; |
| 7345 |
|
| 7346 |
OP_ANYOF: |
| 7347 |
begin |
| 7348 |
GetCharSetFromCharClass(scan + REOpSz + RENextOffSz, False, TempSet); |
| 7349 |
FirstCharSet := FirstCharSet + TempSet; |
| 7350 |
Exit; |
| 7351 |
end; |
| 7352 |
|
| 7353 |
OP_ANYBUT: |
| 7354 |
begin |
| 7355 |
GetCharSetFromCharClass(scan + REOpSz + RENextOffSz, False, TempSet); |
| 7356 |
FirstCharSet := FirstCharSet + (RegExprAllSet - TempSet); |
| 7357 |
Exit; |
| 7358 |
end; |
| 7359 |
|
| 7360 |
OP_ANYOF_CI: |
| 7361 |
begin |
| 7362 |
GetCharSetFromCharClass(scan + REOpSz + RENextOffSz, True, TempSet); |
| 7363 |
FirstCharSet := FirstCharSet + TempSet; |
| 7364 |
Exit; |
| 7365 |
end; |
| 7366 |
|
| 7367 |
OP_ANYBUT_CI: |
| 7368 |
begin |
| 7369 |
GetCharSetFromCharClass(scan + REOpSz + RENextOffSz, True, TempSet); |
| 7370 |
FirstCharSet := FirstCharSet + (RegExprAllSet - TempSet); |
| 7371 |
Exit; |
| 7372 |
end; |
| 7373 |
|
| 7374 |
OP_NOTHING: |
| 7375 |
; |
| 7376 |
OP_COMMENT: |
| 7377 |
; |
| 7378 |
OP_BACK: |
| 7379 |
begin |
| 7380 |
// No point to rescan the code again |
| 7381 |
Next := PRegExprChar(AlignToPtr(scan + 1)) + RENextOffSz;; |
| 7382 |
end; |
| 7383 |
|
| 7384 |
OP_OPEN, OP_OPEN_ATOMIC: |
| 7385 |
begin |
| 7386 |
FillFirstCharSet(Next); |
| 7387 |
Exit; |
| 7388 |
end; |
| 7389 |
|
| 7390 |
OP_CLOSE, OP_CLOSE_ATOMIC: |
| 7391 |
begin |
| 7392 |
FillFirstCharSet(Next); |
| 7393 |
Exit; |
| 7394 |
end; |
| 7395 |
|
| 7396 |
OP_LOOKAHEAD: |
| 7397 |
begin |
| 7398 |
opnd := PRegExprChar(AlignToPtr(Next + 1)) + RENextOffSz; |
| 7399 |
Next := regNextQuick(Next); |
| 7400 |
|
| 7401 |
TempSet := FirstCharSet; |
| 7402 |
FirstCharSet := []; |
| 7403 |
FillFirstCharSet(Next); // after the lookahead |
| 7404 |
|
| 7405 |
Next := PRegExprChar(AlignToPtr(scan + 1)) + RENextOffSz; |
| 7406 |
TmpFirstCharSet := FirstCharSet; |
| 7407 |
FirstCharSet := []; |
| 7408 |
FillFirstCharSet(Next); // inside the lookahead |
| 7409 |
|
| 7410 |
if TmpFirstCharSet = [] then |
| 7411 |
FirstCharSet := TempSet + FirstCharSet |
| 7412 |
else |
| 7413 |
if FirstCharSet = [] then |
| 7414 |
FirstCharSet := TempSet + TmpFirstCharSet |
| 7415 |
else |
| 7416 |
FirstCharSet := TempSet + (FirstCharSet * TmpFirstCharSet); |
| 7417 |
exit; |
| 7418 |
end; |
| 7419 |
|
| 7420 |
OP_LOOKAHEAD_NEG, |
| 7421 |
OP_LOOKBEHIND, OP_LOOKBEHIND_NEG: |
| 7422 |
begin |
| 7423 |
Next := PRegExprChar(AlignToPtr(Next + 1)) + RENextOffSz; |
| 7424 |
end; |
| 7425 |
|
| 7426 |
OP_LOOKAHEAD_END, OP_LOOKBEHIND_END: |
| 7427 |
begin |
| 7428 |
Exit; |
| 7429 |
end; |
| 7430 |
|
| 7431 |
OP_BRANCH, OP_GBRANCH, OP_GBRANCH_EX, OP_GBRANCH_EX_CI: |
| 7432 |
begin |
| 7433 |
repeat |
| 7434 |
TmpFirstCharSet := FirstCharSet; |
| 7435 |
FirstCharSet := []; |
| 7436 |
FillFirstCharSet(scan + REOpSz + RENextOffSz + REBranchArgSz); |
| 7437 |
FirstCharSet := FirstCharSet + TmpFirstCharSet; |
| 7438 |
scan := regNextQuick(scan); |
| 7439 |
until (scan = nil) or |
| 7440 |
( (PREOp(scan)^ <> OP_BRANCH) and (PREOp(Next)^ <> OP_GBRANCH) and |
| 7441 |
(PREOp(scan)^ <> OP_GBRANCH_EX) and (PREOp(scan)^ <> OP_GBRANCH_EX_CI) ); |
| 7442 |
Exit; |
| 7443 |
end; |
| 7444 |
|
| 7445 |
{$IFDEF ComplexBraces} |
| 7446 |
OP_LOOPENTRY: |
| 7447 |
begin |
| 7448 |
min_cnt := PREBracesArg(AlignToPtr(Next + REOpSz + RENextOffSz))^; |
| 7449 |
if min_cnt = 0 then begin |
| 7450 |
opnd := regNext(Next); |
| 7451 |
FillFirstCharSet(opnd); // FirstChar may be after loop |
| 7452 |
end; |
| 7453 |
Next := PRegExprChar(AlignToPtr(scan + 1)) + RENextOffSz; |
| 7454 |
end; |
| 7455 |
|
| 7456 |
OP_LOOP, |
| 7457 |
OP_LOOP_NG, |
| 7458 |
OP_LOOP_POSS: |
| 7459 |
begin |
| 7460 |
min_cnt := PREBracesArg(AlignToPtr(scan + REOpSz + RENextOffSz))^; |
| 7461 |
if min_cnt = 0 then |
| 7462 |
Exit; |
| 7463 |
// zero width loop |
| 7464 |
end; |
| 7465 |
{$ENDIF} |
| 7466 |
|
| 7467 |
OP_STAR, |
| 7468 |
OP_STAR_NG, |
| 7469 |
OP_STAR_POSS: |
| 7470 |
FillFirstCharSet(scan + REOpSz + RENextOffSz); |
| 7471 |
|
| 7472 |
OP_PLUS, |
| 7473 |
OP_PLUS_NG, |
| 7474 |
OP_PLUS_POSS: |
| 7475 |
begin |
| 7476 |
FillFirstCharSet(scan + REOpSz + RENextOffSz); |
| 7477 |
Exit; |
| 7478 |
end; |
| 7479 |
|
| 7480 |
OP_BRACES, |
| 7481 |
OP_BRACES_NG, |
| 7482 |
OP_BRACES_POSS: |
| 7483 |
begin |
| 7484 |
opnd := scan + REOpSz + RENextOffSz + REBracesArgSz * 2; |
| 7485 |
min_cnt := PREBracesArg(AlignToPtr(scan + REOpSz + RENextOffSz))^; // BRACES |
| 7486 |
FillFirstCharSet(opnd); |
| 7487 |
if min_cnt > 0 then |
| 7488 |
Exit; |
| 7489 |
end; |
| 7490 |
|
| 7491 |
OP_EEND: |
| 7492 |
begin |
| 7493 |
FirstCharSet := RegExprAllSet; |
| 7494 |
Exit; |
| 7495 |
end; |
| 7496 |
|
| 7497 |
OP_ANYCATEGORY, |
| 7498 |
OP_NOTCATEGORY: |
| 7499 |
begin |
| 7500 |
FirstCharSet := RegExprAllSet; |
| 7501 |
Exit; |
| 7502 |
end; |
| 7503 |
|
| 7504 |
OP_RECUR, |
| 7505 |
OP_SUBCALL: |
| 7506 |
begin |
| 7507 |
// we cannot optimize // TODO: lookup the called group |
| 7508 |
FirstCharSet := RegExprAllSet; |
| 7509 |
Exit; |
| 7510 |
end; |
| 7511 |
|
| 7512 |
OP_ANYLINEBREAK: |
| 7513 |
begin |
| 7514 |
Include(FirstCharSet, Byte(10)); |
| 7515 |
Include(FirstCharSet, Byte(13)); |
| 7516 |
Include(FirstCharSet, Byte($0B)); |
| 7517 |
Include(FirstCharSet, Byte($0C)); |
| 7518 |
Include(FirstCharSet, Byte($85)); |
| 7519 |
Exit; |
| 7520 |
end; |
| 7521 |
|
| 7522 |
else |
| 7523 |
fLastErrorOpcode := Oper; |
| 7524 |
Error(reeUnknownOpcodeInFillFirst); |
| 7525 |
Exit; |
| 7526 |
end; { of case scan^} |
| 7527 |
scan := Next; |
| 7528 |
end; { of while scan <> nil} |
| 7529 |
end; { of procedure FillFirstCharSet |
| 7530 |
--------------------------------------------------------------} |
| 7531 |
{$ENDIF} |
| 7532 |
|
| 7533 |
procedure TRegExpr.InitCharCheckers; |
| 7534 |
var |
| 7535 |
Cnt: Integer; |
| 7536 |
// |
| 7537 |
function Add(AChecker: TRegExprCharChecker): Byte; |
| 7538 |
begin |
| 7539 |
Inc(Cnt); |
| 7540 |
if Cnt > High(CharCheckers) then |
| 7541 |
Error(reeTooSmallCheckersArray); |
| 7542 |
CharCheckers[Cnt - 1] := AChecker; |
| 7543 |
Result := Cnt - 1; |
| 7544 |
end; |
| 7545 |
// |
| 7546 |
begin |
| 7547 |
Cnt := 0; |
| 7548 |
FillChar(CharCheckers, SizeOf(CharCheckers), 0); |
| 7549 |
|
| 7550 |
CheckerIndex_Word := Add(CharChecker_Word); |
| 7551 |
CheckerIndex_NotWord := Add(CharChecker_NotWord); |
| 7552 |
CheckerIndex_Space := Add(CharChecker_Space); |
| 7553 |
CheckerIndex_NotSpace := Add(CharChecker_NotSpace); |
| 7554 |
CheckerIndex_Digit := Add(CharChecker_Digit); |
| 7555 |
CheckerIndex_NotDigit := Add(CharChecker_NotDigit); |
| 7556 |
CheckerIndex_VertSep := Add(CharChecker_VertSep); |
| 7557 |
CheckerIndex_NotVertSep := Add(CharChecker_NotVertSep); |
| 7558 |
CheckerIndex_HorzSep := Add(CharChecker_HorzSep); |
| 7559 |
CheckerIndex_NotHorzSep := Add(CharChecker_NotHorzSep); |
| 7560 |
//CheckerIndex_AllAZ := Add(CharChecker_AllAZ); |
| 7561 |
CheckerIndex_LowerAZ := Add(CharChecker_LowerAZ); |
| 7562 |
CheckerIndex_UpperAZ := Add(CharChecker_UpperAZ); |
| 7563 |
CheckerIndex_AnyLineBreak := Add(CharChecker_AnyLineBreak); |
| 7564 |
|
| 7565 |
SetLength(CharCheckerInfos, 3); |
| 7566 |
with CharCheckerInfos[0] do |
| 7567 |
begin |
| 7568 |
CharBegin := 'a'; |
| 7569 |
CharEnd:= 'z'; |
| 7570 |
CheckerIndex := CheckerIndex_LowerAZ; |
| 7571 |
end; |
| 7572 |
with CharCheckerInfos[1] do |
| 7573 |
begin |
| 7574 |
CharBegin := 'A'; |
| 7575 |
CharEnd := 'Z'; |
| 7576 |
CheckerIndex := CheckerIndex_UpperAZ; |
| 7577 |
end; |
| 7578 |
with CharCheckerInfos[2] do |
| 7579 |
begin |
| 7580 |
CharBegin := '0'; |
| 7581 |
CharEnd := '9'; |
| 7582 |
CheckerIndex := CheckerIndex_Digit; |
| 7583 |
end; |
| 7584 |
end; |
| 7585 |
|
| 7586 |
function TRegExpr.CharChecker_Word(ch: REChar): Boolean; |
| 7587 |
begin |
| 7588 |
Result := IsWordChar(ch); |
| 7589 |
end; |
| 7590 |
|
| 7591 |
function TRegExpr.CharChecker_NotWord(ch: REChar): Boolean; |
| 7592 |
begin |
| 7593 |
Result := not IsWordChar(ch); |
| 7594 |
end; |
| 7595 |
|
| 7596 |
function TRegExpr.CharChecker_Space(ch: REChar): Boolean; |
| 7597 |
begin |
| 7598 |
Result := IsSpaceChar(ch); |
| 7599 |
end; |
| 7600 |
|
| 7601 |
function TRegExpr.CharChecker_NotSpace(ch: REChar): Boolean; |
| 7602 |
begin |
| 7603 |
Result := not IsSpaceChar(ch); |
| 7604 |
end; |
| 7605 |
|
| 7606 |
function TRegExpr.CharChecker_Digit(ch: REChar): Boolean; |
| 7607 |
begin |
| 7608 |
Result := IsDigitChar(ch); |
| 7609 |
end; |
| 7610 |
|
| 7611 |
function TRegExpr.CharChecker_NotDigit(ch: REChar): Boolean; |
| 7612 |
begin |
| 7613 |
Result := not IsDigitChar(ch); |
| 7614 |
end; |
| 7615 |
|
| 7616 |
function TRegExpr.CharChecker_VertSep(ch: REChar): Boolean; |
| 7617 |
begin |
| 7618 |
Result := IsVertLineSeparator(ch); |
| 7619 |
end; |
| 7620 |
|
| 7621 |
function TRegExpr.CharChecker_NotVertSep(ch: REChar): Boolean; |
| 7622 |
begin |
| 7623 |
Result := not IsVertLineSeparator(ch); |
| 7624 |
end; |
| 7625 |
|
| 7626 |
function TRegExpr.CharChecker_AnyLineBreak(ch: REChar): Boolean; |
| 7627 |
begin |
| 7628 |
Result := IsAnyLineBreak(ch); |
| 7629 |
end; |
| 7630 |
|
| 7631 |
function TRegExpr.CharChecker_HorzSep(ch: REChar): Boolean; |
| 7632 |
begin |
| 7633 |
Result := IsHorzSeparator(ch); |
| 7634 |
end; |
| 7635 |
|
| 7636 |
function TRegExpr.CharChecker_NotHorzSep(ch: REChar): Boolean; |
| 7637 |
begin |
| 7638 |
Result := not IsHorzSeparator(ch); |
| 7639 |
end; |
| 7640 |
|
| 7641 |
function TRegExpr.CharChecker_LowerAZ(ch: REChar): Boolean; |
| 7642 |
begin |
| 7643 |
case ch of |
| 7644 |
'a' .. 'z': |
| 7645 |
Result := True; |
| 7646 |
else |
| 7647 |
Result := False; |
| 7648 |
end; |
| 7649 |
end; |
| 7650 |
|
| 7651 |
function TRegExpr.CharChecker_UpperAZ(ch: REChar): Boolean; |
| 7652 |
begin |
| 7653 |
case ch of |
| 7654 |
'A' .. 'Z': |
| 7655 |
Result := True; |
| 7656 |
else |
| 7657 |
Result := False; |
| 7658 |
end; |
| 7659 |
end; |
| 7660 |
|
| 7661 |
|
| 7662 |
{$IFDEF RegExpPCodeDump} |
| 7663 |
|
| 7664 |
function TRegExpr.DumpOp(op: TREOp): RegExprString; |
| 7665 |
// printable representation of opcode |
| 7666 |
begin |
| 7667 |
case op of |
| 7668 |
OP_BOL: |
| 7669 |
Result := 'BOL'; |
| 7670 |
OP_EOL: |
| 7671 |
Result := 'EOL'; |
| 7672 |
OP_EOL2: |
| 7673 |
Result := 'EOL2'; |
| 7674 |
OP_BOL_ML: |
| 7675 |
Result := 'BOL_ML'; |
| 7676 |
OP_CONTINUE_POS: |
| 7677 |
Result := 'CONTINUE_POS'; |
| 7678 |
OP_EOL_ML: |
| 7679 |
Result := 'EOL_ML'; |
| 7680 |
OP_BOUND: |
| 7681 |
Result := 'BOUND'; |
| 7682 |
OP_NOTBOUND: |
| 7683 |
Result := 'NOTBOUND'; |
| 7684 |
OP_ANY: |
| 7685 |
Result := 'ANY'; |
| 7686 |
OP_ANY_ML: |
| 7687 |
Result := 'ANY_ML'; |
| 7688 |
OP_ANYLETTER: |
| 7689 |
Result := 'ANYLETTER'; |
| 7690 |
OP_NOTLETTER: |
| 7691 |
Result := 'NOTLETTER'; |
| 7692 |
OP_ANYDIGIT: |
| 7693 |
Result := 'ANYDIGIT'; |
| 7694 |
OP_NOTDIGIT: |
| 7695 |
Result := 'NOTDIGIT'; |
| 7696 |
OP_ANYSPACE: |
| 7697 |
Result := 'ANYSPACE'; |
| 7698 |
OP_NOTSPACE: |
| 7699 |
Result := 'NOTSPACE'; |
| 7700 |
OP_ANYHORZSEP: |
| 7701 |
Result := 'ANYHORZSEP'; |
| 7702 |
OP_NOTHORZSEP: |
| 7703 |
Result := 'NOTHORZSEP'; |
| 7704 |
OP_ANYVERTSEP: |
| 7705 |
Result := 'ANYVERTSEP'; |
| 7706 |
OP_NOTVERTSEP: |
| 7707 |
Result := 'NOTVERTSEP'; |
| 7708 |
OP_ANYOF: |
| 7709 |
Result := 'ANYOF'; |
| 7710 |
OP_ANYBUT: |
| 7711 |
Result := 'ANYBUT'; |
| 7712 |
OP_ANYOF_CI: |
| 7713 |
Result := 'ANYOF_CI'; |
| 7714 |
OP_ANYBUT_CI: |
| 7715 |
Result := 'ANYBUT_CI'; |
| 7716 |
OP_BRANCH: |
| 7717 |
Result := 'BRANCH'; |
| 7718 |
OP_GBRANCH: |
| 7719 |
Result := 'G_BRANCH'; |
| 7720 |
OP_GBRANCH_EX: |
| 7721 |
Result := 'G_BRANCH_EX'; |
| 7722 |
OP_GBRANCH_EX_CI: |
| 7723 |
Result := 'G_BRANCH_EX_CI'; |
| 7724 |
OP_EXACTLY: |
| 7725 |
Result := 'EXACTLY'; |
| 7726 |
OP_EXACTLY_CI: |
| 7727 |
Result := 'EXACTLY_CI'; |
| 7728 |
OP_NOTHING: |
| 7729 |
Result := 'NOTHING'; |
| 7730 |
OP_COMMENT: |
| 7731 |
Result := 'COMMENT'; |
| 7732 |
OP_BACK: |
| 7733 |
Result := 'BACK'; |
| 7734 |
OP_EEND: |
| 7735 |
Result := 'END'; |
| 7736 |
OP_BSUBEXP: |
| 7737 |
Result := 'BSUBEXP'; |
| 7738 |
OP_BSUBEXP_CI: |
| 7739 |
Result := 'BSUBEXP_CI'; |
| 7740 |
OP_OPEN: |
| 7741 |
Result := 'OPEN'; |
| 7742 |
OP_CLOSE: |
| 7743 |
Result := 'CLOSE'; |
| 7744 |
OP_OPEN_ATOMIC: |
| 7745 |
Result := 'OPEN_ATOMIC'; |
| 7746 |
OP_CLOSE_ATOMIC: |
| 7747 |
Result := 'CLOSE_ATOMIC'; |
| 7748 |
OP_LOOKAHEAD: |
| 7749 |
Result := 'LOOKAHEAD'; |
| 7750 |
OP_LOOKAHEAD_NEG: |
| 7751 |
Result := 'LOOKAHEAD_NEG'; |
| 7752 |
OP_LOOKBEHIND: |
| 7753 |
Result := 'LOOKBEHIND'; |
| 7754 |
OP_LOOKBEHIND_NEG: |
| 7755 |
Result := 'LOOKBEHIND_NEG'; |
| 7756 |
OP_LOOKAHEAD_END: |
| 7757 |
Result := 'LOOKAHEAD_END'; |
| 7758 |
OP_LOOKBEHIND_END: |
| 7759 |
Result := 'LOOKBEHIND_END'; |
| 7760 |
OP_STAR: |
| 7761 |
Result := 'STAR'; |
| 7762 |
OP_PLUS: |
| 7763 |
Result := 'PLUS'; |
| 7764 |
OP_BRACES: |
| 7765 |
Result := 'BRACES'; |
| 7766 |
{$IFDEF ComplexBraces} |
| 7767 |
OP_LOOPENTRY: |
| 7768 |
Result := 'LOOPENTRY'; |
| 7769 |
OP_LOOP: |
| 7770 |
Result := 'LOOP'; |
| 7771 |
OP_LOOP_NG: |
| 7772 |
Result := 'LOOP_NG'; |
| 7773 |
OP_LOOP_POSS: |
| 7774 |
Result := 'LOOP_POSS'; |
| 7775 |
{$ENDIF} |
| 7776 |
OP_STAR_NG: |
| 7777 |
Result := 'STAR_NG'; |
| 7778 |
OP_PLUS_NG: |
| 7779 |
Result := 'PLUS_NG'; |
| 7780 |
OP_BRACES_NG: |
| 7781 |
Result := 'BRACES_NG'; |
| 7782 |
OP_STAR_POSS: |
| 7783 |
Result := 'STAR_POSS'; |
| 7784 |
OP_PLUS_POSS: |
| 7785 |
Result := 'PLUS_POSS'; |
| 7786 |
OP_BRACES_POSS: |
| 7787 |
Result := 'BRACES_POSS'; |
| 7788 |
OP_ANYCATEGORY: |
| 7789 |
Result := 'ANYCATEGORY'; |
| 7790 |
OP_NOTCATEGORY: |
| 7791 |
Result := 'NOTCATEGORY'; |
| 7792 |
OP_RECUR: |
| 7793 |
Result := 'RECURSION'; |
| 7794 |
OP_SUBCALL: |
| 7795 |
Result := 'SUBCALL'; |
| 7796 |
OP_ANYLINEBREAK: |
| 7797 |
Result := 'ANYLINEBREAK'; |
| 7798 |
OP_RESET_MATCHPOS: |
| 7799 |
Result := 'RESET_MATCHPOS'; |
| 7800 |
else |
| 7801 |
Error(reeDumpCorruptedOpcode); |
| 7802 |
end; |
| 7803 |
end; { of function TRegExpr.DumpOp |
| 7804 |
-------------------------------------------------------------- } |
| 7805 |
|
| 7806 |
function TRegExpr.IsCompiled: Boolean; |
| 7807 |
begin |
| 7808 |
Result := programm <> nil; |
| 7809 |
end; |
| 7810 |
|
| 7811 |
function PrintableChar(AChar: REChar): RegExprString; {$IFDEF InlineFuncs}inline;{$ENDIF} |
| 7812 |
begin |
| 7813 |
if AChar < ' ' then |
| 7814 |
Result := '#' + IntToStr(Ord(AChar)) |
| 7815 |
else |
| 7816 |
Result := AChar; |
| 7817 |
end; |
| 7818 |
|
| 7819 |
function TRegExpr.DumpCheckerIndex(N: Byte): RegExprString; |
| 7820 |
begin |
| 7821 |
Result := '?'; |
| 7822 |
if N = CheckerIndex_Word then Result := '\w' else |
| 7823 |
if N = CheckerIndex_NotWord then Result := '\W' else |
| 7824 |
if N = CheckerIndex_Digit then Result := '\d' else |
| 7825 |
if N = CheckerIndex_NotDigit then Result := '\D' else |
| 7826 |
if N = CheckerIndex_Space then Result := '\s' else |
| 7827 |
if N = CheckerIndex_NotSpace then Result := '\S' else |
| 7828 |
if N = CheckerIndex_HorzSep then Result := '\h' else |
| 7829 |
if N = CheckerIndex_NotHorzSep then Result := '\H' else |
| 7830 |
if N = CheckerIndex_VertSep then Result := '\v' else |
| 7831 |
if N = CheckerIndex_NotVertSep then Result := '\V' else |
| 7832 |
if N = CheckerIndex_LowerAZ then Result := 'az' else |
| 7833 |
if N = CheckerIndex_UpperAZ then Result := 'AZ' else |
| 7834 |
if N = CheckerIndex_AnyLineBreak then Result := '\R' |
| 7835 |
; |
| 7836 |
end; |
| 7837 |
|
| 7838 |
function TRegExpr.DumpCategoryChars(ch, ch2: REChar; Positive: Boolean): RegExprString; |
| 7839 |
const |
| 7840 |
S: array[Boolean] of RegExprString = ('P', 'p'); |
| 7841 |
begin |
| 7842 |
Result := '\' + S[Positive] + '{' + ch; |
| 7843 |
if ch2 <> #0 then |
| 7844 |
Result := Result + ch2; |
| 7845 |
Result := Result + '} '; |
| 7846 |
end; |
| 7847 |
|
| 7848 |
function TRegExpr.Dump(Indent: Integer): RegExprString; |
| 7849 |
// dump a regexp in vaguely comprehensible form |
| 7850 |
var |
| 7851 |
s: PRegExprChar; |
| 7852 |
op: TREOp; // Arbitrary non-END op. |
| 7853 |
next, BranchEnd: PRegExprChar; |
| 7854 |
BranchEndStack: Array of PRegExprChar; |
| 7855 |
i, NLen, CurIndent: Integer; |
| 7856 |
Diff: PtrInt; |
| 7857 |
iByte: Byte; |
| 7858 |
ch, ch2: REChar; |
| 7859 |
begin |
| 7860 |
Result := ''; |
| 7861 |
if not IsProgrammOk then |
| 7862 |
Exit; |
| 7863 |
|
| 7864 |
CurIndent := 0; |
| 7865 |
op := OP_EXACTLY; |
| 7866 |
s := regCodeWork; |
| 7867 |
BranchEnd := nil; |
| 7868 |
while op <> OP_EEND do |
| 7869 |
begin // While that wasn't END last time... |
| 7870 |
op := s^; |
| 7871 |
next := regNext(s); |
| 7872 |
|
| 7873 |
if ((op =OP_CLOSE) or (op = OP_CLOSE_ATOMIC) or (op = OP_LOOP) or (op = OP_LOOP_NG) or (op = OP_LOOP_POSS) or |
| 7874 |
(op = OP_LOOKAHEAD_END) or (op = OP_LOOKBEHIND_END) |
| 7875 |
) and |
| 7876 |
(CurIndent > 0) |
| 7877 |
then |
| 7878 |
dec(CurIndent, Indent); |
| 7879 |
if s = BranchEnd then begin |
| 7880 |
dec(CurIndent, Indent); |
| 7881 |
BranchEnd := nil; |
| 7882 |
if Length(BranchEndStack) > 0 then begin |
| 7883 |
BranchEnd := BranchEndStack[Length(BranchEndStack)-1]; |
| 7884 |
SetLength(BranchEndStack, Length(BranchEndStack)-1); |
| 7885 |
end; |
| 7886 |
end; |
| 7887 |
|
| 7888 |
Result := Result + Format('%3d:%s %s', [s - programm, StringOfChar(' ', CurIndent), DumpOp(s^)]); |
| 7889 |
|
| 7890 |
if (op = OP_OPEN) or (op = OP_OPEN_ATOMIC) or (op = OP_LOOPENTRY) or |
| 7891 |
(op = OP_LOOKAHEAD) or (op = OP_LOOKAHEAD_NEG) or (op = OP_LOOKBEHIND) or (op = OP_LOOKBEHIND_NEG) |
| 7892 |
then |
| 7893 |
inc(CurIndent, Indent); |
| 7894 |
if (op = OP_BRANCH) or (op = OP_GBRANCH) or (op = OP_GBRANCH_EX) or (op = OP_GBRANCH_EX_CI) then begin |
| 7895 |
inc(CurIndent, Indent); |
| 7896 |
if BranchEnd <> nil then begin |
| 7897 |
SetLength(BranchEndStack, Length(BranchEndStack)+1); |
| 7898 |
BranchEndStack[Length(BranchEndStack)-1] := BranchEnd; |
| 7899 |
end; |
| 7900 |
BranchEnd := next; |
| 7901 |
end; |
| 7902 |
|
| 7903 |
// Where, what. |
| 7904 |
if next = nil // Next ptr. |
| 7905 |
then |
| 7906 |
Result := Result + ' (0)' |
| 7907 |
else |
| 7908 |
begin |
| 7909 |
if next > s |
| 7910 |
// PWideChar subtraction workaround (see comments in Tail method for details) |
| 7911 |
then |
| 7912 |
Diff := next - s |
| 7913 |
else |
| 7914 |
Diff := -(s - next); |
| 7915 |
Result := Result + Format(' (%d) ', [(s - programm) + Diff]); |
| 7916 |
end; |
| 7917 |
Inc(s, REOpSz + RENextOffSz); |
| 7918 |
if (op = OP_ANYOF) or (op = OP_ANYOF_CI) or (op = OP_ANYBUT) or (op = OP_ANYBUT_CI) then |
| 7919 |
begin |
| 7920 |
repeat |
| 7921 |
case s^ of |
| 7922 |
OpKind_End: |
| 7923 |
begin |
| 7924 |
Inc(s); |
| 7925 |
Break; |
| 7926 |
end; |
| 7927 |
OpKind_Range: |
| 7928 |
begin |
| 7929 |
Result := Result + 'Rng('; |
| 7930 |
Inc(s); |
| 7931 |
Result := Result + PrintableChar(s^) + '-'; |
| 7932 |
Inc(s); |
| 7933 |
Result := Result + PrintableChar(s^); |
| 7934 |
Result := Result + ') '; |
| 7935 |
Inc(s); |
| 7936 |
end; |
| 7937 |
OpKind_MetaClass: |
| 7938 |
begin |
| 7939 |
Inc(s); |
| 7940 |
Result := Result + DumpCheckerIndex(Byte(s^)) + ' '; |
| 7941 |
Inc(s); |
| 7942 |
end; |
| 7943 |
OpKind_Char: |
| 7944 |
begin |
| 7945 |
Inc(s); |
| 7946 |
NLen := PLongInt(s)^; |
| 7947 |
Inc(s, RENumberSz); |
| 7948 |
Result := Result + 'Ch('; |
| 7949 |
for i := 1 to NLen do |
| 7950 |
begin |
| 7951 |
Result := Result + PrintableChar(s^); |
| 7952 |
Inc(s); |
| 7953 |
end; |
| 7954 |
Result := Result + ') '; |
| 7955 |
end; |
| 7956 |
OpKind_CategoryYes: |
| 7957 |
begin |
| 7958 |
Inc(s); |
| 7959 |
ch := s^; |
| 7960 |
Inc(s); |
| 7961 |
ch2 := s^; |
| 7962 |
Result := Result + DumpCategoryChars(ch, ch2, True); |
| 7963 |
Inc(s); |
| 7964 |
end; |
| 7965 |
OpKind_CategoryNo: |
| 7966 |
begin |
| 7967 |
Inc(s); |
| 7968 |
ch := s^; |
| 7969 |
Inc(s); |
| 7970 |
ch2 := s^; |
| 7971 |
Result := Result + DumpCategoryChars(ch, ch2, False); |
| 7972 |
Inc(s); |
| 7973 |
end; |
| 7974 |
else |
| 7975 |
Error(reeDumpCorruptedOpcode); |
| 7976 |
end; |
| 7977 |
until false; |
| 7978 |
end; |
| 7979 |
if (op = OP_EXACTLY) or (op = OP_EXACTLY_CI) then |
| 7980 |
begin |
| 7981 |
// Literal string, where present. |
| 7982 |
NLen := PLongInt(s)^; |
| 7983 |
Inc(s, RENumberSz); |
| 7984 |
for i := 1 to NLen do |
| 7985 |
begin |
| 7986 |
Result := Result + PrintableChar(s^); |
| 7987 |
Inc(s); |
| 7988 |
end; |
| 7989 |
end; |
| 7990 |
if (op = OP_BSUBEXP) or (op = OP_BSUBEXP_CI) then |
| 7991 |
begin |
| 7992 |
Result := Result + ' \' + IntToStr(PReGroupIndex(s)^); |
| 7993 |
Inc(s, ReGroupIndexSz); |
| 7994 |
end; |
| 7995 |
if (op = OP_SUBCALL) then |
| 7996 |
begin |
| 7997 |
Result := Result + ' (?' + IntToStr(PReGroupIndex(s)^) + ') @' + IntToStr(GrpOpCodes[PReGroupIndex(s)^]-programm); |
| 7998 |
Inc(s, ReGroupIndexSz); |
| 7999 |
end; |
| 8000 |
if (op = OP_OPEN) or (op = OP_OPEN_ATOMIC) or (op = OP_CLOSE) or (op = OP_CLOSE_ATOMIC) then |
| 8001 |
begin |
| 8002 |
Result := Result + ' [' + IntToStr(PReGroupIndex(s)^) + ']'; |
| 8003 |
Inc(s, ReGroupIndexSz); |
| 8004 |
end; |
| 8005 |
if (op = OP_BRACES) or (op = OP_BRACES_NG) or (op = OP_BRACES_POSS) then |
| 8006 |
begin |
| 8007 |
// show min/max argument of braces operator |
| 8008 |
Result := Result + Format('{%d,%d}', [PREBracesArg(AlignToInt(s))^, |
| 8009 |
PREBracesArg(AlignToInt(s + REBracesArgSz))^]); |
| 8010 |
Inc(s, REBracesArgSz * 2); |
| 8011 |
end; |
| 8012 |
{$IFDEF ComplexBraces} |
| 8013 |
if (op = OP_LOOP) or (op = OP_LOOP_NG) or (op = OP_LOOP_POSS) then |
| 8014 |
begin |
| 8015 |
Result := Result + Format(' -> (%d) {%d,%d}', |
| 8016 |
[(s - programm - (REOpSz + RENextOffSz)) + |
| 8017 |
PRENextOff(AlignToPtr(s + 2 * REBracesArgSz))^, |
| 8018 |
PREBracesArg(AlignToInt(s))^, |
| 8019 |
PREBracesArg(AlignToInt(s + REBracesArgSz))^]); |
| 8020 |
Inc(s, 2 * REBracesArgSz + RENextOffSz); |
| 8021 |
end; |
| 8022 |
{$ENDIF} |
| 8023 |
if (op = OP_ANYCATEGORY) or (op = OP_NOTCATEGORY) then |
| 8024 |
begin |
| 8025 |
ch := s^; |
| 8026 |
Inc(s); |
| 8027 |
ch2 := s^; |
| 8028 |
Inc(s); |
| 8029 |
if ch2<>#0 then |
| 8030 |
Result := Result + '{' + ch + ch2 + '}' |
| 8031 |
else |
| 8032 |
Result := Result + '{' + ch + '}'; |
| 8033 |
end; |
| 8034 |
if (op = OP_LOOKBEHIND) or (op = OP_LOOKBEHIND_NEG) then |
| 8035 |
begin |
| 8036 |
if PReOpLookBehindOptions(s)^.IsGreedy = OPT_LOOKBEHIND_FIXED then |
| 8037 |
Result := Result + ' (fixed)' |
| 8038 |
else |
| 8039 |
if PReOpLookBehindOptions(s)^.IsGreedy = OPT_LOOKBEHIND_NON_GREEDY then |
| 8040 |
Result := Result + ' (not greedy)' |
| 8041 |
else |
| 8042 |
Result := Result + ' (greedy)'; |
| 8043 |
Result := Result |
| 8044 |
+ ' Len: ' + IntToStr(PReOpLookBehindOptions(s)^.MatchLenMin) |
| 8045 |
+ '..' + IntToStr(PReOpLookBehindOptions(s)^.MatchLenMax); |
| 8046 |
Inc(s, ReOpLookBehindOptionsSz); |
| 8047 |
end |
| 8048 |
else |
| 8049 |
if (op = OP_BRANCH) or (op = OP_GBRANCH) then |
| 8050 |
begin |
| 8051 |
Inc(s, REBranchArgSz); |
| 8052 |
end |
| 8053 |
else |
| 8054 |
if (op = OP_GBRANCH_EX) or (op = OP_GBRANCH_EX_CI) then |
| 8055 |
begin |
| 8056 |
Result := Result + ' ' + s^; |
| 8057 |
if (op = OP_GBRANCH_EX_CI) then |
| 8058 |
Result := Result + (s+1)^; |
| 8059 |
Inc(s, REBranchArgSz); |
| 8060 |
end; |
| 8061 |
Result := Result + #$d#$a; |
| 8062 |
end; { of while } |
| 8063 |
|
| 8064 |
// Header fields of interest. |
| 8065 |
case regAnchored of |
| 8066 |
raBOL: Result := Result + 'Anchored(BOL); '; |
| 8067 |
raEOL: Result := Result + 'Anchored(EOL); '; |
| 8068 |
raContinue: Result := Result + 'Anchored(\G); '; |
| 8069 |
raOnlyOnce: Result := Result + 'Anchored(start); '; |
| 8070 |
end; |
| 8071 |
|
| 8072 |
if regMustString <> '' then |
| 8073 |
Result := Result + 'Must have: "' + regMustString + '"; '; |
| 8074 |
|
| 8075 |
{$IFDEF UseFirstCharSet} |
| 8076 |
Result := Result + #$d#$a'First charset: '; |
| 8077 |
if FirstCharSet = [] then |
| 8078 |
Result := Result + '<empty set>' |
| 8079 |
else |
| 8080 |
if FirstCharSet = RegExprAllSet then |
| 8081 |
Result := Result + '<all chars>' |
| 8082 |
else |
| 8083 |
for iByte := 0 to 255 do |
| 8084 |
if iByte in FirstCharSet then |
| 8085 |
Result := Result + PrintableChar(REChar(iByte)); |
| 8086 |
{$ENDIF} |
| 8087 |
Result := Result + #$d#$a; |
| 8088 |
end; { of function TRegExpr.Dump |
| 8089 |
-------------------------------------------------------------- } |
| 8090 |
{$ENDIF} |
| 8091 |
|
| 8092 |
|
| 8093 |
function TRegExpr.IsFixedLength(var op: TREOp; var ALen: Integer): Boolean; |
| 8094 |
var |
| 8095 |
s: PRegExprChar; |
| 8096 |
ADummyMaxLen: integer; |
| 8097 |
begin |
| 8098 |
Result := False; |
| 8099 |
if not IsCompiled then Exit; |
| 8100 |
s := regCodeWork; |
| 8101 |
Result := IsPartFixedLength(s, op, ALen, ADummyMaxLen, OP_EEND, nil, []); |
| 8102 |
end; |
| 8103 |
|
| 8104 |
function TRegExpr.IsFixedLengthEx(var op: TREOp; var AMinLen, AMaxLen: integer |
| 8105 |
): boolean; |
| 8106 |
var |
| 8107 |
s: PRegExprChar; |
| 8108 |
begin |
| 8109 |
Result := False; |
| 8110 |
if not IsCompiled then Exit; |
| 8111 |
s := regCodeWork; |
| 8112 |
Result := IsPartFixedLength(s, op, AMinLen, AMaxLen, OP_EEND, nil, []); |
| 8113 |
end; |
| 8114 |
|
| 8115 |
function TRegExpr.IsPartFixedLength(var prog: PRegExprChar; var op: TREOp; |
| 8116 |
var AMinLen, AMaxLen: integer; StopAt: TREOp; StopMaxProg: PRegExprChar; |
| 8117 |
Flags: TRegExprFindFixedLengthFlags): boolean; |
| 8118 |
|
| 8119 |
function MultiplyLen(AVal, AFactor: Integer): Integer; |
| 8120 |
begin |
| 8121 |
if AFactor > High(AVal) div AVal then |
| 8122 |
Result := high(AVal) |
| 8123 |
else |
| 8124 |
Result := AVal * AFactor; |
| 8125 |
end; |
| 8126 |
|
| 8127 |
procedure IncMaxLen(var AVal: Integer; AInc: Integer); |
| 8128 |
begin |
| 8129 |
if AInc > High(AVal) - AVal then |
| 8130 |
AVal := high(AVal) |
| 8131 |
else |
| 8132 |
AVal := AVal + AInc; |
| 8133 |
end; |
| 8134 |
|
| 8135 |
function MaxStopOrNext(next: PRegExprChar): PRegExprChar; |
| 8136 |
begin |
| 8137 |
Result := next; |
| 8138 |
if (Result = nil) or ( (StopMaxProg <> nil) and (Result > StopMaxProg) ) then |
| 8139 |
Result := StopMaxProg; |
| 8140 |
end; |
| 8141 |
|
| 8142 |
var |
| 8143 |
s, next: PRegExprChar; |
| 8144 |
N, N2, FndMaxLen, ASubLen, ABranchLen, ABranchMaxLen, ASubMaxLen: integer; |
| 8145 |
NotFixedLen, r, NextIsNil: Boolean; |
| 8146 |
FirstVarLenOp: TREOp; |
| 8147 |
begin |
| 8148 |
Result := False; |
| 8149 |
NotFixedLen := False; |
| 8150 |
AMinLen := 0; |
| 8151 |
AMaxLen := High(AMaxLen); |
| 8152 |
FndMaxLen := 0; |
| 8153 |
next := prog; |
| 8154 |
s := prog; |
| 8155 |
|
| 8156 |
repeat |
| 8157 |
NextIsNil := next = nil; |
| 8158 |
next := regNext(s); |
| 8159 |
prog := s; |
| 8160 |
op := s^; |
| 8161 |
if not NotFixedLen then |
| 8162 |
FirstVarLenOp := op; |
| 8163 |
|
| 8164 |
if (op = StopAt) or |
| 8165 |
((StopMaxProg <> nil) and (s >= StopMaxProg)) or |
| 8166 |
(NextIsNil and (flfReturnAtNextNil in Flags)) |
| 8167 |
then begin |
| 8168 |
AMaxLen := FndMaxLen; |
| 8169 |
op := FirstVarLenOp; |
| 8170 |
if not NotFixedLen then |
| 8171 |
Result := True; |
| 8172 |
Exit; |
| 8173 |
end; |
| 8174 |
|
| 8175 |
Inc(s, REOpSz + RENextOffSz); |
| 8176 |
|
| 8177 |
case op of |
| 8178 |
OP_EEND, OP_BACK: |
| 8179 |
begin |
| 8180 |
AMaxLen := FndMaxLen; |
| 8181 |
op := FirstVarLenOp; |
| 8182 |
if not NotFixedLen then |
| 8183 |
Result := True; |
| 8184 |
Exit; |
| 8185 |
end; |
| 8186 |
|
| 8187 |
OP_BRANCH, OP_GBRANCH, OP_GBRANCH_EX, OP_GBRANCH_EX_CI: |
| 8188 |
begin |
| 8189 |
s := s + REBranchArgSz; |
| 8190 |
if not IsPartFixedLength(s, op, ABranchLen, ABranchMaxLen, OP_EEND, MaxStopOrNext(next), Flags * [flfReturnAtNextNil, flfSkipLookAround]) then |
| 8191 |
begin |
| 8192 |
if not NotFixedLen then |
| 8193 |
FirstVarLenOp := op; |
| 8194 |
NotFixedLen := True; |
| 8195 |
end; |
| 8196 |
s := next; |
| 8197 |
repeat |
| 8198 |
next := regNext(s); |
| 8199 |
s := s + REBranchArgSz; |
| 8200 |
Inc(s, REOpSz + RENextOffSz); |
| 8201 |
if not IsPartFixedLength(s, op, ASubLen, ASubMaxLen, OP_EEND, MaxStopOrNext(next), Flags * [flfReturnAtNextNil, flfSkipLookAround]) then |
| 8202 |
begin |
| 8203 |
if not NotFixedLen then |
| 8204 |
FirstVarLenOp := op; |
| 8205 |
NotFixedLen := True; |
| 8206 |
end; |
| 8207 |
s := next; |
| 8208 |
if (ASubLen <> ABranchLen) then |
| 8209 |
NotFixedLen := True; |
| 8210 |
if ASubLen < ABranchLen then |
| 8211 |
ABranchLen := ASubLen; |
| 8212 |
if ASubMaxLen > ABranchMaxLen then |
| 8213 |
ABranchMaxLen := ASubMaxLen; |
| 8214 |
until (next^ <> OP_BRANCH) and (next^ <> OP_GBRANCH) and |
| 8215 |
(next^ <> OP_GBRANCH_EX) and (next^ <> OP_GBRANCH_EX_CI); |
| 8216 |
AMinLen := AMinLen + ABranchLen; |
| 8217 |
IncMaxLen(FndMaxLen, ABranchMaxLen); |
| 8218 |
end; |
| 8219 |
|
| 8220 |
OP_OPEN: |
| 8221 |
begin |
| 8222 |
Inc(s, ReGroupIndexSz); |
| 8223 |
if not IsPartFixedLength(s, op, ASubLen, ASubMaxLen, OP_CLOSE, StopMaxProg, Flags * [flfReturnAtNextNil, flfSkipLookAround]) then |
| 8224 |
begin |
| 8225 |
if not NotFixedLen then |
| 8226 |
FirstVarLenOp := op; |
| 8227 |
NotFixedLen := True; |
| 8228 |
end; |
| 8229 |
assert(s^=OP_CLOSE); |
| 8230 |
AMinLen := AMinLen + ASubLen; |
| 8231 |
IncMaxLen(FndMaxLen, ASubMaxLen); |
| 8232 |
Inc(s, REOpSz + RENextOffSz + ReGroupIndexSz); // consume the OP_CLOSE |
| 8233 |
continue; |
| 8234 |
end; |
| 8235 |
|
| 8236 |
OP_OPEN_ATOMIC: |
| 8237 |
begin |
| 8238 |
Inc(s, ReGroupIndexSz); |
| 8239 |
if not IsPartFixedLength(s, op, ASubLen, ASubMaxLen, OP_CLOSE_ATOMIC, StopMaxProg, Flags * [flfReturnAtNextNil, flfSkipLookAround]) then |
| 8240 |
begin |
| 8241 |
if not NotFixedLen then |
| 8242 |
FirstVarLenOp := op; |
| 8243 |
NotFixedLen := True; |
| 8244 |
end; |
| 8245 |
assert(s^=OP_CLOSE_ATOMIC); |
| 8246 |
AMinLen := AMinLen + ASubLen; |
| 8247 |
IncMaxLen(FndMaxLen, ASubMaxLen); |
| 8248 |
Inc(s, REOpSz + RENextOffSz + ReGroupIndexSz); // consume the OP_CLOSE_ATOMIC; |
| 8249 |
continue; |
| 8250 |
end; |
| 8251 |
|
| 8252 |
OP_CLOSE, OP_CLOSE_ATOMIC: |
| 8253 |
begin |
| 8254 |
Inc(s, ReGroupIndexSz); |
| 8255 |
continue; |
| 8256 |
end; |
| 8257 |
|
| 8258 |
OP_LOOKAHEAD, OP_LOOKAHEAD_NEG: |
| 8259 |
begin |
| 8260 |
r := IsPartFixedLength(s, op, ASubLen, ASubMaxLen, OP_LOOKAHEAD_END, MaxStopOrNext(next), [flfSkipLookAround] + Flags * [flfReturnAtNextNil]); |
| 8261 |
s := next; |
| 8262 |
Inc(s, REOpSz + RENextOffSz); // skip the OP_LOOKAHEAD_END |
| 8263 |
if not (flfSkipLookAround in Flags) then |
| 8264 |
begin |
| 8265 |
//if not r then |
| 8266 |
NotFixedLen := True; |
| 8267 |
end; |
| 8268 |
end; |
| 8269 |
|
| 8270 |
OP_LOOKBEHIND, OP_LOOKBEHIND_NEG: |
| 8271 |
begin |
| 8272 |
Inc(s, ReOpLookBehindOptionsSz); |
| 8273 |
r := IsPartFixedLength(s, op, ASubLen, ASubMaxLen, OP_LOOKBEHIND_END, MaxStopOrNext(next), [flfSkipLookAround] + Flags * [flfReturnAtNextNil]); |
| 8274 |
s := next; |
| 8275 |
Inc(s, REOpSz + RENextOffSz); // skip the OP_LOOKBEHIND_END |
| 8276 |
if not (flfSkipLookAround in Flags) then |
| 8277 |
NotFixedLen := True |
| 8278 |
end; |
| 8279 |
|
| 8280 |
OP_LOOKAHEAD_END, OP_LOOKBEHIND_END: |
| 8281 |
if flfSkipLookAround in Flags then |
| 8282 |
begin |
| 8283 |
continue; |
| 8284 |
end; |
| 8285 |
|
| 8286 |
OP_NOTHING, |
| 8287 |
OP_COMMENT, |
| 8288 |
OP_BOUND, |
| 8289 |
OP_NOTBOUND, |
| 8290 |
OP_BOL, |
| 8291 |
OP_BOL_ML, |
| 8292 |
OP_EOL, |
| 8293 |
OP_EOL2, |
| 8294 |
OP_EOL_ML, |
| 8295 |
OP_CONTINUE_POS: |
| 8296 |
Continue; |
| 8297 |
|
| 8298 |
OP_ANY, |
| 8299 |
OP_ANY_ML, |
| 8300 |
OP_ANYDIGIT, |
| 8301 |
OP_NOTDIGIT, |
| 8302 |
OP_ANYLETTER, |
| 8303 |
OP_NOTLETTER, |
| 8304 |
OP_ANYSPACE, |
| 8305 |
OP_NOTSPACE, |
| 8306 |
OP_ANYHORZSEP, |
| 8307 |
OP_NOTHORZSEP, |
| 8308 |
OP_ANYVERTSEP, |
| 8309 |
OP_NOTVERTSEP: |
| 8310 |
begin |
| 8311 |
Inc(AMinLen); |
| 8312 |
IncMaxLen(FndMaxLen, 1); |
| 8313 |
Continue; |
| 8314 |
end; |
| 8315 |
|
| 8316 |
OP_ANYOF, |
| 8317 |
OP_ANYOF_CI, |
| 8318 |
OP_ANYBUT, |
| 8319 |
OP_ANYBUT_CI: |
| 8320 |
begin |
| 8321 |
Inc(AMinLen); |
| 8322 |
IncMaxLen(FndMaxLen, 1); |
| 8323 |
repeat |
| 8324 |
case s^ of |
| 8325 |
OpKind_End: |
| 8326 |
begin |
| 8327 |
Inc(s); |
| 8328 |
Break; |
| 8329 |
end; |
| 8330 |
OpKind_Range: |
| 8331 |
begin |
| 8332 |
Inc(s); |
| 8333 |
Inc(s); |
| 8334 |
Inc(s); |
| 8335 |
end; |
| 8336 |
OpKind_MetaClass: |
| 8337 |
begin |
| 8338 |
Inc(s); |
| 8339 |
Inc(s); |
| 8340 |
end; |
| 8341 |
OpKind_Char: |
| 8342 |
begin |
| 8343 |
Inc(s); |
| 8344 |
Inc(s, RENumberSz + PLongInt(s)^); |
| 8345 |
end; |
| 8346 |
OpKind_CategoryYes, |
| 8347 |
OpKind_CategoryNo: |
| 8348 |
begin |
| 8349 |
Inc(s); |
| 8350 |
Inc(s); |
| 8351 |
Inc(s); |
| 8352 |
end; |
| 8353 |
end; |
| 8354 |
until False; |
| 8355 |
end; |
| 8356 |
|
| 8357 |
OP_EXACTLY, |
| 8358 |
OP_EXACTLY_CI: |
| 8359 |
begin |
| 8360 |
N := PLongInt(s)^; |
| 8361 |
Inc(AMinLen, N); |
| 8362 |
IncMaxLen(FndMaxLen, N); |
| 8363 |
Inc(s, RENumberSz + N); |
| 8364 |
Continue; |
| 8365 |
end; |
| 8366 |
|
| 8367 |
OP_ANYCATEGORY, |
| 8368 |
OP_NOTCATEGORY: |
| 8369 |
begin |
| 8370 |
Inc(AMinLen); |
| 8371 |
IncMaxLen(FndMaxLen, 1); |
| 8372 |
Inc(s, 2); |
| 8373 |
Continue; |
| 8374 |
end; |
| 8375 |
|
| 8376 |
OP_BRACES, |
| 8377 |
OP_BRACES_NG, |
| 8378 |
OP_BRACES_POSS: |
| 8379 |
begin |
| 8380 |
// allow only d{n,n} |
| 8381 |
N := PREBracesArg(AlignToInt(s))^; |
| 8382 |
N2 := PREBracesArg(AlignToInt(s + REBracesArgSz))^; |
| 8383 |
Inc(s, REBracesArgSz * 2); |
| 8384 |
r := IsPartFixedLength(s, op, ASubLen, ASubMaxLen, OP_EEND, MaxStopOrNext(next), [flfSkipLookAround, flfReturnAtNextNil]); |
| 8385 |
if not r then |
| 8386 |
begin |
| 8387 |
if not NotFixedLen then |
| 8388 |
FirstVarLenOp := op; |
| 8389 |
end; |
| 8390 |
|
| 8391 |
Inc(AMinLen, MultiplyLen(ASubLen, N)); |
| 8392 |
IncMaxLen(FndMaxLen, MultiplyLen(ASubMaxLen, N2)); |
| 8393 |
if (not r) or (N <> N2) then |
| 8394 |
NotFixedLen := True; |
| 8395 |
s := next; |
| 8396 |
end; |
| 8397 |
|
| 8398 |
OP_BSUBEXP, OP_BSUBEXP_CI, OP_SUBCALL: |
| 8399 |
begin |
| 8400 |
s := next; |
| 8401 |
NotFixedLen := True; // group may be in look-around. Could be anything |
| 8402 |
FndMaxLen := high(FndMaxLen); |
| 8403 |
end; |
| 8404 |
|
| 8405 |
OP_PLUS, OP_PLUS_NG, OP_PLUS_POSS: |
| 8406 |
begin |
| 8407 |
s := next; |
| 8408 |
Inc(AMinLen, 1); |
| 8409 |
FndMaxLen := high(FndMaxLen); |
| 8410 |
NotFixedLen := True |
| 8411 |
end; |
| 8412 |
|
| 8413 |
else // OP_STAR... |
| 8414 |
begin |
| 8415 |
s := next; |
| 8416 |
FndMaxLen := high(FndMaxLen); |
| 8417 |
NotFixedLen := True |
| 8418 |
end; |
| 8419 |
end; |
| 8420 |
until False; |
| 8421 |
end; |
| 8422 |
|
| 8423 |
procedure TRegExpr.SetInputSubString(const AInputString: RegExprString; |
| 8424 |
AInputStartPos, AInputLen: Integer); |
| 8425 |
begin |
| 8426 |
ClearMatches; |
| 8427 |
|
| 8428 |
if AInputStartPos < 1 then |
| 8429 |
AInputStartPos := 1 |
| 8430 |
else |
| 8431 |
if AInputStartPos > Length(AInputString) then |
| 8432 |
AInputStartPos := Length(AInputString) + 1; |
| 8433 |
if AInputLen < 0 then |
| 8434 |
AInputLen := 0 |
| 8435 |
else |
| 8436 |
if AInputLen > Length(AInputString) + 1 - AInputStartPos then |
| 8437 |
AInputLen := Length(AInputString) + 1 - AInputStartPos; |
| 8438 |
|
| 8439 |
fInputString := AInputString; |
| 8440 |
//UniqueString(fInputString); |
| 8441 |
|
| 8442 |
fInputStart := PRegExprChar(fInputString) + AInputStartPos - 1; |
| 8443 |
fInputEnd := fInputStart + AInputLen; |
| 8444 |
fInputContinue := fInputStart; |
| 8445 |
end; |
| 8446 |
|
| 8447 |
{$IFDEF reRealExceptionAddr} |
| 8448 |
{$OPTIMIZATION ON} |
| 8449 |
// ReturnAddr works correctly only if compiler optimization is ON |
| 8450 |
// I placed this method at very end of unit because there are no |
| 8451 |
// way to restore compiler optimization flag ... |
| 8452 |
{$ENDIF} |
| 8453 |
|
| 8454 |
procedure TRegExpr.Error(AErrorID: Integer); |
| 8455 |
{$IFDEF windows} |
| 8456 |
{$IFDEF reRealExceptionAddr} |
| 8457 |
function ReturnAddr: Pointer; |
| 8458 |
asm |
| 8459 |
mov eax,[ebp+4] |
| 8460 |
end; |
| 8461 |
{$ENDIF} |
| 8462 |
{$ENDIF} |
| 8463 |
var |
| 8464 |
e: ERegExpr; |
| 8465 |
Msg: string; |
| 8466 |
begin |
| 8467 |
fLastError := AErrorID; // dummy stub - useless because will raise exception |
| 8468 |
Msg := ErrorMsg(AErrorID); |
| 8469 |
// compilation error ? |
| 8470 |
if AErrorID < reeFirstRuntimeCode then |
| 8471 |
Msg := Msg + ' (pos ' + IntToStr(CompilerErrorPos) + ')'; |
| 8472 |
e := ERegExpr.Create(Msg); |
| 8473 |
e.ErrorCode := AErrorID; |
| 8474 |
e.CompilerErrorPos := CompilerErrorPos; |
| 8475 |
raise e |
| 8476 |
{$IFDEF windows} |
| 8477 |
{$IFDEF reRealExceptionAddr} |
| 8478 |
at ReturnAddr |
| 8479 |
{$ENDIF} |
| 8480 |
{$ENDIF}; |
| 8481 |
end; { of procedure TRegExpr.Error |
| 8482 |
-------------------------------------------------------------- } |
| 8483 |
|
| 8484 |
{$IFDEF Compat} // APIs needed only for users of old FPC 3.0 |
| 8485 |
function TRegExpr.ExecPos(AOffset: Integer; ATryOnce: Boolean): Boolean; overload; |
| 8486 |
begin |
| 8487 |
// Check that the start position is not negative |
| 8488 |
if AOffset < 1 then |
| 8489 |
begin |
| 8490 |
ClearMatches; |
| 8491 |
Error(reeOffsetMustBePositive); |
| 8492 |
Result := False; |
| 8493 |
Exit; |
| 8494 |
end; |
| 8495 |
if ATryOnce then |
| 8496 |
Result := ExecPrim(AOffset, False, False, AOffset + 1) |
| 8497 |
else |
| 8498 |
Result := ExecPrim(AOffset, False, False, 0); |
| 8499 |
end; |
| 8500 |
|
| 8501 |
function TRegExpr.OldInvertCase(const Ch: REChar): REChar; |
| 8502 |
begin |
| 8503 |
Result := _UpperCase(Ch); |
| 8504 |
if Result = Ch then |
| 8505 |
Result := _LowerCase(Ch); |
| 8506 |
end; |
| 8507 |
|
| 8508 |
class function TRegExpr.InvertCaseFunction(const Ch: REChar): REChar; |
| 8509 |
begin |
| 8510 |
Result := _UpperCase(Ch); |
| 8511 |
if Result = Ch then |
| 8512 |
Result := _LowerCase(Ch); |
| 8513 |
end; |
| 8514 |
|
| 8515 |
function TRegExpr.GetLinePairedSeparator: RegExprString; |
| 8516 |
begin |
| 8517 |
// not supported anymore |
| 8518 |
Result := ''; |
| 8519 |
end; |
| 8520 |
|
| 8521 |
procedure TRegExpr.SetLinePairedSeparator(const AValue: RegExprString); |
| 8522 |
begin |
| 8523 |
// not supported anymore |
| 8524 |
end; |
| 8525 |
|
| 8526 |
procedure TRegExpr.SetUseOsLineEndOnReplace(AValue: Boolean); |
| 8527 |
begin |
| 8528 |
if fUseOsLineEndOnReplace = AValue then |
| 8529 |
Exit; |
| 8530 |
fUseOsLineEndOnReplace := AValue; |
| 8531 |
if fUseOsLineEndOnReplace then |
| 8532 |
fReplaceLineEnd := sLineBreak |
| 8533 |
else |
| 8534 |
fReplaceLineEnd := #10; |
| 8535 |
end; |
| 8536 |
{$ENDIF} |
| 8537 |
|
| 8538 |
end. |