RegularExpression.cxx 38 KB

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