Regex.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. #pragma once
  2. #include <functional>
  3. #include "Array.h"
  4. #include "Critical.h"
  5. #include "Text.h"
  6. namespace Framework
  7. {
  8. namespace Regex
  9. {
  10. class Flags
  11. {
  12. public:
  13. static const int START = 1;
  14. static const int END = 2;
  15. };
  16. template<typename Data> class State;
  17. template<typename Data> class Transition
  18. {
  19. private:
  20. std::function<bool(Data, int)> conditionFunction;
  21. State<Data>* zTarget;
  22. int requiredFlags;
  23. public:
  24. Transition()
  25. : conditionFunction(0),
  26. zTarget(0),
  27. requiredFlags(0)
  28. {}
  29. Transition(std::function<bool(Data, int)> conditionFunction,
  30. State<Data>* zTarget,
  31. int requiredFlags)
  32. : conditionFunction(conditionFunction),
  33. zTarget(zTarget),
  34. requiredFlags(requiredFlags)
  35. {}
  36. Transition(const Transition& other)
  37. : conditionFunction(other.conditionFunction),
  38. zTarget(other.zTarget),
  39. requiredFlags(other.requiredFlags)
  40. {}
  41. bool test(Data value, int flags) const
  42. {
  43. return conditionFunction && conditionFunction(value, flags)
  44. && (requiredFlags | flags) == flags;
  45. }
  46. bool test(int flags) const
  47. {
  48. return !conditionFunction && (requiredFlags | flags) == flags;
  49. }
  50. State<Data>* zTargetState() const
  51. {
  52. return zTarget;
  53. }
  54. std::function<bool(Data, int)> getConditionFunction() const
  55. {
  56. return conditionFunction;
  57. }
  58. int getRequiredFlags() const
  59. {
  60. return requiredFlags;
  61. }
  62. };
  63. template<typename Data> class State : public ReferenceCounter
  64. {
  65. private:
  66. Array<Transition<Data>> transitions;
  67. bool final;
  68. int groupStart;
  69. int groupEnd;
  70. int id;
  71. public:
  72. State(int id)
  73. : ReferenceCounter(),
  74. final(false),
  75. groupStart(0),
  76. groupEnd(0),
  77. id(id)
  78. {}
  79. void addTransition(Transition<Data> transition)
  80. {
  81. transitions.add(transition);
  82. }
  83. void setFinal(bool isFinal)
  84. {
  85. final = isFinal;
  86. }
  87. void setGroupStart(int groupStart)
  88. {
  89. this->groupStart = groupStart;
  90. }
  91. void setGroupEnd(int groupEnd)
  92. {
  93. this->groupEnd = groupEnd;
  94. }
  95. bool isFinal() const
  96. {
  97. return final;
  98. }
  99. int getGroupStart() const
  100. {
  101. return groupStart;
  102. }
  103. int getGroupEnd() const
  104. {
  105. return groupEnd;
  106. }
  107. int getId() const
  108. {
  109. return id;
  110. }
  111. const Array<Transition<Data>>& getTransitions() const
  112. {
  113. return transitions;
  114. }
  115. };
  116. class Group
  117. {
  118. private:
  119. int start;
  120. int end;
  121. int id;
  122. public:
  123. DLLEXPORT Group(int start, int end, int id);
  124. DLLEXPORT Group();
  125. DLLEXPORT int getStart();
  126. DLLEXPORT int getEnd();
  127. DLLEXPORT int getId();
  128. };
  129. class Result : public ReferenceCounter
  130. {
  131. private:
  132. Array<Group> groups;
  133. public:
  134. DLLEXPORT Result(Array<Group> groups);
  135. DLLEXPORT int getGroupCount();
  136. DLLEXPORT Group getGroup(int index);
  137. DLLEXPORT int getStart();
  138. DLLEXPORT int getEnd();
  139. };
  140. template<typename Data> class ExecutionStackFrame
  141. {
  142. private:
  143. ExecutionStackFrame* before;
  144. State<Data>* state;
  145. int transitionIndex;
  146. int strIndex;
  147. public:
  148. ExecutionStackFrame(ExecutionStackFrame<Data>* before,
  149. State<Data>* zState,
  150. int strIndex)
  151. : before(before),
  152. state(zState),
  153. transitionIndex(0),
  154. strIndex(strIndex)
  155. {}
  156. ~ExecutionStackFrame()
  157. {
  158. if (before)
  159. {
  160. delete before;
  161. }
  162. }
  163. ExecutionStackFrame<Data>* getBefore() const
  164. {
  165. return before;
  166. }
  167. State<Data>* zState() const
  168. {
  169. return state;
  170. }
  171. void setStrIndex(int index)
  172. {
  173. strIndex = index;
  174. }
  175. int getStrIndex() const
  176. {
  177. return strIndex;
  178. }
  179. int getTransitionIndex() const
  180. {
  181. return transitionIndex;
  182. }
  183. void setTransitionIndex(int transitionIndex)
  184. {
  185. this->transitionIndex = transitionIndex;
  186. }
  187. void discard()
  188. {
  189. before = 0;
  190. }
  191. Array<Group> getGroups() const
  192. {
  193. Array<Group> groups;
  194. const ExecutionStackFrame* current = this;
  195. while (true)
  196. {
  197. if (current->getBefore())
  198. {
  199. current = current->getBefore();
  200. }
  201. else
  202. {
  203. break;
  204. }
  205. }
  206. groups.add(Group(current->getStrIndex(), strIndex, 0));
  207. current = this;
  208. Array<int> groupEnds;
  209. Array<int> knownGroups;
  210. while (current)
  211. {
  212. if (current->zState()->getGroupEnd())
  213. {
  214. groupEnds.add(current->getStrIndex());
  215. }
  216. if (current->zState()->getGroupStart()
  217. && groupEnds.getEintragAnzahl() > 0)
  218. {
  219. if (knownGroups.getWertIndex(
  220. current->zState()->getGroupStart())
  221. < 0)
  222. {
  223. while (groups.getEintragAnzahl()
  224. <= current->zState()->getGroupStart())
  225. {
  226. groups.add(
  227. Group(-1, -1, groups.getEintragAnzahl()));
  228. }
  229. groups.set(Group(current->getStrIndex(),
  230. groupEnds.get(0),
  231. current->zState()->getGroupStart()),
  232. current->zState()->getGroupStart());
  233. knownGroups.add(current->zState()->getGroupStart());
  234. }
  235. groupEnds.remove(0);
  236. }
  237. current = current->getBefore();
  238. }
  239. return groups;
  240. }
  241. bool isEndlessLoop() const
  242. {
  243. const ExecutionStackFrame* current = this;
  244. while (current)
  245. {
  246. if (current->getBefore()
  247. && current->getBefore()->zState() == state
  248. && current->getBefore()->getStrIndex() == strIndex)
  249. {
  250. return true;
  251. }
  252. current = current->getBefore();
  253. }
  254. return false;
  255. }
  256. };
  257. template<typename Data> class Automata : public ReferenceCounter
  258. {
  259. private:
  260. RCArray<State<Data>> states;
  261. public:
  262. Automata()
  263. : ReferenceCounter()
  264. {}
  265. State<Data>* addState()
  266. {
  267. State<Data>* state = new State<Data>(states.getEintragAnzahl());
  268. states.add(state);
  269. return state;
  270. }
  271. const RCArray<State<Data>>& getStates() const
  272. {
  273. return states;
  274. }
  275. RCArray<Result>* match(const Data* str, int length)
  276. {
  277. RCArray<Result>* results = new RCArray<Result>();
  278. ExecutionStackFrame<Data>* currentFrame
  279. = new ExecutionStackFrame<Data>(0, states.z(0), 0);
  280. int flags = Flags::START;
  281. int startIndex = 0;
  282. while (currentFrame)
  283. {
  284. if (currentFrame->getStrIndex() >= length)
  285. {
  286. flags |= Flags::END;
  287. }
  288. else
  289. {
  290. flags &= ~Flags::END;
  291. }
  292. if (currentFrame->getStrIndex() == 0)
  293. {
  294. flags |= Flags::START;
  295. }
  296. else
  297. {
  298. flags &= ~Flags::START;
  299. }
  300. if (currentFrame->zState()->isFinal())
  301. {
  302. results->add(new Result(currentFrame->getGroups()));
  303. int index = currentFrame->getStrIndex();
  304. if (startIndex < index)
  305. {
  306. delete currentFrame;
  307. currentFrame = new ExecutionStackFrame<Data>(
  308. 0, states.z(0), index);
  309. startIndex = currentFrame->getStrIndex();
  310. if (currentFrame->getStrIndex() > length)
  311. {
  312. delete currentFrame;
  313. currentFrame = 0;
  314. continue;
  315. }
  316. }
  317. }
  318. if (currentFrame->isEndlessLoop())
  319. {
  320. delete currentFrame;
  321. results->release();
  322. return 0; // endless loop
  323. }
  324. bool found = 0;
  325. for (int i = currentFrame->getTransitionIndex();
  326. i < currentFrame->zState()
  327. ->getTransitions()
  328. .getEintragAnzahl();
  329. i++)
  330. {
  331. Transition<Data> t
  332. = currentFrame->zState()->getTransitions().get(i);
  333. if (t.test(flags))
  334. {
  335. currentFrame->setTransitionIndex(i + 1);
  336. currentFrame
  337. = new ExecutionStackFrame<Data>(currentFrame,
  338. t.zTargetState(),
  339. currentFrame->getStrIndex());
  340. found = 1;
  341. break;
  342. }
  343. else if (currentFrame->getStrIndex() < length
  344. && t.test(
  345. str[currentFrame->getStrIndex()], flags))
  346. {
  347. currentFrame->setTransitionIndex(i + 1);
  348. currentFrame
  349. = new ExecutionStackFrame<Data>(currentFrame,
  350. t.zTargetState(),
  351. currentFrame->getStrIndex() + 1);
  352. found = 1;
  353. break;
  354. }
  355. }
  356. if (!found)
  357. {
  358. ExecutionStackFrame<Data>* before
  359. = currentFrame->getBefore();
  360. if (before)
  361. {
  362. currentFrame->discard();
  363. delete currentFrame;
  364. currentFrame = before;
  365. }
  366. else
  367. {
  368. currentFrame->setStrIndex(
  369. currentFrame->getStrIndex() + 1);
  370. startIndex = currentFrame->getStrIndex();
  371. currentFrame->setTransitionIndex(0);
  372. if (currentFrame->getStrIndex() > length)
  373. {
  374. delete currentFrame;
  375. currentFrame = 0;
  376. }
  377. }
  378. }
  379. }
  380. return results;
  381. }
  382. };
  383. template<typename Data>
  384. Automata<Data>* oneOf(Automata<Data>* left, Automata<Data>* right)
  385. {
  386. Automata<Data>* result = new Automata<Data>();
  387. State<Data>* start = result->addState();
  388. int leftStateCount = left->getStates().getEintragAnzahl();
  389. for (State<Data>* leftState : left->getStates())
  390. {
  391. if (leftState)
  392. {
  393. result->addState();
  394. }
  395. }
  396. for (State<Data>* leftState : left->getStates())
  397. {
  398. State<Data>* newState
  399. = result->getStates().z(leftState->getId() + 1);
  400. newState->setFinal(leftState->isFinal());
  401. newState->setGroupStart(leftState->getGroupStart());
  402. newState->setGroupEnd(leftState->getGroupEnd());
  403. for (Transition<Data> transition : leftState->getTransitions())
  404. {
  405. newState->addTransition(
  406. Transition<Data>(transition.getConditionFunction(),
  407. result->getStates().z(
  408. transition.zTargetState()->getId() + 1),
  409. transition.getRequiredFlags()));
  410. }
  411. }
  412. start->addTransition(
  413. Transition<Data>(0, result->getStates().z(1), 0));
  414. for (State<Data>* rightState : right->getStates())
  415. {
  416. if (rightState)
  417. {
  418. result->addState();
  419. }
  420. }
  421. for (State<Data>* rightState : right->getStates())
  422. {
  423. State<Data>* newState = result->getStates().z(
  424. rightState->getId() + 1 + leftStateCount);
  425. newState->setFinal(rightState->isFinal());
  426. newState->setGroupStart(rightState->getGroupStart());
  427. newState->setGroupEnd(rightState->getGroupEnd());
  428. for (Transition<Data> transition : rightState->getTransitions())
  429. {
  430. newState->addTransition(Transition<Data>(
  431. transition.getConditionFunction(),
  432. result->getStates().z(transition.zTargetState()->getId()
  433. + 1 + leftStateCount),
  434. transition.getRequiredFlags()));
  435. }
  436. }
  437. start->addTransition(Transition<Data>(
  438. 0, result->getStates().z(1 + leftStateCount), 0));
  439. left->release();
  440. right->release();
  441. return result;
  442. }
  443. template<typename Data>
  444. Automata<Data>* concat(Automata<Data>* left, Automata<Data>* right)
  445. {
  446. Automata<Data>* result = new Automata<Data>();
  447. int leftStateCount = left->getStates().getEintragAnzahl();
  448. for (State<Data>* leftState : left->getStates())
  449. {
  450. if (leftState)
  451. {
  452. result->addState();
  453. }
  454. }
  455. bool canSkipFirst = !right->getStates().z(0)->getGroupStart()
  456. && !right->getStates().z(0)->getGroupEnd();
  457. int index = 0;
  458. for (State<Data>* rightState : right->getStates())
  459. {
  460. if (rightState && (index != 0 || !canSkipFirst))
  461. {
  462. result->addState();
  463. }
  464. index++;
  465. }
  466. for (State<Data>* leftState : left->getStates())
  467. {
  468. State<Data>* newState
  469. = result->getStates().z(leftState->getId());
  470. newState->setGroupStart(leftState->getGroupStart());
  471. newState->setGroupEnd(leftState->getGroupEnd());
  472. for (Transition<Data> transition : leftState->getTransitions())
  473. {
  474. newState->addTransition(
  475. Transition<Data>(transition.getConditionFunction(),
  476. result->getStates().z(
  477. transition.zTargetState()->getId()),
  478. transition.getRequiredFlags()));
  479. }
  480. if (leftState->isFinal())
  481. {
  482. if (canSkipFirst)
  483. {
  484. State<Data>* oldRightStart = right->getStates().z(0);
  485. if (oldRightStart->isFinal())
  486. {
  487. newState->setFinal(true);
  488. }
  489. for (Transition<Data> transition :
  490. oldRightStart->getTransitions())
  491. {
  492. newState->addTransition(Transition<Data>(
  493. transition.getConditionFunction(),
  494. result->getStates().z(
  495. leftStateCount - 1
  496. + transition.zTargetState()->getId()),
  497. transition.getRequiredFlags()));
  498. }
  499. }
  500. else
  501. {
  502. newState->addTransition(Transition<Data>(
  503. 0, result->getStates().z(leftStateCount), 0));
  504. }
  505. }
  506. }
  507. index = 0;
  508. for (State<Data>* rightState : right->getStates())
  509. {
  510. if (index == 0 && canSkipFirst)
  511. {
  512. index++;
  513. continue;
  514. }
  515. State<Data>* newState
  516. = result->getStates().z(rightState->getId() + leftStateCount
  517. - (canSkipFirst ? 1 : 0));
  518. newState->setFinal(rightState->isFinal());
  519. newState->setGroupStart(rightState->getGroupStart());
  520. newState->setGroupEnd(rightState->getGroupEnd());
  521. for (Transition<Data> transition : rightState->getTransitions())
  522. {
  523. newState->addTransition(
  524. Transition<Data>(transition.getConditionFunction(),
  525. result->getStates().z(
  526. transition.zTargetState()->getId()
  527. + leftStateCount - (canSkipFirst ? 1 : 0)),
  528. transition.getRequiredFlags()));
  529. }
  530. index++;
  531. }
  532. left->release();
  533. right->release();
  534. return result;
  535. }
  536. template<typename Data>
  537. Automata<Data>* many(Automata<Data>* before, bool lazy)
  538. {
  539. Automata<Data>* result = new Automata<Data>();
  540. for (State<Data>* beforeState : before->getStates())
  541. {
  542. if (beforeState)
  543. {
  544. result->addState();
  545. }
  546. }
  547. State<Data>* newFinal = result->addState();
  548. if (lazy)
  549. {
  550. result->getStates().z(0)->addTransition(
  551. Transition<Data>(0, newFinal, 0));
  552. }
  553. newFinal->setFinal(true);
  554. for (State<Data>* beforeState : before->getStates())
  555. {
  556. State<Data>* newState
  557. = result->getStates().z(beforeState->getId());
  558. newState->setGroupStart(beforeState->getGroupStart());
  559. newState->setGroupEnd(beforeState->getGroupEnd());
  560. for (Transition<Data> transition :
  561. beforeState->getTransitions())
  562. {
  563. newState->addTransition(
  564. Transition<Data>(transition.getConditionFunction(),
  565. result->getStates().z(
  566. transition.zTargetState()->getId()),
  567. transition.getRequiredFlags()));
  568. }
  569. if (beforeState->isFinal())
  570. {
  571. newState->addTransition(
  572. Transition<Data>(0, result->getStates().z(0), 0));
  573. }
  574. }
  575. if (!lazy)
  576. {
  577. result->getStates().z(0)->addTransition(
  578. Transition<Data>(0, newFinal, 0));
  579. }
  580. before->release();
  581. return result;
  582. }
  583. template<typename Data>
  584. Automata<Data>* atLeastOnce(Automata<Data>* before, bool lazy)
  585. {
  586. Automata<Data>* result = new Automata<Data>();
  587. for (State<Data>* beforeState : before->getStates())
  588. {
  589. if (beforeState)
  590. {
  591. result->addState();
  592. }
  593. }
  594. State<Data>* newFinal = result->addState();
  595. newFinal->setFinal(true);
  596. for (State<Data>* beforeState : before->getStates())
  597. {
  598. State<Data>* newState
  599. = result->getStates().z(beforeState->getId());
  600. newState->setGroupStart(beforeState->getGroupStart());
  601. newState->setGroupEnd(beforeState->getGroupEnd());
  602. if (lazy && beforeState->isFinal())
  603. {
  604. newState->addTransition(Transition<Data>(0, newFinal, 0));
  605. }
  606. for (Transition<Data> transition :
  607. beforeState->getTransitions())
  608. {
  609. newState->addTransition(
  610. Transition<Data>(transition.getConditionFunction(),
  611. result->getStates().z(
  612. transition.zTargetState()->getId()),
  613. transition.getRequiredFlags()));
  614. }
  615. if (beforeState->isFinal())
  616. {
  617. newState->addTransition(
  618. Transition<Data>(0, result->getStates().z(0), 0));
  619. }
  620. if (!lazy && beforeState->isFinal())
  621. {
  622. newState->addTransition(Transition<Data>(0, newFinal, 0));
  623. }
  624. }
  625. before->release();
  626. return result;
  627. }
  628. template<typename Data> Automata<Data>* maybe(Automata<Data>* before)
  629. {
  630. Automata<Data>* result = new Automata<Data>();
  631. for (State<Data>* beforeState : before->getStates())
  632. {
  633. if (beforeState)
  634. {
  635. result->addState();
  636. }
  637. }
  638. State<Data>* newFinal = result->addState();
  639. newFinal->setFinal(true);
  640. for (State<Data>* beforeState : before->getStates())
  641. {
  642. State<Data>* newState
  643. = result->getStates().z(beforeState->getId());
  644. newState->setGroupStart(beforeState->getGroupStart());
  645. newState->setGroupEnd(beforeState->getGroupEnd());
  646. for (Transition<Data> transition :
  647. beforeState->getTransitions())
  648. {
  649. newState->addTransition(
  650. Transition<Data>(transition.getConditionFunction(),
  651. result->getStates().z(
  652. transition.zTargetState()->getId()),
  653. transition.getRequiredFlags()));
  654. }
  655. if (beforeState->isFinal())
  656. {
  657. newState->addTransition(Transition<Data>(0, newFinal, 0));
  658. }
  659. }
  660. result->getStates().z(0)->addTransition(
  661. Transition<Data>(0, newFinal, 0));
  662. before->release();
  663. return result;
  664. }
  665. template<typename Data> Automata<Data>* repeat(
  666. Automata<Data>* before, int minAmount, int maxAmount)
  667. {
  668. Automata<Data>* result = new Automata<Data>();
  669. for (int i = 0; i < maxAmount; i++)
  670. {
  671. for (State<Data>* beforeState : before->getStates())
  672. {
  673. if (beforeState)
  674. {
  675. result->addState();
  676. }
  677. }
  678. for (State<Data>* beforeState : before->getStates())
  679. {
  680. State<Data>* newState = result->getStates().z(
  681. beforeState->getId()
  682. + before->getStates().getEintragAnzahl() * i);
  683. newState->setGroupStart(beforeState->getGroupStart());
  684. newState->setGroupEnd(beforeState->getGroupEnd());
  685. for (Transition<Data> transition :
  686. beforeState->getTransitions())
  687. {
  688. newState->addTransition(Transition<Data>(
  689. transition.getConditionFunction(),
  690. result->getStates().z(
  691. transition.zTargetState()->getId()
  692. + before->getStates().getEintragAnzahl() * i),
  693. transition.getRequiredFlags()));
  694. }
  695. }
  696. }
  697. State<Data>* newFinal = result->addState();
  698. newFinal->setFinal(true);
  699. if (minAmount == 0)
  700. {
  701. result->getStates().z(0)->addTransition(
  702. Transition<Data>(0, newFinal, 0));
  703. }
  704. for (int i = 0; i < maxAmount; i++)
  705. {
  706. for (State<Data>* beforeState : before->getStates())
  707. {
  708. State<Data>* newState = result->getStates().z(
  709. beforeState->getId()
  710. + before->getStates().getEintragAnzahl() * i);
  711. if (beforeState->isFinal())
  712. {
  713. if (i < maxAmount - 1)
  714. {
  715. newState->addTransition(Transition<Data>(0,
  716. result->getStates().z(
  717. before->getStates().getEintragAnzahl()
  718. * (i + 1)),
  719. 0));
  720. }
  721. if (i >= minAmount - 1)
  722. {
  723. newState->addTransition(
  724. Transition<Data>(0, newFinal, 0));
  725. }
  726. }
  727. }
  728. }
  729. before->release();
  730. return result;
  731. }
  732. template<typename Data>
  733. Automata<Data>* group(Automata<Data>* before, int groupId)
  734. {
  735. Automata<Data>* result = new Automata<Data>();
  736. State<Data>* newStart = result->addState();
  737. newStart->setGroupStart(groupId);
  738. State<Data>* groupStart = result->addState();
  739. newStart->addTransition(Transition<Data>(0, groupStart, 0));
  740. for (State<Data>* beforeState : before->getStates())
  741. {
  742. if (beforeState)
  743. {
  744. result->addState();
  745. }
  746. }
  747. groupStart->addTransition(
  748. Transition<Data>(0, result->getStates().z(2), 0));
  749. State<Data>* groupEnd = result->addState();
  750. groupEnd->setGroupEnd(groupId);
  751. State<Data>* newFinal = result->addState();
  752. groupEnd->addTransition(Transition<Data>(0, newFinal, 0));
  753. newFinal->setFinal(true);
  754. for (State<Data>* beforeState : before->getStates())
  755. {
  756. State<Data>* newState
  757. = result->getStates().z(beforeState->getId() + 2);
  758. newState->setGroupStart(beforeState->getGroupStart());
  759. newState->setGroupEnd(beforeState->getGroupEnd());
  760. for (Transition<Data> transition :
  761. beforeState->getTransitions())
  762. {
  763. newState->addTransition(
  764. Transition<Data>(transition.getConditionFunction(),
  765. result->getStates().z(
  766. transition.zTargetState()->getId() + 2),
  767. transition.getRequiredFlags()));
  768. }
  769. if (beforeState->isFinal())
  770. {
  771. newState->addTransition(Transition<Data>(0, groupEnd, 0));
  772. }
  773. }
  774. before->release();
  775. return result;
  776. }
  777. class RegexConfig
  778. {
  779. private:
  780. Text whitespaceChars;
  781. Text wordChars;
  782. public:
  783. DLLEXPORT RegexConfig();
  784. DLLEXPORT void setWhitespaceChars(Text whitespaceChars);
  785. DLLEXPORT void setWordChars(Text wordChars);
  786. DLLEXPORT Text getWhitespaceChars() const;
  787. DLLEXPORT Text getWordChars() const;
  788. };
  789. DLLEXPORT Automata<char>* parse(Text regex);
  790. DLLEXPORT Automata<char>* parse(Text regex, RegexConfig& config);
  791. } // namespace Regex
  792. } // namespace Framework