RegularExpression.cxx 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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 5 // str Match any character not in this
  188. // string.
  189. #define BRANCH 6 // node Match this alternative, or the
  190. // next...
  191. #define BACK 7 // no Match "", "next" ptr points backward.
  192. #define EXACTLY 8 // str Match this string.
  193. #define NOTHING 9 // no Match empty string.
  194. #define STAR 10 // node Match this (simple) thing 0 or more
  195. // times.
  196. #define PLUS 11 // node Match this (simple) thing 1 or more
  197. // times.
  198. #define OPEN 20 // no Mark this point in input as start of
  199. // #n.
  200. // OPEN+1 is number 1, etc.
  201. #define CLOSE 30 // no Analogous to OPEN.
  202. /*
  203. * Opcode notes:
  204. *
  205. * BRANCH The set of branches constituting a single choice are hooked
  206. * together with their "next" pointers, since precedence prevents
  207. * anything being concatenated to any individual branch. The
  208. * "next" pointer of the last BRANCH in a choice points to the
  209. * thing following the whole choice. This is also where the
  210. * final "next" pointer of each individual branch points; each
  211. * branch starts with the operand node of a BRANCH node.
  212. *
  213. * BACK Normal "next" pointers all implicitly point forward; BACK
  214. * exists to make loop structures possible.
  215. *
  216. * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
  217. * BRANCH structures using BACK. Simple cases (one character
  218. * per match) are implemented with STAR and PLUS for speed
  219. * and to minimize recursive plunges.
  220. *
  221. * OPEN,CLOSE ...are numbered at compile time.
  222. */
  223. /*
  224. * A node is one char of opcode followed by two chars of "next" pointer.
  225. * "Next" pointers are stored as two 8-bit pieces, high order first. The
  226. * value is a positive offset from the opcode of the node containing it.
  227. * An operand, if any, simply follows the node. (Note that much of the
  228. * code generation knows about this implicit relationship.)
  229. *
  230. * Using two bytes for the "next" pointer is vast overkill for most things,
  231. * but allows patterns to get big without disasters.
  232. */
  233. #define OP(p) (*(p))
  234. #define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
  235. #define OPERAND(p) ((p) + 3)
  236. const unsigned char MAGIC = 0234;
  237. /*
  238. * Utility definitions.
  239. */
  240. #define UCHARAT(p) (reinterpret_cast<const unsigned char*>(p))[0]
  241. #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
  242. #define META "^$.[()|?+*\\"
  243. /*
  244. * Flags to be passed up and down.
  245. */
  246. #define HASWIDTH 01 // Known never to match null string.
  247. #define SIMPLE 02 // Simple enough to be STAR/PLUS operand.
  248. #define SPSTART 04 // Starts with * or +.
  249. #define WORST 0 // Worst case.
  250. /////////////////////////////////////////////////////////////////////////
  251. //
  252. // COMPILE AND ASSOCIATED FUNCTIONS
  253. //
  254. /////////////////////////////////////////////////////////////////////////
  255. /*
  256. * Read only utility variables.
  257. */
  258. static char regdummy;
  259. static char* const regdummyptr = &regdummy;
  260. /*
  261. * Utility class for RegularExpression::compile().
  262. */
  263. class RegExpCompile
  264. {
  265. public:
  266. const char* regparse; // Input-scan pointer.
  267. int regnpar; // () count.
  268. char* regcode; // Code-emit pointer; regdummyptr = don't.
  269. long regsize; // Code size.
  270. char* reg(int, int*);
  271. char* regbranch(int*);
  272. char* regpiece(int*);
  273. char* regatom(int*);
  274. char* regnode(char);
  275. void regc(char);
  276. void reginsert(char, char*);
  277. static void regtail(char*, const char*);
  278. static void regoptail(char*, const char*);
  279. };
  280. static const char* regnext(const char*);
  281. static char* regnext(char*);
  282. #ifdef STRCSPN
  283. static int strcspn();
  284. #endif
  285. /*
  286. * We can't allocate space until we know how big the compiled form will be,
  287. * but we can't compile it (and thus know how big it is) until we've got a
  288. * place to put the code. So we cheat: we compile it twice, once with code
  289. * generation turned off and size counting turned on, and once "for real".
  290. * This also means that we don't allocate space until we are sure that the
  291. * thing really will compile successfully, and we never have to move the
  292. * code and thus invalidate pointers into it. (Note that it has to be in
  293. * one piece because free() must be able to free it all.)
  294. *
  295. * Beware that the optimization-preparation code in here knows about some
  296. * of the structure of the compiled regexp.
  297. */
  298. // compile -- compile a regular expression into internal code
  299. // for later pattern matching.
  300. bool RegularExpression::compile(const char* exp)
  301. {
  302. const char* scan;
  303. const char* longest;
  304. size_t len;
  305. int flags;
  306. if (exp == KWSYS_NULLPTR) {
  307. // RAISE Error, SYM(RegularExpression), SYM(No_Expr),
  308. printf("RegularExpression::compile(): No expression supplied.\n");
  309. return false;
  310. }
  311. // First pass: determine size, legality.
  312. RegExpCompile comp;
  313. comp.regparse = exp;
  314. comp.regnpar = 1;
  315. comp.regsize = 0L;
  316. comp.regcode = regdummyptr;
  317. comp.regc(static_cast<char>(MAGIC));
  318. if (!comp.reg(0, &flags)) {
  319. printf("RegularExpression::compile(): Error in compile.\n");
  320. return false;
  321. }
  322. this->regmatch.clear();
  323. // Small enough for pointer-storage convention?
  324. if (comp.regsize >= 32767L) { // Probably could be 65535L.
  325. // RAISE Error, SYM(RegularExpression), SYM(Expr_Too_Big),
  326. printf("RegularExpression::compile(): Expression too big.\n");
  327. return false;
  328. }
  329. // Allocate space.
  330. //#ifndef _WIN32
  331. if (this->program != KWSYS_NULLPTR)
  332. delete[] this->program;
  333. //#endif
  334. this->program = new char[comp.regsize];
  335. this->progsize = static_cast<int>(comp.regsize);
  336. if (this->program == KWSYS_NULLPTR) {
  337. // RAISE Error, SYM(RegularExpression), SYM(Out_Of_Memory),
  338. printf("RegularExpression::compile(): Out of memory.\n");
  339. return false;
  340. }
  341. // Second pass: emit code.
  342. comp.regparse = exp;
  343. comp.regnpar = 1;
  344. comp.regcode = this->program;
  345. comp.regc(static_cast<char>(MAGIC));
  346. comp.reg(0, &flags);
  347. // Dig out information for optimizations.
  348. this->regstart = '\0'; // Worst-case defaults.
  349. this->reganch = 0;
  350. this->regmust = KWSYS_NULLPTR;
  351. this->regmlen = 0;
  352. scan = this->program + 1; // First BRANCH.
  353. if (OP(regnext(scan)) == END) { // Only one top-level choice.
  354. scan = OPERAND(scan);
  355. // Starting-point info.
  356. if (OP(scan) == EXACTLY)
  357. this->regstart = *OPERAND(scan);
  358. else if (OP(scan) == BOL)
  359. this->reganch++;
  360. //
  361. // If there's something expensive in the r.e., find the longest
  362. // literal string that must appear and make it the regmust. Resolve
  363. // ties in favor of later strings, since the regstart check works
  364. // with the beginning of the r.e. and avoiding duplication
  365. // strengthens checking. Not a strong reason, but sufficient in the
  366. // absence of others.
  367. //
  368. if (flags & SPSTART) {
  369. longest = KWSYS_NULLPTR;
  370. len = 0;
  371. for (; scan != KWSYS_NULLPTR; scan = regnext(scan))
  372. if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
  373. longest = OPERAND(scan);
  374. len = strlen(OPERAND(scan));
  375. }
  376. this->regmust = longest;
  377. this->regmlen = len;
  378. }
  379. }
  380. return true;
  381. }
  382. /*
  383. - reg - regular expression, i.e. main body or parenthesized thing
  384. *
  385. * Caller must absorb opening parenthesis.
  386. *
  387. * Combining parenthesis handling with the base level of regular expression
  388. * is a trifle forced, but the need to tie the tails of the branches to what
  389. * follows makes it hard to avoid.
  390. */
  391. char* RegExpCompile::reg(int paren, int* flagp)
  392. {
  393. char* ret;
  394. char* br;
  395. char* ender;
  396. int parno = 0;
  397. int flags;
  398. *flagp = HASWIDTH; // Tentatively.
  399. // Make an OPEN node, if parenthesized.
  400. if (paren) {
  401. if (regnpar >= RegularExpressionMatch::NSUBEXP) {
  402. // RAISE Error, SYM(RegularExpression), SYM(Too_Many_Parens),
  403. printf("RegularExpression::compile(): Too many parentheses.\n");
  404. return KWSYS_NULLPTR;
  405. }
  406. parno = regnpar;
  407. regnpar++;
  408. ret = regnode(static_cast<char>(OPEN + parno));
  409. } else
  410. ret = KWSYS_NULLPTR;
  411. // Pick up the branches, linking them together.
  412. br = regbranch(&flags);
  413. if (br == KWSYS_NULLPTR)
  414. return (KWSYS_NULLPTR);
  415. if (ret != KWSYS_NULLPTR)
  416. regtail(ret, br); // OPEN -> first.
  417. else
  418. ret = br;
  419. if (!(flags & HASWIDTH))
  420. *flagp &= ~HASWIDTH;
  421. *flagp |= flags & SPSTART;
  422. while (*regparse == '|') {
  423. regparse++;
  424. br = regbranch(&flags);
  425. if (br == KWSYS_NULLPTR)
  426. return (KWSYS_NULLPTR);
  427. regtail(ret, br); // BRANCH -> BRANCH.
  428. if (!(flags & HASWIDTH))
  429. *flagp &= ~HASWIDTH;
  430. *flagp |= flags & SPSTART;
  431. }
  432. // Make a closing node, and hook it on the end.
  433. ender = regnode(static_cast<char>((paren) ? CLOSE + parno : END));
  434. regtail(ret, ender);
  435. // Hook the tails of the branches to the closing node.
  436. for (br = ret; br != KWSYS_NULLPTR; br = regnext(br))
  437. regoptail(br, ender);
  438. // Check for proper termination.
  439. if (paren && *regparse++ != ')') {
  440. // RAISE Error, SYM(RegularExpression), SYM(Unmatched_Parens),
  441. printf("RegularExpression::compile(): Unmatched parentheses.\n");
  442. return KWSYS_NULLPTR;
  443. } else if (!paren && *regparse != '\0') {
  444. if (*regparse == ')') {
  445. // RAISE Error, SYM(RegularExpression), SYM(Unmatched_Parens),
  446. printf("RegularExpression::compile(): Unmatched parentheses.\n");
  447. return KWSYS_NULLPTR;
  448. } else {
  449. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  450. printf("RegularExpression::compile(): Internal error.\n");
  451. return KWSYS_NULLPTR;
  452. }
  453. // NOTREACHED
  454. }
  455. return (ret);
  456. }
  457. /*
  458. - regbranch - one alternative of an | operator
  459. *
  460. * Implements the concatenation operator.
  461. */
  462. char* RegExpCompile::regbranch(int* flagp)
  463. {
  464. char* ret;
  465. char* chain;
  466. char* latest;
  467. int flags;
  468. *flagp = WORST; // Tentatively.
  469. ret = regnode(BRANCH);
  470. chain = KWSYS_NULLPTR;
  471. while (*regparse != '\0' && *regparse != '|' && *regparse != ')') {
  472. latest = regpiece(&flags);
  473. if (latest == KWSYS_NULLPTR)
  474. return (KWSYS_NULLPTR);
  475. *flagp |= flags & HASWIDTH;
  476. if (chain == KWSYS_NULLPTR) // First piece.
  477. *flagp |= flags & SPSTART;
  478. else
  479. regtail(chain, latest);
  480. chain = latest;
  481. }
  482. if (chain == KWSYS_NULLPTR) // Loop ran zero times.
  483. regnode(NOTHING);
  484. return (ret);
  485. }
  486. /*
  487. - regpiece - something followed by possible [*+?]
  488. *
  489. * Note that the branching code sequences used for ? and the general cases
  490. * of * and + are somewhat optimized: they use the same NOTHING node as
  491. * both the endmarker for their branch list and the body of the last branch.
  492. * It might seem that this node could be dispensed with entirely, but the
  493. * endmarker role is not redundant.
  494. */
  495. char* RegExpCompile::regpiece(int* flagp)
  496. {
  497. char* ret;
  498. char op;
  499. char* next;
  500. int flags;
  501. ret = regatom(&flags);
  502. if (ret == KWSYS_NULLPTR)
  503. return (KWSYS_NULLPTR);
  504. op = *regparse;
  505. if (!ISMULT(op)) {
  506. *flagp = flags;
  507. return (ret);
  508. }
  509. if (!(flags & HASWIDTH) && op != '?') {
  510. // RAISE Error, SYM(RegularExpression), SYM(Empty_Operand),
  511. printf("RegularExpression::compile() : *+ operand could be empty.\n");
  512. return KWSYS_NULLPTR;
  513. }
  514. *flagp = (op != '+') ? (WORST | SPSTART) : (WORST | HASWIDTH);
  515. if (op == '*' && (flags & SIMPLE))
  516. reginsert(STAR, ret);
  517. else if (op == '*') {
  518. // Emit x* as (x&|), where & means "self".
  519. reginsert(BRANCH, ret); // Either x
  520. regoptail(ret, regnode(BACK)); // and loop
  521. regoptail(ret, ret); // back
  522. regtail(ret, regnode(BRANCH)); // or
  523. regtail(ret, regnode(NOTHING)); // null.
  524. } else if (op == '+' && (flags & SIMPLE))
  525. reginsert(PLUS, ret);
  526. else if (op == '+') {
  527. // Emit x+ as x(&|), where & means "self".
  528. next = regnode(BRANCH); // Either
  529. regtail(ret, next);
  530. regtail(regnode(BACK), ret); // loop back
  531. regtail(next, regnode(BRANCH)); // or
  532. regtail(ret, regnode(NOTHING)); // null.
  533. } else if (op == '?') {
  534. // Emit x? as (x|)
  535. reginsert(BRANCH, ret); // Either x
  536. regtail(ret, regnode(BRANCH)); // or
  537. next = regnode(NOTHING); // null.
  538. regtail(ret, next);
  539. regoptail(ret, next);
  540. }
  541. regparse++;
  542. if (ISMULT(*regparse)) {
  543. // RAISE Error, SYM(RegularExpression), SYM(Nested_Operand),
  544. printf("RegularExpression::compile(): Nested *?+.\n");
  545. return KWSYS_NULLPTR;
  546. }
  547. return (ret);
  548. }
  549. /*
  550. - regatom - the lowest level
  551. *
  552. * Optimization: gobbles an entire sequence of ordinary characters so that
  553. * it can turn them into a single node, which is smaller to store and
  554. * faster to run. Backslashed characters are exceptions, each becoming a
  555. * separate node; the code is simpler that way and it's not worth fixing.
  556. */
  557. char* RegExpCompile::regatom(int* flagp)
  558. {
  559. char* ret;
  560. int flags;
  561. *flagp = WORST; // Tentatively.
  562. switch (*regparse++) {
  563. case '^':
  564. ret = regnode(BOL);
  565. break;
  566. case '$':
  567. ret = regnode(EOL);
  568. break;
  569. case '.':
  570. ret = regnode(ANY);
  571. *flagp |= HASWIDTH | SIMPLE;
  572. break;
  573. case '[': {
  574. int rxpclass;
  575. int rxpclassend;
  576. if (*regparse == '^') { // Complement of range.
  577. ret = regnode(ANYBUT);
  578. regparse++;
  579. } else
  580. ret = regnode(ANYOF);
  581. if (*regparse == ']' || *regparse == '-')
  582. regc(*regparse++);
  583. while (*regparse != '\0' && *regparse != ']') {
  584. if (*regparse == '-') {
  585. regparse++;
  586. if (*regparse == ']' || *regparse == '\0')
  587. regc('-');
  588. else {
  589. rxpclass = UCHARAT(regparse - 2) + 1;
  590. rxpclassend = UCHARAT(regparse);
  591. if (rxpclass > rxpclassend + 1) {
  592. // RAISE Error, SYM(RegularExpression), SYM(Invalid_Range),
  593. printf("RegularExpression::compile(): Invalid range in [].\n");
  594. return KWSYS_NULLPTR;
  595. }
  596. for (; rxpclass <= rxpclassend; rxpclass++)
  597. regc(static_cast<char>(rxpclass));
  598. regparse++;
  599. }
  600. } else
  601. regc(*regparse++);
  602. }
  603. regc('\0');
  604. if (*regparse != ']') {
  605. // RAISE Error, SYM(RegularExpression), SYM(Unmatched_Bracket),
  606. printf("RegularExpression::compile(): Unmatched [].\n");
  607. return KWSYS_NULLPTR;
  608. }
  609. regparse++;
  610. *flagp |= HASWIDTH | SIMPLE;
  611. } break;
  612. case '(':
  613. ret = reg(1, &flags);
  614. if (ret == KWSYS_NULLPTR)
  615. return (KWSYS_NULLPTR);
  616. *flagp |= flags & (HASWIDTH | SPSTART);
  617. break;
  618. case '\0':
  619. case '|':
  620. case ')':
  621. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  622. printf("RegularExpression::compile(): Internal error.\n"); // Never here
  623. return KWSYS_NULLPTR;
  624. case '?':
  625. case '+':
  626. case '*':
  627. // RAISE Error, SYM(RegularExpression), SYM(No_Operand),
  628. printf("RegularExpression::compile(): ?+* follows nothing.\n");
  629. return KWSYS_NULLPTR;
  630. case '\\':
  631. if (*regparse == '\0') {
  632. // RAISE Error, SYM(RegularExpression), SYM(Trailing_Backslash),
  633. printf("RegularExpression::compile(): Trailing backslash.\n");
  634. return KWSYS_NULLPTR;
  635. }
  636. ret = regnode(EXACTLY);
  637. regc(*regparse++);
  638. regc('\0');
  639. *flagp |= HASWIDTH | SIMPLE;
  640. break;
  641. default: {
  642. int len;
  643. char ender;
  644. regparse--;
  645. len = int(strcspn(regparse, META));
  646. if (len <= 0) {
  647. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  648. printf("RegularExpression::compile(): Internal error.\n");
  649. return KWSYS_NULLPTR;
  650. }
  651. ender = *(regparse + len);
  652. if (len > 1 && ISMULT(ender))
  653. len--; // Back off clear of ?+* operand.
  654. *flagp |= HASWIDTH;
  655. if (len == 1)
  656. *flagp |= SIMPLE;
  657. ret = regnode(EXACTLY);
  658. while (len > 0) {
  659. regc(*regparse++);
  660. len--;
  661. }
  662. regc('\0');
  663. } break;
  664. }
  665. return (ret);
  666. }
  667. /*
  668. - regnode - emit a node
  669. Location.
  670. */
  671. char* RegExpCompile::regnode(char op)
  672. {
  673. char* ret;
  674. char* ptr;
  675. ret = regcode;
  676. if (ret == regdummyptr) {
  677. regsize += 3;
  678. return (ret);
  679. }
  680. ptr = ret;
  681. *ptr++ = op;
  682. *ptr++ = '\0'; // Null "next" pointer.
  683. *ptr++ = '\0';
  684. regcode = ptr;
  685. return (ret);
  686. }
  687. /*
  688. - regc - emit (if appropriate) a byte of code
  689. */
  690. void RegExpCompile::regc(char b)
  691. {
  692. if (regcode != regdummyptr)
  693. *regcode++ = b;
  694. else
  695. regsize++;
  696. }
  697. /*
  698. - reginsert - insert an operator in front of already-emitted operand
  699. *
  700. * Means relocating the operand.
  701. */
  702. void RegExpCompile::reginsert(char op, char* opnd)
  703. {
  704. char* src;
  705. char* dst;
  706. char* place;
  707. if (regcode == regdummyptr) {
  708. regsize += 3;
  709. return;
  710. }
  711. src = regcode;
  712. regcode += 3;
  713. dst = regcode;
  714. while (src > opnd)
  715. *--dst = *--src;
  716. place = opnd; // Op node, where operand used to be.
  717. *place++ = op;
  718. *place++ = '\0';
  719. *place = '\0';
  720. }
  721. /*
  722. - regtail - set the next-pointer at the end of a node chain
  723. */
  724. void RegExpCompile::regtail(char* p, const char* val)
  725. {
  726. char* scan;
  727. char* temp;
  728. int offset;
  729. if (p == regdummyptr)
  730. return;
  731. // Find last node.
  732. scan = p;
  733. for (;;) {
  734. temp = regnext(scan);
  735. if (temp == KWSYS_NULLPTR)
  736. break;
  737. scan = temp;
  738. }
  739. if (OP(scan) == BACK)
  740. offset = int(scan - val);
  741. else
  742. offset = int(val - scan);
  743. *(scan + 1) = static_cast<char>((offset >> 8) & 0377);
  744. *(scan + 2) = static_cast<char>(offset & 0377);
  745. }
  746. /*
  747. - regoptail - regtail on operand of first argument; nop if operandless
  748. */
  749. void RegExpCompile::regoptail(char* p, const char* val)
  750. {
  751. // "Operandless" and "op != BRANCH" are synonymous in practice.
  752. if (p == KWSYS_NULLPTR || p == regdummyptr || OP(p) != BRANCH)
  753. return;
  754. regtail(OPERAND(p), val);
  755. }
  756. ////////////////////////////////////////////////////////////////////////
  757. //
  758. // find and friends
  759. //
  760. ////////////////////////////////////////////////////////////////////////
  761. /*
  762. * Utility class for RegularExpression::find().
  763. */
  764. class RegExpFind
  765. {
  766. public:
  767. const char* reginput; // String-input pointer.
  768. const char* regbol; // Beginning of input, for ^ check.
  769. const char** regstartp; // Pointer to startp array.
  770. const char** regendp; // Ditto for endp.
  771. int regtry(const char*, const char**, const char**, const char*);
  772. int regmatch(const char*);
  773. int regrepeat(const char*);
  774. };
  775. // find -- Matches the regular expression to the given string.
  776. // Returns true if found, and sets start and end indexes accordingly.
  777. bool RegularExpression::find(char const* string,
  778. RegularExpressionMatch& rmatch) const
  779. {
  780. const char* s;
  781. rmatch.clear();
  782. rmatch.searchstring = string;
  783. if (!this->program) {
  784. return false;
  785. }
  786. // Check validity of program.
  787. if (UCHARAT(this->program) != MAGIC) {
  788. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  789. printf(
  790. "RegularExpression::find(): Compiled regular expression corrupted.\n");
  791. return false;
  792. }
  793. // If there is a "must appear" string, look for it.
  794. if (this->regmust != KWSYS_NULLPTR) {
  795. s = string;
  796. while ((s = strchr(s, this->regmust[0])) != KWSYS_NULLPTR) {
  797. if (strncmp(s, this->regmust, this->regmlen) == 0)
  798. break; // Found it.
  799. s++;
  800. }
  801. if (s == KWSYS_NULLPTR) // Not present.
  802. return false;
  803. }
  804. RegExpFind regFind;
  805. // Mark beginning of line for ^ .
  806. regFind.regbol = string;
  807. // Simplest case: anchored match need be tried only once.
  808. if (this->reganch)
  809. return (
  810. regFind.regtry(string, rmatch.startp, rmatch.endp, this->program) != 0);
  811. // Messy cases: unanchored match.
  812. s = string;
  813. if (this->regstart != '\0')
  814. // We know what char it must start with.
  815. while ((s = strchr(s, this->regstart)) != KWSYS_NULLPTR) {
  816. if (regFind.regtry(s, rmatch.startp, rmatch.endp, this->program))
  817. return true;
  818. s++;
  819. }
  820. else
  821. // We don't -- general case.
  822. do {
  823. if (regFind.regtry(s, rmatch.startp, rmatch.endp, this->program))
  824. return true;
  825. } while (*s++ != '\0');
  826. // Failure.
  827. return false;
  828. }
  829. /*
  830. - regtry - try match at specific point
  831. 0 failure, 1 success
  832. */
  833. int RegExpFind::regtry(const char* string, const char** start,
  834. const char** end, const char* prog)
  835. {
  836. int i;
  837. const char** sp1;
  838. const char** ep;
  839. reginput = string;
  840. regstartp = start;
  841. regendp = end;
  842. sp1 = start;
  843. ep = end;
  844. for (i = RegularExpressionMatch::NSUBEXP; i > 0; i--) {
  845. *sp1++ = KWSYS_NULLPTR;
  846. *ep++ = KWSYS_NULLPTR;
  847. }
  848. if (regmatch(prog + 1)) {
  849. start[0] = string;
  850. end[0] = reginput;
  851. return (1);
  852. } else
  853. return (0);
  854. }
  855. /*
  856. - regmatch - main matching routine
  857. *
  858. * Conceptually the strategy is simple: check to see whether the current
  859. * node matches, call self recursively to see whether the rest matches,
  860. * and then act accordingly. In practice we make some effort to avoid
  861. * recursion, in particular by going through "ordinary" nodes (that don't
  862. * need to know whether the rest of the match failed) by a loop instead of
  863. * by recursion.
  864. * 0 failure, 1 success
  865. */
  866. int RegExpFind::regmatch(const char* prog)
  867. {
  868. const char* scan; // Current node.
  869. const char* next; // Next node.
  870. scan = prog;
  871. while (scan != KWSYS_NULLPTR) {
  872. next = regnext(scan);
  873. switch (OP(scan)) {
  874. case BOL:
  875. if (reginput != regbol)
  876. return (0);
  877. break;
  878. case EOL:
  879. if (*reginput != '\0')
  880. return (0);
  881. break;
  882. case ANY:
  883. if (*reginput == '\0')
  884. return (0);
  885. reginput++;
  886. break;
  887. case EXACTLY: {
  888. size_t len;
  889. const char* opnd;
  890. opnd = OPERAND(scan);
  891. // Inline the first character, for speed.
  892. if (*opnd != *reginput)
  893. return (0);
  894. len = strlen(opnd);
  895. if (len > 1 && strncmp(opnd, reginput, len) != 0)
  896. return (0);
  897. reginput += len;
  898. } break;
  899. case ANYOF:
  900. if (*reginput == '\0' ||
  901. strchr(OPERAND(scan), *reginput) == KWSYS_NULLPTR)
  902. return (0);
  903. reginput++;
  904. break;
  905. case ANYBUT:
  906. if (*reginput == '\0' ||
  907. strchr(OPERAND(scan), *reginput) != KWSYS_NULLPTR)
  908. return (0);
  909. reginput++;
  910. break;
  911. case NOTHING:
  912. break;
  913. case BACK:
  914. break;
  915. case OPEN + 1:
  916. case OPEN + 2:
  917. case OPEN + 3:
  918. case OPEN + 4:
  919. case OPEN + 5:
  920. case OPEN + 6:
  921. case OPEN + 7:
  922. case OPEN + 8:
  923. case OPEN + 9: {
  924. int no;
  925. const char* save;
  926. no = OP(scan) - OPEN;
  927. save = reginput;
  928. if (regmatch(next)) {
  929. //
  930. // Don't set startp if some later invocation of the
  931. // same parentheses already has.
  932. //
  933. if (regstartp[no] == KWSYS_NULLPTR)
  934. regstartp[no] = save;
  935. return (1);
  936. } else
  937. return (0);
  938. }
  939. // break;
  940. case CLOSE + 1:
  941. case CLOSE + 2:
  942. case CLOSE + 3:
  943. case CLOSE + 4:
  944. case CLOSE + 5:
  945. case CLOSE + 6:
  946. case CLOSE + 7:
  947. case CLOSE + 8:
  948. case CLOSE + 9: {
  949. int no;
  950. const char* save;
  951. no = OP(scan) - CLOSE;
  952. save = reginput;
  953. if (regmatch(next)) {
  954. //
  955. // Don't set endp if some later invocation of the
  956. // same parentheses already has.
  957. //
  958. if (regendp[no] == KWSYS_NULLPTR)
  959. regendp[no] = save;
  960. return (1);
  961. } else
  962. return (0);
  963. }
  964. // break;
  965. case BRANCH: {
  966. const char* save;
  967. if (OP(next) != BRANCH) // No choice.
  968. next = OPERAND(scan); // Avoid recursion.
  969. else {
  970. do {
  971. save = reginput;
  972. if (regmatch(OPERAND(scan)))
  973. return (1);
  974. reginput = save;
  975. scan = regnext(scan);
  976. } while (scan != KWSYS_NULLPTR && OP(scan) == BRANCH);
  977. return (0);
  978. // NOTREACHED
  979. }
  980. } break;
  981. case STAR:
  982. case PLUS: {
  983. char nextch;
  984. int no;
  985. const char* save;
  986. int min_no;
  987. //
  988. // Lookahead to avoid useless match attempts when we know
  989. // what character comes next.
  990. //
  991. nextch = '\0';
  992. if (OP(next) == EXACTLY)
  993. nextch = *OPERAND(next);
  994. min_no = (OP(scan) == STAR) ? 0 : 1;
  995. save = reginput;
  996. no = regrepeat(OPERAND(scan));
  997. while (no >= min_no) {
  998. // If it could work, try it.
  999. if (nextch == '\0' || *reginput == nextch)
  1000. if (regmatch(next))
  1001. return (1);
  1002. // Couldn't or didn't -- back up.
  1003. no--;
  1004. reginput = save + no;
  1005. }
  1006. return (0);
  1007. }
  1008. // break;
  1009. case END:
  1010. return (1); // Success!
  1011. default:
  1012. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  1013. printf(
  1014. "RegularExpression::find(): Internal error -- memory corrupted.\n");
  1015. return 0;
  1016. }
  1017. scan = next;
  1018. }
  1019. //
  1020. // We get here only if there's trouble -- normally "case END" is the
  1021. // terminating point.
  1022. //
  1023. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  1024. printf("RegularExpression::find(): Internal error -- corrupted pointers.\n");
  1025. return (0);
  1026. }
  1027. /*
  1028. - regrepeat - repeatedly match something simple, report how many
  1029. */
  1030. int RegExpFind::regrepeat(const char* p)
  1031. {
  1032. int count = 0;
  1033. const char* scan;
  1034. const char* opnd;
  1035. scan = reginput;
  1036. opnd = OPERAND(p);
  1037. switch (OP(p)) {
  1038. case ANY:
  1039. count = int(strlen(scan));
  1040. scan += count;
  1041. break;
  1042. case EXACTLY:
  1043. while (*opnd == *scan) {
  1044. count++;
  1045. scan++;
  1046. }
  1047. break;
  1048. case ANYOF:
  1049. while (*scan != '\0' && strchr(opnd, *scan) != KWSYS_NULLPTR) {
  1050. count++;
  1051. scan++;
  1052. }
  1053. break;
  1054. case ANYBUT:
  1055. while (*scan != '\0' && strchr(opnd, *scan) == KWSYS_NULLPTR) {
  1056. count++;
  1057. scan++;
  1058. }
  1059. break;
  1060. default: // Oh dear. Called inappropriately.
  1061. // RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  1062. printf("cm RegularExpression::find(): Internal error.\n");
  1063. return 0;
  1064. }
  1065. reginput = scan;
  1066. return (count);
  1067. }
  1068. /*
  1069. - regnext - dig the "next" pointer out of a node
  1070. */
  1071. static const char* regnext(const char* p)
  1072. {
  1073. int offset;
  1074. if (p == regdummyptr)
  1075. return (KWSYS_NULLPTR);
  1076. offset = NEXT(p);
  1077. if (offset == 0)
  1078. return (KWSYS_NULLPTR);
  1079. if (OP(p) == BACK)
  1080. return (p - offset);
  1081. else
  1082. return (p + offset);
  1083. }
  1084. static char* regnext(char* p)
  1085. {
  1086. int offset;
  1087. if (p == regdummyptr)
  1088. return (KWSYS_NULLPTR);
  1089. offset = NEXT(p);
  1090. if (offset == 0)
  1091. return (KWSYS_NULLPTR);
  1092. if (OP(p) == BACK)
  1093. return (p - offset);
  1094. else
  1095. return (p + offset);
  1096. }
  1097. } // namespace KWSYS_NAMESPACE