Skip to content

Commit

Permalink
Unify C++, Dart, Go runtimes
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Kochurkin <[email protected]>
  • Loading branch information
KvanTTT committed Jun 21, 2022
1 parent 35edbbd commit fb6fee8
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 82 deletions.
6 changes: 3 additions & 3 deletions runtime/Cpp/runtime/src/atn/ATNConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* 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.
*/
Expand All @@ -23,7 +23,7 @@ inline constexpr size_t SUPPRESS_PRECEDENCE_FILTER = 0x40000000;
}

ATNConfig::ATNConfig(ATNState *state, size_t alt, Ref<const PredictionContext> context)
: ATNConfig(state, alt, std::move(context), 0, SemanticContext::NONE) {}
: ATNConfig(state, alt, std::move(context), 0, SemanticContext::Predicate::NONE) {}

ATNConfig::ATNConfig(ATNState *state, size_t alt, Ref<const PredictionContext> context, Ref<const SemanticContext> semanticContext)
: ATNConfig(state, alt, std::move(context), 0, std::move(semanticContext)) {}
Expand Down Expand Up @@ -94,7 +94,7 @@ std::string ATNConfig::toString(bool showAlt) const {
if (context) {
ss << ",[" << context->toString() << "]";
}
if (semanticContext != nullptr && semanticContext != SemanticContext::NONE) {
if (semanticContext != nullptr && semanticContext != SemanticContext::Predicate::NONE) {
ss << ",[" << semanticContext->toString() << "]";
}
if (getOuterContextDepth() > 0) {
Expand Down
6 changes: 3 additions & 3 deletions runtime/Cpp/runtime/src/atn/ATNConfigSet.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* 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.
*/
Expand Down Expand Up @@ -43,7 +43,7 @@ bool ATNConfigSet::add(const Ref<ATNConfig> &config, PredictionContextMergeCache
if (_readonly) {
throw IllegalStateException("This set is readonly");
}
if (config->semanticContext != SemanticContext::NONE) {
if (config->semanticContext != SemanticContext::Predicate::NONE) {
hasSemanticContext = true;
}
if (config->getOuterContextDepth() > 0) {
Expand Down Expand Up @@ -114,7 +114,7 @@ std::vector<Ref<const SemanticContext>> ATNConfigSet::getPredicates() const {
std::vector<Ref<const SemanticContext>> preds;
preds.reserve(configs.size());
for (const auto &c : configs) {
if (c->semanticContext != SemanticContext::NONE) {
if (c->semanticContext != SemanticContext::Predicate::NONE) {
preds.push_back(c->semanticContext);
}
}
Expand Down
10 changes: 5 additions & 5 deletions runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ std::vector<Ref<const SemanticContext>> ParserATNSimulator::getPredsForAmbigAlts
size_t nPredAlts = 0;
for (size_t i = 1; i <= nalts; i++) {
if (altToPred[i] == nullptr) {
altToPred[i] = SemanticContext::NONE;
} else if (altToPred[i] != SemanticContext::NONE) {
altToPred[i] = SemanticContext::Predicate::NONE;
} else if (altToPred[i] != SemanticContext::Predicate::NONE) {
nPredAlts++;
}
}
Expand All @@ -712,7 +712,7 @@ std::vector<Ref<const SemanticContext>> ParserATNSimulator::getPredsForAmbigAlts
std::vector<dfa::DFAState::PredPrediction> ParserATNSimulator::getPredicatePredictions(const antlrcpp::BitSet &ambigAlts,
const std::vector<Ref<const SemanticContext>> &altToPred) {
bool containsPredicate = std::find_if(altToPred.begin(), altToPred.end(), [](const Ref<const SemanticContext> &context) {
return context != SemanticContext::NONE;
return context != SemanticContext::Predicate::NONE;
}) != altToPred.end();
std::vector<dfa::DFAState::PredPrediction> pairs;
if (containsPredicate) {
Expand Down Expand Up @@ -767,7 +767,7 @@ std::pair<ATNConfigSet *, ATNConfigSet *> ParserATNSimulator::splitAccordingToSe
ATNConfigSet *succeeded(new ATNConfigSet(configs->fullCtx));
ATNConfigSet *failed(new ATNConfigSet(configs->fullCtx));
for (const auto &c : configs->configs) {
if (c->semanticContext != SemanticContext::NONE) {
if (c->semanticContext != SemanticContext::Predicate::NONE) {
bool predicateEvaluationResult = evalSemanticContext(c->semanticContext, outerContext, c->alt, configs->fullCtx);
if (predicateEvaluationResult) {
succeeded->add(c);
Expand All @@ -785,7 +785,7 @@ BitSet ParserATNSimulator::evalSemanticContext(const std::vector<dfa::DFAState::
ParserRuleContext *outerContext, bool complete) {
BitSet predictions;
for (const auto &prediction : predPredictions) {
if (prediction.pred == SemanticContext::NONE) {
if (prediction.pred == SemanticContext::Predicate::NONE) {
predictions.set(prediction.alt);
if (!complete) {
break;
Expand Down
4 changes: 2 additions & 2 deletions runtime/Cpp/runtime/src/atn/PredictionMode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* 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.
*/
Expand Down Expand Up @@ -62,7 +62,7 @@ bool PredictionModeClass::hasSLLConflictTerminatingPrediction(PredictionMode mod
// dup configs, tossing out semantic predicates
ATNConfigSet dup(true);
for (auto &config : configs->configs) {
Ref<ATNConfig> c = std::make_shared<ATNConfig>(*config, SemanticContext::NONE);
Ref<ATNConfig> c = std::make_shared<ATNConfig>(*config, SemanticContext::Predicate::NONE);
dup.add(c);
}
std::vector<antlrcpp::BitSet> altsets = getConflictingAltSubsets(&dup);
Expand Down
22 changes: 11 additions & 11 deletions runtime/Cpp/runtime/src/atn/SemanticContext.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* 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.
*/
Expand Down Expand Up @@ -137,7 +137,7 @@ bool SemanticContext::PrecedencePredicate::eval(Recognizer *parser, RuleContext
Ref<const SemanticContext> SemanticContext::PrecedencePredicate::evalPrecedence(Recognizer *parser,
RuleContext *parserCallStack) const {
if (parser->precpred(parserCallStack, precedence)) {
return SemanticContext::NONE;
return SemanticContext::Predicate::NONE;
}
return nullptr;
}
Expand Down Expand Up @@ -237,7 +237,7 @@ Ref<const SemanticContext> SemanticContext::AND::evalPrecedence(Recognizer *pars
// The AND context is false if any element is false.
return nullptr;
}
if (evaluated != NONE) {
if (evaluated != Predicate::NONE) {
// Reduce the result by skipping true elements.
operands.push_back(std::move(evaluated));
}
Expand All @@ -249,7 +249,7 @@ Ref<const SemanticContext> SemanticContext::AND::evalPrecedence(Recognizer *pars

if (operands.empty()) {
// All elements were true, so the AND context is true.
return NONE;
return Predicate::NONE;
}

Ref<const SemanticContext> result = std::move(operands[0]);
Expand Down Expand Up @@ -337,9 +337,9 @@ Ref<const SemanticContext> SemanticContext::OR::evalPrecedence(Recognizer *parse
for (const auto &context : getOperands()) {
auto evaluated = context->evalPrecedence(parser, parserCallStack);
differs |= (evaluated != context);
if (evaluated == NONE) {
if (evaluated == Predicate::NONE) {
// The OR context is true if any element is true.
return NONE;
return Predicate::NONE;
}
if (evaluated != nullptr) {
// Reduce the result by skipping false elements.
Expand Down Expand Up @@ -374,18 +374,18 @@ std::string SemanticContext::OR::toString() const {

//------------------ SemanticContext -----------------------------------------------------------------------------------

const Ref<const SemanticContext> SemanticContext::NONE = std::make_shared<Predicate>(INVALID_INDEX, INVALID_INDEX, false);
const Ref<const SemanticContext> SemanticContext::Predicate::NONE = std::make_shared<Predicate>(INVALID_INDEX, INVALID_INDEX, false);

Ref<const SemanticContext> SemanticContext::evalPrecedence(Recognizer * /*parser*/, RuleContext * /*parserCallStack*/) const {
return shared_from_this();
}

Ref<const SemanticContext> SemanticContext::And(Ref<const SemanticContext> a, Ref<const SemanticContext> b) {
if (!a || a == NONE) {
if (!a || a == Predicate::NONE) {
return b;
}

if (!b || b == NONE) {
if (!b || b == Predicate::NONE) {
return a;
}

Expand All @@ -405,8 +405,8 @@ Ref<const SemanticContext> SemanticContext::Or(Ref<const SemanticContext> a, Ref
return a;
}

if (a == NONE || b == NONE) {
return NONE;
if (a == Predicate::NONE || b == Predicate::NONE) {
return Predicate::NONE;
}

Ref<OR> result = std::make_shared<OR>(std::move(a), std::move(b));
Expand Down
14 changes: 7 additions & 7 deletions runtime/Cpp/runtime/src/atn/SemanticContext.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/* 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.
*/
Expand All @@ -20,12 +20,6 @@ namespace atn {
/// SemanticContext within the scope of this outer class.
class ANTLR4CPP_PUBLIC SemanticContext : public std::enable_shared_from_this<SemanticContext> {
public:
/**
* The default {@link SemanticContext}, which is semantically equivalent to
* a predicate of the form {@code {true}?}.
*/
static const Ref<const SemanticContext> NONE;

virtual ~SemanticContext() = default;

SemanticContextType getContextType() const { return _contextType; }
Expand Down Expand Up @@ -99,6 +93,12 @@ namespace atn {

class ANTLR4CPP_PUBLIC SemanticContext::Predicate final : public SemanticContext {
public:
/**
* The default {@link SemanticContext}, which is semantically equivalent to
* a predicate of the form {@code {true}?}.
*/
static const Ref<const SemanticContext> NONE;

static bool is(const SemanticContext &semanticContext) { return semanticContext.getContextType() == SemanticContextType::PREDICATE; }

static bool is(const SemanticContext *semanticContext) { return semanticContext != nullptr && is(*semanticContext); }
Expand Down
6 changes: 3 additions & 3 deletions runtime/Dart/lib/src/atn/src/atn_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class ATNConfig {
this.state,
this.alt,
this.context, [
this.semanticContext = SemanticContext.NONE,
this.semanticContext = Predicate.NONE,
]) : reachesIntoOuterContext = 0;

ATNConfig.dup(
Expand Down Expand Up @@ -169,7 +169,7 @@ class ATNConfig {
buf.write(context.toString());
buf.write(']');
}
if (semanticContext != SemanticContext.NONE) {
if (semanticContext != Predicate.NONE) {
buf.write(',');
buf.write(semanticContext);
}
Expand All @@ -194,7 +194,7 @@ class LexerATNConfig extends ATNConfig {
int alt,
PredictionContext context, [
this.lexerActionExecutor,
]) : super(state, alt, context, SemanticContext.NONE) {
]) : super(state, alt, context, Predicate.NONE) {
passedThroughNonGreedyDecision = false;
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/Dart/lib/src/atn/src/atn_config_set.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class ATNConfigSet extends Iterable<ATNConfig> {
mergeCache,
]) {
if (readOnly) throw StateError('This set is readonly');
if (config.semanticContext != SemanticContext.NONE) {
if (config.semanticContext != Predicate.NONE) {
hasSemanticContext = true;
}
if (config.outerContextDepth > 0) {
Expand Down Expand Up @@ -176,7 +176,7 @@ class ATNConfigSet extends Iterable<ATNConfig> {
List<SemanticContext?> get predicates {
final preds = <SemanticContext?>[];
for (var c in configs) {
if (c.semanticContext != SemanticContext.NONE) {
if (c.semanticContext != Predicate.NONE) {
preds.add(c.semanticContext);
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/Dart/lib/src/atn/src/atn_simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class PredictionContextCache {
/// return that one instead and do not add a new context to the cache.
/// Protect shared cache from unsafe thread access.
PredictionContext add(PredictionContext ctx) {
if (ctx == PredictionContext.EMPTY) return PredictionContext.EMPTY;
if (ctx == EmptyPredictionContext.Instance) return EmptyPredictionContext.Instance;
final existing = cache[ctx];
if (existing != null) {
// System.out.println(name+" reuses "+existing);
Expand Down
4 changes: 2 additions & 2 deletions runtime/Dart/lib/src/atn/src/lexer_atn_simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class LexerATNSimulator extends ATNSimulator {
}

ATNConfigSet computeStartState(CharStream input, ATNState p) {
PredictionContext initialContext = PredictionContext.EMPTY;
PredictionContext initialContext = EmptyPredictionContext.Instance;
ATNConfigSet configs = OrderedATNConfigSet();
for (var i = 0; i < p.numberOfTransitions; i++) {
final target = p.transition(i).target;
Expand Down Expand Up @@ -445,7 +445,7 @@ class LexerATNSimulator extends ATNSimulator {
configs.add(LexerATNConfig.dup(
config,
config.state,
context: PredictionContext.EMPTY,
context: EmptyPredictionContext.Instance,
));
currentAltReachedAcceptState = true;
}
Expand Down
14 changes: 7 additions & 7 deletions runtime/Dart/lib/src/atn/src/parser_atn_simulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,8 @@ class ParserATNSimulator extends ATNSimulator {
var nPredAlts = 0;
for (var i = 1; i <= nalts; i++) {
if (altToPred[i] == null) {
altToPred[i] = SemanticContext.NONE;
} else if (altToPred[i] != SemanticContext.NONE) {
altToPred[i] = Predicate.NONE;
} else if (altToPred[i] != Predicate.NONE) {
nPredAlts++;
}
}
Expand Down Expand Up @@ -1266,7 +1266,7 @@ class ParserATNSimulator extends ATNSimulator {
if (ambigAlts != null && ambigAlts[i]) {
pairs.add(PredPrediction(pred!, i));
}
if (pred != SemanticContext.NONE) containsPredicate = true;
if (pred != Predicate.NONE) containsPredicate = true;
}

if (!containsPredicate) {
Expand Down Expand Up @@ -1370,7 +1370,7 @@ class ParserATNSimulator extends ATNSimulator {
final succeeded = ATNConfigSet(configs.fullCtx);
final failed = ATNConfigSet(configs.fullCtx);
for (var c in configs) {
if (c.semanticContext != SemanticContext.NONE) {
if (c.semanticContext != Predicate.NONE) {
final predicateEvaluationResult = evalSemanticContextOne(
c.semanticContext,
outerContext,
Expand Down Expand Up @@ -1401,7 +1401,7 @@ class ParserATNSimulator extends ATNSimulator {
) {
final predictions = BitSet();
for (var pair in predPredictions) {
if (pair.pred == SemanticContext.NONE) {
if (pair.pred == Predicate.NONE) {
predictions.set(pair.alt);
if (!complete) {
break;
Expand Down Expand Up @@ -1511,7 +1511,7 @@ class ParserATNSimulator extends ATNSimulator {
ATNConfig.dup(
config,
state: config.state,
context: PredictionContext.EMPTY,
context: EmptyPredictionContext.Instance,
),
mergeCache);
continue;
Expand Down Expand Up @@ -2431,7 +2431,7 @@ extension PredictionModeExtension on PredictionMode {
// dup configs, tossing out semantic predicates
final dup = ATNConfigSet();
for (var c in configs) {
c = ATNConfig.dup(c, semanticContext: SemanticContext.NONE);
c = ATNConfig.dup(c, semanticContext: Predicate.NONE);
dup.add(c);
}
configs = dup;
Expand Down
Loading

0 comments on commit fb6fee8

Please sign in to comment.