| 336 | // for later pattern matching. |
| 337 | |
| 338 | bool RegularExpression::compile(char const* exp) |
| 339 | { |
| 340 | char const* scan; |
| 341 | char const* longest; |
| 342 | int flags; |
| 343 | |
| 344 | if (!exp) { |
| 345 | // RAISE Error, SYM(RegularExpression), SYM(No_Expr), |
| 346 | printf("RegularExpression::compile(): No expression supplied.\n"); |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | // First pass: determine size, legality. |
| 351 | RegExpCompile comp; |
| 352 | comp.regparse = exp; |
| 353 | comp.regnpar = 1; |
| 354 | comp.regsize = 0L; |
| 355 | comp.regcode = regdummyptr; |
| 356 | comp.regc(static_cast<char>(MAGIC)); |
| 357 | if (!comp.reg(0, &flags)) { |
| 358 | printf("RegularExpression::compile(): Error in compile.\n"); |
| 359 | return false; |
| 360 | } |
| 361 | this->regmatch.clear(); |
| 362 | |
| 363 | // Small enough for pointer-storage convention? |
| 364 | if (comp.regsize >= 65535L) { |
| 365 | // RAISE Error, SYM(RegularExpression), SYM(Expr_Too_Big), |
| 366 | printf("RegularExpression::compile(): Expression too big.\n"); |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | // Allocate space. |
| 371 | // #ifndef _WIN32 |
| 372 | delete[] this->program; |
| 373 | // #endif |
| 374 | this->program = new char[comp.regsize]; |
| 375 | this->progsize = static_cast<int>(comp.regsize); |
| 376 | this->regnpar = comp.regnpar; |
| 377 | |
| 378 | if (!this->program) { |
| 379 | // RAISE Error, SYM(RegularExpression), SYM(Out_Of_Memory), |
| 380 | printf("RegularExpression::compile(): Out of memory.\n"); |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | #ifdef __clang_analyzer__ /* Convince it that the program is initialized. */ |
| 385 | memset(this->program, 0, comp.regsize); |
| 386 | #endif |
| 387 | |
| 388 | // Second pass: emit code. |
| 389 | comp.regparse = exp; |
| 390 | comp.regnpar = 1; |
| 391 | comp.regcode = this->program; |
| 392 | comp.regc(static_cast<char>(MAGIC)); |
| 393 | comp.reg(0, &flags); |
| 394 | |
| 395 | // Dig out information for optimizations. |