forked from ifsmirnov/jngen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.h
81 lines (56 loc) · 1.44 KB
/
pattern.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#pragma once
#include "common.h"
#include <algorithm>
#include <cctype>
#include <functional>
#include <set>
#include <string>
#include <utility>
#include <vector>
// TODO: adequate error messages
namespace jngen {
class Pattern {
friend class Parser;
public:
Pattern() : isOrPattern(false), min(1), max(1) {}
Pattern(const std::string& s);
std::string next(std::function<int(int)>&& rnd) const;
private:
Pattern(Pattern p, std::pair<int, int> quantity);
Pattern(std::vector<char> chars, std::pair<int, int> quantity);
std::vector<char> chars;
std::vector<Pattern> children;
bool isOrPattern;
int min;
int max;
static std::map<std::string, Pattern> cachedPatterns_;
};
class Parser {
public:
Pattern parse(const std::string& s) {
this->s = s;
pos = 0;
return parsePattern();
}
private:
static bool isControl(char c);
static int control(int c);
int next();
int peek() const;
int peekAndMove(size_t& newPos) const;
// TODO: catch overflows
int readInt();
std::pair<int, int> parseRange();
std::pair<int, int> tryParseQuantity();
std::vector<char> parseBlock();
Pattern parsePattern();
std::string s;
size_t pos;
};
} // namespace jngen
#ifndef JNGEN_DECLARE_ONLY
#define JNGEN_INCLUDE_PATTERN_INL_H
#include "impl/pattern_inl.h"
#undef JNGEN_INCLUDE_PATTERN_INL_H
#endif // JNGEN_DECLARE_ONLY
using jngen::Pattern;