-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathre_keywords.hpp
82 lines (69 loc) · 2.39 KB
/
re_keywords.hpp
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
82
#ifndef MINI_REGEXP_KEYWORDS_H_
#define MINI_REGEXP_KEYWORDS_H_
#include <string>
#include <algorithm>
#include "re_token.hpp"
#include "re_config.hpp"
namespace mini_regexp_keywords
{
using namespace mini_regexp_token;
using namespace mini_regexp_config;
class RE_Keywords
{
private:
/* 正则关键词 */
const std::string REGEXP_KEYWORDS = ".+?*|,(){}^$[]\\";
bool KEYWORDS_BOOL[0xff];
/* 正则(简单操作)关键词到TOKEN转换数组 */
TOKEN KEYWORDS_NORMAL_TOKEN[0xff];
/* 正则预定义转义字符 */
std::string REGEXP_ESCAPE_CHAR[0xff];
public:
RE_Keywords()
{
std::fill(KEYWORDS_BOOL, KEYWORDS_BOOL + 0xff, false);
for (int i = 0; i < REGEXP_KEYWORDS.length(); ++i)
KEYWORDS_BOOL[REGEXP_KEYWORDS[i]] = true;
std::fill(KEYWORDS_NORMAL_TOKEN, KEYWORDS_NORMAL_TOKEN + 0xff, TOKEN::ERR);
KEYWORDS_NORMAL_TOKEN['.'] = TOKEN::ANY;
KEYWORDS_NORMAL_TOKEN['+'] = TOKEN::PLUS;
KEYWORDS_NORMAL_TOKEN['?'] = TOKEN::QUESTION;
KEYWORDS_NORMAL_TOKEN['*'] = TOKEN::CLOSURE;
KEYWORDS_NORMAL_TOKEN['|'] = TOKEN::OR;
KEYWORDS_NORMAL_TOKEN[')'] = TOKEN::RBRACKET;
KEYWORDS_NORMAL_TOKEN[','] = TOKEN::COMMA;
KEYWORDS_NORMAL_TOKEN['}'] = TOKEN::RBRACE;
KEYWORDS_NORMAL_TOKEN[']'] = TOKEN::SQUARE_RBRACKET;
/* 默认'[' ']' */
REGEXP_ESCAPE_CHAR['b'] = " ";
REGEXP_ESCAPE_CHAR['B'] = "^ ";
REGEXP_ESCAPE_CHAR['d'] = "0-9";
REGEXP_ESCAPE_CHAR['D'] = "^0-9";
REGEXP_ESCAPE_CHAR['s'] = " \f\n\r\t\v";
REGEXP_ESCAPE_CHAR['S'] = "^ \f\n\r\t\v";
REGEXP_ESCAPE_CHAR['w'] = "A-Za-z0-9_";
REGEXP_ESCAPE_CHAR['W'] = "^A-Za-z0-9_";
}
bool is_keyword(char c)
{
return KEYWORDS_BOOL[c];
}
bool is_normal_token(char c)
{
return KEYWORDS_NORMAL_TOKEN[c] != TOKEN::ERR;
}
TOKEN get_normal_token(char c)
{
return KEYWORDS_NORMAL_TOKEN[c];
}
bool is_escape_char(char c)
{
return REGEXP_ESCAPE_CHAR[c].length() != 0;
}
std::string& get_escape_char(char c)
{
return REGEXP_ESCAPE_CHAR[c];
}
};
}
#endif