-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalCategory.cpp
More file actions
93 lines (55 loc) · 1.7 KB
/
Copy pathLexicalCategory.cpp
File metadata and controls
93 lines (55 loc) · 1.7 KB
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
83
84
85
86
87
88
89
90
91
92
93
#include "LexicalCategory.h"
#include "LexicalExpression.h"
static bool lexicalExpressionCompareLength(LexicalExpression& a, LexicalExpression& b) {
return a.getExpressionLength() > b.getExpressionLength();
}
static void sortExpressions(std::list<LexicalExpression>& expressionList) {
expressionList.sort(lexicalExpressionCompareLength);
}
bool LexicalCategory::check(const char* text)
{
bool result = false;
bool whitelistExists = false;
if (!this->sorted) {
sortExpressions(this->whitelist);
sortExpressions(this->blacklist);
this->sorted = true;
}
for (LexicalExpression expression : this->whitelist)
{
result |= expression.check(text);
whitelistExists = true;
}
if (!result && whitelistExists) return false;
result = true;
for (LexicalExpression expression : this->blacklist)
{
result &= !expression.check(text);
}
return result;
}
std::string LexicalCategory::getName()
{
return this->name;
}
void LexicalCategory::addExpression(LexicalExpression expression, bool isBlacklist) {
if (isBlacklist) this->blacklist.push_back(expression);
else this->whitelist.push_back(expression);
this->sorted = false;
}
LexicalCategory::~LexicalCategory() {
this->blacklist.clear();
this->whitelist.clear();
}
LexicalCategory::LexicalCategory(std::string name) {
this->name = name;
this->sorted = false;
}
LexicalExpression LexicalCategory::get(unsigned int index, bool isBlacklist) {
auto iterator = ((isBlacklist)?(this->blacklist):(this->whitelist)).begin();
for (int counter = 0; counter < index; counter++, iterator++);
return *iterator;
}
unsigned int LexicalCategory::getSize(bool isBlacklist) {
return ((isBlacklist) ? (this->blacklist.size()) : (whitelist.size()));
}