summaryrefslogtreecommitdiff
path: root/deps/v8/third_party/antlr4/runtime/Cpp/runtime/src/atn/ATN.cpp
blob: d19adfc4d11f46ff5abddf5bfb4bb4b0e6adcc4a (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* 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 "Exceptions.h"
#include "Recognizer.h"
#include "RuleContext.h"
#include "Token.h"
#include "atn/ATNType.h"
#include "atn/DecisionState.h"
#include "atn/LL1Analyzer.h"
#include "atn/RuleTransition.h"
#include "misc/IntervalSet.h"
#include "support/CPPUtils.h"

#include "atn/ATN.h"

using namespace antlr4;
using namespace antlr4::atn;
using namespace antlrcpp;

ATN::ATN() : ATN(ATNType::LEXER, 0) {}

ATN::ATN(ATN&& other) {
  // All source vectors are implicitly cleared by the moves.
  states = std::move(other.states);
  decisionToState = std::move(other.decisionToState);
  ruleToStartState = std::move(other.ruleToStartState);
  ruleToStopState = std::move(other.ruleToStopState);
  grammarType = std::move(other.grammarType);
  maxTokenType = std::move(other.maxTokenType);
  ruleToTokenType = std::move(other.ruleToTokenType);
  lexerActions = std::move(other.lexerActions);
  modeToStartState = std::move(other.modeToStartState);
}

ATN::ATN(ATNType grammarType_, size_t maxTokenType_)
    : grammarType(grammarType_), maxTokenType(maxTokenType_) {}

ATN::~ATN() {
  for (ATNState* state : states) {
    delete state;
  }
}

/**
 * Required to be defined (even though not used) as we have an explicit move
 * assignment operator.
 */
ATN& ATN::operator=(ATN& other) NOEXCEPT {
  states = other.states;
  decisionToState = other.decisionToState;
  ruleToStartState = other.ruleToStartState;
  ruleToStopState = other.ruleToStopState;
  grammarType = other.grammarType;
  maxTokenType = other.maxTokenType;
  ruleToTokenType = other.ruleToTokenType;
  lexerActions = other.lexerActions;
  modeToStartState = other.modeToStartState;

  return *this;
}

/**
 * Explicit move assignment operator to make this the preferred assignment. With
 * implicit copy/move assignment operators it seems the copy operator is
 * preferred causing trouble when releasing the allocated ATNState instances.
 */
ATN& ATN::operator=(ATN&& other) NOEXCEPT {
  // All source vectors are implicitly cleared by the moves.
  states = std::move(other.states);
  decisionToState = std::move(other.decisionToState);
  ruleToStartState = std::move(other.ruleToStartState);
  ruleToStopState = std::move(other.ruleToStopState);
  grammarType = std::move(other.grammarType);
  maxTokenType = std::move(other.maxTokenType);
  ruleToTokenType = std::move(other.ruleToTokenType);
  lexerActions = std::move(other.lexerActions);
  modeToStartState = std::move(other.modeToStartState);

  return *this;
}

misc::IntervalSet ATN::nextTokens(ATNState* s, RuleContext* ctx) const {
  LL1Analyzer analyzer(*this);
  return analyzer.LOOK(s, ctx);
}

misc::IntervalSet const& ATN::nextTokens(ATNState* s) const {
  if (!s->_nextTokenUpdated) {
    std::unique_lock<std::mutex> lock{_mutex};
    if (!s->_nextTokenUpdated) {
      s->_nextTokenWithinRule = nextTokens(s, nullptr);
      s->_nextTokenUpdated = true;
    }
  }
  return s->_nextTokenWithinRule;
}

void ATN::addState(ATNState* state) {
  if (state != nullptr) {
    // state->atn = this;
    state->stateNumber = static_cast<int>(states.size());
  }

  states.push_back(state);
}

void ATN::removeState(ATNState* state) {
  delete states.at(
      state->stateNumber);  // just free mem, don't shift states in list
  states.at(state->stateNumber) = nullptr;
}

int ATN::defineDecisionState(DecisionState* s) {
  decisionToState.push_back(s);
  s->decision = static_cast<int>(decisionToState.size() - 1);
  return s->decision;
}

DecisionState* ATN::getDecisionState(size_t decision) const {
  if (!decisionToState.empty()) {
    return decisionToState[decision];
  }
  return nullptr;
}

size_t ATN::getNumberOfDecisions() const { return decisionToState.size(); }

misc::IntervalSet ATN::getExpectedTokens(size_t stateNumber,
                                         RuleContext* context) const {
  if (stateNumber == ATNState::INVALID_STATE_NUMBER ||
      stateNumber >= states.size()) {
    throw IllegalArgumentException("Invalid state number.");
  }

  RuleContext* ctx = context;
  ATNState* s = states.at(stateNumber);
  misc::IntervalSet following = nextTokens(s);
  if (!following.contains(Token::EPSILON)) {
    return following;
  }

  misc::IntervalSet expected;
  expected.addAll(following);
  expected.remove(Token::EPSILON);
  while (ctx && ctx->invokingState != ATNState::INVALID_STATE_NUMBER &&
         following.contains(Token::EPSILON)) {
    ATNState* invokingState = states.at(ctx->invokingState);
    RuleTransition* rt =
        static_cast<RuleTransition*>(invokingState->transitions[0]);
    following = nextTokens(rt->followState);
    expected.addAll(following);
    expected.remove(Token::EPSILON);

    if (ctx->parent == nullptr) {
      break;
    }
    ctx = static_cast<RuleContext*>(ctx->parent);
  }

  if (following.contains(Token::EPSILON)) {
    expected.add(Token::EOF);
  }

  return expected;
}

std::string ATN::toString() const {
  std::stringstream ss;
  std::string type;
  switch (grammarType) {
    case ATNType::LEXER:
      type = "LEXER ";
      break;

    case ATNType::PARSER:
      type = "PARSER ";
      break;

    default:
      break;
  }
  ss << "(" << type << "ATN " << std::hex << this << std::dec
     << ") maxTokenType: " << maxTokenType << std::endl;
  ss << "states (" << states.size() << ") {" << std::endl;

  size_t index = 0;
  for (auto state : states) {
    if (state == nullptr) {
      ss << "  " << index++ << ": nul" << std::endl;
    } else {
      std::string text = state->toString();
      ss << "  " << index++ << ": " << indent(text, "  ", false) << std::endl;
    }
  }

  index = 0;
  for (auto state : decisionToState) {
    if (state == nullptr) {
      ss << "  " << index++ << ": nul" << std::endl;
    } else {
      std::string text = state->toString();
      ss << "  " << index++ << ": " << indent(text, "  ", false) << std::endl;
    }
  }

  ss << "}";

  return ss.str();
}