Regex.h 30 KB

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