-
Notifications
You must be signed in to change notification settings - Fork 298
/
LexerActionExecutor.cpp
107 lines (86 loc) · 3.4 KB
/
LexerActionExecutor.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
#include "misc/MurmurHash.h"
#include "atn/LexerIndexedCustomAction.h"
#include "support/CPPUtils.h"
#include "support/Arrays.h"
#include "atn/LexerActionExecutor.h"
using namespace antlr4;
using namespace antlr4::atn;
using namespace antlr4::misc;
using namespace antlrcpp;
LexerActionExecutor::LexerActionExecutor(const std::vector<Ref<LexerAction>> &lexerActions)
: _lexerActions(lexerActions), _hashCode(generateHashCode()) {
}
LexerActionExecutor::~LexerActionExecutor() {
}
Ref<LexerActionExecutor> LexerActionExecutor::append(Ref<LexerActionExecutor> const& lexerActionExecutor,
Ref<LexerAction> const& lexerAction) {
if (lexerActionExecutor == nullptr) {
return std::make_shared<LexerActionExecutor>(std::vector<Ref<LexerAction>> { lexerAction });
}
std::vector<Ref<LexerAction>> lexerActions = lexerActionExecutor->_lexerActions; // Make a copy.
lexerActions.push_back(lexerAction);
return std::make_shared<LexerActionExecutor>(lexerActions);
}
Ref<LexerActionExecutor> LexerActionExecutor::fixOffsetBeforeMatch(int offset) {
std::vector<Ref<LexerAction>> updatedLexerActions;
for (size_t i = 0; i < _lexerActions.size(); i++) {
if (_lexerActions[i]->isPositionDependent() && !is<LexerIndexedCustomAction>(_lexerActions[i])) {
if (updatedLexerActions.empty()) {
updatedLexerActions = _lexerActions; // Make a copy.
}
updatedLexerActions[i] = std::make_shared<LexerIndexedCustomAction>(offset, _lexerActions[i]);
}
}
if (updatedLexerActions.empty()) {
return shared_from_this();
}
return std::make_shared<LexerActionExecutor>(updatedLexerActions);
}
std::vector<Ref<LexerAction>> LexerActionExecutor::getLexerActions() const {
return _lexerActions;
}
void LexerActionExecutor::execute(Lexer *lexer, CharStream *input, size_t startIndex) {
bool requiresSeek = false;
size_t stopIndex = input->index();
auto onExit = finally([requiresSeek, input, stopIndex]() {
if (requiresSeek) {
input->seek(stopIndex);
}
});
for (auto lexerAction : _lexerActions) {
if (is<LexerIndexedCustomAction>(lexerAction)) {
int offset = (std::static_pointer_cast<LexerIndexedCustomAction>(lexerAction))->getOffset();
input->seek(startIndex + offset);
lexerAction = std::static_pointer_cast<LexerIndexedCustomAction>(lexerAction)->getAction();
requiresSeek = (startIndex + offset) != stopIndex;
} else if (lexerAction->isPositionDependent()) {
input->seek(stopIndex);
requiresSeek = false;
}
lexerAction->execute(lexer);
}
}
size_t LexerActionExecutor::hashCode() const {
return _hashCode;
}
bool LexerActionExecutor::operator == (const LexerActionExecutor &obj) const {
if (&obj == this) {
return true;
}
return _hashCode == obj._hashCode && Arrays::equals(_lexerActions, obj._lexerActions);
}
bool LexerActionExecutor::operator != (const LexerActionExecutor &obj) const {
return !operator==(obj);
}
size_t LexerActionExecutor::generateHashCode() const {
size_t hash = MurmurHash::initialize();
for (auto lexerAction : _lexerActions) {
hash = MurmurHash::update(hash, lexerAction);
}
hash = MurmurHash::finish(hash, _lexerActions.size());
return hash;
}