| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.regexp; |
| 8 |
|
| 9 |
import java.io.Serializable; |
| 10 |
|
| 11 |
import org.mozilla.javascript.Context; |
| 12 |
import org.mozilla.javascript.Function; |
| 13 |
import org.mozilla.javascript.IdFunctionObject; |
| 14 |
import org.mozilla.javascript.IdScriptableObject; |
| 15 |
import org.mozilla.javascript.Kit; |
| 16 |
import org.mozilla.javascript.ScriptRuntime; |
| 17 |
import org.mozilla.javascript.Scriptable; |
| 18 |
import org.mozilla.javascript.ScriptableObject; |
| 19 |
import org.mozilla.javascript.TopLevel; |
| 20 |
import org.mozilla.javascript.Undefined; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class NativeRegExp extends IdScriptableObject implements Function |
| 39 |
{ |
| 40 |
static final long serialVersionUID = 4965263491464903264L; |
| 41 |
|
| 42 |
private static final Object REGEXP_TAG = new Object(); |
| 43 |
|
| 44 |
public static final int JSREG_GLOB = 0x1; |
| 45 |
public static final int JSREG_FOLD = 0x2; |
| 46 |
public static final int JSREG_MULTILINE = 0x4; |
| 47 |
|
| 48 |
|
| 49 |
public static final int TEST = 0; |
| 50 |
public static final int MATCH = 1; |
| 51 |
public static final int PREFIX = 2; |
| 52 |
|
| 53 |
private static final boolean debug = false; |
| 54 |
|
| 55 |
private static final byte REOP_SIMPLE_START = 1; |
| 56 |
private static final byte REOP_EMPTY = 1; |
| 57 |
private static final byte REOP_BOL = 2; |
| 58 |
private static final byte REOP_EOL = 3; |
| 59 |
private static final byte REOP_WBDRY = 4; |
| 60 |
private static final byte REOP_WNONBDRY = 5; |
| 61 |
private static final byte REOP_DOT = 6; |
| 62 |
private static final byte REOP_DIGIT = 7; |
| 63 |
private static final byte REOP_NONDIGIT = 8; |
| 64 |
private static final byte REOP_ALNUM = 9; |
| 65 |
private static final byte REOP_NONALNUM = 10; |
| 66 |
private static final byte REOP_SPACE = 11; |
| 67 |
private static final byte REOP_NONSPACE = 12; |
| 68 |
private static final byte REOP_BACKREF = 13; |
| 69 |
private static final byte REOP_FLAT = 14; |
| 70 |
private static final byte REOP_FLAT1 = 15; |
| 71 |
private static final byte REOP_FLATi = 16; |
| 72 |
private static final byte REOP_FLAT1i = 17; |
| 73 |
private static final byte REOP_UCFLAT1 = 18; |
| 74 |
private static final byte REOP_UCFLAT1i = 19; |
| 75 |
|
| 76 |
|
| 77 |
private static final byte REOP_CLASS = 22; |
| 78 |
private static final byte REOP_NCLASS = 23; |
| 79 |
private static final byte REOP_SIMPLE_END = 23; |
| 80 |
private static final byte REOP_QUANT = 25; |
| 81 |
private static final byte REOP_STAR = 26; |
| 82 |
private static final byte REOP_PLUS = 27; |
| 83 |
private static final byte REOP_OPT = 28; |
| 84 |
private static final byte REOP_LPAREN = 29; |
| 85 |
private static final byte REOP_RPAREN = 30; |
| 86 |
private static final byte REOP_ALT = 31; |
| 87 |
private static final byte REOP_JUMP = 32; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private static final byte REOP_ASSERT = 41; |
| 94 |
private static final byte REOP_ASSERT_NOT = 42; |
| 95 |
private static final byte REOP_ASSERTTEST = 43; |
| 96 |
private static final byte REOP_ASSERTNOTTEST = 44; |
| 97 |
private static final byte REOP_MINIMALSTAR = 45; |
| 98 |
private static final byte REOP_MINIMALPLUS = 46; |
| 99 |
private static final byte REOP_MINIMALOPT = 47; |
| 100 |
private static final byte REOP_MINIMALQUANT = 48; |
| 101 |
private static final byte REOP_ENDCHILD = 49; |
| 102 |
private static final byte REOP_REPEAT = 51; |
| 103 |
private static final byte REOP_MINIMALREPEAT = 52; |
| 104 |
private static final byte REOP_ALTPREREQ = 53; |
| 105 |
private static final byte REOP_ALTPREREQi = 54; |
| 106 |
private static final byte REOP_ALTPREREQ2 = 55; |
| 107 |
|
| 108 |
private static final byte REOP_END = 57; |
| 109 |
|
| 110 |
private static final int ANCHOR_BOL = -2; |
| 111 |
|
| 112 |
|
| 113 |
public static void init(Context cx, Scriptable scope, boolean sealed) |
| 114 |
{ |
| 115 |
|
| 116 |
NativeRegExp proto = new NativeRegExp(); |
| 117 |
proto.re = compileRE(cx, "", null, false); |
| 118 |
proto.activatePrototypeMap(MAX_PROTOTYPE_ID); |
| 119 |
proto.setParentScope(scope); |
| 120 |
proto.setPrototype(getObjectPrototype(scope)); |
| 121 |
|
| 122 |
NativeRegExpCtor ctor = new NativeRegExpCtor(); |
| 123 |
|
| 124 |
|
| 125 |
proto.defineProperty("constructor", ctor, ScriptableObject.DONTENUM); |
| 126 |
|
| 127 |
ScriptRuntime.setFunctionProtoAndParent(ctor, scope); |
| 128 |
|
| 129 |
ctor.setImmunePrototypeProperty(proto); |
| 130 |
|
| 131 |
if (sealed) { |
| 132 |
proto.sealObject(); |
| 133 |
ctor.sealObject(); |
| 134 |
} |
| 135 |
|
| 136 |
defineProperty(scope, "RegExp", ctor, ScriptableObject.DONTENUM); |
| 137 |
} |
| 138 |
|
| 139 |
NativeRegExp(Scriptable scope, RECompiled regexpCompiled) |
| 140 |
{ |
| 141 |
this.re = regexpCompiled; |
| 142 |
this.lastIndex = 0d; |
| 143 |
ScriptRuntime.setBuiltinProtoAndParent(this, scope, TopLevel.Builtins.RegExp); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public String getClassName() |
| 148 |
{ |
| 149 |
return "RegExp"; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@Override |
| 158 |
public String getTypeOf() |
| 159 |
{ |
| 160 |
return "object"; |
| 161 |
} |
| 162 |
|
| 163 |
public Object call(Context cx, Scriptable scope, Scriptable thisObj, |
| 164 |
Object[] args) |
| 165 |
{ |
| 166 |
return execSub(cx, scope, args, MATCH); |
| 167 |
} |
| 168 |
|
| 169 |
public Scriptable construct(Context cx, Scriptable scope, Object[] args) |
| 170 |
{ |
| 171 |
return (Scriptable)execSub(cx, scope, args, MATCH); |
| 172 |
} |
| 173 |
|
| 174 |
Scriptable compile(Context cx, Scriptable scope, Object[] args) |
| 175 |
{ |
| 176 |
if (args.length > 0 && args[0] instanceof NativeRegExp) { |
| 177 |
if (args.length > 1 && args[1] != Undefined.instance) { |
| 178 |
|
| 179 |
throw ScriptRuntime.typeError0("msg.bad.regexp.compile"); |
| 180 |
} |
| 181 |
NativeRegExp thatObj = (NativeRegExp) args[0]; |
| 182 |
this.re = thatObj.re; |
| 183 |
this.lastIndex = thatObj.lastIndex; |
| 184 |
return this; |
| 185 |
} |
| 186 |
String s = args.length == 0 || args[0] instanceof Undefined ? "" : escapeRegExp(args[0]); |
| 187 |
String global = args.length > 1 && args[1] != Undefined.instance |
| 188 |
? ScriptRuntime.toString(args[1]) |
| 189 |
: null; |
| 190 |
this.re = compileRE(cx, s, global, false); |
| 191 |
this.lastIndex = 0d; |
| 192 |
return this; |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public String toString() |
| 197 |
{ |
| 198 |
StringBuilder buf = new StringBuilder(); |
| 199 |
buf.append('/'); |
| 200 |
if (re.source.length != 0) { |
| 201 |
buf.append(re.source); |
| 202 |
} else { |
| 203 |
|
| 204 |
buf.append("(?:)"); |
| 205 |
} |
| 206 |
buf.append('/'); |
| 207 |
if ((re.flags & JSREG_GLOB) != 0) |
| 208 |
buf.append('g'); |
| 209 |
if ((re.flags & JSREG_FOLD) != 0) |
| 210 |
buf.append('i'); |
| 211 |
if ((re.flags & JSREG_MULTILINE) != 0) |
| 212 |
buf.append('m'); |
| 213 |
return buf.toString(); |
| 214 |
} |
| 215 |
|
| 216 |
NativeRegExp() { } |
| 217 |
|
| 218 |
private static RegExpImpl getImpl(Context cx) |
| 219 |
{ |
| 220 |
return (RegExpImpl) ScriptRuntime.getRegExpProxy(cx); |
| 221 |
} |
| 222 |
|
| 223 |
private static String escapeRegExp(Object src) { |
| 224 |
String s = ScriptRuntime.toString(src); |
| 225 |
|
| 226 |
StringBuilder sb = null; |
| 227 |
int start = 0; |
| 228 |
int slash = s.indexOf('/'); |
| 229 |
while (slash > -1) { |
| 230 |
if (slash == start || s.charAt(slash - 1) != '\\') { |
| 231 |
if (sb == null) { |
| 232 |
sb = new StringBuilder(); |
| 233 |
} |
| 234 |
sb.append(s, start, slash); |
| 235 |
sb.append("\\/"); |
| 236 |
start = slash + 1; |
| 237 |
} |
| 238 |
slash = s.indexOf('/', slash + 1); |
| 239 |
} |
| 240 |
if (sb != null) { |
| 241 |
sb.append(s, start, s.length()); |
| 242 |
s = sb.toString(); |
| 243 |
} |
| 244 |
return s; |
| 245 |
} |
| 246 |
|
| 247 |
private Object execSub(Context cx, Scriptable scopeObj, |
| 248 |
Object[] args, int matchType) |
| 249 |
{ |
| 250 |
RegExpImpl reImpl = getImpl(cx); |
| 251 |
String str; |
| 252 |
if (args.length == 0) { |
| 253 |
str = reImpl.input; |
| 254 |
if (str == null) { |
| 255 |
str = ScriptRuntime.toString(Undefined.instance); |
| 256 |
} |
| 257 |
} else { |
| 258 |
str = ScriptRuntime.toString(args[0]); |
| 259 |
} |
| 260 |
double d = 0; |
| 261 |
if ((re.flags & JSREG_GLOB) != 0) { |
| 262 |
d = ScriptRuntime.toInteger(lastIndex); |
| 263 |
} |
| 264 |
|
| 265 |
Object rval; |
| 266 |
if (d < 0 || str.length() < d) { |
| 267 |
lastIndex = 0d; |
| 268 |
rval = null; |
| 269 |
} |
| 270 |
else { |
| 271 |
int indexp[] = { (int)d }; |
| 272 |
rval = executeRegExp(cx, scopeObj, reImpl, str, indexp, matchType); |
| 273 |
if ((re.flags & JSREG_GLOB) != 0) { |
| 274 |
lastIndex = (rval == null || rval == Undefined.instance) |
| 275 |
? 0d : (double)indexp[0]; |
| 276 |
} |
| 277 |
} |
| 278 |
return rval; |
| 279 |
} |
| 280 |
|
| 281 |
static RECompiled compileRE(Context cx, String str, String global, boolean flat) |
| 282 |
{ |
| 283 |
RECompiled regexp = new RECompiled(str); |
| 284 |
int length = str.length(); |
| 285 |
int flags = 0; |
| 286 |
if (global != null) { |
| 287 |
for (int i = 0; i < global.length(); i++) { |
| 288 |
char c = global.charAt(i); |
| 289 |
int f = 0; |
| 290 |
if (c == 'g') { |
| 291 |
f = JSREG_GLOB; |
| 292 |
} else if (c == 'i') { |
| 293 |
f = JSREG_FOLD; |
| 294 |
} else if (c == 'm') { |
| 295 |
f = JSREG_MULTILINE; |
| 296 |
} else { |
| 297 |
reportError("msg.invalid.re.flag", String.valueOf(c)); |
| 298 |
} |
| 299 |
if ((flags & f) != 0) { |
| 300 |
reportError("msg.invalid.re.flag", String.valueOf(c)); |
| 301 |
} |
| 302 |
flags |= f; |
| 303 |
} |
| 304 |
} |
| 305 |
regexp.flags = flags; |
| 306 |
|
| 307 |
CompilerState state = new CompilerState(cx, regexp.source, length, flags); |
| 308 |
if (flat && length > 0) { |
| 309 |
if (debug) { |
| 310 |
System.out.println("flat = \"" + str + "\""); |
| 311 |
} |
| 312 |
state.result = new RENode(REOP_FLAT); |
| 313 |
state.result.chr = state.cpbegin[0]; |
| 314 |
state.result.length = length; |
| 315 |
state.result.flatIndex = 0; |
| 316 |
state.progLength += 5; |
| 317 |
} else { |
| 318 |
if (!parseDisjunction(state)) |
| 319 |
return null; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
if (state.maxBackReference > state.parenCount) { |
| 324 |
state = new CompilerState(cx, regexp.source, length, flags); |
| 325 |
state.backReferenceLimit = state.parenCount; |
| 326 |
if (!parseDisjunction(state)) |
| 327 |
return null; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
regexp.program = new byte[state.progLength + 1]; |
| 332 |
if (state.classCount != 0) { |
| 333 |
regexp.classList = new RECharSet[state.classCount]; |
| 334 |
regexp.classCount = state.classCount; |
| 335 |
} |
| 336 |
int endPC = emitREBytecode(state, regexp, 0, state.result); |
| 337 |
regexp.program[endPC++] = REOP_END; |
| 338 |
|
| 339 |
if (debug) { |
| 340 |
System.out.println("Prog. length = " + endPC); |
| 341 |
for (int i = 0; i < endPC; i++) { |
| 342 |
System.out.print(regexp.program[i]); |
| 343 |
if (i < (endPC - 1)) System.out.print(", "); |
| 344 |
} |
| 345 |
System.out.println(); |
| 346 |
} |
| 347 |
regexp.parenCount = state.parenCount; |
| 348 |
|
| 349 |
|
| 350 |
switch (regexp.program[0]) { |
| 351 |
case REOP_UCFLAT1: |
| 352 |
case REOP_UCFLAT1i: |
| 353 |
regexp.anchorCh = (char)getIndex(regexp.program, 1); |
| 354 |
break; |
| 355 |
case REOP_FLAT1: |
| 356 |
case REOP_FLAT1i: |
| 357 |
regexp.anchorCh = (char)(regexp.program[1] & 0xFF); |
| 358 |
break; |
| 359 |
case REOP_FLAT: |
| 360 |
case REOP_FLATi: |
| 361 |
int k = getIndex(regexp.program, 1); |
| 362 |
regexp.anchorCh = regexp.source[k]; |
| 363 |
break; |
| 364 |
case REOP_BOL: |
| 365 |
regexp.anchorCh = ANCHOR_BOL; |
| 366 |
break; |
| 367 |
case REOP_ALT: |
| 368 |
RENode n = state.result; |
| 369 |
if (n.kid.op == REOP_BOL && n.kid2.op == REOP_BOL) { |
| 370 |
regexp.anchorCh = ANCHOR_BOL; |
| 371 |
} |
| 372 |
break; |
| 373 |
} |
| 374 |
|
| 375 |
if (debug) { |
| 376 |
if (regexp.anchorCh >= 0) { |
| 377 |
System.out.println("Anchor ch = '" + (char)regexp.anchorCh + "'"); |
| 378 |
} |
| 379 |
} |
| 380 |
return regexp; |
| 381 |
} |
| 382 |
|
| 383 |
static boolean isDigit(char c) |
| 384 |
{ |
| 385 |
return '0' <= c && c <= '9'; |
| 386 |
} |
| 387 |
|
| 388 |
private static boolean isWord(char c) |
| 389 |
{ |
| 390 |
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || isDigit(c) || c == '_'; |
| 391 |
} |
| 392 |
|
| 393 |
private static boolean isControlLetter(char c) |
| 394 |
{ |
| 395 |
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); |
| 396 |
} |
| 397 |
|
| 398 |
private static boolean isLineTerm(char c) |
| 399 |
{ |
| 400 |
return ScriptRuntime.isJSLineTerminator(c); |
| 401 |
} |
| 402 |
|
| 403 |
private static boolean isREWhiteSpace(int c) |
| 404 |
{ |
| 405 |
return ScriptRuntime.isJSWhitespaceOrLineTerminator(c); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
private static char upcase(char ch) |
| 420 |
{ |
| 421 |
if (ch < 128) { |
| 422 |
if ('a' <= ch && ch <= 'z') { |
| 423 |
return (char)(ch + ('A' - 'a')); |
| 424 |
} |
| 425 |
return ch; |
| 426 |
} |
| 427 |
char cu = Character.toUpperCase(ch); |
| 428 |
return (cu < 128) ? ch : cu; |
| 429 |
} |
| 430 |
|
| 431 |
private static char downcase(char ch) |
| 432 |
{ |
| 433 |
if (ch < 128) { |
| 434 |
if ('A' <= ch && ch <= 'Z') { |
| 435 |
return (char)(ch + ('a' - 'A')); |
| 436 |
} |
| 437 |
return ch; |
| 438 |
} |
| 439 |
char cl = Character.toLowerCase(ch); |
| 440 |
return (cl < 128) ? ch : cl; |
| 441 |
|
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
private static int toASCIIHexDigit(int c) |
| 448 |
{ |
| 449 |
if (c < '0') |
| 450 |
return -1; |
| 451 |
if (c <= '9') { |
| 452 |
return c - '0'; |
| 453 |
} |
| 454 |
c |= 0x20; |
| 455 |
if ('a' <= c && c <= 'f') { |
| 456 |
return c - 'a' + 10; |
| 457 |
} |
| 458 |
return -1; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
private static boolean parseDisjunction(CompilerState state) |
| 468 |
{ |
| 469 |
if (!parseAlternative(state)) |
| 470 |
return false; |
| 471 |
char[] source = state.cpbegin; |
| 472 |
int index = state.cp; |
| 473 |
if (index != source.length && source[index] == '|') { |
| 474 |
RENode result; |
| 475 |
++state.cp; |
| 476 |
result = new RENode(REOP_ALT); |
| 477 |
result.kid = state.result; |
| 478 |
if (!parseDisjunction(state)) |
| 479 |
return false; |
| 480 |
result.kid2 = state.result; |
| 481 |
state.result = result; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
if (result.kid.op == REOP_FLAT && result.kid2.op == REOP_FLAT) { |
| 487 |
result.op = (state.flags & JSREG_FOLD) == 0 ? |
| 488 |
REOP_ALTPREREQ : REOP_ALTPREREQi; |
| 489 |
result.chr = result.kid.chr; |
| 490 |
result.index = result.kid2.chr; |
| 491 |
|
| 492 |
|
| 493 |
state.progLength += 13; |
| 494 |
} else if (result.kid.op == REOP_CLASS && result.kid.index < 256 |
| 495 |
&& result.kid2.op == REOP_FLAT && (state.flags & JSREG_FOLD) == 0) { |
| 496 |
result.op = REOP_ALTPREREQ2; |
| 497 |
result.chr = result.kid2.chr; |
| 498 |
result.index = result.kid.index; |
| 499 |
|
| 500 |
|
| 501 |
state.progLength += 13; |
| 502 |
} else if (result.kid.op == REOP_FLAT && result.kid2.op == REOP_CLASS |
| 503 |
&& result.kid2.index < 256 && (state.flags & JSREG_FOLD) == 0) { |
| 504 |
result.op = REOP_ALTPREREQ2; |
| 505 |
result.chr = result.kid.chr; |
| 506 |
result.index = result.kid2.index; |
| 507 |
|
| 508 |
|
| 509 |
state.progLength += 13; |
| 510 |
} else { |
| 511 |
|
| 512 |
state.progLength += 9; |
| 513 |
} |
| 514 |
} |
| 515 |
return true; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
private static boolean parseAlternative(CompilerState state) |
| 523 |
{ |
| 524 |
RENode headTerm = null; |
| 525 |
RENode tailTerm = null; |
| 526 |
char[] source = state.cpbegin; |
| 527 |
while (true) { |
| 528 |
if (state.cp == state.cpend || source[state.cp] == '|' |
| 529 |
|| (state.parenNesting != 0 && source[state.cp] == ')')) |
| 530 |
{ |
| 531 |
if (headTerm == null) { |
| 532 |
state.result = new RENode(REOP_EMPTY); |
| 533 |
} |
| 534 |
else |
| 535 |
state.result = headTerm; |
| 536 |
return true; |
| 537 |
} |
| 538 |
if (!parseTerm(state)) |
| 539 |
return false; |
| 540 |
if (headTerm == null) { |
| 541 |
headTerm = state.result; |
| 542 |
tailTerm = headTerm; |
| 543 |
} |
| 544 |
else |
| 545 |
tailTerm.next = state.result; |
| 546 |
while (tailTerm.next != null) tailTerm = tailTerm.next; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
private static boolean |
| 552 |
calculateBitmapSize(CompilerState state, RENode target, char[] src, |
| 553 |
int index, int end) |
| 554 |
{ |
| 555 |
char rangeStart = 0; |
| 556 |
char c; |
| 557 |
int n; |
| 558 |
int nDigits; |
| 559 |
int i; |
| 560 |
int max = 0; |
| 561 |
boolean inRange = false; |
| 562 |
|
| 563 |
target.bmsize = 0; |
| 564 |
target.sense = true; |
| 565 |
|
| 566 |
if (index == end) |
| 567 |
return true; |
| 568 |
|
| 569 |
if (src[index] == '^') { |
| 570 |
++index; |
| 571 |
target.sense = false; |
| 572 |
} |
| 573 |
|
| 574 |
while (index != end) { |
| 575 |
int localMax = 0; |
| 576 |
nDigits = 2; |
| 577 |
switch (src[index]) { |
| 578 |
case '\\': |
| 579 |
++index; |
| 580 |
c = src[index++]; |
| 581 |
switch (c) { |
| 582 |
case 'b': |
| 583 |
localMax = 0x8; |
| 584 |
break; |
| 585 |
case 'f': |
| 586 |
localMax = 0xC; |
| 587 |
break; |
| 588 |
case 'n': |
| 589 |
localMax = 0xA; |
| 590 |
break; |
| 591 |
case 'r': |
| 592 |
localMax = 0xD; |
| 593 |
break; |
| 594 |
case 't': |
| 595 |
localMax = 0x9; |
| 596 |
break; |
| 597 |
case 'v': |
| 598 |
localMax = 0xB; |
| 599 |
break; |
| 600 |
case 'c': |
| 601 |
if ((index < end) && isControlLetter(src[index])) |
| 602 |
localMax = (char)(src[index++] & 0x1F); |
| 603 |
else |
| 604 |
--index; |
| 605 |
localMax = '\\'; |
| 606 |
break; |
| 607 |
case 'u': |
| 608 |
nDigits += 2; |
| 609 |
|
| 610 |
case 'x': |
| 611 |
n = 0; |
| 612 |
for (i = 0; (i < nDigits) && (index < end); i++) { |
| 613 |
c = src[index++]; |
| 614 |
n = Kit.xDigitToInt(c, n); |
| 615 |
if (n < 0) { |
| 616 |
|
| 617 |
|
| 618 |
index -= (i + 1); |
| 619 |
n = '\\'; |
| 620 |
break; |
| 621 |
} |
| 622 |
} |
| 623 |
localMax = n; |
| 624 |
break; |
| 625 |
case 'd': |
| 626 |
if (inRange) { |
| 627 |
reportError("msg.bad.range", ""); |
| 628 |
return false; |
| 629 |
} |
| 630 |
localMax = '9'; |
| 631 |
break; |
| 632 |
case 'D': |
| 633 |
case 's': |
| 634 |
case 'S': |
| 635 |
case 'w': |
| 636 |
case 'W': |
| 637 |
if (inRange) { |
| 638 |
reportError("msg.bad.range", ""); |
| 639 |
return false; |
| 640 |
} |
| 641 |
target.bmsize = 65536; |
| 642 |
return true; |
| 643 |
case '0': |
| 644 |
case '1': |
| 645 |
case '2': |
| 646 |
case '3': |
| 647 |
case '4': |
| 648 |
case '5': |
| 649 |
case '6': |
| 650 |
case '7': |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
n = (c - '0'); |
| 658 |
c = src[index]; |
| 659 |
if ('0' <= c && c <= '7') { |
| 660 |
index++; |
| 661 |
n = 8 * n + (c - '0'); |
| 662 |
c = src[index]; |
| 663 |
if ('0' <= c && c <= '7') { |
| 664 |
index++; |
| 665 |
i = 8 * n + (c - '0'); |
| 666 |
if (i <= 0377) |
| 667 |
n = i; |
| 668 |
else |
| 669 |
index--; |
| 670 |
} |
| 671 |
} |
| 672 |
localMax = n; |
| 673 |
break; |
| 674 |
|
| 675 |
default: |
| 676 |
localMax = c; |
| 677 |
break; |
| 678 |
} |
| 679 |
break; |
| 680 |
default: |
| 681 |
localMax = src[index++]; |
| 682 |
break; |
| 683 |
} |
| 684 |
if (inRange) { |
| 685 |
if (rangeStart > localMax) { |
| 686 |
reportError("msg.bad.range", ""); |
| 687 |
return false; |
| 688 |
} |
| 689 |
inRange = false; |
| 690 |
} |
| 691 |
else { |
| 692 |
if (index < (end - 1)) { |
| 693 |
if (src[index] == '-') { |
| 694 |
++index; |
| 695 |
inRange = true; |
| 696 |
rangeStart = (char)localMax; |
| 697 |
continue; |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
if ((state.flags & JSREG_FOLD) != 0){ |
| 702 |
char cu = upcase((char)localMax); |
| 703 |
char cd = downcase((char)localMax); |
| 704 |
localMax = (cu >= cd) ? cu : cd; |
| 705 |
} |
| 706 |
if (localMax > max) |
| 707 |
max = localMax; |
| 708 |
} |
| 709 |
target.bmsize = max + 1; |
| 710 |
return true; |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
private static void doFlat(CompilerState state, char c) |
| 768 |
{ |
| 769 |
state.result = new RENode(REOP_FLAT); |
| 770 |
state.result.chr = c; |
| 771 |
state.result.length = 1; |
| 772 |
state.result.flatIndex = -1; |
| 773 |
state.progLength += 3; |
| 774 |
} |
| 775 |
|
| 776 |
private static int |
| 777 |
getDecimalValue(char c, CompilerState state, int maxValue, |
| 778 |
String overflowMessageId) |
| 779 |
{ |
| 780 |
boolean overflow = false; |
| 781 |
int start = state.cp; |
| 782 |
char[] src = state.cpbegin; |
| 783 |
int value = c - '0'; |
| 784 |
for (; state.cp != state.cpend; ++state.cp) { |
| 785 |
c = src[state.cp]; |
| 786 |
if (!isDigit(c)) { |
| 787 |
break; |
| 788 |
} |
| 789 |
if (!overflow) { |
| 790 |
int v = value * 10 + (c - '0'); |
| 791 |
if (v < maxValue) { |
| 792 |
value = v; |
| 793 |
} else { |
| 794 |
overflow = true; |
| 795 |
value = maxValue; |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
if (overflow) { |
| 800 |
reportError(overflowMessageId, |
| 801 |
String.valueOf(src, start, state.cp - start)); |
| 802 |
} |
| 803 |
return value; |
| 804 |
} |
| 805 |
|
| 806 |
private static boolean |
| 807 |
parseTerm(CompilerState state) |
| 808 |
{ |
| 809 |
char[] src = state.cpbegin; |
| 810 |
char c = src[state.cp++]; |
| 811 |
int nDigits = 2; |
| 812 |
int parenBaseCount = state.parenCount; |
| 813 |
int num; |
| 814 |
RENode term; |
| 815 |
int termStart; |
| 816 |
|
| 817 |
switch (c) { |
| 818 |
|
| 819 |
case '^': |
| 820 |
state.result = new RENode(REOP_BOL); |
| 821 |
state.progLength++; |
| 822 |
return true; |
| 823 |
case '$': |
| 824 |
state.result = new RENode(REOP_EOL); |
| 825 |
state.progLength++; |
| 826 |
return true; |
| 827 |
case '\\': |
| 828 |
if (state.cp < state.cpend) { |
| 829 |
c = src[state.cp++]; |
| 830 |
switch (c) { |
| 831 |
|
| 832 |
case 'b' : |
| 833 |
state.result = new RENode(REOP_WBDRY); |
| 834 |
state.progLength++; |
| 835 |
return true; |
| 836 |
case 'B': |
| 837 |
state.result = new RENode(REOP_WNONBDRY); |
| 838 |
state.progLength++; |
| 839 |
return true; |
| 840 |
|
| 841 |
case '0': |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
reportWarning(state.cx, "msg.bad.backref", ""); |
| 849 |
|
| 850 |
num = 0; |
| 851 |
|
| 852 |
|
| 853 |
while (num < 040 && state.cp < state.cpend) { |
| 854 |
c = src[state.cp]; |
| 855 |
if ((c >= '0') && (c <= '7')) { |
| 856 |
state.cp++; |
| 857 |
num = 8 * num + (c - '0'); |
| 858 |
} |
| 859 |
else |
| 860 |
break; |
| 861 |
} |
| 862 |
c = (char)(num); |
| 863 |
doFlat(state, c); |
| 864 |
break; |
| 865 |
case '1': |
| 866 |
case '2': |
| 867 |
case '3': |
| 868 |
case '4': |
| 869 |
case '5': |
| 870 |
case '6': |
| 871 |
case '7': |
| 872 |
case '8': |
| 873 |
case '9': |
| 874 |
termStart = state.cp - 1; |
| 875 |
num = getDecimalValue(c, state, 0xFFFF, |
| 876 |
"msg.overlarge.backref"); |
| 877 |
if (num > state.backReferenceLimit) |
| 878 |
reportWarning(state.cx, "msg.bad.backref", ""); |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
if (num > state.backReferenceLimit) { |
| 884 |
state.cp = termStart; |
| 885 |
if (c >= '8') { |
| 886 |
|
| 887 |
|
| 888 |
c = '\\'; |
| 889 |
doFlat(state, c); |
| 890 |
break; |
| 891 |
} |
| 892 |
state.cp++; |
| 893 |
num = c - '0'; |
| 894 |
while (num < 040 && state.cp < state.cpend) { |
| 895 |
c = src[state.cp]; |
| 896 |
if ((c >= '0') && (c <= '7')) { |
| 897 |
state.cp++; |
| 898 |
num = 8 * num + (c - '0'); |
| 899 |
} |
| 900 |
else |
| 901 |
break; |
| 902 |
} |
| 903 |
c = (char)(num); |
| 904 |
doFlat(state, c); |
| 905 |
break; |
| 906 |
} |
| 907 |
|
| 908 |
state.result = new RENode(REOP_BACKREF); |
| 909 |
state.result.parenIndex = num - 1; |
| 910 |
state.progLength += 3; |
| 911 |
if (state.maxBackReference < num) { |
| 912 |
state.maxBackReference = num; |
| 913 |
} |
| 914 |
break; |
| 915 |
|
| 916 |
case 'f': |
| 917 |
c = 0xC; |
| 918 |
doFlat(state, c); |
| 919 |
break; |
| 920 |
case 'n': |
| 921 |
c = 0xA; |
| 922 |
doFlat(state, c); |
| 923 |
break; |
| 924 |
case 'r': |
| 925 |
c = 0xD; |
| 926 |
doFlat(state, c); |
| 927 |
break; |
| 928 |
case 't': |
| 929 |
c = 0x9; |
| 930 |
doFlat(state, c); |
| 931 |
break; |
| 932 |
case 'v': |
| 933 |
c = 0xB; |
| 934 |
doFlat(state, c); |
| 935 |
break; |
| 936 |
|
| 937 |
case 'c': |
| 938 |
if ((state.cp < state.cpend) && |
| 939 |
isControlLetter(src[state.cp])) |
| 940 |
c = (char)(src[state.cp++] & 0x1F); |
| 941 |
else { |
| 942 |
|
| 943 |
--state.cp; |
| 944 |
c = '\\'; |
| 945 |
} |
| 946 |
doFlat(state, c); |
| 947 |
break; |
| 948 |
|
| 949 |
case 'u': |
| 950 |
nDigits += 2; |
| 951 |
case 'x': |
| 952 |
{ |
| 953 |
int n = 0; |
| 954 |
int i; |
| 955 |
for (i = 0; (i < nDigits) |
| 956 |
&& (state.cp < state.cpend); i++) { |
| 957 |
c = src[state.cp++]; |
| 958 |
n = Kit.xDigitToInt(c, n); |
| 959 |
if (n < 0) { |
| 960 |
|
| 961 |
|
| 962 |
state.cp -= (i + 2); |
| 963 |
n = src[state.cp++]; |
| 964 |
break; |
| 965 |
} |
| 966 |
} |
| 967 |
c = (char)(n); |
| 968 |
} |
| 969 |
doFlat(state, c); |
| 970 |
break; |
| 971 |
|
| 972 |
case 'd': |
| 973 |
state.result = new RENode(REOP_DIGIT); |
| 974 |
state.progLength++; |
| 975 |
break; |
| 976 |
case 'D': |
| 977 |
state.result = new RENode(REOP_NONDIGIT); |
| 978 |
state.progLength++; |
| 979 |
break; |
| 980 |
case 's': |
| 981 |
state.result = new RENode(REOP_SPACE); |
| 982 |
state.progLength++; |
| 983 |
break; |
| 984 |
case 'S': |
| 985 |
state.result = new RENode(REOP_NONSPACE); |
| 986 |
state.progLength++; |
| 987 |
break; |
| 988 |
case 'w': |
| 989 |
state.result = new RENode(REOP_ALNUM); |
| 990 |
state.progLength++; |
| 991 |
break; |
| 992 |
case 'W': |
| 993 |
state.result = new RENode(REOP_NONALNUM); |
| 994 |
state.progLength++; |
| 995 |
break; |
| 996 |
|
| 997 |
default: |
| 998 |
state.result = new RENode(REOP_FLAT); |
| 999 |
state.result.chr = c; |
| 1000 |
state.result.length = 1; |
| 1001 |
state.result.flatIndex = state.cp - 1; |
| 1002 |
state.progLength += 3; |
| 1003 |
break; |
| 1004 |
} |
| 1005 |
break; |
| 1006 |
} |
| 1007 |
else { |
| 1008 |
|
| 1009 |
reportError("msg.trail.backslash", ""); |
| 1010 |
return false; |
| 1011 |
} |
| 1012 |
case '(': { |
| 1013 |
RENode result = null; |
| 1014 |
termStart = state.cp; |
| 1015 |
if (state.cp + 1 < state.cpend && src[state.cp] == '?' |
| 1016 |
&& ((c = src[state.cp + 1]) == '=' || c == '!' || c == ':')) |
| 1017 |
{ |
| 1018 |
state.cp += 2; |
| 1019 |
if (c == '=') { |
| 1020 |
result = new RENode(REOP_ASSERT); |
| 1021 |
|
| 1022 |
state.progLength += 4; |
| 1023 |
} else if (c == '!') { |
| 1024 |
result = new RENode(REOP_ASSERT_NOT); |
| 1025 |
|
| 1026 |
state.progLength += 4; |
| 1027 |
} |
| 1028 |
} else { |
| 1029 |
result = new RENode(REOP_LPAREN); |
| 1030 |
|
| 1031 |
state.progLength += 6; |
| 1032 |
result.parenIndex = state.parenCount++; |
| 1033 |
} |
| 1034 |
++state.parenNesting; |
| 1035 |
if (!parseDisjunction(state)) |
| 1036 |
return false; |
| 1037 |
if (state.cp == state.cpend || src[state.cp] != ')') { |
| 1038 |
reportError("msg.unterm.paren", ""); |
| 1039 |
return false; |
| 1040 |
} |
| 1041 |
++state.cp; |
| 1042 |
--state.parenNesting; |
| 1043 |
if (result != null) { |
| 1044 |
result.kid = state.result; |
| 1045 |
state.result = result; |
| 1046 |
} |
| 1047 |
break; |
| 1048 |
} |
| 1049 |
case ')': |
| 1050 |
reportError("msg.re.unmatched.right.paren", ""); |
| 1051 |
return false; |
| 1052 |
case '[': |
| 1053 |
state.result = new RENode(REOP_CLASS); |
| 1054 |
termStart = state.cp; |
| 1055 |
state.result.startIndex = termStart; |
| 1056 |
while (true) { |
| 1057 |
if (state.cp == state.cpend) { |
| 1058 |
reportError("msg.unterm.class", ""); |
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
if (src[state.cp] == '\\') |
| 1062 |
state.cp++; |
| 1063 |
else { |
| 1064 |
if (src[state.cp] == ']') { |
| 1065 |
state.result.kidlen = state.cp - termStart; |
| 1066 |
break; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
state.cp++; |
| 1070 |
} |
| 1071 |
state.result.index = state.classCount++; |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
if (!calculateBitmapSize(state, state.result, src, termStart, state.cp++)) |
| 1077 |
return false; |
| 1078 |
state.progLength += 3; |
| 1079 |
break; |
| 1080 |
|
| 1081 |
case '.': |
| 1082 |
state.result = new RENode(REOP_DOT); |
| 1083 |
state.progLength++; |
| 1084 |
break; |
| 1085 |
case '*': |
| 1086 |
case '+': |
| 1087 |
case '?': |
| 1088 |
reportError("msg.bad.quant", String.valueOf(src[state.cp - 1])); |
| 1089 |
return false; |
| 1090 |
default: |
| 1091 |
state.result = new RENode(REOP_FLAT); |
| 1092 |
state.result.chr = c; |
| 1093 |
state.result.length = 1; |
| 1094 |
state.result.flatIndex = state.cp - 1; |
| 1095 |
state.progLength += 3; |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
|
| 1099 |
term = state.result; |
| 1100 |
if (state.cp == state.cpend) { |
| 1101 |
return true; |
| 1102 |
} |
| 1103 |
boolean hasQ = false; |
| 1104 |
switch (src[state.cp]) { |
| 1105 |
case '+': |
| 1106 |
state.result = new RENode(REOP_QUANT); |
| 1107 |
state.result.min = 1; |
| 1108 |
state.result.max = -1; |
| 1109 |
|
| 1110 |
state.progLength += 8; |
| 1111 |
hasQ = true; |
| 1112 |
break; |
| 1113 |
case '*': |
| 1114 |
state.result = new RENode(REOP_QUANT); |
| 1115 |
state.result.min = 0; |
| 1116 |
state.result.max = -1; |
| 1117 |
|
| 1118 |
state.progLength += 8; |
| 1119 |
hasQ = true; |
| 1120 |
break; |
| 1121 |
case '?': |
| 1122 |
state.result = new RENode(REOP_QUANT); |
| 1123 |
state.result.min = 0; |
| 1124 |
state.result.max = 1; |
| 1125 |
|
| 1126 |
state.progLength += 8; |
| 1127 |
hasQ = true; |
| 1128 |
break; |
| 1129 |
case '{': |
| 1130 |
{ |
| 1131 |
int min = 0; |
| 1132 |
int max = -1; |
| 1133 |
int leftCurl = state.cp; |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
if (++state.cp < src.length && isDigit(c = src[state.cp])) { |
| 1142 |
++state.cp; |
| 1143 |
min = getDecimalValue(c, state, 0xFFFF, |
| 1144 |
"msg.overlarge.min"); |
| 1145 |
c = src[state.cp]; |
| 1146 |
if (c == ',') { |
| 1147 |
c = src[++state.cp]; |
| 1148 |
if (isDigit(c)) { |
| 1149 |
++state.cp; |
| 1150 |
max = getDecimalValue(c, state, 0xFFFF, |
| 1151 |
"msg.overlarge.max"); |
| 1152 |
c = src[state.cp]; |
| 1153 |
if (min > max) { |
| 1154 |
reportError("msg.max.lt.min", |
| 1155 |
String.valueOf(src[state.cp])); |
| 1156 |
return false; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
} else { |
| 1160 |
max = min; |
| 1161 |
} |
| 1162 |
|
| 1163 |
if (c == '}') { |
| 1164 |
state.result = new RENode(REOP_QUANT); |
| 1165 |
state.result.min = min; |
| 1166 |
state.result.max = max; |
| 1167 |
|
| 1168 |
|
| 1169 |
state.progLength += 12; |
| 1170 |
hasQ = true; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
if (!hasQ) { |
| 1174 |
state.cp = leftCurl; |
| 1175 |
} |
| 1176 |
break; |
| 1177 |
} |
| 1178 |
} |
| 1179 |
if (!hasQ) |
| 1180 |
return true; |
| 1181 |
|
| 1182 |
++state.cp; |
| 1183 |
state.result.kid = term; |
| 1184 |
state.result.parenIndex = parenBaseCount; |
| 1185 |
state.result.parenCount = state.parenCount - parenBaseCount; |
| 1186 |
if ((state.cp < state.cpend) && (src[state.cp] == '?')) { |
| 1187 |
++state.cp; |
| 1188 |
state.result.greedy = false; |
| 1189 |
} |
| 1190 |
else |
| 1191 |
state.result.greedy = true; |
| 1192 |
return true; |
| 1193 |
} |
| 1194 |
|
| 1195 |
private static void resolveForwardJump(byte[] array, int from, int pc) |
| 1196 |
{ |
| 1197 |
if (from > pc) throw Kit.codeBug(); |
| 1198 |
addIndex(array, from, pc - from); |
| 1199 |
} |
| 1200 |
|
| 1201 |
private static int getOffset(byte[] array, int pc) |
| 1202 |
{ |
| 1203 |
return getIndex(array, pc); |
| 1204 |
} |
| 1205 |
|
| 1206 |
private static int addIndex(byte[] array, int pc, int index) |
| 1207 |
{ |
| 1208 |
if (index < 0) throw Kit.codeBug(); |
| 1209 |
if (index > 0xFFFF) |
| 1210 |
throw Context.reportRuntimeError("Too complex regexp"); |
| 1211 |
array[pc] = (byte)(index >> 8); |
| 1212 |
array[pc + 1] = (byte)(index); |
| 1213 |
return pc + 2; |
| 1214 |
} |
| 1215 |
|
| 1216 |
private static int getIndex(byte[] array, int pc) |
| 1217 |
{ |
| 1218 |
return ((array[pc] & 0xFF) << 8) | (array[pc + 1] & 0xFF); |
| 1219 |
} |
| 1220 |
|
| 1221 |
private static final int INDEX_LEN = 2; |
| 1222 |
|
| 1223 |
private static int |
| 1224 |
emitREBytecode(CompilerState state, RECompiled re, int pc, RENode t) |
| 1225 |
{ |
| 1226 |
RENode nextAlt; |
| 1227 |
int nextAltFixup, nextTermFixup; |
| 1228 |
byte[] program = re.program; |
| 1229 |
|
| 1230 |
while (t != null) { |
| 1231 |
program[pc++] = t.op; |
| 1232 |
switch (t.op) { |
| 1233 |
case REOP_EMPTY: |
| 1234 |
--pc; |
| 1235 |
break; |
| 1236 |
case REOP_ALTPREREQ: |
| 1237 |
case REOP_ALTPREREQi: |
| 1238 |
case REOP_ALTPREREQ2: |
| 1239 |
boolean ignoreCase = t.op == REOP_ALTPREREQi; |
| 1240 |
addIndex(program, pc, ignoreCase ? upcase(t.chr) : t.chr); |
| 1241 |
pc += INDEX_LEN; |
| 1242 |
addIndex(program, pc, ignoreCase ? upcase((char)t.index) : t.index); |
| 1243 |
pc += INDEX_LEN; |
| 1244 |
|
| 1245 |
case REOP_ALT: |
| 1246 |
nextAlt = t.kid2; |
| 1247 |
nextAltFixup = pc; |
| 1248 |
pc += INDEX_LEN; |
| 1249 |
pc = emitREBytecode(state, re, pc, t.kid); |
| 1250 |
program[pc++] = REOP_JUMP; |
| 1251 |
nextTermFixup = pc; |
| 1252 |
pc += INDEX_LEN; |
| 1253 |
resolveForwardJump(program, nextAltFixup, pc); |
| 1254 |
pc = emitREBytecode(state, re, pc, nextAlt); |
| 1255 |
|
| 1256 |
program[pc++] = REOP_JUMP; |
| 1257 |
nextAltFixup = pc; |
| 1258 |
pc += INDEX_LEN; |
| 1259 |
|
| 1260 |
resolveForwardJump(program, nextTermFixup, pc); |
| 1261 |
resolveForwardJump(program, nextAltFixup, pc); |
| 1262 |
break; |
| 1263 |
case REOP_FLAT: |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
if (t.flatIndex != -1) { |
| 1268 |
while ((t.next != null) && (t.next.op == REOP_FLAT) |
| 1269 |
&& ((t.flatIndex + t.length) |
| 1270 |
== t.next.flatIndex)) { |
| 1271 |
t.length += t.next.length; |
| 1272 |
t.next = t.next.next; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
if ((t.flatIndex != -1) && (t.length > 1)) { |
| 1276 |
if ((state.flags & JSREG_FOLD) != 0) |
| 1277 |
program[pc - 1] = REOP_FLATi; |
| 1278 |
else |
| 1279 |
program[pc - 1] = REOP_FLAT; |
| 1280 |
pc = addIndex(program, pc, t.flatIndex); |
| 1281 |
pc = addIndex(program, pc, t.length); |
| 1282 |
} |
| 1283 |
else { |
| 1284 |
if (t.chr < 256) { |
| 1285 |
if ((state.flags & JSREG_FOLD) != 0) |
| 1286 |
program[pc - 1] = REOP_FLAT1i; |
| 1287 |
else |
| 1288 |
program[pc - 1] = REOP_FLAT1; |
| 1289 |
program[pc++] = (byte)(t.chr); |
| 1290 |
} |
| 1291 |
else { |
| 1292 |
if ((state.flags & JSREG_FOLD) != 0) |
| 1293 |
program[pc - 1] = REOP_UCFLAT1i; |
| 1294 |
else |
| 1295 |
program[pc - 1] = REOP_UCFLAT1; |
| 1296 |
pc = addIndex(program, pc, t.chr); |
| 1297 |
} |
| 1298 |
} |
| 1299 |
break; |
| 1300 |
case REOP_LPAREN: |
| 1301 |
pc = addIndex(program, pc, t.parenIndex); |
| 1302 |
pc = emitREBytecode(state, re, pc, t.kid); |
| 1303 |
program[pc++] = REOP_RPAREN; |
| 1304 |
pc = addIndex(program, pc, t.parenIndex); |
| 1305 |
break; |
| 1306 |
case REOP_BACKREF: |
| 1307 |
pc = addIndex(program, pc, t.parenIndex); |
| 1308 |
break; |
| 1309 |
case REOP_ASSERT: |
| 1310 |
nextTermFixup = pc; |
| 1311 |
pc += INDEX_LEN; |
| 1312 |
pc = emitREBytecode(state, re, pc, t.kid); |
| 1313 |
program[pc++] = REOP_ASSERTTEST; |
| 1314 |
resolveForwardJump(program, nextTermFixup, pc); |
| 1315 |
break; |
| 1316 |
case REOP_ASSERT_NOT: |
| 1317 |
nextTermFixup = pc; |
| 1318 |
pc += INDEX_LEN; |
| 1319 |
pc = emitREBytecode(state, re, pc, t.kid); |
| 1320 |
program[pc++] = REOP_ASSERTNOTTEST; |
| 1321 |
resolveForwardJump(program, nextTermFixup, pc); |
| 1322 |
break; |
| 1323 |
case REOP_QUANT: |
| 1324 |
if ((t.min == 0) && (t.max == -1)) |
| 1325 |
program[pc - 1] = (t.greedy) ? REOP_STAR : REOP_MINIMALSTAR; |
| 1326 |
else |
| 1327 |
if ((t.min == 0) && (t.max == 1)) |
| 1328 |
program[pc - 1] = (t.greedy) ? REOP_OPT : REOP_MINIMALOPT; |
| 1329 |
else |
| 1330 |
if ((t.min == 1) && (t.max == -1)) |
| 1331 |
program[pc - 1] = (t.greedy) ? REOP_PLUS : REOP_MINIMALPLUS; |
| 1332 |
else { |
| 1333 |
if (!t.greedy) program[pc - 1] = REOP_MINIMALQUANT; |
| 1334 |
pc = addIndex(program, pc, t.min); |
| 1335 |
|
| 1336 |
pc = addIndex(program, pc, t.max + 1); |
| 1337 |
} |
| 1338 |
pc = addIndex(program, pc, t.parenCount); |
| 1339 |
pc = addIndex(program, pc, t.parenIndex); |
| 1340 |
nextTermFixup = pc; |
| 1341 |
pc += INDEX_LEN; |
| 1342 |
pc = emitREBytecode(state, re, pc, t.kid); |
| 1343 |
program[pc++] = REOP_ENDCHILD; |
| 1344 |
resolveForwardJump(program, nextTermFixup, pc); |
| 1345 |
break; |
| 1346 |
case REOP_CLASS: |
| 1347 |
if (!t.sense) |
| 1348 |
program[pc - 1] = REOP_NCLASS; |
| 1349 |
pc = addIndex(program, pc, t.index); |
| 1350 |
re.classList[t.index] = new RECharSet(t.bmsize, t.startIndex, |
| 1351 |
t.kidlen, t.sense); |
| 1352 |
break; |
| 1353 |
default: |
| 1354 |
break; |
| 1355 |
} |
| 1356 |
t = t.next; |
| 1357 |
} |
| 1358 |
return pc; |
| 1359 |
} |
| 1360 |
|
| 1361 |
private static void |
| 1362 |
pushProgState(REGlobalData gData, int min, int max, int cp, |
| 1363 |
REBackTrackData backTrackLastToSave, |
| 1364 |
int continuationOp, int continuationPc) |
| 1365 |
{ |
| 1366 |
gData.stateStackTop = new REProgState(gData.stateStackTop, min, max, |
| 1367 |
cp, backTrackLastToSave, |
| 1368 |
continuationOp, continuationPc); |
| 1369 |
} |
| 1370 |
|
| 1371 |
private static REProgState |
| 1372 |
popProgState(REGlobalData gData) |
| 1373 |
{ |
| 1374 |
REProgState state = gData.stateStackTop; |
| 1375 |
gData.stateStackTop = state.previous; |
| 1376 |
return state; |
| 1377 |
} |
| 1378 |
|
| 1379 |
private static void |
| 1380 |
pushBackTrackState(REGlobalData gData, byte op, int pc) |
| 1381 |
{ |
| 1382 |
REProgState state = gData.stateStackTop; |
| 1383 |
gData.backTrackStackTop = new REBackTrackData(gData, op, pc, |
| 1384 |
gData.cp, state.continuationOp, state.continuationPc); |
| 1385 |
} |
| 1386 |
|
| 1387 |
private static void |
| 1388 |
pushBackTrackState(REGlobalData gData, byte op, int pc, |
| 1389 |
int cp, int continuationOp, int continuationPc) |
| 1390 |
{ |
| 1391 |
gData.backTrackStackTop = new REBackTrackData(gData, op, pc, |
| 1392 |
cp, continuationOp, continuationPc); |
| 1393 |
} |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
private static boolean |
| 1399 |
flatNMatcher(REGlobalData gData, int matchChars, |
| 1400 |
int length, String input, int end) |
| 1401 |
{ |
| 1402 |
if ((gData.cp + length) > end) |
| 1403 |
return false; |
| 1404 |
for (int i = 0; i < length; i++) { |
| 1405 |
if (gData.regexp.source[matchChars + i] != input.charAt(gData.cp + i)) { |
| 1406 |
return false; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
gData.cp += length; |
| 1410 |
return true; |
| 1411 |
} |
| 1412 |
|
| 1413 |
private static boolean |
| 1414 |
flatNIMatcher(REGlobalData gData, int matchChars, |
| 1415 |
int length, String input, int end) |
| 1416 |
{ |
| 1417 |
if ((gData.cp + length) > end) |
| 1418 |
return false; |
| 1419 |
char[] source = gData.regexp.source; |
| 1420 |
for (int i = 0; i < length; i++) { |
| 1421 |
char c1 = source[matchChars + i]; |
| 1422 |
char c2 = input.charAt(gData.cp + i); |
| 1423 |
if (c1 != c2 && upcase(c1) != upcase(c2)) { |
| 1424 |
return false; |
| 1425 |
} |
| 1426 |
} |
| 1427 |
gData.cp += length; |
| 1428 |
return true; |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
private static boolean |
| 1455 |
backrefMatcher(REGlobalData gData, int parenIndex, |
| 1456 |
String input, int end) |
| 1457 |
{ |
| 1458 |
int len; |
| 1459 |
int i; |
| 1460 |
if (gData.parens == null || parenIndex >= gData.parens.length) |
| 1461 |
return false; |
| 1462 |
int parenContent = gData.parensIndex(parenIndex); |
| 1463 |
if (parenContent == -1) |
| 1464 |
return true; |
| 1465 |
|
| 1466 |
len = gData.parensLength(parenIndex); |
| 1467 |
if ((gData.cp + len) > end) |
| 1468 |
return false; |
| 1469 |
|
| 1470 |
if ((gData.regexp.flags & JSREG_FOLD) != 0) { |
| 1471 |
for (i = 0; i < len; i++) { |
| 1472 |
char c1 = input.charAt(parenContent + i); |
| 1473 |
char c2 = input.charAt(gData.cp + i); |
| 1474 |
if (c1 != c2 && upcase(c1) != upcase(c2)) |
| 1475 |
return false; |
| 1476 |
} |
| 1477 |
} |
| 1478 |
else if (!input.regionMatches(parenContent, input, gData.cp, len)) { |
| 1479 |
return false; |
| 1480 |
} |
| 1481 |
gData.cp += len; |
| 1482 |
return true; |
| 1483 |
} |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
private static void |
| 1488 |
addCharacterToCharSet(RECharSet cs, char c) |
| 1489 |
{ |
| 1490 |
int byteIndex = (c / 8); |
| 1491 |
if (c >= cs.length) { |
| 1492 |
throw ScriptRuntime.constructError("SyntaxError", |
| 1493 |
"invalid range in character class"); |
| 1494 |
} |
| 1495 |
cs.bits[byteIndex] |= 1 << (c & 0x7); |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
private static void |
| 1501 |
addCharacterRangeToCharSet(RECharSet cs, char c1, char c2) |
| 1502 |
{ |
| 1503 |
int i; |
| 1504 |
|
| 1505 |
int byteIndex1 = (c1 / 8); |
| 1506 |
int byteIndex2 = (c2 / 8); |
| 1507 |
|
| 1508 |
if ((c2 >= cs.length) || (c1 > c2)) { |
| 1509 |
throw ScriptRuntime.constructError("SyntaxError", |
| 1510 |
"invalid range in character class"); |
| 1511 |
} |
| 1512 |
|
| 1513 |
c1 &= 0x7; |
| 1514 |
c2 &= 0x7; |
| 1515 |
|
| 1516 |
if (byteIndex1 == byteIndex2) { |
| 1517 |
cs.bits[byteIndex1] |= ((0xFF) >> (7 - (c2 - c1))) << c1; |
| 1518 |
} |
| 1519 |
else { |
| 1520 |
cs.bits[byteIndex1] |= 0xFF << c1; |
| 1521 |
for (i = byteIndex1 + 1; i < byteIndex2; i++) |
| 1522 |
cs.bits[i] = (byte)0xFF; |
| 1523 |
cs.bits[byteIndex2] |= (0xFF) >> (7 - c2); |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
|
| 1528 |
private static void |
| 1529 |
processCharSet(REGlobalData gData, RECharSet charSet) |
| 1530 |
{ |
| 1531 |
synchronized (charSet) { |
| 1532 |
if (!charSet.converted) { |
| 1533 |
processCharSetImpl(gData, charSet); |
| 1534 |
charSet.converted = true; |
| 1535 |
} |
| 1536 |
} |
| 1537 |
} |
| 1538 |
|
| 1539 |
|
| 1540 |
private static void |
| 1541 |
processCharSetImpl(REGlobalData gData, RECharSet charSet) |
| 1542 |
{ |
| 1543 |
int src = charSet.startIndex; |
| 1544 |
int end = src + charSet.strlength; |
| 1545 |
|
| 1546 |
char rangeStart = 0, thisCh; |
| 1547 |
int byteLength; |
| 1548 |
char c; |
| 1549 |
int n; |
| 1550 |
int nDigits; |
| 1551 |
int i; |
| 1552 |
boolean inRange = false; |
| 1553 |
|
| 1554 |
byteLength = (charSet.length + 7) / 8; |
| 1555 |
charSet.bits = new byte[byteLength]; |
| 1556 |
|
| 1557 |
if (src == end) |
| 1558 |
return; |
| 1559 |
|
| 1560 |
if (gData.regexp.source[src] == '^') { |
| 1561 |
assert (!charSet.sense); |
| 1562 |
++src; |
| 1563 |
} else { |
| 1564 |
assert (charSet.sense); |
| 1565 |
} |
| 1566 |
|
| 1567 |
while (src != end) { |
| 1568 |
nDigits = 2; |
| 1569 |
switch (gData.regexp.source[src]) { |
| 1570 |
case '\\': |
| 1571 |
++src; |
| 1572 |
c = gData.regexp.source[src++]; |
| 1573 |
switch (c) { |
| 1574 |
case 'b': |
| 1575 |
thisCh = 0x8; |
| 1576 |
break; |
| 1577 |
case 'f': |
| 1578 |
thisCh = 0xC; |
| 1579 |
break; |
| 1580 |
case 'n': |
| 1581 |
thisCh = 0xA; |
| 1582 |
break; |
| 1583 |
case 'r': |
| 1584 |
thisCh = 0xD; |
| 1585 |
break; |
| 1586 |
case 't': |
| 1587 |
thisCh = 0x9; |
| 1588 |
break; |
| 1589 |
case 'v': |
| 1590 |
thisCh = 0xB; |
| 1591 |
break; |
| 1592 |
case 'c': |
| 1593 |
if ((src < end) && isControlLetter(gData.regexp.source[src])) |
| 1594 |
thisCh = (char)(gData.regexp.source[src++] & 0x1F); |
| 1595 |
else { |
| 1596 |
--src; |
| 1597 |
thisCh = '\\'; |
| 1598 |
} |
| 1599 |
break; |
| 1600 |
case 'u': |
| 1601 |
nDigits += 2; |
| 1602 |
|
| 1603 |
case 'x': |
| 1604 |
n = 0; |
| 1605 |
for (i = 0; (i < nDigits) && (src < end); i++) { |
| 1606 |
c = gData.regexp.source[src++]; |
| 1607 |
int digit = toASCIIHexDigit(c); |
| 1608 |
if (digit < 0) { |
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
src -= (i + 1); |
| 1613 |
n = '\\'; |
| 1614 |
break; |
| 1615 |
} |
| 1616 |
n = (n << 4) | digit; |
| 1617 |
} |
| 1618 |
thisCh = (char)(n); |
| 1619 |
break; |
| 1620 |
case '0': |
| 1621 |
case '1': |
| 1622 |
case '2': |
| 1623 |
case '3': |
| 1624 |
case '4': |
| 1625 |
case '5': |
| 1626 |
case '6': |
| 1627 |
case '7': |
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
n = (c - '0'); |
| 1635 |
c = gData.regexp.source[src]; |
| 1636 |
if ('0' <= c && c <= '7') { |
| 1637 |
src++; |
| 1638 |
n = 8 * n + (c - '0'); |
| 1639 |
c = gData.regexp.source[src]; |
| 1640 |
if ('0' <= c && c <= '7') { |
| 1641 |
src++; |
| 1642 |
i = 8 * n + (c - '0'); |
| 1643 |
if (i <= 0377) |
| 1644 |
n = i; |
| 1645 |
else |
| 1646 |
src--; |
| 1647 |
} |
| 1648 |
} |
| 1649 |
thisCh = (char)(n); |
| 1650 |
break; |
| 1651 |
|
| 1652 |
case 'd': |
| 1653 |
addCharacterRangeToCharSet(charSet, '0', '9'); |
| 1654 |
continue; |
| 1655 |
case 'D': |
| 1656 |
addCharacterRangeToCharSet(charSet, (char)0, (char)('0' - 1)); |
| 1657 |
addCharacterRangeToCharSet(charSet, (char)('9' + 1), |
| 1658 |
(char)(charSet.length - 1)); |
| 1659 |
continue; |
| 1660 |
case 's': |
| 1661 |
for (i = (charSet.length - 1); i >= 0; i--) |
| 1662 |
if (isREWhiteSpace(i)) |
| 1663 |
addCharacterToCharSet(charSet, (char)(i)); |
| 1664 |
continue; |
| 1665 |
case 'S': |
| 1666 |
for (i = (charSet.length - 1); i >= 0; i--) |
| 1667 |
if (!isREWhiteSpace(i)) |
| 1668 |
addCharacterToCharSet(charSet, (char)(i)); |
| 1669 |
continue; |
| 1670 |
case 'w': |
| 1671 |
for (i = (charSet.length - 1); i >= 0; i--) |
| 1672 |
if (isWord((char)i)) |
| 1673 |
addCharacterToCharSet(charSet, (char)(i)); |
| 1674 |
continue; |
| 1675 |
case 'W': |
| 1676 |
for (i = (charSet.length - 1); i >= 0; i--) |
| 1677 |
if (!isWord((char)i)) |
| 1678 |
addCharacterToCharSet(charSet, (char)(i)); |
| 1679 |
continue; |
| 1680 |
default: |
| 1681 |
thisCh = c; |
| 1682 |
break; |
| 1683 |
|
| 1684 |
} |
| 1685 |
break; |
| 1686 |
|
| 1687 |
default: |
| 1688 |
thisCh = gData.regexp.source[src++]; |
| 1689 |
break; |
| 1690 |
|
| 1691 |
} |
| 1692 |
if (inRange) { |
| 1693 |
if ((gData.regexp.flags & JSREG_FOLD) != 0) { |
| 1694 |
assert(rangeStart <= thisCh); |
| 1695 |
for (c = rangeStart; c <= thisCh;) { |
| 1696 |
addCharacterToCharSet(charSet, c); |
| 1697 |
char uch = upcase(c); |
| 1698 |
char dch = downcase(c); |
| 1699 |
if (c != uch) |
| 1700 |
addCharacterToCharSet(charSet, uch); |
| 1701 |
if (c != dch) |
| 1702 |
addCharacterToCharSet(charSet, dch); |
| 1703 |
if (++c == 0) |
| 1704 |
break; |
| 1705 |
} |
| 1706 |
} else { |
| 1707 |
addCharacterRangeToCharSet(charSet, rangeStart, thisCh); |
| 1708 |
} |
| 1709 |
inRange = false; |
| 1710 |
} |
| 1711 |
else { |
| 1712 |
if ((gData.regexp.flags & JSREG_FOLD) != 0) { |
| 1713 |
addCharacterToCharSet(charSet, upcase(thisCh)); |
| 1714 |
addCharacterToCharSet(charSet, downcase(thisCh)); |
| 1715 |
} else { |
| 1716 |
addCharacterToCharSet(charSet, thisCh); |
| 1717 |
} |
| 1718 |
if (src < (end - 1)) { |
| 1719 |
if (gData.regexp.source[src] == '-') { |
| 1720 |
++src; |
| 1721 |
inRange = true; |
| 1722 |
rangeStart = thisCh; |
| 1723 |
} |
| 1724 |
} |
| 1725 |
} |
| 1726 |
} |
| 1727 |
} |
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
private static boolean |
| 1735 |
classMatcher(REGlobalData gData, RECharSet charSet, char ch) |
| 1736 |
{ |
| 1737 |
if (!charSet.converted) { |
| 1738 |
processCharSet(gData, charSet); |
| 1739 |
} |
| 1740 |
|
| 1741 |
int byteIndex = ch >> 3; |
| 1742 |
return (charSet.length == 0 || |
| 1743 |
ch >= charSet.length || |
| 1744 |
(charSet.bits[byteIndex] & (1 << (ch & 0x7))) == 0) ^ charSet.sense; |
| 1745 |
} |
| 1746 |
|
| 1747 |
private static boolean reopIsSimple(int op) { |
| 1748 |
return op >= REOP_SIMPLE_START && op <= REOP_SIMPLE_END; |
| 1749 |
} |
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
private static int simpleMatch(REGlobalData gData, String input, int op, |
| 1758 |
byte[] program, int pc, int end, boolean updatecp) |
| 1759 |
{ |
| 1760 |
boolean result = false; |
| 1761 |
char matchCh; |
| 1762 |
int parenIndex; |
| 1763 |
int offset, length, index; |
| 1764 |
int startcp = gData.cp; |
| 1765 |
|
| 1766 |
switch (op) { |
| 1767 |
case REOP_EMPTY: |
| 1768 |
result = true; |
| 1769 |
break; |
| 1770 |
case REOP_BOL: |
| 1771 |
if (gData.cp != 0) { |
| 1772 |
if (!gData.multiline || !isLineTerm(input.charAt(gData.cp - 1))) { |
| 1773 |
break; |
| 1774 |
} |
| 1775 |
} |
| 1776 |
result = true; |
| 1777 |
break; |
| 1778 |
case REOP_EOL: |
| 1779 |
if (gData.cp != end) { |
| 1780 |
if (!gData.multiline || !isLineTerm(input.charAt(gData.cp))) { |
| 1781 |
break; |
| 1782 |
} |
| 1783 |
} |
| 1784 |
result = true; |
| 1785 |
break; |
| 1786 |
case REOP_WBDRY: |
| 1787 |
result = ((gData.cp == 0 || !isWord(input.charAt(gData.cp - 1))) |
| 1788 |
^ !((gData.cp < end) && isWord(input.charAt(gData.cp)))); |
| 1789 |
break; |
| 1790 |
case REOP_WNONBDRY: |
| 1791 |
result = ((gData.cp == 0 || !isWord(input.charAt(gData.cp - 1))) |
| 1792 |
^ ((gData.cp < end) && isWord(input.charAt(gData.cp)))); |
| 1793 |
break; |
| 1794 |
case REOP_DOT: |
| 1795 |
if (gData.cp != end && !isLineTerm(input.charAt(gData.cp))) { |
| 1796 |
result = true; |
| 1797 |
gData.cp++; |
| 1798 |
} |
| 1799 |
break; |
| 1800 |
case REOP_DIGIT: |
| 1801 |
if (gData.cp != end && isDigit(input.charAt(gData.cp))) { |
| 1802 |
result = true; |
| 1803 |
gData.cp++; |
| 1804 |
} |
| 1805 |
break; |
| 1806 |
case REOP_NONDIGIT: |
| 1807 |
if (gData.cp != end && !isDigit(input.charAt(gData.cp))) { |
| 1808 |
result = true; |
| 1809 |
gData.cp++; |
| 1810 |
} |
| 1811 |
break; |
| 1812 |
case REOP_ALNUM: |
| 1813 |
if (gData.cp != end && isWord(input.charAt(gData.cp))) { |
| 1814 |
result = true; |
| 1815 |
gData.cp++; |
| 1816 |
} |
| 1817 |
break; |
| 1818 |
case REOP_NONALNUM: |
| 1819 |
if (gData.cp != end && !isWord(input.charAt(gData.cp))) { |
| 1820 |
result = true; |
| 1821 |
gData.cp++; |
| 1822 |
} |
| 1823 |
break; |
| 1824 |
case REOP_SPACE: |
| 1825 |
if (gData.cp != end && isREWhiteSpace(input.charAt(gData.cp))) { |
| 1826 |
result = true; |
| 1827 |
gData.cp++; |
| 1828 |
} |
| 1829 |
break; |
| 1830 |
case REOP_NONSPACE: |
| 1831 |
if (gData.cp != end && !isREWhiteSpace(input.charAt(gData.cp))) { |
| 1832 |
result = true; |
| 1833 |
gData.cp++; |
| 1834 |
} |
| 1835 |
break; |
| 1836 |
case REOP_BACKREF: |
| 1837 |
{ |
| 1838 |
parenIndex = getIndex(program, pc); |
| 1839 |
pc += INDEX_LEN; |
| 1840 |
result = backrefMatcher(gData, parenIndex, input, end); |
| 1841 |
} |
| 1842 |
break; |
| 1843 |
case REOP_FLAT: |
| 1844 |
{ |
| 1845 |
offset = getIndex(program, pc); |
| 1846 |
pc += INDEX_LEN; |
| 1847 |
length = getIndex(program, pc); |
| 1848 |
pc += INDEX_LEN; |
| 1849 |
result = flatNMatcher(gData, offset, length, input, end); |
| 1850 |
} |
| 1851 |
break; |
| 1852 |
case REOP_FLAT1: |
| 1853 |
{ |
| 1854 |
matchCh = (char)(program[pc++] & 0xFF); |
| 1855 |
if (gData.cp != end && input.charAt(gData.cp) == matchCh) { |
| 1856 |
result = true; |
| 1857 |
gData.cp++; |
| 1858 |
} |
| 1859 |
} |
| 1860 |
break; |
| 1861 |
case REOP_FLATi: |
| 1862 |
{ |
| 1863 |
offset = getIndex(program, pc); |
| 1864 |
pc += INDEX_LEN; |
| 1865 |
length = getIndex(program, pc); |
| 1866 |
pc += INDEX_LEN; |
| 1867 |
result = flatNIMatcher(gData, offset, length, input, end); |
| 1868 |
} |
| 1869 |
break; |
| 1870 |
case REOP_FLAT1i: |
| 1871 |
{ |
| 1872 |
matchCh = (char)(program[pc++] & 0xFF); |
| 1873 |
if (gData.cp != end) { |
| 1874 |
char c = input.charAt(gData.cp); |
| 1875 |
if (matchCh == c || upcase(matchCh) == upcase(c)) { |
| 1876 |
result = true; |
| 1877 |
gData.cp++; |
| 1878 |
} |
| 1879 |
} |
| 1880 |
} |
| 1881 |
break; |
| 1882 |
case REOP_UCFLAT1: |
| 1883 |
{ |
| 1884 |
matchCh = (char)getIndex(program, pc); |
| 1885 |
pc += INDEX_LEN; |
| 1886 |
if (gData.cp != end && input.charAt(gData.cp) == matchCh) { |
| 1887 |
result = true; |
| 1888 |
gData.cp++; |
| 1889 |
} |
| 1890 |
} |
| 1891 |
break; |
| 1892 |
case REOP_UCFLAT1i: |
| 1893 |
{ |
| 1894 |
matchCh = (char)getIndex(program, pc); |
| 1895 |
pc += INDEX_LEN; |
| 1896 |
if (gData.cp != end) { |
| 1897 |
char c = input.charAt(gData.cp); |
| 1898 |
if (matchCh == c || upcase(matchCh) == upcase(c)) { |
| 1899 |
result = true; |
| 1900 |
gData.cp++; |
| 1901 |
} |
| 1902 |
} |
| 1903 |
} |
| 1904 |
break; |
| 1905 |
|
| 1906 |
case REOP_CLASS: |
| 1907 |
case REOP_NCLASS: |
| 1908 |
{ |
| 1909 |
index = getIndex(program, pc); |
| 1910 |
pc += INDEX_LEN; |
| 1911 |
if (gData.cp != end) { |
| 1912 |
if (classMatcher(gData, gData.regexp.classList[index], |
| 1913 |
input.charAt(gData.cp))) |
| 1914 |
{ |
| 1915 |
gData.cp++; |
| 1916 |
result = true; |
| 1917 |
break; |
| 1918 |
} |
| 1919 |
} |
| 1920 |
} |
| 1921 |
break; |
| 1922 |
|
| 1923 |
default: |
| 1924 |
throw Kit.codeBug(); |
| 1925 |
} |
| 1926 |
if (result) { |
| 1927 |
if (!updatecp) |
| 1928 |
gData.cp = startcp; |
| 1929 |
return pc; |
| 1930 |
} |
| 1931 |
gData.cp = startcp; |
| 1932 |
return -1; |
| 1933 |
} |
| 1934 |
|
| 1935 |
|
| 1936 |
private static boolean |
| 1937 |
executeREBytecode(REGlobalData gData, String input, int end) |
| 1938 |
{ |
| 1939 |
int pc = 0; |
| 1940 |
byte program[] = gData.regexp.program; |
| 1941 |
int continuationOp = REOP_END; |
| 1942 |
int continuationPc = 0; |
| 1943 |
boolean result = false; |
| 1944 |
|
| 1945 |
int op = program[pc++]; |
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
if (gData.regexp.anchorCh < 0 && reopIsSimple(op)) { |
| 1952 |
boolean anchor = false; |
| 1953 |
while (gData.cp <= end) { |
| 1954 |
int match = simpleMatch(gData, input, op, program, pc, end, true); |
| 1955 |
if (match >= 0) { |
| 1956 |
anchor = true; |
| 1957 |
pc = match; |
| 1958 |
op = program[pc++]; |
| 1959 |
break; |
| 1960 |
} |
| 1961 |
gData.skipped++; |
| 1962 |
gData.cp++; |
| 1963 |
} |
| 1964 |
if (!anchor) |
| 1965 |
return false; |
| 1966 |
} |
| 1967 |
|
| 1968 |
for (;;) { |
| 1969 |
|
| 1970 |
if (reopIsSimple(op)) { |
| 1971 |
int match = simpleMatch(gData, input, op, program, pc, end, true); |
| 1972 |
result = match >= 0; |
| 1973 |
if (result) |
| 1974 |
pc = match; |
| 1975 |
} else { |
| 1976 |
switchStatement: |
| 1977 |
switch (op) { |
| 1978 |
case REOP_ALTPREREQ: |
| 1979 |
case REOP_ALTPREREQi: |
| 1980 |
case REOP_ALTPREREQ2: |
| 1981 |
{ |
| 1982 |
char matchCh1 = (char)getIndex(program, pc); |
| 1983 |
pc += INDEX_LEN; |
| 1984 |
char matchCh2 = (char)getIndex(program, pc); |
| 1985 |
pc += INDEX_LEN; |
| 1986 |
|
| 1987 |
if (gData.cp == end) { |
| 1988 |
result = false; |
| 1989 |
break; |
| 1990 |
} |
| 1991 |
char c = input.charAt(gData.cp); |
| 1992 |
if (op == REOP_ALTPREREQ2) { |
| 1993 |
if (c != matchCh1 && |
| 1994 |
!classMatcher(gData, gData.regexp.classList[matchCh2], c)) { |
| 1995 |
result = false; |
| 1996 |
break; |
| 1997 |
} |
| 1998 |
} else { |
| 1999 |
if (op == REOP_ALTPREREQi) |
| 2000 |
c = upcase(c); |
| 2001 |
if (c != matchCh1 && c != matchCh2) { |
| 2002 |
result = false; |
| 2003 |
break; |
| 2004 |
} |
| 2005 |
} |
| 2006 |
} |
| 2007 |
|
| 2008 |
case REOP_ALT: |
| 2009 |
{ |
| 2010 |
int nextpc = pc + getOffset(program, pc); |
| 2011 |
pc += INDEX_LEN; |
| 2012 |
op = program[pc++]; |
| 2013 |
int startcp = gData.cp; |
| 2014 |
if (reopIsSimple(op)) { |
| 2015 |
int match = simpleMatch(gData, input, op, program, pc, end, true); |
| 2016 |
if (match < 0) { |
| 2017 |
op = program[nextpc++]; |
| 2018 |
pc = nextpc; |
| 2019 |
continue; |
| 2020 |
} |
| 2021 |
result = true; |
| 2022 |
pc = match; |
| 2023 |
op = program[pc++]; |
| 2024 |
} |
| 2025 |
byte nextop = program[nextpc++]; |
| 2026 |
pushBackTrackState(gData, nextop, nextpc, startcp, |
| 2027 |
continuationOp, continuationPc); |
| 2028 |
} |
| 2029 |
continue; |
| 2030 |
|
| 2031 |
case REOP_JUMP: |
| 2032 |
{ |
| 2033 |
int offset = getOffset(program, pc); |
| 2034 |
pc += offset; |
| 2035 |
op = program[pc++]; |
| 2036 |
} |
| 2037 |
continue; |
| 2038 |
|
| 2039 |
|
| 2040 |
case REOP_LPAREN: |
| 2041 |
{ |
| 2042 |
int parenIndex = getIndex(program, pc); |
| 2043 |
pc += INDEX_LEN; |
| 2044 |
gData.setParens(parenIndex, gData.cp, 0); |
| 2045 |
op = program[pc++]; |
| 2046 |
} |
| 2047 |
continue; |
| 2048 |
case REOP_RPAREN: |
| 2049 |
{ |
| 2050 |
int parenIndex = getIndex(program, pc); |
| 2051 |
pc += INDEX_LEN; |
| 2052 |
int cap_index = gData.parensIndex(parenIndex); |
| 2053 |
gData.setParens(parenIndex, cap_index, |
| 2054 |
gData.cp - cap_index); |
| 2055 |
op = program[pc++]; |
| 2056 |
} |
| 2057 |
continue; |
| 2058 |
|
| 2059 |
case REOP_ASSERT: |
| 2060 |
{ |
| 2061 |
int nextpc = pc + getIndex(program, pc); |
| 2062 |
pc += INDEX_LEN; |
| 2063 |
op = program[pc++]; |
| 2064 |
if (reopIsSimple(op) && simpleMatch(gData, input, op, program, pc, end, false) < 0) { |
| 2065 |
result = false; |
| 2066 |
break; |
| 2067 |
} |
| 2068 |
pushProgState(gData, 0, 0, gData.cp, gData.backTrackStackTop, |
| 2069 |
continuationOp, continuationPc); |
| 2070 |
pushBackTrackState(gData, REOP_ASSERTTEST, nextpc); |
| 2071 |
} |
| 2072 |
continue; |
| 2073 |
case REOP_ASSERT_NOT: |
| 2074 |
{ |
| 2075 |
int nextpc = pc + getIndex(program, pc); |
| 2076 |
pc += INDEX_LEN; |
| 2077 |
op = program[pc++]; |
| 2078 |
if (reopIsSimple(op)) { |
| 2079 |
int match = simpleMatch(gData, input, op, program, pc, end, false); |
| 2080 |
if (match >= 0 && program[match] == REOP_ASSERTNOTTEST) { |
| 2081 |
result = false; |
| 2082 |
break; |
| 2083 |
} |
| 2084 |
} |
| 2085 |
pushProgState(gData, 0, 0, gData.cp, gData.backTrackStackTop, |
| 2086 |
continuationOp, continuationPc); |
| 2087 |
pushBackTrackState(gData, REOP_ASSERTNOTTEST, nextpc); |
| 2088 |
} |
| 2089 |
continue; |
| 2090 |
|
| 2091 |
case REOP_ASSERTTEST: |
| 2092 |
case REOP_ASSERTNOTTEST: |
| 2093 |
{ |
| 2094 |
REProgState state = popProgState(gData); |
| 2095 |
gData.cp = state.index; |
| 2096 |
gData.backTrackStackTop = state.backTrack; |
| 2097 |
continuationPc = state.continuationPc; |
| 2098 |
continuationOp = state.continuationOp; |
| 2099 |
if (op == REOP_ASSERTNOTTEST) { |
| 2100 |
result = !result; |
| 2101 |
} |
| 2102 |
} |
| 2103 |
break; |
| 2104 |
|
| 2105 |
case REOP_STAR: |
| 2106 |
case REOP_PLUS: |
| 2107 |
case REOP_OPT: |
| 2108 |
case REOP_QUANT: |
| 2109 |
case REOP_MINIMALSTAR: |
| 2110 |
case REOP_MINIMALPLUS: |
| 2111 |
case REOP_MINIMALOPT: |
| 2112 |
case REOP_MINIMALQUANT: |
| 2113 |
{ |
| 2114 |
int min, max; |
| 2115 |
boolean greedy = false; |
| 2116 |
switch (op) { |
| 2117 |
case REOP_STAR: |
| 2118 |
greedy = true; |
| 2119 |
|
| 2120 |
case REOP_MINIMALSTAR: |
| 2121 |
min = 0; |
| 2122 |
max = -1; |
| 2123 |
break; |
| 2124 |
case REOP_PLUS: |
| 2125 |
greedy = true; |
| 2126 |
|
| 2127 |
case REOP_MINIMALPLUS: |
| 2128 |
min = 1; |
| 2129 |
max = -1; |
| 2130 |
break; |
| 2131 |
case REOP_OPT: |
| 2132 |
greedy = true; |
| 2133 |
|
| 2134 |
case REOP_MINIMALOPT: |
| 2135 |
min = 0; |
| 2136 |
max = 1; |
| 2137 |
break; |
| 2138 |
case REOP_QUANT: |
| 2139 |
greedy = true; |
| 2140 |
|
| 2141 |
case REOP_MINIMALQUANT: |
| 2142 |
min = getOffset(program, pc); |
| 2143 |
pc += INDEX_LEN; |
| 2144 |
|
| 2145 |
max = getOffset(program, pc) - 1; |
| 2146 |
pc += INDEX_LEN; |
| 2147 |
break; |
| 2148 |
default: |
| 2149 |
throw Kit.codeBug(); |
| 2150 |
} |
| 2151 |
pushProgState(gData, min, max, gData.cp, null, |
| 2152 |
continuationOp, continuationPc); |
| 2153 |
if (greedy) { |
| 2154 |
pushBackTrackState(gData, REOP_REPEAT, pc); |
| 2155 |
continuationOp = REOP_REPEAT; |
| 2156 |
continuationPc = pc; |
| 2157 |
|
| 2158 |
pc += 3 * INDEX_LEN; |
| 2159 |
op = program[pc++]; |
| 2160 |
} else { |
| 2161 |
if (min != 0) { |
| 2162 |
continuationOp = REOP_MINIMALREPEAT; |
| 2163 |
continuationPc = pc; |
| 2164 |
|
| 2165 |
pc += 3 * INDEX_LEN; |
| 2166 |
op = program[pc++]; |
| 2167 |
} else { |
| 2168 |
pushBackTrackState(gData, REOP_MINIMALREPEAT, pc); |
| 2169 |
popProgState(gData); |
| 2170 |
pc += 2 * INDEX_LEN; |
| 2171 |
pc = pc + getOffset(program, pc); |
| 2172 |
op = program[pc++]; |
| 2173 |
} |
| 2174 |
} |
| 2175 |
} |
| 2176 |
continue; |
| 2177 |
|
| 2178 |
case REOP_ENDCHILD: |
| 2179 |
|
| 2180 |
|
| 2181 |
result = true; |
| 2182 |
|
| 2183 |
pc = continuationPc; |
| 2184 |
op = continuationOp; |
| 2185 |
continue; |
| 2186 |
|
| 2187 |
case REOP_REPEAT: |
| 2188 |
{ |
| 2189 |
int nextpc, nextop; |
| 2190 |
do { |
| 2191 |
REProgState state = popProgState(gData); |
| 2192 |
if (!result) { |
| 2193 |
|
| 2194 |
if (state.min == 0) |
| 2195 |
result = true; |
| 2196 |
continuationPc = state.continuationPc; |
| 2197 |
continuationOp = state.continuationOp; |
| 2198 |
pc += 2 * INDEX_LEN; |
| 2199 |
pc += getOffset(program, pc); |
| 2200 |
break switchStatement; |
| 2201 |
} |
| 2202 |
if (state.min == 0 && gData.cp == state.index) { |
| 2203 |
|
| 2204 |
result = false; |
| 2205 |
continuationPc = state.continuationPc; |
| 2206 |
continuationOp = state.continuationOp; |
| 2207 |
pc += 2 * INDEX_LEN; |
| 2208 |
pc += getOffset(program, pc); |
| 2209 |
break switchStatement; |
| 2210 |
} |
| 2211 |
int new_min = state.min, new_max = state.max; |
| 2212 |
if (new_min != 0) new_min--; |
| 2213 |
if (new_max != -1) new_max--; |
| 2214 |
if (new_max == 0) { |
| 2215 |
result = true; |
| 2216 |
continuationPc = state.continuationPc; |
| 2217 |
continuationOp = state.continuationOp; |
| 2218 |
pc += 2 * INDEX_LEN; |
| 2219 |
pc += getOffset(program, pc); |
| 2220 |
break switchStatement; |
| 2221 |
} |
| 2222 |
nextpc = pc + 3 * INDEX_LEN; |
| 2223 |
nextop = program[nextpc]; |
| 2224 |
int startcp = gData.cp; |
| 2225 |
if (reopIsSimple(nextop)) { |
| 2226 |
nextpc++; |
| 2227 |
int match = simpleMatch(gData, input, nextop, program, nextpc, end, true); |
| 2228 |
if (match < 0) { |
| 2229 |
result = (new_min == 0); |
| 2230 |
continuationPc = state.continuationPc; |
| 2231 |
continuationOp = state.continuationOp; |
| 2232 |
pc += 2 * INDEX_LEN; |
| 2233 |
pc += getOffset(program, pc); |
| 2234 |
break switchStatement; |
| 2235 |
} |
| 2236 |
result = true; |
| 2237 |
nextpc = match; |
| 2238 |
} |
| 2239 |
continuationOp = REOP_REPEAT; |
| 2240 |
continuationPc = pc; |
| 2241 |
pushProgState(gData, new_min, new_max, startcp, null, |
| 2242 |
state.continuationOp, state.continuationPc); |
| 2243 |
if (new_min == 0) { |
| 2244 |
pushBackTrackState(gData, REOP_REPEAT, pc, startcp, |
| 2245 |
state.continuationOp, state.continuationPc); |
| 2246 |
int parenCount = getIndex(program, pc); |
| 2247 |
int parenIndex = getIndex(program, pc + INDEX_LEN); |
| 2248 |
for (int k = 0; k < parenCount; k++) { |
| 2249 |
gData.setParens(parenIndex + k, -1, 0); |
| 2250 |
} |
| 2251 |
} |
| 2252 |
} while (program[nextpc] == REOP_ENDCHILD); |
| 2253 |
|
| 2254 |
pc = nextpc; |
| 2255 |
op = program[pc++]; |
| 2256 |
} |
| 2257 |
continue; |
| 2258 |
|
| 2259 |
case REOP_MINIMALREPEAT: |
| 2260 |
{ |
| 2261 |
REProgState state = popProgState(gData); |
| 2262 |
if (!result) { |
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
if (state.max == -1 || state.max > 0) { |
| 2267 |
pushProgState(gData, state.min, state.max, gData.cp, null, |
| 2268 |
state.continuationOp, state.continuationPc); |
| 2269 |
continuationOp = REOP_MINIMALREPEAT; |
| 2270 |
continuationPc = pc; |
| 2271 |
int parenCount = getIndex(program, pc); |
| 2272 |
pc += INDEX_LEN; |
| 2273 |
int parenIndex = getIndex(program, pc); |
| 2274 |
pc += 2 * INDEX_LEN; |
| 2275 |
for (int k = 0; k < parenCount; k++) { |
| 2276 |
gData.setParens(parenIndex + k, -1, 0); |
| 2277 |
} |
| 2278 |
op = program[pc++]; |
| 2279 |
continue; |
| 2280 |
} else { |
| 2281 |
|
| 2282 |
continuationPc = state.continuationPc; |
| 2283 |
continuationOp = state.continuationOp; |
| 2284 |
break; |
| 2285 |
} |
| 2286 |
} else { |
| 2287 |
if (state.min == 0 && gData.cp == state.index) { |
| 2288 |
|
| 2289 |
result = false; |
| 2290 |
continuationPc = state.continuationPc; |
| 2291 |
continuationOp = state.continuationOp; |
| 2292 |
break; |
| 2293 |
} |
| 2294 |
int new_min = state.min, new_max = state.max; |
| 2295 |
if (new_min != 0) new_min--; |
| 2296 |
if (new_max != -1) new_max--; |
| 2297 |
pushProgState(gData, new_min, new_max, gData.cp, null, |
| 2298 |
state.continuationOp, state.continuationPc); |
| 2299 |
if (new_min != 0) { |
| 2300 |
continuationOp = REOP_MINIMALREPEAT; |
| 2301 |
continuationPc = pc; |
| 2302 |
int parenCount = getIndex(program, pc); |
| 2303 |
pc += INDEX_LEN; |
| 2304 |
int parenIndex = getIndex(program, pc); |
| 2305 |
pc += 2 * INDEX_LEN; |
| 2306 |
for (int k = 0; k < parenCount; k++) { |
| 2307 |
gData.setParens(parenIndex + k, -1, 0); |
| 2308 |
} |
| 2309 |
op = program[pc++]; |
| 2310 |
} else { |
| 2311 |
continuationPc = state.continuationPc; |
| 2312 |
continuationOp = state.continuationOp; |
| 2313 |
pushBackTrackState(gData, REOP_MINIMALREPEAT, pc); |
| 2314 |
popProgState(gData); |
| 2315 |
pc += 2 * INDEX_LEN; |
| 2316 |
pc = pc + getOffset(program, pc); |
| 2317 |
op = program[pc++]; |
| 2318 |
} |
| 2319 |
continue; |
| 2320 |
} |
| 2321 |
} |
| 2322 |
|
| 2323 |
case REOP_END: |
| 2324 |
return true; |
| 2325 |
|
| 2326 |
default: |
| 2327 |
throw Kit.codeBug("invalid bytecode"); |
| 2328 |
|
| 2329 |
} |
| 2330 |
} |
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
|
| 2335 |
if (!result) { |
| 2336 |
REBackTrackData backTrackData = gData.backTrackStackTop; |
| 2337 |
if (backTrackData != null) { |
| 2338 |
gData.backTrackStackTop = backTrackData.previous; |
| 2339 |
gData.parens = backTrackData.parens; |
| 2340 |
gData.cp = backTrackData.cp; |
| 2341 |
gData.stateStackTop = backTrackData.stateStackTop; |
| 2342 |
continuationOp = backTrackData.continuationOp; |
| 2343 |
continuationPc = backTrackData.continuationPc; |
| 2344 |
pc = backTrackData.pc; |
| 2345 |
op = backTrackData.op; |
| 2346 |
continue; |
| 2347 |
} |
| 2348 |
else |
| 2349 |
return false; |
| 2350 |
} |
| 2351 |
|
| 2352 |
op = program[pc++]; |
| 2353 |
} |
| 2354 |
|
| 2355 |
} |
| 2356 |
|
| 2357 |
private static boolean |
| 2358 |
matchRegExp(REGlobalData gData, RECompiled re, |
| 2359 |
String input, int start, int end, boolean multiline) |
| 2360 |
{ |
| 2361 |
if (re.parenCount != 0) { |
| 2362 |
gData.parens = new long[re.parenCount]; |
| 2363 |
} else { |
| 2364 |
gData.parens = null; |
| 2365 |
} |
| 2366 |
|
| 2367 |
gData.backTrackStackTop = null; |
| 2368 |
gData.stateStackTop = null; |
| 2369 |
|
| 2370 |
gData.multiline = multiline || (re.flags & JSREG_MULTILINE) != 0; |
| 2371 |
gData.regexp = re; |
| 2372 |
|
| 2373 |
int anchorCh = gData.regexp.anchorCh; |
| 2374 |
|
| 2375 |
|
| 2376 |
|
| 2377 |
|
| 2378 |
for (int i = start; i <= end; ++i) { |
| 2379 |
|
| 2380 |
|
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
if (anchorCh >= 0) { |
| 2385 |
for (;;) { |
| 2386 |
if (i == end) { |
| 2387 |
return false; |
| 2388 |
} |
| 2389 |
char matchCh = input.charAt(i); |
| 2390 |
if (matchCh == anchorCh || |
| 2391 |
((gData.regexp.flags & JSREG_FOLD) != 0 |
| 2392 |
&& upcase(matchCh) == upcase((char)anchorCh))) |
| 2393 |
{ |
| 2394 |
break; |
| 2395 |
} |
| 2396 |
++i; |
| 2397 |
} |
| 2398 |
} |
| 2399 |
gData.cp = i; |
| 2400 |
gData.skipped = i - start; |
| 2401 |
for (int j = 0; j < re.parenCount; j++) { |
| 2402 |
gData.parens[j] = -1L; |
| 2403 |
} |
| 2404 |
boolean result = executeREBytecode(gData, input, end); |
| 2405 |
|
| 2406 |
gData.backTrackStackTop = null; |
| 2407 |
gData.stateStackTop = null; |
| 2408 |
if (result) { |
| 2409 |
return true; |
| 2410 |
} |
| 2411 |
if (anchorCh == ANCHOR_BOL && !gData.multiline) { |
| 2412 |
gData.skipped = end; |
| 2413 |
return false; |
| 2414 |
} |
| 2415 |
i = start + gData.skipped; |
| 2416 |
} |
| 2417 |
return false; |
| 2418 |
} |
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
Object executeRegExp(Context cx, Scriptable scope, RegExpImpl res, |
| 2424 |
String str, int indexp[], int matchType) |
| 2425 |
{ |
| 2426 |
REGlobalData gData = new REGlobalData(); |
| 2427 |
|
| 2428 |
int start = indexp[0]; |
| 2429 |
int end = str.length(); |
| 2430 |
if (start > end) |
| 2431 |
start = end; |
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
boolean matches = matchRegExp(gData, re, str, start, end, |
| 2436 |
res.multiline); |
| 2437 |
if (!matches) { |
| 2438 |
if (matchType != PREFIX) return null; |
| 2439 |
return Undefined.instance; |
| 2440 |
} |
| 2441 |
int index = gData.cp; |
| 2442 |
int ep = indexp[0] = index; |
| 2443 |
int matchlen = ep - (start + gData.skipped); |
| 2444 |
index -= matchlen; |
| 2445 |
Object result; |
| 2446 |
Scriptable obj; |
| 2447 |
|
| 2448 |
if (matchType == TEST) { |
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
result = Boolean.TRUE; |
| 2454 |
obj = null; |
| 2455 |
} |
| 2456 |
else { |
| 2457 |
|
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
result = cx.newArray(scope, 0); |
| 2464 |
obj = (Scriptable) result; |
| 2465 |
|
| 2466 |
String matchstr = str.substring(index, index + matchlen); |
| 2467 |
obj.put(0, obj, matchstr); |
| 2468 |
} |
| 2469 |
|
| 2470 |
if (re.parenCount == 0) { |
| 2471 |
res.parens = null; |
| 2472 |
res.lastParen = SubString.emptySubString; |
| 2473 |
} else { |
| 2474 |
SubString parsub = null; |
| 2475 |
int num; |
| 2476 |
res.parens = new SubString[re.parenCount]; |
| 2477 |
for (num = 0; num < re.parenCount; num++) { |
| 2478 |
int cap_index = gData.parensIndex(num); |
| 2479 |
String parstr; |
| 2480 |
if (cap_index != -1) { |
| 2481 |
int cap_length = gData.parensLength(num); |
| 2482 |
parsub = new SubString(str, cap_index, cap_length); |
| 2483 |
res.parens[num] = parsub; |
| 2484 |
if (matchType != TEST) |
| 2485 |
obj.put(num+1, obj, parsub.toString()); |
| 2486 |
} |
| 2487 |
else { |
| 2488 |
if (matchType != TEST) |
| 2489 |
obj.put(num+1, obj, Undefined.instance); |
| 2490 |
} |
| 2491 |
} |
| 2492 |
res.lastParen = parsub; |
| 2493 |
} |
| 2494 |
|
| 2495 |
if (! (matchType == TEST)) { |
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
obj.put("index", obj, Integer.valueOf(start + gData.skipped)); |
| 2501 |
obj.put("input", obj, str); |
| 2502 |
} |
| 2503 |
|
| 2504 |
if (res.lastMatch == null) { |
| 2505 |
res.lastMatch = new SubString(); |
| 2506 |
res.leftContext = new SubString(); |
| 2507 |
res.rightContext = new SubString(); |
| 2508 |
} |
| 2509 |
res.lastMatch.str = str; |
| 2510 |
res.lastMatch.index = index; |
| 2511 |
res.lastMatch.length = matchlen; |
| 2512 |
|
| 2513 |
res.leftContext.str = str; |
| 2514 |
if (cx.getLanguageVersion() == Context.VERSION_1_2) { |
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
res.leftContext.index = start; |
| 2529 |
res.leftContext.length = gData.skipped; |
| 2530 |
} else { |
| 2531 |
|
| 2532 |
|
| 2533 |
|
| 2534 |
|
| 2535 |
|
| 2536 |
res.leftContext.index = 0; |
| 2537 |
res.leftContext.length = start + gData.skipped; |
| 2538 |
} |
| 2539 |
|
| 2540 |
res.rightContext.str = str; |
| 2541 |
res.rightContext.index = ep; |
| 2542 |
res.rightContext.length = end - ep; |
| 2543 |
|
| 2544 |
return result; |
| 2545 |
} |
| 2546 |
|
| 2547 |
int getFlags() |
| 2548 |
{ |
| 2549 |
return re.flags; |
| 2550 |
} |
| 2551 |
|
| 2552 |
private static void reportWarning(Context cx, String messageId, String arg) |
| 2553 |
{ |
| 2554 |
if (cx.hasFeature(Context.FEATURE_STRICT_MODE)) { |
| 2555 |
String msg = ScriptRuntime.getMessage1(messageId, arg); |
| 2556 |
Context.reportWarning(msg); |
| 2557 |
} |
| 2558 |
} |
| 2559 |
|
| 2560 |
private static void reportError(String messageId, String arg) |
| 2561 |
{ |
| 2562 |
String msg = ScriptRuntime.getMessage1(messageId, arg); |
| 2563 |
throw ScriptRuntime.constructError("SyntaxError", msg); |
| 2564 |
} |
| 2565 |
|
| 2566 |
|
| 2567 |
|
| 2568 |
private static final int |
| 2569 |
Id_lastIndex = 1, |
| 2570 |
Id_source = 2, |
| 2571 |
Id_global = 3, |
| 2572 |
Id_ignoreCase = 4, |
| 2573 |
Id_multiline = 5, |
| 2574 |
|
| 2575 |
MAX_INSTANCE_ID = 5; |
| 2576 |
|
| 2577 |
@Override |
| 2578 |
protected int getMaxInstanceId() |
| 2579 |
{ |
| 2580 |
return MAX_INSTANCE_ID; |
| 2581 |
} |
| 2582 |
|
| 2583 |
@Override |
| 2584 |
protected int findInstanceIdInfo(String s) |
| 2585 |
{ |
| 2586 |
int id; |
| 2587 |
|
| 2588 |
L0: { id = 0; String X = null; int c; |
| 2589 |
int s_length = s.length(); |
| 2590 |
if (s_length==6) { |
| 2591 |
c=s.charAt(0); |
| 2592 |
if (c=='g') { X="global";id=Id_global; } |
| 2593 |
else if (c=='s') { X="source";id=Id_source; } |
| 2594 |
} |
| 2595 |
else if (s_length==9) { |
| 2596 |
c=s.charAt(0); |
| 2597 |
if (c=='l') { X="lastIndex";id=Id_lastIndex; } |
| 2598 |
else if (c=='m') { X="multiline";id=Id_multiline; } |
| 2599 |
} |
| 2600 |
else if (s_length==10) { X="ignoreCase";id=Id_ignoreCase; } |
| 2601 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 2602 |
break L0; |
| 2603 |
} |
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
if (id == 0) return super.findInstanceIdInfo(s); |
| 2608 |
|
| 2609 |
int attr; |
| 2610 |
switch (id) { |
| 2611 |
case Id_lastIndex: |
| 2612 |
attr = lastIndexAttr; |
| 2613 |
break; |
| 2614 |
case Id_source: |
| 2615 |
case Id_global: |
| 2616 |
case Id_ignoreCase: |
| 2617 |
case Id_multiline: |
| 2618 |
attr = PERMANENT | READONLY | DONTENUM; |
| 2619 |
break; |
| 2620 |
default: |
| 2621 |
throw new IllegalStateException(); |
| 2622 |
} |
| 2623 |
return instanceIdInfo(attr, id); |
| 2624 |
} |
| 2625 |
|
| 2626 |
@Override |
| 2627 |
protected String getInstanceIdName(int id) |
| 2628 |
{ |
| 2629 |
switch (id) { |
| 2630 |
case Id_lastIndex: return "lastIndex"; |
| 2631 |
case Id_source: return "source"; |
| 2632 |
case Id_global: return "global"; |
| 2633 |
case Id_ignoreCase: return "ignoreCase"; |
| 2634 |
case Id_multiline: return "multiline"; |
| 2635 |
} |
| 2636 |
return super.getInstanceIdName(id); |
| 2637 |
} |
| 2638 |
|
| 2639 |
@Override |
| 2640 |
protected Object getInstanceIdValue(int id) |
| 2641 |
{ |
| 2642 |
switch (id) { |
| 2643 |
case Id_lastIndex: |
| 2644 |
return lastIndex; |
| 2645 |
case Id_source: |
| 2646 |
return new String(re.source); |
| 2647 |
case Id_global: |
| 2648 |
return ScriptRuntime.wrapBoolean((re.flags & JSREG_GLOB) != 0); |
| 2649 |
case Id_ignoreCase: |
| 2650 |
return ScriptRuntime.wrapBoolean((re.flags & JSREG_FOLD) != 0); |
| 2651 |
case Id_multiline: |
| 2652 |
return ScriptRuntime.wrapBoolean((re.flags & JSREG_MULTILINE) != 0); |
| 2653 |
} |
| 2654 |
return super.getInstanceIdValue(id); |
| 2655 |
} |
| 2656 |
|
| 2657 |
@Override |
| 2658 |
protected void setInstanceIdValue(int id, Object value) |
| 2659 |
{ |
| 2660 |
switch (id) { |
| 2661 |
case Id_lastIndex: |
| 2662 |
lastIndex = value; |
| 2663 |
return; |
| 2664 |
case Id_source: |
| 2665 |
case Id_global: |
| 2666 |
case Id_ignoreCase: |
| 2667 |
case Id_multiline: |
| 2668 |
return; |
| 2669 |
} |
| 2670 |
super.setInstanceIdValue(id, value); |
| 2671 |
} |
| 2672 |
|
| 2673 |
@Override |
| 2674 |
protected void setInstanceIdAttributes(int id, int attr) { |
| 2675 |
switch (id) { |
| 2676 |
case Id_lastIndex: |
| 2677 |
lastIndexAttr = attr; |
| 2678 |
return; |
| 2679 |
} |
| 2680 |
super.setInstanceIdAttributes(id, attr); |
| 2681 |
} |
| 2682 |
|
| 2683 |
@Override |
| 2684 |
protected void initPrototypeId(int id) |
| 2685 |
{ |
| 2686 |
String s; |
| 2687 |
int arity; |
| 2688 |
switch (id) { |
| 2689 |
case Id_compile: arity=2; s="compile"; break; |
| 2690 |
case Id_toString: arity=0; s="toString"; break; |
| 2691 |
case Id_toSource: arity=0; s="toSource"; break; |
| 2692 |
case Id_exec: arity=1; s="exec"; break; |
| 2693 |
case Id_test: arity=1; s="test"; break; |
| 2694 |
case Id_prefix: arity=1; s="prefix"; break; |
| 2695 |
default: throw new IllegalArgumentException(String.valueOf(id)); |
| 2696 |
} |
| 2697 |
initPrototypeMethod(REGEXP_TAG, id, s, arity); |
| 2698 |
} |
| 2699 |
|
| 2700 |
@Override |
| 2701 |
public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope, |
| 2702 |
Scriptable thisObj, Object[] args) |
| 2703 |
{ |
| 2704 |
if (!f.hasTag(REGEXP_TAG)) { |
| 2705 |
return super.execIdCall(f, cx, scope, thisObj, args); |
| 2706 |
} |
| 2707 |
int id = f.methodId(); |
| 2708 |
switch (id) { |
| 2709 |
case Id_compile: |
| 2710 |
return realThis(thisObj, f).compile(cx, scope, args); |
| 2711 |
|
| 2712 |
case Id_toString: |
| 2713 |
case Id_toSource: |
| 2714 |
return realThis(thisObj, f).toString(); |
| 2715 |
|
| 2716 |
case Id_exec: |
| 2717 |
return realThis(thisObj, f).execSub(cx, scope, args, MATCH); |
| 2718 |
|
| 2719 |
case Id_test: { |
| 2720 |
Object x = realThis(thisObj, f).execSub(cx, scope, args, TEST); |
| 2721 |
return Boolean.TRUE.equals(x) ? Boolean.TRUE : Boolean.FALSE; |
| 2722 |
} |
| 2723 |
|
| 2724 |
case Id_prefix: |
| 2725 |
return realThis(thisObj, f).execSub(cx, scope, args, PREFIX); |
| 2726 |
} |
| 2727 |
throw new IllegalArgumentException(String.valueOf(id)); |
| 2728 |
} |
| 2729 |
|
| 2730 |
private static NativeRegExp realThis(Scriptable thisObj, IdFunctionObject f) |
| 2731 |
{ |
| 2732 |
if (!(thisObj instanceof NativeRegExp)) |
| 2733 |
throw incompatibleCallError(f); |
| 2734 |
return (NativeRegExp)thisObj; |
| 2735 |
} |
| 2736 |
|
| 2737 |
|
| 2738 |
@Override |
| 2739 |
protected int findPrototypeId(String s) |
| 2740 |
{ |
| 2741 |
int id; |
| 2742 |
|
| 2743 |
L0: { id = 0; String X = null; int c; |
| 2744 |
L: switch (s.length()) { |
| 2745 |
case 4: c=s.charAt(0); |
| 2746 |
if (c=='e') { X="exec";id=Id_exec; } |
| 2747 |
else if (c=='t') { X="test";id=Id_test; } |
| 2748 |
break L; |
| 2749 |
case 6: X="prefix";id=Id_prefix; break L; |
| 2750 |
case 7: X="compile";id=Id_compile; break L; |
| 2751 |
case 8: c=s.charAt(3); |
| 2752 |
if (c=='o') { X="toSource";id=Id_toSource; } |
| 2753 |
else if (c=='t') { X="toString";id=Id_toString; } |
| 2754 |
break L; |
| 2755 |
} |
| 2756 |
if (X!=null && X!=s && !X.equals(s)) id = 0; |
| 2757 |
break L0; |
| 2758 |
} |
| 2759 |
|
| 2760 |
return id; |
| 2761 |
} |
| 2762 |
|
| 2763 |
private static final int |
| 2764 |
Id_compile = 1, |
| 2765 |
Id_toString = 2, |
| 2766 |
Id_toSource = 3, |
| 2767 |
Id_exec = 4, |
| 2768 |
Id_test = 5, |
| 2769 |
Id_prefix = 6, |
| 2770 |
|
| 2771 |
MAX_PROTOTYPE_ID = 6; |
| 2772 |
|
| 2773 |
|
| 2774 |
|
| 2775 |
private RECompiled re; |
| 2776 |
Object lastIndex = 0d; |
| 2777 |
private int lastIndexAttr = DONTENUM | PERMANENT; |
| 2778 |
|
| 2779 |
} |
| 2780 |
|
| 2781 |
class RECompiled implements Serializable |
| 2782 |
{ |
| 2783 |
static final long serialVersionUID = -6144956577595844213L; |
| 2784 |
|
| 2785 |
final char[] source; |
| 2786 |
int parenCount; |
| 2787 |
int flags; |
| 2788 |
byte[] program; |
| 2789 |
int classCount; |
| 2790 |
RECharSet[] classList; |
| 2791 |
int anchorCh = -1; |
| 2792 |
|
| 2793 |
RECompiled(String str) { |
| 2794 |
this.source = str.toCharArray(); |
| 2795 |
} |
| 2796 |
} |
| 2797 |
|
| 2798 |
class RENode { |
| 2799 |
|
| 2800 |
RENode(byte op) |
| 2801 |
{ |
| 2802 |
this.op = op; |
| 2803 |
} |
| 2804 |
|
| 2805 |
byte op; |
| 2806 |
RENode next; |
| 2807 |
RENode kid; |
| 2808 |
|
| 2809 |
RENode kid2; |
| 2810 |
int parenIndex; |
| 2811 |
|
| 2812 |
|
| 2813 |
int min; |
| 2814 |
int max; |
| 2815 |
int parenCount; |
| 2816 |
boolean greedy; |
| 2817 |
|
| 2818 |
|
| 2819 |
int startIndex; |
| 2820 |
int kidlen; |
| 2821 |
int bmsize; |
| 2822 |
int index; |
| 2823 |
boolean sense; |
| 2824 |
|
| 2825 |
|
| 2826 |
char chr; |
| 2827 |
int length; |
| 2828 |
int flatIndex; |
| 2829 |
|
| 2830 |
} |
| 2831 |
|
| 2832 |
class CompilerState { |
| 2833 |
|
| 2834 |
CompilerState(Context cx, char[] source, int length, int flags) |
| 2835 |
{ |
| 2836 |
this.cx = cx; |
| 2837 |
this.cpbegin = source; |
| 2838 |
this.cp = 0; |
| 2839 |
this.cpend = length; |
| 2840 |
this.flags = flags; |
| 2841 |
this.backReferenceLimit = Integer.MAX_VALUE; |
| 2842 |
this.maxBackReference = 0; |
| 2843 |
this.parenCount = 0; |
| 2844 |
this.classCount = 0; |
| 2845 |
this.progLength = 0; |
| 2846 |
} |
| 2847 |
|
| 2848 |
Context cx; |
| 2849 |
char cpbegin[]; |
| 2850 |
int cpend; |
| 2851 |
int cp; |
| 2852 |
int flags; |
| 2853 |
int backReferenceLimit; |
| 2854 |
int maxBackReference; |
| 2855 |
int parenCount; |
| 2856 |
int parenNesting; |
| 2857 |
int classCount; |
| 2858 |
int progLength; |
| 2859 |
RENode result; |
| 2860 |
} |
| 2861 |
|
| 2862 |
class REProgState |
| 2863 |
{ |
| 2864 |
REProgState(REProgState previous, int min, int max, int index, |
| 2865 |
REBackTrackData backTrack, |
| 2866 |
int continuationOp, int continuationPc) |
| 2867 |
{ |
| 2868 |
this.previous = previous; |
| 2869 |
this.min = min; |
| 2870 |
this.max = max; |
| 2871 |
this.index = index; |
| 2872 |
this.continuationOp = continuationOp; |
| 2873 |
this.continuationPc = continuationPc; |
| 2874 |
this.backTrack = backTrack; |
| 2875 |
} |
| 2876 |
|
| 2877 |
final REProgState previous; |
| 2878 |
|
| 2879 |
final int min; |
| 2880 |
final int max; |
| 2881 |
final int index; |
| 2882 |
final int continuationOp; |
| 2883 |
final int continuationPc; |
| 2884 |
final REBackTrackData backTrack; |
| 2885 |
} |
| 2886 |
|
| 2887 |
class REBackTrackData { |
| 2888 |
|
| 2889 |
REBackTrackData(REGlobalData gData, int op, int pc, int cp, |
| 2890 |
int continuationOp, int continuationPc) |
| 2891 |
{ |
| 2892 |
previous = gData.backTrackStackTop; |
| 2893 |
this.op = op; |
| 2894 |
this.pc = pc; |
| 2895 |
this.cp = cp; |
| 2896 |
this.continuationOp = continuationOp; |
| 2897 |
this.continuationPc = continuationPc; |
| 2898 |
parens = gData.parens; |
| 2899 |
stateStackTop = gData.stateStackTop; |
| 2900 |
} |
| 2901 |
|
| 2902 |
final REBackTrackData previous; |
| 2903 |
|
| 2904 |
final int op; |
| 2905 |
final int pc; |
| 2906 |
final int cp; |
| 2907 |
final int continuationOp; |
| 2908 |
final int continuationPc; |
| 2909 |
final long[] parens; |
| 2910 |
final REProgState stateStackTop; |
| 2911 |
} |
| 2912 |
|
| 2913 |
class REGlobalData { |
| 2914 |
boolean multiline; |
| 2915 |
RECompiled regexp; |
| 2916 |
int skipped; |
| 2917 |
|
| 2918 |
int cp; |
| 2919 |
long[] parens; |
| 2920 |
|
| 2921 |
REProgState stateStackTop; |
| 2922 |
|
| 2923 |
REBackTrackData backTrackStackTop; |
| 2924 |
|
| 2925 |
|
| 2926 |
|
| 2927 |
|
| 2928 |
|
| 2929 |
int parensIndex(int i) |
| 2930 |
{ |
| 2931 |
return (int)(parens[i]); |
| 2932 |
} |
| 2933 |
|
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
int parensLength(int i) |
| 2938 |
{ |
| 2939 |
return (int)(parens[i] >>> 32); |
| 2940 |
} |
| 2941 |
|
| 2942 |
void setParens(int i, int index, int length) |
| 2943 |
{ |
| 2944 |
|
| 2945 |
if (backTrackStackTop != null && backTrackStackTop.parens == parens) { |
| 2946 |
parens = parens.clone(); |
| 2947 |
} |
| 2948 |
parens[i] = (index & 0xffffffffL) | ((long)length << 32); |
| 2949 |
} |
| 2950 |
|
| 2951 |
} |
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
final class RECharSet implements Serializable |
| 2962 |
{ |
| 2963 |
static final long serialVersionUID = 7931787979395898394L; |
| 2964 |
|
| 2965 |
RECharSet(int length, int startIndex, int strlength, boolean sense) |
| 2966 |
{ |
| 2967 |
this.length = length; |
| 2968 |
this.startIndex = startIndex; |
| 2969 |
this.strlength = strlength; |
| 2970 |
this.sense = sense; |
| 2971 |
} |
| 2972 |
|
| 2973 |
final int length; |
| 2974 |
final int startIndex; |
| 2975 |
final int strlength; |
| 2976 |
final boolean sense; |
| 2977 |
|
| 2978 |
volatile transient boolean converted; |
| 2979 |
volatile transient byte[] bits; |
| 2980 |
} |
| 2981 |
|
| 2982 |
|