RegularExpression.cxx 34 KB

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