]> gitweb.ps.run Git - toc/blob - antlr4-cpp-runtime-4.9.2-source/runtime/src/atn/ATNState.cpp
add antlr source code and ReadMe
[toc] / antlr4-cpp-runtime-4.9.2-source / runtime / src / atn / ATNState.cpp
1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
2  * Use of this file is governed by the BSD 3-clause license that
3  * can be found in the LICENSE.txt file in the project root.
4  */
5
6 #include "atn/ATN.h"
7 #include "atn/Transition.h"
8 #include "misc/IntervalSet.h"
9 #include "support/CPPUtils.h"
10
11 #include "atn/ATNState.h"
12
13 using namespace antlr4::atn;
14 using namespace antlrcpp;
15
16 ATNState::ATNState() {
17 }
18
19 ATNState::~ATNState() {
20   for (auto *transition : transitions) {
21     delete transition;
22   }
23 }
24
25 const std::vector<std::string> ATNState::serializationNames = {
26   "INVALID", "BASIC", "RULE_START", "BLOCK_START",
27   "PLUS_BLOCK_START", "STAR_BLOCK_START", "TOKEN_START", "RULE_STOP",
28   "BLOCK_END", "STAR_LOOP_BACK", "STAR_LOOP_ENTRY", "PLUS_LOOP_BACK", "LOOP_END"
29 };
30
31 size_t ATNState::hashCode() {
32   return stateNumber;
33 }
34
35 bool ATNState::operator == (const ATNState &other) {
36   return stateNumber == other.stateNumber;
37 }
38
39 bool ATNState::isNonGreedyExitState() {
40   return false;
41 }
42
43 std::string ATNState::toString() const {
44   return std::to_string(stateNumber);
45 }
46
47 void ATNState::addTransition(Transition *e) {
48   addTransition(transitions.size(), e);
49 }
50
51 void ATNState::addTransition(size_t index, Transition *e) {
52   for (Transition *transition : transitions)
53     if (transition->target->stateNumber == e->target->stateNumber) {
54       delete e;
55       return;
56     }
57
58   if (transitions.empty()) {
59     epsilonOnlyTransitions = e->isEpsilon();
60   } else if (epsilonOnlyTransitions != e->isEpsilon()) {
61     std::cerr << "ATN state %d has both epsilon and non-epsilon transitions.\n" << stateNumber;
62     epsilonOnlyTransitions = false;
63   }
64
65   transitions.insert(transitions.begin() + index, e);
66 }
67
68 Transition *ATNState::removeTransition(size_t index) {
69   Transition *result = transitions[index];
70   transitions.erase(transitions.begin() + index);
71   return result;
72 }