IOSS 2.0
Ioss_Glob.h
Go to the documentation of this file.
1// Apache License
2// Version 2.0, January 2004
3// http://www.apache.org/licenses/
4// https://github.com/alexst07/glob-cpp
5
6#pragma once
7
8#include <exception>
9#include <iostream>
10#include <memory>
11#include <stddef.h>
12#include <string>
13#include <tuple>
14#include <vector>
15
16#include "ioss_export.h"
17
18namespace Ioss::glob {
19
20 template <class charT> using String = std::basic_string<charT>;
21
22 template <class charT> class Automata;
23
24 class IOSS_EXPORT Error : public std::exception
25 {
26 public:
27 explicit Error(std::string msg) : msg_{std::move(msg)} {}
28 ~Error() override;
29
30 const char *what() const noexcept override { return msg_.c_str(); }
31
32 private:
33 std::string msg_;
34 };
35
46
47 // From cppreference.com
48 template <class T, class U = T> T exchange(T &obj, U &&new_value)
49 {
50 T old_value = std::move(obj);
51 obj = std::forward<U>(new_value);
52 return old_value;
53 }
54
55 template <class charT> class State
56 {
57 public:
58 State(StateType type, Automata<charT> &states) : type_(type), states_(states) {}
59
60 virtual ~State() = default;
61
62 virtual bool Check(const String<charT> &str, size_t pos) = 0;
63
64 virtual std::tuple<size_t, size_t> Next(const String<charT> &str, size_t pos) = 0;
65
66 StateType Type() const { return type_; }
67
69
70 void AddNextState(size_t state_pos) { next_states_.push_back(state_pos); }
71
72 const std::vector<size_t> &GetNextStates() const { return next_states_; }
73
75
76 virtual void ResetState() {}
77
78 protected:
79 void SetMatchedStr(const String<charT> &str) { matched_str_ = str; }
80
81 void SetMatchedStr(charT c) { matched_str_ = c; }
82
83 private:
86 std::vector<size_t> next_states_;
88 };
89
90 template <class charT> class StateFail : public State<charT>
91 {
92 public:
93 explicit StateFail(Automata<charT> &states) : State<charT>(StateType::FAIL, states) {}
94
95 bool Check(const String<charT> &, size_t) override { return false; }
96
97 std::tuple<size_t, size_t> Next(const String<charT> &, size_t pos) override
98 {
99 return {0, ++pos};
100 }
101 };
102
103 template <class charT> class StateMatch : public State<charT>
104 {
105 public:
106 explicit StateMatch(Automata<charT> &states) : State<charT>(StateType::MATCH, states) {}
107
108 bool Check(const String<charT> &, size_t) override { return true; }
109
110 std::tuple<size_t, size_t> Next(const String<charT> &, size_t pos) override
111 {
112 return {0, ++pos};
113 }
114 };
115
116 template <class charT> class Automata
117 {
118 public:
119 Automata() = default;
120
121 Automata(const Automata<charT> &) = delete;
122
123 Automata<charT> &operator=(const Automata<charT> &automata) = delete;
124
126 : states_{std::move(automata.states_)}, match_state_{automata.match_state_},
127 fail_state_{exchange(automata.fail_state_, 0)},
129 {
130 }
131
133 {
134 states_ = std::move(automata.states_);
135 match_state_ = automata.match_state_;
136 fail_state_ = automata.fail_state_;
137 start_state_ = automata.start_state_;
138
139 return *this;
140 }
141
142 const State<charT> &GetState(size_t pos) const { return *states_[pos]; }
143
144 State<charT> &GetState(size_t pos) { return *states_[pos]; }
145
146 size_t FailState() const { return fail_state_; }
147
148 Automata<charT> &SetFailState(size_t state_pos)
149 {
150 fail_state_ = state_pos;
151 return *this;
152 }
153
155 {
156 match_state_ = state_pos;
157 return *this;
158 }
159
160 size_t GetNumStates() const { return states_.size(); }
161
162 std::tuple<bool, size_t> Exec(const String<charT> &str, bool comp_end = true)
163 {
164 auto r = ExecAux(str, comp_end);
165 ResetStates();
166 return r;
167 }
168
169 std::vector<String<charT>> GetMatchedStrings() const
170 {
171 std::vector<String<charT>> vec;
172
173 for (auto &state : states_) {
174 if (state->Type() == StateType::MULT || state->Type() == StateType::QUESTION ||
175 state->Type() == StateType::GROUP || state->Type() == StateType::SET) {
176 vec.push_back(state->MatchedStr());
177 }
178 }
179
180 return vec;
181 }
182
183 template <class T, typename... Args> size_t NewState(Args &&...args)
184 {
185 size_t state_pos = states_.size();
186 auto state = std::unique_ptr<State<charT>>(new T(*this, std::forward<Args>(args)...));
187
188 states_.push_back(std::move(state));
189 return state_pos;
190 }
191
192 size_t fail_state_{0};
193
194 private:
195 std::tuple<bool, size_t> ExecAux(const String<charT> &str, bool comp_end = true) const
196 {
197 size_t state_pos = 0;
198 size_t str_pos = 0;
199
200 // run the state vector until state reaches fail or match state, or
201 // until the string is all consumed
202 while (state_pos != fail_state_ && state_pos != match_state_ && str_pos < str.length()) {
203 std::tie(state_pos, str_pos) = states_[state_pos]->Next(str, str_pos);
204 }
205
206 // if comp_end is true it matches only if the automata reached the end of
207 // the string
208 if (comp_end) {
209 if ((state_pos == match_state_) && (str_pos == str.length())) {
210 return {true, str_pos};
211 }
212
213 return {false, str_pos};
214 }
215 // if comp_end is false, compare only if the states reached the
216 // match state
217 return {state_pos == match_state_, str_pos};
218 }
219
221 {
222 for (auto &state : states_) {
223 state->ResetState();
224 }
225 }
226
227 std::vector<std::unique_ptr<State<charT>>> states_;
228 size_t match_state_{};
229
230 size_t start_state_{0};
231 };
232
233 template <class charT> class StateChar : public State<charT>
234 {
235 using State<charT>::GetNextStates;
236 using State<charT>::GetAutomata;
237
238 public:
239 StateChar(Automata<charT> &states, charT c) : State<charT>(StateType::CHAR, states), c_{c} {}
240
241 bool Check(const String<charT> &str, size_t pos) override { return (c_ == str[pos]); }
242
243 std::tuple<size_t, size_t> Next(const String<charT> &str, size_t pos) override
244 {
245 if (c_ == str[pos]) {
246 this->SetMatchedStr(c_);
247 return std::tuple<size_t, size_t>(GetNextStates()[0], pos + 1);
248 }
249
250 return std::tuple<size_t, size_t>(GetAutomata().FailState(), pos + 1);
251 }
252
253 private:
254 charT c_;
255 };
256
257 template <class charT> class StateAny : public State<charT>
258 {
259 using State<charT>::GetNextStates;
260 using State<charT>::GetAutomata;
261
262 public:
263 explicit StateAny(Automata<charT> &states) : State<charT>(StateType::QUESTION, states) {}
264
265 bool Check(const String<charT> &, size_t) override
266 {
267 // as it match any char, it is always tried
268 return true;
269 }
270
271 std::tuple<size_t, size_t> Next(const String<charT> &str, size_t pos) override
272 {
273 this->SetMatchedStr(str[pos]);
274 // state any always match with any char
275 return std::tuple<size_t, size_t>(GetNextStates()[0], pos + 1);
276 }
277 };
278
279 template <class charT> class StateStar : public State<charT>
280 {
281 using State<charT>::GetNextStates;
282 using State<charT>::GetAutomata;
283
284 public:
285 explicit StateStar(Automata<charT> &states) : State<charT>(StateType::MULT, states) {}
286
287 bool Check(const String<charT> &, size_t) override
288 {
289 // as it match any char, it is always tried
290 return true;
291 }
292
293 std::tuple<size_t, size_t> Next(const String<charT> &str, size_t pos) override
294 {
295 // next state vector from StateStar has two elements, the element 0 points
296 // to the same state, and the element points to next state if the
297 // conditions is satisfied
298 if (GetAutomata().GetState(GetNextStates()[1]).Type() == StateType::MATCH) {
299 // this case occurs when star is in the end of the glob, so the pos is
300 // the end of the string, because all string is consumed
301 this->SetMatchedStr(str.substr(pos));
302 return std::tuple<size_t, size_t>(GetNextStates()[1], str.length());
303 }
304
305 bool res = GetAutomata().GetState(GetNextStates()[1]).Check(str, pos);
306 // if the next state is satisfied goes to next state
307 if (res) {
308 return std::tuple<size_t, size_t>(GetNextStates()[1], pos);
309 }
310
311 // while the next state check is false, the string is consumed by star state
312 this->SetMatchedStr(this->MatchedStr() + str[pos]);
313 return std::tuple<size_t, size_t>(GetNextStates()[0], pos + 1);
314 }
315 };
316
317 template <class charT> class SetItem
318 {
319 public:
320 SetItem() = default;
321 virtual ~SetItem() = default;
322
323 virtual bool Check(charT c) const = 0;
324 };
325
326 template <class charT> class SetItemChar : public SetItem<charT>
327 {
328 public:
329 explicit SetItemChar(charT c) : c_{c} {}
330
331 bool Check(charT c) const override { return c == c_; }
332
333 private:
334 charT c_;
335 };
336
337 template <class charT> class SetItemRange : public SetItem<charT>
338 {
339 public:
340 SetItemRange(charT start, charT end)
341 : start_{start < end ? start : end}, end_{start < end ? end : start}
342 {
343 }
344
345 bool Check(charT c) const override { return (c >= start_) && (c <= end_); }
346
347 private:
348 charT start_;
349 charT end_;
350 };
351
352 template <class charT> class StateSet : public State<charT>
353 {
354 using State<charT>::GetNextStates;
355 using State<charT>::GetAutomata;
356
357 public:
358 StateSet(Automata<charT> &states, std::vector<std::unique_ptr<SetItem<charT>>> items,
359 bool neg = false)
360 : State<charT>(StateType::SET, states), items_{std::move(items)}, neg_{neg}
361 {
362 }
363
364 bool SetCheck(const String<charT> &str, size_t pos) const
365 {
366 // TODO: Replace with std::any_of()
367 for (auto &item : items_) {
368 // if any item match, then the set match with char
369 if (item.get()->Check(str[pos])) {
370 return true;
371 }
372 }
373
374 return false;
375 }
376
377 bool Check(const String<charT> &str, size_t pos) override
378 {
379 if (neg_) {
380 return !SetCheck(str, pos);
381 }
382
383 return SetCheck(str, pos);
384 }
385
386 std::tuple<size_t, size_t> Next(const String<charT> &str, size_t pos) override
387 {
388 if (Check(str, pos)) {
389 this->SetMatchedStr(str[pos]);
390 return std::tuple<size_t, size_t>(GetNextStates()[0], pos + 1);
391 }
392
393 return std::tuple<size_t, size_t>(GetAutomata().FailState(), pos + 1);
394 }
395
396 private:
397 std::vector<std::unique_ptr<SetItem<charT>>> items_;
398 bool neg_;
399 };
400
401 template <class charT> class StateGroup : public State<charT>
402 {
403 using State<charT>::GetNextStates;
404 using State<charT>::GetAutomata;
405
406 public:
407 enum class Type { BASIC, ANY, STAR, PLUS, NEG, AT };
408
410 std::vector<std::unique_ptr<Automata<charT>>> &&automatas)
411 : State<charT>(StateType::GROUP, states), type_{type}, automatas_{std::move(automatas)}
412 {
413 }
414
415 void ResetState() override { match_one_ = false; }
416
417 std::tuple<bool, size_t> BasicCheck(const String<charT> &str, size_t pos)
418 {
419 String<charT> str_part = str.substr(pos);
420 bool r;
421 size_t str_pos = 0;
422
423 // each automata is a part of a union of the group, in basic check,
424 // we want find only if any automata is true
425 for (auto &automata : automatas_) {
426 std::tie(r, str_pos) = automata->Exec(str_part, false);
427 if (r) {
428 return {r, pos + str_pos};
429 }
430 }
431
432 return {false, pos + str_pos};
433 }
434
435 bool Check(const String<charT> &str, size_t pos) override
436 {
437 bool r = false;
438 switch (type_) {
439 case Type::BASIC:
440 case Type::AT:
441 case Type::ANY:
442 case Type::STAR:
443 case Type::PLUS:
444 case Type::NEG: {
445 std::tie(r, std::ignore) = BasicCheck(str, pos);
446 break;
447 }
448 }
449 return r;
450 }
451
452 std::tuple<size_t, size_t> Next(const String<charT> &str, size_t pos) override
453 {
454 // STATE 1 -> is the next state
455 // STATE 0 -> is the same state
456 switch (type_) {
457 case Type::BASIC:
458 case Type::AT: {
459 return NextBasic(str, pos);
460 }
461
462 case Type::ANY: {
463 return NextAny(str, pos);
464 }
465
466 case Type::STAR: {
467 return NextStar(str, pos);
468 }
469
470 case Type::PLUS: {
471 return NextPlus(str, pos);
472 }
473
474 case Type::NEG: {
475 return NextNeg(str, pos);
476 }
477 }
478 return {0, 0};
479 }
480
481 std::tuple<size_t, size_t> NextNeg(const String<charT> &str, size_t pos)
482 {
483 bool r;
484 size_t new_pos;
485 std::tie(r, new_pos) = BasicCheck(str, pos);
486 if (r) {
487 this->SetMatchedStr(this->MatchedStr() + str.substr(pos, new_pos - pos));
488 return std::tuple<size_t, size_t>(GetAutomata().FailState(), new_pos);
489 }
490
491 return std::tuple<size_t, size_t>(GetNextStates()[1], pos);
492 }
493
494 std::tuple<size_t, size_t> NextBasic(const String<charT> &str, size_t pos)
495 {
496 bool r;
497 size_t new_pos;
498 std::tie(r, new_pos) = BasicCheck(str, pos);
499 if (r) {
500 this->SetMatchedStr(this->MatchedStr() + str.substr(pos, new_pos - pos));
501 return std::tuple<size_t, size_t>(GetNextStates()[1], new_pos);
502 }
503
504 return std::tuple<size_t, size_t>(GetAutomata().FailState(), new_pos);
505 }
506
507 std::tuple<size_t, size_t> NextAny(const String<charT> &str, size_t pos)
508 {
509 bool r;
510 size_t new_pos;
511 std::tie(r, new_pos) = BasicCheck(str, pos);
512 if (r) {
513 this->SetMatchedStr(this->MatchedStr() + str.substr(pos, new_pos - pos));
514 return std::tuple<size_t, size_t>(GetNextStates()[1], new_pos);
515 }
516
517 return std::tuple<size_t, size_t>(GetNextStates()[1], pos);
518 }
519
520 std::tuple<size_t, size_t> NextStar(const String<charT> &str, size_t pos)
521 {
522 bool r;
523 size_t new_pos;
524 std::tie(r, new_pos) = BasicCheck(str, pos);
525 if (r) {
526 this->SetMatchedStr(this->MatchedStr() + str.substr(pos, new_pos - pos));
527 if (GetAutomata().GetState(GetNextStates()[1]).Type() == StateType::MATCH &&
528 new_pos == str.length()) {
529 return std::tuple<size_t, size_t>(GetNextStates()[1], new_pos);
530 }
531 return std::tuple<size_t, size_t>(GetNextStates()[0], new_pos);
532 }
533
534 return std::tuple<size_t, size_t>(GetNextStates()[1], pos);
535 }
536
537 std::tuple<size_t, size_t> NextPlus(const String<charT> &str, size_t pos)
538 {
539 bool r;
540 size_t new_pos;
541 std::tie(r, new_pos) = BasicCheck(str, pos);
542 if (r) {
543 match_one_ = true;
544 this->SetMatchedStr(this->MatchedStr() + str.substr(pos, new_pos - pos));
545
546 // if it matches and the string reached at the end, and the next
547 // state is the match state, goes to next state to avoid state mistake
548 if (GetAutomata().GetState(GetNextStates()[1]).Type() == StateType::MATCH &&
549 new_pos == str.length()) {
550 return std::tuple<size_t, size_t>(GetNextStates()[1], new_pos);
551 }
552 return std::tuple<size_t, size_t>(GetNextStates()[0], new_pos);
553 }
554
555 // case where the next state matches and the group already matched
556 // one time -> goes to next state
557 bool res = GetAutomata().GetState(GetNextStates()[1]).Check(str, pos);
558 if (res && match_one_) {
559 return std::tuple<size_t, size_t>(GetNextStates()[1], pos);
560 }
561
562 if (match_one_) {
563 return std::tuple<size_t, size_t>(GetNextStates()[1], pos);
564 }
565 return std::tuple<size_t, size_t>(GetAutomata().FailState(), new_pos);
566 }
567
568 private:
570 std::vector<std::unique_ptr<Automata<charT>>> automatas_;
571 bool match_one_{false};
572 };
573
574#define TOKEN(X, Y) X,
575 enum class TokenKind {
578 TOKEN(EOS, "end of source") TOKEN(SUB, "-") TOKEN(STAR, "*") TOKEN(QUESTION, "?")
579 TOKEN(LPAREN, "(") TOKEN(QUESTLPAREN, "?(") TOKEN(STARLPAREN, "*(") TOKEN(PLUSLPAREN, "+(")
580 TOKEN(NEGLPAREN, "!(") TOKEN(ATLPAREN, "@(") TOKEN(RPAREN, ")") TOKEN(UNION, "|")
581 TOKEN(LBRACKET, "[") TOKEN(RBRACKET, "]") TOKEN(NEGLBRACKET, "[^") NUM_TOKENS
582 };
583#undef TOKEN
584
585#define TOKEN(X, Y) #X,
586 static const char *token_name_str[] = {
587 "UNKNOWN", // UNKNOWN
588 "CHAR",
589 TOKEN(EOS, "end of source") TOKEN(SUB, "-") TOKEN(STAR, "*") TOKEN(QUESTION, "?")
590 TOKEN(LPAREN, "(") TOKEN(QUESTLPAREN, "?(") TOKEN(STARLPAREN, "*(")
591 TOKEN(PLUSLPAREN, "+(") TOKEN(NEGLPAREN, "!(") TOKEN(ATLPAREN, "@(")
592 TOKEN(RPAREN, ")") TOKEN(UNION, "|") TOKEN(LBRACKET, "[") TOKEN(RBRACKET, "]")
593 TOKEN(NEGLBRACKET, "[^") ""};
594#undef TOKEN
595
596 template <class charT> class Token
597 {
598 public:
599 explicit Token(TokenKind kind) : kind_{kind} {}
600 Token(TokenKind kind, charT value) : kind_{kind}, value_{value} {}
601 TokenKind Kind() const { return kind_; }
602
603 charT Value() const { return value_; }
604
605 bool operator==(TokenKind kind) { return kind_ == kind; }
606
607 bool operator==(TokenKind kind) const { return kind_ == kind; }
608
609 bool operator!=(TokenKind kind) { return kind_ != kind; }
610
611 bool operator!=(TokenKind kind) const { return kind_ != kind; }
612
613 private:
614 template <class charU>
615 friend std::ostream &operator<<(std::ostream &stream, const Token<charU> &token);
616
618 charT value_{};
619 };
620
621 template <class charT>
622 inline std::ostream &operator<<(std::ostream &stream, const Token<charT> &token)
623 {
624 stream << '[' << token_name_str[static_cast<int>(token.kind_)] << ']';
625 return stream;
626 }
627
628 template <class charT> class Lexer
629 {
630 public:
631 static const char kEndOfInput = -1;
632
633 explicit Lexer(const String<charT> &str) : str_(str), c_{str[0]} {}
634
635 std::vector<Token<charT>> Scanner()
636 {
637 std::vector<Token<charT>> tokens;
638 while (true) {
639 switch (c_) {
640 case '?': {
641 Advance();
642 if (c_ == '(') {
643 tokens.push_back(Select(TokenKind::QUESTLPAREN));
644 Advance();
645 }
646 else {
647 tokens.push_back(Select(TokenKind::QUESTION));
648 }
649 break;
650 }
651
652 case '*': {
653 Advance();
654 if (c_ == '(') {
655 tokens.push_back(Select(TokenKind::STARLPAREN));
656 Advance();
657 }
658 else {
659 tokens.push_back(Select(TokenKind::STAR));
660 }
661 break;
662 }
663
664 case '+': {
665 Advance();
666 if (c_ == '(') {
667 tokens.push_back(Select(TokenKind::PLUSLPAREN));
668 Advance();
669 }
670 else {
671 tokens.push_back(Select(TokenKind::CHAR, '+'));
672 }
673 break;
674 }
675
676 case '-': {
677 tokens.push_back(Select(TokenKind::SUB));
678 Advance();
679 break;
680 }
681
682 case '|': {
683 tokens.push_back(Select(TokenKind::UNION));
684 Advance();
685 break;
686 }
687
688 case '@': {
689 Advance();
690 if (c_ == '(') {
691 tokens.push_back(Select(TokenKind::ATLPAREN));
692 Advance();
693 }
694 else {
695 tokens.push_back(Select(TokenKind::CHAR, '@'));
696 }
697 break;
698 }
699
700 case '!': {
701 Advance();
702 if (c_ == '(') {
703 tokens.push_back(Select(TokenKind::NEGLPAREN));
704 Advance();
705 }
706 else {
707 tokens.push_back(Select(TokenKind::CHAR, '!'));
708 }
709 break;
710 }
711
712 case '(': {
713 tokens.push_back(Select(TokenKind::LPAREN));
714 Advance();
715 break;
716 }
717
718 case ')': {
719 tokens.push_back(Select(TokenKind::RPAREN));
720 Advance();
721 break;
722 }
723
724 case '[': {
725 Advance();
726 if (c_ == '!') {
727 tokens.push_back(Select(TokenKind::NEGLBRACKET));
728 Advance();
729 }
730 else {
731 tokens.push_back(Select(TokenKind::LBRACKET));
732 }
733 break;
734 }
735
736 case ']': {
737 tokens.push_back(Select(TokenKind::RBRACKET));
738 Advance();
739 break;
740 }
741
742 case '\\': {
743 Advance();
744 if (c_ == kEndOfInput) {
745 throw Error("No valid char after '\\'");
746 }
747 tokens.push_back(Select(TokenKind::CHAR, c_));
748 Advance();
749 break;
750 }
751
752 default: {
753 if (c_ == kEndOfInput) {
754 tokens.push_back(Select(TokenKind::EOS));
755 return tokens;
756 }
757 tokens.push_back(Select(TokenKind::CHAR, c_));
758 Advance();
759 }
760 }
761 }
762 }
763
764 private:
765 inline Token<charT> Select(TokenKind k) { return Token<charT>(k); }
766
767 inline Token<charT> Select(TokenKind k, charT value) { return Token<charT>(k, value); }
768
769 void Advance()
770 {
771 if (pos_ == (str_.length() - 1)) {
772 c_ = kEndOfInput;
773 return;
774 }
775
776 c_ = str_[++pos_];
777 }
778
780 size_t pos_{0};
781 charT c_;
782 };
783
784#define GLOB_AST_NODE_LIST(V) \
785 V(CharNode) \
786 V(RangeNode) \
787 V(SetItemsNode) \
788 V(PositiveSetNode) \
789 V(NegativeSetNode) \
790 V(StarNode) \
791 V(AnyNode) \
792 V(GroupNode) \
793 V(ConcatNode) \
794 V(UnionNode) \
795 V(GlobNode)
796
797 template <class charT> class AstVisitor;
798
799// declare all classes used for nodes
800#define DECLARE_TYPE_CLASS(type) template <class charT> class type;
802
803#undef DECLARE_TYPE_CLASS
804
805 template <class charT> class AstNode
806 {
807 public:
823
824 virtual ~AstNode() = default;
825
826 Type GetType() const { return type_; }
827
828 virtual void Accept(AstVisitor<charT> *visitor) = 0;
829
830 protected:
831 explicit AstNode(Type type) : type_{type} {}
832
833 private:
835 };
836
837 template <class charT> using AstNodePtr = std::unique_ptr<AstNode<charT>>;
838
839 template <class charT> class AstVisitor
840 {
841 public:
842 virtual ~AstVisitor() = default;
843// define all visitor methods for the nodes
844#define DECLARE_VIRTUAL_FUNC(type) \
845 virtual void Visit##type(type<charT> * /*node*/) {}
847#undef DECLARE_VIRTUAL_FUNC
848 };
849
850 template <class charT> class CharNode : public AstNode<charT>
851 {
852 public:
853 explicit CharNode(charT c) : AstNode<charT>(AstNode<charT>::Type::CHAR), c_{c} {}
854
855 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitCharNode(this); }
856
857 char GetValue() const { return c_; }
858
859 private:
860 charT c_;
861 };
862
863 template <class charT> class RangeNode : public AstNode<charT>
864 {
865 public:
867 : AstNode<charT>(AstNode<charT>::Type::RANGE), start_{std::move(start)},
868 end_{std::move(end)}
869 {
870 }
871
872 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitRangeNode(this); }
873
874 AstNode<charT> *GetStart() const { return start_.get(); }
875
876 AstNode<charT> *GetEnd() const { return end_.get(); }
877
878 private:
881 };
882
883 template <class charT> class SetItemsNode : public AstNode<charT>
884 {
885 public:
886 explicit SetItemsNode(std::vector<AstNodePtr<charT>> &&items)
887 : AstNode<charT>(AstNode<charT>::Type::SET_ITEMS), items_{std::move(items)}
888 {
889 }
890
891 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitSetItemsNode(this); }
892
893 std::vector<AstNodePtr<charT>> &GetItems() { return items_; }
894
895 private:
896 std::vector<AstNodePtr<charT>> items_;
897 };
898
899 template <class charT> class PositiveSetNode : public AstNode<charT>
900 {
901 public:
903 : AstNode<charT>(AstNode<charT>::Type::POS_SET), set_{std::move(set)}
904 {
905 }
906
907 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitPositiveSetNode(this); }
908
909 AstNode<charT> *GetSet() { return set_.get(); }
910
911 private:
913 };
914
915 template <class charT> class NegativeSetNode : public AstNode<charT>
916 {
917 public:
919 : AstNode<charT>(AstNode<charT>::Type::NEG_SET), set_{std::move(set)}
920 {
921 }
922
923 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitNegativeSetNode(this); }
924
925 AstNode<charT> *GetSet() { return set_.get(); }
926
927 private:
929 };
930
931 template <class charT> class StarNode : public AstNode<charT>
932 {
933 public:
934 StarNode() : AstNode<charT>(AstNode<charT>::Type::STAR) {}
935
936 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitStarNode(this); }
937 };
938
939 template <class charT> class AnyNode : public AstNode<charT>
940 {
941 public:
942 AnyNode() : AstNode<charT>(AstNode<charT>::Type::ANY) {}
943
944 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitAnyNode(this); }
945 };
946
947 template <class charT> class GroupNode : public AstNode<charT>
948 {
949 public:
950 enum class GroupType { BASIC, ANY, STAR, PLUS, NEG, AT };
951
953 : AstNode<charT>(AstNode<charT>::Type::GROUP), glob_{std::move(glob)},
954 group_type_{group_type}
955 {
956 }
957
958 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitGroupNode(this); }
959
960 AstNode<charT> *GetGlob() { return glob_.get(); }
961
963
964 private:
967 };
968
969 template <class charT> class ConcatNode : public AstNode<charT>
970 {
971 public:
972 explicit ConcatNode(std::vector<AstNodePtr<charT>> &&basic_glob)
973 : AstNode<charT>(AstNode<charT>::Type::CONCAT_GLOB), basic_glob_{std::move(basic_glob)}
974 {
975 }
976
977 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitConcatNode(this); }
978
979 std::vector<AstNodePtr<charT>> &GetBasicGlobs() { return basic_glob_; }
980
981 private:
982 std::vector<AstNodePtr<charT>> basic_glob_;
983 };
984
985 template <class charT> class UnionNode : public AstNode<charT>
986 {
987 public:
988 explicit UnionNode(std::vector<AstNodePtr<charT>> &&items)
989 : AstNode<charT>(AstNode<charT>::Type::UNION), items_{std::move(items)}
990 {
991 }
992
993 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitUnionNode(this); }
994
995 std::vector<AstNodePtr<charT>> &GetItems() { return items_; }
996
997 private:
998 std::vector<AstNodePtr<charT>> items_;
999 };
1000
1001 template <class charT> class GlobNode : public AstNode<charT>
1002 {
1003 public:
1005 : AstNode<charT>(AstNode<charT>::Type::GLOB), glob_{std::move(glob)}
1006 {
1007 }
1008
1009 void Accept(AstVisitor<charT> *visitor) override { visitor->VisitGlobNode(this); }
1010
1011 AstNode<charT> *GetConcat() { return glob_.get(); }
1012
1013 private:
1015 };
1016
1017 template <class charT> class Parser
1018 {
1019 public:
1020 Parser() = delete;
1021
1022 explicit Parser(std::vector<Token<charT>> &&tok_vec) : tok_vec_{std::move(tok_vec)}, pos_{0} {}
1023
1025
1026 private:
1028 {
1029 Token<charT> &tk = NextToken();
1030 if (tk != TokenKind::CHAR) {
1031 throw Error("char expected");
1032 }
1033
1034 charT c = tk.Value();
1035 return AstNodePtr<charT>(new CharNode<charT>(c));
1036 }
1037
1039 {
1040 AstNodePtr<charT> char_start = ParserChar();
1041
1042 Token<charT> &tk = NextToken();
1043 if (tk != TokenKind::SUB) {
1044 throw Error("range expected");
1045 }
1046
1047 AstNodePtr<charT> char_end = ParserChar();
1048 return AstNodePtr<charT>(new RangeNode<charT>(std::move(char_start), std::move(char_end)));
1049 }
1050
1052 {
1053 if (PeekAhead() == TokenKind::SUB) {
1054 return ParserRange();
1055 }
1056
1057 return ParserChar();
1058 }
1059
1061 {
1062 std::vector<AstNodePtr<charT>> items;
1063
1064 do {
1065 items.push_back(ParserSetItem());
1066 } while (GetToken() != TokenKind::RBRACKET);
1067
1068 Advance();
1069
1070 return AstNodePtr<charT>(new SetItemsNode<charT>(std::move(items)));
1071 }
1072
1074 {
1075 Token<charT> &tk = NextToken();
1076
1077 if (tk == TokenKind::LBRACKET) {
1079 }
1080 if (tk == TokenKind::NEGLBRACKET) {
1082 }
1083 throw Error("set expected");
1084 }
1085
1087 {
1088 Token<charT> &tk = GetToken();
1089
1090 switch (tk.Kind()) {
1091 case TokenKind::QUESTION: Advance(); return AstNodePtr<charT>(new AnyNode<charT>());
1092
1093 case TokenKind::STAR: Advance(); return AstNodePtr<charT>(new StarNode<charT>());
1094
1095 case TokenKind::SUB: Advance(); return AstNodePtr<charT>(new CharNode<charT>('-'));
1096
1097 case TokenKind::CHAR: return ParserChar();
1098
1099 case TokenKind::LBRACKET:
1100 case TokenKind::NEGLBRACKET: return ParserSet();
1101
1102 case TokenKind::LPAREN:
1103 case TokenKind::QUESTLPAREN:
1104 case TokenKind::STARLPAREN:
1105 case TokenKind::PLUSLPAREN:
1106 case TokenKind::NEGLPAREN:
1107 case TokenKind::ATLPAREN: return ParserGroup();
1108
1109 default: throw Error("basic glob expected");
1110 }
1111 }
1112
1114 {
1115 typename GroupNode<charT>::GroupType type;
1116 Token<charT> &tk = NextToken();
1117
1118 switch (tk.Kind()) {
1119 case TokenKind::LPAREN: type = GroupNode<charT>::GroupType::BASIC; break;
1120
1121 case TokenKind::QUESTLPAREN: type = GroupNode<charT>::GroupType::ANY; break;
1122
1123 case TokenKind::STARLPAREN: type = GroupNode<charT>::GroupType::STAR; break;
1124
1125 case TokenKind::PLUSLPAREN: type = GroupNode<charT>::GroupType::PLUS; break;
1126
1127 case TokenKind::NEGLPAREN: type = GroupNode<charT>::GroupType::NEG; break;
1128
1129 case TokenKind::ATLPAREN: type = GroupNode<charT>::GroupType::AT; break;
1130
1131 default: throw Error("Not valid group"); break;
1132 }
1133
1134 AstNodePtr<charT> group_glob = ParserUnion();
1135 tk = NextToken();
1136 if (tk != TokenKind::RPAREN) {
1137 throw Error("Expected ')' at and of group");
1138 }
1139
1140 return AstNodePtr<charT>(new GroupNode<charT>(type, std::move(group_glob)));
1141 }
1142
1144 {
1145 auto check_end = [&]() -> bool {
1146 Token<charT> &tk = GetToken();
1147
1148 switch (tk.Kind()) {
1149 case TokenKind::EOS:
1150 case TokenKind::RPAREN:
1151 case TokenKind::UNION: return true;
1152
1153 default: return false;
1154 }
1155 };
1156
1157 std::vector<AstNodePtr<charT>> parts;
1158
1159 while (!check_end()) {
1160 parts.push_back(ParserBasicGlob());
1161 }
1162
1163 return AstNodePtr<charT>(new ConcatNode<charT>(std::move(parts)));
1164 }
1165
1167 {
1168 std::vector<AstNodePtr<charT>> items;
1169 items.push_back(ParserConcat());
1170
1171 while (GetToken() == TokenKind::UNION) {
1172 Advance();
1173 items.push_back(ParserConcat());
1174 }
1175
1176 return AstNodePtr<charT>(new UnionNode<charT>(std::move(items)));
1177 }
1178
1180 {
1182
1183 if (GetToken() != TokenKind::EOS) {
1184 throw Error("Expected the end of glob");
1185 }
1186
1187 return AstNodePtr<charT>(new GlobNode<charT>(std::move(glob)));
1188 }
1189
1190 inline const Token<charT> &GetToken() const { return tok_vec_.at(pos_); }
1191
1192 inline Token<charT> &GetToken() { return tok_vec_.at(pos_); }
1193
1194 inline const Token<charT> &PeekAhead() const
1195 {
1196 if (pos_ >= (tok_vec_.size() - 1)) {
1197 return tok_vec_.back();
1198 }
1199
1200 return tok_vec_.at(pos_ + 1);
1201 }
1202
1204 {
1205 if (pos_ >= (tok_vec_.size() - 1)) {
1206 return tok_vec_.back();
1207 }
1208
1209 Token<charT> &tk = tok_vec_.at(pos_);
1210 pos_++;
1211 return tk;
1212 }
1213
1214 inline bool Advance()
1215 {
1216 if (pos_ == tok_vec_.size() - 1) {
1217 return false;
1218 }
1219
1220 ++pos_;
1221 return true;
1222 }
1223
1224 inline size_t Size() const noexcept { return tok_vec_.size(); }
1225
1226 std::vector<Token<charT>> tok_vec_;
1227 size_t pos_;
1228 };
1229
1230 template <class charT> class AstConsumer
1231 {
1232 public:
1233 AstConsumer() = default;
1234
1235 void GenAutomata(AstNode<charT> *root_node, Automata<charT> &automata)
1236 {
1237 AstNode<charT> *concat_node = static_cast<GlobNode<charT> *>(root_node)->GetConcat();
1238 ExecConcat(concat_node, automata);
1239
1240 size_t match_state = automata.template NewState<StateMatch<charT>>();
1241 automata.GetState(preview_state_).AddNextState(match_state);
1242 automata.SetMatchState(match_state);
1243
1244 size_t fail_state = automata.template NewState<StateFail<charT>>();
1245 automata.SetFailState(fail_state);
1246 }
1247
1248 private:
1250 {
1251 auto *concat_node = static_cast<ConcatNode<charT> *>(node);
1252 auto &basic_globs = concat_node->GetBasicGlobs();
1253
1254 for (auto &basic_glob : basic_globs) {
1255 ExecBasicGlob(basic_glob.get(), automata);
1256 }
1257 }
1258
1260 {
1261 switch (node->GetType()) {
1262 case AstNode<charT>::Type::CHAR: ExecChar(node, automata); break;
1263
1264 case AstNode<charT>::Type::ANY: ExecAny(node, automata); break;
1265
1266 case AstNode<charT>::Type::STAR: ExecStar(node, automata); break;
1267
1268 case AstNode<charT>::Type::POS_SET: ExecPositiveSet(node, automata); break;
1269
1270 case AstNode<charT>::Type::NEG_SET: ExecNegativeSet(node, automata); break;
1271
1272 case AstNode<charT>::Type::GROUP: ExecGroup(node, automata); break;
1273
1274 default: break;
1275 }
1276 }
1277
1279 {
1280 auto *char_node = static_cast<CharNode<charT> *>(node);
1281 char c = char_node->GetValue();
1282 NewState<StateChar<charT>>(automata, c);
1283 }
1284
1286 {
1287 NewState<StateAny<charT>>(automata);
1288 }
1289
1291 {
1292 NewState<StateStar<charT>>(automata);
1293 automata.GetState(current_state_).AddNextState(current_state_);
1294 }
1295
1297 {
1298 auto *pos_set_node = static_cast<PositiveSetNode<charT> *>(node);
1299
1300 auto items = ProcessSetItems(pos_set_node->GetSet());
1301 NewState<StateSet<charT>>(automata, std::move(items));
1302 }
1303
1305 {
1306 auto *pos_set_node = static_cast<NegativeSetNode<charT> *>(node);
1307
1308 auto items = ProcessSetItems(pos_set_node->GetSet());
1309 NewState<StateSet<charT>>(automata, std::move(items), /*neg*/ true);
1310 }
1311
1312 std::vector<std::unique_ptr<SetItem<charT>>> ProcessSetItems(AstNode<charT> *node)
1313 {
1314 auto *set_node = static_cast<SetItemsNode<charT> *>(node);
1315 std::vector<std::unique_ptr<SetItem<charT>>> vec;
1316 auto &items = set_node->GetItems();
1317 for (auto &item : items) {
1318 vec.push_back(std::move(ProcessSetItem(item.get())));
1319 }
1320
1321 return vec;
1322 }
1323
1324 std::unique_ptr<SetItem<charT>> ProcessSetItem(AstNode<charT> *node)
1325 {
1326 if (node->GetType() == AstNode<charT>::Type::CHAR) {
1327 auto *char_node = static_cast<CharNode<charT> *>(node);
1328 char c = char_node->GetValue();
1329 return std::unique_ptr<SetItem<charT>>(new SetItemChar<charT>(c));
1330 }
1331 if (node->GetType() == AstNode<charT>::Type::RANGE) {
1332 auto *range_node = static_cast<RangeNode<charT> *>(node);
1333 auto *start_node = static_cast<CharNode<charT> *>(range_node->GetStart());
1334 auto *end_node = static_cast<CharNode<charT> *>(range_node->GetEnd());
1335
1336 char start_char = start_node->GetValue();
1337 char end_char = end_node->GetValue();
1338 return std::unique_ptr<SetItem<charT>>(new SetItemRange<charT>(start_char, end_char));
1339 }
1340 throw Error("Not valid set item");
1341 }
1342
1344 {
1345 auto *group_node = static_cast<GroupNode<charT> *>(node);
1346 auto *union_node = group_node->GetGlob();
1347 auto automatas = ExecUnion(union_node);
1348
1349 typename StateGroup<charT>::Type state_group_type{};
1350 switch (group_node->GetGroupType()) {
1352 state_group_type = StateGroup<charT>::Type::BASIC;
1353 break;
1354
1355 case GroupNode<charT>::GroupType::ANY: state_group_type = StateGroup<charT>::Type::ANY; break;
1356
1358 state_group_type = StateGroup<charT>::Type::STAR;
1359 break;
1360
1362 state_group_type = StateGroup<charT>::Type::PLUS;
1363 break;
1364
1365 case GroupNode<charT>::GroupType::AT: state_group_type = StateGroup<charT>::Type::AT; break;
1366
1367 case GroupNode<charT>::GroupType::NEG: state_group_type = StateGroup<charT>::Type::NEG; break;
1368 }
1369
1370 NewState<StateGroup<charT>>(automata, state_group_type, std::move(automatas));
1371 automata.GetState(current_state_).AddNextState(current_state_);
1372 }
1373
1374 std::vector<std::unique_ptr<Automata<charT>>> ExecUnion(AstNode<charT> *node)
1375 {
1376 auto *union_node = static_cast<UnionNode<charT> *>(node);
1377 auto &items = union_node->GetItems();
1378 std::vector<std::unique_ptr<Automata<charT>>> vec_automatas;
1379 for (auto &item : items) {
1380 std::unique_ptr<Automata<charT>> automata_ptr(new Automata<charT>);
1381 AstConsumer ast_consumer;
1382 ast_consumer.ExecConcat(item.get(), *automata_ptr);
1383
1384 size_t match_state = automata_ptr->template NewState<StateMatch<charT>>();
1385 automata_ptr->GetState(ast_consumer.preview_state_).AddNextState(match_state);
1386 automata_ptr->SetMatchState(match_state);
1387
1388 size_t fail_state = automata_ptr->template NewState<StateFail<charT>>();
1389 automata_ptr->SetFailState(fail_state);
1390
1391 vec_automatas.push_back(std::move(automata_ptr));
1392 }
1393
1394 return vec_automatas;
1395 }
1396
1397 template <class T, typename... Args> void NewState(Automata<charT> &automata, Args &&...args)
1398 {
1399 current_state_ = automata.template NewState<T>(std::forward<Args>(args)...);
1400 if (preview_state_ >= 0) {
1401 automata.GetState(preview_state_).AddNextState(current_state_);
1402 }
1404 }
1405
1407 size_t current_state_ = 0;
1408 };
1409
1410 template <class charT> class ExtendedGlob
1411 {
1412 public:
1413 explicit ExtendedGlob(const String<charT> &pattern)
1414 {
1415 Lexer<charT> l(pattern);
1416 std::vector<Token<charT>> tokens = l.Scanner();
1417 Parser<charT> p(std::move(tokens));
1418 AstNodePtr<charT> ast_ptr = p.GenAst();
1419
1420 AstConsumer<charT> ast_consumer;
1421 ast_consumer.GenAutomata(ast_ptr.get(), automata_);
1422 }
1423
1424 ExtendedGlob(const ExtendedGlob &) = delete;
1426
1428
1430 {
1431 automata_ = std::move(glob.automata_);
1432 return *this;
1433 }
1434
1435 bool Exec(const String<charT> &str)
1436 {
1437 bool r;
1438 std::tie(r, std::ignore) = automata_.Exec(str);
1439 return r;
1440 }
1441
1442 const Automata<charT> &GetAutomata() const { return automata_; }
1443
1444 private:
1446 };
1447
1448 template <class charT> class SimpleGlob
1449 {
1450 public:
1451 explicit SimpleGlob(const String<charT> &pattern) { Parser(pattern); }
1452
1453 SimpleGlob(const SimpleGlob &) = delete;
1455
1457
1459 {
1460 automata_ = std::move(glob.automata_);
1461 return *this;
1462 }
1463
1464 void Parser(const String<charT> &pattern)
1465 {
1466 size_t pos = 0;
1467 int preview_state = -1;
1468
1469 while (pos < pattern.length()) {
1470 size_t current_state = 0;
1471 char c = pattern[pos];
1472 switch (c) {
1473 case '?': {
1474 current_state = automata_.template NewState<StateAny<charT>>();
1475 ++pos;
1476 break;
1477 }
1478
1479 case '*': {
1480 current_state = automata_.template NewState<StateStar<charT>>();
1481 automata_.GetState(current_state).AddNextState(current_state);
1482 ++pos;
1483 break;
1484 }
1485
1486 default: {
1487 current_state = automata_.template NewState<StateChar<charT>>(c);
1488 ++pos;
1489 break;
1490 }
1491 }
1492
1493 if (preview_state >= 0) {
1494 automata_.GetState(preview_state).AddNextState(current_state);
1495 }
1496 preview_state = current_state;
1497 }
1498
1499 size_t match_state = automata_.template NewState<StateMatch<charT>>();
1500 automata_.GetState(preview_state).AddNextState(match_state);
1501 automata_.SetMatchState(match_state);
1502
1503 size_t fail_state = automata_.template NewState<StateFail<charT>>();
1504 automata_.SetFailState(fail_state);
1505 }
1506
1507 bool Exec(const String<charT> &str) const
1508 {
1509 bool r;
1510 std::tie(r, std::ignore) = automata_.Exec(str);
1511 return r;
1512 }
1513
1514 const Automata<charT> &GetAutomata() const { return automata_; }
1515
1516 private:
1518 };
1519
1520 template <class charT> using extended_glob = ExtendedGlob<charT>;
1521
1522 template <class charT> using no_extended_glob = SimpleGlob<charT>;
1523
1524 template <class charT> class MatchResults;
1525
1526 template <class charT, class globT = extended_glob<charT>> class BasicGlob
1527 {
1528 public:
1529 explicit BasicGlob(const String<charT> &pattern) : glob_{pattern} {}
1530
1531 BasicGlob(const BasicGlob &) = delete;
1533
1535
1537 {
1538 glob_ = std::move(glob.glob_);
1539 return *this;
1540 }
1541
1542 const Automata<charT> &GetAutomata() const { return glob_.GetAutomata(); }
1543
1544 private:
1545 bool Exec(const String<charT> &str) { return glob_.Exec(str); }
1546
1547 template <class charU, class globU>
1549
1550 template <class charU, class globU>
1551 friend bool glob_match(const charU *str, BasicGlob<charU, globU> &glob);
1552
1553 template <class charU, class globU>
1554 friend bool glob_match(const String<charU> &str, MatchResults<charU> &res,
1556
1557 template <class charU, class globU>
1558 friend bool glob_match(const charU *str, MatchResults<charU> &res,
1560
1561 globT glob_;
1562 };
1563
1564 template <class charT> class MatchResults
1565 {
1566 public:
1567 using const_iterator = typename std::vector<String<charT>>::const_iterator;
1568
1569 MatchResults() = default;
1570
1572
1574
1576 {
1577 results_ = m.results_;
1578
1579 return *this;
1580 }
1581
1583 {
1584 results_ = std::move(m.results_);
1585
1586 return *this;
1587 }
1588
1589 bool empty() const { return results_.empty(); }
1590
1591 size_t size() const { return results_.size(); }
1592
1593 const_iterator begin() const noexcept { return results_.begin(); }
1594
1595 const_iterator end() const noexcept { return results_.end(); }
1596
1597 const_iterator cbegin() const noexcept { return results_.cbegin(); }
1598
1599 const_iterator cend() const noexcept { return results_.cend(); }
1600
1601 String<charT> &operator[](size_t n) const { return results_[n]; }
1602
1603 private:
1604 void SetResults(std::vector<String<charT>> &&results) { results_ = std::move(results); }
1605
1606 template <class charU, class globU>
1608
1609 template <class charU, class globU>
1610 friend bool glob_match(const charU *str, BasicGlob<charU, globU> &glob);
1611
1612 template <class charU, class globU>
1613 friend bool glob_match(const String<charU> &str, MatchResults<charU> &res,
1615
1616 template <class charU, class globU>
1617 friend bool glob_match(const charU *str, MatchResults<charU> &res,
1619
1620 std::vector<String<charT>> results_;
1621 };
1622
1623 template <class charT, class globT = extended_glob<charT>>
1625 {
1626 return glob.Exec(str);
1627 }
1628
1629 template <class charT, class globT = extended_glob<charT>>
1630 bool glob_match(const charT *str, BasicGlob<charT, globT> &glob)
1631 {
1632 return glob.Exec(str);
1633 }
1634
1635 template <class charT, class globT = extended_glob<charT>>
1637 {
1638 bool r = glob.Exec(str);
1639 res.SetResults(glob.GetAutomata().GetMatchedStrings());
1640 return r;
1641 }
1642
1643 template <class charT, class globT = extended_glob<charT>>
1645 {
1646 bool r = glob.Exec(str);
1647 res.SetResults(glob.GetAutomata().GetMatchedStrings());
1648 return r;
1649 }
1650
1651 template <class charT, class globT = extended_glob<charT>>
1653
1655
1657
1659
1661
1662} // namespace Ioss::glob
#define DECLARE_TYPE_CLASS(type)
Definition Ioss_Glob.h:800
#define GLOB_AST_NODE_LIST(V)
Definition Ioss_Glob.h:784
#define DECLARE_VIRTUAL_FUNC(type)
Definition Ioss_Glob.h:844
Definition Ioss_Glob.h:940
AnyNode()
Definition Ioss_Glob.h:942
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:944
Definition Ioss_Glob.h:1231
size_t current_state_
Definition Ioss_Glob.h:1407
void ExecStar(AstNode< charT > *, Automata< charT > &automata)
Definition Ioss_Glob.h:1290
std::vector< std::unique_ptr< SetItem< charT > > > ProcessSetItems(AstNode< charT > *node)
Definition Ioss_Glob.h:1312
int preview_state_
Definition Ioss_Glob.h:1406
void ExecConcat(AstNode< charT > *node, Automata< charT > &automata)
Definition Ioss_Glob.h:1249
void ExecAny(AstNode< charT > *, Automata< charT > &automata)
Definition Ioss_Glob.h:1285
void ExecBasicGlob(AstNode< charT > *node, Automata< charT > &automata)
Definition Ioss_Glob.h:1259
void NewState(Automata< charT > &automata, Args &&...args)
Definition Ioss_Glob.h:1397
std::vector< std::unique_ptr< Automata< charT > > > ExecUnion(AstNode< charT > *node)
Definition Ioss_Glob.h:1374
void ExecNegativeSet(AstNode< charT > *node, Automata< charT > &automata)
Definition Ioss_Glob.h:1304
void GenAutomata(AstNode< charT > *root_node, Automata< charT > &automata)
Definition Ioss_Glob.h:1235
void ExecChar(AstNode< charT > *node, Automata< charT > &automata)
Definition Ioss_Glob.h:1278
void ExecPositiveSet(AstNode< charT > *node, Automata< charT > &automata)
Definition Ioss_Glob.h:1296
void ExecGroup(AstNode< charT > *node, Automata< charT > &automata)
Definition Ioss_Glob.h:1343
std::unique_ptr< SetItem< charT > > ProcessSetItem(AstNode< charT > *node)
Definition Ioss_Glob.h:1324
Definition Ioss_Glob.h:806
Type
Definition Ioss_Glob.h:808
@ RANGE
Definition Ioss_Glob.h:810
@ CHAR
Definition Ioss_Glob.h:809
@ GLOB
Definition Ioss_Glob.h:821
@ POS_SET
Definition Ioss_Glob.h:813
@ CONCAT_GLOB
Definition Ioss_Glob.h:819
@ SET_ITEMS
Definition Ioss_Glob.h:812
@ NEG_SET
Definition Ioss_Glob.h:814
@ SET_ITEM
Definition Ioss_Glob.h:811
@ GROUP
Definition Ioss_Glob.h:818
@ STAR
Definition Ioss_Glob.h:816
@ SET
Definition Ioss_Glob.h:815
@ ANY
Definition Ioss_Glob.h:817
@ UNION
Definition Ioss_Glob.h:820
AstNode(Type type)
Definition Ioss_Glob.h:831
Type type_
Definition Ioss_Glob.h:834
virtual ~AstNode()=default
Type GetType() const
Definition Ioss_Glob.h:826
virtual void Accept(AstVisitor< charT > *visitor)=0
Definition Ioss_Glob.h:840
virtual ~AstVisitor()=default
Definition Ioss_Glob.h:117
Automata< charT > & SetMatchState(size_t state_pos)
Definition Ioss_Glob.h:154
size_t match_state_
Definition Ioss_Glob.h:228
std::vector< String< charT > > GetMatchedStrings() const
Definition Ioss_Glob.h:169
Automata< charT > & operator=(Automata< charT > &&automata)
Definition Ioss_Glob.h:132
const State< charT > & GetState(size_t pos) const
Definition Ioss_Glob.h:142
Automata(const Automata< charT > &)=delete
size_t fail_state_
Definition Ioss_Glob.h:192
void ResetStates()
Definition Ioss_Glob.h:220
size_t NewState(Args &&...args)
Definition Ioss_Glob.h:183
size_t FailState() const
Definition Ioss_Glob.h:146
std::tuple< bool, size_t > Exec(const String< charT > &str, bool comp_end=true)
Definition Ioss_Glob.h:162
Automata< charT > & operator=(const Automata< charT > &automata)=delete
std::tuple< bool, size_t > ExecAux(const String< charT > &str, bool comp_end=true) const
Definition Ioss_Glob.h:195
Automata(Automata< charT > &&automata)
Definition Ioss_Glob.h:125
size_t GetNumStates() const
Definition Ioss_Glob.h:160
std::vector< std::unique_ptr< State< charT > > > states_
Definition Ioss_Glob.h:227
Automata< charT > & SetFailState(size_t state_pos)
Definition Ioss_Glob.h:148
size_t start_state_
Definition Ioss_Glob.h:230
State< charT > & GetState(size_t pos)
Definition Ioss_Glob.h:144
Definition Ioss_Glob.h:1527
const Automata< charT > & GetAutomata() const
Definition Ioss_Glob.h:1542
BasicGlob(BasicGlob &&glob)
Definition Ioss_Glob.h:1534
BasicGlob(const String< charT > &pattern)
Definition Ioss_Glob.h:1529
BasicGlob & operator=(BasicGlob &)=delete
friend bool glob_match(const String< charU > &str, BasicGlob< charU, globU > &glob)
friend bool glob_match(const String< charU > &str, MatchResults< charU > &res, BasicGlob< charU, globU > &glob)
friend bool glob_match(const charU *str, MatchResults< charU > &res, BasicGlob< charU, globU > &glob)
BasicGlob(const BasicGlob &)=delete
friend bool glob_match(const charU *str, BasicGlob< charU, globU > &glob)
bool Exec(const String< charT > &str)
Definition Ioss_Glob.h:1545
BasicGlob & operator=(BasicGlob &&glob)
Definition Ioss_Glob.h:1536
Definition Ioss_Glob.h:851
CharNode(charT c)
Definition Ioss_Glob.h:853
char GetValue() const
Definition Ioss_Glob.h:857
charT c_
Definition Ioss_Glob.h:860
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:855
Definition Ioss_Glob.h:970
ConcatNode(std::vector< AstNodePtr< charT > > &&basic_glob)
Definition Ioss_Glob.h:972
std::vector< AstNodePtr< charT > > basic_glob_
Definition Ioss_Glob.h:982
std::vector< AstNodePtr< charT > > & GetBasicGlobs()
Definition Ioss_Glob.h:979
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:977
Definition Ioss_Glob.h:25
std::string msg_
Definition Ioss_Glob.h:33
~Error() override
Error(std::string msg)
Definition Ioss_Glob.h:27
const char * what() const noexcept override
Definition Ioss_Glob.h:30
Definition Ioss_Glob.h:1411
Automata< charT > automata_
Definition Ioss_Glob.h:1445
ExtendedGlob & operator=(ExtendedGlob &)=delete
bool Exec(const String< charT > &str)
Definition Ioss_Glob.h:1435
ExtendedGlob(const ExtendedGlob &)=delete
const Automata< charT > & GetAutomata() const
Definition Ioss_Glob.h:1442
ExtendedGlob & operator=(ExtendedGlob &&glob)
Definition Ioss_Glob.h:1429
ExtendedGlob(const String< charT > &pattern)
Definition Ioss_Glob.h:1413
ExtendedGlob(ExtendedGlob &&glob)
Definition Ioss_Glob.h:1427
Definition Ioss_Glob.h:1002
AstNodePtr< charT > glob_
Definition Ioss_Glob.h:1014
GlobNode(AstNodePtr< charT > &&glob)
Definition Ioss_Glob.h:1004
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:1009
AstNode< charT > * GetConcat()
Definition Ioss_Glob.h:1011
Definition Ioss_Glob.h:948
AstNodePtr< charT > glob_
Definition Ioss_Glob.h:965
GroupType
Definition Ioss_Glob.h:950
@ NEG
Definition Ioss_Glob.h:950
@ PLUS
Definition Ioss_Glob.h:950
@ STAR
Definition Ioss_Glob.h:950
@ ANY
Definition Ioss_Glob.h:950
@ BASIC
Definition Ioss_Glob.h:950
@ AT
Definition Ioss_Glob.h:950
GroupType group_type_
Definition Ioss_Glob.h:966
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:958
GroupType GetGroupType() const
Definition Ioss_Glob.h:962
AstNode< charT > * GetGlob()
Definition Ioss_Glob.h:960
GroupNode(GroupType group_type, AstNodePtr< charT > &&glob)
Definition Ioss_Glob.h:952
Definition Ioss_Glob.h:629
std::vector< Token< charT > > Scanner()
Definition Ioss_Glob.h:635
String< charT > str_
Definition Ioss_Glob.h:779
charT c_
Definition Ioss_Glob.h:781
void Advance()
Definition Ioss_Glob.h:769
static const char kEndOfInput
Definition Ioss_Glob.h:631
Token< charT > Select(TokenKind k)
Definition Ioss_Glob.h:765
Token< charT > Select(TokenKind k, charT value)
Definition Ioss_Glob.h:767
Lexer(const String< charT > &str)
Definition Ioss_Glob.h:633
size_t pos_
Definition Ioss_Glob.h:780
Definition Ioss_Glob.h:1565
bool empty() const
Definition Ioss_Glob.h:1589
MatchResults(const MatchResults &m)
Definition Ioss_Glob.h:1571
String< charT > & operator[](size_t n) const
Definition Ioss_Glob.h:1601
friend bool glob_match(const String< charU > &str, BasicGlob< charU, globU > &glob)
std::vector< String< char > > results_
Definition Ioss_Glob.h:1620
friend bool glob_match(const String< charU > &str, MatchResults< charU > &res, BasicGlob< charU, globU > &glob)
const_iterator cbegin() const noexcept
Definition Ioss_Glob.h:1597
const_iterator begin() const noexcept
Definition Ioss_Glob.h:1593
MatchResults(MatchResults &&m)
Definition Ioss_Glob.h:1573
void SetResults(std::vector< String< charT > > &&results)
Definition Ioss_Glob.h:1604
const_iterator end() const noexcept
Definition Ioss_Glob.h:1595
friend bool glob_match(const charU *str, MatchResults< charU > &res, BasicGlob< charU, globU > &glob)
MatchResults & operator=(MatchResults &&m)
Definition Ioss_Glob.h:1582
MatchResults & operator=(const MatchResults &m)
Definition Ioss_Glob.h:1575
typename std::vector< String< charT > >::const_iterator const_iterator
Definition Ioss_Glob.h:1567
size_t size() const
Definition Ioss_Glob.h:1591
friend bool glob_match(const charU *str, BasicGlob< charU, globU > &glob)
const_iterator cend() const noexcept
Definition Ioss_Glob.h:1599
Definition Ioss_Glob.h:916
AstNode< charT > * GetSet()
Definition Ioss_Glob.h:925
NegativeSetNode(AstNodePtr< charT > &&set)
Definition Ioss_Glob.h:918
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:923
AstNodePtr< charT > set_
Definition Ioss_Glob.h:928
Definition Ioss_Glob.h:1018
AstNodePtr< charT > ParserRange()
Definition Ioss_Glob.h:1038
AstNodePtr< charT > ParserSet()
Definition Ioss_Glob.h:1073
AstNodePtr< charT > ParserConcat()
Definition Ioss_Glob.h:1143
AstNodePtr< charT > ParserChar()
Definition Ioss_Glob.h:1027
AstNodePtr< charT > ParserBasicGlob()
Definition Ioss_Glob.h:1086
AstNodePtr< charT > ParserSetItem()
Definition Ioss_Glob.h:1051
AstNodePtr< charT > ParserSetItems()
Definition Ioss_Glob.h:1060
Token< charT > & NextToken()
Definition Ioss_Glob.h:1203
Token< charT > & GetToken()
Definition Ioss_Glob.h:1192
size_t pos_
Definition Ioss_Glob.h:1227
Parser(std::vector< Token< charT > > &&tok_vec)
Definition Ioss_Glob.h:1022
AstNodePtr< charT > GenAst()
Definition Ioss_Glob.h:1024
AstNodePtr< charT > ParserGroup()
Definition Ioss_Glob.h:1113
const Token< charT > & GetToken() const
Definition Ioss_Glob.h:1190
AstNodePtr< charT > ParserGlob()
Definition Ioss_Glob.h:1179
std::vector< Token< charT > > tok_vec_
Definition Ioss_Glob.h:1226
const Token< charT > & PeekAhead() const
Definition Ioss_Glob.h:1194
bool Advance()
Definition Ioss_Glob.h:1214
AstNodePtr< charT > ParserUnion()
Definition Ioss_Glob.h:1166
size_t Size() const noexcept
Definition Ioss_Glob.h:1224
Definition Ioss_Glob.h:900
PositiveSetNode(AstNodePtr< charT > &&set)
Definition Ioss_Glob.h:902
AstNode< charT > * GetSet()
Definition Ioss_Glob.h:909
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:907
AstNodePtr< charT > set_
Definition Ioss_Glob.h:912
Definition Ioss_Glob.h:864
AstNodePtr< charT > end_
Definition Ioss_Glob.h:880
AstNodePtr< charT > start_
Definition Ioss_Glob.h:879
RangeNode(AstNodePtr< charT > &&start, AstNodePtr< charT > &&end)
Definition Ioss_Glob.h:866
AstNode< charT > * GetEnd() const
Definition Ioss_Glob.h:876
AstNode< charT > * GetStart() const
Definition Ioss_Glob.h:874
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:872
Definition Ioss_Glob.h:327
SetItemChar(charT c)
Definition Ioss_Glob.h:329
bool Check(charT c) const override
Definition Ioss_Glob.h:331
charT c_
Definition Ioss_Glob.h:334
Definition Ioss_Glob.h:338
charT end_
Definition Ioss_Glob.h:349
charT start_
Definition Ioss_Glob.h:348
SetItemRange(charT start, charT end)
Definition Ioss_Glob.h:340
bool Check(charT c) const override
Definition Ioss_Glob.h:345
Definition Ioss_Glob.h:318
virtual ~SetItem()=default
virtual bool Check(charT c) const =0
Definition Ioss_Glob.h:884
std::vector< AstNodePtr< charT > > & GetItems()
Definition Ioss_Glob.h:893
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:891
SetItemsNode(std::vector< AstNodePtr< charT > > &&items)
Definition Ioss_Glob.h:886
std::vector< AstNodePtr< charT > > items_
Definition Ioss_Glob.h:896
Definition Ioss_Glob.h:1449
SimpleGlob(const String< charT > &pattern)
Definition Ioss_Glob.h:1451
SimpleGlob(const SimpleGlob &)=delete
SimpleGlob(SimpleGlob &&glob)
Definition Ioss_Glob.h:1456
const Automata< charT > & GetAutomata() const
Definition Ioss_Glob.h:1514
SimpleGlob & operator=(SimpleGlob &&glob)
Definition Ioss_Glob.h:1458
SimpleGlob & operator=(SimpleGlob &)=delete
void Parser(const String< charT > &pattern)
Definition Ioss_Glob.h:1464
Automata< charT > automata_
Definition Ioss_Glob.h:1517
bool Exec(const String< charT > &str) const
Definition Ioss_Glob.h:1507
Definition Ioss_Glob.h:932
StarNode()
Definition Ioss_Glob.h:934
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:936
std::tuple< size_t, size_t > Next(const String< charT > &str, size_t pos) override
Definition Ioss_Glob.h:271
StateAny(Automata< charT > &states)
Definition Ioss_Glob.h:263
bool Check(const String< charT > &, size_t) override
Definition Ioss_Glob.h:265
bool Check(const String< charT > &str, size_t pos) override
Definition Ioss_Glob.h:241
StateChar(Automata< charT > &states, charT c)
Definition Ioss_Glob.h:239
charT c_
Definition Ioss_Glob.h:254
std::tuple< size_t, size_t > Next(const String< charT > &str, size_t pos) override
Definition Ioss_Glob.h:243
bool Check(const String< charT > &, size_t) override
Definition Ioss_Glob.h:95
StateFail(Automata< charT > &states)
Definition Ioss_Glob.h:93
std::tuple< size_t, size_t > Next(const String< charT > &, size_t pos) override
Definition Ioss_Glob.h:97
std::tuple< bool, size_t > BasicCheck(const String< charT > &str, size_t pos)
Definition Ioss_Glob.h:417
std::tuple< size_t, size_t > NextBasic(const String< charT > &str, size_t pos)
Definition Ioss_Glob.h:494
bool match_one_
Definition Ioss_Glob.h:571
std::tuple< size_t, size_t > NextAny(const String< charT > &str, size_t pos)
Definition Ioss_Glob.h:507
std::vector< std::unique_ptr< Automata< charT > > > automatas_
Definition Ioss_Glob.h:570
StateGroup(Automata< charT > &states, Type type, std::vector< std::unique_ptr< Automata< charT > > > &&automatas)
Definition Ioss_Glob.h:409
std::tuple< size_t, size_t > Next(const String< charT > &str, size_t pos) override
Definition Ioss_Glob.h:452
std::tuple< size_t, size_t > NextStar(const String< charT > &str, size_t pos)
Definition Ioss_Glob.h:520
Type
Definition Ioss_Glob.h:407
@ NEG
Definition Ioss_Glob.h:407
@ PLUS
Definition Ioss_Glob.h:407
@ STAR
Definition Ioss_Glob.h:407
@ ANY
Definition Ioss_Glob.h:407
@ BASIC
Definition Ioss_Glob.h:407
@ AT
Definition Ioss_Glob.h:407
std::tuple< size_t, size_t > NextNeg(const String< charT > &str, size_t pos)
Definition Ioss_Glob.h:481
void ResetState() override
Definition Ioss_Glob.h:415
bool Check(const String< charT > &str, size_t pos) override
Definition Ioss_Glob.h:435
Type type_
Definition Ioss_Glob.h:569
std::tuple< size_t, size_t > NextPlus(const String< charT > &str, size_t pos)
Definition Ioss_Glob.h:537
StateMatch(Automata< charT > &states)
Definition Ioss_Glob.h:106
bool Check(const String< charT > &, size_t) override
Definition Ioss_Glob.h:108
std::tuple< size_t, size_t > Next(const String< charT > &, size_t pos) override
Definition Ioss_Glob.h:110
bool Check(const String< charT > &str, size_t pos) override
Definition Ioss_Glob.h:377
bool neg_
Definition Ioss_Glob.h:398
std::tuple< size_t, size_t > Next(const String< charT > &str, size_t pos) override
Definition Ioss_Glob.h:386
StateSet(Automata< charT > &states, std::vector< std::unique_ptr< SetItem< charT > > > items, bool neg=false)
Definition Ioss_Glob.h:358
bool SetCheck(const String< charT > &str, size_t pos) const
Definition Ioss_Glob.h:364
std::vector< std::unique_ptr< SetItem< charT > > > items_
Definition Ioss_Glob.h:397
std::tuple< size_t, size_t > Next(const String< charT > &str, size_t pos) override
Definition Ioss_Glob.h:293
StateStar(Automata< charT > &states)
Definition Ioss_Glob.h:285
bool Check(const String< charT > &, size_t) override
Definition Ioss_Glob.h:287
Definition Ioss_Glob.h:56
StateType type_
Definition Ioss_Glob.h:84
State(StateType type, Automata< charT > &states)
Definition Ioss_Glob.h:58
const String< charT > & MatchedStr()
Definition Ioss_Glob.h:74
virtual std::tuple< size_t, size_t > Next(const String< charT > &str, size_t pos)=0
virtual void ResetState()
Definition Ioss_Glob.h:76
const std::vector< size_t > & GetNextStates() const
Definition Ioss_Glob.h:72
void AddNextState(size_t state_pos)
Definition Ioss_Glob.h:70
std::vector< size_t > next_states_
Definition Ioss_Glob.h:86
void SetMatchedStr(const String< charT > &str)
Definition Ioss_Glob.h:79
virtual bool Check(const String< charT > &str, size_t pos)=0
String< charT > matched_str_
Definition Ioss_Glob.h:87
Automata< charT > & states_
Definition Ioss_Glob.h:85
Automata< charT > & GetAutomata()
Definition Ioss_Glob.h:68
StateType Type() const
Definition Ioss_Glob.h:66
void SetMatchedStr(charT c)
Definition Ioss_Glob.h:81
virtual ~State()=default
Definition Ioss_Glob.h:597
friend std::ostream & operator<<(std::ostream &stream, const Token< charU > &token)
charT Value() const
Definition Ioss_Glob.h:603
Token(TokenKind kind)
Definition Ioss_Glob.h:599
bool operator!=(TokenKind kind) const
Definition Ioss_Glob.h:611
charT value_
Definition Ioss_Glob.h:618
Token(TokenKind kind, charT value)
Definition Ioss_Glob.h:600
bool operator==(TokenKind kind) const
Definition Ioss_Glob.h:607
TokenKind Kind() const
Definition Ioss_Glob.h:601
bool operator!=(TokenKind kind)
Definition Ioss_Glob.h:609
bool operator==(TokenKind kind)
Definition Ioss_Glob.h:605
TokenKind kind_
Definition Ioss_Glob.h:617
Definition Ioss_Glob.h:986
UnionNode(std::vector< AstNodePtr< charT > > &&items)
Definition Ioss_Glob.h:988
void Accept(AstVisitor< charT > *visitor) override
Definition Ioss_Glob.h:993
std::vector< AstNodePtr< charT > > items_
Definition Ioss_Glob.h:998
std::vector< AstNodePtr< charT > > & GetItems()
Definition Ioss_Glob.h:995
Definition Ioss_Glob.h:18
std::ostream & operator<<(std::ostream &stream, const Token< charT > &token)
Definition Ioss_Glob.h:622
std::basic_string< charT > String
Definition Ioss_Glob.h:20
ExtendedGlob< charT > extended_glob
Definition Ioss_Glob.h:1520
std::unique_ptr< AstNode< charT > > AstNodePtr
Definition Ioss_Glob.h:837
static const char * token_name_str[]
Definition Ioss_Glob.h:586
basic_glob< wchar_t, extended_glob< wchar_t > > wglob
Definition Ioss_Glob.h:1656
MatchResults< char > cmatch
Definition Ioss_Glob.h:1658
StateType
Definition Ioss_Glob.h:36
@ CHAR
Definition Ioss_Glob.h:39
@ QUESTION
Definition Ioss_Glob.h:40
@ MULT
Definition Ioss_Glob.h:41
@ MATCH
Definition Ioss_Glob.h:37
@ GROUP
Definition Ioss_Glob.h:43
@ SET
Definition Ioss_Glob.h:42
@ FAIL
Definition Ioss_Glob.h:38
@ UNION
Definition Ioss_Glob.h:44
basic_glob< char, extended_glob< char > > glob
Definition Ioss_Glob.h:1654
BasicGlob< charT, globT > basic_glob
Definition Ioss_Glob.h:1652
SimpleGlob< charT > no_extended_glob
Definition Ioss_Glob.h:1522
TokenKind
Definition Ioss_Glob.h:575
@ CHAR
Definition Ioss_Glob.h:577
@ UNKNOWN
Definition Ioss_Glob.h:576
@ TOKEN
Definition Ioss_Glob.h:578
bool glob_match(const String< charT > &str, BasicGlob< charT, globT > &glob)
Definition Ioss_Glob.h:1624
T exchange(T &obj, U &&new_value)
Definition Ioss_Glob.h:48
MatchResults< wchar_t > wmatch
Definition Ioss_Glob.h:1660