blob: 4cc0ab7cc1814fb7f1ec9f05d1fa695b27970c9f (
plain) (
blame)
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
108
109
110
111
112
113
114
115
|
/* 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 "dfa/DFASerializer.h"
#include "dfa/LexerDFASerializer.h"
#include "support/CPPUtils.h"
#include "atn/StarLoopEntryState.h"
#include "atn/ATNConfigSet.h"
#include "support/Casts.h"
#include "dfa/DFA.h"
using namespace antlr4;
using namespace antlr4::dfa;
using namespace antlrcpp;
DFA::DFA(atn::DecisionState *atnStartState) : DFA(atnStartState, 0) {
}
DFA::DFA(atn::DecisionState *atnStartState, size_t decision)
: atnStartState(atnStartState), s0(nullptr), decision(decision) {
_precedenceDfa = false;
if (atn::StarLoopEntryState::is(atnStartState)) {
if (downCast<atn::StarLoopEntryState*>(atnStartState)->isPrecedenceDecision) {
_precedenceDfa = true;
s0 = new DFAState(std::unique_ptr<atn::ATNConfigSet>(new atn::ATNConfigSet()));
s0->isAcceptState = false;
s0->requiresFullContext = false;
}
}
}
DFA::DFA(DFA &&other) : atnStartState(other.atnStartState), s0(other.s0), decision(other.decision) {
// Source states are implicitly cleared by the move.
states = std::move(other.states);
other.atnStartState = nullptr;
other.decision = 0;
other.s0 = nullptr;
_precedenceDfa = other._precedenceDfa;
other._precedenceDfa = false;
}
DFA::~DFA() {
bool s0InList = (s0 == nullptr);
for (auto *state : states) {
if (state == s0)
s0InList = true;
delete state;
}
if (!s0InList) {
delete s0;
}
}
bool DFA::isPrecedenceDfa() const {
return _precedenceDfa;
}
DFAState* DFA::getPrecedenceStartState(int precedence) const {
assert(_precedenceDfa); // Only precedence DFAs may contain a precedence start state.
auto iterator = s0->edges.find(precedence);
if (iterator == s0->edges.end())
return nullptr;
return iterator->second;
}
void DFA::setPrecedenceStartState(int precedence, DFAState *startState) {
if (!isPrecedenceDfa()) {
throw IllegalStateException("Only precedence DFAs may contain a precedence start state.");
}
if (precedence < 0) {
return;
}
s0->edges[precedence] = startState;
}
std::vector<DFAState *> DFA::getStates() const {
std::vector<DFAState *> result;
for (auto *state : states)
result.push_back(state);
std::sort(result.begin(), result.end(), [](DFAState *o1, DFAState *o2) -> bool {
return o1->stateNumber < o2->stateNumber;
});
return result;
}
std::string DFA::toString(const Vocabulary &vocabulary) const {
if (s0 == nullptr) {
return "";
}
DFASerializer serializer(this, vocabulary);
return serializer.toString();
}
std::string DFA::toLexerString() const {
if (s0 == nullptr) {
return "";
}
LexerDFASerializer serializer(this);
return serializer.toString();
}
|