| 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 |
|
| 18 |
namespace 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 |
|
| 36 |
enum class StateType { |
| 37 |
MATCH, |
| 38 |
FAIL, |
| 39 |
CHAR, |
| 40 |
QUESTION, |
| 41 |
MULT, |
| 42 |
SET, |
| 43 |
GROUP, |
| 44 |
UNION, |
| 45 |
}; |
| 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 |
|
| 68 |
Automata<charT> &GetAutomata() { return states_; } |
| 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 |
|
| 74 |
const String<charT> &MatchedStr() { return matched_str_; } |
| 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: |
| 84 |
StateType type_; |
| 85 |
Automata<charT> &states_; |
| 86 |
std::vector<size_t> next_states_; |
| 87 |
String<charT> matched_str_; |
| 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 |
|
| 125 |
Automata(Automata<charT> &&automata) |
| 126 |
: states_{std::move(automata.states_)}, match_state_{automata.match_state_}, |
| 127 |
fail_state_{exchange(automata.fail_state_, 0)}, |
| 128 |
start_state_{exchange(automata.start_state_, 0)} |
| 129 |
{ |
| 130 |
} |
| 131 |
|
| 132 |
Automata<charT> &operator=(Automata<charT> &&automata) |
| 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 |
|
| 154 |
Automata<charT> &SetMatchState(size_t state_pos) |
| 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 |
|
| 220 |
void ResetStates() |
| 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 |
|
| 409 |
StateGroup(Automata<charT> &states, Type type, |
| 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: |
| 569 |
Type type_{}; |
| 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 { |
| 576 |
UNKNOWN = 0, |
| 577 |
CHAR, |
| 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 |
|
| 617 |
TokenKind kind_; |
| 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 |
|
| 779 |
String<charT> str_; |
| 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; |
| 801 |
GLOB_AST_NODE_LIST(DECLARE_TYPE_CLASS) |
| 802 |
|
| 803 |
#undef DECLARE_TYPE_CLASS |
| 804 |
|
| 805 |
template <class charT> class AstNode |
| 806 |
{ |
| 807 |
public: |
| 808 |
enum class Type { |
| 809 |
CHAR, |
| 810 |
RANGE, |
| 811 |
SET_ITEM, |
| 812 |
SET_ITEMS, |
| 813 |
POS_SET, |
| 814 |
NEG_SET, |
| 815 |
SET, |
| 816 |
STAR, |
| 817 |
ANY, |
| 818 |
GROUP, |
| 819 |
CONCAT_GLOB, |
| 820 |
UNION, |
| 821 |
GLOB |
| 822 |
}; |
| 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: |
| 834 |
Type type_; |
| 835 |
}; |
| 836 |
|
| 837 |
template <class charT> using AstNodePtr = std::unique_ptr<AstNode<charT>>; |
| 838 |
|
| 839 |
template <class charT> class AstVisitor |
| 840 |
{ |
| 841 |
public: |
| 842 |
// define all visitor methods for the nodes |
| 843 |
#define DECLARE_VIRTUAL_FUNC(type) \ |
| 844 |
virtual void Visit##type(type<charT> * /*node*/) {} |
| 845 |
GLOB_AST_NODE_LIST(DECLARE_VIRTUAL_FUNC) |
| 846 |
#undef DECLARE_VIRTUAL_FUNC |
| 847 |
}; |
| 848 |
|
| 849 |
template <class charT> class CharNode : public AstNode<charT> |
| 850 |
{ |
| 851 |
public: |
| 852 |
explicit CharNode(charT c) : AstNode<charT>(AstNode<charT>::Type::CHAR), c_{c} {} |
| 853 |
|
| 854 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitCharNode(this); } |
| 855 |
|
| 856 |
char GetValue() const { return c_; } |
| 857 |
|
| 858 |
private: |
| 859 |
charT c_; |
| 860 |
}; |
| 861 |
|
| 862 |
template <class charT> class RangeNode : public AstNode<charT> |
| 863 |
{ |
| 864 |
public: |
| 865 |
RangeNode(AstNodePtr<charT> &&start, AstNodePtr<charT> &&end) |
| 866 |
: AstNode<charT>(AstNode<charT>::Type::RANGE), start_{std::move(start)}, |
| 867 |
end_{std::move(end)} |
| 868 |
{ |
| 869 |
} |
| 870 |
|
| 871 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitRangeNode(this); } |
| 872 |
|
| 873 |
AstNode<charT> *GetStart() const { return start_.get(); } |
| 874 |
|
| 875 |
AstNode<charT> *GetEnd() const { return end_.get(); } |
| 876 |
|
| 877 |
private: |
| 878 |
AstNodePtr<charT> start_; |
| 879 |
AstNodePtr<charT> end_; |
| 880 |
}; |
| 881 |
|
| 882 |
template <class charT> class SetItemsNode : public AstNode<charT> |
| 883 |
{ |
| 884 |
public: |
| 885 |
explicit SetItemsNode(std::vector<AstNodePtr<charT>> &&items) |
| 886 |
: AstNode<charT>(AstNode<charT>::Type::SET_ITEMS), items_{std::move(items)} |
| 887 |
{ |
| 888 |
} |
| 889 |
|
| 890 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitSetItemsNode(this); } |
| 891 |
|
| 892 |
std::vector<AstNodePtr<charT>> &GetItems() { return items_; } |
| 893 |
|
| 894 |
private: |
| 895 |
std::vector<AstNodePtr<charT>> items_; |
| 896 |
}; |
| 897 |
|
| 898 |
template <class charT> class PositiveSetNode : public AstNode<charT> |
| 899 |
{ |
| 900 |
public: |
| 901 |
explicit PositiveSetNode(AstNodePtr<charT> &&set) |
| 902 |
: AstNode<charT>(AstNode<charT>::Type::POS_SET), set_{std::move(set)} |
| 903 |
{ |
| 904 |
} |
| 905 |
|
| 906 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitPositiveSetNode(this); } |
| 907 |
|
| 908 |
AstNode<charT> *GetSet() { return set_.get(); } |
| 909 |
|
| 910 |
private: |
| 911 |
AstNodePtr<charT> set_; |
| 912 |
}; |
| 913 |
|
| 914 |
template <class charT> class NegativeSetNode : public AstNode<charT> |
| 915 |
{ |
| 916 |
public: |
| 917 |
explicit NegativeSetNode(AstNodePtr<charT> &&set) |
| 918 |
: AstNode<charT>(AstNode<charT>::Type::NEG_SET), set_{std::move(set)} |
| 919 |
{ |
| 920 |
} |
| 921 |
|
| 922 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitNegativeSetNode(this); } |
| 923 |
|
| 924 |
AstNode<charT> *GetSet() { return set_.get(); } |
| 925 |
|
| 926 |
private: |
| 927 |
AstNodePtr<charT> set_; |
| 928 |
}; |
| 929 |
|
| 930 |
template <class charT> class StarNode : public AstNode<charT> |
| 931 |
{ |
| 932 |
public: |
| 933 |
StarNode() : AstNode<charT>(AstNode<charT>::Type::STAR) {} |
| 934 |
|
| 935 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitStarNode(this); } |
| 936 |
}; |
| 937 |
|
| 938 |
template <class charT> class AnyNode : public AstNode<charT> |
| 939 |
{ |
| 940 |
public: |
| 941 |
AnyNode() : AstNode<charT>(AstNode<charT>::Type::ANY) {} |
| 942 |
|
| 943 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitAnyNode(this); } |
| 944 |
}; |
| 945 |
|
| 946 |
template <class charT> class GroupNode : public AstNode<charT> |
| 947 |
{ |
| 948 |
public: |
| 949 |
enum class GroupType { BASIC, ANY, STAR, PLUS, NEG, AT }; |
| 950 |
|
| 951 |
GroupNode(GroupType group_type, AstNodePtr<charT> &&glob) |
| 952 |
: AstNode<charT>(AstNode<charT>::Type::GROUP), glob_{std::move(glob)}, |
| 953 |
group_type_{group_type} |
| 954 |
{ |
| 955 |
} |
| 956 |
|
| 957 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitGroupNode(this); } |
| 958 |
|
| 959 |
AstNode<charT> *GetGlob() { return glob_.get(); } |
| 960 |
|
| 961 |
GroupType GetGroupType() const { return group_type_; } |
| 962 |
|
| 963 |
private: |
| 964 |
AstNodePtr<charT> glob_; |
| 965 |
GroupType group_type_; |
| 966 |
}; |
| 967 |
|
| 968 |
template <class charT> class ConcatNode : public AstNode<charT> |
| 969 |
{ |
| 970 |
public: |
| 971 |
explicit ConcatNode(std::vector<AstNodePtr<charT>> &&basic_glob) |
| 972 |
: AstNode<charT>(AstNode<charT>::Type::CONCAT_GLOB), basic_glob_{std::move(basic_glob)} |
| 973 |
{ |
| 974 |
} |
| 975 |
|
| 976 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitConcatNode(this); } |
| 977 |
|
| 978 |
std::vector<AstNodePtr<charT>> &GetBasicGlobs() { return basic_glob_; } |
| 979 |
|
| 980 |
private: |
| 981 |
std::vector<AstNodePtr<charT>> basic_glob_; |
| 982 |
}; |
| 983 |
|
| 984 |
template <class charT> class UnionNode : public AstNode<charT> |
| 985 |
{ |
| 986 |
public: |
| 987 |
explicit UnionNode(std::vector<AstNodePtr<charT>> &&items) |
| 988 |
: AstNode<charT>(AstNode<charT>::Type::UNION), items_{std::move(items)} |
| 989 |
{ |
| 990 |
} |
| 991 |
|
| 992 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitUnionNode(this); } |
| 993 |
|
| 994 |
std::vector<AstNodePtr<charT>> &GetItems() { return items_; } |
| 995 |
|
| 996 |
private: |
| 997 |
std::vector<AstNodePtr<charT>> items_; |
| 998 |
}; |
| 999 |
|
| 1000 |
template <class charT> class GlobNode : public AstNode<charT> |
| 1001 |
{ |
| 1002 |
public: |
| 1003 |
explicit GlobNode(AstNodePtr<charT> &&glob) |
| 1004 |
: AstNode<charT>(AstNode<charT>::Type::GLOB), glob_{std::move(glob)} |
| 1005 |
{ |
| 1006 |
} |
| 1007 |
|
| 1008 |
void Accept(AstVisitor<charT> *visitor) override { visitor->VisitGlobNode(this); } |
| 1009 |
|
| 1010 |
AstNode<charT> *GetConcat() { return glob_.get(); } |
| 1011 |
|
| 1012 |
private: |
| 1013 |
AstNodePtr<charT> glob_; |
| 1014 |
}; |
| 1015 |
|
| 1016 |
template <class charT> class Parser |
| 1017 |
{ |
| 1018 |
public: |
| 1019 |
Parser() = delete; |
| 1020 |
|
| 1021 |
explicit Parser(std::vector<Token<charT>> &&tok_vec) : tok_vec_{std::move(tok_vec)}, pos_{0} {} |
| 1022 |
|
| 1023 |
AstNodePtr<charT> GenAst() { return ParserGlob(); } |
| 1024 |
|
| 1025 |
private: |
| 1026 |
AstNodePtr<charT> ParserChar() |
| 1027 |
{ |
| 1028 |
Token<charT> &tk = NextToken(); |
| 1029 |
if (tk != TokenKind::CHAR) { |
| 1030 |
throw Error("char expected"); |
| 1031 |
} |
| 1032 |
|
| 1033 |
charT c = tk.Value(); |
| 1034 |
return AstNodePtr<charT>(new CharNode<charT>(c)); |
| 1035 |
} |
| 1036 |
|
| 1037 |
AstNodePtr<charT> ParserRange() |
| 1038 |
{ |
| 1039 |
AstNodePtr<charT> char_start = ParserChar(); |
| 1040 |
|
| 1041 |
Token<charT> &tk = NextToken(); |
| 1042 |
if (tk != TokenKind::SUB) { |
| 1043 |
throw Error("range expected"); |
| 1044 |
} |
| 1045 |
|
| 1046 |
AstNodePtr<charT> char_end = ParserChar(); |
| 1047 |
return AstNodePtr<charT>(new RangeNode<charT>(std::move(char_start), std::move(char_end))); |
| 1048 |
} |
| 1049 |
|
| 1050 |
AstNodePtr<charT> ParserSetItem() |
| 1051 |
{ |
| 1052 |
if (PeekAhead() == TokenKind::SUB) { |
| 1053 |
return ParserRange(); |
| 1054 |
} |
| 1055 |
|
| 1056 |
return ParserChar(); |
| 1057 |
} |
| 1058 |
|
| 1059 |
AstNodePtr<charT> ParserSetItems() |
| 1060 |
{ |
| 1061 |
std::vector<AstNodePtr<charT>> items; |
| 1062 |
|
| 1063 |
do { |
| 1064 |
items.push_back(ParserSetItem()); |
| 1065 |
} while (GetToken() != TokenKind::RBRACKET); |
| 1066 |
|
| 1067 |
Advance(); |
| 1068 |
|
| 1069 |
return AstNodePtr<charT>(new SetItemsNode<charT>(std::move(items))); |
| 1070 |
} |
| 1071 |
|
| 1072 |
AstNodePtr<charT> ParserSet() |
| 1073 |
{ |
| 1074 |
Token<charT> &tk = NextToken(); |
| 1075 |
|
| 1076 |
if (tk == TokenKind::LBRACKET) { |
| 1077 |
return AstNodePtr<charT>(new PositiveSetNode<charT>(ParserSetItems())); |
| 1078 |
} |
| 1079 |
if (tk == TokenKind::NEGLBRACKET) { |
| 1080 |
return AstNodePtr<charT>(new NegativeSetNode<charT>(ParserSetItems())); |
| 1081 |
} |
| 1082 |
throw Error("set expected"); |
| 1083 |
} |
| 1084 |
|
| 1085 |
AstNodePtr<charT> ParserBasicGlob() |
| 1086 |
{ |
| 1087 |
Token<charT> &tk = GetToken(); |
| 1088 |
|
| 1089 |
switch (tk.Kind()) { |
| 1090 |
case TokenKind::QUESTION: Advance(); return AstNodePtr<charT>(new AnyNode<charT>()); |
| 1091 |
|
| 1092 |
case TokenKind::STAR: Advance(); return AstNodePtr<charT>(new StarNode<charT>()); |
| 1093 |
|
| 1094 |
case TokenKind::SUB: Advance(); return AstNodePtr<charT>(new CharNode<charT>('-')); |
| 1095 |
|
| 1096 |
case TokenKind::CHAR: return ParserChar(); |
| 1097 |
|
| 1098 |
case TokenKind::LBRACKET: |
| 1099 |
case TokenKind::NEGLBRACKET: return ParserSet(); |
| 1100 |
|
| 1101 |
case TokenKind::LPAREN: |
| 1102 |
case TokenKind::QUESTLPAREN: |
| 1103 |
case TokenKind::STARLPAREN: |
| 1104 |
case TokenKind::PLUSLPAREN: |
| 1105 |
case TokenKind::NEGLPAREN: |
| 1106 |
case TokenKind::ATLPAREN: return ParserGroup(); |
| 1107 |
|
| 1108 |
default: throw Error("basic glob expected"); |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
AstNodePtr<charT> ParserGroup() |
| 1113 |
{ |
| 1114 |
typename GroupNode<charT>::GroupType type; |
| 1115 |
Token<charT> &tk = NextToken(); |
| 1116 |
|
| 1117 |
switch (tk.Kind()) { |
| 1118 |
case TokenKind::LPAREN: type = GroupNode<charT>::GroupType::BASIC; break; |
| 1119 |
|
| 1120 |
case TokenKind::QUESTLPAREN: type = GroupNode<charT>::GroupType::ANY; break; |
| 1121 |
|
| 1122 |
case TokenKind::STARLPAREN: type = GroupNode<charT>::GroupType::STAR; break; |
| 1123 |
|
| 1124 |
case TokenKind::PLUSLPAREN: type = GroupNode<charT>::GroupType::PLUS; break; |
| 1125 |
|
| 1126 |
case TokenKind::NEGLPAREN: type = GroupNode<charT>::GroupType::NEG; break; |
| 1127 |
|
| 1128 |
case TokenKind::ATLPAREN: type = GroupNode<charT>::GroupType::AT; break; |
| 1129 |
|
| 1130 |
default: throw Error("Not valid group"); break; |
| 1131 |
} |
| 1132 |
|
| 1133 |
AstNodePtr<charT> group_glob = ParserUnion(); |
| 1134 |
tk = NextToken(); |
| 1135 |
if (tk != TokenKind::RPAREN) { |
| 1136 |
throw Error("Expected ')' at and of group"); |
| 1137 |
} |
| 1138 |
|
| 1139 |
return AstNodePtr<charT>(new GroupNode<charT>(type, std::move(group_glob))); |
| 1140 |
} |
| 1141 |
|
| 1142 |
AstNodePtr<charT> ParserConcat() |
| 1143 |
{ |
| 1144 |
auto check_end = [&]() -> bool { |
| 1145 |
Token<charT> &tk = GetToken(); |
| 1146 |
|
| 1147 |
switch (tk.Kind()) { |
| 1148 |
case TokenKind::EOS: |
| 1149 |
case TokenKind::RPAREN: |
| 1150 |
case TokenKind::UNION: return true; |
| 1151 |
|
| 1152 |
default: return false; |
| 1153 |
} |
| 1154 |
}; |
| 1155 |
|
| 1156 |
std::vector<AstNodePtr<charT>> parts; |
| 1157 |
|
| 1158 |
while (!check_end()) { |
| 1159 |
parts.push_back(ParserBasicGlob()); |
| 1160 |
} |
| 1161 |
|
| 1162 |
return AstNodePtr<charT>(new ConcatNode<charT>(std::move(parts))); |
| 1163 |
} |
| 1164 |
|
| 1165 |
AstNodePtr<charT> ParserUnion() |
| 1166 |
{ |
| 1167 |
std::vector<AstNodePtr<charT>> items; |
| 1168 |
items.push_back(ParserConcat()); |
| 1169 |
|
| 1170 |
while (GetToken() == TokenKind::UNION) { |
| 1171 |
Advance(); |
| 1172 |
items.push_back(ParserConcat()); |
| 1173 |
} |
| 1174 |
|
| 1175 |
return AstNodePtr<charT>(new UnionNode<charT>(std::move(items))); |
| 1176 |
} |
| 1177 |
|
| 1178 |
AstNodePtr<charT> ParserGlob() |
| 1179 |
{ |
| 1180 |
AstNodePtr<charT> glob = ParserConcat(); |
| 1181 |
|
| 1182 |
if (GetToken() != TokenKind::EOS) { |
| 1183 |
throw Error("Expected the end of glob"); |
| 1184 |
} |
| 1185 |
|
| 1186 |
return AstNodePtr<charT>(new GlobNode<charT>(std::move(glob))); |
| 1187 |
} |
| 1188 |
|
| 1189 |
inline const Token<charT> &GetToken() const { return tok_vec_.at(pos_); } |
| 1190 |
|
| 1191 |
inline Token<charT> &GetToken() { return tok_vec_.at(pos_); } |
| 1192 |
|
| 1193 |
inline const Token<charT> &PeekAhead() const |
| 1194 |
{ |
| 1195 |
if (pos_ >= (tok_vec_.size() - 1)) { |
| 1196 |
return tok_vec_.back(); |
| 1197 |
} |
| 1198 |
|
| 1199 |
return tok_vec_.at(pos_ + 1); |
| 1200 |
} |
| 1201 |
|
| 1202 |
inline Token<charT> &NextToken() |
| 1203 |
{ |
| 1204 |
if (pos_ >= (tok_vec_.size() - 1)) { |
| 1205 |
return tok_vec_.back(); |
| 1206 |
} |
| 1207 |
|
| 1208 |
Token<charT> &tk = tok_vec_.at(pos_); |
| 1209 |
pos_++; |
| 1210 |
return tk; |
| 1211 |
} |
| 1212 |
|
| 1213 |
inline bool Advance() |
| 1214 |
{ |
| 1215 |
if (pos_ == tok_vec_.size() - 1) { |
| 1216 |
return false; |
| 1217 |
} |
| 1218 |
|
| 1219 |
++pos_; |
| 1220 |
return true; |
| 1221 |
} |
| 1222 |
|
| 1223 |
inline size_t Size() const noexcept { return tok_vec_.size(); } |
| 1224 |
|
| 1225 |
std::vector<Token<charT>> tok_vec_; |
| 1226 |
size_t pos_; |
| 1227 |
}; |
| 1228 |
|
| 1229 |
template <class charT> class AstConsumer |
| 1230 |
{ |
| 1231 |
public: |
| 1232 |
AstConsumer() = default; |
| 1233 |
|
| 1234 |
void GenAutomata(AstNode<charT> *root_node, Automata<charT> &automata) |
| 1235 |
{ |
| 1236 |
AstNode<charT> *concat_node = static_cast<GlobNode<charT> *>(root_node)->GetConcat(); |
| 1237 |
ExecConcat(concat_node, automata); |
| 1238 |
|
| 1239 |
size_t match_state = automata.template NewState<StateMatch<charT>>(); |
| 1240 |
automata.GetState(preview_state_).AddNextState(match_state); |
| 1241 |
automata.SetMatchState(match_state); |
| 1242 |
|
| 1243 |
size_t fail_state = automata.template NewState<StateFail<charT>>(); |
| 1244 |
automata.SetFailState(fail_state); |
| 1245 |
} |
| 1246 |
|
| 1247 |
private: |
| 1248 |
void ExecConcat(AstNode<charT> *node, Automata<charT> &automata) |
| 1249 |
{ |
| 1250 |
auto *concat_node = static_cast<ConcatNode<charT> *>(node); |
| 1251 |
auto &basic_globs = concat_node->GetBasicGlobs(); |
| 1252 |
|
| 1253 |
for (auto &basic_glob : basic_globs) { |
| 1254 |
ExecBasicGlob(basic_glob.get(), automata); |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
void ExecBasicGlob(AstNode<charT> *node, Automata<charT> &automata) |
| 1259 |
{ |
| 1260 |
switch (node->GetType()) { |
| 1261 |
case AstNode<charT>::Type::CHAR: ExecChar(node, automata); break; |
| 1262 |
|
| 1263 |
case AstNode<charT>::Type::ANY: ExecAny(node, automata); break; |
| 1264 |
|
| 1265 |
case AstNode<charT>::Type::STAR: ExecStar(node, automata); break; |
| 1266 |
|
| 1267 |
case AstNode<charT>::Type::POS_SET: ExecPositiveSet(node, automata); break; |
| 1268 |
|
| 1269 |
case AstNode<charT>::Type::NEG_SET: ExecNegativeSet(node, automata); break; |
| 1270 |
|
| 1271 |
case AstNode<charT>::Type::GROUP: ExecGroup(node, automata); break; |
| 1272 |
|
| 1273 |
default: break; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
void ExecChar(AstNode<charT> *node, Automata<charT> &automata) |
| 1278 |
{ |
| 1279 |
auto *char_node = static_cast<CharNode<charT> *>(node); |
| 1280 |
char c = char_node->GetValue(); |
| 1281 |
NewState<StateChar<charT>>(automata, c); |
| 1282 |
} |
| 1283 |
|
| 1284 |
void ExecAny(AstNode<charT> *, Automata<charT> &automata) |
| 1285 |
{ |
| 1286 |
NewState<StateAny<charT>>(automata); |
| 1287 |
} |
| 1288 |
|
| 1289 |
void ExecStar(AstNode<charT> *, Automata<charT> &automata) |
| 1290 |
{ |
| 1291 |
NewState<StateStar<charT>>(automata); |
| 1292 |
automata.GetState(current_state_).AddNextState(current_state_); |
| 1293 |
} |
| 1294 |
|
| 1295 |
void ExecPositiveSet(AstNode<charT> *node, Automata<charT> &automata) |
| 1296 |
{ |
| 1297 |
auto *pos_set_node = static_cast<PositiveSetNode<charT> *>(node); |
| 1298 |
|
| 1299 |
auto items = ProcessSetItems(pos_set_node->GetSet()); |
| 1300 |
NewState<StateSet<charT>>(automata, std::move(items)); |
| 1301 |
} |
| 1302 |
|
| 1303 |
void ExecNegativeSet(AstNode<charT> *node, Automata<charT> &automata) |
| 1304 |
{ |
| 1305 |
auto *pos_set_node = static_cast<NegativeSetNode<charT> *>(node); |
| 1306 |
|
| 1307 |
auto items = ProcessSetItems(pos_set_node->GetSet()); |
| 1308 |
NewState<StateSet<charT>>(automata, std::move(items), /*neg*/ true); |
| 1309 |
} |
| 1310 |
|
| 1311 |
std::vector<std::unique_ptr<SetItem<charT>>> ProcessSetItems(AstNode<charT> *node) |
| 1312 |
{ |
| 1313 |
auto *set_node = static_cast<SetItemsNode<charT> *>(node); |
| 1314 |
std::vector<std::unique_ptr<SetItem<charT>>> vec; |
| 1315 |
auto &items = set_node->GetItems(); |
| 1316 |
for (auto &item : items) { |
| 1317 |
vec.push_back(std::move(ProcessSetItem(item.get()))); |
| 1318 |
} |
| 1319 |
|
| 1320 |
return vec; |
| 1321 |
} |
| 1322 |
|
| 1323 |
std::unique_ptr<SetItem<charT>> ProcessSetItem(AstNode<charT> *node) |
| 1324 |
{ |
| 1325 |
if (node->GetType() == AstNode<charT>::Type::CHAR) { |
| 1326 |
auto *char_node = static_cast<CharNode<charT> *>(node); |
| 1327 |
char c = char_node->GetValue(); |
| 1328 |
return std::unique_ptr<SetItem<charT>>(new SetItemChar<charT>(c)); |
| 1329 |
} |
| 1330 |
if (node->GetType() == AstNode<charT>::Type::RANGE) { |
| 1331 |
auto *range_node = static_cast<RangeNode<charT> *>(node); |
| 1332 |
auto *start_node = static_cast<CharNode<charT> *>(range_node->GetStart()); |
| 1333 |
auto *end_node = static_cast<CharNode<charT> *>(range_node->GetEnd()); |
| 1334 |
|
| 1335 |
char start_char = start_node->GetValue(); |
| 1336 |
char end_char = end_node->GetValue(); |
| 1337 |
return std::unique_ptr<SetItem<charT>>(new SetItemRange<charT>(start_char, end_char)); |
| 1338 |
} |
| 1339 |
throw Error("Not valid set item"); |
| 1340 |
} |
| 1341 |
|
| 1342 |
void ExecGroup(AstNode<charT> *node, Automata<charT> &automata) |
| 1343 |
{ |
| 1344 |
auto *group_node = static_cast<GroupNode<charT> *>(node); |
| 1345 |
auto *union_node = group_node->GetGlob(); |
| 1346 |
auto automatas = ExecUnion(union_node); |
| 1347 |
|
| 1348 |
typename StateGroup<charT>::Type state_group_type{}; |
| 1349 |
switch (group_node->GetGroupType()) { |
| 1350 |
case GroupNode<charT>::GroupType::BASIC: |
| 1351 |
state_group_type = StateGroup<charT>::Type::BASIC; |
| 1352 |
break; |
| 1353 |
|
| 1354 |
case GroupNode<charT>::GroupType::ANY: state_group_type = StateGroup<charT>::Type::ANY; break; |
| 1355 |
|
| 1356 |
case GroupNode<charT>::GroupType::STAR: |
| 1357 |
state_group_type = StateGroup<charT>::Type::STAR; |
| 1358 |
break; |
| 1359 |
|
| 1360 |
case GroupNode<charT>::GroupType::PLUS: |
| 1361 |
state_group_type = StateGroup<charT>::Type::PLUS; |
| 1362 |
break; |
| 1363 |
|
| 1364 |
case GroupNode<charT>::GroupType::AT: state_group_type = StateGroup<charT>::Type::AT; break; |
| 1365 |
|
| 1366 |
case GroupNode<charT>::GroupType::NEG: state_group_type = StateGroup<charT>::Type::NEG; break; |
| 1367 |
} |
| 1368 |
|
| 1369 |
NewState<StateGroup<charT>>(automata, state_group_type, std::move(automatas)); |
| 1370 |
automata.GetState(current_state_).AddNextState(current_state_); |
| 1371 |
} |
| 1372 |
|
| 1373 |
std::vector<std::unique_ptr<Automata<charT>>> ExecUnion(AstNode<charT> *node) |
| 1374 |
{ |
| 1375 |
auto *union_node = static_cast<UnionNode<charT> *>(node); |
| 1376 |
auto &items = union_node->GetItems(); |
| 1377 |
std::vector<std::unique_ptr<Automata<charT>>> vec_automatas; |
| 1378 |
for (auto &item : items) { |
| 1379 |
std::unique_ptr<Automata<charT>> automata_ptr(new Automata<charT>); |
| 1380 |
AstConsumer ast_consumer; |
| 1381 |
ast_consumer.ExecConcat(item.get(), *automata_ptr); |
| 1382 |
|
| 1383 |
size_t match_state = automata_ptr->template NewState<StateMatch<charT>>(); |
| 1384 |
automata_ptr->GetState(ast_consumer.preview_state_).AddNextState(match_state); |
| 1385 |
automata_ptr->SetMatchState(match_state); |
| 1386 |
|
| 1387 |
size_t fail_state = automata_ptr->template NewState<StateFail<charT>>(); |
| 1388 |
automata_ptr->SetFailState(fail_state); |
| 1389 |
|
| 1390 |
vec_automatas.push_back(std::move(automata_ptr)); |
| 1391 |
} |
| 1392 |
|
| 1393 |
return vec_automatas; |
| 1394 |
} |
| 1395 |
|
| 1396 |
template <class T, typename... Args> void NewState(Automata<charT> &automata, Args &&...args) |
| 1397 |
{ |
| 1398 |
current_state_ = automata.template NewState<T>(std::forward<Args>(args)...); |
| 1399 |
if (preview_state_ >= 0) { |
| 1400 |
automata.GetState(preview_state_).AddNextState(current_state_); |
| 1401 |
} |
| 1402 |
preview_state_ = current_state_; |
| 1403 |
} |
| 1404 |
|
| 1405 |
int preview_state_ = -1; |
| 1406 |
size_t current_state_ = 0; |
| 1407 |
}; |
| 1408 |
|
| 1409 |
template <class charT> class ExtendedGlob |
| 1410 |
{ |
| 1411 |
public: |
| 1412 |
explicit ExtendedGlob(const String<charT> &pattern) |
| 1413 |
{ |
| 1414 |
Lexer<charT> l(pattern); |
| 1415 |
std::vector<Token<charT>> tokens = l.Scanner(); |
| 1416 |
Parser<charT> p(std::move(tokens)); |
| 1417 |
AstNodePtr<charT> ast_ptr = p.GenAst(); |
| 1418 |
|
| 1419 |
AstConsumer<charT> ast_consumer; |
| 1420 |
ast_consumer.GenAutomata(ast_ptr.get(), automata_); |
| 1421 |
} |
| 1422 |
|
| 1423 |
ExtendedGlob(const ExtendedGlob &) = delete; |
| 1424 |
ExtendedGlob &operator=(ExtendedGlob &) = delete; |
| 1425 |
|
| 1426 |
ExtendedGlob(ExtendedGlob &&glob) : automata_{std::move(glob.automata_)} {} |
| 1427 |
|
| 1428 |
ExtendedGlob &operator=(ExtendedGlob &&glob) |
| 1429 |
{ |
| 1430 |
automata_ = std::move(glob.automata_); |
| 1431 |
return *this; |
| 1432 |
} |
| 1433 |
|
| 1434 |
bool Exec(const String<charT> &str) |
| 1435 |
{ |
| 1436 |
bool r; |
| 1437 |
std::tie(r, std::ignore) = automata_.Exec(str); |
| 1438 |
return r; |
| 1439 |
} |
| 1440 |
|
| 1441 |
const Automata<charT> &GetAutomata() const { return automata_; } |
| 1442 |
|
| 1443 |
private: |
| 1444 |
Automata<charT> automata_; |
| 1445 |
}; |
| 1446 |
|
| 1447 |
template <class charT> class SimpleGlob |
| 1448 |
{ |
| 1449 |
public: |
| 1450 |
explicit SimpleGlob(const String<charT> &pattern) { Parser(pattern); } |
| 1451 |
|
| 1452 |
SimpleGlob(const SimpleGlob &) = delete; |
| 1453 |
SimpleGlob &operator=(SimpleGlob &) = delete; |
| 1454 |
|
| 1455 |
SimpleGlob(SimpleGlob &&glob) : automata_{std::move(glob.automata_)} {} |
| 1456 |
|
| 1457 |
SimpleGlob &operator=(SimpleGlob &&glob) |
| 1458 |
{ |
| 1459 |
automata_ = std::move(glob.automata_); |
| 1460 |
return *this; |
| 1461 |
} |
| 1462 |
|
| 1463 |
void Parser(const String<charT> &pattern) |
| 1464 |
{ |
| 1465 |
size_t pos = 0; |
| 1466 |
int preview_state = -1; |
| 1467 |
|
| 1468 |
while (pos < pattern.length()) { |
| 1469 |
size_t current_state = 0; |
| 1470 |
char c = pattern[pos]; |
| 1471 |
switch (c) { |
| 1472 |
case '?': { |
| 1473 |
current_state = automata_.template NewState<StateAny<charT>>(); |
| 1474 |
++pos; |
| 1475 |
break; |
| 1476 |
} |
| 1477 |
|
| 1478 |
case '*': { |
| 1479 |
current_state = automata_.template NewState<StateStar<charT>>(); |
| 1480 |
automata_.GetState(current_state).AddNextState(current_state); |
| 1481 |
++pos; |
| 1482 |
break; |
| 1483 |
} |
| 1484 |
|
| 1485 |
default: { |
| 1486 |
current_state = automata_.template NewState<StateChar<charT>>(c); |
| 1487 |
++pos; |
| 1488 |
break; |
| 1489 |
} |
| 1490 |
} |
| 1491 |
|
| 1492 |
if (preview_state >= 0) { |
| 1493 |
automata_.GetState(preview_state).AddNextState(current_state); |
| 1494 |
} |
| 1495 |
preview_state = current_state; |
| 1496 |
} |
| 1497 |
|
| 1498 |
size_t match_state = automata_.template NewState<StateMatch<charT>>(); |
| 1499 |
automata_.GetState(preview_state).AddNextState(match_state); |
| 1500 |
automata_.SetMatchState(match_state); |
| 1501 |
|
| 1502 |
size_t fail_state = automata_.template NewState<StateFail<charT>>(); |
| 1503 |
automata_.SetFailState(fail_state); |
| 1504 |
} |
| 1505 |
|
| 1506 |
bool Exec(const String<charT> &str) const |
| 1507 |
{ |
| 1508 |
bool r; |
| 1509 |
std::tie(r, std::ignore) = automata_.Exec(str); |
| 1510 |
return r; |
| 1511 |
} |
| 1512 |
|
| 1513 |
const Automata<charT> &GetAutomata() const { return automata_; } |
| 1514 |
|
| 1515 |
private: |
| 1516 |
Automata<charT> automata_; |
| 1517 |
}; |
| 1518 |
|
| 1519 |
template <class charT> using extended_glob = ExtendedGlob<charT>; |
| 1520 |
|
| 1521 |
template <class charT> using no_extended_glob = SimpleGlob<charT>; |
| 1522 |
|
| 1523 |
template <class charT> class MatchResults; |
| 1524 |
|
| 1525 |
template <class charT, class globT = extended_glob<charT>> class BasicGlob |
| 1526 |
{ |
| 1527 |
public: |
| 1528 |
explicit BasicGlob(const String<charT> &pattern) : glob_{pattern} {} |
| 1529 |
|
| 1530 |
BasicGlob(const BasicGlob &) = delete; |
| 1531 |
BasicGlob &operator=(BasicGlob &) = delete; |
| 1532 |
|
| 1533 |
BasicGlob(BasicGlob &&glob) : glob_{std::move(glob.glob_)} {} |
| 1534 |
|
| 1535 |
BasicGlob &operator=(BasicGlob &&glob) |
| 1536 |
{ |
| 1537 |
glob_ = std::move(glob.glob_); |
| 1538 |
return *this; |
| 1539 |
} |
| 1540 |
|
| 1541 |
const Automata<charT> &GetAutomata() const { return glob_.GetAutomata(); } |
| 1542 |
|
| 1543 |
private: |
| 1544 |
bool Exec(const String<charT> &str) { return glob_.Exec(str); } |
| 1545 |
|
| 1546 |
template <class charU, class globU> |
| 1547 |
friend bool glob_match(const String<charU> &str, BasicGlob<charU, globU> &glob); |
| 1548 |
|
| 1549 |
template <class charU, class globU> |
| 1550 |
friend bool glob_match(const charU *str, BasicGlob<charU, globU> &glob); |
| 1551 |
|
| 1552 |
template <class charU, class globU> |
| 1553 |
friend bool glob_match(const String<charU> &str, MatchResults<charU> &res, |
| 1554 |
BasicGlob<charU, globU> &glob); |
| 1555 |
|
| 1556 |
template <class charU, class globU> |
| 1557 |
friend bool glob_match(const charU *str, MatchResults<charU> &res, |
| 1558 |
BasicGlob<charU, globU> &glob); |
| 1559 |
|
| 1560 |
globT glob_; |
| 1561 |
}; |
| 1562 |
|
| 1563 |
template <class charT> class MatchResults |
| 1564 |
{ |
| 1565 |
public: |
| 1566 |
using const_iterator = typename std::vector<String<charT>>::const_iterator; |
| 1567 |
|
| 1568 |
MatchResults() = default; |
| 1569 |
|
| 1570 |
MatchResults(const MatchResults &m) : results_{m.results_} {} |
| 1571 |
|
| 1572 |
MatchResults(MatchResults &&m) : results_{std::move(m.results_)} {} |
| 1573 |
|
| 1574 |
MatchResults &operator=(const MatchResults &m) |
| 1575 |
{ |
| 1576 |
results_ = m.results_; |
| 1577 |
|
| 1578 |
return *this; |
| 1579 |
} |
| 1580 |
|
| 1581 |
MatchResults &operator=(MatchResults &&m) |
| 1582 |
{ |
| 1583 |
results_ = std::move(m.results_); |
| 1584 |
|
| 1585 |
return *this; |
| 1586 |
} |
| 1587 |
|
| 1588 |
bool empty() const { return results_.empty(); } |
| 1589 |
|
| 1590 |
size_t size() const { return results_.size(); } |
| 1591 |
|
| 1592 |
const_iterator begin() const noexcept { return results_.begin(); } |
| 1593 |
|
| 1594 |
const_iterator end() const noexcept { return results_.end(); } |
| 1595 |
|
| 1596 |
const_iterator cbegin() const noexcept { return results_.cbegin(); } |
| 1597 |
|
| 1598 |
const_iterator cend() const noexcept { return results_.cend(); } |
| 1599 |
|
| 1600 |
String<charT> &operator[](size_t n) const { return results_[n]; } |
| 1601 |
|
| 1602 |
private: |
| 1603 |
void SetResults(std::vector<String<charT>> &&results) { results_ = std::move(results); } |
| 1604 |
|
| 1605 |
template <class charU, class globU> |
| 1606 |
friend bool glob_match(const String<charU> &str, BasicGlob<charU, globU> &glob); |
| 1607 |
|
| 1608 |
template <class charU, class globU> |
| 1609 |
friend bool glob_match(const charU *str, BasicGlob<charU, globU> &glob); |
| 1610 |
|
| 1611 |
template <class charU, class globU> |
| 1612 |
friend bool glob_match(const String<charU> &str, MatchResults<charU> &res, |
| 1613 |
BasicGlob<charU, globU> &glob); |
| 1614 |
|
| 1615 |
template <class charU, class globU> |
| 1616 |
friend bool glob_match(const charU *str, MatchResults<charU> &res, |
| 1617 |
BasicGlob<charU, globU> &glob); |
| 1618 |
|
| 1619 |
std::vector<String<charT>> results_; |
| 1620 |
}; |
| 1621 |
|
| 1622 |
template <class charT, class globT = extended_glob<charT>> |
| 1623 |
bool glob_match(const String<charT> &str, BasicGlob<charT, globT> &glob) |
| 1624 |
{ |
| 1625 |
return glob.Exec(str); |
| 1626 |
} |
| 1627 |
|
| 1628 |
template <class charT, class globT = extended_glob<charT>> |
| 1629 |
bool glob_match(const charT *str, BasicGlob<charT, globT> &glob) |
| 1630 |
{ |
| 1631 |
return glob.Exec(str); |
| 1632 |
} |
| 1633 |
|
| 1634 |
template <class charT, class globT = extended_glob<charT>> |
| 1635 |
bool glob_match(const String<charT> &str, MatchResults<charT> &res, BasicGlob<charT, globT> &glob) |
| 1636 |
{ |
| 1637 |
bool r = glob.Exec(str); |
| 1638 |
res.SetResults(glob.GetAutomata().GetMatchedStrings()); |
| 1639 |
return r; |
| 1640 |
} |
| 1641 |
|
| 1642 |
template <class charT, class globT = extended_glob<charT>> |
| 1643 |
bool glob_match(const charT *str, MatchResults<charT> &res, BasicGlob<charT, globT> &glob) |
| 1644 |
{ |
| 1645 |
bool r = glob.Exec(str); |
| 1646 |
res.SetResults(glob.GetAutomata().GetMatchedStrings()); |
| 1647 |
return r; |
| 1648 |
} |
| 1649 |
|
| 1650 |
template <class charT, class globT = extended_glob<charT>> |
| 1651 |
using basic_glob = BasicGlob<charT, globT>; |
| 1652 |
|
| 1653 |
using glob = basic_glob<char, extended_glob<char>>; |
| 1654 |
|
| 1655 |
using wglob = basic_glob<wchar_t, extended_glob<wchar_t>>; |
| 1656 |
|
| 1657 |
using cmatch = MatchResults<char>; |
| 1658 |
|
| 1659 |
using wmatch = MatchResults<wchar_t>; |
| 1660 |
|
| 1661 |
} // namespace Ioss::glob |