Fixed all ATNConfigSet memory leaks.

This commit is contained in:
Mike Lischke 2016-04-09 17:39:38 +02:00
parent 7f8ad7bd2d
commit 6f344b376b
25 changed files with 165 additions and 148 deletions

View File

@ -33,6 +33,9 @@ int main(int argc, const char * argv[]) {
ANTLRInputStream input(L"ab"); ANTLRInputStream input(L"ab");
TestLexer lexer(&input); TestLexer lexer(&input);
CommonTokenStream tokens(&lexer); CommonTokenStream tokens(&lexer);
for (auto token : tokens.getTokens()) {
std::cout << token << std::endl;
}
TestParser parser(&tokens); TestParser parser(&tokens);
std::shared_ptr<tree::ParseTree> tree = parser.main(); std::shared_ptr<tree::ParseTree> tree = parser.main();

View File

@ -598,7 +598,7 @@
27C666861C9584050021E494 /* LexerInterpreter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LexerInterpreter.cpp; path = ../../runtime/LexerInterpreter.cpp; sourceTree = SOURCE_ROOT; }; 27C666861C9584050021E494 /* LexerInterpreter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LexerInterpreter.cpp; path = ../../runtime/LexerInterpreter.cpp; sourceTree = SOURCE_ROOT; };
27C666871C9584050021E494 /* LexerInterpreter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LexerInterpreter.h; path = ../../runtime/LexerInterpreter.h; sourceTree = SOURCE_ROOT; }; 27C666871C9584050021E494 /* LexerInterpreter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LexerInterpreter.h; path = ../../runtime/LexerInterpreter.h; sourceTree = SOURCE_ROOT; };
27C666881C9584050021E494 /* LexerNoViableAltException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LexerNoViableAltException.cpp; path = ../../runtime/LexerNoViableAltException.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; }; 27C666881C9584050021E494 /* LexerNoViableAltException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LexerNoViableAltException.cpp; path = ../../runtime/LexerNoViableAltException.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
27C666891C9584050021E494 /* LexerNoViableAltException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LexerNoViableAltException.h; path = ../../runtime/LexerNoViableAltException.h; sourceTree = SOURCE_ROOT; }; 27C666891C9584050021E494 /* LexerNoViableAltException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LexerNoViableAltException.h; path = ../../runtime/LexerNoViableAltException.h; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
27C6668A1C9584050021E494 /* ListTokenSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ListTokenSource.cpp; path = ../../runtime/ListTokenSource.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; }; 27C6668A1C9584050021E494 /* ListTokenSource.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ListTokenSource.cpp; path = ../../runtime/ListTokenSource.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
27C6668B1C9584050021E494 /* ListTokenSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ListTokenSource.h; path = ../../runtime/ListTokenSource.h; sourceTree = SOURCE_ROOT; }; 27C6668B1C9584050021E494 /* ListTokenSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ListTokenSource.h; path = ../../runtime/ListTokenSource.h; sourceTree = SOURCE_ROOT; };
27C6668C1C9584050021E494 /* NoViableAltException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = NoViableAltException.cpp; path = ../../runtime/NoViableAltException.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; }; 27C6668C1C9584050021E494 /* NoViableAltException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = NoViableAltException.cpp; path = ../../runtime/NoViableAltException.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
@ -729,7 +729,7 @@
27C668661C9584B60021E494 /* DFA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFA.h; sourceTree = "<group>"; }; 27C668661C9584B60021E494 /* DFA.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFA.h; sourceTree = "<group>"; };
27C668671C9584B60021E494 /* DFASerializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFASerializer.cpp; sourceTree = "<group>"; wrapsLines = 0; }; 27C668671C9584B60021E494 /* DFASerializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFASerializer.cpp; sourceTree = "<group>"; wrapsLines = 0; };
27C668681C9584B60021E494 /* DFASerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFASerializer.h; sourceTree = "<group>"; }; 27C668681C9584B60021E494 /* DFASerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFASerializer.h; sourceTree = "<group>"; };
27C668691C9584B60021E494 /* DFAState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFAState.cpp; sourceTree = "<group>"; }; 27C668691C9584B60021E494 /* DFAState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DFAState.cpp; sourceTree = "<group>"; wrapsLines = 0; };
27C6686A1C9584B60021E494 /* DFAState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFAState.h; sourceTree = "<group>"; }; 27C6686A1C9584B60021E494 /* DFAState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DFAState.h; sourceTree = "<group>"; };
27C6686B1C9584B60021E494 /* LexerDFASerializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LexerDFASerializer.cpp; sourceTree = "<group>"; }; 27C6686B1C9584B60021E494 /* LexerDFASerializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LexerDFASerializer.cpp; sourceTree = "<group>"; };
27C6686C1C9584B60021E494 /* LexerDFASerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerDFASerializer.h; sourceTree = "<group>"; }; 27C6686C1C9584B60021E494 /* LexerDFASerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerDFASerializer.h; sourceTree = "<group>"; };

View File

@ -108,7 +108,7 @@ namespace runtime {
/// <param name="configs"> the ATN configuration set where the ambiguity was /// <param name="configs"> the ATN configuration set where the ambiguity was
/// determined </param> /// determined </param>
virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact, virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact,
antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) = 0; antlrcpp::BitSet *ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) = 0;
/// <summary> /// <summary>
/// This method is called when an SLL conflict occurs and the parser is about /// This method is called when an SLL conflict occurs and the parser is about
@ -131,7 +131,7 @@ namespace runtime {
/// <param name="configs"> the ATN configuration set where the SLL conflict was /// <param name="configs"> the ATN configuration set where the SLL conflict was
/// detected </param> /// detected </param>
virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) = 0; antlrcpp::BitSet *conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) = 0;
/// <summary> /// <summary>
/// This method is called by the parser when a full-context prediction has a /// This method is called by the parser when a full-context prediction has a
@ -161,7 +161,7 @@ namespace runtime {
/// <param name="configs"> the ATN configuration set where the unambiguous prediction /// <param name="configs"> the ATN configuration set where the unambiguous prediction
/// was determined </param> /// was determined </param>
virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
int prediction, atn::ATNConfigSet *configs) = 0; int prediction, std::shared_ptr<atn::ATNConfigSet> configs) = 0;
}; };
} // namespace runtime } // namespace runtime

View File

@ -39,13 +39,13 @@ void BaseErrorListener::syntaxError(IRecognizer *recognizer, Token *offendingSym
} }
void BaseErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, void BaseErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
bool exact, antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) { bool exact, antlrcpp::BitSet *ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) {
} }
void BaseErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, void BaseErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex,
size_t stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) { size_t stopIndex, antlrcpp::BitSet *conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) {
} }
void BaseErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, void BaseErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
int prediction, atn::ATNConfigSet *configs) { int prediction, std::shared_ptr<atn::ATNConfigSet> configs) {
} }

View File

@ -48,13 +48,13 @@ namespace runtime {
const std::wstring &msg, std::exception_ptr e) override; const std::wstring &msg, std::exception_ptr e) override;
virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact, virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact,
antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) override; antlrcpp::BitSet *ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) override;
virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) override; antlrcpp::BitSet *conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) override;
virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
int prediction, atn::ATNConfigSet *configs) override; int prediction, std::shared_ptr<atn::ATNConfigSet> configs) override;
}; };
} // namespace runtime } // namespace runtime

View File

@ -47,7 +47,7 @@ DiagnosticErrorListener::DiagnosticErrorListener(bool exactOnly) : exactOnly(exa
} }
void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
bool exact, antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) { bool exact, antlrcpp::BitSet *ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) {
if (exactOnly && !exact) { if (exactOnly && !exact) {
return; return;
} }
@ -61,7 +61,7 @@ void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa,
} }
void DiagnosticErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, void DiagnosticErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex,
size_t stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) { size_t stopIndex, antlrcpp::BitSet *conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) {
std::wstring decision = getDecisionDescription(recognizer, dfa); std::wstring decision = getDecisionDescription(recognizer, dfa);
std::wstring text = recognizer->getTokenStream()->getText(misc::Interval((int)startIndex, (int)stopIndex)); std::wstring text = recognizer->getTokenStream()->getText(misc::Interval((int)startIndex, (int)stopIndex));
std::wstring message = L"reportAttemptingFullContext d = " + decision + L", input = '" + text + L"'"; std::wstring message = L"reportAttemptingFullContext d = " + decision + L", input = '" + text + L"'";
@ -69,7 +69,7 @@ void DiagnosticErrorListener::reportAttemptingFullContext(Parser *recognizer, df
} }
void DiagnosticErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, void DiagnosticErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex,
size_t stopIndex, int prediction, atn::ATNConfigSet *configs) { size_t stopIndex, int prediction, std::shared_ptr<atn::ATNConfigSet> configs) {
std::wstring decision = getDecisionDescription(recognizer, dfa); std::wstring decision = getDecisionDescription(recognizer, dfa);
std::wstring text = recognizer->getTokenStream()->getText(misc::Interval((int)startIndex, (int)stopIndex)); std::wstring text = recognizer->getTokenStream()->getText(misc::Interval((int)startIndex, (int)stopIndex));
std::wstring message = L"reportContextSensitivity d = " + decision + L", input = '" + text + L"'"; std::wstring message = L"reportContextSensitivity d = " + decision + L", input = '" + text + L"'";
@ -93,7 +93,8 @@ std::wstring DiagnosticErrorListener::getDecisionDescription(Parser *recognizer,
return std::to_wstring(decision) + L"(" + ruleName + L")"; return std::to_wstring(decision) + L"(" + ruleName + L")";
} }
antlrcpp::BitSet *DiagnosticErrorListener::getConflictingAlts(antlrcpp::BitSet *reportedAlts, atn::ATNConfigSet *configs) { antlrcpp::BitSet *DiagnosticErrorListener::getConflictingAlts(antlrcpp::BitSet *reportedAlts,
std::shared_ptr<atn::ATNConfigSet> configs) {
if (reportedAlts != nullptr) { if (reportedAlts != nullptr) {
return reportedAlts; return reportedAlts;
} }

View File

@ -82,13 +82,13 @@ namespace runtime {
DiagnosticErrorListener(bool exactOnly); DiagnosticErrorListener(bool exactOnly);
virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact, virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact,
antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) override; antlrcpp::BitSet *ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) override;
virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) override; antlrcpp::BitSet *conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) override;
virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
int prediction, atn::ATNConfigSet *configs) override; int prediction, std::shared_ptr<atn::ATNConfigSet> configs) override;
protected: protected:
virtual std::wstring getDecisionDescription(Parser *recognizer, dfa::DFA *dfa); virtual std::wstring getDecisionDescription(Parser *recognizer, dfa::DFA *dfa);
@ -103,7 +103,7 @@ namespace runtime {
/// <param name="configs"> The conflicting or ambiguous configuration set. </param> /// <param name="configs"> The conflicting or ambiguous configuration set. </param>
/// <returns> Returns {@code reportedAlts} if it is not {@code null}, otherwise /// <returns> Returns {@code reportedAlts} if it is not {@code null}, otherwise
/// returns the set of alternatives represented in {@code configs}. </returns> /// returns the set of alternatives represented in {@code configs}. </returns>
virtual antlrcpp::BitSet *getConflictingAlts(antlrcpp::BitSet *reportedAlts, atn::ATNConfigSet *configs); virtual antlrcpp::BitSet *getConflictingAlts(antlrcpp::BitSet *reportedAlts, std::shared_ptr<atn::ATNConfigSet> configs);
}; };
} // namespace runtime } // namespace runtime

View File

@ -38,7 +38,8 @@
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
LexerNoViableAltException::LexerNoViableAltException(Lexer *lexer, CharStream *input, size_t startIndex, atn::ATNConfigSet *deadEndConfigs) LexerNoViableAltException::LexerNoViableAltException(Lexer *lexer, CharStream *input, size_t startIndex,
std::shared_ptr<atn::ATNConfigSet> deadEndConfigs)
: RecognitionException(lexer, input, nullptr, nullptr), _startIndex(startIndex), _deadEndConfigs(deadEndConfigs) { : RecognitionException(lexer, input, nullptr, nullptr), _startIndex(startIndex), _deadEndConfigs(deadEndConfigs) {
} }
@ -46,7 +47,7 @@ size_t LexerNoViableAltException::getStartIndex() {
return _startIndex; return _startIndex;
} }
atn::ATNConfigSet* LexerNoViableAltException::getDeadEndConfigs() { std::shared_ptr<atn::ATNConfigSet> LexerNoViableAltException::getDeadEndConfigs() {
return _deadEndConfigs; return _deadEndConfigs;
} }

View File

@ -41,10 +41,11 @@ namespace runtime {
class LexerNoViableAltException : public RecognitionException { class LexerNoViableAltException : public RecognitionException {
public: public:
LexerNoViableAltException(Lexer *lexer, CharStream *input, size_t startIndex, atn::ATNConfigSet *deadEndConfigs); LexerNoViableAltException(Lexer *lexer, CharStream *input, size_t startIndex,
std::shared_ptr<atn::ATNConfigSet> deadEndConfigs);
virtual size_t getStartIndex(); virtual size_t getStartIndex();
virtual atn::ATNConfigSet* getDeadEndConfigs(); virtual std::shared_ptr<atn::ATNConfigSet> getDeadEndConfigs();
virtual std::wstring toString(); virtual std::wstring toString();
private: private:
@ -52,7 +53,7 @@ namespace runtime {
const size_t _startIndex; const size_t _startIndex;
/// Which configurations did we try at input.index() that couldn't match input.LA(1)? /// Which configurations did we try at input.index() that couldn't match input.LA(1)?
atn::ATNConfigSet* _deadEndConfigs; std::shared_ptr<atn::ATNConfigSet> _deadEndConfigs;
}; };

View File

@ -36,12 +36,12 @@
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
NoViableAltException::NoViableAltException(Parser *recognizer) NoViableAltException::NoViableAltException(Parser *recognizer)
: NoViableAltException(recognizer, recognizer->getInputStream(), recognizer->getCurrentToken(), recognizer->getCurrentToken(), : NoViableAltException(recognizer, recognizer->getInputStream(), recognizer->getCurrentToken(),
nullptr, recognizer->ctx) { recognizer->getCurrentToken(), nullptr, recognizer->ctx) {
} }
NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input, Token *startToken, NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input, Token *startToken,
Token *offendingToken, atn::ATNConfigSet *deadEndConfigs, ParserRuleContext *ctx) Token *offendingToken, std::shared_ptr<atn::ATNConfigSet> deadEndConfigs, ParserRuleContext *ctx)
: RecognitionException(recognizer, input, ctx, offendingToken), _deadEndConfigs(deadEndConfigs), _startToken(startToken) { : RecognitionException(recognizer, input, ctx, offendingToken), _deadEndConfigs(deadEndConfigs), _startToken(startToken) {
} }
@ -49,6 +49,6 @@ Token* NoViableAltException::getStartToken() const {
return _startToken; return _startToken;
} }
atn::ATNConfigSet* NoViableAltException::getDeadEndConfigs() const { std::shared_ptr<atn::ATNConfigSet> NoViableAltException::getDeadEndConfigs() const {
return _deadEndConfigs; return _deadEndConfigs;
} }

View File

@ -48,14 +48,14 @@ namespace runtime {
public: public:
NoViableAltException(Parser *recognizer); // LL(1) error NoViableAltException(Parser *recognizer); // LL(1) error
NoViableAltException(Parser *recognizer, TokenStream *input, Token *startToken, Token *offendingToken, NoViableAltException(Parser *recognizer, TokenStream *input, Token *startToken, Token *offendingToken,
atn::ATNConfigSet *deadEndConfigs, ParserRuleContext *ctx); std::shared_ptr<atn::ATNConfigSet> deadEndConfigs, ParserRuleContext *ctx);
virtual Token* getStartToken() const; virtual Token* getStartToken() const;
virtual atn::ATNConfigSet* getDeadEndConfigs() const; virtual std::shared_ptr<atn::ATNConfigSet> getDeadEndConfigs() const;
private: private:
/// Which configurations did we try at input.index() that couldn't match input.LT(1)? /// Which configurations did we try at input.index() that couldn't match input.LT(1)?
atn::ATNConfigSet* _deadEndConfigs; std::shared_ptr<atn::ATNConfigSet> _deadEndConfigs;
/// The token object at the start index; the input stream might /// The token object at the start index; the input stream might
/// not be buffering tokens so get a reference to it. (At the /// not be buffering tokens so get a reference to it. (At the

View File

@ -34,21 +34,21 @@
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
void ProxyErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, void ProxyErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
bool exact, antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) { bool exact, antlrcpp::BitSet *ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) {
for (auto listener : *delegates) { for (auto listener : *delegates) {
listener->reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs); listener->reportAmbiguity(recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
} }
} }
void ProxyErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, void ProxyErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex,
size_t stopIndex, antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) { size_t stopIndex, antlrcpp::BitSet *conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) {
for (auto listener : *delegates) { for (auto listener : *delegates) {
listener->reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs); listener->reportAttemptingFullContext(recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs);
} }
} }
void ProxyErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, void ProxyErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
int prediction, atn::ATNConfigSet *configs) { int prediction, std::shared_ptr<atn::ATNConfigSet> configs) {
for (auto listener : *delegates) { for (auto listener : *delegates) {
listener->reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs); listener->reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs);
} }

View File

@ -62,13 +62,13 @@ namespace runtime {
} }
virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact, virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact,
antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) override; antlrcpp::BitSet *ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) override;
virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, virtual void reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
antlrcpp::BitSet *conflictingAlts, atn::ATNConfigSet *configs) override; antlrcpp::BitSet *conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) override;
virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, virtual void reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
int prediction, atn::ATNConfigSet *configs) override; int prediction, std::shared_ptr<atn::ATNConfigSet> configs) override;
}; };
} // namespace runtime } // namespace runtime

View File

@ -62,7 +62,7 @@ ATNConfigSet::ATNConfigSet(bool fullCtx, std::shared_ptr<ConfigLookup> lookup) :
InitializeInstanceFields(); InitializeInstanceFields();
} }
ATNConfigSet::ATNConfigSet(ATNConfigSet *old) : ATNConfigSet(old->fullCtx, std::shared_ptr<ConfigLookup>()) { ATNConfigSet::ATNConfigSet(std::shared_ptr<ATNConfigSet> old) : ATNConfigSet(old->fullCtx, std::shared_ptr<ConfigLookup>()) {
addAll(old); addAll(old);
uniqueAlt = old->uniqueAlt; uniqueAlt = old->uniqueAlt;
conflictingAlts = old->conflictingAlts; conflictingAlts = old->conflictingAlts;
@ -106,7 +106,7 @@ bool ATNConfigSet::add(ATNConfig *config, PredictionContextMergeCache *mergeCach
return true; return true;
} }
bool ATNConfigSet::addAll(ATNConfigSet *other) { bool ATNConfigSet::addAll(std::shared_ptr<ATNConfigSet> other) {
for (auto &c : other->configs) { for (auto &c : other->configs) {
add(c); add(c);
} }
@ -152,19 +152,19 @@ void ATNConfigSet::optimizeConfigs(ATNSimulator *interpreter) {
} }
} }
bool ATNConfigSet::equals(ATNConfigSet *other) { bool ATNConfigSet::operator == (const ATNConfigSet &other) {
if (other == this) { if (&other == this) {
return true; return true;
} }
bool configEquals = true; bool configEquals = true;
if (configs.size() == other->configs.size()) { if (configs.size() == other.configs.size()) {
for (size_t i = 0; i < configs.size(); i++) { for (size_t i = 0; i < configs.size(); i++) {
if (other->configs.size() < i) { if (other.configs.size() < i) {
configEquals = false; configEquals = false;
break; break;
} }
if (configs[i] != other->configs[i]) { if (configs[i] != other.configs[i]) {
configEquals = false; configEquals = false;
break; break;
} }
@ -173,9 +173,9 @@ bool ATNConfigSet::equals(ATNConfigSet *other) {
configEquals = false; configEquals = false;
} }
bool same = configs.size() > 0 && configEquals && this->fullCtx == other->fullCtx && this->uniqueAlt == other->uniqueAlt && bool same = configs.size() > 0 && configEquals && fullCtx == other.fullCtx && uniqueAlt == other.uniqueAlt &&
this->conflictingAlts == other->conflictingAlts && this->hasSemanticContext == other->hasSemanticContext && conflictingAlts == other.conflictingAlts && hasSemanticContext == other.hasSemanticContext &&
this->dipsIntoOuterContext == other->dipsIntoOuterContext; // includes stack context dipsIntoOuterContext == other.dipsIntoOuterContext; // includes stack context
return same; return same;
} }

View File

@ -84,7 +84,7 @@ namespace atn {
const bool fullCtx; const bool fullCtx;
ATNConfigSet(bool fullCtx, std::shared_ptr<ConfigLookup> lookup); ATNConfigSet(bool fullCtx, std::shared_ptr<ConfigLookup> lookup);
ATNConfigSet(ATNConfigSet *old); ATNConfigSet(std::shared_ptr<ATNConfigSet> old);
virtual ~ATNConfigSet(); virtual ~ATNConfigSet();
@ -114,9 +114,9 @@ namespace atn {
virtual void optimizeConfigs(ATNSimulator *interpreter); virtual void optimizeConfigs(ATNSimulator *interpreter);
bool addAll(ATNConfigSet *other); bool addAll(std::shared_ptr<ATNConfigSet> other);
virtual bool equals(ATNConfigSet *other); bool operator == (const ATNConfigSet &other);
virtual size_t hashCode(); virtual size_t hashCode();
virtual size_t size(); virtual size_t size();
virtual bool isEmpty(); virtual bool isEmpty();

View File

@ -40,7 +40,7 @@
using namespace org::antlr::v4::runtime::dfa; using namespace org::antlr::v4::runtime::dfa;
using namespace org::antlr::v4::runtime::atn; using namespace org::antlr::v4::runtime::atn;
DFAState ATNSimulator::ERROR(INT32_MAX); const std::shared_ptr<DFAState> ATNSimulator::ERROR = std::make_shared<DFAState>(INT32_MAX);
ATNSimulator::ATNSimulator() { ATNSimulator::ATNSimulator() {
} }

View File

@ -48,7 +48,7 @@ namespace atn {
ATNSimulator(); ATNSimulator();
/// Must distinguish between missing edge and edge we know leads nowhere. /// Must distinguish between missing edge and edge we know leads nowhere.
static dfa::DFAState ERROR; static const std::shared_ptr<dfa::DFAState> ERROR;
ATN atn; ATN atn;
ATNSimulator(const ATN &atn, std::shared_ptr<PredictionContextCache> sharedContextCache); ATNSimulator(const ATN &atn, std::shared_ptr<PredictionContextCache> sharedContextCache);

View File

@ -125,7 +125,7 @@ int LexerATNSimulator::matchATN(CharStream *input) {
size_t old_mode = _mode; size_t old_mode = _mode;
ATNConfigSet *s0_closure = computeStartState(input, startState); std::shared_ptr<ATNConfigSet> s0_closure = computeStartState(input, startState);
bool suppressEdge = s0_closure->hasSemanticContext; bool suppressEdge = s0_closure->hasSemanticContext;
s0_closure->hasSemanticContext = false; s0_closure->hasSemanticContext = false;
@ -179,7 +179,7 @@ int LexerATNSimulator::execATN(CharStream *input, dfa::DFAState *ds0) {
target = computeTargetState(input, s, t); target = computeTargetState(input, s, t);
} }
if (target == &ERROR) { if (target == ERROR.get()) {
break; break;
} }
@ -215,7 +215,7 @@ dfa::DFAState *LexerATNSimulator::getExistingTargetState(dfa::DFAState *s, ssize
} }
dfa::DFAState *LexerATNSimulator::computeTargetState(CharStream *input, dfa::DFAState *s, ssize_t t) { dfa::DFAState *LexerATNSimulator::computeTargetState(CharStream *input, dfa::DFAState *s, ssize_t t) {
OrderedATNConfigSet *reach = new OrderedATNConfigSet(); std::shared_ptr<OrderedATNConfigSet> reach = std::make_shared<OrderedATNConfigSet>();
// if we don't find an existing DFA state // if we don't find an existing DFA state
// Fill reach starting from closure, following t transitions // Fill reach starting from closure, following t transitions
@ -224,16 +224,16 @@ dfa::DFAState *LexerATNSimulator::computeTargetState(CharStream *input, dfa::DFA
if (reach->isEmpty()) { // we got nowhere on t from s if (reach->isEmpty()) { // we got nowhere on t from s
// we got nowhere on t, don't throw out this knowledge; it'd // we got nowhere on t, don't throw out this knowledge; it'd
// cause a failover from DFA later. // cause a failover from DFA later.
addDFAEdge(s, t, &ERROR); addDFAEdge(s, t, ERROR.get());
// stop when we can't match any more char // stop when we can't match any more char
return &ERROR; return ERROR.get();
} }
// Add an edge from s to target DFA found/created for reach // Add an edge from s to target DFA found/created for reach
return addDFAEdge(s, t, reach); return addDFAEdge(s, t, reach);
} }
int LexerATNSimulator::failOrAccept(SimState *prevAccept, CharStream *input, ATNConfigSet *reach, ssize_t t) { int LexerATNSimulator::failOrAccept(SimState *prevAccept, CharStream *input, std::shared_ptr<ATNConfigSet> reach, ssize_t t) {
if (prevAccept->dfaState != nullptr) { if (prevAccept->dfaState != nullptr) {
int ruleIndex = prevAccept->dfaState->lexerRuleIndex; int ruleIndex = prevAccept->dfaState->lexerRuleIndex;
int actionIndex = prevAccept->dfaState->lexerActionIndex; int actionIndex = prevAccept->dfaState->lexerActionIndex;
@ -249,7 +249,8 @@ int LexerATNSimulator::failOrAccept(SimState *prevAccept, CharStream *input, ATN
} }
} }
void LexerATNSimulator::getReachableConfigSet(CharStream *input, ATNConfigSet *closure, ATNConfigSet *reach, ssize_t t) { void LexerATNSimulator::getReachableConfigSet(CharStream *input, std::shared_ptr<ATNConfigSet> closure,
std::shared_ptr<ATNConfigSet> reach, ssize_t t) {
// this is used to skip processing for configs which have a lower priority // this is used to skip processing for configs which have a lower priority
// than a config that already reached an accept state for the same rule // than a config that already reached an accept state for the same rule
int skipAlt = ATN::INVALID_ALT_NUMBER; int skipAlt = ATN::INVALID_ALT_NUMBER;
@ -313,9 +314,9 @@ atn::ATNState *LexerATNSimulator::getReachableTarget(Transition *trans, ssize_t
return nullptr; return nullptr;
} }
atn::ATNConfigSet *LexerATNSimulator::computeStartState(CharStream *input, ATNState *p) { std::shared_ptr<ATNConfigSet> LexerATNSimulator::computeStartState(CharStream *input, ATNState *p) {
std::shared_ptr<EmptyPredictionContext> initialContext = PredictionContext::EMPTY; // ml: the purpose of this assignment is unclear std::shared_ptr<EmptyPredictionContext> initialContext = PredictionContext::EMPTY; // ml: the purpose of this assignment is unclear
ATNConfigSet *configs = new OrderedATNConfigSet(); std::shared_ptr<ATNConfigSet> configs = std::make_shared<OrderedATNConfigSet>();
for (size_t i = 0; i < p->getNumberOfTransitions(); i++) { for (size_t i = 0; i < p->getNumberOfTransitions(); i++) {
ATNState *target = p->transition(i)->target; ATNState *target = p->transition(i)->target;
LexerATNConfig *c = new LexerATNConfig(target, (int)(i + 1), initialContext); LexerATNConfig *c = new LexerATNConfig(target, (int)(i + 1), initialContext);
@ -324,7 +325,7 @@ atn::ATNConfigSet *LexerATNSimulator::computeStartState(CharStream *input, ATNSt
return configs; return configs;
} }
bool LexerATNSimulator::closure(CharStream *input, LexerATNConfig *config, ATNConfigSet *configs, bool LexerATNSimulator::closure(CharStream *input, LexerATNConfig *config, std::shared_ptr<ATNConfigSet> configs,
bool currentAltReachedAcceptState, bool speculative) { bool currentAltReachedAcceptState, bool speculative) {
if (debug) { if (debug) {
std::wcout << L"closure(" << config->toString(true) << L")" << std::endl; std::wcout << L"closure(" << config->toString(true) << L")" << std::endl;
@ -382,7 +383,8 @@ bool LexerATNSimulator::closure(CharStream *input, LexerATNConfig *config, ATNCo
return currentAltReachedAcceptState; return currentAltReachedAcceptState;
} }
atn::LexerATNConfig *LexerATNSimulator::getEpsilonTarget(CharStream *input, LexerATNConfig *config, Transition *t, ATNConfigSet *configs, bool speculative) { atn::LexerATNConfig *LexerATNSimulator::getEpsilonTarget(CharStream *input, LexerATNConfig *config, Transition *t,
std::shared_ptr<ATNConfigSet> configs, bool speculative) {
LexerATNConfig *c = nullptr; LexerATNConfig *c = nullptr;
switch (t->getSerializationType()) { switch (t->getSerializationType()) {
case Transition::RULE: { case Transition::RULE: {
@ -469,7 +471,7 @@ void LexerATNSimulator::captureSimState(SimState *settings, CharStream *input, d
settings->dfaState = dfaState; settings->dfaState = dfaState;
} }
dfa::DFAState *LexerATNSimulator::addDFAEdge(dfa::DFAState *from, ssize_t t, ATNConfigSet *q) { dfa::DFAState *LexerATNSimulator::addDFAEdge(dfa::DFAState *from, ssize_t t, std::shared_ptr<ATNConfigSet> q) {
/* leading to this call, ATNConfigSet.hasSemanticContext is used as a /* leading to this call, ATNConfigSet.hasSemanticContext is used as a
* marker indicating dynamic predicate evaluation makes this edge * marker indicating dynamic predicate evaluation makes this edge
* dependent on the specific input sequence, so the static edge in the * dependent on the specific input sequence, so the static edge in the
@ -512,7 +514,7 @@ void LexerATNSimulator::addDFAEdge(dfa::DFAState *p, ssize_t t, dfa::DFAState *q
p->edges[(size_t)(t - MIN_DFA_EDGE)] = q; // connect p->edges[(size_t)(t - MIN_DFA_EDGE)] = q; // connect
} }
dfa::DFAState *LexerATNSimulator::addDFAState(ATNConfigSet *configs) { dfa::DFAState *LexerATNSimulator::addDFAState(std::shared_ptr<ATNConfigSet> configs) {
/* the lexer evaluates predicates on-the-fly; by this point configs /* the lexer evaluates predicates on-the-fly; by this point configs
* should not contain any configurations with unevaluated predicates. * should not contain any configurations with unevaluated predicates.
*/ */

View File

@ -155,20 +155,20 @@ namespace atn {
/// returns <seealso cref="#ERROR"/>. </returns> /// returns <seealso cref="#ERROR"/>. </returns>
virtual dfa::DFAState *computeTargetState(CharStream *input, dfa::DFAState *s, ssize_t t); virtual dfa::DFAState *computeTargetState(CharStream *input, dfa::DFAState *s, ssize_t t);
virtual int failOrAccept(SimState *prevAccept, CharStream *input, ATNConfigSet *reach, ssize_t t); virtual int failOrAccept(SimState *prevAccept, CharStream *input, std::shared_ptr<ATNConfigSet> reach, ssize_t t);
/// <summary> /// <summary>
/// Given a starting configuration set, figure out all ATN configurations /// Given a starting configuration set, figure out all ATN configurations
/// we can reach upon input {@code t}. Parameter {@code reach} is a return /// we can reach upon input {@code t}. Parameter {@code reach} is a return
/// parameter. /// parameter.
/// </summary> /// </summary>
void getReachableConfigSet(CharStream *input, ATNConfigSet *closure, ATNConfigSet *reach, ssize_t t); void getReachableConfigSet(CharStream *input, std::shared_ptr<ATNConfigSet> closure, std::shared_ptr<ATNConfigSet> reach, ssize_t t);
virtual void accept(CharStream *input, int ruleIndex, int actionIndex, size_t index, size_t line, size_t charPos); virtual void accept(CharStream *input, int ruleIndex, int actionIndex, size_t index, size_t line, size_t charPos);
virtual ATNState *getReachableTarget(Transition *trans, ssize_t t); virtual ATNState *getReachableTarget(Transition *trans, ssize_t t);
virtual atn::ATNConfigSet *computeStartState(CharStream *input, ATNState *p); virtual std::shared_ptr<ATNConfigSet> computeStartState(CharStream *input, ATNState *p);
/// <summary> /// <summary>
/// Since the alternatives within any lexer decision are ordered by /// Since the alternatives within any lexer decision are ordered by
@ -179,10 +179,12 @@ namespace atn {
/// </summary> /// </summary>
/// <returns> {@code true} if an accept state is reached, otherwise /// <returns> {@code true} if an accept state is reached, otherwise
/// {@code false}. </returns> /// {@code false}. </returns>
virtual bool closure(CharStream *input, LexerATNConfig *config, ATNConfigSet *configs, bool currentAltReachedAcceptState, bool speculative); virtual bool closure(CharStream *input, LexerATNConfig *config, std::shared_ptr<ATNConfigSet> configs,
bool currentAltReachedAcceptState, bool speculative);
// side-effect: can alter configs.hasSemanticContext // side-effect: can alter configs.hasSemanticContext
virtual LexerATNConfig *getEpsilonTarget(CharStream *input, LexerATNConfig *config, Transition *t, ATNConfigSet *configs, bool speculative); virtual LexerATNConfig *getEpsilonTarget(CharStream *input, LexerATNConfig *config, Transition *t,
std::shared_ptr<ATNConfigSet> configs, bool speculative);
/// <summary> /// <summary>
/// Evaluate a predicate specified in the lexer. /// Evaluate a predicate specified in the lexer.
@ -207,7 +209,7 @@ namespace atn {
virtual bool evaluatePredicate(CharStream *input, int ruleIndex, int predIndex, bool speculative); virtual bool evaluatePredicate(CharStream *input, int ruleIndex, int predIndex, bool speculative);
virtual void captureSimState(SimState *settings, CharStream *input, dfa::DFAState *dfaState); virtual void captureSimState(SimState *settings, CharStream *input, dfa::DFAState *dfaState);
virtual dfa::DFAState *addDFAEdge(dfa::DFAState *from, ssize_t t, ATNConfigSet *q); virtual dfa::DFAState *addDFAEdge(dfa::DFAState *from, ssize_t t, std::shared_ptr<ATNConfigSet> q);
virtual void addDFAEdge(dfa::DFAState *p, ssize_t t, dfa::DFAState *q); virtual void addDFAEdge(dfa::DFAState *p, ssize_t t, dfa::DFAState *q);
/// <summary> /// <summary>
@ -216,7 +218,7 @@ namespace atn {
/// configuration containing an ATN rule stop state. Later, when /// configuration containing an ATN rule stop state. Later, when
/// traversing the DFA, we will know which rule to accept. /// traversing the DFA, we will know which rule to accept.
/// </summary> /// </summary>
virtual dfa::DFAState *addDFAState(ATNConfigSet *configs); virtual dfa::DFAState *addDFAState(std::shared_ptr<ATNConfigSet> configs);
public: public:
dfa::DFA *getDFA(size_t mode); dfa::DFA *getDFA(size_t mode);

View File

@ -99,7 +99,7 @@ int ParserATNSimulator::adaptivePredict(TokenStream *input, int decision, Parser
std::wcout << L"predictATN decision " << dfa->decision << L" exec LA(1)==" << getLookaheadName(input) << L", outerContext=" << outerContext->toString(parser) << std::endl; std::wcout << L"predictATN decision " << dfa->decision << L" exec LA(1)==" << getLookaheadName(input) << L", outerContext=" << outerContext->toString(parser) << std::endl;
} }
bool fullCtx = false; bool fullCtx = false;
ATNConfigSet *s0_closure = computeStartState(dynamic_cast<ATNState*>(dfa->atnStartState), std::shared_ptr<ATNConfigSet> s0_closure = computeStartState(dynamic_cast<ATNState*>(dfa->atnStartState),
ParserRuleContext::EMPTY, fullCtx); ParserRuleContext::EMPTY, fullCtx);
dfa->s0 = addDFAState(dfa, new dfa::DFAState(s0_closure)); dfa->s0 = addDFAState(dfa, new dfa::DFAState(s0_closure));
} }
@ -142,7 +142,7 @@ int ParserATNSimulator::execATN(dfa::DFA *dfa, dfa::DFAState *s0, TokenStream *i
D = computeTargetState(dfa, previousD, t); D = computeTargetState(dfa, previousD, t);
} }
if (D == &ERROR) { if (D == ERROR.get()) {
// if any configs in previous dipped into outer context, that // if any configs in previous dipped into outer context, that
// means that input up to t actually finished entry rule // means that input up to t actually finished entry rule
// at least for SLL decision. Full LL doesn't dip into outer // at least for SLL decision. Full LL doesn't dip into outer
@ -192,7 +192,7 @@ int ParserATNSimulator::execATN(dfa::DFA *dfa, dfa::DFAState *s0, TokenStream *i
std::wcout << L"ctx sensitive state " << outerContext << L" in " << D << std::endl; std::wcout << L"ctx sensitive state " << outerContext << L" in " << D << std::endl;
} }
bool fullCtx = true; bool fullCtx = true;
ATNConfigSet *s0_closure = computeStartState(dfa->atnStartState, outerContext, fullCtx); std::shared_ptr<ATNConfigSet> s0_closure = computeStartState(dfa->atnStartState, outerContext, fullCtx);
reportAttemptingFullContext(dfa, conflictingAlts, D->configs, startIndex, input->index()); reportAttemptingFullContext(dfa, conflictingAlts, D->configs, startIndex, input->index());
int alt = execATNWithFullContext(dfa, D, s0_closure, input, startIndex, outerContext); int alt = execATNWithFullContext(dfa, D, s0_closure, input, startIndex, outerContext);
return alt; return alt;
@ -240,10 +240,10 @@ dfa::DFAState *ParserATNSimulator::getExistingTargetState(dfa::DFAState *previou
} }
dfa::DFAState *ParserATNSimulator::computeTargetState(dfa::DFA *dfa, dfa::DFAState *previousD, ssize_t t) { dfa::DFAState *ParserATNSimulator::computeTargetState(dfa::DFA *dfa, dfa::DFAState *previousD, ssize_t t) {
ATNConfigSet *reach = computeReachSet(previousD->configs, t, false); std::shared_ptr<ATNConfigSet> reach = computeReachSet(previousD->configs, t, false);
if (reach == nullptr) { if (reach == nullptr) {
addDFAEdge(dfa, previousD, t, &ERROR); addDFAEdge(dfa, previousD, t, ERROR.get());
return &ERROR; return ERROR.get();
} }
// create new target state; we'll add to DFA after it's complete // create new target state; we'll add to DFA after it's complete
@ -309,15 +309,15 @@ void ParserATNSimulator::predicateDFAState(dfa::DFAState *dfaState, DecisionStat
} }
} }
int ParserATNSimulator::execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D, ATNConfigSet *s0, TokenStream *input, int ParserATNSimulator::execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D, std::shared_ptr<ATNConfigSet> s0,
size_t startIndex, ParserRuleContext *outerContext) { TokenStream *input, size_t startIndex, ParserRuleContext *outerContext) {
if (debug || debug_list_atn_decisions) { if (debug || debug_list_atn_decisions) {
std::cout << "execATNWithFullContext " << s0 << std::endl; std::cout << "execATNWithFullContext " << s0 << std::endl;
} }
bool fullCtx = true; bool fullCtx = true;
bool foundExactAmbig = false; bool foundExactAmbig = false;
ATNConfigSet *reach = nullptr; std::shared_ptr<ATNConfigSet> reach;
ATNConfigSet *previous = s0; std::shared_ptr<ATNConfigSet> previous = s0;
input->seek(startIndex); input->seek(startIndex);
ssize_t t = input->LA(1); ssize_t t = input->LA(1);
int predictedAlt; int predictedAlt;
@ -422,12 +422,13 @@ int ParserATNSimulator::execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D,
return predictedAlt; return predictedAlt;
} }
atn::ATNConfigSet *ParserATNSimulator::computeReachSet(ATNConfigSet *closure, ssize_t t, bool fullCtx) { std::shared_ptr<ATNConfigSet> ParserATNSimulator::computeReachSet(std::shared_ptr<ATNConfigSet> closure,
ssize_t t, bool fullCtx) {
if (debug) { if (debug) {
std::wcout << L"in computeReachSet, starting closure: " << closure << std::endl; std::wcout << L"in computeReachSet, starting closure: " << closure << std::endl;
} }
ATNConfigSet *intermediate = new ATNConfigSet(fullCtx, std::shared_ptr<ConfigLookup>()); std::shared_ptr<ATNConfigSet> intermediate = std::make_shared<ATNConfigSet>(fullCtx, std::shared_ptr<ConfigLookup>());
/* Configurations already in a rule stop state indicate reaching the end /* Configurations already in a rule stop state indicate reaching the end
* of the decision rule (local context) or end of the start rule (full * of the decision rule (local context) or end of the start rule (full
@ -472,8 +473,7 @@ atn::ATNConfigSet *ParserATNSimulator::computeReachSet(ATNConfigSet *closure, ss
} }
// Now figure out where the reach operation can take us... // Now figure out where the reach operation can take us...
std::shared_ptr<ATNConfigSet> reach;
ATNConfigSet *reach = nullptr;
/* This block optimizes the reach operation for intermediate sets which /* This block optimizes the reach operation for intermediate sets which
* trivially indicate a termination state for the overall * trivially indicate a termination state for the overall
@ -502,7 +502,7 @@ atn::ATNConfigSet *ParserATNSimulator::computeReachSet(ATNConfigSet *closure, ss
* operation on the intermediate set to compute its initial value. * operation on the intermediate set to compute its initial value.
*/ */
if (reach == nullptr) { if (reach == nullptr) {
reach = new ATNConfigSet(fullCtx, std::shared_ptr<ConfigLookup>()); reach = std::make_shared<ATNConfigSet>(fullCtx, std::shared_ptr<ConfigLookup>());
std::set<ATNConfig*> closureBusy; std::set<ATNConfig*> closureBusy;
for (auto c : *intermediate->configLookup) { for (auto c : *intermediate->configLookup) {
@ -553,12 +553,13 @@ atn::ATNConfigSet *ParserATNSimulator::computeReachSet(ATNConfigSet *closure, ss
return reach; return reach;
} }
atn::ATNConfigSet *ParserATNSimulator::removeAllConfigsNotInRuleStopState(ATNConfigSet *configs, bool lookToEndOfRule) { std::shared_ptr<ATNConfigSet> ParserATNSimulator::removeAllConfigsNotInRuleStopState(std::shared_ptr<ATNConfigSet> configs,
bool lookToEndOfRule) {
if (PredictionModeClass::allConfigsInRuleStopStates(configs)) { if (PredictionModeClass::allConfigsInRuleStopStates(configs)) {
return configs; return configs;
} }
ATNConfigSet *result = new ATNConfigSet(configs->fullCtx, std::shared_ptr<ConfigLookup>()); std::shared_ptr<ATNConfigSet> result = std::make_shared<ATNConfigSet>(configs->fullCtx, std::shared_ptr<ConfigLookup>());
for (auto config : *configs->configLookup) { for (auto config : *configs->configLookup) {
if (dynamic_cast<RuleStopState*>(config->state) != nullptr) { if (dynamic_cast<RuleStopState*>(config->state) != nullptr) {
@ -578,10 +579,10 @@ atn::ATNConfigSet *ParserATNSimulator::removeAllConfigsNotInRuleStopState(ATNCon
return result; return result;
} }
atn::ATNConfigSet *ParserATNSimulator::computeStartState(ATNState *p, RuleContext *ctx, bool fullCtx) { std::shared_ptr<ATNConfigSet> ParserATNSimulator::computeStartState(ATNState *p, RuleContext *ctx, bool fullCtx) {
// always at least the implicit call to start rule // always at least the implicit call to start rule
PredictionContextRef initialContext = PredictionContext::fromRuleContext(atn, ctx); PredictionContextRef initialContext = PredictionContext::fromRuleContext(atn, ctx);
ATNConfigSet *configs = new ATNConfigSet(fullCtx, std::shared_ptr<ConfigLookup>()); std::shared_ptr<ATNConfigSet> configs = std::make_shared<ATNConfigSet>(fullCtx, std::shared_ptr<ConfigLookup>());
for (size_t i = 0; i < p->getNumberOfTransitions(); i++) { for (size_t i = 0; i < p->getNumberOfTransitions(); i++) {
ATNState *target = p->transition(i)->target; ATNState *target = p->transition(i)->target;
@ -603,7 +604,7 @@ atn::ATNState *ParserATNSimulator::getReachableTarget(Transition *trans, int tty
// Note that caller must memory manage the returned value from this function // Note that caller must memory manage the returned value from this function
std::vector<SemanticContextRef> ParserATNSimulator::getPredsForAmbigAlts(BitSet *ambigAlts, std::vector<SemanticContextRef> ParserATNSimulator::getPredsForAmbigAlts(BitSet *ambigAlts,
ATNConfigSet *configs, size_t nalts) { std::shared_ptr<ATNConfigSet> configs, size_t nalts) {
// REACH=[1|1|[]|0:0, 1|2|[]|0:1] // REACH=[1|1|[]|0:0, 1|2|[]|0:1]
/* altToPred starts as an array of all null contexts. The entry at index i /* altToPred starts as an array of all null contexts. The entry at index i
* corresponds to alternative i. altToPred[i] may have one of three values: * corresponds to alternative i. altToPred[i] may have one of three values:
@ -670,7 +671,7 @@ std::vector<dfa::DFAState::PredPrediction *> ParserATNSimulator::getPredicatePre
return pairs; return pairs;
} }
int ParserATNSimulator::getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs) { int ParserATNSimulator::getAltThatFinishedDecisionEntryRule(std::shared_ptr<ATNConfigSet> configs) {
misc::IntervalSet *alts = nullptr; misc::IntervalSet *alts = nullptr;
for (auto c : *configs->configLookup) { for (auto c : *configs->configLookup) {
if (c->reachesIntoOuterContext > 0 || (dynamic_cast<RuleStopState*>(c->state) != nullptr && c->context->hasEmptyPath())) { if (c->reachesIntoOuterContext > 0 || (dynamic_cast<RuleStopState*>(c->state) != nullptr && c->context->hasEmptyPath())) {
@ -713,14 +714,16 @@ BitSet *ParserATNSimulator::evalSemanticContext(std::vector<dfa::DFAState::PredP
return predictions; return predictions;
} }
void ParserATNSimulator::closure(ATNConfig *config, ATNConfigSet *configs, std::set<ATNConfig*> &closureBusy, bool collectPredicates, bool fullCtx) { void ParserATNSimulator::closure(ATNConfig *config, std::shared_ptr<ATNConfigSet> configs,
std::set<ATNConfig*> &closureBusy, bool collectPredicates, bool fullCtx) {
const int initialDepth = 0; const int initialDepth = 0;
closureCheckingStopState(config, configs, closureBusy, collectPredicates, fullCtx, initialDepth); closureCheckingStopState(config, configs, closureBusy, collectPredicates, fullCtx, initialDepth);
assert(!fullCtx || !configs->dipsIntoOuterContext); assert(!fullCtx || !configs->dipsIntoOuterContext);
} }
void ParserATNSimulator::closureCheckingStopState(ATNConfig *config, ATNConfigSet *configs, std::set<ATNConfig*> &closureBusy, bool collectPredicates, bool fullCtx, int depth) { void ParserATNSimulator::closureCheckingStopState(ATNConfig *config, std::shared_ptr<ATNConfigSet> configs,
std::set<ATNConfig*> &closureBusy, bool collectPredicates, bool fullCtx, int depth) {
if (debug) { if (debug) {
std::wcout << L"closure(" << config->toString(true) << L")" << std::endl; std::wcout << L"closure(" << config->toString(true) << L")" << std::endl;
} }
@ -770,7 +773,8 @@ void ParserATNSimulator::closureCheckingStopState(ATNConfig *config, ATNConfigSe
closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth); closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth);
} }
void ParserATNSimulator::closure_(ATNConfig *config, ATNConfigSet *configs, std::set<ATNConfig*> &closureBusy, bool collectPredicates, bool fullCtx, int depth) { void ParserATNSimulator::closure_(ATNConfig *config, std::shared_ptr<ATNConfigSet> configs,
std::set<ATNConfig*> &closureBusy, bool collectPredicates, bool fullCtx, int depth) {
ATNState *p = config->state; ATNState *p = config->state;
// optimization // optimization
if (!p->onlyHasEpsilonTransitions()) { if (!p->onlyHasEpsilonTransitions()) {
@ -940,12 +944,12 @@ atn::ATNConfig *ParserATNSimulator::ruleTransition(ATNConfig *config, RuleTransi
return new atn::ATNConfig(config, t->target, newContext); return new atn::ATNConfig(config, t->target, newContext);
} }
BitSet ParserATNSimulator::getConflictingAlts(ATNConfigSet *configs) { BitSet ParserATNSimulator::getConflictingAlts(std::shared_ptr<ATNConfigSet> configs) {
std::vector<BitSet> altsets = PredictionModeClass::getConflictingAltSubsets(configs); std::vector<BitSet> altsets = PredictionModeClass::getConflictingAltSubsets(configs);
return PredictionModeClass::getAlts(altsets); return PredictionModeClass::getAlts(altsets);
} }
BitSet ParserATNSimulator::getConflictingAltsOrUniqueAlt(ATNConfigSet *configs) { BitSet ParserATNSimulator::getConflictingAltsOrUniqueAlt(std::shared_ptr<ATNConfigSet> configs) {
BitSet conflictingAlts; BitSet conflictingAlts;
if (configs->uniqueAlt != ATN::INVALID_ALT_NUMBER) { if (configs->uniqueAlt != ATN::INVALID_ALT_NUMBER) {
conflictingAlts.set((size_t)configs->uniqueAlt); conflictingAlts.set((size_t)configs->uniqueAlt);
@ -997,11 +1001,11 @@ void ParserATNSimulator::dumpDeadEndConfigs(NoViableAltException *nvae) {
} }
NoViableAltException *ParserATNSimulator::noViableAlt(TokenStream *input, ParserRuleContext *outerContext, NoViableAltException *ParserATNSimulator::noViableAlt(TokenStream *input, ParserRuleContext *outerContext,
ATNConfigSet *configs, size_t startIndex) { std::shared_ptr<ATNConfigSet> configs, size_t startIndex) {
return new NoViableAltException(parser, input, input->get(startIndex), input->LT(1), configs, outerContext); return new NoViableAltException(parser, input, input->get(startIndex), input->LT(1), configs, outerContext);
} }
int ParserATNSimulator::getUniqueAlt(ATNConfigSet *configs) { int ParserATNSimulator::getUniqueAlt(std::shared_ptr<ATNConfigSet> configs) {
int alt = ATN::INVALID_ALT_NUMBER; int alt = ATN::INVALID_ALT_NUMBER;
for (auto c : *configs->configLookup) { for (auto c : *configs->configLookup) {
if (alt == ATN::INVALID_ALT_NUMBER) { if (alt == ATN::INVALID_ALT_NUMBER) {
@ -1049,7 +1053,7 @@ dfa::DFAState *ParserATNSimulator::addDFAEdge(dfa::DFA *dfa, dfa::DFAState *from
} }
dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA *dfa, dfa::DFAState *D) { dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA *dfa, dfa::DFAState *D) {
if (D == &ERROR) { if (D == ERROR.get()) {
return D; return D;
} }
@ -1073,8 +1077,8 @@ dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA *dfa, dfa::DFAState *D)
} }
} }
void ParserATNSimulator::reportAttemptingFullContext(dfa::DFA *dfa, BitSet *conflictingAlts, ATNConfigSet *configs, void ParserATNSimulator::reportAttemptingFullContext(dfa::DFA *dfa, BitSet *conflictingAlts,
size_t startIndex, size_t stopIndex) { std::shared_ptr<ATNConfigSet> configs, size_t startIndex, size_t stopIndex) {
if (debug || retry_debug) { if (debug || retry_debug) {
misc::Interval interval = misc::Interval((int)startIndex, (int)stopIndex); misc::Interval interval = misc::Interval((int)startIndex, (int)stopIndex);
std::wcout << L"reportAttemptingFullContext decision=" << dfa->decision << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl; std::wcout << L"reportAttemptingFullContext decision=" << dfa->decision << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl;
@ -1084,8 +1088,8 @@ void ParserATNSimulator::reportAttemptingFullContext(dfa::DFA *dfa, BitSet *conf
} }
} }
void ParserATNSimulator::reportContextSensitivity(dfa::DFA *dfa, int prediction, ATNConfigSet *configs, size_t startIndex, void ParserATNSimulator::reportContextSensitivity(dfa::DFA *dfa, int prediction, std::shared_ptr<ATNConfigSet> configs,
size_t stopIndex) { size_t startIndex, size_t stopIndex) {
if (debug || retry_debug) { if (debug || retry_debug) {
misc::Interval interval = misc::Interval((int)startIndex, (int)stopIndex); misc::Interval interval = misc::Interval((int)startIndex, (int)stopIndex);
std::wcout << L"reportContextSensitivity decision=" << dfa->decision << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl; std::wcout << L"reportContextSensitivity decision=" << dfa->decision << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl;
@ -1096,7 +1100,7 @@ void ParserATNSimulator::reportContextSensitivity(dfa::DFA *dfa, int prediction,
} }
void ParserATNSimulator::reportAmbiguity(dfa::DFA *dfa, dfa::DFAState *D, size_t startIndex, size_t stopIndex, void ParserATNSimulator::reportAmbiguity(dfa::DFA *dfa, dfa::DFAState *D, size_t startIndex, size_t stopIndex,
bool exact, BitSet *ambigAlts, ATNConfigSet *configs) { bool exact, BitSet *ambigAlts, std::shared_ptr<ATNConfigSet> configs) {
if (debug || retry_debug) { if (debug || retry_debug) {
// ParserATNPathFinder finder = new ParserATNPathFinder(parser, atn); // ParserATNPathFinder finder = new ParserATNPathFinder(parser, atn);
// int i = 1; // int i = 1;

View File

@ -353,9 +353,9 @@ namespace atn {
virtual void predicateDFAState(dfa::DFAState *dfaState, DecisionState *decisionState); virtual void predicateDFAState(dfa::DFAState *dfaState, DecisionState *decisionState);
// comes back with reach.uniqueAlt set to a valid alt // comes back with reach.uniqueAlt set to a valid alt
virtual int execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D, ATNConfigSet *s0, TokenStream *input, size_t startIndex, ParserRuleContext *outerContext); // how far we got before failing over virtual int execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D, std::shared_ptr<ATNConfigSet> s0, TokenStream *input, size_t startIndex, ParserRuleContext *outerContext); // how far we got before failing over
virtual ATNConfigSet *computeReachSet(ATNConfigSet *closure, ssize_t t, bool fullCtx); virtual std::shared_ptr<ATNConfigSet> computeReachSet(std::shared_ptr<ATNConfigSet> closure, ssize_t t, bool fullCtx);
/// <summary> /// <summary>
/// Return a configuration set containing only the configurations from /// Return a configuration set containing only the configurations from
@ -376,19 +376,19 @@ namespace atn {
/// <returns> {@code configs} if all configurations in {@code configs} are in a /// <returns> {@code configs} if all configurations in {@code configs} are in a
/// rule stop state, otherwise return a new configuration set containing only /// rule stop state, otherwise return a new configuration set containing only
/// the configurations from {@code configs} which are in a rule stop state </returns> /// the configurations from {@code configs} which are in a rule stop state </returns>
virtual ATNConfigSet *removeAllConfigsNotInRuleStopState(ATNConfigSet *configs, bool lookToEndOfRule); virtual std::shared_ptr<ATNConfigSet> removeAllConfigsNotInRuleStopState(std::shared_ptr<ATNConfigSet> configs, bool lookToEndOfRule);
virtual ATNConfigSet *computeStartState(ATNState *p, RuleContext *ctx, bool fullCtx); virtual std::shared_ptr<ATNConfigSet> computeStartState(ATNState *p, RuleContext *ctx, bool fullCtx);
virtual ATNState *getReachableTarget(Transition *trans, int ttype); virtual ATNState *getReachableTarget(Transition *trans, int ttype);
virtual std::vector<SemanticContextRef> getPredsForAmbigAlts(antlrcpp::BitSet *ambigAlts, virtual std::vector<SemanticContextRef> getPredsForAmbigAlts(antlrcpp::BitSet *ambigAlts,
ATNConfigSet *configs, size_t nalts); std::shared_ptr<ATNConfigSet> configs, size_t nalts);
virtual std::vector<dfa::DFAState::PredPrediction*> getPredicatePredictions(antlrcpp::BitSet *ambigAlts, virtual std::vector<dfa::DFAState::PredPrediction*> getPredicatePredictions(antlrcpp::BitSet *ambigAlts,
std::vector<SemanticContextRef> altToPred); std::vector<SemanticContextRef> altToPred);
virtual int getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs); virtual int getAltThatFinishedDecisionEntryRule(std::shared_ptr<ATNConfigSet> configs);
/// <summary> /// <summary>
/// Look through a list of predicate/alt pairs, returning alts for the /// Look through a list of predicate/alt pairs, returning alts for the
@ -407,14 +407,14 @@ namespace atn {
ambig detection thought :( ambig detection thought :(
*/ */
virtual void closure(ATNConfig *config, ATNConfigSet *configs, std::set<ATNConfig*> &closureBusy, virtual void closure(ATNConfig *config, std::shared_ptr<ATNConfigSet> configs, std::set<ATNConfig*> &closureBusy,
bool collectPredicates, bool fullCtx); bool collectPredicates, bool fullCtx);
virtual void closureCheckingStopState(ATNConfig *config, ATNConfigSet *configs, std::set<ATNConfig*> &closureBusy, virtual void closureCheckingStopState(ATNConfig *config, std::shared_ptr<ATNConfigSet> configs,
bool collectPredicates, bool fullCtx, int depth); std::set<ATNConfig*> &closureBusy, bool collectPredicates, bool fullCtx, int depth);
/// Do the actual work of walking epsilon edges. /// Do the actual work of walking epsilon edges.
virtual void closure_(ATNConfig *config, ATNConfigSet *configs, std::set<ATNConfig*> &closureBusy, virtual void closure_(ATNConfig *config, std::shared_ptr<ATNConfigSet> configs, std::set<ATNConfig*> &closureBusy,
bool collectPredicates, bool fullCtx, int depth); bool collectPredicates, bool fullCtx, int depth);
public: public:
@ -432,7 +432,7 @@ namespace atn {
virtual ATNConfig *ruleTransition(ATNConfig *config, RuleTransition *t); virtual ATNConfig *ruleTransition(ATNConfig *config, RuleTransition *t);
virtual antlrcpp::BitSet getConflictingAlts(ATNConfigSet *configs); virtual antlrcpp::BitSet getConflictingAlts(std::shared_ptr<ATNConfigSet> configs);
/// <summary> /// <summary>
/// Sam pointed out a problem with the previous definition, v3, of /// Sam pointed out a problem with the previous definition, v3, of
@ -471,7 +471,7 @@ namespace atn {
/// that we still need to pursue. /// that we still need to pursue.
/// </summary> /// </summary>
virtual antlrcpp::BitSet getConflictingAltsOrUniqueAlt(ATNConfigSet *configs); virtual antlrcpp::BitSet getConflictingAltsOrUniqueAlt(std::shared_ptr<ATNConfigSet> configs);
public: public:
virtual std::wstring getTokenName(ssize_t t); virtual std::wstring getTokenName(ssize_t t);
@ -486,9 +486,10 @@ namespace atn {
virtual void dumpDeadEndConfigs(NoViableAltException *nvae); virtual void dumpDeadEndConfigs(NoViableAltException *nvae);
protected: protected:
virtual NoViableAltException *noViableAlt(TokenStream *input, ParserRuleContext *outerContext, ATNConfigSet *configs, size_t startIndex); virtual NoViableAltException *noViableAlt(TokenStream *input, ParserRuleContext *outerContext,
std::shared_ptr<ATNConfigSet> configs, size_t startIndex);
static int getUniqueAlt(ATNConfigSet *configs); static int getUniqueAlt(std::shared_ptr<ATNConfigSet> configs);
/// <summary> /// <summary>
/// Add an edge to the DFA, if possible. This method calls /// Add an edge to the DFA, if possible. This method calls
@ -527,14 +528,16 @@ namespace atn {
/// state was not already present. </returns> /// state was not already present. </returns>
virtual dfa::DFAState *addDFAState(dfa::DFA *dfa, dfa::DFAState *D); virtual dfa::DFAState *addDFAState(dfa::DFA *dfa, dfa::DFAState *D);
virtual void reportAttemptingFullContext(dfa::DFA *dfa, antlrcpp::BitSet *conflictingAlts, ATNConfigSet *configs, virtual void reportAttemptingFullContext(dfa::DFA *dfa, antlrcpp::BitSet *conflictingAlts,
size_t startIndex, size_t stopIndex); std::shared_ptr<ATNConfigSet> configs, size_t startIndex, size_t stopIndex);
virtual void reportContextSensitivity(dfa::DFA *dfa, int prediction, ATNConfigSet *configs, size_t startIndex, size_t stopIndex); virtual void reportContextSensitivity(dfa::DFA *dfa, int prediction, std::shared_ptr<ATNConfigSet> configs,
size_t startIndex, size_t stopIndex);
/// <summary> /// <summary>
/// If context sensitive parsing, we know it's ambiguity not conflict </summary> /// If context sensitive parsing, we know it's ambiguity not conflict </summary>
virtual void reportAmbiguity(dfa::DFA *dfa, dfa::DFAState *D, size_t startIndex, size_t stopIndex, bool exact, antlrcpp::BitSet *ambigAlts, ATNConfigSet *configs); virtual void reportAmbiguity(dfa::DFA *dfa, dfa::DFAState *D, size_t startIndex, size_t stopIndex, bool exact,
antlrcpp::BitSet *ambigAlts, std::shared_ptr<ATNConfigSet> configs);
public: public:
void setPredictionMode(PredictionMode mode); void setPredictionMode(PredictionMode mode);

View File

@ -59,7 +59,7 @@ struct AltAndContextConfigComparer {
} }
}; };
bool PredictionModeClass::hasSLLConflictTerminatingPrediction(PredictionMode* mode, ATNConfigSet* configs) { bool PredictionModeClass::hasSLLConflictTerminatingPrediction(PredictionMode* mode, std::shared_ptr<ATNConfigSet> configs) {
/* Configs in rule stop states indicate reaching the end of the decision /* Configs in rule stop states indicate reaching the end of the decision
* rule (local context) or end of start rule (full context). If all * rule (local context) or end of start rule (full context). If all
* configs meet this condition, then none of the configurations is able * configs meet this condition, then none of the configurations is able
@ -76,7 +76,7 @@ bool PredictionModeClass::hasSLLConflictTerminatingPrediction(PredictionMode* mo
// since we'll often fail over anyway. // since we'll often fail over anyway.
if (configs->hasSemanticContext) { if (configs->hasSemanticContext) {
// dup configs, tossing out semantic predicates // dup configs, tossing out semantic predicates
ATNConfigSet* dup = new ATNConfigSet(true, std::shared_ptr<ConfigLookup>()); std::shared_ptr<ATNConfigSet> dup = std::make_shared<ATNConfigSet>(true, std::shared_ptr<ConfigLookup>());
for (ATNConfig config : *configs->configLookup) { for (ATNConfig config : *configs->configLookup) {
ATNConfig* c = new ATNConfig(&config, SemanticContext::NONE); ATNConfig* c = new ATNConfig(&config, SemanticContext::NONE);
dup->add(c); dup->add(c);
@ -93,7 +93,7 @@ bool PredictionModeClass::hasSLLConflictTerminatingPrediction(PredictionMode* mo
return heuristic; return heuristic;
} }
bool PredictionModeClass::hasConfigInRuleStopState(ATNConfigSet* configs) { bool PredictionModeClass::hasConfigInRuleStopState(std::shared_ptr<ATNConfigSet> configs) {
for (ATNConfig c : *configs->configLookup) { for (ATNConfig c : *configs->configLookup) {
if (dynamic_cast<RuleStopState*>(c.state) != NULL) { if (dynamic_cast<RuleStopState*>(c.state) != NULL) {
return true; return true;
@ -103,7 +103,7 @@ bool PredictionModeClass::hasConfigInRuleStopState(ATNConfigSet* configs) {
return false; return false;
} }
bool PredictionModeClass::allConfigsInRuleStopStates(ATNConfigSet* configs) { bool PredictionModeClass::allConfigsInRuleStopStates(std::shared_ptr<ATNConfigSet> configs) {
for (ATNConfig config : *configs->configLookup) { for (ATNConfig config : *configs->configLookup) {
if (dynamic_cast<RuleStopState*>(config.state) == NULL) { if (dynamic_cast<RuleStopState*>(config.state) == NULL) {
return false; return false;
@ -170,7 +170,7 @@ antlrcpp::BitSet PredictionModeClass::getAlts(const std::vector<antlrcpp::BitSet
return all; return all;
} }
std::vector<antlrcpp::BitSet> PredictionModeClass::getConflictingAltSubsets(ATNConfigSet* configs) { std::vector<antlrcpp::BitSet> PredictionModeClass::getConflictingAltSubsets(std::shared_ptr<ATNConfigSet> configs) {
std::unordered_map<ATNConfig *, antlrcpp::BitSet, AltAndContextConfigHasher, AltAndContextConfigComparer> configToAlts; std::unordered_map<ATNConfig *, antlrcpp::BitSet, AltAndContextConfigHasher, AltAndContextConfigComparer> configToAlts;
for (auto config : *configs->configLookup) { for (auto config : *configs->configLookup) {
configToAlts[config].set((size_t)config->alt); configToAlts[config].set((size_t)config->alt);
@ -182,7 +182,7 @@ std::vector<antlrcpp::BitSet> PredictionModeClass::getConflictingAltSubsets(ATNC
return values; return values;
} }
std::map<ATNState*, antlrcpp::BitSet> PredictionModeClass::getStateToAltMap(ATNConfigSet* configs) { std::map<ATNState*, antlrcpp::BitSet> PredictionModeClass::getStateToAltMap(std::shared_ptr<ATNConfigSet> configs) {
std::map<ATNState*, antlrcpp::BitSet> m; std::map<ATNState*, antlrcpp::BitSet> m;
for (ATNConfig c : *configs->configLookup) { for (ATNConfig c : *configs->configLookup) {
m[c.state].set((size_t)c.alt); m[c.state].set((size_t)c.alt);
@ -190,7 +190,7 @@ std::map<ATNState*, antlrcpp::BitSet> PredictionModeClass::getStateToAltMap(ATNC
return m; return m;
} }
bool PredictionModeClass::hasStateAssociatedWithOneAlt(ATNConfigSet* configs) { bool PredictionModeClass::hasStateAssociatedWithOneAlt(std::shared_ptr<ATNConfigSet> configs) {
std::map<ATNState*, antlrcpp::BitSet> x = getStateToAltMap(configs); std::map<ATNState*, antlrcpp::BitSet> x = getStateToAltMap(configs);
for (std::map<ATNState*, antlrcpp::BitSet>::iterator it = x.begin(); it != x.end(); it++){ for (std::map<ATNState*, antlrcpp::BitSet>::iterator it = x.begin(); it != x.end(); it++){
if (it->second.count() == 1) return true; if (it->second.count() == 1) return true;

View File

@ -204,8 +204,7 @@ namespace atn {
/// the configurations to strip out all of the predicates so that a standard /// the configurations to strip out all of the predicates so that a standard
/// <seealso cref="ATNConfigSet"/> will merge everything ignoring predicates. /// <seealso cref="ATNConfigSet"/> will merge everything ignoring predicates.
/// </summary> /// </summary>
static bool hasSLLConflictTerminatingPrediction(PredictionMode *mode, static bool hasSLLConflictTerminatingPrediction(PredictionMode *mode, std::shared_ptr<ATNConfigSet> configs);
ATNConfigSet *configs);
/// <summary> /// <summary>
/// Checks if any configuration in {@code configs} is in a /// Checks if any configuration in {@code configs} is in a
@ -217,7 +216,7 @@ namespace atn {
/// <param name="configs"> the configuration set to test </param> /// <param name="configs"> the configuration set to test </param>
/// <returns> {@code true} if any configuration in {@code configs} is in a /// <returns> {@code true} if any configuration in {@code configs} is in a
/// <seealso cref="RuleStopState"/>, otherwise {@code false} </returns> /// <seealso cref="RuleStopState"/>, otherwise {@code false} </returns>
static bool hasConfigInRuleStopState(ATNConfigSet *configs); static bool hasConfigInRuleStopState(std::shared_ptr<ATNConfigSet> configs);
/// <summary> /// <summary>
/// Checks if all configurations in {@code configs} are in a /// Checks if all configurations in {@code configs} are in a
@ -229,7 +228,7 @@ namespace atn {
/// <param name="configs"> the configuration set to test </param> /// <param name="configs"> the configuration set to test </param>
/// <returns> {@code true} if all configurations in {@code configs} are in a /// <returns> {@code true} if all configurations in {@code configs} are in a
/// <seealso cref="RuleStopState"/>, otherwise {@code false} </returns> /// <seealso cref="RuleStopState"/>, otherwise {@code false} </returns>
static bool allConfigsInRuleStopStates(ATNConfigSet *configs); static bool allConfigsInRuleStopStates(std::shared_ptr<ATNConfigSet> configs);
/// <summary> /// <summary>
/// Full LL prediction termination. /// Full LL prediction termination.
@ -486,7 +485,7 @@ namespace atn {
/// alt and not pred /// alt and not pred
/// </pre> /// </pre>
/// </summary> /// </summary>
static std::vector<antlrcpp::BitSet> getConflictingAltSubsets(ATNConfigSet *configs); static std::vector<antlrcpp::BitSet> getConflictingAltSubsets(std::shared_ptr<ATNConfigSet> configs);
/// <summary> /// <summary>
/// Get a map from state to alt subset from a configuration set. For each /// Get a map from state to alt subset from a configuration set. For each
@ -497,9 +496,9 @@ namespace atn {
/// cref="ATNConfig#alt alt"/> /// cref="ATNConfig#alt alt"/>
/// </pre> /// </pre>
/// </summary> /// </summary>
static std::map<ATNState*, antlrcpp::BitSet> getStateToAltMap(ATNConfigSet *configs); static std::map<ATNState*, antlrcpp::BitSet> getStateToAltMap(std::shared_ptr<ATNConfigSet> configs);
static bool hasStateAssociatedWithOneAlt(ATNConfigSet *configs); static bool hasStateAssociatedWithOneAlt(std::shared_ptr<ATNConfigSet> configs);
static int getSingleViableAlt(const std::vector<antlrcpp::BitSet> &altsets); static int getSingleViableAlt(const std::vector<antlrcpp::BitSet> &altsets);
}; };

View File

@ -55,10 +55,11 @@ void DFAState::PredPrediction::InitializeInstanceFields() {
DFAState::DFAState() : DFAState(-1) { DFAState::DFAState() : DFAState(-1) {
} }
DFAState::DFAState(int stateNumber) : DFAState(new atn::ATNConfigSet(true, std::shared_ptr<ConfigLookup>()), stateNumber) { DFAState::DFAState(int stateNumber) : DFAState(std::make_shared<ATNConfigSet>(true, std::shared_ptr<ConfigLookup>()),
stateNumber) {
} }
DFAState::DFAState(atn::ATNConfigSet *configs, int stateNumber) : configs(configs), stateNumber(stateNumber) { DFAState::DFAState(std::shared_ptr<ATNConfigSet> configs, int stateNumber) : configs(configs), stateNumber(stateNumber) {
InitializeInstanceFields(); InitializeInstanceFields();
} }
@ -82,13 +83,13 @@ size_t DFAState::hashCode() {
return hash; return hash;
} }
bool DFAState::equals(DFAState *o) { bool DFAState::operator == (const DFAState &o) {
// compare set of ATN configurations in this set with other // compare set of ATN configurations in this set with other
if (this == o) { if (this == &o) {
return true; return true;
} }
return configs->equals(o->configs); return configs == o.configs;
} }
std::wstring DFAState::toString() { std::wstring DFAState::toString() {

View File

@ -78,7 +78,7 @@ namespace dfa {
public: public:
int stateNumber; int stateNumber;
atn::ATNConfigSet *configs; std::shared_ptr<atn::ATNConfigSet> configs;
/// <summary> /// <summary>
/// {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1) /// {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1)
@ -124,7 +124,7 @@ namespace dfa {
/// Map a predicate to a predicted alternative. /// Map a predicate to a predicted alternative.
DFAState(); DFAState();
DFAState(int stateNumber); DFAState(int stateNumber);
DFAState(atn::ATNConfigSet *configs, int stateNumber = -1); DFAState(std::shared_ptr<atn::ATNConfigSet> configs, int stateNumber = -1);
/// <summary> /// <summary>
/// Get the set of all alts mentioned by all ATN configurations in this /// Get the set of all alts mentioned by all ATN configurations in this
@ -147,7 +147,7 @@ namespace dfa {
/// exists that has this exact set of ATN configurations. The /// exists that has this exact set of ATN configurations. The
/// <seealso cref="#stateNumber"/> is irrelevant. /// <seealso cref="#stateNumber"/> is irrelevant.
/// </summary> /// </summary>
virtual bool equals(DFAState *o); bool operator == (const DFAState &o);
virtual std::wstring toString(); virtual std::wstring toString();