Skip to content

Commit

Permalink
#132 Refactor duplication of all tokens set computation
Browse files Browse the repository at this point in the history
Signed-off-by: vityaman <vityaman.dev@yandex.ru>
  • Loading branch information
vityaman committed Aug 7, 2024
1 parent 045f76f commit 5b276d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
35 changes: 18 additions & 17 deletions ports/cpp/source/antlr4-c3/CodeCompletionCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,25 +413,22 @@ bool CodeCompletionCore::collectFollowSets( // NOLINT
collectFollowSets(transition->target, stopState, followSets, stateStack, ruleStack);
isExhaustive = isExhaustive && nextStateFollowSetsIsExhaustive;
} else if (transition->getTransitionType() == antlr4::atn::TransitionType::WILDCARD) {
FollowSetWithPath set;
set.intervals = antlr4::misc::IntervalSet::of(
antlr4::Token::MIN_USER_TOKEN_TYPE, static_cast<ptrdiff_t>(atn->maxTokenType)
);
set.path = ruleStack;
followSets.emplace_back(std::move(set));
followSets.push_back({
.intervals = allUserTokens(),
.path = ruleStack,
.following = {},
});
} else {
antlr4::misc::IntervalSet label = transition->label();
if (!label.isEmpty()) {
if (transition->getTransitionType() == antlr4::atn::TransitionType::NOT_SET) {
label = label.complement(antlr4::misc::IntervalSet::of(
antlr4::Token::MIN_USER_TOKEN_TYPE, static_cast<ptrdiff_t>(atn->maxTokenType)
));
label = label.complement(allUserTokens());
}
FollowSetWithPath set;
set.intervals = label;
set.path = ruleStack;
set.following = getFollowingTokens(transition);
followSets.emplace_back(std::move(set));
followSets.push_back({
.intervals = label,
.path = ruleStack,
.following = getFollowingTokens(transition),
});
}
}
}
Expand Down Expand Up @@ -710,9 +707,7 @@ CodeCompletionCore::RuleEndStatus CodeCompletionCore::processRule( // NOLINT
antlr4::misc::IntervalSet set = transition->label();
if (!set.isEmpty()) {
if (transition->getTransitionType() == antlr4::atn::TransitionType::NOT_SET) {
set = set.complement(antlr4::misc::IntervalSet::of(
antlr4::Token::MIN_USER_TOKEN_TYPE, static_cast<ptrdiff_t>(atn->maxTokenType)
));
set = set.complement(allUserTokens());
}
if (atCaret) {
if (!translateStackToRuleIndex(callStack)) {
Expand Down Expand Up @@ -768,6 +763,12 @@ CodeCompletionCore::RuleEndStatus CodeCompletionCore::processRule( // NOLINT
return result;
}

antlr4::misc::IntervalSet CodeCompletionCore::allUserTokens() const {
const auto min = antlr4::Token::MIN_USER_TOKEN_TYPE;
const auto max = static_cast<ptrdiff_t>(atn->maxTokenType);
return antlr4::misc::IntervalSet::of(min, max);
}

std::string CodeCompletionCore::generateBaseDescription(antlr4::atn::ATNState* state) {
const std::string stateValue = (state->stateNumber == antlr4::atn::ATNState::INVALID_STATE_NUMBER)
? "Invalid"
Expand Down
4 changes: 4 additions & 0 deletions ports/cpp/source/antlr4-c3/CodeCompletionCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <Parser.h>
#include <ParserRuleContext.h>
#include <Token.h>
#include <Vocabulary.h>
#include <atn/ATNState.h>
#include <atn/PredicateTransition.h>
#include <atn/RuleStartState.h>
Expand All @@ -20,6 +21,7 @@
#include <chrono>
#include <cstddef>
#include <map>
#include <optional>
#include <string>
#include <typeindex>
#include <unordered_map>
Expand Down Expand Up @@ -244,6 +246,8 @@ class CodeCompletionCore {
bool& timedOut
);

antlr4::misc::IntervalSet allUserTokens() const;

std::string generateBaseDescription(antlr4::atn::ATNState* state);

void printDescription(
Expand Down

0 comments on commit 5b276d5

Please sign in to comment.