RegularExpression.cxx 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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 (const char*);
  283. static char* regnext (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. const char* scan;
  308. const char* longest;
  309. 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. char* ret;
  397. char* br;
  398. char* ender;
  399. 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. char* ret;
  470. char* chain;
  471. 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. char* ret;
  502. char op;
  503. 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. 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. }
  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. int len;
  651. 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. char* ret;
  682. 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. char* src;
  711. char* dst;
  712. 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. char* scan;
  732. char* temp;
  733. 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. // find -- Matches the regular expression to the given string.
  785. // Returns true if found, and sets start and end indexes accordingly.
  786. bool RegularExpression::find (const char* string) {
  787. const char* s;
  788. this->searchstring = string;
  789. if (!this->program)
  790. {
  791. return false;
  792. }
  793. // Check validity of program.
  794. if (UCHARAT(this->program) != MAGIC) {
  795. //RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  796. printf ("RegularExpression::find(): Compiled regular expression corrupted.\n");
  797. return 0;
  798. }
  799. // If there is a "must appear" string, look for it.
  800. if (this->regmust != 0) {
  801. s = string;
  802. while ((s = strchr(s, this->regmust[0])) != 0) {
  803. if (strncmp(s, this->regmust, this->regmlen) == 0)
  804. break; // Found it.
  805. s++;
  806. }
  807. if (s == 0) // Not present.
  808. return (0);
  809. }
  810. // Mark beginning of line for ^ .
  811. regbol = string;
  812. // Simplest case: anchored match need be tried only once.
  813. if (this->reganch)
  814. return (regtry(string, this->startp, this->endp, this->program) != 0);
  815. // Messy cases: unanchored match.
  816. s = string;
  817. if (this->regstart != '\0')
  818. // We know what char it must start with.
  819. while ((s = strchr(s, this->regstart)) != 0) {
  820. if (regtry(s, this->startp, this->endp, this->program))
  821. return (1);
  822. s++;
  823. }
  824. else
  825. // We don't -- general case.
  826. do {
  827. if (regtry(s, this->startp, this->endp, this->program))
  828. return (1);
  829. } while (*s++ != '\0');
  830. // Failure.
  831. return (0);
  832. }
  833. /*
  834. - regtry - try match at specific point
  835. 0 failure, 1 success
  836. */
  837. static int regtry (const char* string, const char* *start,
  838. const char* *end, const char* prog) {
  839. int i;
  840. const char* *sp1;
  841. const char* *ep;
  842. reginput = string;
  843. regstartp = start;
  844. regendp = end;
  845. sp1 = start;
  846. ep = end;
  847. for (i = RegularExpression::NSUBEXP; i > 0; i--) {
  848. *sp1++ = 0;
  849. *ep++ = 0;
  850. }
  851. if (regmatch(prog + 1)) {
  852. start[0] = string;
  853. end[0] = reginput;
  854. return (1);
  855. }
  856. else
  857. return (0);
  858. }
  859. /*
  860. - regmatch - main matching routine
  861. *
  862. * Conceptually the strategy is simple: check to see whether the current
  863. * node matches, call self recursively to see whether the rest matches,
  864. * and then act accordingly. In practice we make some effort to avoid
  865. * recursion, in particular by going through "ordinary" nodes (that don't
  866. * need to know whether the rest of the match failed) by a loop instead of
  867. * by recursion.
  868. * 0 failure, 1 success
  869. */
  870. static int regmatch (const char* prog) {
  871. const char* scan; // Current node.
  872. const char* next; // Next node.
  873. scan = prog;
  874. while (scan != 0) {
  875. next = regnext(scan);
  876. switch (OP(scan)) {
  877. case BOL:
  878. if (reginput != regbol)
  879. return (0);
  880. break;
  881. case EOL:
  882. if (*reginput != '\0')
  883. return (0);
  884. break;
  885. case ANY:
  886. if (*reginput == '\0')
  887. return (0);
  888. reginput++;
  889. break;
  890. case EXACTLY:{
  891. size_t len;
  892. const char* opnd;
  893. opnd = OPERAND(scan);
  894. // Inline the first character, for speed.
  895. if (*opnd != *reginput)
  896. return (0);
  897. len = strlen(opnd);
  898. if (len > 1 && strncmp(opnd, reginput, len) != 0)
  899. return (0);
  900. reginput += len;
  901. }
  902. break;
  903. case ANYOF:
  904. if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == 0)
  905. return (0);
  906. reginput++;
  907. break;
  908. case ANYBUT:
  909. if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != 0)
  910. return (0);
  911. reginput++;
  912. break;
  913. case NOTHING:
  914. break;
  915. case BACK:
  916. break;
  917. case OPEN + 1:
  918. case OPEN + 2:
  919. case OPEN + 3:
  920. case OPEN + 4:
  921. case OPEN + 5:
  922. case OPEN + 6:
  923. case OPEN + 7:
  924. case OPEN + 8:
  925. case OPEN + 9:{
  926. int no;
  927. const char* save;
  928. no = OP(scan) - OPEN;
  929. save = reginput;
  930. if (regmatch(next)) {
  931. //
  932. // Don't set startp if some later invocation of the
  933. // same parentheses already has.
  934. //
  935. if (regstartp[no] == 0)
  936. regstartp[no] = save;
  937. return (1);
  938. }
  939. else
  940. return (0);
  941. }
  942. // break;
  943. case CLOSE + 1:
  944. case CLOSE + 2:
  945. case CLOSE + 3:
  946. case CLOSE + 4:
  947. case CLOSE + 5:
  948. case CLOSE + 6:
  949. case CLOSE + 7:
  950. case CLOSE + 8:
  951. case CLOSE + 9:{
  952. int no;
  953. const char* save;
  954. no = OP(scan) - CLOSE;
  955. save = reginput;
  956. if (regmatch(next)) {
  957. //
  958. // Don't set endp if some later invocation of the
  959. // same parentheses already has.
  960. //
  961. if (regendp[no] == 0)
  962. regendp[no] = save;
  963. return (1);
  964. }
  965. else
  966. return (0);
  967. }
  968. // break;
  969. case BRANCH:{
  970. const char* save;
  971. if (OP(next) != BRANCH) // No choice.
  972. next = OPERAND(scan); // Avoid recursion.
  973. else {
  974. do {
  975. save = reginput;
  976. if (regmatch(OPERAND(scan)))
  977. return (1);
  978. reginput = save;
  979. scan = regnext(scan);
  980. } while (scan != 0 && OP(scan) == BRANCH);
  981. return (0);
  982. // NOTREACHED
  983. }
  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 ("RegularExpression::find(): Internal error -- memory corrupted.\n");
  1019. return 0;
  1020. }
  1021. scan = next;
  1022. }
  1023. //
  1024. // We get here only if there's trouble -- normally "case END" is the
  1025. // terminating point.
  1026. //
  1027. //RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  1028. printf ("RegularExpression::find(): Internal error -- corrupted pointers.\n");
  1029. return (0);
  1030. }
  1031. /*
  1032. - regrepeat - repeatedly match something simple, report how many
  1033. */
  1034. static int regrepeat (const char* p) {
  1035. int count = 0;
  1036. const char* scan;
  1037. const char* opnd;
  1038. scan = reginput;
  1039. opnd = OPERAND(p);
  1040. switch (OP(p)) {
  1041. case ANY:
  1042. count = int(strlen(scan));
  1043. scan += count;
  1044. break;
  1045. case EXACTLY:
  1046. while (*opnd == *scan) {
  1047. count++;
  1048. scan++;
  1049. }
  1050. break;
  1051. case ANYOF:
  1052. while (*scan != '\0' && strchr(opnd, *scan) != 0) {
  1053. count++;
  1054. scan++;
  1055. }
  1056. break;
  1057. case ANYBUT:
  1058. while (*scan != '\0' && strchr(opnd, *scan) == 0) {
  1059. count++;
  1060. scan++;
  1061. }
  1062. break;
  1063. default: // Oh dear. Called inappropriately.
  1064. //RAISE Error, SYM(RegularExpression), SYM(Internal_Error),
  1065. printf ("cm RegularExpression::find(): Internal error.\n");
  1066. return 0;
  1067. }
  1068. reginput = scan;
  1069. return (count);
  1070. }
  1071. /*
  1072. - regnext - dig the "next" pointer out of a node
  1073. */
  1074. static const char* regnext (const char* p) {
  1075. int offset;
  1076. if (p == &regdummy)
  1077. return (0);
  1078. offset = NEXT(p);
  1079. if (offset == 0)
  1080. return (0);
  1081. if (OP(p) == BACK)
  1082. return (p - offset);
  1083. else
  1084. return (p + offset);
  1085. }
  1086. static char* regnext (char* p) {
  1087. int offset;
  1088. if (p == &regdummy)
  1089. return (0);
  1090. offset = NEXT(p);
  1091. if (offset == 0)
  1092. return (0);
  1093. if (OP(p) == BACK)
  1094. return (p - offset);
  1095. else
  1096. return (p + offset);
  1097. }
  1098. } // namespace KWSYS_NAMESPACE