RegularExpression.cxx 39 KB

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