RegularExpression.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. //
  4. // Copyright (C) 1991 Texas Instruments Incorporated.
  5. //
  6. // Permission is granted to any individual or institution to use, copy, modify
  7. // and distribute this software, provided that this complete copyright and
  8. // permission notice is maintained, intact, in all copies and supporting
  9. // documentation.
  10. //
  11. // Texas Instruments Incorporated provides this software "as is" without
  12. // express or implied warranty.
  13. //
  14. //
  15. // Created: MNF 06/13/89 Initial Design and Implementation
  16. // Updated: LGO 08/09/89 Inherit from Generic
  17. // Updated: MBN 09/07/89 Added conditional exception handling
  18. // Updated: MBN 12/15/89 Sprinkled "const" qualifiers all over the place!
  19. // Updated: DLS 03/22/91 New lite version
  20. //
  21. #include "kwsysPrivate.h"
  22. #include KWSYS_HEADER(RegularExpression.hxx)
  23. // Work-around CMake dependency scanning limitation. This must
  24. // duplicate the above list of headers.
  25. #if 0
  26. # include "RegularExpression.hxx.in"
  27. #endif
  28. #include <stdio.h>
  29. #include <string.h>
  30. namespace KWSYS_NAMESPACE {
  31. // RegularExpression -- Copies the given regular expression.
  32. RegularExpression::RegularExpression(const RegularExpression& rxp)
  33. {
  34. if (!rxp.program) {
  35. this->program = KWSYS_NULLPTR;
  36. return;
  37. }
  38. int ind;
  39. this->progsize = rxp.progsize; // Copy regular expression size
  40. this->program = new char[this->progsize]; // Allocate storage
  41. for (ind = this->progsize; ind-- != 0;) // Copy regular expression
  42. this->program[ind] = rxp.program[ind];
  43. // Copy pointers into last successful "find" operation
  44. this->regmatch = rxp.regmatch;
  45. this->regmust = rxp.regmust; // Copy field
  46. if (rxp.regmust != KWSYS_NULLPTR) {
  47. char* dum = rxp.program;
  48. ind = 0;
  49. while (dum != rxp.regmust) {
  50. ++dum;
  51. ++ind;
  52. }
  53. this->regmust = this->program + ind;
  54. }
  55. this->regstart = rxp.regstart; // Copy starting index
  56. this->reganch = rxp.reganch; // Copy remaining private data
  57. this->regmlen = rxp.regmlen; // Copy remaining private data
  58. }
  59. // operator= -- Copies the given regular expression.
  60. RegularExpression& RegularExpression::operator=(const RegularExpression& rxp)
  61. {
  62. if (this == &rxp) {
  63. return *this;
  64. }
  65. if (!rxp.program) {
  66. this->program = KWSYS_NULLPTR;
  67. return *this;
  68. }
  69. int ind;
  70. this->progsize = rxp.progsize; // Copy regular expression size
  71. delete[] this->program;
  72. this->program = new char[this->progsize]; // Allocate storage
  73. for (ind = this->progsize; ind-- != 0;) // Copy regular expression
  74. this->program[ind] = rxp.program[ind];
  75. // Copy pointers into last successful "find" operation
  76. this->regmatch = rxp.regmatch;
  77. this->regmust = rxp.regmust; // Copy field
  78. if (rxp.regmust != KWSYS_NULLPTR) {
  79. char* dum = rxp.program;
  80. ind = 0;
  81. while (dum != rxp.regmust) {
  82. ++dum;
  83. ++ind;
  84. }
  85. this->regmust = this->program + ind;
  86. }
  87. this->regstart = rxp.regstart; // Copy starting index
  88. this->reganch = rxp.reganch; // Copy remaining private data
  89. this->regmlen = rxp.regmlen; // Copy remaining private data
  90. return *this;
  91. }
  92. // operator== -- Returns true if two regular expressions have the same
  93. // compiled program for pattern matching.
  94. bool RegularExpression::operator==(const RegularExpression& rxp) const
  95. {
  96. if (this != &rxp) { // Same address?
  97. int ind = this->progsize; // Get regular expression size
  98. if (ind != rxp.progsize) // If different size regexp
  99. return false; // Return failure
  100. while (ind-- != 0) // Else while still characters
  101. if (this->program[ind] != rxp.program[ind]) // If regexp are different
  102. return false; // Return failure
  103. }
  104. return true; // Else same, return success
  105. }
  106. // deep_equal -- Returns true if have the same compiled regular expressions
  107. // and the same start and end pointers.
  108. bool RegularExpression::deep_equal(const RegularExpression& rxp) const
  109. {
  110. int ind = this->progsize; // Get regular expression size
  111. if (ind != rxp.progsize) // If different size regexp
  112. return false; // Return failure
  113. while (ind-- != 0) // Else while still characters
  114. if (this->program[ind] != rxp.program[ind]) // If regexp are different
  115. return false; // Return failure
  116. // Else if same start/end ptrs, return true
  117. return (this->regmatch.start() == rxp.regmatch.start() &&
  118. this->regmatch.end() == rxp.regmatch.end());
  119. }
  120. // The remaining code in this file is derived from the regular expression code
  121. // whose copyright statement appears below. It has been changed to work
  122. // with the class concepts of C++ and COOL.
  123. /*
  124. * compile and find
  125. *
  126. * Copyright (c) 1986 by University of Toronto.
  127. * Written by Henry Spencer. Not derived from licensed software.
  128. *
  129. * Permission is granted to anyone to use this software for any
  130. * purpose on any computer system, and to redistribute it freely,
  131. * subject to the following restrictions:
  132. *
  133. * 1. The author is not responsible for the consequences of use of
  134. * this software, no matter how awful, even if they arise
  135. * from defects in it.
  136. *
  137. * 2. The origin of this software must not be misrepresented, either
  138. * by explicit claim or by omission.
  139. *
  140. * 3. Altered versions must be plainly marked as such, and must not
  141. * be misrepresented as being the original software.
  142. *
  143. * Beware that some of this code is subtly aware of the way operator
  144. * precedence is structured in regular expressions. Serious changes in
  145. * regular-expression syntax might require a total rethink.
  146. */
  147. /*
  148. * The "internal use only" fields in regexp.h are present to pass info from
  149. * compile to execute that permits the execute phase to run lots faster on
  150. * simple cases. They are:
  151. *
  152. * regstart char that must begin a match; '\0' if none obvious
  153. * reganch is the match anchored (at beginning-of-line only)?
  154. * regmust string (pointer into program) that match must include, or NULL
  155. * regmlen length of regmust string
  156. *
  157. * Regstart and reganch permit very fast decisions on suitable starting points
  158. * for a match, cutting down the work a lot. Regmust permits fast rejection
  159. * of lines that cannot possibly match. The regmust tests are costly enough
  160. * that compile() supplies a regmust only if the r.e. contains something
  161. * potentially expensive (at present, the only such thing detected is * or +
  162. * at the start of the r.e., which can involve a lot of backup). Regmlen is
  163. * supplied because the test in find() needs it and compile() is computing
  164. * it anyway.
  165. */
  166. /*
  167. * Structure for regexp "program". This is essentially a linear encoding
  168. * of a nondeterministic finite-state machine (aka syntax charts or
  169. * "railroad normal form" in parsing technology). Each node is an opcode
  170. * plus a "next" pointer, possibly plus an operand. "Next" pointers of
  171. * all nodes except BRANCH implement concatenation; a "next" pointer with
  172. * a BRANCH on both ends of it is connecting two alternatives. (Here we
  173. * have one of the subtle syntax dependencies: an individual BRANCH (as
  174. * opposed to a collection of them) is never concatenated with anything
  175. * because of operator precedence.) The operand of some types of node is
  176. * a literal string; for others, it is a node leading into a sub-FSM. In
  177. * particular, the operand of a BRANCH node is the first node of the branch.
  178. * (NB this is *not* a tree structure: the tail of the branch connects
  179. * to the thing following the set of BRANCHes.) The opcodes are:
  180. */
  181. // definition number opnd? meaning
  182. #define END 0 // no End of program.
  183. #define BOL 1 // no Match "" at beginning of line.
  184. #define EOL 2 // no Match "" at end of line.
  185. #define ANY 3 // no Match any one character.
  186. #define ANYOF 4 // str Match any character in this string.
  187. #define ANYBUT \
  188. 5 // str Match any character not in this
  189. // string.
  190. #define BRANCH \
  191. 6 // node Match this alternative, or the
  192. // next...
  193. #define BACK 7 // no Match "", "next" ptr points backward.
  194. #define EXACTLY 8 // str Match this string.
  195. #define NOTHING 9 // no Match empty string.
  196. #define STAR \
  197. 10 // node Match this (simple) thing 0 or more
  198. // times.
  199. #define PLUS \
  200. 11 // node Match this (simple) thing 1 or more
  201. // times.
  202. #define OPEN \
  203. 20 // no Mark this point in input as start of
  204. // #n.
  205. // OPEN+1 is number 1, etc.
  206. #define CLOSE 30 // no Analogous to OPEN.
  207. /*
  208. * Opcode notes:
  209. *
  210. * BRANCH The set of branches constituting a single choice are hooked
  211. * together with their "next" pointers, since precedence prevents
  212. * anything being concatenated to any individual branch. The
  213. * "next" pointer of the last BRANCH in a choice points to the
  214. * thing following the whole choice. This is also where the
  215. * final "next" pointer of each individual branch points; each
  216. * branch starts with the operand node of a BRANCH node.
  217. *
  218. * BACK Normal "next" pointers all implicitly point forward; BACK
  219. * exists to make loop structures possible.
  220. *
  221. * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
  222. * BRANCH structures using BACK. Simple cases (one character
  223. * per match) are implemented with STAR and PLUS for speed
  224. * and to minimize recursive plunges.
  225. *
  226. * OPEN,CLOSE ...are numbered at compile time.
  227. */
  228. /*
  229. * A node is one char of opcode followed by two chars of "next" pointer.
  230. * "Next" pointers are stored as two 8-bit pieces, high order first. The
  231. * value is a positive offset from the opcode of the node containing it.
  232. * An operand, if any, simply follows the node. (Note that much of the
  233. * code generation knows about this implicit relationship.)
  234. *
  235. * Using two bytes for the "next" pointer is vast overkill for most things,
  236. * but allows patterns to get big without disasters.
  237. */
  238. #define OP(p) (*(p))
  239. #define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
  240. #define OPERAND(p) ((p) + 3)
  241. const unsigned char MAGIC = 0234;
  242. /*
  243. * Utility definitions.
  244. */
  245. #define UCHARAT(p) (reinterpret_cast<const unsigned char*>(p))[0]
  246. #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
  247. #define META "^$.[()|?+*\\"
  248. /*
  249. * Flags to be passed up and down.
  250. */
  251. #define HASWIDTH 01 // Known never to match null string.
  252. #define SIMPLE 02 // Simple enough to be STAR/PLUS operand.
  253. #define SPSTART 04 // Starts with * or +.
  254. #define WORST 0 // Worst case.
  255. /////////////////////////////////////////////////////////////////////////
  256. //
  257. // COMPILE AND ASSOCIATED FUNCTIONS
  258. //
  259. /////////////////////////////////////////////////////////////////////////
  260. /*
  261. * Read only utility variables.
  262. */
  263. static char regdummy;
  264. static char* const regdummyptr = &regdummy;
  265. /*
  266. * Utility class for RegularExpression::compile().
  267. */
  268. class RegExpCompile
  269. {
  270. public:
  271. const char* regparse; // Input-scan pointer.
  272. int regnpar; // () count.
  273. char* regcode; // Code-emit pointer; regdummyptr = don't.
  274. long regsize; // Code size.
  275. char* reg(int, int*);
  276. char* regbranch(int*);
  277. char* regpiece(int*);
  278. char* regatom(int*);
  279. char* regnode(char);
  280. void regc(char);
  281. void reginsert(char, char*);
  282. static void regtail(char*, const char*);
  283. static void regoptail(char*, const char*);
  284. };
  285. static const char* regnext(const char*);
  286. static char* regnext(char*);
  287. #ifdef STRCSPN
  288. static int strcspn();
  289. #endif
  290. /*
  291. * We can't allocate space until we know how big the compiled form will be,
  292. * but we can't compile it (and thus know how big it is) until we've got a
  293. * place to put the code. So we cheat: we compile it twice, once with code
  294. * generation turned off and size counting turned on, and once "for real".
  295. * This also means that we don't allocate space until we are sure that the
  296. * thing really will compile successfully, and we never have to move the
  297. * code and thus invalidate pointers into it. (Note that it has to be in
  298. * one piece because free() must be able to free it all.)
  299. *
  300. * Beware that the optimization-preparation code in here knows about some
  301. * of the structure of the compiled regexp.
  302. */
  303. // compile -- compile a regular expression into internal code
  304. // for later pattern matching.
  305. bool RegularExpression::compile(const char* exp)
  306. {
  307. const char* scan;
  308. const char* longest;
  309. size_t len;
  310. int flags;
  311. if (exp == KWSYS_NULLPTR) {
  312. // RAISE Error, SYM(RegularExpression), SYM(No_Expr),
  313. printf("RegularExpression::compile(): No expression supplied.\n");
  314. return false;
  315. }
  316. // First pass: determine size, legality.
  317. RegExpCompile comp;
  318. comp.regparse = exp;
  319. comp.regnpar = 1;
  320. comp.regsize = 0L;
  321. comp.regcode = regdummyptr;
  322. comp.regc(static_cast<char>(MAGIC));
  323. if (!comp.reg(0, &flags)) {
  324. printf("RegularExpression::compile(): Error in compile.\n");
  325. return false;
  326. }
  327. this->regmatch.clear();
  328. // Small enough for pointer-storage convention?
  329. if (comp.regsize >= 32767L) { // Probably could be 65535L.
  330. // RAISE Error, SYM(RegularExpression), SYM(Expr_Too_Big),
  331. printf("RegularExpression::compile(): Expression too big.\n");
  332. return false;
  333. }
  334. // Allocate space.
  335. //#ifndef _WIN32
  336. if (this->program != KWSYS_NULLPTR)
  337. delete[] this->program;
  338. //#endif
  339. this->program = new char[comp.regsize];
  340. this->progsize = static_cast<int>(comp.regsize);
  341. if (this->program == KWSYS_NULLPTR) {
  342. // RAISE Error, SYM(RegularExpression), SYM(Out_Of_Memory),
  343. printf("RegularExpression::compile(): Out of memory.\n");
  344. return false;
  345. }
  346. // Second pass: emit code.
  347. comp.regparse = exp;
  348. comp.regnpar = 1;
  349. comp.regcode = this->program;
  350. comp.regc(static_cast<char>(MAGIC));
  351. comp.reg(0, &flags);
  352. // Dig out information for optimizations.
  353. this->regstart = '\0'; // Worst-case defaults.
  354. this->reganch = 0;
  355. this->regmust = KWSYS_NULLPTR;
  356. this->regmlen = 0;
  357. scan = this->program + 1; // First BRANCH.
  358. if (OP(regnext(scan)) == END) { // Only one top-level choice.
  359. scan = OPERAND(scan);
  360. // Starting-point info.
  361. if (OP(scan) == EXACTLY)
  362. this->regstart = *OPERAND(scan);
  363. else if (OP(scan) == BOL)
  364. this->reganch++;
  365. //
  366. // If there's something expensive in the r.e., find the longest
  367. // literal string that must appear and make it the regmust. Resolve
  368. // ties in favor of later strings, since the regstart check works
  369. // with the beginning of the r.e. and avoiding duplication
  370. // strengthens checking. Not a strong reason, but sufficient in the
  371. // absence of others.
  372. //
  373. if (flags & SPSTART) {
  374. longest = KWSYS_NULLPTR;
  375. len = 0;
  376. for (; scan != KWSYS_NULLPTR; scan = regnext(scan))
  377. if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
  378. longest = OPERAND(scan);
  379. len = strlen(OPERAND(scan));
  380. }
  381. this->regmust = longest;
  382. this->regmlen = len;
  383. }
  384. }
  385. return true;
  386. }
  387. /*
  388. - reg - regular expression, i.e. main body or parenthesized thing
  389. *
  390. * Caller must absorb opening parenthesis.
  391. *
  392. * Combining parenthesis handling with the base level of regular expression
  393. * is a trifle forced, but the need to tie the tails of the branches to what
  394. * follows makes it hard to avoid.
  395. */
  396. char* RegExpCompile::reg(int paren, int* flagp)
  397. {
  398. char* ret;
  399. char* br;
  400. char* ender;
  401. int parno = 0;
  402. int flags;
  403. *flagp = HASWIDTH; // Tentatively.
  404. // Make an OPEN node, if parenthesized.
  405. if (paren) {
  406. if (regnpar >= RegularExpressionMatch::NSUBEXP) {
  407. // RAISE Error, SYM(RegularExpression), SYM(Too_Many_Parens),
  408. printf("RegularExpression::compile(): Too many parentheses.\n");
  409. return KWSYS_NULLPTR;
  410. }
  411. parno = regnpar;
  412. regnpar++;
  413. ret = regnode(static_cast<char>(OPEN + parno));
  414. } else
  415. ret = KWSYS_NULLPTR;
  416. // Pick up the branches, linking them together.
  417. br = regbranch(&flags);
  418. if (br == KWSYS_NULLPTR)
  419. return (KWSYS_NULLPTR);
  420. if (ret != KWSYS_NULLPTR)
  421. regtail(ret, br); // OPEN -> first.
  422. else
  423. ret = br;
  424. if (!(flags & HASWIDTH))
  425. *flagp &= ~HASWIDTH;
  426. *flagp |= flags & SPSTART;
  427. while (*regparse == '|') {
  428. regparse++;
  429. br = regbranch(&flags);
  430. if (br == KWSYS_NULLPTR)
  431. return (KWSYS_NULLPTR);
  432. regtail(ret, br); // BRANCH -> BRANCH.
  433. if (!(flags & HASWIDTH))
  434. *flagp &= ~HASWIDTH;
  435. *flagp |= flags & SPSTART;
  436. }
  437. // Make a closing node, and hook it on the end.
  438. ender = regnode(static_cast<char>((paren) ? CLOSE + parno : END));
  439. regtail(ret, ender);
  440. // Hook the tails of the branches to the closing node.
  441. for (br = ret; br != KWSYS_NULLPTR; br = regnext(br))
  442. regoptail(br, ender);
  443. // Check for proper termination.
  444. if (paren && *regparse++ != ')') {
  445. // RAISE Error, SYM(RegularExpression), SYM(Unmatched_Parens),
  446. printf("RegularExpression::compile(): Unmatched parentheses.\n");
  447. return KWSYS_NULLPTR;
  448. } else if (!paren && *regparse != '\0') {
  449. if (*regparse == ')') {
  450. // RAISE Error, SYM(RegularExpression), SYM(Unmatched_Parens),
  451. printf("RegularExpression::compile(): Unmatched parentheses.\n");
  452. return KWSYS_NULLPTR;
  453. } else {
  454. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  455. printf("RegularExpression::compile(): Internal error.\n");
  456. return KWSYS_NULLPTR;
  457. }
  458. // NOTREACHED
  459. }
  460. return (ret);
  461. }
  462. /*
  463. - regbranch - one alternative of an | operator
  464. *
  465. * Implements the concatenation operator.
  466. */
  467. char* RegExpCompile::regbranch(int* flagp)
  468. {
  469. char* ret;
  470. char* chain;
  471. char* latest;
  472. int flags;
  473. *flagp = WORST; // Tentatively.
  474. ret = regnode(BRANCH);
  475. chain = KWSYS_NULLPTR;
  476. while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
  477. latest = regpiece(&flags);
  478. if (latest == KWSYS_NULLPTR)
  479. return (KWSYS_NULLPTR);
  480. *flagp |= flags & HASWIDTH;
  481. if (chain == KWSYS_NULLPTR) // First piece.
  482. *flagp |= flags & SPSTART;
  483. else
  484. regtail(chain, latest);
  485. chain = latest;
  486. }
  487. if (chain == KWSYS_NULLPTR) // Loop ran zero times.
  488. regnode(NOTHING);
  489. return (ret);
  490. }
  491. /*
  492. - regpiece - something followed by possible [*+?]
  493. *
  494. * Note that the branching code sequences used for ? and the general cases
  495. * of * and + are somewhat optimized: they use the same NOTHING node as
  496. * both the endmarker for their branch list and the body of the last branch.
  497. * It might seem that this node could be dispensed with entirely, but the
  498. * endmarker role is not redundant.
  499. */
  500. char* RegExpCompile::regpiece(int* flagp)
  501. {
  502. char* ret;
  503. char op;
  504. char* next;
  505. int flags;
  506. ret = regatom(&flags);
  507. if (ret == KWSYS_NULLPTR)
  508. return (KWSYS_NULLPTR);
  509. op = *regparse;
  510. if (!ISMULT(op)) {
  511. *flagp = flags;
  512. return (ret);
  513. }
  514. if (!(flags & HASWIDTH) && op != '?') {
  515. // RAISE Error, SYM(RegularExpression), SYM(Empty_Operand),
  516. printf("RegularExpression::compile() : *+ operand could be empty.\n");
  517. return KWSYS_NULLPTR;
  518. }
  519. *flagp = (op != '+') ? (WORST | SPSTART) : (WORST | HASWIDTH);
  520. if (op == '*' && (flags & SIMPLE))
  521. reginsert(STAR, ret);
  522. else if (op == '*') {
  523. // Emit x* as (x&|), where & means "self".
  524. reginsert(BRANCH, ret); // Either x
  525. regoptail(ret, regnode(BACK)); // and loop
  526. regoptail(ret, ret); // back
  527. regtail(ret, regnode(BRANCH)); // or
  528. regtail(ret, regnode(NOTHING)); // null.
  529. } else if (op == '+' && (flags & SIMPLE))
  530. reginsert(PLUS, ret);
  531. else if (op == '+') {
  532. // Emit x+ as x(&|), where & means "self".
  533. next = regnode(BRANCH); // Either
  534. regtail(ret, next);
  535. regtail(regnode(BACK), ret); // loop back
  536. regtail(next, regnode(BRANCH)); // or
  537. regtail(ret, regnode(NOTHING)); // null.
  538. } else if (op == '?') {
  539. // Emit x? as (x|)
  540. reginsert(BRANCH, ret); // Either x
  541. regtail(ret, regnode(BRANCH)); // or
  542. next = regnode(NOTHING); // null.
  543. regtail(ret, next);
  544. regoptail(ret, next);
  545. }
  546. regparse++;
  547. if (ISMULT(*regparse)) {
  548. // RAISE Error, SYM(RegularExpression), SYM(Nested_Operand),
  549. printf("RegularExpression::compile(): Nested *?+.\n");
  550. return KWSYS_NULLPTR;
  551. }
  552. return (ret);
  553. }
  554. /*
  555. - regatom - the lowest level
  556. *
  557. * Optimization: gobbles an entire sequence of ordinary characters so that
  558. * it can turn them into a single node, which is smaller to store and
  559. * faster to run. Backslashed characters are exceptions, each becoming a
  560. * separate node; the code is simpler that way and it's not worth fixing.
  561. */
  562. char* RegExpCompile::regatom(int* flagp)
  563. {
  564. char* ret;
  565. int flags;
  566. *flagp = WORST; // Tentatively.
  567. switch (*regparse++) {
  568. case '^':
  569. ret = regnode(BOL);
  570. break;
  571. case '$':
  572. ret = regnode(EOL);
  573. break;
  574. case '.':
  575. ret = regnode(ANY);
  576. *flagp |= HASWIDTH | SIMPLE;
  577. break;
  578. case '[': {
  579. int rxpclass;
  580. int rxpclassend;
  581. if (*regparse == '^') { // Complement of range.
  582. ret = regnode(ANYBUT);
  583. regparse++;
  584. } else
  585. ret = regnode(ANYOF);
  586. if (*regparse == ']' || *regparse == '-')
  587. regc(*regparse++);
  588. while (*regparse != '\0' && *regparse != ']') {
  589. if (*regparse == '-') {
  590. regparse++;
  591. if (*regparse == ']' || *regparse == '\0')
  592. regc('-');
  593. else {
  594. rxpclass = UCHARAT(regparse - 2) + 1;
  595. rxpclassend = UCHARAT(regparse);
  596. if (rxpclass > rxpclassend + 1) {
  597. // RAISE Error, SYM(RegularExpression), SYM(Invalid_Range),
  598. printf("RegularExpression::compile(): Invalid range in [].\n");
  599. return KWSYS_NULLPTR;
  600. }
  601. for (; rxpclass <= rxpclassend; rxpclass++)
  602. regc(static_cast<char>(rxpclass));
  603. regparse++;
  604. }
  605. } else
  606. regc(*regparse++);
  607. }
  608. regc('\0');
  609. if (*regparse != ']') {
  610. // RAISE Error, SYM(RegularExpression), SYM(Unmatched_Bracket),
  611. printf("RegularExpression::compile(): Unmatched [].\n");
  612. return KWSYS_NULLPTR;
  613. }
  614. regparse++;
  615. *flagp |= HASWIDTH | SIMPLE;
  616. } break;
  617. case '(':
  618. ret = reg(1, &flags);
  619. if (ret == KWSYS_NULLPTR)
  620. return (KWSYS_NULLPTR);
  621. *flagp |= flags & (HASWIDTH | SPSTART);
  622. break;
  623. case '\0':
  624. case '|':
  625. case ')':
  626. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  627. printf("RegularExpression::compile(): Internal error.\n"); // Never here
  628. return KWSYS_NULLPTR;
  629. case '?':
  630. case '+':
  631. case '*':
  632. // RAISE Error, SYM(RegularExpression), SYM(No_Operand),
  633. printf("RegularExpression::compile(): ?+* follows nothing.\n");
  634. return KWSYS_NULLPTR;
  635. case '\\':
  636. if (*regparse == '\0') {
  637. // RAISE Error, SYM(RegularExpression), SYM(Trailing_Backslash),
  638. printf("RegularExpression::compile(): Trailing backslash.\n");
  639. return KWSYS_NULLPTR;
  640. }
  641. ret = regnode(EXACTLY);
  642. regc(*regparse++);
  643. regc('\0');
  644. *flagp |= HASWIDTH | SIMPLE;
  645. break;
  646. default: {
  647. int len;
  648. char ender;
  649. regparse--;
  650. len = int(strcspn(regparse, META));
  651. if (len <= 0) {
  652. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  653. printf("RegularExpression::compile(): Internal error.\n");
  654. return KWSYS_NULLPTR;
  655. }
  656. ender = *(regparse + len);
  657. if (len > 1 && ISMULT(ender))
  658. len--; // Back off clear of ?+* operand.
  659. *flagp |= HASWIDTH;
  660. if (len == 1)
  661. *flagp |= SIMPLE;
  662. ret = regnode(EXACTLY);
  663. while (len > 0) {
  664. regc(*regparse++);
  665. len--;
  666. }
  667. regc('\0');
  668. } break;
  669. }
  670. return (ret);
  671. }
  672. /*
  673. - regnode - emit a node
  674. Location.
  675. */
  676. char* RegExpCompile::regnode(char op)
  677. {
  678. char* ret;
  679. char* ptr;
  680. ret = regcode;
  681. if (ret == regdummyptr) {
  682. regsize += 3;
  683. return (ret);
  684. }
  685. ptr = ret;
  686. *ptr++ = op;
  687. *ptr++ = '\0'; // Null "next" pointer.
  688. *ptr++ = '\0';
  689. regcode = ptr;
  690. return (ret);
  691. }
  692. /*
  693. - regc - emit (if appropriate) a byte of code
  694. */
  695. void RegExpCompile::regc(char b)
  696. {
  697. if (regcode != regdummyptr)
  698. *regcode++ = b;
  699. else
  700. regsize++;
  701. }
  702. /*
  703. - reginsert - insert an operator in front of already-emitted operand
  704. *
  705. * Means relocating the operand.
  706. */
  707. void RegExpCompile::reginsert(char op, char* opnd)
  708. {
  709. char* src;
  710. char* dst;
  711. char* place;
  712. if (regcode == regdummyptr) {
  713. regsize += 3;
  714. return;
  715. }
  716. src = regcode;
  717. regcode += 3;
  718. dst = regcode;
  719. while (src > opnd)
  720. *--dst = *--src;
  721. place = opnd; // Op node, where operand used to be.
  722. *place++ = op;
  723. *place++ = '\0';
  724. *place = '\0';
  725. }
  726. /*
  727. - regtail - set the next-pointer at the end of a node chain
  728. */
  729. void RegExpCompile::regtail(char* p, const char* val)
  730. {
  731. char* scan;
  732. char* temp;
  733. int offset;
  734. if (p == regdummyptr)
  735. return;
  736. // Find last node.
  737. scan = p;
  738. for (;;) {
  739. temp = regnext(scan);
  740. if (temp == KWSYS_NULLPTR)
  741. break;
  742. scan = temp;
  743. }
  744. if (OP(scan) == BACK)
  745. offset = int(scan - val);
  746. else
  747. offset = int(val - scan);
  748. *(scan + 1) = static_cast<char>((offset >> 8) & 0377);
  749. *(scan + 2) = static_cast<char>(offset & 0377);
  750. }
  751. /*
  752. - regoptail - regtail on operand of first argument; nop if operandless
  753. */
  754. void RegExpCompile::regoptail(char* p, const char* val)
  755. {
  756. // "Operandless" and "op != BRANCH" are synonymous in practice.
  757. if (p == KWSYS_NULLPTR || p == regdummyptr || OP(p) != BRANCH)
  758. return;
  759. regtail(OPERAND(p), val);
  760. }
  761. ////////////////////////////////////////////////////////////////////////
  762. //
  763. // find and friends
  764. //
  765. ////////////////////////////////////////////////////////////////////////
  766. /*
  767. * Utility class for RegularExpression::find().
  768. */
  769. class RegExpFind
  770. {
  771. public:
  772. const char* reginput; // String-input pointer.
  773. const char* regbol; // Beginning of input, for ^ check.
  774. const char** regstartp; // Pointer to startp array.
  775. const char** regendp; // Ditto for endp.
  776. int regtry(const char*, const char**, const char**, const char*);
  777. int regmatch(const char*);
  778. int regrepeat(const char*);
  779. };
  780. // find -- Matches the regular expression to the given string.
  781. // Returns true if found, and sets start and end indexes accordingly.
  782. bool RegularExpression::find(char const* string,
  783. RegularExpressionMatch& rmatch) const
  784. {
  785. const char* s;
  786. rmatch.clear();
  787. rmatch.searchstring = string;
  788. if (!this->program) {
  789. return false;
  790. }
  791. // Check validity of program.
  792. if (UCHARAT(this->program) != MAGIC) {
  793. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  794. printf(
  795. "RegularExpression::find(): Compiled regular expression corrupted.\n");
  796. return false;
  797. }
  798. // If there is a "must appear" string, look for it.
  799. if (this->regmust != KWSYS_NULLPTR) {
  800. s = string;
  801. while ((s = strchr(s, this->regmust[0])) != KWSYS_NULLPTR) {
  802. if (strncmp(s, this->regmust, this->regmlen) == 0)
  803. break; // Found it.
  804. s++;
  805. }
  806. if (s == KWSYS_NULLPTR) // Not present.
  807. return false;
  808. }
  809. RegExpFind regFind;
  810. // Mark beginning of line for ^ .
  811. regFind.regbol = string;
  812. // Simplest case: anchored match need be tried only once.
  813. if (this->reganch)
  814. return (
  815. regFind.regtry(string, rmatch.startp, rmatch.endp, this->program) != 0);
  816. // Messy cases: unanchored match.
  817. s = string;
  818. if (this->regstart != '\0')
  819. // We know what char it must start with.
  820. while ((s = strchr(s, this->regstart)) != KWSYS_NULLPTR) {
  821. if (regFind.regtry(s, rmatch.startp, rmatch.endp, this->program))
  822. return true;
  823. s++;
  824. }
  825. else
  826. // We don't -- general case.
  827. do {
  828. if (regFind.regtry(s, rmatch.startp, rmatch.endp, this->program))
  829. return true;
  830. } while (*s++ != '\0');
  831. // Failure.
  832. return false;
  833. }
  834. /*
  835. - regtry - try match at specific point
  836. 0 failure, 1 success
  837. */
  838. int RegExpFind::regtry(const char* string, const char** start,
  839. const char** end, const char* prog)
  840. {
  841. int i;
  842. const char** sp1;
  843. const char** ep;
  844. reginput = string;
  845. regstartp = start;
  846. regendp = end;
  847. sp1 = start;
  848. ep = end;
  849. for (i = RegularExpressionMatch::NSUBEXP; i > 0; i--) {
  850. *sp1++ = KWSYS_NULLPTR;
  851. *ep++ = KWSYS_NULLPTR;
  852. }
  853. if (regmatch(prog + 1)) {
  854. start[0] = string;
  855. end[0] = reginput;
  856. return (1);
  857. } else
  858. return (0);
  859. }
  860. /*
  861. - regmatch - main matching routine
  862. *
  863. * Conceptually the strategy is simple: check to see whether the current
  864. * node matches, call self recursively to see whether the rest matches,
  865. * and then act accordingly. In practice we make some effort to avoid
  866. * recursion, in particular by going through "ordinary" nodes (that don't
  867. * need to know whether the rest of the match failed) by a loop instead of
  868. * by recursion.
  869. * 0 failure, 1 success
  870. */
  871. int RegExpFind::regmatch(const char* prog)
  872. {
  873. const char* scan; // Current node.
  874. const char* next; // Next node.
  875. scan = prog;
  876. while (scan != KWSYS_NULLPTR) {
  877. next = regnext(scan);
  878. switch (OP(scan)) {
  879. case BOL:
  880. if (reginput != regbol)
  881. return (0);
  882. break;
  883. case EOL:
  884. if (*reginput != '\0')
  885. return (0);
  886. break;
  887. case ANY:
  888. if (*reginput == '\0')
  889. return (0);
  890. reginput++;
  891. break;
  892. case EXACTLY: {
  893. size_t len;
  894. const char* opnd;
  895. opnd = OPERAND(scan);
  896. // Inline the first character, for speed.
  897. if (*opnd != *reginput)
  898. return (0);
  899. len = strlen(opnd);
  900. if (len > 1 && strncmp(opnd, reginput, len) != 0)
  901. return (0);
  902. reginput += len;
  903. } break;
  904. case ANYOF:
  905. if (*reginput == '\0' ||
  906. strchr(OPERAND(scan), *reginput) == KWSYS_NULLPTR)
  907. return (0);
  908. reginput++;
  909. break;
  910. case ANYBUT:
  911. if (*reginput == '\0' ||
  912. strchr(OPERAND(scan), *reginput) != KWSYS_NULLPTR)
  913. return (0);
  914. reginput++;
  915. break;
  916. case NOTHING:
  917. break;
  918. case BACK:
  919. break;
  920. case OPEN + 1:
  921. case OPEN + 2:
  922. case OPEN + 3:
  923. case OPEN + 4:
  924. case OPEN + 5:
  925. case OPEN + 6:
  926. case OPEN + 7:
  927. case OPEN + 8:
  928. case OPEN + 9: {
  929. int no;
  930. const char* save;
  931. no = OP(scan) - OPEN;
  932. save = reginput;
  933. if (regmatch(next)) {
  934. //
  935. // Don't set startp if some later invocation of the
  936. // same parentheses already has.
  937. //
  938. if (regstartp[no] == KWSYS_NULLPTR)
  939. regstartp[no] = save;
  940. return (1);
  941. } else
  942. return (0);
  943. }
  944. // break;
  945. case CLOSE + 1:
  946. case CLOSE + 2:
  947. case CLOSE + 3:
  948. case CLOSE + 4:
  949. case CLOSE + 5:
  950. case CLOSE + 6:
  951. case CLOSE + 7:
  952. case CLOSE + 8:
  953. case CLOSE + 9: {
  954. int no;
  955. const char* save;
  956. no = OP(scan) - CLOSE;
  957. save = reginput;
  958. if (regmatch(next)) {
  959. //
  960. // Don't set endp if some later invocation of the
  961. // same parentheses already has.
  962. //
  963. if (regendp[no] == KWSYS_NULLPTR)
  964. regendp[no] = save;
  965. return (1);
  966. } else
  967. return (0);
  968. }
  969. // break;
  970. case BRANCH: {
  971. const char* save;
  972. if (OP(next) != BRANCH) // No choice.
  973. next = OPERAND(scan); // Avoid recursion.
  974. else {
  975. do {
  976. save = reginput;
  977. if (regmatch(OPERAND(scan)))
  978. return (1);
  979. reginput = save;
  980. scan = regnext(scan);
  981. } while (scan != KWSYS_NULLPTR && OP(scan) == BRANCH);
  982. return (0);
  983. // NOTREACHED
  984. }
  985. } break;
  986. case STAR:
  987. case PLUS: {
  988. char nextch;
  989. int no;
  990. const char* save;
  991. int min_no;
  992. //
  993. // Lookahead to avoid useless match attempts when we know
  994. // what character comes next.
  995. //
  996. nextch = '\0';
  997. if (OP(next) == EXACTLY)
  998. nextch = *OPERAND(next);
  999. min_no = (OP(scan) == STAR) ? 0 : 1;
  1000. save = reginput;
  1001. no = regrepeat(OPERAND(scan));
  1002. while (no >= min_no) {
  1003. // If it could work, try it.
  1004. if (nextch == '\0' || *reginput == nextch)
  1005. if (regmatch(next))
  1006. return (1);
  1007. // Couldn't or didn't -- back up.
  1008. no--;
  1009. reginput = save + no;
  1010. }
  1011. return (0);
  1012. }
  1013. // break;
  1014. case END:
  1015. return (1); // Success!
  1016. default:
  1017. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  1018. printf(
  1019. "RegularExpression::find(): Internal error -- memory corrupted.\n");
  1020. return 0;
  1021. }
  1022. scan = next;
  1023. }
  1024. //
  1025. // We get here only if there's trouble -- normally "case END" is the
  1026. // terminating point.
  1027. //
  1028. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  1029. printf("RegularExpression::find(): Internal error -- corrupted pointers.\n");
  1030. return (0);
  1031. }
  1032. /*
  1033. - regrepeat - repeatedly match something simple, report how many
  1034. */
  1035. int RegExpFind::regrepeat(const char* p)
  1036. {
  1037. int count = 0;
  1038. const char* scan;
  1039. const char* opnd;
  1040. scan = reginput;
  1041. opnd = OPERAND(p);
  1042. switch (OP(p)) {
  1043. case ANY:
  1044. count = int(strlen(scan));
  1045. scan += count;
  1046. break;
  1047. case EXACTLY:
  1048. while (*opnd == *scan) {
  1049. count++;
  1050. scan++;
  1051. }
  1052. break;
  1053. case ANYOF:
  1054. while (*scan != '\0' && strchr(opnd, *scan) != KWSYS_NULLPTR) {
  1055. count++;
  1056. scan++;
  1057. }
  1058. break;
  1059. case ANYBUT:
  1060. while (*scan != '\0' && strchr(opnd, *scan) == KWSYS_NULLPTR) {
  1061. count++;
  1062. scan++;
  1063. }
  1064. break;
  1065. default: // Oh dear. Called inappropriately.
  1066. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  1067. printf("cm RegularExpression::find(): Internal error.\n");
  1068. return 0;
  1069. }
  1070. reginput = scan;
  1071. return (count);
  1072. }
  1073. /*
  1074. - regnext - dig the "next" pointer out of a node
  1075. */
  1076. static const char* regnext(const char* p)
  1077. {
  1078. int offset;
  1079. if (p == regdummyptr)
  1080. return (KWSYS_NULLPTR);
  1081. offset = NEXT(p);
  1082. if (offset == 0)
  1083. return (KWSYS_NULLPTR);
  1084. if (OP(p) == BACK)
  1085. return (p - offset);
  1086. else
  1087. return (p + offset);
  1088. }
  1089. static char* regnext(char* p)
  1090. {
  1091. int offset;
  1092. if (p == regdummyptr)
  1093. return (KWSYS_NULLPTR);
  1094. offset = NEXT(p);
  1095. if (offset == 0)
  1096. return (KWSYS_NULLPTR);
  1097. if (OP(p) == BACK)
  1098. return (p - offset);
  1099. else
  1100. return (p + offset);
  1101. }
  1102. } // namespace KWSYS_NAMESPACE