The parser is parsing for the first time.

- Removed ultra simple test grammar + parser. No longer needed.
- Removed long list of keywords from (regular) test grammar.
- Fixed a number of toString() methods to get better debug output.
- Moved Ref typedefs from Declarations.h to the individual classes as defining them on the forward declarations totally confuses the XCode debugger.
- Removed reference to the owning ATN in an ATNState. We cannot guarantee to have the correct address there due to the way the states are created. The reference is not needed anyway.
- ATNDeserializationOptions now has verifyATN set by default (as in the Java target).
- Had to add a workaround for a weird situation: static initialization in ATNDeseralizer stopped working for no apparent reason. Need to investigate this.
- Added a few support methods to the CPPUtils, mostly to ease debug output creation.
- Added console listener by default to the listeners list (as done in the Java target).
- Fixed translation mistakes in the CommonTokenStream class.
- Fixed some memory leaks and exception handling bugs.
This commit is contained in:
Mike Lischke 2016-04-17 13:13:15 +02:00
parent eb0241f767
commit f292d14abc
131 changed files with 1154 additions and 1762 deletions

1
.gitignore vendored
View File

@ -63,3 +63,4 @@ runtime-testsuite/target
tool-testsuite/target tool-testsuite/target
runtime/Cpp/demo/generated runtime/Cpp/demo/generated
runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/xcuserdata runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/xcuserdata
runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/project.xcworkspace/xcuserdata

View File

@ -12,39 +12,18 @@
#include "TLexer.h" #include "TLexer.h"
#include "TParser.h" #include "TParser.h"
#include "TestLexer.h"
#include "TestParser.h"
using namespace antlrcpptest; using namespace antlrcpptest;
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
int main(int argc, const char * argv[]) { int main(int argc, const char * argv[]) {
ANTLRInputStream input(L"divide and conquer");
ANTLRInputStream input(L"a");
TLexer lexer(&input); TLexer lexer(&input);
CommonTokenStream tokens(&lexer); CommonTokenStream tokens(&lexer);
TParser parser(&tokens); TParser parser(&tokens);
std::shared_ptr<tree::ParseTree> tree = parser.main(); std::shared_ptr<tree::ParseTree> tree = parser.main();
for (auto token : tokens.getTokens()) {
std::wcout << token->toString() << std::endl;
}
std::wcout << tree->toStringTree(&parser);
/*
ANTLRInputStream input(L"1");
TLexer lexer(&input);
CommonTokenStream tokens(&lexer);
TParser parser(&tokens);
std::shared_ptr<tree::ParseTree> tree = parser.main();
for (auto token : tokens.getTokens()) {
std::wcout << token->toString() << std::endl;
}
std::wcout << tree->toStringTree(&parser) << std::endl; std::wcout << tree->toStringTree(&parser) << std::endl;
*/
return 0; return 0;
} }

View File

@ -56,7 +56,7 @@ using namespace org::antlr::v4::runtime;
- (void)testExample { - (void)testExample {
try { try {
std::vector<dfa::DFA *> decisionToDFA; std::vector<dfa::DFA> decisionToDFA;
atn::ATN atn; atn::ATN atn;
org::antlr::v4::runtime::atn::ParserATNSimulator foo(nullptr, atn, decisionToDFA, nullptr); org::antlr::v4::runtime::atn::ParserATNSimulator foo(nullptr, atn, decisionToDFA, nullptr);

View File

@ -21,8 +21,8 @@
276927251C9ED49100E4EBF8 /* antlrcpp-Prefix.h in Headers */ = {isa = PBXBuildFile; fileRef = 276927231C9ED49100E4EBF8 /* antlrcpp-Prefix.h */; }; 276927251C9ED49100E4EBF8 /* antlrcpp-Prefix.h in Headers */ = {isa = PBXBuildFile; fileRef = 276927231C9ED49100E4EBF8 /* antlrcpp-Prefix.h */; };
278A66FB1C95838E002D667E /* ANTLRErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 278A66FA1C95838E002D667E /* ANTLRErrorListener.cpp */; }; 278A66FB1C95838E002D667E /* ANTLRErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 278A66FA1C95838E002D667E /* ANTLRErrorListener.cpp */; };
278A66FC1C95838E002D667E /* ANTLRErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 278A66FA1C95838E002D667E /* ANTLRErrorListener.cpp */; }; 278A66FC1C95838E002D667E /* ANTLRErrorListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 278A66FA1C95838E002D667E /* ANTLRErrorListener.cpp */; };
27BC755D1CB41B0D00AE780B /* TestLexer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27BC75591CB41B0D00AE780B /* TestLexer.cpp */; }; 27A23EA31CC2A8D60036D8A3 /* TLexer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27A23EA11CC2A8D60036D8A3 /* TLexer.cpp */; };
27BC755E1CB41B0D00AE780B /* TestParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27BC755B1CB41B0D00AE780B /* TestParser.cpp */; }; 27A23EA41CC2A8D60036D8A3 /* TLexer.h in Headers */ = {isa = PBXBuildFile; fileRef = 27A23EA21CC2A8D60036D8A3 /* TLexer.h */; };
27C666AC1C9584050021E494 /* ANTLRErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C6665D1C9584050021E494 /* ANTLRErrorListener.h */; }; 27C666AC1C9584050021E494 /* ANTLRErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C6665D1C9584050021E494 /* ANTLRErrorListener.h */; };
27C666AD1C9584050021E494 /* ANTLRErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C6665D1C9584050021E494 /* ANTLRErrorListener.h */; }; 27C666AD1C9584050021E494 /* ANTLRErrorListener.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C6665D1C9584050021E494 /* ANTLRErrorListener.h */; };
27C666AE1C9584050021E494 /* ANTLRErrorStrategy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6665E1C9584050021E494 /* ANTLRErrorStrategy.cpp */; }; 27C666AE1C9584050021E494 /* ANTLRErrorStrategy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6665E1C9584050021E494 /* ANTLRErrorStrategy.cpp */; };
@ -501,7 +501,6 @@
27C66A1F1C958AB30021E494 /* TokenTagToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C669FF1C958AB30021E494 /* TokenTagToken.h */; }; 27C66A1F1C958AB30021E494 /* TokenTagToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C669FF1C958AB30021E494 /* TokenTagToken.h */; };
27C66A6A1C9591280021E494 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C66A691C9591280021E494 /* main.cpp */; }; 27C66A6A1C9591280021E494 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C66A691C9591280021E494 /* main.cpp */; };
27C66A701C9591550021E494 /* antlrcpp.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 37D727AA1867AF1E007B6D10 /* antlrcpp.dylib */; }; 27C66A701C9591550021E494 /* antlrcpp.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 37D727AA1867AF1E007B6D10 /* antlrcpp.dylib */; };
27C6E17F1C972FFC0079AF06 /* TLexer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6E1711C972FFB0079AF06 /* TLexer.cpp */; };
27C6E1801C972FFC0079AF06 /* TParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6E1741C972FFC0079AF06 /* TParser.cpp */; }; 27C6E1801C972FFC0079AF06 /* TParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6E1741C972FFC0079AF06 /* TParser.cpp */; };
27C6E1811C972FFC0079AF06 /* TParserBaseListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6E1771C972FFC0079AF06 /* TParserBaseListener.cpp */; }; 27C6E1811C972FFC0079AF06 /* TParserBaseListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6E1771C972FFC0079AF06 /* TParserBaseListener.cpp */; };
27C6E1821C972FFC0079AF06 /* TParserBaseVisitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6E1791C972FFC0079AF06 /* TParserBaseVisitor.cpp */; }; 27C6E1821C972FFC0079AF06 /* TParserBaseVisitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6E1791C972FFC0079AF06 /* TParserBaseVisitor.cpp */; };
@ -547,10 +546,8 @@
274FC6D81CA96B6C008D4374 /* MiscClassTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MiscClassTests.mm; sourceTree = "<group>"; wrapsLines = 0; }; 274FC6D81CA96B6C008D4374 /* MiscClassTests.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MiscClassTests.mm; sourceTree = "<group>"; wrapsLines = 0; };
276927231C9ED49100E4EBF8 /* antlrcpp-Prefix.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; name = "antlrcpp-Prefix.h"; path = "../../runtime/antlrcpp-Prefix.h"; sourceTree = "<group>"; }; 276927231C9ED49100E4EBF8 /* antlrcpp-Prefix.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; name = "antlrcpp-Prefix.h"; path = "../../runtime/antlrcpp-Prefix.h"; sourceTree = "<group>"; };
278A66FA1C95838E002D667E /* ANTLRErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ANTLRErrorListener.cpp; path = ../../runtime/ANTLRErrorListener.cpp; sourceTree = SOURCE_ROOT; }; 278A66FA1C95838E002D667E /* ANTLRErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ANTLRErrorListener.cpp; path = ../../runtime/ANTLRErrorListener.cpp; sourceTree = SOURCE_ROOT; };
27BC75591CB41B0D00AE780B /* TestLexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TestLexer.cpp; path = ../generated/TestLexer.cpp; sourceTree = "<group>"; }; 27A23EA11CC2A8D60036D8A3 /* TLexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TLexer.cpp; path = ../generated/TLexer.cpp; sourceTree = "<group>"; };
27BC755A1CB41B0D00AE780B /* TestLexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestLexer.h; path = ../generated/TestLexer.h; sourceTree = "<group>"; }; 27A23EA21CC2A8D60036D8A3 /* TLexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TLexer.h; path = ../generated/TLexer.h; sourceTree = "<group>"; };
27BC755B1CB41B0D00AE780B /* TestParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TestParser.cpp; path = ../generated/TestParser.cpp; sourceTree = "<group>"; };
27BC755C1CB41B0D00AE780B /* TestParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestParser.h; path = ../generated/TestParser.h; sourceTree = "<group>"; };
27C6665D1C9584050021E494 /* ANTLRErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ANTLRErrorListener.h; path = ../../runtime/ANTLRErrorListener.h; sourceTree = SOURCE_ROOT; wrapsLines = 0; }; 27C6665D1C9584050021E494 /* ANTLRErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ANTLRErrorListener.h; path = ../../runtime/ANTLRErrorListener.h; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
27C6665E1C9584050021E494 /* ANTLRErrorStrategy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ANTLRErrorStrategy.cpp; path = ../../runtime/ANTLRErrorStrategy.cpp; sourceTree = SOURCE_ROOT; }; 27C6665E1C9584050021E494 /* ANTLRErrorStrategy.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ANTLRErrorStrategy.cpp; path = ../../runtime/ANTLRErrorStrategy.cpp; sourceTree = SOURCE_ROOT; };
27C6665F1C9584050021E494 /* ANTLRErrorStrategy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ANTLRErrorStrategy.h; path = ../../runtime/ANTLRErrorStrategy.h; sourceTree = SOURCE_ROOT; }; 27C6665F1C9584050021E494 /* ANTLRErrorStrategy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ANTLRErrorStrategy.h; path = ../../runtime/ANTLRErrorStrategy.h; sourceTree = SOURCE_ROOT; };
@ -570,7 +567,7 @@
27C6666D1C9584050021E494 /* CommonToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommonToken.h; path = ../../runtime/CommonToken.h; sourceTree = SOURCE_ROOT; }; 27C6666D1C9584050021E494 /* CommonToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommonToken.h; path = ../../runtime/CommonToken.h; sourceTree = SOURCE_ROOT; };
27C6666E1C9584050021E494 /* CommonTokenFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CommonTokenFactory.cpp; path = ../../runtime/CommonTokenFactory.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; }; 27C6666E1C9584050021E494 /* CommonTokenFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CommonTokenFactory.cpp; path = ../../runtime/CommonTokenFactory.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
27C6666F1C9584050021E494 /* CommonTokenFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommonTokenFactory.h; path = ../../runtime/CommonTokenFactory.h; sourceTree = SOURCE_ROOT; wrapsLines = 0; }; 27C6666F1C9584050021E494 /* CommonTokenFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommonTokenFactory.h; path = ../../runtime/CommonTokenFactory.h; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
27C666701C9584050021E494 /* CommonTokenStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CommonTokenStream.cpp; path = ../../runtime/CommonTokenStream.cpp; sourceTree = SOURCE_ROOT; }; 27C666701C9584050021E494 /* CommonTokenStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = CommonTokenStream.cpp; path = ../../runtime/CommonTokenStream.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
27C666711C9584050021E494 /* CommonTokenStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommonTokenStream.h; path = ../../runtime/CommonTokenStream.h; sourceTree = SOURCE_ROOT; }; 27C666711C9584050021E494 /* CommonTokenStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CommonTokenStream.h; path = ../../runtime/CommonTokenStream.h; sourceTree = SOURCE_ROOT; };
27C666721C9584050021E494 /* ConsoleErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ConsoleErrorListener.cpp; path = ../../runtime/ConsoleErrorListener.cpp; sourceTree = SOURCE_ROOT; }; 27C666721C9584050021E494 /* ConsoleErrorListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ConsoleErrorListener.cpp; path = ../../runtime/ConsoleErrorListener.cpp; sourceTree = SOURCE_ROOT; };
27C666731C9584050021E494 /* ConsoleErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ConsoleErrorListener.h; path = ../../runtime/ConsoleErrorListener.h; sourceTree = SOURCE_ROOT; }; 27C666731C9584050021E494 /* ConsoleErrorListener.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ConsoleErrorListener.h; path = ../../runtime/ConsoleErrorListener.h; sourceTree = SOURCE_ROOT; };
@ -591,8 +588,8 @@
27C666831C9584050021E494 /* IRecognizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IRecognizer.h; path = ../../runtime/IRecognizer.h; sourceTree = SOURCE_ROOT; }; 27C666831C9584050021E494 /* IRecognizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IRecognizer.h; path = ../../runtime/IRecognizer.h; sourceTree = SOURCE_ROOT; };
27C666841C9584050021E494 /* Lexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Lexer.cpp; path = ../../runtime/Lexer.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; }; 27C666841C9584050021E494 /* Lexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Lexer.cpp; path = ../../runtime/Lexer.cpp; sourceTree = SOURCE_ROOT; wrapsLines = 0; };
27C666851C9584050021E494 /* Lexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Lexer.h; path = ../../runtime/Lexer.h; sourceTree = SOURCE_ROOT; }; 27C666851C9584050021E494 /* Lexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Lexer.h; path = ../../runtime/Lexer.h; sourceTree = SOURCE_ROOT; };
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; wrapsLines = 0; };
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; 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; }; 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; wrapsLines = 0; }; 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; };
@ -689,7 +686,7 @@
27C667811C95846E0021E494 /* PlusLoopbackState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlusLoopbackState.h; sourceTree = "<group>"; }; 27C667811C95846E0021E494 /* PlusLoopbackState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PlusLoopbackState.h; sourceTree = "<group>"; };
27C667821C95846E0021E494 /* PrecedencePredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PrecedencePredicateTransition.cpp; sourceTree = "<group>"; wrapsLines = 0; }; 27C667821C95846E0021E494 /* PrecedencePredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PrecedencePredicateTransition.cpp; sourceTree = "<group>"; wrapsLines = 0; };
27C667831C95846E0021E494 /* PrecedencePredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrecedencePredicateTransition.h; sourceTree = "<group>"; }; 27C667831C95846E0021E494 /* PrecedencePredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrecedencePredicateTransition.h; sourceTree = "<group>"; };
27C667841C95846E0021E494 /* PredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredicateTransition.cpp; sourceTree = "<group>"; }; 27C667841C95846E0021E494 /* PredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredicateTransition.cpp; sourceTree = "<group>"; wrapsLines = 0; };
27C667851C95846E0021E494 /* PredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredicateTransition.h; sourceTree = "<group>"; }; 27C667851C95846E0021E494 /* PredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredicateTransition.h; sourceTree = "<group>"; };
27C667861C95846E0021E494 /* PredictionContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredictionContext.cpp; sourceTree = "<group>"; wrapsLines = 0; }; 27C667861C95846E0021E494 /* PredictionContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredictionContext.cpp; sourceTree = "<group>"; wrapsLines = 0; };
27C667871C95846E0021E494 /* PredictionContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredictionContext.h; sourceTree = "<group>"; wrapsLines = 0; }; 27C667871C95846E0021E494 /* PredictionContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredictionContext.h; sourceTree = "<group>"; wrapsLines = 0; };
@ -701,7 +698,7 @@
27C6678F1C95846E0021E494 /* RuleStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleStartState.h; sourceTree = "<group>"; }; 27C6678F1C95846E0021E494 /* RuleStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleStartState.h; sourceTree = "<group>"; };
27C667901C95846E0021E494 /* RuleStopState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleStopState.cpp; sourceTree = "<group>"; }; 27C667901C95846E0021E494 /* RuleStopState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleStopState.cpp; sourceTree = "<group>"; };
27C667911C95846E0021E494 /* RuleStopState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleStopState.h; sourceTree = "<group>"; }; 27C667911C95846E0021E494 /* RuleStopState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleStopState.h; sourceTree = "<group>"; };
27C667921C95846E0021E494 /* RuleTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleTransition.cpp; sourceTree = "<group>"; }; 27C667921C95846E0021E494 /* RuleTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleTransition.cpp; sourceTree = "<group>"; wrapsLines = 0; };
27C667931C95846E0021E494 /* RuleTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleTransition.h; sourceTree = "<group>"; }; 27C667931C95846E0021E494 /* RuleTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleTransition.h; sourceTree = "<group>"; };
27C667941C95846E0021E494 /* SemanticContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SemanticContext.cpp; sourceTree = "<group>"; wrapsLines = 0; }; 27C667941C95846E0021E494 /* SemanticContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SemanticContext.cpp; sourceTree = "<group>"; wrapsLines = 0; };
27C667951C95846E0021E494 /* SemanticContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SemanticContext.h; sourceTree = "<group>"; }; 27C667951C95846E0021E494 /* SemanticContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SemanticContext.h; sourceTree = "<group>"; };
@ -795,8 +792,6 @@
27C66A691C9591280021E494 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; }; 27C66A691C9591280021E494 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
27C66A731C9592400021E494 /* TLexer.g4 */ = {isa = PBXFileReference; lastKnownFileType = text; name = TLexer.g4; path = ../../TLexer.g4; sourceTree = "<group>"; }; 27C66A731C9592400021E494 /* TLexer.g4 */ = {isa = PBXFileReference; lastKnownFileType = text; name = TLexer.g4; path = ../../TLexer.g4; sourceTree = "<group>"; };
27C66A741C9592400021E494 /* TParser.g4 */ = {isa = PBXFileReference; lastKnownFileType = text; name = TParser.g4; path = ../../TParser.g4; sourceTree = "<group>"; }; 27C66A741C9592400021E494 /* TParser.g4 */ = {isa = PBXFileReference; lastKnownFileType = text; name = TParser.g4; path = ../../TParser.g4; sourceTree = "<group>"; };
27C6E1711C972FFB0079AF06 /* TLexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TLexer.cpp; path = ../generated/TLexer.cpp; sourceTree = "<group>"; };
27C6E1721C972FFC0079AF06 /* TLexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TLexer.h; path = ../generated/TLexer.h; sourceTree = "<group>"; };
27C6E1741C972FFC0079AF06 /* TParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TParser.cpp; path = ../generated/TParser.cpp; sourceTree = "<group>"; wrapsLines = 0; }; 27C6E1741C972FFC0079AF06 /* TParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TParser.cpp; path = ../generated/TParser.cpp; sourceTree = "<group>"; wrapsLines = 0; };
27C6E1751C972FFC0079AF06 /* TParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TParser.h; path = ../generated/TParser.h; sourceTree = "<group>"; wrapsLines = 0; }; 27C6E1751C972FFC0079AF06 /* TParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TParser.h; path = ../generated/TParser.h; sourceTree = "<group>"; wrapsLines = 0; };
27C6E1771C972FFC0079AF06 /* TParserBaseListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TParserBaseListener.cpp; path = ../generated/TParserBaseListener.cpp; sourceTree = "<group>"; }; 27C6E1771C972FFC0079AF06 /* TParserBaseListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TParserBaseListener.cpp; path = ../generated/TParserBaseListener.cpp; sourceTree = "<group>"; };
@ -848,36 +843,6 @@
/* End PBXFrameworksBuildPhase section */ /* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */ /* Begin PBXGroup section */
27BC755F1CB41B1100AE780B /* Simple */ = {
isa = PBXGroup;
children = (
27BC75591CB41B0D00AE780B /* TestLexer.cpp */,
27BC755A1CB41B0D00AE780B /* TestLexer.h */,
27BC755B1CB41B0D00AE780B /* TestParser.cpp */,
27BC755C1CB41B0D00AE780B /* TestParser.h */,
27C6E1711C972FFB0079AF06 /* TLexer.cpp */,
27C6E1721C972FFC0079AF06 /* TLexer.h */,
);
name = Simple;
sourceTree = "<group>";
};
27BC75601CB41B2400AE780B /* Advanced */ = {
isa = PBXGroup;
children = (
27C6E1741C972FFC0079AF06 /* TParser.cpp */,
27C6E1751C972FFC0079AF06 /* TParser.h */,
27C6E1771C972FFC0079AF06 /* TParserBaseListener.cpp */,
27C6E1781C972FFC0079AF06 /* TParserBaseListener.h */,
27C6E1791C972FFC0079AF06 /* TParserBaseVisitor.cpp */,
27C6E1851C97322F0079AF06 /* TParserBaseVisitor.h */,
27C6E17B1C972FFC0079AF06 /* TParserListener.cpp */,
27C6E17C1C972FFC0079AF06 /* TParserListener.h */,
27C6E17D1C972FFC0079AF06 /* TParserVisitor.cpp */,
27C6E1861C97322F0079AF06 /* TParserVisitor.h */,
);
name = Advanced;
sourceTree = "<group>";
};
27C667461C9584570021E494 /* atn */ = { 27C667461C9584570021E494 /* atn */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -1088,8 +1053,18 @@
27C66A5C1C958EB50021E494 /* generated */ = { 27C66A5C1C958EB50021E494 /* generated */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
27BC75601CB41B2400AE780B /* Advanced */, 27A23EA11CC2A8D60036D8A3 /* TLexer.cpp */,
27BC755F1CB41B1100AE780B /* Simple */, 27A23EA21CC2A8D60036D8A3 /* TLexer.h */,
27C6E1741C972FFC0079AF06 /* TParser.cpp */,
27C6E1751C972FFC0079AF06 /* TParser.h */,
27C6E1771C972FFC0079AF06 /* TParserBaseListener.cpp */,
27C6E1781C972FFC0079AF06 /* TParserBaseListener.h */,
27C6E1791C972FFC0079AF06 /* TParserBaseVisitor.cpp */,
27C6E1851C97322F0079AF06 /* TParserBaseVisitor.h */,
27C6E17B1C972FFC0079AF06 /* TParserListener.cpp */,
27C6E17C1C972FFC0079AF06 /* TParserListener.h */,
27C6E17D1C972FFC0079AF06 /* TParserVisitor.cpp */,
27C6E1861C97322F0079AF06 /* TParserVisitor.h */,
); );
name = generated; name = generated;
sourceTree = "<group>"; sourceTree = "<group>";
@ -1425,6 +1400,7 @@
27C667C41C95846E0021E494 /* ATNDeserializer.h in Headers */, 27C667C41C95846E0021E494 /* ATNDeserializer.h in Headers */,
2747A70C1CA691310030247B /* ConfigLookup.h in Headers */, 2747A70C1CA691310030247B /* ConfigLookup.h in Headers */,
27C668731C9584B60021E494 /* DFASerializer.h in Headers */, 27C668731C9584B60021E494 /* DFASerializer.h in Headers */,
27A23EA41CC2A8D60036D8A3 /* TLexer.h in Headers */,
27C667361C9584050021E494 /* TokenStream.h in Headers */, 27C667361C9584050021E494 /* TokenStream.h in Headers */,
27C666E41C9584050021E494 /* Exceptions.h in Headers */, 27C666E41C9584050021E494 /* Exceptions.h in Headers */,
27C667321C9584050021E494 /* TokenSource.h in Headers */, 27C667321C9584050021E494 /* TokenSource.h in Headers */,
@ -1656,9 +1632,6 @@
27C6E1811C972FFC0079AF06 /* TParserBaseListener.cpp in Sources */, 27C6E1811C972FFC0079AF06 /* TParserBaseListener.cpp in Sources */,
27C6E1841C972FFC0079AF06 /* TParserVisitor.cpp in Sources */, 27C6E1841C972FFC0079AF06 /* TParserVisitor.cpp in Sources */,
27C6E1801C972FFC0079AF06 /* TParser.cpp in Sources */, 27C6E1801C972FFC0079AF06 /* TParser.cpp in Sources */,
27BC755D1CB41B0D00AE780B /* TestLexer.cpp in Sources */,
27C6E17F1C972FFC0079AF06 /* TLexer.cpp in Sources */,
27BC755E1CB41B0D00AE780B /* TestParser.cpp in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
@ -1868,6 +1841,7 @@
27C667021C9584050021E494 /* LexerNoViableAltException.cpp in Sources */, 27C667021C9584050021E494 /* LexerNoViableAltException.cpp in Sources */,
27C667EE1C95846E0021E494 /* EmptyPredictionContext.cpp in Sources */, 27C667EE1C95846E0021E494 /* EmptyPredictionContext.cpp in Sources */,
27C666C61C9584050021E494 /* CharStream.cpp in Sources */, 27C666C61C9584050021E494 /* CharStream.cpp in Sources */,
27A23EA31CC2A8D60036D8A3 /* TLexer.cpp in Sources */,
27C667CE1C95846E0021E494 /* ATNState.cpp in Sources */, 27C667CE1C95846E0021E494 /* ATNState.cpp in Sources */,
27C666BE1C9584050021E494 /* BaseErrorListener.cpp in Sources */, 27C666BE1C9584050021E494 /* BaseErrorListener.cpp in Sources */,
27C6671E1C9584050021E494 /* RecognitionException.cpp in Sources */, 27C6671E1C9584050021E494 /* RecognitionException.cpp in Sources */,
@ -2032,7 +2006,6 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@ -2073,7 +2046,6 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES; CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;

View File

@ -36,10 +36,10 @@ INT: Digit+;
Digit: [0..9]; Digit: [0..9];
ID: LETTER (LETTER | '0'..'9')*; ID: LETTER (LETTER | '0'..'9')*;
fragment LETTER : [a-zA-Z\u0080-\u00FF_] ; fragment LETTER : [a-zA-Z\u0080-\uFFFD] ;
LessThan: '<' -> pushMode(Mode1); LessThan: '<' -> pushMode(Mode1);
GreaterThan: '>' -> popMode; GreaterThan: '>';// -> popMode;
Equal: '='; Equal: '=';
And: 'and'; And: 'and';
@ -70,663 +70,3 @@ Dot: '.';
mode Mode2; mode Mode2;
DotDot: '..'; DotDot: '..';
/*
ACCESSIBLE_SYMBOL: 'ACCESSIBLE' ;
ACCOUNT_SYMBOL: 'ACCOUNT' ;
ACTION_SYMBOL: 'ACTION'; // SQL-2003-N
ADD_SYMBOL: 'ADD'; // SQL-2003-R
ADDDATE_SYMBOL: 'ADDDATE' ;
AFTER_SYMBOL: 'AFTER'; // SQL-2003-N
AGAINST_SYMBOL: 'AGAINST';
AGGREGATE_SYMBOL: 'AGGREGATE';
ALGORITHM_SYMBOL: 'ALGORITHM';
ALL_SYMBOL: 'ALL'; // SQL-2003-R
ALTER_SYMBOL: 'ALTER'; // SQL-2003-R
ALWAYS_SYMBOL: 'ALWAYS' ;
ANALYSE_SYMBOL: 'ANALYSE';
ANALYZE_SYMBOL: 'ANALYZE';
AND_SYMBOL: 'AND'; // SQL-2003-R
ANY_SYMBOL: 'ANY'; // SQL-2003-R
AS_SYMBOL: 'AS'; // SQL-2003-R
ASC_SYMBOL: 'ASC'; // SQL-2003-N
ASCII_SYMBOL: 'ASCII'; // MYSQL-FUNC
ASENSITIVE_SYMBOL: 'ASENSITIVE' ;
AT_SYMBOL: 'AT';
AUTHORS_SYMBOL: 'AUTHORS' ;
AUTOEXTEND_SIZE_SYMBOL: 'AUTOEXTEND_SIZE';
AUTO_INCREMENT_SYMBOL: 'AUTO_INCREMENT';
AVG_ROW_LENGTH_SYMBOL: 'AVG_ROW_LENGTH';
AVG_SYMBOL: 'AVG'; // SQL-2003-N
BACKUP_SYMBOL: 'BACKUP';
BEFORE_SYMBOL: 'BEFORE' ;
BEGIN_SYMBOL: 'BEGIN'; // SQL-2003-R
BETWEEN_SYMBOL: 'BETWEEN'; // SQL-2003-R
BIGINT_SYMBOL: 'BIGINT'; // SQL-2003-R
BINARY_SYMBOL: 'BINARY'; // SQL-2003-R
BINLOG_SYMBOL: 'BINLOG';
BIN_NUM_SYMBOL: 'BIN_NUM';
BIT_AND_SYMBOL: 'BIT_AND' ;
BIT_OR_SYMBOL: 'BIT_OR' ;
BIT_SYMBOL: 'BIT'; // MYSQL-FUNC
BIT_XOR_SYMBOL: 'BIT_XOR' ;
BLOB_SYMBOL: 'BLOB'; // SQL-2003-R
BLOCK_SYMBOL: 'BLOCK';
BOOLEAN_SYMBOL: 'BOOLEAN'; // SQL-2003-R
BOOL_SYMBOL: 'BOOL';
BOTH_SYMBOL: 'BOTH'; // SQL-2003-R
BTREE_SYMBOL: 'BTREE';
BY_SYMBOL: 'BY'; // SQL-2003-R
BYTE_SYMBOL: 'BYTE';
CACHE_SYMBOL: 'CACHE';
CALL_SYMBOL: 'CALL' ;
CASCADE_SYMBOL: 'CASCADE'; // SQL-2003-N
CASCADED_SYMBOL: 'CASCADED'; // SQL-2003-R
CASE_SYMBOL: 'CASE'; // SQL-2003-R
CAST_SYMBOL: 'CAST' ;
CATALOG_NAME_SYMBOL: 'CATALOG_NAME'; // SQL-2003-N
CHAIN_SYMBOL: 'CHAIN'; // SQL-2003-N
CHANGE_SYMBOL: 'CHANGE';
CHANGED_SYMBOL: 'CHANGED';
CHANNEL_SYMBOL: 'CHANNEL' ;
CHARSET_SYMBOL: 'CHARSET';
CHARACTER_SYMBOL: 'CHARACTER' ;
CHAR_SYMBOL: 'CHAR'; // SQL-2003-R
CHECKSUM_SYMBOL: 'CHECKSUM';
CHECK_SYMBOL: 'CHECK' ;
CIPHER_SYMBOL: 'CIPHER';
CLASS_ORIGIN_SYMBOL: 'CLASS_ORIGIN'; // SQL-2003-N
CLIENT_SYMBOL: 'CLIENT';
CLOSE_SYMBOL: 'CLOSE'; // SQL-2003-R
COALESCE_SYMBOL: 'COALESCE'; // SQL-2003-N
CODE_SYMBOL: 'CODE';
COLLATE_SYMBOL: 'COLLATE' ;
COLLATION_SYMBOL: 'COLLATION'; // SQL-2003-N
COLUMNS_SYMBOL: 'COLUMNS';
COLUMN_SYMBOL: 'COLUMN'; // SQL-2003-R
COLUMN_NAME_SYMBOL: 'COLUMN_NAME'; // SQL-2003-N
COLUMN_FORMAT_SYMBOL: 'COLUMN_FORMAT' ;
COMMENT_SYMBOL: 'COMMENT';
COMMITTED_SYMBOL: 'COMMITTED'; // SQL-2003-N
COMMIT_SYMBOL: 'COMMIT'; // SQL-2003-R
COMPACT_SYMBOL: 'COMPACT';
COMPLETION_SYMBOL: 'COMPLETION';
COMPRESSED_SYMBOL: 'COMPRESSED';
COMPRESSION_SYMBOL: 'COMPRESSION' ;
CONCURRENT_SYMBOL: 'CONCURRENT';
CONDITION_SYMBOL: 'CONDITION' ;
CONNECTION_SYMBOL: 'CONNECTION' ;
CONSISTENT_SYMBOL: 'CONSISTENT';
CONSTRAINT_SYMBOL: 'CONSTRAINT'; // SQL-2003-R
CONSTRAINT_CATALOG_SYMBOL: 'CONSTRAINT_CATALOG'; // SQL-2003-N
CONSTRAINT_NAME_SYMBOL: 'CONSTRAINT_NAME'; // SQL-2003-N
CONSTRAINT_SCHEMA_SYMBOL: 'CONSTRAINT_SCHEMA'; // SQL-2003-N
CONTAINS_SYMBOL: 'CONTAINS'; // SQL-2003-N
CONTEXT_SYMBOL: 'CONTEXT';
CONTINUE_SYMBOL: 'CONTINUE' ;
CONTRIBUTORS_SYMBOL: 'CONTRIBUTORS' ;
CONVERT_SYMBOL: 'CONVERT' ;
COUNT_SYMBOL: 'COUNT' ;
CPU_SYMBOL: 'CPU';
CREATE_SYMBOL: 'CREATE'; // SQL-2003-R
CROSS_SYMBOL: 'CROSS'; // SQL-2003-R
CUBE_SYMBOL: 'CUBE'; // SQL-2003-R
CURDATE_SYMBOL: 'CURDATE' ;
CURRENT_SYMBOL: 'CURRENT' ;
CURRENT_TIMESTAMP_SYMBOL: 'CURRENT_TIMESTAMP' ;
CURRENT_USER_SYMBOL: 'CURRENT_USER' ;
CURSOR_SYMBOL: 'CURSOR' ;
CURSOR_NAME_SYMBOL: 'CURSOR_NAME'; // SQL-2003-N
CURTIME_SYMBOL: 'CURTIME' ;
DATABASE_SYMBOL: 'DATABASE';
DATABASES_SYMBOL: 'DATABASES';
DATAFILE_SYMBOL: 'DATAFILE';
DATA_SYMBOL: 'DATA'; // SQL-2003-N
DATETIME_SYMBOL: 'DATETIME';
DATE_ADD_SYMBOL: 'DATE_ADD' ;
DATE_ADD_INTERVAL_SYMBOL: 'DATE_ADD_INTERVAL'; // MYSQL-FUNC
DATE_SUB_SYMBOL: 'DATE_SUB' ;
DATE_SUB_INTERVAL_SYMBOL: 'DATE_SUB_INTERVAL'; // MYSQL-FUNC
DATE_SYMBOL: 'DATE'; // SQL-2003-R
DAYOFMONTH_SYMBOL: 'DAYOFMONTH' ;
DAY_HOUR_SYMBOL: 'DAY_HOUR';
DAY_MICROSECOND_SYMBOL: 'DAY_MICROSECOND' ;
DAY_MINUTE_SYMBOL: 'DAY_MINUTE';
DAY_SECOND_SYMBOL: 'DAY_SECOND';
DAY_SYMBOL: 'DAY'; // SQL-2003-R
DEALLOCATE_SYMBOL: 'DEALLOCATE'; // SQL-2003-R
DEC_SYMBOL: 'DEC' ;
DECIMAL_NUM_SYMBOL: 'DECIMAL_NUM';
DECIMAL_SYMBOL: 'DECIMAL'; // SQL-2003-R
DECLARE_SYMBOL: 'DECLARE' ;
DEFAULT_SYMBOL: 'DEFAULT'; // SQL-2003-R
DEFAULT_AUTH_SYMBOL: 'DEFAULT_AUTH' ;
DEFINER_SYMBOL: 'DEFINER';
DELAYED_SYMBOL: 'DELAYED';
DELAY_KEY_WRITE_SYMBOL: 'DELAY_KEY_WRITE';
DELETE_SYMBOL: 'DELETE'; // SQL-2003-R
DESC_SYMBOL: 'DESC'; // SQL-2003-N
DESCRIBE_SYMBOL: 'DESCRIBE'; // SQL-2003-R
DES_KEY_FILE_SYMBOL: 'DES_KEY_FILE';
DETERMINISTIC_SYMBOL: 'DETERMINISTIC' ;
DIAGNOSTICS_SYMBOL: 'DIAGNOSTICS' ;
DIRECTORY_SYMBOL: 'DIRECTORY';
DISABLE_SYMBOL: 'DISABLE';
DISCARD_SYMBOL: 'DISCARD';
DISK_SYMBOL: 'DISK';
DISTINCT_SYMBOL: 'DISTINCT'; // SQL-2003-R
DISTINCTROW_SYMBOL: 'DISTINCTROW' ;
DIV_SYMBOL: 'DIV' ;
DOUBLE_SYMBOL: 'DOUBLE'; // SQL-2003-R
DO_SYMBOL: 'DO';
DROP_SYMBOL: 'DROP'; // SQL-2003-R
DUAL_SYMBOL: 'DUAL' ;
DUMPFILE_SYMBOL: 'DUMPFILE';
DUPLICATE_SYMBOL: 'DUPLICATE';
DYNAMIC_SYMBOL: 'DYNAMIC'; // SQL-2003-R
EACH_SYMBOL: 'EACH' ;
ELSE_SYMBOL: 'ELSE'; // SQL-2003-R
ELSEIF_SYMBOL: 'ELSEIF' ;
ENABLE_SYMBOL: 'ENABLE';
ENCLOSED_SYMBOL: 'ENCLOSED';
END_SYMBOL: 'END'; // SQL-2003-R
ENDS_SYMBOL: 'ENDS';
END_OF_INPUT_SYMBOL: 'END_OF_INPUT'; // INTERNAL
ENGINES_SYMBOL: 'ENGINES';
ENGINE_SYMBOL: 'ENGINE';
ENUM_SYMBOL: 'ENUM';
ERROR_SYMBOL: 'ERROR';
ERRORS_SYMBOL: 'ERRORS';
ESCAPED_SYMBOL: 'ESCAPED';
ESCAPE_SYMBOL: 'ESCAPE'; // SQL-2003-R
EVENTS_SYMBOL: 'EVENTS';
EVENT_SYMBOL: 'EVENT';
EVERY_SYMBOL: 'EVERY'; // SQL-2003-N
EXCHANGE_SYMBOL: 'EXCHANGE' ;
EXECUTE_SYMBOL: 'EXECUTE'; // SQL-2003-R
EXISTS_SYMBOL: 'EXISTS'; // SQL-2003-R
EXIT_SYMBOL: 'EXIT' ;
EXPANSION_SYMBOL: 'EXPANSION';
EXPIRE_SYMBOL: 'EXPIRE' ;
EXPLAIN_SYMBOL: 'EXPLAIN' ;
EXPORT_SYMBOL: 'EXPORT' ;
EXTENDED_SYMBOL: 'EXTENDED';
EXTENT_SIZE_SYMBOL: 'EXTENT_SIZE';
EXTRACT_SYMBOL: 'EXTRACT' ;
FALSE_SYMBOL: 'FALSE' ;
FAST_SYMBOL: 'FAST';
FAULTS_SYMBOL: 'FAULTS';
FETCH_SYMBOL: 'FETCH' ;
FIELDS_SYMBOL: 'FIELDS' ;
FILE_SYMBOL: 'FILE';
FILE_BLOCK_SIZE_SYMBOL: 'FILE_BLOCK_SIZE' ;
FILTER_SYMBOL: 'FILTER' ;
FIRST_SYMBOL: 'FIRST'; // SQL-2003-N
FIXED_SYMBOL: 'FIXED';
FLOAT4_SYMBOL: 'FLOAT4' ;
FLOAT8_SYMBOL: 'FLOAT8' ;
FLOAT_SYMBOL: 'FLOAT'; // SQL-2003-R
FLUSH_SYMBOL: 'FLUSH';
FOLLOWS_SYMBOL: 'FOLLOWS' ;
FORCE_SYMBOL: 'FORCE' ;
FOREIGN_SYMBOL: 'FOREIGN'; // SQL-2003-R
FOR_SYMBOL: 'FOR'; // SQL-2003-R
FORMAT_SYMBOL: 'FORMAT' ;
FOUND_SYMBOL: 'FOUND'; // SQL-2003-R
FRAC_SECOND_SYMBOL: 'FRAC_SECOND' ;
FROM_SYMBOL: 'FROM';
FULL_SYMBOL: 'FULL'; // SQL-2003-R
FULLTEXT_SYMBOL: 'FULLTEXT';
FUNCTION_SYMBOL: 'FUNCTION'; // SQL-2003-R
GET_SYMBOL: 'GET' ;
GENERAL_SYMBOL: 'GENERAL' ;
GENERATED_SYMBOL: 'GENERATED' ;
GROUP_REPLICATION_SYMBOL: 'GROUP_REPLICATION' ;
GEOMETRYCOLLECTION_SYMBOL: 'GEOMETRYCOLLECTION';
GEOMETRY_SYMBOL: 'GEOMETRY';
GET_FORMAT_SYMBOL: 'GET_FORMAT'; // MYSQL-FUNC
GLOBAL_SYMBOL: 'GLOBAL'; // SQL-2003-R
GRANT_SYMBOL: 'GRANT'; // SQL-2003-R
GRANTS_SYMBOL: 'GRANTS';
GROUP_SYMBOL: 'GROUP'; // SQL-2003-R
GROUP_CONCAT_SYMBOL: 'GROUP_CONCAT' ;
HANDLER_SYMBOL: 'HANDLER';
HASH_SYMBOL: 'HASH';
HAVING_SYMBOL: 'HAVING'; // SQL-2003-R
HELP_SYMBOL: 'HELP';
HIGH_PRIORITY_SYMBOL: 'HIGH_PRIORITY';
HOST_SYMBOL: 'HOST';
HOSTS_SYMBOL: 'HOSTS';
HOUR_MICROSECOND_SYMBOL: 'HOUR_MICROSECOND' ;
HOUR_MINUTE_SYMBOL: 'HOUR_MINUTE';
HOUR_SECOND_SYMBOL: 'HOUR_SECOND';
HOUR_SYMBOL: 'HOUR'; // SQL-2003-R
IDENTIFIED_SYMBOL: 'IDENTIFIED';
IF_SYMBOL: 'IF';
IGNORE_SYMBOL: 'IGNORE';
IGNORE_SERVER_IDS_SYMBOL: 'IGNORE_SERVER_IDS' ;
IMPORT_SYMBOL: 'IMPORT';
INDEXES_SYMBOL: 'INDEXES';
INDEX_SYMBOL: 'INDEX';
INFILE_SYMBOL: 'INFILE';
INITIAL_SIZE_SYMBOL: 'INITIAL_SIZE';
INNER_SYMBOL: 'INNER'; // SQL-2003-R
INNODB_SYMBOL: 'INNODB' ;
INOUT_SYMBOL: 'INOUT' ;
INSENSITIVE_SYMBOL: 'INSENSITIVE' ;
INSERT_SYMBOL: 'INSERT'; // SQL-2003-R
INSERT_METHOD_SYMBOL: 'INSERT_METHOD';
INSTALL_SYMBOL: 'INSTALL';
INTEGER_SYMBOL: 'INTEGER' ;
INTERVAL_SYMBOL: 'INTERVAL'; // SQL-2003-R
INTO_SYMBOL: 'INTO'; // SQL-2003-R
INT_SYMBOL: 'INT'; // SQL-2003-R
INVOKER_SYMBOL: 'INVOKER';
IN_SYMBOL: 'IN'; // SQL-2003-R
IO_AFTER_GTIDS_SYMBOL: 'IO_AFTER_GTIDS' ;
IO_BEFORE_GTIDS_SYMBOL: 'IO_BEFORE_GTIDS' ;
IO_THREAD_SYMBOL: 'IO_THREAD' ;
IO_SYMBOL: 'IO';
IPC_SYMBOL: 'IPC';
IS_SYMBOL: 'IS'; // SQL-2003-R
ISOLATION_SYMBOL: 'ISOLATION'; // SQL-2003-R
ISSUER_SYMBOL: 'ISSUER';
ITERATE_SYMBOL: 'ITERATE' ;
JOIN_SYMBOL: 'JOIN'; // SQL-2003-R
JSON_SYMBOL: 'JSON' ;
KEYS_SYMBOL: 'KEYS';
KEY_BLOCK_SIZE_SYMBOL: 'KEY_BLOCK_SIZE';
KEY_SYMBOL: 'KEY'; // SQL-2003-N
KILL_SYMBOL: 'KILL';
LANGUAGE_SYMBOL: 'LANGUAGE'; // SQL-2003-R
LAST_SYMBOL: 'LAST'; // SQL-2003-N
LEADING_SYMBOL: 'LEADING'; // SQL-2003-R
LEAVES_SYMBOL: 'LEAVES' ;
LEAVE_SYMBOL: 'LEAVE';
LEFT_SYMBOL: 'LEFT'; // SQL-2003-R
LESS_SYMBOL: 'LESS';
LEVEL_SYMBOL: 'LEVEL';
LIKE_SYMBOL: 'LIKE'; // SQL-2003-R
LIMIT_SYMBOL: 'LIMIT';
LINEAR_SYMBOL: 'LINEAR' ;
LINES_SYMBOL: 'LINES';
LINESTRING_SYMBOL: 'LINESTRING';
LIST_SYMBOL: 'LIST';
LOAD_SYMBOL: 'LOAD';
LOCALTIME_SYMBOL: 'LOCALTIME' ;
LOCALTIMESTAMP_SYMBOL: 'LOCALTIMESTAMP' ;
LOCAL_SYMBOL: 'LOCAL'; // SQL-2003-R
LOCATOR_SYMBOL: 'LOCATOR'; // SQL-2003-N
LOCKS_SYMBOL: 'LOCKS';
LOCK_SYMBOL: 'LOCK';
LOGFILE_SYMBOL: 'LOGFILE';
LOGS_SYMBOL: 'LOGS';
LONGBLOB_SYMBOL: 'LONGBLOB';
LONGTEXT_SYMBOL: 'LONGTEXT';
LONG_NUM_SYMBOL: 'LONG_NUM';
LONG_SYMBOL: 'LONG';
LOOP_SYMBOL: 'LOOP' ;
LOW_PRIORITY_SYMBOL: 'LOW_PRIORITY';
MASTER_AUTO_POSITION_SYMBOL: 'MASTER_AUTO_POSITION' ;
MASTER_BIND_SYMBOL: 'MASTER_BIND' ;
MASTER_CONNECT_RETRY_SYMBOL: 'MASTER_CONNECT_RETRY';
MASTER_DELAY_SYMBOL: 'MASTER_DELAY' ;
MASTER_HOST_SYMBOL: 'MASTER_HOST';
MASTER_LOG_FILE_SYMBOL: 'MASTER_LOG_FILE';
MASTER_LOG_POS_SYMBOL: 'MASTER_LOG_POS';
MASTER_PASSWORD_SYMBOL: 'MASTER_PASSWORD';
MASTER_PORT_SYMBOL: 'MASTER_PORT';
MASTER_RETRY_COUNT_SYMBOL: 'MASTER_RETRY_COUNT' ;
MASTER_SERVER_ID_SYMBOL: 'MASTER_SERVER_ID';
MASTER_SSL_CAPATH_SYMBOL: 'MASTER_SSL_CAPATH';
MASTER_SSL_CA_SYMBOL: 'MASTER_SSL_CA';
MASTER_SSL_CERT_SYMBOL: 'MASTER_SSL_CERT';
MASTER_SSL_CIPHER_SYMBOL: 'MASTER_SSL_CIPHER';
MASTER_SSL_CRL_SYMBOL: 'MASTER_SSL_CRL' ;
MASTER_SSL_CRLPATH_SYMBOL: 'MASTER_SSL_CRLPATH' ;
MASTER_SSL_KEY_SYMBOL: 'MASTER_SSL_KEY';
MASTER_SSL_SYMBOL: 'MASTER_SSL';
MASTER_SSL_VERIFY_SERVER_CERT_SYMBOL: 'MASTER_SSL_VERIFY_SERVER_CERT' ;
MASTER_SYMBOL: 'MASTER';
MASTER_USER_SYMBOL: 'MASTER_USER';
MASTER_HEARTBEAT_PERIOD_SYMBOL: 'MASTER_HEARTBEAT_PERIOD' ;
MATCH_SYMBOL: 'MATCH'; // SQL-2003-R
MAX_CONNECTIONS_PER_HOUR_SYMBOL: 'MAX_CONNECTIONS_PER_HOUR';
MAX_QUERIES_PER_HOUR_SYMBOL: 'MAX_QUERIES_PER_HOUR';
MAX_ROWS_SYMBOL: 'MAX_ROWS';
MAX_SIZE_SYMBOL: 'MAX_SIZE';
MAX_STATEMENT_TIME_SYMBOL: 'MAX_STATEMENT_TIME' ;
MAX_SYMBOL: 'MAX' ;
MAX_UPDATES_PER_HOUR_SYMBOL: 'MAX_UPDATES_PER_HOUR';
MAX_USER_CONNECTIONS_SYMBOL: 'MAX_USER_CONNECTIONS';
MAXVALUE_SYMBOL: 'MAXVALUE' ;
MEDIUMBLOB_SYMBOL: 'MEDIUMBLOB';
MEDIUMINT_SYMBOL: 'MEDIUMINT';
MEDIUMTEXT_SYMBOL: 'MEDIUMTEXT';
MEDIUM_SYMBOL: 'MEDIUM';
MEMORY_SYMBOL: 'MEMORY';
MERGE_SYMBOL: 'MERGE'; // SQL-2003-R
MESSAGE_TEXT_SYMBOL: 'MESSAGE_TEXT'; // SQL-2003-N
MICROSECOND_SYMBOL: 'MICROSECOND'; // MYSQL-FUNC
MID_SYMBOL: 'MID' ;
MIDDLEINT_SYMBOL: 'MIDDLEINT' ;
MIGRATE_SYMBOL: 'MIGRATE';
MINUTE_MICROSECOND_SYMBOL: 'MINUTE_MICROSECOND' ;
MINUTE_SECOND_SYMBOL: 'MINUTE_SECOND';
MINUTE_SYMBOL: 'MINUTE'; // SQL-2003-R
MIN_ROWS_SYMBOL: 'MIN_ROWS';
MIN_SYMBOL: 'MIN' ;
MODE_SYMBOL: 'MODE' ;
MODIFIES_SYMBOL: 'MODIFIES' ;
MODIFY_SYMBOL: 'MODIFY';
MOD_SYMBOL: 'MOD'; // SQL-2003-N
MONTH_SYMBOL: 'MONTH'; // SQL-2003-R
MULTILINESTRING_SYMBOL: 'MULTILINESTRING';
MULTIPOINT_SYMBOL: 'MULTIPOINT';
MULTIPOLYGON_SYMBOL: 'MULTIPOLYGON';
MUTEX_SYMBOL: 'MUTEX';
MYSQL_ERRNO_SYMBOL: 'MYSQL_ERRNO';
NAMES_SYMBOL: 'NAMES'; // SQL-2003-N
NAME_SYMBOL: 'NAME'; // SQL-2003-N
NATIONAL_SYMBOL: 'NATIONAL'; // SQL-2003-R
NATURAL_SYMBOL: 'NATURAL'; // SQL-2003-R
NCHAR_STRING_SYMBOL: 'NCHAR_STRING';
NCHAR_SYMBOL: 'NCHAR'; // SQL-2003-R
NDB_SYMBOL: 'NDB' ;
NDBCLUSTER_SYMBOL: 'NDBCLUSTER';
NEG_SYMBOL: 'NEG';
NEVER_SYMBOL: 'NEVER' ;
NEW_SYMBOL: 'NEW'; // SQL-2003-R
NEXT_SYMBOL: 'NEXT'; // SQL-2003-N
NODEGROUP_SYMBOL: 'NODEGROUP';
NONE_SYMBOL: 'NONE'; // SQL-2003-R
NONBLOCKING_SYMBOL: 'NONBLOCKING' ;
NOT_SYMBOL: 'NOT' ;
NOW_SYMBOL: 'NOW' ;
NO_SYMBOL: 'NO'; // SQL-2003-R
NO_WAIT_SYMBOL: 'NO_WAIT';
NO_WRITE_TO_BINLOG_SYMBOL: 'NO_WRITE_TO_BINLOG' ;
NULL_SYMBOL: 'NULL'; // SQL-2003-R
NUMBER_SYMBOL: 'NUMBER' ;
NUMERIC_SYMBOL: 'NUMERIC'; // SQL-2003-R
NVARCHAR_SYMBOL: 'NVARCHAR';
OFFLINE_SYMBOL: 'OFFLINE';
OFFSET_SYMBOL: 'OFFSET';
OLD_PASSWORD_SYMBOL: 'OLD_PASSWORD' ;
ON_SYMBOL: 'ON'; // SQL-2003-R
ONE_SHOT_SYMBOL: 'ONE_SHOT' ;
ONE_SYMBOL: 'ONE';
ONLINE_SYMBOL: 'ONLINE';
ONLY_SYMBOL: 'ONLY' ;
OPEN_SYMBOL: 'OPEN'; // SQL-2003-R
OPTIMIZE_SYMBOL: 'OPTIMIZE';
OPTIMIZER_COSTS_SYMBOL: 'OPTIMIZER_COSTS' ;
OPTIONS_SYMBOL: 'OPTIONS';
OPTION_SYMBOL: 'OPTION'; // SQL-2003-N
OPTIONALLY_SYMBOL: 'OPTIONALLY';
ORDER_SYMBOL: 'ORDER'; // SQL-2003-R
OR_SYMBOL: 'OR'; // SQL-2003-R
OUTER_SYMBOL: 'OUTER';
OUTFILE_SYMBOL: 'OUTFILE';
OUT_SYMBOL: 'OUT' ;
OWNER_SYMBOL: 'OWNER';
PACK_KEYS_SYMBOL: 'PACK_KEYS';
PAGE_SYMBOL: 'PAGE';
PARSER_SYMBOL: 'PARSER';
// PARSE_GCOL_EXPR_SYMBOL: 'PARSE_GCOL_EXPR' { $type = TYPE_FROM_VERSION(50707, $type); };
PARTIAL_SYMBOL: 'PARTIAL'; // SQL-2003-N
PARTITIONING_SYMBOL: 'PARTITIONING';
PARTITIONS_SYMBOL: 'PARTITIONS';
PARTITION_SYMBOL: 'PARTITION' ;
PASSWORD_SYMBOL: 'PASSWORD';
PHASE_SYMBOL: 'PHASE';
PLUGINS_SYMBOL: 'PLUGINS';
PLUGIN_DIR_SYMBOL: 'PLUGIN_DIR' ;
PLUGIN_SYMBOL: 'PLUGIN';
POINT_SYMBOL: 'POINT';
POLYGON_SYMBOL: 'POLYGON';
PORT_SYMBOL: 'PORT';
POSITION_SYMBOL: 'POSITION' ;
PRECEDES_SYMBOL: 'PRECEDES' ;
PRECISION_SYMBOL: 'PRECISION'; // SQL-2003-R
PREPARE_SYMBOL: 'PREPARE'; // SQL-2003-R
PRESERVE_SYMBOL: 'PRESERVE';
PREV_SYMBOL: 'PREV';
PRIMARY_SYMBOL: 'PRIMARY'; // SQL-2003-R
PRIVILEGES_SYMBOL: 'PRIVILEGES'; // SQL-2003-N
PROCEDURE_SYMBOL: 'PROCEDURE'; // SQL-2003-R
PROCESS_SYMBOL: 'PROCESS';
PROCESSLIST_SYMBOL: 'PROCESSLIST';
PROFILE_SYMBOL: 'PROFILE';
PROFILES_SYMBOL: 'PROFILES';
PROXY_SYMBOL: 'PROXY' ;
PURGE_SYMBOL: 'PURGE';
QUARTER_SYMBOL: 'QUARTER';
QUERY_SYMBOL: 'QUERY';
QUICK_SYMBOL: 'QUICK';
RANGE_SYMBOL: 'RANGE' ;
READS_SYMBOL: 'READS' ;
READ_ONLY_SYMBOL: 'READ_ONLY' ;
READ_SYMBOL: 'READ'; // SQL-2003-N
READ_WRITE_SYMBOL: 'READ_WRITE' ;
REAL_SYMBOL: 'REAL'; // SQL-2003-R
REBUILD_SYMBOL: 'REBUILD';
RECOVER_SYMBOL: 'RECOVER';
REDOFILE_SYMBOL: 'REDOFILE';
REDO_BUFFER_SIZE_SYMBOL: 'REDO_BUFFER_SIZE';
REDUNDANT_SYMBOL: 'REDUNDANT';
REFERENCES_SYMBOL: 'REFERENCES'; // SQL-2003-R
REGEXP_SYMBOL: 'REGEXP';
RELAY_SYMBOL: 'RELAY';
RELAYLOG_SYMBOL: 'RELAYLOG';
RELAY_LOG_FILE_SYMBOL: 'RELAY_LOG_FILE';
RELAY_LOG_POS_SYMBOL: 'RELAY_LOG_POS';
RELAY_THREAD_SYMBOL: 'RELAY_THREAD';
RELEASE_SYMBOL: 'RELEASE' ;
RELOAD_SYMBOL: 'RELOAD';
REMOVE_SYMBOL: 'REMOVE';
RENAME_SYMBOL: 'RENAME';
REORGANIZE_SYMBOL: 'REORGANIZE';
REPAIR_SYMBOL: 'REPAIR';
REPEATABLE_SYMBOL: 'REPEATABLE'; // SQL-2003-N
REPEAT_SYMBOL: 'REPEAT' ;
REPLACE_SYMBOL: 'REPLACE'; // MYSQL-FUNC
REPLICATION_SYMBOL: 'REPLICATION';
REPLICATE_DO_DB_SYMBOL: 'REPLICATE_DO_DB' ;
REPLICATE_IGNORE_DB_SYMBOL: 'REPLICATE_IGNORE_DB' ;
REPLICATE_DO_TABLE_SYMBOL: 'REPLICATE_DO_TABLE' ;
REPLICATE_IGNORE_TABLE_SYMBOL: 'REPLICATE_IGNORE_TABLE' ;
REPLICATE_WILD_DO_TABLE_SYMBOL: 'REPLICATE_WILD_DO_TABLE' ;
REPLICATE_WILD_IGNORE_TABLE_SYMBOL: 'REPLICATE_WILD_IGNORE_TABLE' ;
REPLICATE_REWRITE_DB_SYMBOL: 'REPLICATE_REWRITE_DB' ;
REQUIRE_SYMBOL: 'REQUIRE' ;
RESET_SYMBOL: 'RESET';
RESIGNAL_SYMBOL: 'RESIGNAL' ;
RESTORE_SYMBOL: 'RESTORE';
RESTRICT_SYMBOL: 'RESTRICT';
RESUME_SYMBOL: 'RESUME';
RETURNED_SQLSTATE_SYMBOL: 'RETURNED_SQLSTATE' ;
RETURNS_SYMBOL: 'RETURNS'; // SQL-2003-R
RETURN_SYMBOL: 'RETURN' ;
REVERSE_SYMBOL: 'REVERSE' ;
REVOKE_SYMBOL: 'REVOKE'; // SQL-2003-R
RIGHT_SYMBOL: 'RIGHT'; // SQL-2003-R
RLIKE_SYMBOL: 'RLIKE' ;
ROLLBACK_SYMBOL: 'ROLLBACK'; // SQL-2003-R
ROLLUP_SYMBOL: 'ROLLUP'; // SQL-2003-R
ROUTINE_SYMBOL: 'ROUTINE'; // SQL-2003-N
ROWS_SYMBOL: 'ROWS'; // SQL-2003-R
ROW_COUNT_SYMBOL: 'ROW_COUNT';
ROW_FORMAT_SYMBOL: 'ROW_FORMAT';
ROW_SYMBOL: 'ROW'; // SQL-2003-R
RTREE_SYMBOL: 'RTREE';
SAVEPOINT_SYMBOL: 'SAVEPOINT'; // SQL-2003-R
SCHEDULE_SYMBOL: 'SCHEDULE';
SCHEMA_SYMBOL: 'SCHEMA' ;
SCHEMA_NAME_SYMBOL: 'SCHEMA_NAME'; // SQL-2003-N
SCHEMAS_SYMBOL: 'SCHEMAS' ;
SECOND_MICROSECOND_SYMBOL: 'SECOND_MICROSECOND' ;
SECOND_SYMBOL: 'SECOND'; // SQL-2003-R
SECURITY_SYMBOL: 'SECURITY'; // SQL-2003-N
SELECT_SYMBOL: 'SELECT'; // SQL-2003-R
SENSITIVE_SYMBOL: 'SENSITIVE' ;
SEPARATOR_SYMBOL: 'SEPARATOR' ;
SERIALIZABLE_SYMBOL: 'SERIALIZABLE'; // SQL-2003-N
SERIAL_SYMBOL: 'SERIAL';
SESSION_SYMBOL: 'SESSION'; // SQL-2003-N
SERVER_SYMBOL: 'SERVER';
SERVER_OPTIONS_SYMBOL: 'SERVER_OPTIONS';
SESSION_USER_SYMBOL: 'SESSION_USER' ;
SET_SYMBOL: 'SET'; // SQL-2003-R
SET_VAR_SYMBOL: 'SET_VAR';
SHARE_SYMBOL: 'SHARE';
SHOW_SYMBOL: 'SHOW';
SHUTDOWN_SYMBOL: 'SHUTDOWN';
SIGNAL_SYMBOL: 'SIGNAL' ;
SIGNED_SYMBOL: 'SIGNED';
SIMPLE_SYMBOL: 'SIMPLE'; // SQL-2003-N
SLAVE_SYMBOL: 'SLAVE';
SLOW_SYMBOL: 'SLOW' ;
SMALLINT_SYMBOL: 'SMALLINT'; // SQL-2003-R
SNAPSHOT_SYMBOL: 'SNAPSHOT';
SOCKET_SYMBOL: 'SOCKET';
SONAME_SYMBOL: 'SONAME';
SOUNDS_SYMBOL: 'SOUNDS';
SOURCE_SYMBOL: 'SOURCE';
SPATIAL_SYMBOL: 'SPATIAL' ;
SPECIFIC_SYMBOL: 'SPECIFIC' ;
SQLEXCEPTION_SYMBOL: 'SQLEXCEPTION' ;
SQLSTATE_SYMBOL: 'SQLSTATE' ;
SQLWARNING_SYMBOL: 'SQLWARNING' ;
SQL_AFTER_GTIDS_SYMBOL: 'SQL_AFTER_GTIDS' ;
SQL_AFTER_MTS_GAPS_SYMBOL: 'SQL_AFTER_MTS_GAPS' ;
SQL_BEFORE_GTIDS_SYMBOL: 'SQL_BEFORE_GTIDS' ;
SQL_BIG_RESULT_SYMBOL: 'SQL_BIG_RESULT';
SQL_BUFFER_RESULT_SYMBOL: 'SQL_BUFFER_RESULT';
SQL_CACHE_SYMBOL: 'SQL_CACHE';
SQL_CALC_FOUND_ROWS_SYMBOL: 'SQL_CALC_FOUND_ROWS' ;
SQL_NO_CACHE_SYMBOL: 'SQL_NO_CACHE';
SQL_SMALL_RESULT_SYMBOL: 'SQL_SMALL_RESULT';
SQL_SYMBOL: 'SQL' ;
SQL_THREAD_SYMBOL: 'SQL_THREAD';
SSL_SYMBOL: 'SSL' ;
STACKED_SYMBOL: 'STACKED' ;
STARTING_SYMBOL: 'STARTING';
STARTS_SYMBOL: 'STARTS';
START_SYMBOL: 'START'; // SQL-2003-R
STATS_AUTO_RECALC_SYMBOL: 'STATS_AUTO_RECALC' ;
STATS_PERSISTENT_SYMBOL: 'STATS_PERSISTENT' ;
STATS_SAMPLE_PAGES_SYMBOL: 'STATS_SAMPLE_PAGES' ;
STATUS_SYMBOL: 'STATUS';
STDDEV_SAMP_SYMBOL: 'STDDEV_SAMP' ;
STDDEV_SYMBOL: 'STDDEV' ;
STDDEV_POP_SYMBOL: 'STDDEV_POP' ;
STD_SYMBOL: 'STD' ;
STOP_SYMBOL: 'STOP';
STORAGE_SYMBOL: 'STORAGE';
STORED_SYMBOL: 'STORED' ;
STRAIGHT_JOIN_SYMBOL: 'STRAIGHT_JOIN';
STRING_SYMBOL: 'STRING';
SUBCLASS_ORIGIN_SYMBOL: 'SUBCLASS_ORIGIN'; // SQL-2003-N
SUBDATE_SYMBOL: 'SUBDATE' ;
SUBJECT_SYMBOL: 'SUBJECT';
SUBPARTITIONS_SYMBOL: 'SUBPARTITIONS';
SUBPARTITION_SYMBOL: 'SUBPARTITION';
SUBSTR_SYMBOL: 'SUBSTR' ;
SUBSTRING_SYMBOL: 'SUBSTRING' ;
SUM_SYMBOL: 'SUM' ;
SUPER_SYMBOL: 'SUPER';
SUSPEND_SYMBOL: 'SUSPEND';
SWAPS_SYMBOL: 'SWAPS';
SWITCHES_SYMBOL: 'SWITCHES';
SYSDATE_SYMBOL: 'SYSDATE' ;
SYSTEM_USER_SYMBOL: 'SYSTEM_USER' ;
TABLES_SYMBOL: 'TABLES';
TABLESPACE_SYMBOL: 'TABLESPACE';
TABLE_REF_PRIORITY_SYMBOL: 'TABLE_REF_PRIORITY';
TABLE_SYMBOL: 'TABLE'; // SQL-2003-R
TABLE_CHECKSUM_SYMBOL: 'TABLE_CHECKSUM';
TABLE_NAME_SYMBOL: 'TABLE_NAME'; // SQL-2003-N
TEMPORARY_SYMBOL: 'TEMPORARY'; // SQL-2003-N
TEMPTABLE_SYMBOL: 'TEMPTABLE';
TERMINATED_SYMBOL: 'TERMINATED';
TEXT_SYMBOL: 'TEXT';
THAN_SYMBOL: 'THAN';
THEN_SYMBOL: 'THEN'; // SQL-2003-R
TIMESTAMP_SYMBOL: 'TIMESTAMP'; // SQL-2003-R
TIMESTAMP_ADD_SYMBOL: 'TIMESTAMP_ADD';
TIMESTAMP_DIFF_SYMBOL: 'TIMESTAMP_DIFF';
TIME_SYMBOL: 'TIME'; // SQL-2003-R
TINYBLOB_SYMBOL: 'TINYBLOB';
TINYINT_SYMBOL: 'TINYINT';
TINYTEXT_SYMBOL: 'TINYTEXT';
TO_SYMBOL: 'TO'; // SQL-2003-R
TRAILING_SYMBOL: 'TRAILING'; // SQL-2003-R
TRANSACTION_SYMBOL: 'TRANSACTION';
TRIGGERS_SYMBOL: 'TRIGGERS' ;
TRIGGER_SYMBOL: 'TRIGGER'; // SQL-2003-R
TRIM_SYMBOL: 'TRIM' ;
TRUE_SYMBOL: 'TRUE' ;
TRUNCATE_SYMBOL: 'TRUNCATE';
TYPES_SYMBOL: 'TYPES';
TYPE_SYMBOL: 'TYPE'; // SQL-2003-N
UDF_RETURNS_SYMBOL: 'UDF_RETURNS';
UNCOMMITTED_SYMBOL: 'UNCOMMITTED'; // SQL-2003-N
UNDEFINED_SYMBOL: 'UNDEFINED';
UNDOFILE_SYMBOL: 'UNDOFILE';
UNDO_BUFFER_SIZE_SYMBOL: 'UNDO_BUFFER_SIZE';
UNDO_SYMBOL: 'UNDO' ;
UNICODE_SYMBOL: 'UNICODE';
UNINSTALL_SYMBOL: 'UNINSTALL';
UNION_SYMBOL: 'UNION'; // SQL-2003-R
UNIQUE_SYMBOL: 'UNIQUE';
UNKNOWN_SYMBOL: 'UNKNOWN'; // SQL-2003-R
UNLOCK_SYMBOL: 'UNLOCK';
UNSIGNED_SYMBOL: 'UNSIGNED';
UNTIL_SYMBOL: 'UNTIL';
UPDATE_SYMBOL: 'UPDATE'; // SQL-2003-R
UPGRADE_SYMBOL: 'UPGRADE' ;
USAGE_SYMBOL: 'USAGE'; // SQL-2003-N
USER_RESOURCES_SYMBOL: 'USER_RESOURCES';
USER_SYMBOL: 'USER'; // SQL-2003-R
USE_FRM_SYMBOL: 'USE_FRM';
USE_SYMBOL: 'USE';
USING_SYMBOL: 'USING'; // SQL-2003-R
UTC_DATE_SYMBOL: 'UTC_DATE' ;
UTC_TIMESTAMP_SYMBOL: 'UTC_TIMESTAMP' ;
UTC_TIME_SYMBOL: 'UTC_TIME' ;
VALIDATION_SYMBOL: 'VALIDATION' ;
VALUES_SYMBOL: 'VALUES'; // SQL-2003-R
VALUE_SYMBOL: 'VALUE'; // SQL-2003-R
VARBINARY_SYMBOL: 'VARBINARY';
VARCHAR_SYMBOL: 'VARCHAR'; // SQL-2003-R
VARCHARACTER_SYMBOL: 'VARCHARACTER' ;
VARIABLES_SYMBOL: 'VARIABLES';
VARIANCE_SYMBOL: 'VARIANCE' ;
VARYING_SYMBOL: 'VARYING'; // SQL-2003-R
VAR_POP_SYMBOL: 'VAR_POP' ;
VAR_SAMP_SYMBOL: 'VAR_SAMP' ;
VIEW_SYMBOL: 'VIEW'; // SQL-2003-N
VIRTUAL_SYMBOL: 'VIRTUAL' ;
WAIT_SYMBOL: 'WAIT';
WARNINGS_SYMBOL: 'WARNINGS';
WEEK_SYMBOL: 'WEEK';
WEIGHT_STRING_SYMBOL: 'WEIGHT_STRING' ;
WHEN_SYMBOL: 'WHEN'; // SQL-2003-R
WHERE_SYMBOL: 'WHERE'; // SQL-2003-R
WHILE_SYMBOL: 'WHILE' ;
WITH_SYMBOL: 'WITH'; // SQL-2003-R
WITH_CUBE_SYMBOL: 'WITH_CUBE'; // INTERNAL
WITH_ROLLUP_SYMBOL: 'WITH_ROLLUP'; // INTERNAL
WITHOUT_SYMBOL: 'WITHOUT'; // SQL-2003-R
WORK_SYMBOL: 'WORK'; // SQL-2003-N
WRAPPER_SYMBOL: 'WRAPPER';
WRITE_SYMBOL: 'WRITE'; // SQL-2003-N
X509_SYMBOL: 'X509';
XA_SYMBOL: 'XA';
XID_SYMBOL: 'XID' ;
XML_SYMBOL: 'XML';
XOR_SYMBOL: 'XOR' ;
YEAR_MONTH_SYMBOL: 'YEAR_MONTH';
YEAR_SYMBOL: 'YEAR'; // SQL-2003-R
ZEROFILL_SYMBOL: 'ZEROFILL';
*/

View File

@ -56,6 +56,7 @@ conquer:
// Unused rule to demonstrate some of the special features. // Unused rule to demonstrate some of the special features.
unused[double input] returns [double calculated] locals [size_t _a, double _b, int _c] @init{ doInit(); } @after { doAfter(); } : unused[double input] returns [double calculated] locals [size_t _a, double _b, int _c] @init{ doInit(); } @after { doAfter(); } :
stat
; ;
catch [...] { catch [...] {
// Replaces the standard exception handling. // Replaces the standard exception handling.

View File

@ -1,5 +0,0 @@
grammar Test;
main: ID ID;
ID: [a-z]+;

View File

@ -26,11 +26,6 @@ set -o errexit
# antlr jar over and over again. # antlr jar over and over again.
CLASSPATH=../../../tool/resources/:ST-4.0.8.jar:../../../tool/target/classes:../../../runtime/Java/target/classes:../../../../antlr3/runtime/Java/target/classes CLASSPATH=../../../tool/resources/:ST-4.0.8.jar:../../../tool/target/classes:../../../runtime/Java/target/classes:../../../../antlr3/runtime/Java/target/classes
# Minimal lexer + parser.
java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Cpp -no-listener -no-visitor -o generated/ -package antlrcpptest Test.g4
java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Java -listener -visitor -o generated/ Test.g4
# A more complete lexer/parser set + listeners.
java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Cpp -listener -visitor -o generated/ -package antlrcpptest TLexer.g4 TParser.g4 java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Cpp -listener -visitor -o generated/ -package antlrcpptest TLexer.g4 TParser.g4
#java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Cpp -listener -visitor -o generated/ -package antlrcpptest -XdbgST TLexer.g4 TParser.g4 #java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Cpp -listener -visitor -o generated/ -package antlrcpptest -XdbgST TLexer.g4 TParser.g4
#java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Java -listener -visitor -o generated/ TLexer.g4 TParser.g4 #java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Java -listener -visitor -o generated/ TLexer.g4 TParser.g4

View File

@ -77,7 +77,7 @@ namespace runtime {
/// the reporting of an error. It is null in the case where /// the reporting of an error. It is null in the case where
/// the parser was able to recover in line without exiting the /// the parser was able to recover in line without exiting the
/// surrounding rule. </param> /// surrounding rule. </param>
virtual void syntaxError(IRecognizer *recognizer, TokenRef offendingSymbol, size_t line, int charPositionInLine, virtual void syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol, size_t line, int charPositionInLine,
const std::wstring &msg, std::exception_ptr e) = 0; const std::wstring &msg, std::exception_ptr e) = 0;
/// <summary> /// <summary>
@ -107,7 +107,7 @@ namespace runtime {
/// <param name="ambigAlts"> the potentially ambiguous alternatives </param> /// <param name="ambigAlts"> the potentially ambiguous alternatives </param>
/// <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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact,
const antlrcpp::BitSet &ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) = 0; const antlrcpp::BitSet &ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) = 0;
/// <summary> /// <summary>
@ -130,7 +130,7 @@ namespace runtime {
/// represented in {@code configs}. </param> /// represented in {@code configs}. </param>
/// <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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
const antlrcpp::BitSet &conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) = 0; const antlrcpp::BitSet &conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) = 0;
/// <summary> /// <summary>
@ -160,7 +160,7 @@ namespace runtime {
/// <param name="prediction"> the unambiguous result of the full-context prediction </param> /// <param name="prediction"> the unambiguous result of the full-context prediction </param>
/// <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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
int prediction, std::shared_ptr<atn::ATNConfigSet> configs) = 0; int prediction, std::shared_ptr<atn::ATNConfigSet> configs) = 0;
}; };

View File

@ -31,6 +31,8 @@
#pragma once #pragma once
#include "Token.h"
namespace org { namespace org {
namespace antlr { namespace antlr {
namespace v4 { namespace v4 {
@ -77,7 +79,7 @@ namespace runtime {
/// <param name="recognizer"> the parser instance </param> /// <param name="recognizer"> the parser instance </param>
/// <exception cref="RecognitionException"> if the error strategy was not able to /// <exception cref="RecognitionException"> if the error strategy was not able to
/// recover from the unexpected input symbol </exception> /// recover from the unexpected input symbol </exception>
virtual TokenRef recoverInline(Parser *recognizer) = 0; virtual Token::Ref recoverInline(Parser *recognizer) = 0;
/// <summary> /// <summary>
/// This method is called to recover from exception {@code e}. This method is /// This method is called to recover from exception {@code e}. This method is

View File

@ -165,7 +165,7 @@ std::string ANTLRInputStream::getSourceName() const {
return name; return name;
} }
std::wstring ANTLRInputStream::toString() { std::wstring ANTLRInputStream::toString() const {
return data; return data;
} }

View File

@ -90,7 +90,7 @@ namespace runtime {
virtual void seek(size_t index) override; virtual void seek(size_t index) override;
virtual std::wstring getText(const misc::Interval &interval) override; virtual std::wstring getText(const misc::Interval &interval) override;
virtual std::string getSourceName() const override; virtual std::string getSourceName() const override;
virtual std::wstring toString(); virtual std::wstring toString() const override;
private: private:
void InitializeInstanceFields(); void InitializeInstanceFields();

View File

@ -40,7 +40,7 @@ using namespace org::antlr::v4::runtime;
void BailErrorStrategy::recover(Parser *recognizer, const RecognitionException &e) { void BailErrorStrategy::recover(Parser *recognizer, const RecognitionException &e) {
std::exception_ptr exception = std::make_exception_ptr(e); std::exception_ptr exception = std::make_exception_ptr(e);
ParserRuleContextRef context = recognizer->getContext(); ParserRuleContext::Ref context = recognizer->getContext();
do { do {
context->exception = exception; context->exception = exception;
if (context->getParent().expired()) if (context->getParent().expired())
@ -55,11 +55,11 @@ void BailErrorStrategy::recover(Parser *recognizer, const RecognitionException &
} }
} }
TokenRef BailErrorStrategy::recoverInline(Parser *recognizer) { Token::Ref BailErrorStrategy::recoverInline(Parser *recognizer) {
InputMismatchException e(recognizer); InputMismatchException e(recognizer);
std::exception_ptr exception = std::make_exception_ptr(e); std::exception_ptr exception = std::make_exception_ptr(e);
ParserRuleContextRef context = recognizer->getContext(); ParserRuleContext::Ref context = recognizer->getContext();
do { do {
context->exception = exception; context->exception = exception;
if (context->getParent().expired()) if (context->getParent().expired())

View File

@ -57,7 +57,7 @@ namespace runtime {
/// Make sure we don't attempt to recover inline; if the parser /// Make sure we don't attempt to recover inline; if the parser
/// successfully recovers, it won't throw an exception. /// successfully recovers, it won't throw an exception.
/// </summary> /// </summary>
virtual TokenRef recoverInline(Parser *recognizer) override; virtual Token::Ref recoverInline(Parser *recognizer) override;
/// <summary> /// <summary>
/// Make sure we don't attempt to recover from problems in subrules. </summary> /// Make sure we don't attempt to recover from problems in subrules. </summary>

View File

@ -34,18 +34,18 @@
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
void BaseErrorListener::syntaxError(IRecognizer *recognizer, TokenRef offendingSymbol, size_t line, int charPositionInLine, void BaseErrorListener::syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol, size_t line, int charPositionInLine,
const std::wstring &msg, std::exception_ptr e) { const std::wstring &msg, std::exception_ptr e) {
} }
void BaseErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, void BaseErrorListener::reportAmbiguity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
bool exact, const antlrcpp::BitSet &ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) { bool exact, const 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, const dfa::DFA &dfa, size_t startIndex,
size_t stopIndex, const antlrcpp::BitSet &conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) { size_t stopIndex, const 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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
int prediction, std::shared_ptr<atn::ATNConfigSet> configs) { int prediction, std::shared_ptr<atn::ATNConfigSet> configs) {
} }

View File

@ -44,16 +44,16 @@ namespace runtime {
class BaseErrorListener : public ANTLRErrorListener { class BaseErrorListener : public ANTLRErrorListener {
virtual void syntaxError(IRecognizer *recognizer, TokenRef offendingSymbol, size_t line, int charPositionInLine, virtual void syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol, size_t line, int charPositionInLine,
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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact,
const antlrcpp::BitSet &ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) override; const 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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
const antlrcpp::BitSet &conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) override; const 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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
int prediction, std::shared_ptr<atn::ATNConfigSet> configs) override; int prediction, std::shared_ptr<atn::ATNConfigSet> configs) override;
}; };

View File

@ -85,6 +85,8 @@ void BufferedTokenStream::consume() {
} }
bool BufferedTokenStream::sync(size_t i) { bool BufferedTokenStream::sync(size_t i) {
if (i + 1 < _tokens.size())
return true;
size_t n = i - _tokens.size() + 1; // how many more elements we need? size_t n = i - _tokens.size() + 1; // how many more elements we need?
if (n > 0) { if (n > 0) {
@ -101,7 +103,7 @@ size_t BufferedTokenStream::fetch(size_t n) {
} }
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
TokenRef t = _tokenSource->nextToken(); Token::Ref t = _tokenSource->nextToken();
if (is<WritableToken>(t)) { if (is<WritableToken>(t)) {
(std::dynamic_pointer_cast<WritableToken>(t))->setTokenIndex((int)_tokens.size()); (std::dynamic_pointer_cast<WritableToken>(t))->setTokenIndex((int)_tokens.size());
} }
@ -115,7 +117,7 @@ size_t BufferedTokenStream::fetch(size_t n) {
return n; return n;
} }
TokenRef BufferedTokenStream::get(size_t i) const { Token::Ref BufferedTokenStream::get(size_t i) const {
if (i >= _tokens.size()) { if (i >= _tokens.size()) {
throw IndexOutOfBoundsException(std::string("token index ") + throw IndexOutOfBoundsException(std::string("token index ") +
std::to_string(i) + std::to_string(i) +
@ -125,8 +127,8 @@ TokenRef BufferedTokenStream::get(size_t i) const {
return _tokens[i]; return _tokens[i];
} }
std::vector<TokenRef> BufferedTokenStream::get(size_t start, size_t stop) { std::vector<Token::Ref> BufferedTokenStream::get(size_t start, size_t stop) {
std::vector<TokenRef> subset; std::vector<Token::Ref> subset;
lazyInit(); lazyInit();
@ -138,7 +140,7 @@ std::vector<TokenRef> BufferedTokenStream::get(size_t start, size_t stop) {
stop = _tokens.size() - 1; stop = _tokens.size() - 1;
} }
for (size_t i = start; i <= stop; i++) { for (size_t i = start; i <= stop; i++) {
TokenRef t = _tokens[i]; Token::Ref t = _tokens[i];
if (t->getType() == EOF) { if (t->getType() == EOF) {
break; break;
} }
@ -151,23 +153,23 @@ ssize_t BufferedTokenStream::LA(ssize_t i) {
return LT(i)->getType(); return LT(i)->getType();
} }
TokenRef BufferedTokenStream::LB(size_t k) { Token::Ref BufferedTokenStream::LB(size_t k) {
if (k > _p) { if (k > _p) {
return nullptr; return nullptr;
} }
return _tokens[(size_t)(_p - k)]; return _tokens[(size_t)(_p - k)];
} }
TokenRef BufferedTokenStream::LT(ssize_t k) { Token::Ref BufferedTokenStream::LT(ssize_t k) {
lazyInit(); lazyInit();
if (k == 0) { if (k == 0) {
return nullptr; return nullptr;
} }
if (k < 0) { if (k < 0) {
return LB((size_t)-k); return LB(-k);
} }
size_t i = (size_t)((ssize_t)_p + k - 1); size_t i = _p + k - 1;
sync(i); sync(i);
if (i >= _tokens.size()) { // return EOF token if (i >= _tokens.size()) { // return EOF token
// EOF must be last token // EOF must be last token
@ -177,7 +179,7 @@ TokenRef BufferedTokenStream::LT(ssize_t k) {
return _tokens[i]; return _tokens[i];
} }
size_t BufferedTokenStream::adjustSeekIndex(size_t i) { ssize_t BufferedTokenStream::adjustSeekIndex(size_t i) {
return i; return i;
} }
@ -199,15 +201,15 @@ void BufferedTokenStream::setTokenSource(TokenSource *tokenSource) {
_needSetup = true; _needSetup = true;
} }
std::vector<TokenRef> BufferedTokenStream::getTokens() { std::vector<Token::Ref> BufferedTokenStream::getTokens() {
return _tokens; return _tokens;
} }
std::vector<TokenRef> BufferedTokenStream::getTokens(int start, int stop) { std::vector<Token::Ref> BufferedTokenStream::getTokens(int start, int stop) {
return getTokens(start, stop, std::vector<int>()); return getTokens(start, stop, std::vector<int>());
} }
std::vector<TokenRef> BufferedTokenStream::getTokens(int start, int stop, const std::vector<int> &types) { std::vector<Token::Ref> BufferedTokenStream::getTokens(int start, int stop, const std::vector<int> &types) {
lazyInit(); lazyInit();
if (start < 0 || stop >= (int)_tokens.size() || stop < 0 || start >= (int)_tokens.size()) { if (start < 0 || stop >= (int)_tokens.size() || stop < 0 || start >= (int)_tokens.size()) {
throw IndexOutOfBoundsException(std::string("start ") + throw IndexOutOfBoundsException(std::string("start ") +
@ -218,7 +220,7 @@ std::vector<TokenRef> BufferedTokenStream::getTokens(int start, int stop, const
std::to_string(_tokens.size() - 1)); std::to_string(_tokens.size() - 1));
} }
std::vector<TokenRef> filteredTokens; std::vector<Token::Ref> filteredTokens;
if (start > stop) { if (start > stop) {
return filteredTokens; return filteredTokens;
@ -226,7 +228,7 @@ std::vector<TokenRef> BufferedTokenStream::getTokens(int start, int stop, const
// list = tokens[start:stop]:{T t, t.getType() in types} // list = tokens[start:stop]:{T t, t.getType() in types}
for (size_t i = (size_t)start; i <= (size_t)stop; i++) { for (size_t i = (size_t)start; i <= (size_t)stop; i++) {
TokenRef tok = _tokens[i]; Token::Ref tok = _tokens[i];
if (types.empty() || std::find(types.begin(), types.end(), tok->getType()) != types.end()) { if (types.empty() || std::find(types.begin(), types.end(), tok->getType()) != types.end()) {
filteredTokens.push_back(tok); filteredTokens.push_back(tok);
@ -235,7 +237,7 @@ std::vector<TokenRef> BufferedTokenStream::getTokens(int start, int stop, const
return filteredTokens; return filteredTokens;
} }
std::vector<TokenRef> BufferedTokenStream::getTokens(int start, int stop, int ttype) { std::vector<Token::Ref> BufferedTokenStream::getTokens(int start, int stop, int ttype) {
std::vector<int> s; std::vector<int> s;
s.push_back(ttype); s.push_back(ttype);
return getTokens(start, stop, s); return getTokens(start, stop, s);
@ -247,7 +249,7 @@ ssize_t BufferedTokenStream::nextTokenOnChannel(size_t i, int channel) {
return -1; return -1;
} }
TokenRef token = _tokens[i]; Token::Ref token = _tokens[i];
while (token->getChannel() != channel) { while (token->getChannel() != channel) {
if (token->getType() == EOF) { if (token->getType() == EOF) {
return -1; return -1;
@ -256,13 +258,13 @@ ssize_t BufferedTokenStream::nextTokenOnChannel(size_t i, int channel) {
sync(i); sync(i);
token = _tokens[i]; token = _tokens[i];
} }
return (ssize_t)i; return i;
} }
ssize_t BufferedTokenStream::previousTokenOnChannel(size_t i, int channel) const { ssize_t BufferedTokenStream::previousTokenOnChannel(ssize_t i, int channel) const {
do { do {
if (_tokens[i]->getChannel() == channel) if (_tokens[(size_t)i]->getChannel() == channel)
return (ssize_t)i; return i;
if (i == 0) if (i == 0)
return -1; return -1;
i--; i--;
@ -270,7 +272,7 @@ ssize_t BufferedTokenStream::previousTokenOnChannel(size_t i, int channel) const
return -1; return -1;
} }
std::vector<TokenRef> BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex, int channel) { std::vector<Token::Ref> BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex, int channel) {
lazyInit(); lazyInit();
if (tokenIndex >= _tokens.size()) { if (tokenIndex >= _tokens.size()) {
throw IndexOutOfBoundsException(std::to_string(tokenIndex) + " not in 0.." + std::to_string(_tokens.size() - 1)); throw IndexOutOfBoundsException(std::to_string(tokenIndex) + " not in 0.." + std::to_string(_tokens.size() - 1));
@ -289,19 +291,19 @@ std::vector<TokenRef> BufferedTokenStream::getHiddenTokensToRight(size_t tokenIn
return filterForChannel(from, (size_t)to, channel); return filterForChannel(from, (size_t)to, channel);
} }
std::vector<TokenRef> BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex) { std::vector<Token::Ref> BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex) {
return getHiddenTokensToRight(tokenIndex, -1); return getHiddenTokensToRight(tokenIndex, -1);
} }
std::vector<TokenRef> BufferedTokenStream::getHiddenTokensToLeft(size_t tokenIndex, int channel) { std::vector<Token::Ref> BufferedTokenStream::getHiddenTokensToLeft(size_t tokenIndex, int channel) {
lazyInit(); lazyInit();
if (tokenIndex >= _tokens.size()) { if (tokenIndex >= _tokens.size()) {
throw IndexOutOfBoundsException(std::to_string(tokenIndex) + " not in 0.." + std::to_string(_tokens.size() - 1)); throw IndexOutOfBoundsException(std::to_string(tokenIndex) + " not in 0.." + std::to_string(_tokens.size() - 1));
} }
ssize_t prevOnChannel = previousTokenOnChannel(tokenIndex - 1, Lexer::DEFAULT_TOKEN_CHANNEL); ssize_t prevOnChannel = previousTokenOnChannel((ssize_t)tokenIndex - 1, Lexer::DEFAULT_TOKEN_CHANNEL);
if (prevOnChannel == (ssize_t)tokenIndex - 1) { if (prevOnChannel == (ssize_t)tokenIndex - 1) {
return std::vector<TokenRef>(); return std::vector<Token::Ref>();
} }
// if none onchannel to left, prevOnChannel=-1 then from=0 // if none onchannel to left, prevOnChannel=-1 then from=0
size_t from = (size_t)(prevOnChannel + 1); size_t from = (size_t)(prevOnChannel + 1);
@ -310,14 +312,14 @@ std::vector<TokenRef> BufferedTokenStream::getHiddenTokensToLeft(size_t tokenInd
return filterForChannel(from, to, channel); return filterForChannel(from, to, channel);
} }
std::vector<TokenRef> BufferedTokenStream::getHiddenTokensToLeft(size_t tokenIndex) { std::vector<Token::Ref> BufferedTokenStream::getHiddenTokensToLeft(size_t tokenIndex) {
return getHiddenTokensToLeft(tokenIndex, -1); return getHiddenTokensToLeft(tokenIndex, -1);
} }
std::vector<TokenRef> BufferedTokenStream::filterForChannel(size_t from, size_t to, int channel) { std::vector<Token::Ref> BufferedTokenStream::filterForChannel(size_t from, size_t to, int channel) {
std::vector<TokenRef> hidden; std::vector<Token::Ref> hidden;
for (size_t i = from; i <= to; i++) { for (size_t i = from; i <= to; i++) {
TokenRef t = _tokens[i]; Token::Ref t = _tokens[i];
if (channel == -1) { if (channel == -1) {
if (t->getChannel() != Lexer::DEFAULT_TOKEN_CHANNEL) { if (t->getChannel() != Lexer::DEFAULT_TOKEN_CHANNEL) {
hidden.push_back(t); hidden.push_back(t);
@ -359,7 +361,7 @@ std::wstring BufferedTokenStream::getText(const misc::Interval &interval) {
std::wstringstream ss; std::wstringstream ss;
for (size_t i = (size_t)start; i <= (size_t)stop; i++) { for (size_t i = (size_t)start; i <= (size_t)stop; i++) {
TokenRef t = _tokens[i]; Token::Ref t = _tokens[i];
if (t->getType() == EOF) { if (t->getType() == EOF) {
break; break;
} }
@ -372,7 +374,7 @@ std::wstring BufferedTokenStream::getText(RuleContext *ctx) {
return getText(ctx->getSourceInterval()); return getText(ctx->getSourceInterval());
} }
std::wstring BufferedTokenStream::getText(TokenRef start, TokenRef stop) { std::wstring BufferedTokenStream::getText(Token::Ref start, Token::Ref stop) {
if (start != nullptr && stop != nullptr) { if (start != nullptr && stop != nullptr) {
return getText(misc::Interval(start->getTokenIndex(), stop->getTokenIndex())); return getText(misc::Interval(start->getTokenIndex(), stop->getTokenIndex()));
} }

View File

@ -63,57 +63,57 @@ namespace runtime {
virtual size_t size() override; virtual size_t size() override;
virtual void consume() override; virtual void consume() override;
virtual TokenRef get(size_t i) const override; virtual Token::Ref get(size_t i) const override;
/// Get all tokens from start..stop inclusively. /// Get all tokens from start..stop inclusively.
virtual std::vector<TokenRef> get(size_t start, size_t stop); virtual std::vector<Token::Ref> get(size_t start, size_t stop);
virtual ssize_t LA(ssize_t i) override; virtual ssize_t LA(ssize_t i) override;
virtual TokenRef LT(ssize_t k) override; virtual Token::Ref LT(ssize_t k) override;
/// Reset this token stream by setting its token source. /// Reset this token stream by setting its token source.
virtual void setTokenSource(TokenSource *tokenSource); virtual void setTokenSource(TokenSource *tokenSource);
virtual std::vector<TokenRef> getTokens(); virtual std::vector<Token::Ref> getTokens();
virtual std::vector<TokenRef> getTokens(int start, int stop); virtual std::vector<Token::Ref> getTokens(int start, int stop);
/// <summary> /// <summary>
/// Given a start and stop index, return a List of all tokens in /// Given a start and stop index, return a List of all tokens in
/// the token type BitSet. Return null if no tokens were found. This /// the token type BitSet. Return null if no tokens were found. This
/// method looks at both on and off channel tokens. /// method looks at both on and off channel tokens.
/// </summary> /// </summary>
virtual std::vector<TokenRef> getTokens(int start, int stop, const std::vector<int> &types); virtual std::vector<Token::Ref> getTokens(int start, int stop, const std::vector<int> &types);
virtual std::vector<TokenRef> getTokens(int start, int stop, int ttype); virtual std::vector<Token::Ref> getTokens(int start, int stop, int ttype);
/// Collect all tokens on specified channel to the right of /// Collect all tokens on specified channel to the right of
/// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or
/// EOF. If channel is -1, find any non default channel token. /// EOF. If channel is -1, find any non default channel token.
virtual std::vector<TokenRef> getHiddenTokensToRight(size_t tokenIndex, int channel); virtual std::vector<Token::Ref> getHiddenTokensToRight(size_t tokenIndex, int channel);
/// <summary> /// <summary>
/// Collect all hidden tokens (any off-default channel) to the right of /// Collect all hidden tokens (any off-default channel) to the right of
/// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL
/// of EOF. /// of EOF.
/// </summary> /// </summary>
virtual std::vector<TokenRef> getHiddenTokensToRight(size_t tokenIndex); virtual std::vector<Token::Ref> getHiddenTokensToRight(size_t tokenIndex);
/// <summary> /// <summary>
/// Collect all tokens on specified channel to the left of /// Collect all tokens on specified channel to the left of
/// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.
/// If channel is -1, find any non default channel token. /// If channel is -1, find any non default channel token.
/// </summary> /// </summary>
virtual std::vector<TokenRef> getHiddenTokensToLeft(size_t tokenIndex, int channel); virtual std::vector<Token::Ref> getHiddenTokensToLeft(size_t tokenIndex, int channel);
/// <summary> /// <summary>
/// Collect all hidden tokens (any off-default channel) to the left of /// Collect all hidden tokens (any off-default channel) to the left of
/// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL.
/// </summary> /// </summary>
virtual std::vector<TokenRef> getHiddenTokensToLeft(size_t tokenIndex); virtual std::vector<Token::Ref> getHiddenTokensToLeft(size_t tokenIndex);
virtual std::string getSourceName() const override; virtual std::string getSourceName() const override;
virtual std::wstring getText() override; virtual std::wstring getText() override;
virtual std::wstring getText(const misc::Interval &interval) override; virtual std::wstring getText(const misc::Interval &interval) override;
virtual std::wstring getText(RuleContext *ctx) override; virtual std::wstring getText(RuleContext *ctx) override;
virtual std::wstring getText(TokenRef start, TokenRef stop) override; virtual std::wstring getText(Token::Ref start, Token::Ref stop) override;
/// <summary> /// <summary>
/// Get all tokens from lexer until EOF </summary> /// Get all tokens from lexer until EOF </summary>
@ -126,7 +126,7 @@ namespace runtime {
/// chunks of it later. This list captures everything so we can access /// chunks of it later. This list captures everything so we can access
/// complete input text. /// complete input text.
// ml: we own the tokens produced by the token factory. // ml: we own the tokens produced by the token factory.
std::vector<TokenRef> _tokens; std::vector<Token::Ref> _tokens;
/// <summary> /// <summary>
/// The index into <seealso cref="#tokens"/> of the current token (next token to /// The index into <seealso cref="#tokens"/> of the current token (next token to
@ -160,7 +160,7 @@ namespace runtime {
/// <returns> The actual number of elements added to the buffer. </returns> /// <returns> The actual number of elements added to the buffer. </returns>
virtual size_t fetch(size_t n); virtual size_t fetch(size_t n);
virtual TokenRef LB(size_t k); virtual Token::Ref LB(size_t k);
/// Allowed derived classes to modify the behavior of operations which change /// Allowed derived classes to modify the behavior of operations which change
/// the current stream position by adjusting the target token index of a seek /// the current stream position by adjusting the target token index of a seek
@ -173,7 +173,7 @@ namespace runtime {
/// ///
/// <param name="i"> The target token index. </param> /// <param name="i"> The target token index. </param>
/// <returns> The adjusted target token index. </returns> /// <returns> The adjusted target token index. </returns>
virtual size_t adjustSeekIndex(size_t i); virtual ssize_t adjustSeekIndex(size_t i);
void lazyInit(); void lazyInit();
virtual void setup(); virtual void setup();
@ -185,9 +185,9 @@ namespace runtime {
/// Given a starting index, return the index of the previous token on channel. /// Given a starting index, return the index of the previous token on channel.
/// Return i if tokens[i] is on channel. Return -1 if there are no tokens /// Return i if tokens[i] is on channel. Return -1 if there are no tokens
/// on channel between i and 0. /// on channel between i and 0.
virtual ssize_t previousTokenOnChannel(size_t i, int channel) const; virtual ssize_t previousTokenOnChannel(ssize_t i, int channel) const;
virtual std::vector<TokenRef> filterForChannel(size_t from, size_t to, int channel); virtual std::vector<Token::Ref> filterForChannel(size_t from, size_t to, int channel);
private: private:
bool _needSetup; bool _needSetup;
void InitializeInstanceFields(); void InitializeInstanceFields();

View File

@ -58,6 +58,8 @@ namespace runtime {
/// <exception cref="UnsupportedOperationException"> if the stream does not support /// <exception cref="UnsupportedOperationException"> if the stream does not support
/// getting the text of the specified interval </exception> /// getting the text of the specified interval </exception>
virtual std::wstring getText(const misc::Interval &interval) = 0; virtual std::wstring getText(const misc::Interval &interval) = 0;
virtual std::wstring toString() const = 0;
}; };
} // namespace runtime } // namespace runtime

View File

@ -43,29 +43,31 @@ CommonTokenStream::CommonTokenStream(TokenSource *tokenSource, int channel) : Bu
this->channel = channel; this->channel = channel;
} }
size_t CommonTokenStream::adjustSeekIndex(size_t i) { ssize_t CommonTokenStream::adjustSeekIndex(size_t i) {
// XXX ml: that code is questionable. If there is no next token on the given channel we get an invalid stream position (-1). return nextTokenOnChannel(i, channel);
return (size_t)nextTokenOnChannel(i, channel);
} }
TokenRef CommonTokenStream::LB(size_t k) { Token::Ref CommonTokenStream::LB(size_t k) {
if (k == 0 || k > _p) { if (k == 0 || k > _p) {
return TokenRef(); return Token::Ref();
} }
size_t i = _p; ssize_t i = (ssize_t)_p;
size_t n = 1; size_t n = 1;
// find k good tokens looking backwards // find k good tokens looking backwards
while (n <= k) { while (n <= k) {
// skip off-channel tokens // skip off-channel tokens
// XXX ml: also here, no error handling for -1 i = previousTokenOnChannel(i - 1, channel);
i = (size_t)previousTokenOnChannel(i - 1, channel);
n++; n++;
} }
if (i < 0) {
return nullptr;
}
return _tokens[i]; return _tokens[i];
} }
TokenRef CommonTokenStream::LT(ssize_t k) { Token::Ref CommonTokenStream::LT(ssize_t k) {
lazyInit(); lazyInit();
if (k == 0) { if (k == 0) {
return nullptr; return nullptr;
@ -74,13 +76,12 @@ TokenRef CommonTokenStream::LT(ssize_t k) {
return LB((size_t)-k); return LB((size_t)-k);
} }
size_t i = _p; size_t i = _p;
size_t n = 1; // we know tokens[p] is a good one ssize_t n = 1; // we know tokens[p] is a good one
// find k good tokens // find k good tokens
while (n < (size_t)k) { while (n < k) {
// skip off-channel tokens, but make sure to not look past EOF // skip off-channel tokens, but make sure to not look past EOF
if (sync(i + 1)) { if (sync(i + 1)) {
// XXX ml: no error handling either i = nextTokenOnChannel(i + 1, channel);
i = (size_t)nextTokenOnChannel(i + 1, channel);
} }
n++; n++;
} }
@ -92,7 +93,7 @@ int CommonTokenStream::getNumberOfOnChannelTokens() {
int n = 0; int n = 0;
fill(); fill();
for (size_t i = 0; i < _tokens.size(); i++) { for (size_t i = 0; i < _tokens.size(); i++) {
TokenRef t = _tokens[i]; Token::Ref t = _tokens[i];
if (t->getChannel() == channel) { if (t->getChannel() == channel) {
n++; n++;
} }

View File

@ -66,12 +66,12 @@ namespace runtime {
CommonTokenStream(TokenSource *tokenSource, int channel); CommonTokenStream(TokenSource *tokenSource, int channel);
protected: protected:
virtual size_t adjustSeekIndex(size_t i) override; virtual ssize_t adjustSeekIndex(size_t i) override;
virtual TokenRef LB(size_t k) override; virtual Token::Ref LB(size_t k) override;
public: public:
virtual TokenRef LT(ssize_t k) override; virtual Token::Ref LT(ssize_t k) override;
/// Count EOF just once. /// Count EOF just once.
virtual int getNumberOfOnChannelTokens(); virtual int getNumberOfOnChannelTokens();

View File

@ -33,4 +33,9 @@
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
ConsoleErrorListener *const ConsoleErrorListener::INSTANCE = new ConsoleErrorListener(); ConsoleErrorListener ConsoleErrorListener::INSTANCE;
void ConsoleErrorListener::syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol,
size_t line, int charPositionInLine, const std::wstring &msg, std::exception_ptr e) {
std::wcerr << L"line " << line << L":" << charPositionInLine << L" " << msg << std::endl;
}

View File

@ -40,13 +40,9 @@ namespace runtime {
class ConsoleErrorListener : public BaseErrorListener { class ConsoleErrorListener : public BaseErrorListener {
public: public:
static ConsoleErrorListener *const INSTANCE; static ConsoleErrorListener INSTANCE;
void syntaxError(IRecognizer *recognizer, void *offendingSymbol, virtual void syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol, size_t line, int charPositionInLine, const std::wstring &msg, std::exception_ptr e) override;
int line, int charPositionInLine, const std::wstring &msg,
RecognitionException *e) {
std::wcerr << L"line " << line << L":" << charPositionInLine << L" " << msg;
}
}; };
} // namespace runtime } // namespace runtime

View File

@ -195,7 +195,7 @@ void DefaultErrorStrategy::reportUnwantedToken(Parser *recognizer) {
beginErrorCondition(recognizer); beginErrorCondition(recognizer);
TokenRef t = recognizer->getCurrentToken(); Token::Ref t = recognizer->getCurrentToken();
std::wstring tokenName = getTokenErrorDisplay(t); std::wstring tokenName = getTokenErrorDisplay(t);
misc::IntervalSet expecting = getExpectedTokens(recognizer); misc::IntervalSet expecting = getExpectedTokens(recognizer);
@ -210,16 +210,16 @@ void DefaultErrorStrategy::reportMissingToken(Parser *recognizer) {
beginErrorCondition(recognizer); beginErrorCondition(recognizer);
TokenRef t = recognizer->getCurrentToken(); Token::Ref t = recognizer->getCurrentToken();
misc::IntervalSet expecting = getExpectedTokens(recognizer); misc::IntervalSet expecting = getExpectedTokens(recognizer);
std::wstring msg = std::wstring(L"missing ") + expecting.toString(recognizer->getTokenNames()) + std::wstring(L" at ") + getTokenErrorDisplay(t); std::wstring msg = std::wstring(L"missing ") + expecting.toString(recognizer->getTokenNames()) + std::wstring(L" at ") + getTokenErrorDisplay(t);
recognizer->notifyErrorListeners(t, msg, nullptr); recognizer->notifyErrorListeners(t, msg, nullptr);
} }
TokenRef DefaultErrorStrategy::recoverInline(Parser *recognizer) { Token::Ref DefaultErrorStrategy::recoverInline(Parser *recognizer) {
// SINGLE TOKEN DELETION // SINGLE TOKEN DELETION
TokenRef matchedSymbol = singleTokenDeletion(recognizer); Token::Ref matchedSymbol = singleTokenDeletion(recognizer);
if (matchedSymbol) { if (matchedSymbol) {
// we have deleted the extra token. // we have deleted the extra token.
// now, move past ttype token as if all were ok // now, move past ttype token as if all were ok
@ -253,22 +253,22 @@ bool DefaultErrorStrategy::singleTokenInsertion(Parser *recognizer) {
return false; return false;
} }
TokenRef DefaultErrorStrategy::singleTokenDeletion(Parser *recognizer) { Token::Ref DefaultErrorStrategy::singleTokenDeletion(Parser *recognizer) {
ssize_t nextTokenType = recognizer->getInputStream()->LA(2); ssize_t nextTokenType = recognizer->getInputStream()->LA(2);
misc::IntervalSet expecting = getExpectedTokens(recognizer); misc::IntervalSet expecting = getExpectedTokens(recognizer);
if (expecting.contains((int)nextTokenType)) { if (expecting.contains((int)nextTokenType)) {
reportUnwantedToken(recognizer); reportUnwantedToken(recognizer);
recognizer->consume(); // simply delete extra token recognizer->consume(); // simply delete extra token
// we want to return the token we're actually matching // we want to return the token we're actually matching
TokenRef matchedSymbol = recognizer->getCurrentToken(); Token::Ref matchedSymbol = recognizer->getCurrentToken();
reportMatch(recognizer); // we know current token is correct reportMatch(recognizer); // we know current token is correct
return matchedSymbol; return matchedSymbol;
} }
return nullptr; return nullptr;
} }
TokenRef DefaultErrorStrategy::getMissingSymbol(Parser *recognizer) { Token::Ref DefaultErrorStrategy::getMissingSymbol(Parser *recognizer) {
TokenRef currentSymbol = recognizer->getCurrentToken(); Token::Ref currentSymbol = recognizer->getCurrentToken();
misc::IntervalSet expecting = getExpectedTokens(recognizer); misc::IntervalSet expecting = getExpectedTokens(recognizer);
ssize_t expectedTokenType = expecting.getMinElement(); // get any element ssize_t expectedTokenType = expecting.getMinElement(); // get any element
std::wstring tokenText; std::wstring tokenText;
@ -277,8 +277,8 @@ TokenRef DefaultErrorStrategy::getMissingSymbol(Parser *recognizer) {
} else { } else {
tokenText = std::wstring(L"<missing ") + recognizer->getTokenNames()[(size_t)expectedTokenType] + std::wstring(L">"); tokenText = std::wstring(L"<missing ") + recognizer->getTokenNames()[(size_t)expectedTokenType] + std::wstring(L">");
} }
TokenRef current = currentSymbol; Token::Ref current = currentSymbol;
TokenRef lookback = recognizer->getTokenStream()->LT(-1); Token::Ref lookback = recognizer->getTokenStream()->LT(-1);
if (current->getType() == EOF && lookback != nullptr) { if (current->getType() == EOF && lookback != nullptr) {
current = lookback; current = lookback;
} }
@ -291,7 +291,7 @@ misc::IntervalSet DefaultErrorStrategy::getExpectedTokens(Parser *recognizer) {
return recognizer->getExpectedTokens(); return recognizer->getExpectedTokens();
} }
std::wstring DefaultErrorStrategy::getTokenErrorDisplay(TokenRef t) { std::wstring DefaultErrorStrategy::getTokenErrorDisplay(Token::Ref t) {
if (t == nullptr) { if (t == nullptr) {
return L"<no token>"; return L"<no token>";
} }
@ -306,11 +306,11 @@ std::wstring DefaultErrorStrategy::getTokenErrorDisplay(TokenRef t) {
return escapeWSAndQuote(s); return escapeWSAndQuote(s);
} }
std::wstring DefaultErrorStrategy::getSymbolText(TokenRef symbol) { std::wstring DefaultErrorStrategy::getSymbolText(Token::Ref symbol) {
return symbol->getText(); return symbol->getText();
} }
int DefaultErrorStrategy::getSymbolType(TokenRef symbol) { int DefaultErrorStrategy::getSymbolType(Token::Ref symbol) {
return symbol->getType(); return symbol->getType();
} }
@ -324,7 +324,7 @@ std::wstring DefaultErrorStrategy::escapeWSAndQuote(std::wstring &s) {
misc::IntervalSet DefaultErrorStrategy::getErrorRecoverySet(Parser *recognizer) { misc::IntervalSet DefaultErrorStrategy::getErrorRecoverySet(Parser *recognizer) {
const atn::ATN &atn = recognizer->getInterpreter<atn::ATNSimulator>()->atn; const atn::ATN &atn = recognizer->getInterpreter<atn::ATNSimulator>()->atn;
RuleContextRef ctx = recognizer->getContext(); RuleContext::Ref ctx = recognizer->getContext();
misc::IntervalSet recoverSet; misc::IntervalSet recoverSet;
while (ctx->invokingState >= 0) { while (ctx->invokingState >= 0) {
// compute what follows who invoked us // compute what follows who invoked us

View File

@ -305,7 +305,7 @@ namespace runtime {
/// in rule {@code atom}. It can assume that you forgot the {@code ')'}. /// in rule {@code atom}. It can assume that you forgot the {@code ')'}.
/// </summary> /// </summary>
public: public:
virtual TokenRef recoverInline(Parser *recognizer) override; virtual Token::Ref recoverInline(Parser *recognizer) override;
/// <summary> /// <summary>
/// This method implements the single-token insertion inline error recovery /// This method implements the single-token insertion inline error recovery
@ -344,7 +344,7 @@ namespace runtime {
/// <returns> the successfully matched <seealso cref="Token"/> instance if single-token /// <returns> the successfully matched <seealso cref="Token"/> instance if single-token
/// deletion successfully recovers from the mismatched input, otherwise /// deletion successfully recovers from the mismatched input, otherwise
/// {@code null} </returns> /// {@code null} </returns>
virtual TokenRef singleTokenDeletion(Parser *recognizer); virtual Token::Ref singleTokenDeletion(Parser *recognizer);
/// <summary> /// <summary>
/// Conjure up a missing token during error recovery. /// Conjure up a missing token during error recovery.
@ -366,7 +366,7 @@ namespace runtime {
/// If you change what tokens must be created by the lexer, /// If you change what tokens must be created by the lexer,
/// override this method to create the appropriate tokens. /// override this method to create the appropriate tokens.
/// </summary> /// </summary>
virtual TokenRef getMissingSymbol(Parser *recognizer); virtual Token::Ref getMissingSymbol(Parser *recognizer);
virtual misc::IntervalSet getExpectedTokens(Parser *recognizer); virtual misc::IntervalSet getExpectedTokens(Parser *recognizer);
@ -379,11 +379,11 @@ namespace runtime {
/// your token objects because you don't have to go modify your lexer /// your token objects because you don't have to go modify your lexer
/// so that it creates a new Java type. /// so that it creates a new Java type.
/// </summary> /// </summary>
virtual std::wstring getTokenErrorDisplay(TokenRef t); virtual std::wstring getTokenErrorDisplay(Token::Ref t);
virtual std::wstring getSymbolText(TokenRef symbol); virtual std::wstring getSymbolText(Token::Ref symbol);
virtual int getSymbolType(TokenRef symbol); virtual int getSymbolType(Token::Ref symbol);
virtual std::wstring escapeWSAndQuote(std::wstring &s); virtual std::wstring escapeWSAndQuote(std::wstring &s);

View File

@ -46,7 +46,7 @@ DiagnosticErrorListener::DiagnosticErrorListener() : exactOnly(true) {
DiagnosticErrorListener::DiagnosticErrorListener(bool exactOnly) : exactOnly(exactOnly) { DiagnosticErrorListener::DiagnosticErrorListener(bool exactOnly) : exactOnly(exactOnly) {
} }
void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
bool exact, const antlrcpp::BitSet &ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) { bool exact, const 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,
recognizer->notifyErrorListeners(message); recognizer->notifyErrorListeners(message);
} }
void DiagnosticErrorListener::reportAttemptingFullContext(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, void DiagnosticErrorListener::reportAttemptingFullContext(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex,
size_t stopIndex, const antlrcpp::BitSet &conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) { size_t stopIndex, const 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));
@ -69,7 +69,7 @@ void DiagnosticErrorListener::reportAttemptingFullContext(Parser *recognizer, df
recognizer->notifyErrorListeners(message); recognizer->notifyErrorListeners(message);
} }
void DiagnosticErrorListener::reportContextSensitivity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, void DiagnosticErrorListener::reportContextSensitivity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex,
size_t stopIndex, int prediction, std::shared_ptr<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));
@ -77,9 +77,9 @@ void DiagnosticErrorListener::reportContextSensitivity(Parser *recognizer, dfa::
recognizer->notifyErrorListeners(message); recognizer->notifyErrorListeners(message);
} }
std::wstring DiagnosticErrorListener::getDecisionDescription(Parser *recognizer, dfa::DFA *dfa) { std::wstring DiagnosticErrorListener::getDecisionDescription(Parser *recognizer, const dfa::DFA &dfa) {
int decision = dfa->decision; int decision = dfa.decision;
int ruleIndex = ((atn::ATNState*)dfa->atnStartState)->ruleIndex; int ruleIndex = ((atn::ATNState*)dfa.atnStartState)->ruleIndex;
const std::vector<std::wstring>& ruleNames = recognizer->getRuleNames(); const std::vector<std::wstring>& ruleNames = recognizer->getRuleNames();
if (ruleIndex < 0 || ruleIndex >= (int)ruleNames.size()) { if (ruleIndex < 0 || ruleIndex >= (int)ruleNames.size()) {

View File

@ -81,17 +81,17 @@ namespace runtime {
/// {@code false} to report all ambiguities. </param> /// {@code false} to report all ambiguities. </param>
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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact,
const antlrcpp::BitSet &ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) override; const 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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
const antlrcpp::BitSet &conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) override; const 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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
int prediction, std::shared_ptr<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, const dfa::DFA &dfa);
/// <summary> /// <summary>
/// Computes the set of conflicting or ambiguous alternatives from a /// Computes the set of conflicting or ambiguous alternatives from a

View File

@ -66,7 +66,7 @@ void Lexer::reset() {
getInterpreter<atn::LexerATNSimulator>()->reset(); getInterpreter<atn::LexerATNSimulator>()->reset();
} }
TokenRef Lexer::nextToken() { Token::Ref Lexer::nextToken() {
// Mark start location in char stream so unbuffered streams are // Mark start location in char stream so unbuffered streams are
// guaranteed at least have text of current token // guaranteed at least have text of current token
ssize_t tokenStartMarker = _input->mark(); ssize_t tokenStartMarker = _input->mark();
@ -167,18 +167,18 @@ CharStream* Lexer::getInputStream() {
return _input; return _input;
} }
void Lexer::emit(TokenRef token) { void Lexer::emit(Token::Ref token) {
_token = token; _token = token;
} }
TokenRef Lexer::emit() { Token::Ref Lexer::emit() {
TokenRef t = std::dynamic_pointer_cast<Token>(_factory->create({ this, _input }, _type, _text, _channel, Token::Ref t = std::dynamic_pointer_cast<Token>(_factory->create({ this, _input }, _type, _text, _channel,
_tokenStartCharIndex, getCharIndex() - 1, _tokenStartLine, _tokenStartCharPositionInLine)); _tokenStartCharIndex, getCharIndex() - 1, _tokenStartLine, _tokenStartCharPositionInLine));
emit(t); emit(t);
return t; return t;
} }
TokenRef Lexer::emitEOF() { Token::Ref Lexer::emitEOF() {
int cpos = getCharPositionInLine(); int cpos = getCharPositionInLine();
// The character position for EOF is one beyond the position of // The character position for EOF is one beyond the position of
// the previous token's last character // the previous token's last character
@ -186,7 +186,7 @@ TokenRef Lexer::emitEOF() {
int n = _token->getStopIndex() - _token->getStartIndex() + 1; int n = _token->getStopIndex() - _token->getStartIndex() + 1;
cpos = _token->getCharPositionInLine() + n; cpos = _token->getCharPositionInLine() + n;
} }
TokenRef eof = std::dynamic_pointer_cast<Token>(_factory->create({ this, _input }, EOF, L"", Token::DEFAULT_CHANNEL, Token::Ref eof = std::dynamic_pointer_cast<Token>(_factory->create({ this, _input }, EOF, L"", Token::DEFAULT_CHANNEL,
(int)_input->index(), (int)_input->index() - 1, (int)getLine(), cpos)); (int)_input->index(), (int)_input->index() - 1, (int)getLine(), cpos));
emit(eof); emit(eof);
return eof; return eof;
@ -223,11 +223,11 @@ void Lexer::setText(const std::wstring &text) {
this->_text = text; this->_text = text;
} }
TokenRef Lexer::getToken() { Token::Ref Lexer::getToken() {
return _token; return _token;
} }
void Lexer::setToken(TokenRef token) { void Lexer::setToken(Token::Ref token) {
_token = token; _token = token;
} }
@ -247,9 +247,9 @@ int Lexer::getChannel() {
return _channel; return _channel;
} }
std::vector<TokenRef> Lexer::getAllTokens() { std::vector<Token::Ref> Lexer::getAllTokens() {
std::vector<TokenRef> tokens; std::vector<Token::Ref> tokens;
TokenRef t = nextToken(); Token::Ref t = nextToken();
while (t->getType() != EOF) { while (t->getType() != EOF) {
tokens.push_back(t); tokens.push_back(t);
t = nextToken(); t = nextToken();
@ -268,14 +268,13 @@ void Lexer::notifyListeners(const LexerNoViableAltException &e) {
std::wstring text = _input->getText(misc::Interval(_tokenStartCharIndex, (int)_input->index())); std::wstring text = _input->getText(misc::Interval(_tokenStartCharIndex, (int)_input->index()));
std::wstring msg = std::wstring(L"token recognition error at: '") + getErrorDisplay(text) + std::wstring(L"'"); std::wstring msg = std::wstring(L"token recognition error at: '") + getErrorDisplay(text) + std::wstring(L"'");
ANTLRErrorListener *listener = getErrorListenerDispatch(); ProxyErrorListener &listener = getErrorListenerDispatch();
listener->syntaxError(this, nullptr, (size_t)_tokenStartLine, _tokenStartCharPositionInLine, msg, std::make_exception_ptr(e)); listener.syntaxError(this, nullptr, (size_t)_tokenStartLine, _tokenStartCharPositionInLine, msg, std::make_exception_ptr(e));
} }
std::wstring Lexer::getErrorDisplay(const std::wstring &s) { std::wstring Lexer::getErrorDisplay(const std::wstring &s) {
std::wstringstream ss; std::wstringstream ss;
for (size_t i = 0; i < s.length(); i++) { for (auto c : s) {
char c = ((char*)s.c_str())[i];
ss << getErrorDisplay(c); ss << getErrorDisplay(c);
} }
return ss.str(); return ss.str();
@ -297,7 +296,7 @@ std::wstring Lexer::getErrorDisplay(int c) {
s = L"\\r"; s = L"\\r";
break; break;
default: default:
s = std::to_wstring(c); s = c;
break; break;
} }
return s; return s;

View File

@ -70,7 +70,7 @@ namespace runtime {
/// emissions, then set this to the last token to be matched or /// emissions, then set this to the last token to be matched or
/// something nonnull so that the auto token emit mechanism will not /// something nonnull so that the auto token emit mechanism will not
/// emit another token. /// emit another token.
TokenRef _token; Token::Ref _token;
/// <summary> /// <summary>
/// What character index in the stream did the current token start at? /// What character index in the stream did the current token start at?
@ -114,7 +114,7 @@ namespace runtime {
virtual void reset(); virtual void reset();
/// Return a token from this source; i.e., match a token on the char stream. /// Return a token from this source; i.e., match a token on the char stream.
virtual TokenRef nextToken() override; virtual Token::Ref nextToken() override;
/// Instruct the lexer to skip creating a token for current lexer rule /// Instruct the lexer to skip creating a token for current lexer rule
/// and look for another token. nextToken() knows to keep looking when /// and look for another token. nextToken() knows to keep looking when
@ -152,7 +152,7 @@ namespace runtime {
/// and getToken (to push tokens into a list and pull from that list /// and getToken (to push tokens into a list and pull from that list
/// rather than a single variable as this implementation does). /// rather than a single variable as this implementation does).
/// </summary> /// </summary>
virtual void emit(TokenRef token); virtual void emit(Token::Ref token);
/// <summary> /// <summary>
/// The standard method called to automatically emit a token at the /// The standard method called to automatically emit a token at the
@ -161,9 +161,9 @@ namespace runtime {
/// use that to set the token's text. Override this method to emit /// use that to set the token's text. Override this method to emit
/// custom Token objects or provide a new factory. /// custom Token objects or provide a new factory.
/// </summary> /// </summary>
virtual TokenRef emit(); virtual Token::Ref emit();
virtual TokenRef emitEOF(); virtual Token::Ref emitEOF();
virtual size_t getLine() const override; virtual size_t getLine() const override;
@ -191,9 +191,9 @@ namespace runtime {
/// <summary> /// <summary>
/// Override if emitting multiple tokens. </summary> /// Override if emitting multiple tokens. </summary>
virtual TokenRef getToken(); virtual Token::Ref getToken();
virtual void setToken(TokenRef token); virtual void setToken(Token::Ref token);
virtual void setType(int ttype); virtual void setType(int ttype);
@ -207,7 +207,7 @@ namespace runtime {
/// Return a list of all Token objects in input char stream. /// Return a list of all Token objects in input char stream.
/// Forces load of all tokens. Does not include EOF token. /// Forces load of all tokens. Does not include EOF token.
virtual std::vector<TokenRef> getAllTokens(); virtual std::vector<Token::Ref> getAllTokens();
virtual void recover(const LexerNoViableAltException &e); virtual void recover(const LexerNoViableAltException &e);

View File

@ -39,26 +39,20 @@
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
LexerInterpreter::LexerInterpreter(const std::wstring &grammarFileName, std::vector<std::wstring> *tokenNames, std::vector<std::wstring> *ruleNames, std::vector<std::wstring> *modeNames, const atn::ATN &atn, CharStream *input) : Lexer(input), grammarFileName(grammarFileName), _atn(atn), _sharedContextCache(new atn::PredictionContextCache()) { LexerInterpreter::LexerInterpreter(const std::wstring &grammarFileName, const std::vector<std::wstring> &tokenNames,
const std::vector<std::wstring> &ruleNames, const std::vector<std::wstring> &modeNames, const atn::ATN &atn,
CharStream *input)
: Lexer(input), grammarFileName(grammarFileName), _tokenNames(tokenNames), _ruleNames(ruleNames), _modeNames(modeNames),
_atn(atn), _sharedContextCache(new atn::PredictionContextCache()) {
if (_atn.grammarType != atn::ATNType::LEXER) { if (_atn.grammarType != atn::ATNType::LEXER) {
throw IllegalArgumentException("The ATN must be a lexer ATN."); throw IllegalArgumentException("The ATN must be a lexer ATN.");
} }
for (size_t i = 0; i < (size_t)atn.getNumberOfDecisions(); ++i) {
for (int i = 0; i < (int)_decisionToDFA.size(); i++) { _decisionToDFA.push_back(dfa::DFA(_atn.getDecisionState((int)i), (int)i));
_decisionToDFA[(size_t)i] = new dfa::DFA(_atn.getDecisionState(i), i);
} }
_interpreter = new atn::LexerATNSimulator(_atn, _decisionToDFA, _sharedContextCache); _interpreter = new atn::LexerATNSimulator(_atn, _decisionToDFA, _sharedContextCache);
if (tokenNames) {
_tokenNames = *tokenNames;
}
if (ruleNames) {
_ruleNames = *ruleNames;
}
if (modeNames) {
_modeNames = *modeNames;
}
} }
LexerInterpreter::~LexerInterpreter() LexerInterpreter::~LexerInterpreter()

View File

@ -39,30 +39,28 @@ namespace v4 {
namespace runtime { namespace runtime {
class LexerInterpreter : public Lexer { class LexerInterpreter : public Lexer {
public:
LexerInterpreter(const std::wstring &grammarFileName, const std::vector<std::wstring> &tokenNames,
const std::vector<std::wstring> &ruleNames, const std::vector<std::wstring> &modeNames,
const atn::ATN &atn, CharStream *input);
~LexerInterpreter();
virtual const atn::ATN& getATN() const override;
virtual std::wstring getGrammarFileName() const override;
virtual const std::vector<std::wstring>& getTokenNames() const override;
virtual const std::vector<std::wstring>& getRuleNames() const override;
virtual const std::vector<std::wstring>& getModeNames() const override;
protected: protected:
const std::wstring grammarFileName; const std::wstring grammarFileName;
const atn::ATN &_atn; const atn::ATN &_atn;
std::vector<std::wstring> _tokenNames; const std::vector<std::wstring> &_tokenNames;
std::vector<std::wstring> _ruleNames; const std::vector<std::wstring> &_ruleNames;
std::vector<std::wstring> _modeNames; const std::vector<std::wstring> &_modeNames;
std::vector<dfa::DFA*> _decisionToDFA; std::vector<dfa::DFA> _decisionToDFA;
std::shared_ptr<atn::PredictionContextCache> _sharedContextCache; std::shared_ptr<atn::PredictionContextCache> _sharedContextCache;
public:
LexerInterpreter(const std::wstring &grammarFileName, std::vector<std::wstring> *tokenNames, std::vector<std::wstring> *ruleNames, std::vector<std::wstring> *modeNames, const atn::ATN &atn, CharStream *input);
~LexerInterpreter();
virtual const atn::ATN& getATN() const override;
virtual std::wstring getGrammarFileName() const override;
virtual const std::vector<std::wstring>& getTokenNames() const override;
virtual const std::vector<std::wstring>& getRuleNames() const override;
virtual const std::vector<std::wstring>& getModeNames() const override;
}; };
} // namespace runtime } // namespace runtime

View File

@ -45,7 +45,7 @@ int ListTokenSource::getCharPositionInLine() {
} else if (tokens.size() > 0) { } else if (tokens.size() > 0) {
// have to calculate the result from the line/column of the previous // have to calculate the result from the line/column of the previous
// token, along with the text of the token. // token, along with the text of the token.
TokenRef lastToken = tokens.back(); Token::Ref lastToken = tokens.back();
std::wstring tokenText = lastToken->getText(); std::wstring tokenText = lastToken->getText();
if (tokenText != L"") { if (tokenText != L"") {
int lastNewLine = (int)tokenText.rfind(L'\n'); int lastNewLine = (int)tokenText.rfind(L'\n');
@ -62,7 +62,7 @@ int ListTokenSource::getCharPositionInLine() {
return 0; return 0;
} }
TokenRef ListTokenSource::nextToken() { Token::Ref ListTokenSource::nextToken() {
if (i >= tokens.size()) { if (i >= tokens.size()) {
if (eofToken == nullptr) { if (eofToken == nullptr) {
int start = -1; int start = -1;
@ -81,7 +81,7 @@ TokenRef ListTokenSource::nextToken() {
return eofToken; return eofToken;
} }
TokenRef t = tokens[i]; Token::Ref t = tokens[i];
if (i == tokens.size() - 1 && t->getType() == EOF) { if (i == tokens.size() - 1 && t->getType() == EOF) {
eofToken = t; eofToken = t;
} }
@ -98,7 +98,7 @@ size_t ListTokenSource::getLine() const {
} else if (tokens.size() > 0) { } else if (tokens.size() > 0) {
// have to calculate the result from the line/column of the previous // have to calculate the result from the line/column of the previous
// token, along with the text of the token. // token, along with the text of the token.
TokenRef lastToken = tokens.back(); Token::Ref lastToken = tokens.back();
int line = lastToken->getLine(); int line = lastToken->getLine();
std::wstring tokenText = lastToken->getText(); std::wstring tokenText = lastToken->getText();

View File

@ -49,7 +49,7 @@ namespace runtime {
/// </summary> /// </summary>
class ListTokenSource : public TokenSource { class ListTokenSource : public TokenSource {
protected: protected:
const std::vector<TokenRef> tokens; const std::vector<Token::Ref> tokens;
/// <summary> /// <summary>
/// The name of the input source. If this value is {@code null}, a call to /// The name of the input source. If this value is {@code null}, a call to
@ -71,7 +71,7 @@ namespace runtime {
/// <summary> /// <summary>
/// This field caches the EOF token for the token source. /// This field caches the EOF token for the token source.
/// </summary> /// </summary>
TokenRef eofToken; Token::Ref eofToken;
/// <summary> /// <summary>
/// This is the backing field for <seealso cref="#getTokenFactory"/> and /// This is the backing field for <seealso cref="#getTokenFactory"/> and
@ -115,7 +115,7 @@ namespace runtime {
} }
virtual int getCharPositionInLine() override; virtual int getCharPositionInLine() override;
virtual TokenRef nextToken() override; virtual Token::Ref nextToken() override;
virtual size_t getLine() const override; virtual size_t getLine() const override;
virtual CharStream* getInputStream() override; virtual CharStream* getInputStream() override;
virtual std::string getSourceName() override; virtual std::string getSourceName() override;

View File

@ -40,12 +40,12 @@ NoViableAltException::NoViableAltException(Parser *recognizer)
recognizer->getCurrentToken(), nullptr, recognizer->getContext()) { recognizer->getCurrentToken(), nullptr, recognizer->getContext()) {
} }
NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input, TokenRef startToken, NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input, Token::Ref startToken,
TokenRef offendingToken, std::shared_ptr<atn::ATNConfigSet> deadEndConfigs, ParserRuleContextRef ctx) Token::Ref offendingToken, std::shared_ptr<atn::ATNConfigSet> deadEndConfigs, ParserRuleContext::Ref ctx)
: RecognitionException(recognizer, input, ctx, offendingToken), _deadEndConfigs(deadEndConfigs), _startToken(startToken) { : RecognitionException(recognizer, input, ctx, offendingToken), _deadEndConfigs(deadEndConfigs), _startToken(startToken) {
} }
TokenRef NoViableAltException::getStartToken() const { Token::Ref NoViableAltException::getStartToken() const {
return _startToken; return _startToken;
} }

View File

@ -47,10 +47,10 @@ namespace runtime {
class NoViableAltException : public RecognitionException { class NoViableAltException : public RecognitionException {
public: public:
NoViableAltException(Parser *recognizer); // LL(1) error NoViableAltException(Parser *recognizer); // LL(1) error
NoViableAltException(Parser *recognizer, TokenStream *input, TokenRef startToken, TokenRef offendingToken, NoViableAltException(Parser *recognizer, TokenStream *input, Token::Ref startToken, Token::Ref offendingToken,
std::shared_ptr<atn::ATNConfigSet> deadEndConfigs, ParserRuleContextRef ctx); std::shared_ptr<atn::ATNConfigSet> deadEndConfigs, ParserRuleContext::Ref ctx);
virtual TokenRef getStartToken() const; virtual Token::Ref getStartToken() const;
virtual std::shared_ptr<atn::ATNConfigSet> getDeadEndConfigs() const; virtual std::shared_ptr<atn::ATNConfigSet> getDeadEndConfigs() const;
private: private:
@ -61,7 +61,7 @@ namespace runtime {
/// not be buffering tokens so get a reference to it. (At the /// not be buffering tokens so get a reference to it. (At the
/// time the error occurred, of course the stream needs to keep a /// time the error occurred, of course the stream needs to keep a
/// buffer all of the tokens but later we might not have access to those.) /// buffer all of the tokens but later we might not have access to those.)
TokenRef _startToken; Token::Ref _startToken;
}; };

View File

@ -57,7 +57,7 @@ std::map<std::wstring, atn::ATN> Parser::bypassAltsAtnCache;
Parser::TraceListener::TraceListener(Parser *outerInstance) : outerInstance(outerInstance) { Parser::TraceListener::TraceListener(Parser *outerInstance) : outerInstance(outerInstance) {
} }
void Parser::TraceListener::enterEveryRule(ParserRuleContextRef ctx) { void Parser::TraceListener::enterEveryRule(ParserRuleContext::Ref ctx) {
std::cout << "enter " std::cout << "enter "
<< antlrcpp::ws2s(outerInstance->getRuleNames()[(size_t)ctx->getRuleIndex()]) << antlrcpp::ws2s(outerInstance->getRuleNames()[(size_t)ctx->getRuleIndex()])
<< ", LT(1)=" << antlrcpp::ws2s(outerInstance->_input->LT(1)->getText()) << ", LT(1)=" << antlrcpp::ws2s(outerInstance->_input->LT(1)->getText())
@ -74,7 +74,7 @@ void Parser::TraceListener::visitTerminal(std::shared_ptr<tree::TerminalNode> no
void Parser::TraceListener::visitErrorNode(std::shared_ptr<tree::ErrorNode> node) { void Parser::TraceListener::visitErrorNode(std::shared_ptr<tree::ErrorNode> node) {
} }
void Parser::TraceListener::exitEveryRule(ParserRuleContextRef ctx) { void Parser::TraceListener::exitEveryRule(ParserRuleContext::Ref ctx) {
std::cout << "exit " std::cout << "exit "
<< antlrcpp::ws2s(outerInstance->getRuleNames()[(size_t)ctx->getRuleIndex()]) << antlrcpp::ws2s(outerInstance->getRuleNames()[(size_t)ctx->getRuleIndex()])
<< ", LT(1)=" << antlrcpp::ws2s(outerInstance->_input->LT(1)->getText()) << ", LT(1)=" << antlrcpp::ws2s(outerInstance->_input->LT(1)->getText())
@ -84,7 +84,7 @@ void Parser::TraceListener::exitEveryRule(ParserRuleContextRef ctx) {
const std::shared_ptr<Parser::TrimToSizeListener> Parser::TrimToSizeListener::INSTANCE = const std::shared_ptr<Parser::TrimToSizeListener> Parser::TrimToSizeListener::INSTANCE =
std::make_shared<Parser::TrimToSizeListener>(); std::make_shared<Parser::TrimToSizeListener>();
void Parser::TrimToSizeListener::enterEveryRule(ParserRuleContextRef ctx) { void Parser::TrimToSizeListener::enterEveryRule(ParserRuleContext::Ref ctx) {
} }
void Parser::TrimToSizeListener::visitTerminal(std::shared_ptr<tree::TerminalNode> node) { void Parser::TrimToSizeListener::visitTerminal(std::shared_ptr<tree::TerminalNode> node) {
@ -93,7 +93,7 @@ void Parser::TrimToSizeListener::visitTerminal(std::shared_ptr<tree::TerminalNod
void Parser::TrimToSizeListener::visitErrorNode(std::shared_ptr<tree::ErrorNode> node) { void Parser::TrimToSizeListener::visitErrorNode(std::shared_ptr<tree::ErrorNode> node) {
} }
void Parser::TrimToSizeListener::exitEveryRule(ParserRuleContextRef ctx) { void Parser::TrimToSizeListener::exitEveryRule(ParserRuleContext::Ref ctx) {
ctx->children.shrink_to_fit(); ctx->children.shrink_to_fit();
} }
@ -121,8 +121,8 @@ void Parser::reset() {
} }
} }
TokenRef Parser::match(int ttype) { Token::Ref Parser::match(int ttype) {
TokenRef t = getCurrentToken(); Token::Ref t = getCurrentToken();
if (t->getType() == ttype) { if (t->getType() == ttype) {
_errHandler->reportMatch(this); _errHandler->reportMatch(this);
consume(); consume();
@ -137,8 +137,8 @@ TokenRef Parser::match(int ttype) {
return t; return t;
} }
TokenRef Parser::matchWildcard() { Token::Ref Parser::matchWildcard() {
TokenRef t = getCurrentToken(); Token::Ref t = getCurrentToken();
if (t->getType() > 0) { if (t->getType() > 0) {
_errHandler->reportMatch(this); _errHandler->reportMatch(this);
consume(); consume();
@ -286,13 +286,12 @@ TokenStream* Parser::getTokenStream() {
} }
void Parser::setTokenStream(TokenStream *input) { void Parser::setTokenStream(TokenStream *input) {
delete _input; _input = nullptr; // Just a reference we don't own.
_input = nullptr;
reset(); reset();
_input = input; _input = input;
} }
TokenRef Parser::getCurrentToken() { Token::Ref Parser::getCurrentToken() {
return _input->LT(1); return _input->LT(1);
} }
@ -300,19 +299,19 @@ void Parser::notifyErrorListeners(const std::wstring &msg) {
notifyErrorListeners(getCurrentToken(), msg, nullptr); notifyErrorListeners(getCurrentToken(), msg, nullptr);
} }
void Parser::notifyErrorListeners(TokenRef offendingToken, const std::wstring &msg, std::exception_ptr e) { void Parser::notifyErrorListeners(Token::Ref offendingToken, const std::wstring &msg, std::exception_ptr e) {
_syntaxErrors++; _syntaxErrors++;
int line = -1; int line = -1;
int charPositionInLine = -1; int charPositionInLine = -1;
line = offendingToken->getLine(); line = offendingToken->getLine();
charPositionInLine = offendingToken->getCharPositionInLine(); charPositionInLine = offendingToken->getCharPositionInLine();
ANTLRErrorListener *listener = getErrorListenerDispatch(); ProxyErrorListener &listener = getErrorListenerDispatch();
listener->syntaxError(this, offendingToken, (size_t)line, charPositionInLine, msg, e); listener.syntaxError(this, offendingToken, (size_t)line, charPositionInLine, msg, e);
} }
TokenRef Parser::consume() { Token::Ref Parser::consume() {
TokenRef o = getCurrentToken(); Token::Ref o = getCurrentToken();
if (o->getType() != EOF) { if (o->getType() != EOF) {
getInputStream()->consume(); getInputStream()->consume();
} }
@ -342,11 +341,11 @@ void Parser::addContextToParseTree() {
if (_ctx->parent.expired()) if (_ctx->parent.expired())
return; return;
ParserRuleContextRef parent = std::dynamic_pointer_cast<ParserRuleContext>(_ctx->parent.lock()); ParserRuleContext::Ref parent = std::dynamic_pointer_cast<ParserRuleContext>(_ctx->parent.lock());
parent->addChild(_ctx); parent->addChild(_ctx);
} }
void Parser::enterRule(ParserRuleContextRef localctx, int state, int ruleIndex) { void Parser::enterRule(ParserRuleContext::Ref localctx, int state, int ruleIndex) {
setState(state); setState(state);
_ctx = localctx; _ctx = localctx;
_ctx->start = _input->LT(1); _ctx->start = _input->LT(1);
@ -368,12 +367,12 @@ void Parser::exitRule() {
_ctx = std::dynamic_pointer_cast<ParserRuleContext>(_ctx->parent.lock()); _ctx = std::dynamic_pointer_cast<ParserRuleContext>(_ctx->parent.lock());
} }
void Parser::enterOuterAlt(ParserRuleContextRef localctx, int altNum) { void Parser::enterOuterAlt(ParserRuleContext::Ref localctx, int altNum) {
// if we have new localctx, make sure we replace existing ctx // if we have new localctx, make sure we replace existing ctx
// that is previous child of parse tree // that is previous child of parse tree
if (_buildParseTrees && _ctx != localctx) { if (_buildParseTrees && _ctx != localctx) {
if (!_ctx->parent.expired()) { if (!_ctx->parent.expired()) {
ParserRuleContextRef parent = std::dynamic_pointer_cast<ParserRuleContext>(_ctx->parent.lock()); ParserRuleContext::Ref parent = std::dynamic_pointer_cast<ParserRuleContext>(_ctx->parent.lock());
parent->removeLastChild(); parent->removeLastChild();
parent->addChild(localctx); parent->addChild(localctx);
} }
@ -381,11 +380,11 @@ void Parser::enterOuterAlt(ParserRuleContextRef localctx, int altNum) {
_ctx = localctx; _ctx = localctx;
} }
void Parser::enterRecursionRule(ParserRuleContextRef localctx, int ruleIndex) { void Parser::enterRecursionRule(ParserRuleContext::Ref localctx, int ruleIndex) {
enterRecursionRule(localctx, getATN().ruleToStartState[(size_t)ruleIndex]->stateNumber, ruleIndex, 0); enterRecursionRule(localctx, getATN().ruleToStartState[(size_t)ruleIndex]->stateNumber, ruleIndex, 0);
} }
void Parser::enterRecursionRule(ParserRuleContextRef localctx, int state, int ruleIndex, int precedence) { void Parser::enterRecursionRule(ParserRuleContext::Ref localctx, int state, int ruleIndex, int precedence) {
setState(state); setState(state);
_precedenceStack.push_back(precedence); _precedenceStack.push_back(precedence);
_ctx = localctx; _ctx = localctx;
@ -395,8 +394,8 @@ void Parser::enterRecursionRule(ParserRuleContextRef localctx, int state, int ru
} }
} }
void Parser::pushNewRecursionContext(ParserRuleContextRef localctx, int state, int ruleIndex) { void Parser::pushNewRecursionContext(ParserRuleContext::Ref localctx, int state, int ruleIndex) {
ParserRuleContextRef previous = _ctx; ParserRuleContext::Ref previous = _ctx;
previous->parent = localctx; previous->parent = localctx;
previous->invokingState = state; previous->invokingState = state;
previous->stop = _input->LT(-1); previous->stop = _input->LT(-1);
@ -412,10 +411,10 @@ void Parser::pushNewRecursionContext(ParserRuleContextRef localctx, int state, i
} }
} }
void Parser::unrollRecursionContexts(ParserRuleContextRef parentctx) { void Parser::unrollRecursionContexts(ParserRuleContext::Ref parentctx) {
_precedenceStack.pop_back(); _precedenceStack.pop_back();
_ctx->stop = _input->LT(-1); _ctx->stop = _input->LT(-1);
ParserRuleContextRef retctx = _ctx; // save current ctx (return value) ParserRuleContext::Ref retctx = _ctx; // save current ctx (return value)
// unroll so ctx is as it was before call to recursive method // unroll so ctx is as it was before call to recursive method
if (_parseListeners.size() > 0) { if (_parseListeners.size() > 0) {
@ -436,8 +435,8 @@ void Parser::unrollRecursionContexts(ParserRuleContextRef parentctx) {
} }
} }
ParserRuleContextRef Parser::getInvokingContext(int ruleIndex) { ParserRuleContext::Ref Parser::getInvokingContext(int ruleIndex) {
ParserRuleContextRef p = _ctx; ParserRuleContext::Ref p = _ctx;
while (p) { while (p) {
if (p->getRuleIndex() == ruleIndex) { if (p->getRuleIndex() == ruleIndex) {
return p; return p;
@ -446,18 +445,18 @@ ParserRuleContextRef Parser::getInvokingContext(int ruleIndex) {
break; break;
p = std::dynamic_pointer_cast<ParserRuleContext>(p->parent.lock()); p = std::dynamic_pointer_cast<ParserRuleContext>(p->parent.lock());
} }
return ParserRuleContextRef(); return ParserRuleContext::Ref();
} }
ParserRuleContextRef Parser::getContext() { ParserRuleContext::Ref Parser::getContext() {
return _ctx; return _ctx;
} }
void Parser::setContext(ParserRuleContextRef ctx) { void Parser::setContext(ParserRuleContext::Ref ctx) {
_ctx = ctx; _ctx = ctx;
} }
bool Parser::precpred(RuleContextRef localctx, int precedence) { bool Parser::precpred(RuleContext::Ref localctx, int precedence) {
return precedence >= _precedenceStack.back(); return precedence >= _precedenceStack.back();
} }
@ -468,7 +467,7 @@ bool Parser::inContext(const std::wstring &context) {
bool Parser::isExpectedToken(int symbol) { bool Parser::isExpectedToken(int symbol) {
const atn::ATN &atn = getInterpreter<atn::ParserATNSimulator>()->atn; const atn::ATN &atn = getInterpreter<atn::ParserATNSimulator>()->atn;
ParserRuleContextRef ctx = _ctx; ParserRuleContext::Ref ctx = _ctx;
atn::ATNState *s = atn.states[(size_t)getState()]; atn::ATNState *s = atn.states[(size_t)getState()];
misc::IntervalSet following = atn.nextTokens(s); misc::IntervalSet following = atn.nextTokens(s);
@ -516,7 +515,7 @@ int Parser::getRuleIndex(const std::wstring &ruleName) {
return m.at(ruleName); return m.at(ruleName);
} }
ParserRuleContextRef Parser::getRuleContext() { ParserRuleContext::Ref Parser::getRuleContext() {
return _ctx; return _ctx;
} }
@ -524,7 +523,7 @@ std::vector<std::wstring> Parser::getRuleInvocationStack() {
return getRuleInvocationStack(_ctx); return getRuleInvocationStack(_ctx);
} }
std::vector<std::wstring> Parser::getRuleInvocationStack(RuleContextRef p) { std::vector<std::wstring> Parser::getRuleInvocationStack(RuleContext::Ref p) {
std::vector<std::wstring> ruleNames = getRuleNames(); std::vector<std::wstring> ruleNames = getRuleNames();
std::vector<std::wstring> stack = std::vector<std::wstring>(); std::vector<std::wstring> stack = std::vector<std::wstring>();
while (p) { while (p) {
@ -549,8 +548,8 @@ std::vector<std::wstring> Parser::getDFAStrings() {
std::vector<std::wstring> s; std::vector<std::wstring> s;
for (size_t d = 0; d < simulator->_decisionToDFA.size(); d++) { for (size_t d = 0; d < simulator->_decisionToDFA.size(); d++) {
dfa::DFA *dfa = simulator->_decisionToDFA[d]; dfa::DFA &dfa = simulator->_decisionToDFA[d];
s.push_back(dfa->toString(getTokenNames())); s.push_back(dfa.toString(getTokenNames()));
} }
return s; return s;
} }
@ -563,14 +562,14 @@ void Parser::dumpDFA() {
std::lock_guard<std::mutex> lck(mtx); std::lock_guard<std::mutex> lck(mtx);
bool seenOne = false; bool seenOne = false;
for (size_t d = 0; d < simulator->_decisionToDFA.size(); d++) { for (size_t d = 0; d < simulator->_decisionToDFA.size(); d++) {
dfa::DFA *dfa = simulator->_decisionToDFA[d]; dfa::DFA &dfa = simulator->_decisionToDFA[d];
if (!dfa->states->empty()) { if (!dfa.states.empty()) {
if (seenOne) { if (seenOne) {
std::cout << std::endl; std::cout << std::endl;
} }
std::cout << L"Decision " << dfa->decision << L":" << std::endl; std::cout << L"Decision " << dfa.decision << L":" << std::endl;
dfa->toString(getTokenNames()); dfa.toString(getTokenNames());
std::wcout << dfa->toString(getTokenNames()); std::wcout << dfa.toString(getTokenNames());
seenOne = true; seenOne = true;
} }
} }

View File

@ -51,10 +51,10 @@ namespace runtime {
TraceListener(Parser *outerInstance); TraceListener(Parser *outerInstance);
virtual ~TraceListener() {}; virtual ~TraceListener() {};
virtual void enterEveryRule(ParserRuleContextRef ctx) override; virtual void enterEveryRule(ParserRuleContext::Ref ctx) override;
virtual void visitTerminal(std::shared_ptr<tree::TerminalNode> node) override; virtual void visitTerminal(std::shared_ptr<tree::TerminalNode> node) override;
virtual void visitErrorNode(std::shared_ptr<tree::ErrorNode> node) override; virtual void visitErrorNode(std::shared_ptr<tree::ErrorNode> node) override;
virtual void exitEveryRule(ParserRuleContextRef ctx) override; virtual void exitEveryRule(ParserRuleContext::Ref ctx) override;
private: private:
Parser *const outerInstance; Parser *const outerInstance;
@ -64,10 +64,10 @@ namespace runtime {
public: public:
static const std::shared_ptr<TrimToSizeListener> INSTANCE; static const std::shared_ptr<TrimToSizeListener> INSTANCE;
virtual void enterEveryRule(ParserRuleContextRef ctx) override; virtual void enterEveryRule(ParserRuleContext::Ref ctx) override;
virtual void visitTerminal(std::shared_ptr<tree::TerminalNode> node) override; virtual void visitTerminal(std::shared_ptr<tree::TerminalNode> node) override;
virtual void visitErrorNode(std::shared_ptr<tree::ErrorNode> node) override; virtual void visitErrorNode(std::shared_ptr<tree::ErrorNode> node) override;
virtual void exitEveryRule(ParserRuleContextRef ctx) override; virtual void exitEveryRule(ParserRuleContext::Ref ctx) override;
}; };
Parser(TokenStream *input); Parser(TokenStream *input);
@ -94,7 +94,7 @@ namespace runtime {
/// <exception cref="RecognitionException"> if the current input symbol did not match /// <exception cref="RecognitionException"> if the current input symbol did not match
/// {@code ttype} and the error strategy could not recover from the /// {@code ttype} and the error strategy could not recover from the
/// mismatched symbol </exception> /// mismatched symbol </exception>
virtual TokenRef match(int ttype); virtual Token::Ref match(int ttype);
/// <summary> /// <summary>
/// Match current input symbol as a wildcard. If the symbol type matches /// Match current input symbol as a wildcard. If the symbol type matches
@ -112,7 +112,7 @@ namespace runtime {
/// <exception cref="RecognitionException"> if the current input symbol did not match /// <exception cref="RecognitionException"> if the current input symbol did not match
/// a wildcard and the error strategy could not recover from the mismatched /// a wildcard and the error strategy could not recover from the mismatched
/// symbol </exception> /// symbol </exception>
virtual TokenRef matchWildcard(); virtual Token::Ref matchWildcard();
/// <summary> /// <summary>
/// Track the <seealso cref="ParserRuleContext"/> objects during the parse and hook /// Track the <seealso cref="ParserRuleContext"/> objects during the parse and hook
@ -267,11 +267,11 @@ namespace runtime {
/// Match needs to return the current input symbol, which gets put /// Match needs to return the current input symbol, which gets put
/// into the label for the associated token ref; e.g., x=ID. /// into the label for the associated token ref; e.g., x=ID.
/// </summary> /// </summary>
virtual TokenRef getCurrentToken(); virtual Token::Ref getCurrentToken();
void notifyErrorListeners(const std::wstring &msg); void notifyErrorListeners(const std::wstring &msg);
virtual void notifyErrorListeners(TokenRef offendingToken, const std::wstring &msg, std::exception_ptr e); virtual void notifyErrorListeners(Token::Ref offendingToken, const std::wstring &msg, std::exception_ptr e);
/// <summary> /// <summary>
/// Consume and return the <seealso cref="#getCurrentToken current symbol"/>. /// Consume and return the <seealso cref="#getCurrentToken current symbol"/>.
@ -294,37 +294,37 @@ namespace runtime {
/// <seealso cref="ParseTreeListener#visitErrorNode"/> is called on any parse /// <seealso cref="ParseTreeListener#visitErrorNode"/> is called on any parse
/// listeners. /// listeners.
/// </summary> /// </summary>
virtual TokenRef consume(); virtual Token::Ref consume();
/// <summary> /// <summary>
/// Always called by generated parsers upon entry to a rule. Access field /// Always called by generated parsers upon entry to a rule. Access field
/// <seealso cref="#_ctx"/> get the current context. /// <seealso cref="#_ctx"/> get the current context.
/// </summary> /// </summary>
virtual void enterRule(ParserRuleContextRef localctx, int state, int ruleIndex); virtual void enterRule(ParserRuleContext::Ref localctx, int state, int ruleIndex);
virtual void exitRule(); virtual void exitRule();
virtual void enterOuterAlt(ParserRuleContextRef localctx, int altNum); virtual void enterOuterAlt(ParserRuleContext::Ref localctx, int altNum);
/// @deprecated Use /// @deprecated Use
/// <seealso cref="#enterRecursionRule(ParserRuleContext, int, int, int)"/> instead. /// <seealso cref="#enterRecursionRule(ParserRuleContext, int, int, int)"/> instead.
virtual void enterRecursionRule(ParserRuleContextRef localctx, int ruleIndex); virtual void enterRecursionRule(ParserRuleContext::Ref localctx, int ruleIndex);
virtual void enterRecursionRule(ParserRuleContextRef localctx, int state, int ruleIndex, int precedence); virtual void enterRecursionRule(ParserRuleContext::Ref localctx, int state, int ruleIndex, int precedence);
/// <summary> /// <summary>
/// Like <seealso cref="#enterRule"/> but for recursive rules. /// Like <seealso cref="#enterRule"/> but for recursive rules.
/// </summary> /// </summary>
virtual void pushNewRecursionContext(ParserRuleContextRef localctx, int state, int ruleIndex); virtual void pushNewRecursionContext(ParserRuleContext::Ref localctx, int state, int ruleIndex);
virtual void unrollRecursionContexts(ParserRuleContextRef parentctx); virtual void unrollRecursionContexts(ParserRuleContext::Ref parentctx);
virtual ParserRuleContextRef getInvokingContext(int ruleIndex); virtual ParserRuleContext::Ref getInvokingContext(int ruleIndex);
virtual std::shared_ptr<ParserRuleContext> getContext(); virtual ParserRuleContext::Ref getContext();
virtual void setContext(ParserRuleContextRef ctx); virtual void setContext(ParserRuleContext::Ref ctx);
virtual bool precpred(RuleContextRef localctx, int precedence) override; virtual bool precpred(RuleContext::Ref localctx, int precedence) override;
virtual bool inContext(const std::wstring &context); virtual bool inContext(const std::wstring &context);
@ -357,7 +357,7 @@ namespace runtime {
/// Get a rule's index (i.e., {@code RULE_ruleName} field) or -1 if not found. </summary> /// Get a rule's index (i.e., {@code RULE_ruleName} field) or -1 if not found. </summary>
virtual int getRuleIndex(const std::wstring &ruleName); virtual int getRuleIndex(const std::wstring &ruleName);
virtual ParserRuleContextRef getRuleContext(); virtual ParserRuleContext::Ref getRuleContext();
/// <summary> /// <summary>
/// Return List&lt;String&gt; of the rule names in your parser instance /// Return List&lt;String&gt; of the rule names in your parser instance
@ -369,7 +369,7 @@ namespace runtime {
/// </summary> /// </summary>
virtual std::vector<std::wstring> getRuleInvocationStack(); virtual std::vector<std::wstring> getRuleInvocationStack();
virtual std::vector<std::wstring> getRuleInvocationStack(RuleContextRef p); virtual std::vector<std::wstring> getRuleInvocationStack(RuleContext::Ref p);
/// <summary> /// <summary>
/// For debugging and other purposes. </summary> /// For debugging and other purposes. </summary>
@ -390,7 +390,7 @@ namespace runtime {
protected: protected:
/// The ParserRuleContext object for the currently executing rule. /// The ParserRuleContext object for the currently executing rule.
/// This is always non-null during the parsing process. /// This is always non-null during the parsing process.
ParserRuleContextRef _ctx; ParserRuleContext::Ref _ctx;
/// The error handling strategy for the parser. The default is DefaultErrorStrategy. /// The error handling strategy for the parser. The default is DefaultErrorStrategy.
/// See also getErrorHandler. /// See also getErrorHandler.

View File

@ -55,7 +55,7 @@ ParserInterpreter::ParserInterpreter(const std::wstring &grammarFileName, const
: Parser(input), _grammarFileName(grammarFileName), _tokenNames(tokenNames), _atn(atn), _ruleNames(ruleNames) { : Parser(input), _grammarFileName(grammarFileName), _tokenNames(tokenNames), _atn(atn), _ruleNames(ruleNames) {
for (int i = 0; i < _atn.getNumberOfDecisions(); i++) { for (int i = 0; i < _atn.getNumberOfDecisions(); i++) {
_decisionToDFA.push_back(new dfa::DFA(_atn.getDecisionState(i), i)); _decisionToDFA.push_back(dfa::DFA(_atn.getDecisionState(i), i));
} }
// identify the ATN states where pushNewRecursionContext must be called // identify the ATN states where pushNewRecursionContext must be called
@ -103,7 +103,7 @@ std::wstring ParserInterpreter::getGrammarFileName() const {
return _grammarFileName; return _grammarFileName;
} }
ParserRuleContextRef ParserInterpreter::parse(int startRuleIndex) { ParserRuleContext::Ref ParserInterpreter::parse(int startRuleIndex) {
atn::RuleStartState *startRuleStartState = _atn.ruleToStartState[(size_t)startRuleIndex]; atn::RuleStartState *startRuleStartState = _atn.ruleToStartState[(size_t)startRuleIndex];
std::shared_ptr<InterpreterRuleContext> rootContext = std::shared_ptr<InterpreterRuleContext> rootContext =
@ -135,7 +135,7 @@ ParserRuleContextRef ParserInterpreter::parse(int startRuleIndex) {
} }
} }
void ParserInterpreter::enterRecursionRule(ParserRuleContextRef localctx, int state, int ruleIndex, int precedence) { void ParserInterpreter::enterRecursionRule(ParserRuleContext::Ref localctx, int state, int ruleIndex, int precedence) {
_parentContextStack.push({ _ctx, localctx->invokingState }); _parentContextStack.push({ _ctx, localctx->invokingState });
Parser::enterRecursionRule(localctx, state, ruleIndex, precedence); Parser::enterRecursionRule(localctx, state, ruleIndex, precedence);
} }
@ -227,7 +227,7 @@ void ParserInterpreter::visitState(atn::ATNState *p) {
void ParserInterpreter::visitRuleStopState(atn::ATNState *p) { void ParserInterpreter::visitRuleStopState(atn::ATNState *p) {
atn::RuleStartState *ruleStartState = _atn.ruleToStartState[(size_t)p->ruleIndex]; atn::RuleStartState *ruleStartState = _atn.ruleToStartState[(size_t)p->ruleIndex];
if (ruleStartState->isPrecedenceRule) { if (ruleStartState->isPrecedenceRule) {
std::pair<ParserRuleContextRef, int> parentContext = _parentContextStack.top(); std::pair<ParserRuleContext::Ref, int> parentContext = _parentContextStack.top();
_parentContextStack.pop(); _parentContextStack.pop();
unrollRecursionContexts(parentContext.first); unrollRecursionContexts(parentContext.first);

View File

@ -67,9 +67,9 @@ namespace runtime {
virtual std::wstring getGrammarFileName() const override; virtual std::wstring getGrammarFileName() const override;
/// Begin parsing at startRuleIndex /// Begin parsing at startRuleIndex
virtual ParserRuleContextRef parse(int startRuleIndex); virtual ParserRuleContext::Ref parse(int startRuleIndex);
virtual void enterRecursionRule(ParserRuleContextRef localctx, int state, int ruleIndex, int precedence) override; virtual void enterRecursionRule(ParserRuleContext::Ref localctx, int state, int ruleIndex, int precedence) override;
protected: protected:
const std::wstring _grammarFileName; const std::wstring _grammarFileName;
@ -79,10 +79,10 @@ namespace runtime {
std::vector<std::wstring> _ruleNames; std::vector<std::wstring> _ruleNames;
antlrcpp::BitSet _pushRecursionContextStates; antlrcpp::BitSet _pushRecursionContextStates;
std::vector<dfa::DFA *> _decisionToDFA; // not shared like it is for generated parsers std::vector<dfa::DFA> _decisionToDFA; // not shared like it is for generated parsers
std::shared_ptr<atn::PredictionContextCache> _sharedContextCache; std::shared_ptr<atn::PredictionContextCache> _sharedContextCache;
std::stack<std::pair<ParserRuleContextRef, int>> _parentContextStack; std::stack<std::pair<ParserRuleContext::Ref, int>> _parentContextStack;
virtual atn::ATNState *getATNState(); virtual atn::ATNState *getATNState();
virtual void visitState(atn::ATNState *p); virtual void visitState(atn::ATNState *p);

View File

@ -67,7 +67,7 @@ std::shared_ptr<tree::TerminalNode> ParserRuleContext::addChild(std::shared_ptr<
return t; return t;
} }
RuleContextRef ParserRuleContext::addChild(RuleContextRef ruleInvocation) { RuleContext::Ref ParserRuleContext::addChild(RuleContext::Ref ruleInvocation) {
children.push_back(ruleInvocation); children.push_back(ruleInvocation);
return ruleInvocation; return ruleInvocation;
} }
@ -78,14 +78,14 @@ void ParserRuleContext::removeLastChild() {
} }
} }
std::shared_ptr<tree::TerminalNode> ParserRuleContext::addChild(TokenRef matchedToken) { std::shared_ptr<tree::TerminalNode> ParserRuleContext::addChild(Token::Ref matchedToken) {
std::shared_ptr<tree::TerminalNodeImpl> t = std::make_shared<tree::TerminalNodeImpl>(matchedToken); std::shared_ptr<tree::TerminalNodeImpl> t = std::make_shared<tree::TerminalNodeImpl>(matchedToken);
addChild(t); addChild(t);
t->parent = shared_from_this(); t->parent = shared_from_this();
return t; return t;
} }
std::shared_ptr<tree::ErrorNode> ParserRuleContext::addErrorNode(TokenRef badToken) { std::shared_ptr<tree::ErrorNode> ParserRuleContext::addErrorNode(Token::Ref badToken) {
std::shared_ptr<tree::ErrorNodeImpl> t = std::make_shared<tree::ErrorNodeImpl>(badToken); std::shared_ptr<tree::ErrorNodeImpl> t = std::make_shared<tree::ErrorNodeImpl>(badToken);
addChild(t); addChild(t);
t->parent = shared_from_this(); t->parent = shared_from_this();
@ -105,7 +105,7 @@ std::shared_ptr<tree::TerminalNode> ParserRuleContext::getToken(int ttype, std::
for (auto o : children) { for (auto o : children) {
if (is<tree::TerminalNode>(o)) { if (is<tree::TerminalNode>(o)) {
std::shared_ptr<tree::TerminalNode> tnode = std::dynamic_pointer_cast<tree::TerminalNode>(o); std::shared_ptr<tree::TerminalNode> tnode = std::dynamic_pointer_cast<tree::TerminalNode>(o);
TokenRef symbol = tnode->getSymbol(); Token::Ref symbol = tnode->getSymbol();
if (symbol->getType() == ttype) { if (symbol->getType() == ttype) {
if (j++ == i) { if (j++ == i) {
return tnode; return tnode;
@ -122,7 +122,7 @@ std::vector<std::shared_ptr<tree::TerminalNode>> ParserRuleContext::getTokens(in
for (auto &o : children) { for (auto &o : children) {
if (is<tree::TerminalNode>(o)) { if (is<tree::TerminalNode>(o)) {
std::shared_ptr<tree::TerminalNode> tnode = std::dynamic_pointer_cast<tree::TerminalNode>(o); std::shared_ptr<tree::TerminalNode> tnode = std::dynamic_pointer_cast<tree::TerminalNode>(o);
TokenRef symbol = tnode->getSymbol(); Token::Ref symbol = tnode->getSymbol();
if (symbol->getType() == ttype) { if (symbol->getType() == ttype) {
tokens.push_back(tnode); tokens.push_back(tnode);
} }
@ -145,11 +145,11 @@ misc::Interval ParserRuleContext::getSourceInterval() {
return misc::Interval(start->getTokenIndex(), stop->getTokenIndex()); return misc::Interval(start->getTokenIndex(), stop->getTokenIndex());
} }
TokenRef ParserRuleContext::getStart() { Token::Ref ParserRuleContext::getStart() {
return start; return start;
} }
TokenRef ParserRuleContext::getStop() { Token::Ref ParserRuleContext::getStop() {
return stop; return stop;
} }

View File

@ -31,6 +31,7 @@
#pragma once #pragma once
#include "Token.h"
#include "RuleContext.h" #include "RuleContext.h"
#include "CPPUtils.h" #include "CPPUtils.h"
@ -64,14 +65,14 @@ namespace runtime {
/// satisfy the superclass interface. /// satisfy the superclass interface.
/// </summary> /// </summary>
class ParserRuleContext : public RuleContext { class ParserRuleContext : public RuleContext {
/// <summary> public:
typedef std::shared_ptr<ParserRuleContext> Ref;
/// If we are debugging or building a parse tree for a visitor, /// If we are debugging or building a parse tree for a visitor,
/// we need to track all of the tokens and rule invocations associated /// we need to track all of the tokens and rule invocations associated
/// with this rule's context. This is empty for parsing w/o tree constr. /// with this rule's context. This is empty for parsing w/o tree constr.
/// operation because we don't the need to track the details about /// operation because we don't the need to track the details about
/// how we parse this rule. /// how we parse this rule.
/// </summary>
public:
std::vector<std::shared_ptr<ParseTree>> children; std::vector<std::shared_ptr<ParseTree>> children;
/// <summary> /// <summary>
@ -95,7 +96,7 @@ namespace runtime {
/// </summary> /// </summary>
// public List<Integer> states; // public List<Integer> states;
TokenRef start, stop; Token::Ref start, stop;
/// The exception that forced this rule to return. If the rule successfully /// The exception that forced this rule to return. If the rule successfully
/// completed, this is "null exception pointer". /// completed, this is "null exception pointer".
@ -117,16 +118,16 @@ namespace runtime {
/// Does not set parent link; other add methods do that. /// Does not set parent link; other add methods do that.
virtual std::shared_ptr<tree::TerminalNode> addChild(std::shared_ptr<tree::TerminalNode> t); virtual std::shared_ptr<tree::TerminalNode> addChild(std::shared_ptr<tree::TerminalNode> t);
virtual RuleContextRef addChild(RuleContextRef ruleInvocation); virtual RuleContext::Ref addChild(RuleContext::Ref ruleInvocation);
/// Used by enterOuterAlt to toss out a RuleContext previously added as /// Used by enterOuterAlt to toss out a RuleContext previously added as
/// we entered a rule. If we have # label, we will need to remove /// we entered a rule. If we have # label, we will need to remove
/// generic ruleContext object. /// generic ruleContext object.
virtual void removeLastChild(); virtual void removeLastChild();
virtual std::shared_ptr<tree::TerminalNode> addChild(TokenRef matchedToken); virtual std::shared_ptr<tree::TerminalNode> addChild(Token::Ref matchedToken);
virtual std::shared_ptr<tree::ErrorNode> addErrorNode(TokenRef badToken); virtual std::shared_ptr<tree::ErrorNode> addErrorNode(Token::Ref badToken);
std::weak_ptr<ParserRuleContext> getParent() { return std::dynamic_pointer_cast<ParserRuleContext>(getParentReference().lock()); }; std::weak_ptr<ParserRuleContext> getParent() { return std::dynamic_pointer_cast<ParserRuleContext>(getParentReference().lock()); };
@ -166,8 +167,8 @@ namespace runtime {
virtual std::size_t getChildCount() override; virtual std::size_t getChildCount() override;
virtual misc::Interval getSourceInterval() override; virtual misc::Interval getSourceInterval() override;
virtual TokenRef getStart(); virtual Token::Ref getStart();
virtual TokenRef getStop(); virtual Token::Ref getStop();
/// <summary> /// <summary>
/// Used for rule context info debugging during parse-time, not so much for ATN debugging </summary> /// Used for rule context info debugging during parse-time, not so much for ATN debugging </summary>

View File

@ -33,23 +33,47 @@
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::addErrorListener(ANTLRErrorListener *listener) {
if (listener == nullptr) {
throw L"listener cannot be null.";
}
_delegates.insert(listener);
}
void ProxyErrorListener::removeErrorListener(ANTLRErrorListener *listener) {
_delegates.erase(listener);
}
void ProxyErrorListener::removeErrorListeners() {
_delegates.clear();
}
void ProxyErrorListener::syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol, size_t line,
int charPositionInLine, const std::wstring &msg, std::exception_ptr e) {
for (auto listener : _delegates) {
listener->syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
}
}
void ProxyErrorListener::reportAmbiguity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
bool exact, const antlrcpp::BitSet &ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) { bool exact, const 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, const dfa::DFA &dfa, size_t startIndex,
size_t stopIndex, const antlrcpp::BitSet &conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) { size_t stopIndex, const 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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
int prediction, std::shared_ptr<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

@ -44,30 +44,23 @@ namespace runtime {
/// listeners. /// listeners.
class ProxyErrorListener : public ANTLRErrorListener { class ProxyErrorListener : public ANTLRErrorListener {
private: private:
std::vector<ANTLRErrorListener*> *const delegates; std::set<ANTLRErrorListener *> _delegates; // Not owned.
public: public:
template<typename T1> //where T1 : ANTLRErrorListener void addErrorListener(ANTLRErrorListener *listener);
ProxyErrorListener(std::vector<T1> *delegates) : delegates(delegates) { void removeErrorListener(ANTLRErrorListener *listener);
if (delegates == nullptr) { void removeErrorListeners();
throw NullPointerException("delegates");
}
}
void syntaxError(IRecognizer *recognizer, TokenRef offendingSymbol, size_t line, int charPositionInLine, void syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol, size_t line, int charPositionInLine,
const std::wstring &msg, std::exception_ptr e) override { const std::wstring &msg, std::exception_ptr e) override;
for (auto listener : *delegates) {
listener->syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
}
}
virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact, virtual void reportAmbiguity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact,
const antlrcpp::BitSet &ambigAlts, std::shared_ptr<atn::ATNConfigSet> configs) override; const 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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
const antlrcpp::BitSet &conflictingAlts, std::shared_ptr<atn::ATNConfigSet> configs) override; const 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, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex,
int prediction, std::shared_ptr<atn::ATNConfigSet> configs) override; int prediction, std::shared_ptr<atn::ATNConfigSet> configs) override;
}; };

View File

@ -39,12 +39,12 @@
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
RecognitionException::RecognitionException(IRecognizer *recognizer, IntStream *input, RecognitionException::RecognitionException(IRecognizer *recognizer, IntStream *input,
ParserRuleContextRef ctx, TokenRef offendingToken) ParserRuleContext::Ref ctx, Token::Ref offendingToken)
: RecognitionException("", recognizer, input, ctx, offendingToken) { : RecognitionException("", recognizer, input, ctx, offendingToken) {
} }
RecognitionException::RecognitionException(const std::string &message, IRecognizer *recognizer, IntStream *input, RecognitionException::RecognitionException(const std::string &message, IRecognizer *recognizer, IntStream *input,
ParserRuleContextRef ctx, TokenRef offendingToken) ParserRuleContext::Ref ctx, Token::Ref offendingToken)
: RuntimeException(message), _recognizer(recognizer), _input(input), _offendingToken(offendingToken), _ctx(ctx) { : RuntimeException(message), _recognizer(recognizer), _input(input), _offendingToken(offendingToken), _ctx(ctx) {
InitializeInstanceFields(); InitializeInstanceFields();
if (recognizer != nullptr) { if (recognizer != nullptr) {
@ -67,7 +67,7 @@ misc::IntervalSet RecognitionException::getExpectedTokens() const {
return misc::IntervalSet::EMPTY_SET; return misc::IntervalSet::EMPTY_SET;
} }
RuleContextRef RecognitionException::getCtx() const { RuleContext::Ref RecognitionException::getCtx() const {
return _ctx; return _ctx;
} }
@ -75,7 +75,7 @@ IntStream* RecognitionException::getInputStream() const {
return _input; return _input;
} }
TokenRef RecognitionException::getOffendingToken() const { Token::Ref RecognitionException::getOffendingToken() const {
return _offendingToken; return _offendingToken;
} }

View File

@ -36,6 +36,7 @@
#include "IRecognizer.h" #include "IRecognizer.h"
#include "IntStream.h" #include "IntStream.h"
#include "RuleContext.h" #include "RuleContext.h"
#include "ParserRuleContext.h"
#include "Token.h" #include "Token.h"
namespace org { namespace org {
@ -53,20 +54,20 @@ namespace runtime {
/// The Recognizer where this exception originated. /// The Recognizer where this exception originated.
IRecognizer *_recognizer; IRecognizer *_recognizer;
IntStream *_input; IntStream *_input;
RuleContextRef _ctx; ParserRuleContext::Ref _ctx;
/// The current Token when an error occurred. Since not all streams /// The current Token when an error occurred. Since not all streams
/// support accessing symbols by index, we have to track the Token /// support accessing symbols by index, we have to track the Token
/// instance itself. /// instance itself.
TokenRef _offendingToken; Token::Ref _offendingToken;
int _offendingState; int _offendingState;
public: public:
RecognitionException(IRecognizer *recognizer, IntStream *input, ParserRuleContextRef ctx, RecognitionException(IRecognizer *recognizer, IntStream *input, ParserRuleContext::Ref ctx,
TokenRef offendingToken = TokenRef()); Token::Ref offendingToken = Token::Ref());
RecognitionException(const std::string &message, IRecognizer *recognizer, IntStream *input, RecognitionException(const std::string &message, IRecognizer *recognizer, IntStream *input,
ParserRuleContextRef ctx, TokenRef offendingToken = TokenRef()); ParserRuleContext::Ref ctx, Token::Ref offendingToken = Token::Ref());
/// Get the ATN state number the parser was in at the time the error /// Get the ATN state number the parser was in at the time the error
/// occurred. For NoViableAltException and /// occurred. For NoViableAltException and
@ -98,7 +99,7 @@ namespace runtime {
/// </summary> /// </summary>
/// <returns> The <seealso cref="RuleContext"/> at the time this exception was thrown. /// <returns> The <seealso cref="RuleContext"/> at the time this exception was thrown.
/// If the context is not available, this method returns {@code null}. </returns> /// If the context is not available, this method returns {@code null}. </returns>
virtual RuleContextRef getCtx() const; virtual RuleContext::Ref getCtx() const;
/// <summary> /// <summary>
/// Gets the input stream which is the symbol source for the recognizer where /// Gets the input stream which is the symbol source for the recognizer where
@ -111,7 +112,7 @@ namespace runtime {
/// available. </returns> /// available. </returns>
virtual IntStream* getInputStream() const; virtual IntStream* getInputStream() const;
virtual TokenRef getOffendingToken() const; virtual Token::Ref getOffendingToken() const;
/// <summary> /// <summary>
/// Gets the <seealso cref="Recognizer"/> where this exception occurred. /// Gets the <seealso cref="Recognizer"/> where this exception occurred.

View File

@ -29,10 +29,10 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "ConsoleErrorListener.h"
#include "RecognitionException.h" #include "RecognitionException.h"
#include "CPPUtils.h" #include "CPPUtils.h"
#include "Strings.h" #include "Strings.h"
#include "ProxyErrorListener.h"
#include "Token.h" #include "Token.h"
#include "CPPUtils.h" #include "CPPUtils.h"
@ -43,6 +43,11 @@ using namespace org::antlr::v4::runtime;
std::map<std::vector<std::wstring>, std::map<std::wstring, int>> Recognizer::_tokenTypeMapCache; std::map<std::vector<std::wstring>, std::map<std::wstring, int>> Recognizer::_tokenTypeMapCache;
std::map<std::vector<std::wstring>, std::map<std::wstring, int>> Recognizer::_ruleIndexMapCache; std::map<std::vector<std::wstring>, std::map<std::wstring, int>> Recognizer::_ruleIndexMapCache;
Recognizer::Recognizer() {
InitializeInstanceFields();
_proxListener.addErrorListener(&ConsoleErrorListener::INSTANCE);
}
std::map<std::wstring, int> Recognizer::getTokenTypeMap() { std::map<std::wstring, int> Recognizer::getTokenTypeMap() {
const std::vector<std::wstring>& tokenNames = getTokenNames(); const std::vector<std::wstring>& tokenNames = getTokenNames();
if (tokenNames.empty()) { if (tokenNames.empty()) {
@ -120,37 +125,30 @@ std::wstring Recognizer::getTokenErrorDisplay(Token *t) {
} }
void Recognizer::addErrorListener(ANTLRErrorListener *listener) { void Recognizer::addErrorListener(ANTLRErrorListener *listener) {
if (listener == nullptr) { _proxListener.addErrorListener(listener);
throw L"listener cannot be null.";
}
_listeners.push_back(listener);
} }
void Recognizer::removeErrorListener(ANTLRErrorListener *listener) { void Recognizer::removeErrorListener(ANTLRErrorListener *listener) {
//_listeners.remove(listener); does this work the same way? _proxListener.removeErrorListener(listener);
std::vector<ANTLRErrorListener*>::iterator it;
it = std::find(_listeners.begin(), _listeners.end(), listener);
_listeners.erase(it);
} }
void Recognizer::removeErrorListeners() { void Recognizer::removeErrorListeners() {
_listeners.clear(); _proxListener.removeErrorListeners();
} }
ANTLRErrorListener *Recognizer::getErrorListenerDispatch() { ProxyErrorListener& Recognizer::getErrorListenerDispatch() {
return (ANTLRErrorListener *)new ProxyErrorListener(getErrorListeners()); return _proxListener;
} }
bool Recognizer::sempred(RuleContextRef localctx, int ruleIndex, int actionIndex) { bool Recognizer::sempred(RuleContext::Ref localctx, int ruleIndex, int actionIndex) {
return true; return true;
} }
bool Recognizer::precpred(RuleContextRef localctx, int precedence) { bool Recognizer::precpred(RuleContext::Ref localctx, int precedence) {
return true; return true;
} }
void Recognizer::action(RuleContextRef localctx, int ruleIndex, int actionIndex) { void Recognizer::action(RuleContext::Ref localctx, int ruleIndex, int actionIndex) {
} }
int Recognizer::getState() { int Recognizer::getState() {
@ -165,9 +163,5 @@ void Recognizer::setState(int atnState) {
void Recognizer::InitializeInstanceFields() { void Recognizer::InitializeInstanceFields() {
_stateNumber = -1; _stateNumber = -1;
_interpreter = nullptr; _interpreter = nullptr;
_listeners = std::vector<ANTLRErrorListener*>();
} }
Recognizer::Recognizer() {
InitializeInstanceFields();
}

View File

@ -31,6 +31,7 @@
#pragma once #pragma once
#include "ProxyErrorListener.h"
#include "IRecognizer.h" #include "IRecognizer.h"
namespace org { namespace org {
@ -40,16 +41,15 @@ namespace runtime {
class Recognizer : public IRecognizer { class Recognizer : public IRecognizer {
public: public:
//static const int _EOF = -1; ml: we don't need it IntStream, nor in Token, nor here
Recognizer(); Recognizer();
private: private:
static std::map<std::vector<std::wstring>, std::map<std::wstring, int>> _tokenTypeMapCache; static std::map<std::vector<std::wstring>, std::map<std::wstring, int>> _tokenTypeMapCache;
static std::map<std::vector<std::wstring>, std::map<std::wstring, int>> _ruleIndexMapCache; static std::map<std::vector<std::wstring>, std::map<std::wstring, int>> _ruleIndexMapCache;
std::vector<ANTLRErrorListener*> _listeners; ProxyErrorListener _proxListener; // Manages a collection of listeners.
//Mutex to manage synchronized access for multithreading
// Mutex to manage synchronized access for multithreading.
std::mutex mtx; std::mutex mtx;
protected: protected:
@ -130,19 +130,15 @@ namespace runtime {
virtual void removeErrorListeners(); virtual void removeErrorListeners();
virtual std::vector<ANTLRErrorListener *> *getErrorListeners() { virtual ProxyErrorListener& getErrorListenerDispatch();
return &_listeners;
}
virtual ANTLRErrorListener *getErrorListenerDispatch();
// subclass needs to override these if there are sempreds or actions // subclass needs to override these if there are sempreds or actions
// that the ATN interp needs to execute // that the ATN interp needs to execute
virtual bool sempred(RuleContextRef localctx, int ruleIndex, int actionIndex); virtual bool sempred(RuleContext::Ref localctx, int ruleIndex, int actionIndex);
virtual bool precpred(RuleContextRef localctx, int precedence); virtual bool precpred(RuleContext::Ref localctx, int precedence);
virtual void action(RuleContextRef localctx, int ruleIndex, int actionIndex); virtual void action(RuleContext::Ref localctx, int ruleIndex, int actionIndex);
int getState(); int getState();

View File

@ -37,7 +37,7 @@
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
const ParserRuleContextRef RuleContext::EMPTY = std::make_shared<ParserRuleContext>(); const RuleContext::Ref RuleContext::EMPTY = std::make_shared<ParserRuleContext>();
RuleContext::RuleContext() { RuleContext::RuleContext() {
InitializeInstanceFields(); InitializeInstanceFields();
@ -51,7 +51,7 @@ RuleContext::RuleContext(std::weak_ptr<RuleContext> parent, int invokingState) {
int RuleContext::depth() { int RuleContext::depth() {
int n = 1; int n = 1;
RuleContextRef p = shared_from_this(); RuleContext::Ref p = shared_from_this();
while (true) { while (true) {
if (p->parent.expired()) if (p->parent.expired())
break; break;
@ -69,7 +69,7 @@ misc::Interval RuleContext::getSourceInterval() {
return misc::Interval::INVALID; return misc::Interval::INVALID;
} }
RuleContextRef RuleContext::getRuleContext() { RuleContext::Ref RuleContext::getRuleContext() {
return shared_from_this(); return shared_from_this();
} }
@ -148,14 +148,14 @@ std::wstring RuleContext::toStringTree() {
std::wstring RuleContext::toString(const std::vector<std::wstring> &ruleNames) { std::wstring RuleContext::toString(const std::vector<std::wstring> &ruleNames) {
return toString(ruleNames, RuleContextRef()); return toString(ruleNames, RuleContext::Ref());
} }
std::wstring RuleContext::toString(const std::vector<std::wstring> &ruleNames, RuleContextRef stop) { std::wstring RuleContext::toString(const std::vector<std::wstring> &ruleNames, RuleContext::Ref stop) {
std::wstringstream ss; std::wstringstream ss;
RuleContextRef parent = shared_from_this(); RuleContext::Ref parent = shared_from_this();
ss << L"["; ss << L"[";
while (parent != stop) { while (parent != stop) {
if (ruleNames.empty()) { if (ruleNames.empty()) {
@ -190,7 +190,7 @@ std::wstring RuleContext::toString(Recognizer *recog) {
return toString(recog, ParserRuleContext::EMPTY); return toString(recog, ParserRuleContext::EMPTY);
} }
std::wstring RuleContext::toString(Recognizer *recog, RuleContextRef stop) { std::wstring RuleContext::toString(Recognizer *recog, RuleContext::Ref stop) {
return toString(recog->getRuleNames(), stop); return toString(recog->getRuleNames(), stop);
} }

View File

@ -60,7 +60,9 @@ namespace runtime {
/// <seealso cref= ParserRuleContext </seealso> /// <seealso cref= ParserRuleContext </seealso>
class RuleContext : public tree::RuleNode, public std::enable_shared_from_this<RuleContext> { class RuleContext : public tree::RuleNode, public std::enable_shared_from_this<RuleContext> {
public: public:
static const ParserRuleContextRef EMPTY; typedef std::shared_ptr<RuleContext> Ref;
static const RuleContext::Ref EMPTY;
/// <summary> /// <summary>
/// What context invoked this rule? </summary> /// What context invoked this rule? </summary>
@ -86,7 +88,7 @@ namespace runtime {
virtual misc::Interval getSourceInterval() override; virtual misc::Interval getSourceInterval() override;
virtual RuleContextRef getRuleContext() override; virtual RuleContext::Ref getRuleContext() override;
virtual std::wstring getText() override; virtual std::wstring getText() override;
virtual ssize_t getRuleIndex() const; virtual ssize_t getRuleIndex() const;
@ -133,9 +135,9 @@ namespace runtime {
std::wstring toString(const std::vector<std::wstring> &ruleNames); std::wstring toString(const std::vector<std::wstring> &ruleNames);
// recog null unless ParserRuleContext, in which case we use subclass toString(...) // recog null unless ParserRuleContext, in which case we use subclass toString(...)
std::wstring toString(Recognizer *recog, RuleContextRef stop); std::wstring toString(Recognizer *recog, RuleContext::Ref stop);
virtual std::wstring toString(const std::vector<std::wstring> &ruleNames, RuleContextRef stop); virtual std::wstring toString(const std::vector<std::wstring> &ruleNames, RuleContext::Ref stop);
bool operator == (const RuleContext &other) { return this == &other; } // Simple address comparison. bool operator == (const RuleContext &other) { return this == &other; } // Simple address comparison.

View File

@ -45,6 +45,8 @@ namespace runtime {
/// </summary> /// </summary>
class Token { class Token {
public: public:
typedef std::shared_ptr<Token> Ref;
static const size_t INVALID_TYPE = 0; static const size_t INVALID_TYPE = 0;
/// <summary> /// <summary>

View File

@ -31,6 +31,7 @@
#pragma once #pragma once
#include "Token.h"
#include "TokenFactory.h" #include "TokenFactory.h"
namespace org { namespace org {
@ -61,7 +62,7 @@ namespace runtime {
/// to the parser. /// to the parser.
/// </summary> /// </summary>
public: public:
virtual TokenRef nextToken() = 0; virtual Token::Ref nextToken() = 0;
/// <summary> /// <summary>
/// Get the line number for the current position in the input stream. The /// Get the line number for the current position in the input stream. The

View File

@ -31,6 +31,7 @@
#pragma once #pragma once
#include "Token.h"
#include "IntStream.h" #include "IntStream.h"
namespace org { namespace org {
@ -53,7 +54,7 @@ namespace runtime {
public: public:
virtual ~TokenStream(); virtual ~TokenStream();
virtual TokenRef LT(ssize_t k) = 0; virtual Token::Ref LT(ssize_t k) = 0;
/// <summary> /// <summary>
/// Gets the <seealso cref="Token"/> at the specified {@code index} in the stream. When /// Gets the <seealso cref="Token"/> at the specified {@code index} in the stream. When
@ -72,7 +73,7 @@ namespace runtime {
/// <exception cref="IllegalArgumentException"> if {code index} is less than 0 </exception> /// <exception cref="IllegalArgumentException"> if {code index} is less than 0 </exception>
/// <exception cref="UnsupportedOperationException"> if the stream does not support /// <exception cref="UnsupportedOperationException"> if the stream does not support
/// retrieving the token at the specified index </exception> /// retrieving the token at the specified index </exception>
virtual TokenRef get(size_t index) const = 0; virtual Token::Ref get(size_t index) const = 0;
/// Gets the underlying TokenSource which provides tokens for this stream. /// Gets the underlying TokenSource which provides tokens for this stream.
virtual TokenSource* getTokenSource() const = 0; virtual TokenSource* getTokenSource() const = 0;
@ -160,7 +161,7 @@ namespace runtime {
/// </returns> /// </returns>
/// <exception cref="UnsupportedOperationException"> if this stream does not support /// <exception cref="UnsupportedOperationException"> if this stream does not support
/// this method for the specified tokens </exception> /// this method for the specified tokens </exception>
virtual std::wstring getText(TokenRef start, TokenRef stop) = 0; virtual std::wstring getText(Token::Ref start, Token::Ref stop) = 0;
}; };
} // namespace runtime } // namespace runtime

View File

@ -287,7 +287,7 @@ std::wstring TokenStreamRewriter::getText(const std::wstring &programName, const
while (i <= (size_t)stop && i < tokens->size()) { while (i <= (size_t)stop && i < tokens->size()) {
RewriteOperation *op = indexToOp[i]; RewriteOperation *op = indexToOp[i];
indexToOp.erase(i); // remove so any left have index size-1 indexToOp.erase(i); // remove so any left have index size-1
TokenRef t = tokens->get(i); Token::Ref t = tokens->get(i);
if (op == nullptr) { if (op == nullptr) {
// no operation at that index, just dump token // no operation at that index, just dump token
if (t->getType() != EOF) { if (t->getType() != EOF) {

View File

@ -85,7 +85,7 @@ size_t UnbufferedCharStream::fill(size_t n) {
size_t c = nextChar(); size_t c = nextChar();
add(c); add(c);
} catch (IOException &ioe) { } catch (IOException &ioe) {
throw std::exception(ioe); std::throw_with_nested(RuntimeException());
} }
} }

View File

@ -54,7 +54,7 @@ UnbufferedTokenStream::UnbufferedTokenStream(TokenSource *tokenSource, int buffe
UnbufferedTokenStream::~UnbufferedTokenStream() { UnbufferedTokenStream::~UnbufferedTokenStream() {
} }
TokenRef UnbufferedTokenStream::get(size_t i) const Token::Ref UnbufferedTokenStream::get(size_t i) const
{ // get absolute index { // get absolute index
size_t bufferStartIndex = getBufferStartIndex(); size_t bufferStartIndex = getBufferStartIndex();
if (i < bufferStartIndex || i >= bufferStartIndex + _tokens.size()) { if (i < bufferStartIndex || i >= bufferStartIndex + _tokens.size()) {
@ -64,7 +64,7 @@ TokenRef UnbufferedTokenStream::get(size_t i) const
return _tokens[i - bufferStartIndex]; return _tokens[i - bufferStartIndex];
} }
TokenRef UnbufferedTokenStream::LT(ssize_t i) Token::Ref UnbufferedTokenStream::LT(ssize_t i)
{ {
if (i == -1) { if (i == -1) {
return _lastToken; return _lastToken;
@ -104,7 +104,7 @@ std::wstring UnbufferedTokenStream::getText(RuleContext* ctx)
return getText(ctx->getSourceInterval()); return getText(ctx->getSourceInterval());
} }
std::wstring UnbufferedTokenStream::getText(TokenRef start, TokenRef stop) std::wstring UnbufferedTokenStream::getText(Token::Ref start, Token::Ref stop)
{ {
return getText(misc::Interval(start->getTokenIndex(), stop->getTokenIndex())); return getText(misc::Interval(start->getTokenIndex(), stop->getTokenIndex()));
} }
@ -156,16 +156,16 @@ size_t UnbufferedTokenStream::fill(size_t n)
return i; return i;
} }
TokenRef t = _tokenSource->nextToken(); Token::Ref t = _tokenSource->nextToken();
add(t); add(t);
} }
return n; return n;
} }
void UnbufferedTokenStream::add(TokenRef t) void UnbufferedTokenStream::add(Token::Ref t)
{ {
WritableTokenRef writable = std::dynamic_pointer_cast<WritableToken>(t); WritableToken::Ref writable = std::dynamic_pointer_cast<WritableToken>(t);
if (writable) { if (writable) {
writable->setTokenIndex(int(getBufferStartIndex() + _tokens.size())); writable->setTokenIndex(int(getBufferStartIndex() + _tokens.size()));
} }
@ -203,7 +203,7 @@ void UnbufferedTokenStream::release(ssize_t marker)
if (_p > 0) { if (_p > 0) {
// Copy tokens[p]..tokens[n-1] to tokens[0]..tokens[(n-1)-p], reset ptrs // Copy tokens[p]..tokens[n-1] to tokens[0]..tokens[(n-1)-p], reset ptrs
// p is last valid token; move nothing if p==n as we have no valid char // p is last valid token; move nothing if p==n as we have no valid char
std::vector<TokenRef>(_tokens.begin() + (ssize_t)_p, _tokens.end()).swap(_tokens); std::vector<Token::Ref>(_tokens.begin() + (ssize_t)_p, _tokens.end()).swap(_tokens);
_p = 0; _p = 0;
} }
@ -274,7 +274,7 @@ std::wstring UnbufferedTokenStream::getText(const misc::Interval &interval)
std::wstringstream ss; std::wstringstream ss;
for (size_t i = a; i <= b; i++) { for (size_t i = a; i <= b; i++) {
TokenRef t = _tokens[i]; Token::Ref t = _tokens[i];
if (i > 0) if (i > 0)
ss << L", "; ss << L", ";
ss << t->getText(); ss << t->getText();

View File

@ -44,8 +44,8 @@ namespace runtime {
UnbufferedTokenStream(TokenSource *tokenSource, int bufferSize); UnbufferedTokenStream(TokenSource *tokenSource, int bufferSize);
virtual ~UnbufferedTokenStream(); virtual ~UnbufferedTokenStream();
virtual TokenRef get(size_t i) const override; virtual Token::Ref get(size_t i) const override;
virtual TokenRef LT(ssize_t i) override; virtual Token::Ref LT(ssize_t i) override;
virtual ssize_t LA(ssize_t i) override; virtual ssize_t LA(ssize_t i) override;
virtual TokenSource* getTokenSource() const override; virtual TokenSource* getTokenSource() const override;
@ -53,7 +53,7 @@ namespace runtime {
virtual std::wstring getText(const misc::Interval &interval) override; virtual std::wstring getText(const misc::Interval &interval) override;
virtual std::wstring getText() override; virtual std::wstring getText() override;
virtual std::wstring getText(RuleContext *ctx) override; virtual std::wstring getText(RuleContext *ctx) override;
virtual std::wstring getText(TokenRef start, TokenRef stop) override; virtual std::wstring getText(Token::Ref start, Token::Ref stop) override;
virtual void consume() override; virtual void consume() override;
@ -83,7 +83,7 @@ namespace runtime {
/// we start filling at index 0 again. /// we start filling at index 0 again.
/// </summary> /// </summary>
std::vector<TokenRef> _tokens; std::vector<Token::Ref> _tokens;
/// <summary> /// <summary>
/// 0..n-1 index into <seealso cref="#tokens tokens"/> of next token. /// 0..n-1 index into <seealso cref="#tokens tokens"/> of next token.
@ -104,13 +104,13 @@ namespace runtime {
/// <summary> /// <summary>
/// This is the {@code LT(-1)} token for the current position. /// This is the {@code LT(-1)} token for the current position.
/// </summary> /// </summary>
TokenRef _lastToken; Token::Ref _lastToken;
/// <summary> /// <summary>
/// When {@code numMarkers > 0}, this is the {@code LT(-1)} token for the /// When {@code numMarkers > 0}, this is the {@code LT(-1)} token for the
/// first token in <seealso cref="#tokens"/>. Otherwise, this is {@code null}. /// first token in <seealso cref="#tokens"/>. Otherwise, this is {@code null}.
/// </summary> /// </summary>
TokenRef _lastTokenBufferStart; Token::Ref _lastTokenBufferStart;
/// <summary> /// <summary>
/// Absolute token index. It's the index of the token about to be read via /// Absolute token index. It's the index of the token about to be read via
@ -130,7 +130,7 @@ namespace runtime {
/// then EOF was reached before {@code n} tokens could be added. /// then EOF was reached before {@code n} tokens could be added.
/// </summary> /// </summary>
virtual size_t fill(size_t n); virtual size_t fill(size_t n);
virtual void add(TokenRef t); virtual void add(Token::Ref t);
size_t getBufferStartIndex() const; size_t getBufferStartIndex() const;

View File

@ -40,6 +40,8 @@ namespace runtime {
class WritableToken : public Token { class WritableToken : public Token {
public: public:
typedef std::shared_ptr<WritableToken> Ref;
virtual void setText(const std::wstring &text) = 0; virtual void setText(const std::wstring &text) = 0;
virtual void setType(int ttype) = 0; virtual void setType(int ttype) = 0;

View File

@ -38,19 +38,21 @@
#include "Recognizer.h" #include "Recognizer.h"
#include "ATNType.h" #include "ATNType.h"
#include "Exceptions.h" #include "Exceptions.h"
#include "CPPUtils.h"
#include "ATN.h" #include "ATN.h"
using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime;
using namespace org::antlr::v4::runtime::atn; using namespace org::antlr::v4::runtime::atn;
using namespace antlrcpp;
ATN::ATN() : ATN(ATNType::LEXER, 0) { ATN::ATN() : ATN(ATNType::LEXER, 0) {
} }
ATN::ATN(ATNType grammarType, int maxTokenType) : grammarType(grammarType), maxTokenType(maxTokenType) { ATN::ATN(ATNType grammarType, size_t maxTokenType) : grammarType(grammarType), maxTokenType(maxTokenType) {
} }
misc::IntervalSet ATN::nextTokens(ATNState *s, RuleContextRef ctx) const { misc::IntervalSet ATN::nextTokens(ATNState *s, RuleContext::Ref ctx) const {
LL1Analyzer analyzer(*this); LL1Analyzer analyzer(*this);
return analyzer.LOOK(s, ctx); return analyzer.LOOK(s, ctx);
@ -66,7 +68,7 @@ misc::IntervalSet ATN::nextTokens(ATNState *s) const {
void ATN::addState(ATNState *state) { void ATN::addState(ATNState *state) {
if (state != nullptr) { if (state != nullptr) {
state->atn = this; //state->atn = this;
state->stateNumber = (int)states.size(); state->stateNumber = (int)states.size();
} }
@ -95,12 +97,12 @@ int ATN::getNumberOfDecisions() const {
return (int)decisionToState.size(); return (int)decisionToState.size();
} }
misc::IntervalSet ATN::getExpectedTokens(int stateNumber, RuleContextRef context) const { misc::IntervalSet ATN::getExpectedTokens(int stateNumber, RuleContext::Ref context) const {
if (stateNumber < 0 || stateNumber >= (int)states.size()) { if (stateNumber < 0 || stateNumber >= (int)states.size()) {
throw IllegalArgumentException("Invalid state number."); throw IllegalArgumentException("Invalid state number.");
} }
RuleContextRef ctx = context; RuleContext::Ref ctx = context;
ATNState *s = states.at((size_t)stateNumber); ATNState *s = states.at((size_t)stateNumber);
misc::IntervalSet following = nextTokens(s); misc::IntervalSet following = nextTokens(s);
if (!following.contains(Token::EPSILON)) { if (!following.contains(Token::EPSILON)) {
@ -129,3 +131,47 @@ misc::IntervalSet ATN::getExpectedTokens(int stateNumber, RuleContextRef context
return expected; return expected;
} }
std::wstring ATN::toString() const {
std::wstringstream ss;
std::wstring type;
switch (grammarType) {
case ATNType::LEXER:
type = L"LEXER ";
break;
case ATNType::PARSER:
type = L"PARSER ";
break;
default:
break;
}
ss << "(" << type << "ATN " << std::hex << this << std::dec << ") maxTokenType: " << maxTokenType << std::endl;
ss << "states (" << states.size() << ") {" << std::endl;
size_t index = 0;
for (auto state : states) {
if (state == nullptr) {
ss << " " << index++ << ": null" << std::endl;
} else {
std::wstring text = state->toString();
ss << " " << index++ << ": " << indent(text, L" ", false) << std::endl;
}
}
index = 0;
for (auto state : decisionToState) {
if (state == nullptr) {
ss << " " << index++ << ": null" << std::endl;
} else {
std::wstring text = state->toString();
ss << " " << index++ << ": " << indent(text, L" ", false) << std::endl;
}
}
ss << "}";
return ss.str();
}

View File

@ -31,6 +31,8 @@
#pragma once #pragma once
#include "RuleContext.h"
namespace org { namespace org {
namespace antlr { namespace antlr {
namespace v4 { namespace v4 {
@ -43,7 +45,7 @@ namespace atn {
/// Used for runtime deserialization of ATNs from strings. /// Used for runtime deserialization of ATNs from strings.
ATN(); ATN();
ATN(ATNType grammarType, int maxTokenType); ATN(ATNType grammarType, size_t maxTokenType);
std::vector<ATNState*> states; std::vector<ATNState*> states;
@ -62,7 +64,7 @@ namespace atn {
ATNType grammarType; ATNType grammarType;
/// The maximum value for any symbol recognized by a transition in the ATN. /// The maximum value for any symbol recognized by a transition in the ATN.
int maxTokenType; size_t maxTokenType;
/// <summary> /// <summary>
/// For lexer ATNs, this maps the rule index to the resulting token type. /// For lexer ATNs, this maps the rule index to the resulting token type.
@ -89,7 +91,7 @@ namespace atn {
/// the rule surrounding {@code s}. In other words, the set will be /// the rule surrounding {@code s}. In other words, the set will be
/// restricted to tokens reachable staying within {@code s}'s rule. /// restricted to tokens reachable staying within {@code s}'s rule.
/// </summary> /// </summary>
virtual misc::IntervalSet nextTokens(ATNState *s, RuleContextRef ctx) const; virtual misc::IntervalSet nextTokens(ATNState *s, RuleContext::Ref ctx) const;
/// <summary> /// <summary>
/// Compute the set of valid tokens that can occur starting in {@code s} and /// Compute the set of valid tokens that can occur starting in {@code s} and
@ -126,7 +128,9 @@ namespace atn {
/// specified state in the specified context. </returns> /// specified state in the specified context. </returns>
/// <exception cref="IllegalArgumentException"> if the ATN does not contain a state with /// <exception cref="IllegalArgumentException"> if the ATN does not contain a state with
/// number {@code stateNumber} </exception> /// number {@code stateNumber} </exception>
virtual misc::IntervalSet getExpectedTokens(int stateNumber, RuleContextRef context) const; virtual misc::IntervalSet getExpectedTokens(int stateNumber, RuleContext::Ref context) const;
std::wstring toString() const;
}; };
} // namespace atn } // namespace atn

View File

@ -36,11 +36,11 @@
using namespace org::antlr::v4::runtime::atn; using namespace org::antlr::v4::runtime::atn;
ATNConfig::ATNConfig(ATNState *state, int alt, PredictionContextRef context) ATNConfig::ATNConfig(ATNState *state, int alt, PredictionContext::Ref context)
: ATNConfig(state, alt, context, SemanticContext::NONE) { : ATNConfig(state, alt, context, SemanticContext::NONE) {
} }
ATNConfig::ATNConfig(ATNState *state, int alt, PredictionContextRef context, SemanticContextRef semanticContext) ATNConfig::ATNConfig(ATNState *state, int alt, PredictionContext::Ref context, SemanticContext::Ref semanticContext)
: state(state), alt(alt), context(context), semanticContext(semanticContext) { : state(state), alt(alt), context(context), semanticContext(semanticContext) {
reachesIntoOuterContext = 0; reachesIntoOuterContext = 0;
} }
@ -51,19 +51,19 @@ ATNConfig::ATNConfig(ATNConfig *c) : ATNConfig(c, c->state, c->context, c->seman
ATNConfig::ATNConfig(ATNConfig *c, ATNState *state) : ATNConfig(c, state, c->context, c->semanticContext) { ATNConfig::ATNConfig(ATNConfig *c, ATNState *state) : ATNConfig(c, state, c->context, c->semanticContext) {
} }
ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, SemanticContextRef semanticContext) ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, SemanticContext::Ref semanticContext)
: ATNConfig(c, state, c->context, semanticContext) { : ATNConfig(c, state, c->context, semanticContext) {
} }
ATNConfig::ATNConfig(ATNConfig *c, SemanticContextRef semanticContext) ATNConfig::ATNConfig(ATNConfig *c, SemanticContext::Ref semanticContext)
: ATNConfig(c, c->state, c->context, semanticContext) { : ATNConfig(c, c->state, c->context, semanticContext) {
} }
ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, PredictionContextRef context) ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, PredictionContext::Ref context)
: ATNConfig(c, state, context, c->semanticContext) { : ATNConfig(c, state, context, c->semanticContext) {
} }
ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, PredictionContextRef context, SemanticContextRef semanticContext) ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, PredictionContext::Ref context, SemanticContext::Ref semanticContext)
: state(state), alt(c->alt), context(context), semanticContext(semanticContext), reachesIntoOuterContext(c->reachesIntoOuterContext) { : state(state), alt(c->alt), context(context), semanticContext(semanticContext), reachesIntoOuterContext(c->reachesIntoOuterContext) {
} }

View File

@ -63,7 +63,7 @@ namespace atn {
/// execution of the ATN simulator. /// execution of the ATN simulator.
/// ///
/// Can be shared between multiple ANTConfig instances. /// Can be shared between multiple ANTConfig instances.
PredictionContextRef context; PredictionContext::Ref context;
/// We cannot execute predicates dependent upon local context unless /// We cannot execute predicates dependent upon local context unless
/// we know for sure we are in the correct context. Because there is /// we know for sure we are in the correct context. Because there is
@ -77,17 +77,17 @@ namespace atn {
int reachesIntoOuterContext; int reachesIntoOuterContext;
/// Can be shared between multiple ATNConfig instances. /// Can be shared between multiple ATNConfig instances.
SemanticContextRef semanticContext; SemanticContext::Ref semanticContext;
ATNConfig(ATNState *state, int alt, PredictionContextRef context); ATNConfig(ATNState *state, int alt, PredictionContext::Ref context);
ATNConfig(ATNState *state, int alt, PredictionContextRef context, SemanticContextRef semanticContext); ATNConfig(ATNState *state, int alt, PredictionContext::Ref context, SemanticContext::Ref semanticContext);
ATNConfig(ATNConfig *c); // dup ATNConfig(ATNConfig *c); // dup
ATNConfig(ATNConfig *c, ATNState *state); ATNConfig(ATNConfig *c, ATNState *state);
ATNConfig(ATNConfig *c, ATNState *state, SemanticContextRef semanticContext); ATNConfig(ATNConfig *c, ATNState *state, SemanticContext::Ref semanticContext);
ATNConfig(ATNConfig *c, SemanticContextRef semanticContext); ATNConfig(ATNConfig *c, SemanticContext::Ref semanticContext);
ATNConfig(ATNConfig *c, ATNState *state, PredictionContextRef context); ATNConfig(ATNConfig *c, ATNState *state, PredictionContext::Ref context);
ATNConfig(ATNConfig *c, ATNState *state, PredictionContextRef context, SemanticContextRef semanticContext); ATNConfig(ATNConfig *c, ATNState *state, PredictionContext::Ref context, SemanticContext::Ref semanticContext);
virtual size_t hashCode() const; virtual size_t hashCode() const;

View File

@ -96,7 +96,7 @@ bool ATNConfigSet::add(ATNConfig *config, PredictionContextMergeCache *mergeCach
} }
// a previous (s,i,pi,_), merge with it and save result // a previous (s,i,pi,_), merge with it and save result
bool rootIsWildcard = !fullCtx; bool rootIsWildcard = !fullCtx;
PredictionContextRef merged = PredictionContext::merge(existing->context, config->context, rootIsWildcard, mergeCache); PredictionContext::Ref merged = PredictionContext::merge(existing->context, config->context, rootIsWildcard, mergeCache);
// no need to check for existing.context, config.context in cache // no need to check for existing.context, config.context in cache
// since only way to create new graphs is "call rule" and here. We // since only way to create new graphs is "call rule" and here. We
// cache at both places. // cache at both places.
@ -124,8 +124,8 @@ std::vector<ATNState*>* ATNConfigSet::getStates() {
return states; return states;
} }
std::vector<SemanticContextRef> ATNConfigSet::getPredicates() { std::vector<SemanticContext::Ref> ATNConfigSet::getPredicates() {
std::vector<SemanticContextRef> preds; std::vector<SemanticContext::Ref> preds;
for (auto c : configs) { for (auto c : configs) {
if (c->semanticContext != SemanticContext::NONE) { if (c->semanticContext != SemanticContext::NONE) {
preds.push_back(c->semanticContext); preds.push_back(c->semanticContext);

View File

@ -106,7 +106,7 @@ namespace atn {
virtual std::vector<ATNState*> *getStates(); virtual std::vector<ATNState*> *getStates();
virtual std::vector<SemanticContextRef> getPredicates(); virtual std::vector<SemanticContext::Ref> getPredicates();
virtual ATNConfig *get(size_t i) const; virtual ATNConfig *get(size_t i) const;

View File

@ -35,12 +35,11 @@ using namespace org::antlr::v4::runtime::atn;
ATNDeserializationOptions ATNDeserializationOptions::defaultOptions; ATNDeserializationOptions ATNDeserializationOptions::defaultOptions;
ATNDeserializationOptions::ATNDeserializationOptions() ATNDeserializationOptions::ATNDeserializationOptions() {
: readOnly(true) { InitializeInstanceFields();
} }
ATNDeserializationOptions::ATNDeserializationOptions(ATNDeserializationOptions *options) { ATNDeserializationOptions::ATNDeserializationOptions(ATNDeserializationOptions *options) : ATNDeserializationOptions() {
InitializeInstanceFields();
this->verifyATN = options->verifyATN; this->verifyATN = options->verifyATN;
this->generateRuleBypassTransitions = options->generateRuleBypassTransitions; this->generateRuleBypassTransitions = options->generateRuleBypassTransitions;
} }
@ -83,6 +82,6 @@ void ATNDeserializationOptions::throwIfReadOnly() {
void ATNDeserializationOptions::InitializeInstanceFields() { void ATNDeserializationOptions::InitializeInstanceFields() {
readOnly = false; readOnly = false;
verifyATN = false; verifyATN = true;
generateRuleBypassTransitions = false; generateRuleBypassTransitions = false;
} }

View File

@ -63,27 +63,38 @@
#include "IntervalSet.h" #include "IntervalSet.h"
#include "Exceptions.h" #include "Exceptions.h"
#include "CPPUtils.h"
#include "Strings.h"
#include "ATNDeserializer.h" #include "ATNDeserializer.h"
using namespace org::antlr::v4::runtime::atn; using namespace org::antlr::v4::runtime::atn;
using namespace antlrcpp;
const size_t ATNDeserializer::SERIALIZED_VERSION = 3; const size_t ATNDeserializer::SERIALIZED_VERSION;
/** /**
* This value should never change. Updates following this version are * This value should never change. Updates following this version are
* reflected as change in the unique ID SERIALIZED_UUID. * reflected as change in the unique ID SERIALIZED_UUID.
*/ */
const Guid ATNDeserializer::BASE_SERIALIZED_UUID("33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3"); Guid ATNDeserializer::BASE_SERIALIZED_UUID("33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3");
const Guid ATNDeserializer::ADDED_PRECEDENCE_TRANSITIONS("1DA0C57D-6C06-438A-9B27-10BCB3CE0F61"); Guid ATNDeserializer::ADDED_PRECEDENCE_TRANSITIONS("1DA0C57D-6C06-438A-9B27-10BCB3CE0F61");
const Guid ATNDeserializer::ADDED_LEXER_ACTIONS("AADB8D7E-AEEF-4415-AD2B-8204D6CF042E"); Guid ATNDeserializer::ADDED_LEXER_ACTIONS("AADB8D7E-AEEF-4415-AD2B-8204D6CF042E");
const std::vector<Guid> ATNDeserializer::SUPPORTED_UUIDS = supportedUUIDsInitializer(); std::vector<Guid> ATNDeserializer::SUPPORTED_UUIDS = { BASE_SERIALIZED_UUID, ADDED_PRECEDENCE_TRANSITIONS };
const Guid ATNDeserializer::SERIALIZED_UUID = ADDED_LEXER_ACTIONS; Guid ATNDeserializer::SERIALIZED_UUID = ADDED_PRECEDENCE_TRANSITIONS; //ADDED_LEXER_ACTIONS;
ATNDeserializer::ATNDeserializer(): deserializationOptions(ATNDeserializationOptions::getDefaultOptions()) { ATNDeserializer::ATNDeserializer(): ATNDeserializer(ATNDeserializationOptions::getDefaultOptions()) {
} }
ATNDeserializer::ATNDeserializer(const ATNDeserializationOptions& dso): deserializationOptions(dso) { ATNDeserializer::ATNDeserializer(const ATNDeserializationOptions& dso): deserializationOptions(dso) {
// XXX: wery weird, from one moment to the next static initialization stopped working. Find out why.
if (SUPPORTED_UUIDS.empty()) {
BASE_SERIALIZED_UUID = Guid("33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3");
ADDED_PRECEDENCE_TRANSITIONS = Guid("1DA0C57D-6C06-438A-9B27-10BCB3CE0F61");
ADDED_LEXER_ACTIONS = Guid("AADB8D7E-AEEF-4415-AD2B-8204D6CF042E");
SUPPORTED_UUIDS = { BASE_SERIALIZED_UUID, ADDED_PRECEDENCE_TRANSITIONS };
SERIALIZED_UUID = ADDED_PRECEDENCE_TRANSITIONS;
}
} }
bool ATNDeserializer::isFeatureSupported(const Guid &feature, const Guid &actualUuid) { bool ATNDeserializer::isFeatureSupported(const Guid &feature, const Guid &actualUuid) {
@ -101,7 +112,7 @@ bool ATNDeserializer::isFeatureSupported(const Guid &feature, const Guid &actual
ATN ATNDeserializer::deserialize(const std::wstring& input) { ATN ATNDeserializer::deserialize(const std::wstring& input) {
// Don't adjust the first value since that's the version number. // Don't adjust the first value since that's the version number.
wchar_t data[input.size()]; uint16_t data[input.size()];
data[0] = input[0]; data[0] = input[0];
for (size_t i = 1; i < input.size(); ++i) { for (size_t i = 1; i < input.size(); ++i) {
data[i] = input[i] - 2; data[i] = input[i] - 2;
@ -116,8 +127,8 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
} }
Guid uuid = toUUID(data, p); Guid uuid = toUUID(data, p);
auto uuidIterator = std::find(SUPPORTED_UUIDS.begin(), SUPPORTED_UUIDS.end(), uuid);
p += 8; p += 8;
auto uuidIterator = std::find(SUPPORTED_UUIDS.begin(), SUPPORTED_UUIDS.end(), uuid);
if (uuidIterator == SUPPORTED_UUIDS.end()) { if (uuidIterator == SUPPORTED_UUIDS.end()) {
std::string reason = "Could not deserialize ATN with UUID " + uuid.toString() + " (expected " + std::string reason = "Could not deserialize ATN with UUID " + uuid.toString() + " (expected " +
SERIALIZED_UUID.toString() + " or a legacy UUID)."; SERIALIZED_UUID.toString() + " or a legacy UUID).";
@ -128,7 +139,7 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
bool supportsPrecedencePredicates = isFeatureSupported(ADDED_PRECEDENCE_TRANSITIONS, uuid); bool supportsPrecedencePredicates = isFeatureSupported(ADDED_PRECEDENCE_TRANSITIONS, uuid);
ATNType grammarType = (ATNType)data[p++]; ATNType grammarType = (ATNType)data[p++];
int maxTokenType = data[p++]; size_t maxTokenType = data[p++];
ATN atn(grammarType, maxTokenType); ATN atn(grammarType, maxTokenType);
// //
@ -146,17 +157,17 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
} }
int ruleIndex = data[p++]; int ruleIndex = data[p++];
if (ruleIndex == WCHAR_MAX) { if (ruleIndex == 0xFFFF) { // Not WCHAR_MAX as this can take a different value.
ruleIndex = -1; ruleIndex = -1;
} }
ATNState *s = stateFactory(stype, ruleIndex); ATNState *s = stateFactory(stype, ruleIndex);
if (stype == ATNState::LOOP_END) { // special case if (stype == ATNState::LOOP_END) { // special case
int loopBackStateNumber = data[p++]; int loopBackStateNumber = data[p++];
loopBackStateNumbers.push_back({ (LoopEndState*) s, loopBackStateNumber }); loopBackStateNumbers.push_back({ (LoopEndState*)s, loopBackStateNumber });
} else if (dynamic_cast<BlockStartState*>(s) != nullptr) { } else if (is<BlockStartState*>(s)) {
int endStateNumber = data[p++]; int endStateNumber = data[p++];
endStateNumbers.push_back({ (BlockStartState*) s, endStateNumber }); endStateNumbers.push_back({ (BlockStartState*)s, endStateNumber });
} }
atn.addState(s); atn.addState(s);
} }
@ -218,7 +229,7 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
atn.ruleToStopState.resize(nrules); atn.ruleToStopState.resize(nrules);
for (ATNState *state : atn.states) { for (ATNState *state : atn.states) {
if (!(dynamic_cast<RuleStopState*>(state) != nullptr)) { if (!is<RuleStopState*>(state)) {
continue; continue;
} }
@ -242,8 +253,7 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
std::vector<misc::IntervalSet> sets; std::vector<misc::IntervalSet> sets;
int nsets = data[p++]; int nsets = data[p++];
for (int i = 0; i < nsets; i++) { for (int i = 0; i < nsets; i++) {
int nintervals = data[p]; int nintervals = data[p++];
p++;
misc::IntervalSet set; misc::IntervalSet set;
bool containsEof = data[p++] != 0; bool containsEof = data[p++] != 0;
@ -279,7 +289,7 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
for (ATNState *state : atn.states) { for (ATNState *state : atn.states) {
for (size_t i = 0; i < state->getNumberOfTransitions(); i++) { for (size_t i = 0; i < state->getNumberOfTransitions(); i++) {
Transition *t = state->transition(i); Transition *t = state->transition(i);
if (!(dynamic_cast<RuleTransition*>(t) != nullptr)) { if (!is<RuleTransition*>(t)) {
continue; continue;
} }
@ -289,34 +299,36 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
} }
for (ATNState *state : atn.states) { for (ATNState *state : atn.states) {
if (dynamic_cast<BlockStartState*>(state) != nullptr) { if (is<BlockStartState *>(state)) {
BlockStartState *startState = static_cast<BlockStartState *>(state);
// we need to know the end state to set its start state // we need to know the end state to set its start state
if ((static_cast<BlockStartState*>(state))->endState == nullptr) { if (startState->endState == nullptr) {
throw IllegalStateException(); throw IllegalStateException();
} }
// block end states can only be associated to a single block start state // block end states can only be associated to a single block start state
if ((static_cast<BlockStartState*>(state))->endState->startState != nullptr) { if (startState->endState->startState != nullptr) {
throw IllegalStateException(); throw IllegalStateException();
} }
(static_cast<BlockStartState*>(state))->endState->startState = static_cast<BlockStartState*>(state); startState->endState->startState = static_cast<BlockStartState*>(state);
} }
if (dynamic_cast<PlusLoopbackState*>(state) != nullptr) { if (is<PlusLoopbackState*>(state)) {
PlusLoopbackState *loopbackState = static_cast<PlusLoopbackState*>(state); PlusLoopbackState *loopbackState = static_cast<PlusLoopbackState *>(state);
for (size_t i = 0; i < loopbackState->getNumberOfTransitions(); i++) { for (size_t i = 0; i < loopbackState->getNumberOfTransitions(); i++) {
ATNState *target = loopbackState->transition(i)->target; ATNState *target = loopbackState->transition(i)->target;
if (dynamic_cast<PlusBlockStartState*>(target) != nullptr) { if (is<PlusBlockStartState *>(target)) {
(static_cast<PlusBlockStartState*>(target))->loopBackState = loopbackState; (static_cast<PlusBlockStartState *>(target))->loopBackState = loopbackState;
} }
} }
} else if (dynamic_cast<StarLoopbackState*>(state) != nullptr) { } else if (is<StarLoopbackState *>(state)) {
StarLoopbackState *loopbackState = static_cast<StarLoopbackState*>(state); StarLoopbackState *loopbackState = static_cast<StarLoopbackState *>(state);
for (size_t i = 0; i < loopbackState->getNumberOfTransitions(); i++) { for (size_t i = 0; i < loopbackState->getNumberOfTransitions(); i++) {
ATNState *target = loopbackState->transition(i)->target; ATNState *target = loopbackState->transition(i)->target;
if (dynamic_cast<StarLoopEntryState*>(target) != nullptr) { if (is<StarLoopEntryState *>(target)) {
(static_cast<StarLoopEntryState*>(target))->loopBackState = loopbackState; (static_cast<StarLoopEntryState*>(target))->loopBackState = loopbackState;
} }
} }
@ -328,8 +340,11 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
// //
size_t ndecisions = (size_t)data[p++]; size_t ndecisions = (size_t)data[p++];
for (size_t i = 1; i <= ndecisions; i++) { for (size_t i = 1; i <= ndecisions; i++) {
size_t s = (size_t)data[p++]; size_t s = data[p++];
DecisionState *decState = static_cast<DecisionState*>(atn.states[s]); DecisionState *decState = dynamic_cast<DecisionState*>(atn.states[s]);
if (decState == nullptr)
throw IllegalStateException();
atn.decisionToState.push_back(decState); atn.decisionToState.push_back(decState);
decState->decision = (int)i - 1; decState->decision = (int)i - 1;
} }
@ -340,8 +355,8 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) {
if (deserializationOptions.isGenerateRuleBypassTransitions() && atn.grammarType == ATNType::PARSER) { if (deserializationOptions.isGenerateRuleBypassTransitions() && atn.grammarType == ATNType::PARSER) {
atn.ruleToTokenType.resize(atn.ruleToStartState.size()); atn.ruleToTokenType.resize(atn.ruleToStartState.size());
for (std::vector<RuleStartState*>::size_type i = 0; i < atn.ruleToStartState.size(); i++) { for (size_t i = 0; i < atn.ruleToStartState.size(); i++) {
atn.ruleToTokenType[i] = atn.maxTokenType + (int)i + 1; atn.ruleToTokenType[i] = int(atn.maxTokenType + i + 1);
} }
for (std::vector<RuleStartState*>::size_type i = 0; i < atn.ruleToStartState.size(); i++) { for (std::vector<RuleStartState*>::size_type i = 0; i < atn.ruleToStartState.size(); i++) {
@ -440,20 +455,20 @@ void ATNDeserializer::verifyATN(const ATN &atn) {
checkCondition(state->onlyHasEpsilonTransitions() || state->getNumberOfTransitions() <= 1); checkCondition(state->onlyHasEpsilonTransitions() || state->getNumberOfTransitions() <= 1);
if (dynamic_cast<PlusBlockStartState*>(state) != nullptr) { if (is<PlusBlockStartState *>(state)) {
checkCondition((static_cast<PlusBlockStartState*>(state))->loopBackState != nullptr); checkCondition((static_cast<PlusBlockStartState *>(state))->loopBackState != nullptr);
} }
if (dynamic_cast<StarLoopEntryState*>(state) != nullptr) { if (is<StarLoopEntryState *>(state)) {
StarLoopEntryState *starLoopEntryState = static_cast<StarLoopEntryState*>(state); StarLoopEntryState *starLoopEntryState = static_cast<StarLoopEntryState*>(state);
checkCondition(starLoopEntryState->loopBackState != nullptr); checkCondition(starLoopEntryState->loopBackState != nullptr);
checkCondition(starLoopEntryState->getNumberOfTransitions() == 2); checkCondition(starLoopEntryState->getNumberOfTransitions() == 2);
if (dynamic_cast<StarBlockStartState*>(starLoopEntryState->transition(0)->target) != nullptr) { if (is<StarBlockStartState *>(starLoopEntryState->transition(0)->target)) {
checkCondition(dynamic_cast<LoopEndState*>(starLoopEntryState->transition(1)->target) != nullptr); checkCondition(static_cast<LoopEndState *>(starLoopEntryState->transition(1)->target) != nullptr);
checkCondition(!starLoopEntryState->nonGreedy); checkCondition(!starLoopEntryState->nonGreedy);
} else if (dynamic_cast<LoopEndState*>(starLoopEntryState->transition(0)->target) != nullptr) { } else if (is<LoopEndState *>(starLoopEntryState->transition(0)->target)) {
checkCondition(dynamic_cast<StarBlockStartState*>(starLoopEntryState->transition(1)->target) != nullptr); checkCondition(is<StarBlockStartState *>(starLoopEntryState->transition(1)->target));
checkCondition(starLoopEntryState->nonGreedy); checkCondition(starLoopEntryState->nonGreedy);
} else { } else {
throw IllegalStateException(); throw IllegalStateException();
@ -461,32 +476,32 @@ void ATNDeserializer::verifyATN(const ATN &atn) {
} }
} }
if (dynamic_cast<StarLoopbackState*>(state) != nullptr) { if (is<StarLoopbackState *>(state)) {
checkCondition(state->getNumberOfTransitions() == 1); checkCondition(state->getNumberOfTransitions() == 1);
checkCondition(dynamic_cast<StarLoopEntryState*>(state->transition(0)->target) != nullptr); checkCondition(is<StarLoopEntryState *>(state->transition(0)->target));
} }
if (dynamic_cast<LoopEndState*>(state) != nullptr) { if (is<LoopEndState *>(state)) {
checkCondition((static_cast<LoopEndState*>(state))->loopBackState != nullptr); checkCondition((static_cast<LoopEndState *>(state))->loopBackState != nullptr);
} }
if (dynamic_cast<RuleStartState*>(state) != nullptr) { if (is<RuleStartState *>(state)) {
checkCondition((static_cast<RuleStartState*>(state))->stopState != nullptr); checkCondition((static_cast<RuleStartState *>(state))->stopState != nullptr);
} }
if (dynamic_cast<BlockStartState*>(state) != nullptr) { if (is<BlockStartState *>(state)) {
checkCondition((static_cast<BlockStartState*>(state))->endState != nullptr); checkCondition((static_cast<BlockStartState *>(state))->endState != nullptr);
} }
if (dynamic_cast<BlockEndState*>(state) != nullptr) { if (is<BlockEndState *>(state)) {
checkCondition((static_cast<BlockEndState*>(state))->startState != nullptr); checkCondition((static_cast<BlockEndState *>(state))->startState != nullptr);
} }
if (dynamic_cast<DecisionState*>(state) != nullptr) { if (is<DecisionState *>(state)) {
DecisionState *decisionState = static_cast<DecisionState*>(state); DecisionState *decisionState = static_cast<DecisionState *>(state);
checkCondition(decisionState->getNumberOfTransitions() <= 1 || decisionState->decision >= 0); checkCondition(decisionState->getNumberOfTransitions() <= 1 || decisionState->decision >= 0);
} else { } else {
checkCondition(state->getNumberOfTransitions() <= 1 || dynamic_cast<RuleStopState*>(state) != nullptr); checkCondition(state->getNumberOfTransitions() <= 1 || is<RuleStopState *>(state));
} }
} }
} }
@ -501,8 +516,8 @@ void ATNDeserializer::checkCondition(bool condition, const std::string &message)
} }
} }
Guid ATNDeserializer::toUUID(const wchar_t *data, int offset) { Guid ATNDeserializer::toUUID(const unsigned short *data, int offset) {
return Guid((uint32_t *)data + offset, true); return Guid((uint16_t *)data + offset, true);
} }
Transition *ATNDeserializer::edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3, Transition *ATNDeserializer::edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3,
@ -592,12 +607,3 @@ ATNState *ATNDeserializer::stateFactory(int type, int ruleIndex) {
s->ruleIndex = ruleIndex; s->ruleIndex = ruleIndex;
return s; return s;
} }
std::vector<Guid> ATNDeserializer::supportedUUIDsInitializer() {
std::vector<Guid> supportedUUIDs;
supportedUUIDs.push_back(BASE_SERIALIZED_UUID);
supportedUUIDs.push_back(ADDED_PRECEDENCE_TRANSITIONS);
supportedUUIDs.push_back(ADDED_LEXER_ACTIONS);
return supportedUUIDs;
}

View File

@ -41,50 +41,31 @@ namespace atn {
class ATNDeserializer { class ATNDeserializer {
public: public:
static const size_t SERIALIZED_VERSION; static const size_t SERIALIZED_VERSION = 3;
//static ATNDeserializer();
/// <summary>
/// This is the earliest supported serialized UUID.
/// </summary>
private:
static Guid const BASE_SERIALIZED_UUID;
/// <summary>
/// This UUID indicates an extension of <seealso cref="BASE_SERIALIZED_UUID"/> for the
/// addition of precedence predicates.
/// </summary>
static Guid const ADDED_PRECEDENCE_TRANSITIONS;
/**
* This UUID indicates an extension of ADDED_PRECEDENCE_TRANSITIONS
* for the addition of lexer actions encoded as a sequence of
* LexerAction instances.
*/
static Guid const ADDED_LEXER_ACTIONS;
/// <summary>
/// This list contains all of the currently supported UUIDs, ordered by when
/// the feature first appeared in this branch.
/// </summary>
static const std::vector<Guid> SUPPORTED_UUIDS;
/// <summary>
/// This is the current serialized UUID. /// This is the current serialized UUID.
/// </summary> static Guid SERIALIZED_UUID;
public:
static Guid const SERIALIZED_UUID;
private:
ATNDeserializationOptions deserializationOptions;
public:
ATNDeserializer(); ATNDeserializer();
ATNDeserializer(const ATNDeserializationOptions& dso); ATNDeserializer(const ATNDeserializationOptions& dso);
/// <summary> static Guid toUUID(const unsigned short *data, int offset);
virtual ATN deserialize(const std::wstring &input);
virtual void verifyATN(const ATN &atn);
virtual void checkCondition(bool condition);
virtual void checkCondition(bool condition, const std::string &message);
virtual Transition *edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3,
const std::vector<misc::IntervalSet> &sets);
virtual ATNState *stateFactory(int type, int ruleIndex);
protected:
/// Determines if a particular serialized representation of an ATN supports /// Determines if a particular serialized representation of an ATN supports
/// a particular feature, identified by the <seealso cref="UUID"/> used for serializing /// a particular feature, identified by the <seealso cref="UUID"/> used for serializing
/// the ATN at the time the feature was first introduced. /// the ATN at the time the feature was first introduced.
/// </summary> ///
/// <param name="feature"> The <seealso cref="UUID"/> marking the first time the feature was /// <param name="feature"> The <seealso cref="UUID"/> marking the first time the feature was
/// supported in the serialized ATN. </param> /// supported in the serialized ATN. </param>
/// <param name="actualUuid"> The <seealso cref="UUID"/> of the actual serialized ATN which is /// <param name="actualUuid"> The <seealso cref="UUID"/> of the actual serialized ATN which is
@ -92,27 +73,29 @@ namespace atn {
/// <returns> {@code true} if the {@code actualUuid} value represents a /// <returns> {@code true} if the {@code actualUuid} value represents a
/// serialized ATN at or after the feature identified by {@code feature} was /// serialized ATN at or after the feature identified by {@code feature} was
/// introduced; otherwise, {@code false}. </returns> /// introduced; otherwise, {@code false}. </returns>
protected:
virtual bool isFeatureSupported(const Guid &feature, const Guid &actualUuid); virtual bool isFeatureSupported(const Guid &feature, const Guid &actualUuid);
public:
virtual ATN deserialize(const std::wstring& input);
public:
virtual void verifyATN(const ATN &atn);
virtual void checkCondition(bool condition);
virtual void checkCondition(bool condition, const std::string &message);
static Guid toUUID(const wchar_t *data, int offset);
virtual Transition *edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3,
const std::vector<misc::IntervalSet> &sets);
virtual ATNState *stateFactory(int type, int ruleIndex);
private: private:
/// This is the earliest supported serialized UUID.
static Guid BASE_SERIALIZED_UUID;
/// This UUID indicates an extension of <seealso cref="BASE_SERIALIZED_UUID"/> for the
/// addition of precedence predicates.
static Guid ADDED_PRECEDENCE_TRANSITIONS;
/**
* This UUID indicates an extension of ADDED_PRECEDENCE_TRANSITIONS
* for the addition of lexer actions encoded as a sequence of
* LexerAction instances.
*/
static Guid ADDED_LEXER_ACTIONS;
/// This list contains all of the currently supported UUIDs, ordered by when
/// the feature first appeared in this branch.
static std::vector<Guid> SUPPORTED_UUIDS;
ATNDeserializationOptions deserializationOptions;
static std::vector<Guid> supportedUUIDsInitializer(); static std::vector<Guid> supportedUUIDsInitializer();
}; };

View File

@ -323,7 +323,7 @@ std::vector<size_t> ATNSerializer::serialize() {
//------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------
std::wstring ATNSerializer::decode(const std::wstring &inpdata) { std::wstring ATNSerializer::decode(const std::wstring &inpdata) {
wchar_t data[inpdata.size()]; uint16_t data[inpdata.size()];
data[0] = inpdata[0]; data[0] = inpdata[0];
// Don't adjust the first value since that's the version number. // Don't adjust the first value since that's the version number.
@ -375,7 +375,7 @@ std::wstring ATNSerializer::decode(const std::wstring &inpdata) {
} }
buf.append(std::to_wstring(i)) buf.append(std::to_wstring(i))
.append(L":") .append(L":")
.append(ATNState::serializationNames[stype]) .append(ATNState::serializationNames[(size_t)stype])
.append(L" ") .append(L" ")
.append(std::to_wstring(ruleIndex)) .append(std::to_wstring(ruleIndex))
.append(arg) .append(arg)

View File

@ -42,9 +42,6 @@ using namespace org::antlr::v4::runtime::atn;
const std::shared_ptr<DFAState> ATNSimulator::ERROR = std::make_shared<DFAState>(INT32_MAX); const std::shared_ptr<DFAState> ATNSimulator::ERROR = std::make_shared<DFAState>(INT32_MAX);
ATNSimulator::ATNSimulator() {
}
ATNSimulator::ATNSimulator(const ATN &atn, std::shared_ptr<PredictionContextCache> sharedContextCache) ATNSimulator::ATNSimulator(const ATN &atn, std::shared_ptr<PredictionContextCache> sharedContextCache)
: atn(atn), _sharedContextCache(sharedContextCache) { : atn(atn), _sharedContextCache(sharedContextCache) {
} }
@ -53,9 +50,9 @@ std::shared_ptr<PredictionContextCache> ATNSimulator::getSharedContextCache() {
return _sharedContextCache; return _sharedContextCache;
} }
PredictionContextRef ATNSimulator::getCachedContext(PredictionContextRef context) { PredictionContext::Ref ATNSimulator::getCachedContext(PredictionContext::Ref context) {
std::lock_guard<std::mutex> lck(mtx); std::lock_guard<std::mutex> lck(mtx);
std::map<PredictionContextRef, PredictionContextRef> visited; std::map<PredictionContext::Ref, PredictionContext::Ref> visited;
return PredictionContext::getCachedContext(context, _sharedContextCache, visited); return PredictionContext::getCachedContext(context, _sharedContextCache, visited);
} }

View File

@ -45,18 +45,16 @@ namespace atn {
public: public:
virtual ~ATNSimulator() {}; virtual ~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 const std::shared_ptr<dfa::DFAState> ERROR; static const std::shared_ptr<dfa::DFAState> ERROR;
ATN atn; const ATN &atn;
ATNSimulator(const ATN &atn, std::shared_ptr<PredictionContextCache> sharedContextCache); ATNSimulator(const ATN &atn, std::shared_ptr<PredictionContextCache> sharedContextCache);
virtual void reset() = 0; virtual void reset() = 0;
virtual std::shared_ptr<PredictionContextCache> getSharedContextCache(); virtual std::shared_ptr<PredictionContextCache> getSharedContextCache();
virtual PredictionContextRef getCachedContext(PredictionContextRef context); virtual PredictionContext::Ref getCachedContext(PredictionContext::Ref context);
/// @deprecated Use <seealso cref="ATNDeserializer#deserialize"/> instead. /// @deprecated Use <seealso cref="ATNDeserializer#deserialize"/> instead.
static ATN deserialize(const std::wstring &data); static ATN deserialize(const std::wstring &data);

View File

@ -32,18 +32,23 @@
#include "ATN.h" #include "ATN.h"
#include "Transition.h" #include "Transition.h"
#include "IntervalSet.h" #include "IntervalSet.h"
#include "CPPUtils.h"
#include "ATNState.h" #include "ATNState.h"
using namespace org::antlr::v4::runtime::atn; using namespace org::antlr::v4::runtime::atn;
using namespace antlrcpp;
const int ATNState::INITIAL_NUM_TRANSITIONS; const int ATNState::INITIAL_NUM_TRANSITIONS;
const int ATNState::INVALID_STATE_NUMBER; const int ATNState::INVALID_STATE_NUMBER;
ATNState::~ATNState() { ATNState::~ATNState() {
for (auto transition : transitions) {
delete transition;
}
}; };
const wchar_t * ATNState::serializationNames[] = {L"INVALID", L"BASIC", L"RULE_START", L"BLOCK_START", const std::vector<std::wstring> ATNState::serializationNames = {L"INVALID", L"BASIC", L"RULE_START", L"BLOCK_START",
L"PLUS_BLOCK_START", L"STAR_BLOCK_START", L"TOKEN_START", L"RULE_STOP", L"PLUS_BLOCK_START", L"STAR_BLOCK_START", L"TOKEN_START", L"RULE_STOP",
L"BLOCK_END", L"STAR_LOOP_BACK", L"STAR_LOOP_ENTRY", L"PLUS_LOOP_BACK", L"LOOP_END"}; L"BLOCK_END", L"STAR_LOOP_BACK", L"STAR_LOOP_ENTRY", L"PLUS_LOOP_BACK", L"LOOP_END"};
@ -60,13 +65,26 @@ bool ATNState::isNonGreedyExitState() {
} }
std::wstring ATNState::toString() const { std::wstring ATNState::toString() const {
return std::to_wstring(stateNumber); std::wstringstream ss;
ss << "(ATNState " << std::hex << this << std::dec << ") {" << std::endl;
if (stateNumber < 0 || stateNumber >= (int)serializationNames.size())
ss << " state: INVALID ";
else
ss << " state: " << serializationNames[(size_t)stateNumber];
ss << " (" << stateNumber << ")" << std::endl;
ss << " ruleIndex: " << ruleIndex << std::endl << " epsilonOnlyTransitions: " << epsilonOnlyTransitions << std::endl;
ss << " transistions (" << transitions.size() << "):" << std::endl;
for (auto transition : transitions) {
ss << indent(transition->toString(), L" ") << std::endl;
}
ss << "}";
return ss.str();
} }
std::vector<Transition*> ATNState::getTransitions() { std::vector<Transition*> ATNState::getTransitions() {
std::vector<Transition*> arr(transitions); return transitions;
return arr;
} }
size_t ATNState::getNumberOfTransitions() { size_t ATNState::getNumberOfTransitions() {
@ -108,10 +126,3 @@ bool ATNState::onlyHasEpsilonTransitions() {
void ATNState::setRuleIndex(int ruleIndex) { void ATNState::setRuleIndex(int ruleIndex) {
this->ruleIndex = ruleIndex; this->ruleIndex = ruleIndex;
} }
void ATNState::InitializeInstanceFields() {
atn = 0;
stateNumber = INVALID_STATE_NUMBER;
ruleIndex = 0;
epsilonOnlyTransitions = false;
}

View File

@ -122,63 +122,39 @@ namespace atn {
LOOP_END = 12 LOOP_END = 12
}; };
static const wchar_t * serializationNames[]; static const std::vector<std::wstring> serializationNames;
/// <summary> /// Which ATN are we in?
/// Which ATN are we in? </summary> // ml: just a reference to the owner. Set when the state gets added to an ATN.
ATN *atn = nullptr; //const ATN *atn = nullptr;
int stateNumber = INVALID_STATE_NUMBER;
int ruleIndex = 0; // at runtime, we don't have Rule objects
bool epsilonOnlyTransitions = false;
int stateNumber;
int ruleIndex; // at runtime, we don't have Rule objects
bool epsilonOnlyTransitions;
/// <summary>
/// Track the transitions emanating from this ATN state. </summary>
protected: protected:
/// Track the transitions emanating from this ATN state.
std::vector<Transition*> transitions; std::vector<Transition*> transitions;
/// <summary>
/// Used to cache lookahead during parsing, not used during construction </summary>
public: public:
/// Used to cache lookahead during parsing, not used during construction.
misc::IntervalSet nextTokenWithinRule; misc::IntervalSet nextTokenWithinRule;
virtual size_t hashCode(); virtual size_t hashCode();
bool operator == (const ATNState &other); bool operator == (const ATNState &other);
virtual bool isNonGreedyExitState(); virtual bool isNonGreedyExitState();
virtual std::wstring toString() const; virtual std::wstring toString() const;
virtual std::vector<Transition*> getTransitions(); virtual std::vector<Transition*> getTransitions();
virtual size_t getNumberOfTransitions(); virtual size_t getNumberOfTransitions();
virtual void addTransition(Transition *e); virtual void addTransition(Transition *e);
virtual void addTransition(int index, Transition *e); virtual void addTransition(int index, Transition *e);
virtual Transition *transition(size_t i); virtual Transition *transition(size_t i);
virtual void setTransition(size_t i, Transition *e); virtual void setTransition(size_t i, Transition *e);
virtual Transition *removeTransition(int index); virtual Transition *removeTransition(int index);
virtual int getStateType() = 0; virtual int getStateType() = 0;
bool onlyHasEpsilonTransitions(); bool onlyHasEpsilonTransitions();
virtual void setRuleIndex(int ruleIndex); virtual void setRuleIndex(int ruleIndex);
private:
void InitializeInstanceFields();
public:
// ATNState() : transitions(new std::list<Transition*>()) {
// InitializeInstanceFields();
// }
}; };
} // namespace atn } // namespace atn

View File

@ -37,23 +37,10 @@ namespace v4 {
namespace runtime { namespace runtime {
namespace atn { namespace atn {
/// <summary>
/// Represents the type of recognizer an ATN applies to. /// Represents the type of recognizer an ATN applies to.
///
/// @author Sam Harwell
/// </summary>
enum class ATNType { enum class ATNType {
LEXER = 0,
/// <summary> PARSER = 1,
/// A lexer grammar.
/// </summary>
LEXER,
/// <summary>
/// A parser grammar.
/// </summary>
PARSER,
}; };
} // namespace atn } // namespace atn

View File

@ -52,5 +52,6 @@ bool ActionTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbo
} }
std::wstring ActionTransition::toString() const { std::wstring ActionTransition::toString() const {
return std::wstring(L"action_") + std::to_wstring(ruleIndex) + std::wstring(L":") + std::to_wstring(actionIndex); return L" ACTION " + Transition::toString() + L" { ruleIndex: " + std::to_wstring(ruleIndex) + L", actionIndex: " +
std::to_wstring(actionIndex) + L", isCtxDependent: " + std::to_wstring(isCtxDependent) + L" }";
} }

View File

@ -55,7 +55,7 @@ namespace atn {
virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override;
virtual std::wstring toString() const; virtual std::wstring toString() const override;
}; };
} // namespace atn } // namespace atn

View File

@ -36,7 +36,7 @@
using namespace org::antlr::v4::runtime::atn; using namespace org::antlr::v4::runtime::atn;
ArrayPredictionContext::ArrayPredictionContext(SingletonPredictionContextRef a) ArrayPredictionContext::ArrayPredictionContext(SingletonPredictionContext::Ref a)
: ArrayPredictionContext({ a->parent }, { a->returnState }) { : ArrayPredictionContext({ a->parent }, { a->returnState }) {
} }

View File

@ -52,7 +52,7 @@ namespace atn {
/// Sorted for merge, no duplicates; if present, EMPTY_RETURN_STATE is always last. /// Sorted for merge, no duplicates; if present, EMPTY_RETURN_STATE is always last.
const std::vector<int> returnStates; const std::vector<int> returnStates;
ArrayPredictionContext(SingletonPredictionContextRef a); ArrayPredictionContext(SingletonPredictionContext::Ref a);
ArrayPredictionContext(const std::vector<std::weak_ptr<PredictionContext>> &parents, ArrayPredictionContext(const std::vector<std::weak_ptr<PredictionContext>> &parents,
const std::vector<int> &returnStates); const std::vector<int> &returnStates);

View File

@ -53,5 +53,5 @@ bool AtomTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol)
} }
std::wstring AtomTransition::toString() const { std::wstring AtomTransition::toString() const {
return std::to_wstring(_label); return L"ATOM " + Transition::toString() + L" { label: " + std::to_wstring(_label) + L" }";
} }

View File

@ -54,7 +54,7 @@ namespace atn {
virtual misc::IntervalSet label() const override; virtual misc::IntervalSet label() const override;
virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override;
virtual std::wstring toString() const; virtual std::wstring toString() const override;
}; };
} // namespace atn } // namespace atn

View File

@ -37,3 +37,7 @@ void DecisionState::InitializeInstanceFields() {
decision = -1; decision = -1;
nonGreedy = false; nonGreedy = false;
} }
std::wstring DecisionState::toString() const {
return L"DECISION " + ATNState::toString();
}

View File

@ -51,6 +51,8 @@ namespace atn {
DecisionState() { DecisionState() {
InitializeInstanceFields(); InitializeInstanceFields();
} }
virtual std::wstring toString() const override;
}; };
} // namespace atn } // namespace atn

View File

@ -49,5 +49,5 @@ bool EpsilonTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymb
} }
std::wstring EpsilonTransition::toString() const { std::wstring EpsilonTransition::toString() const {
return L"epsilon"; return L"EPSILON " + Transition::toString() + L" {}";
} }

View File

@ -48,7 +48,7 @@ namespace atn {
virtual bool isEpsilon() const override; virtual bool isEpsilon() const override;
virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override;
virtual std::wstring toString() const; virtual std::wstring toString() const override;
}; };
} // namespace atn } // namespace atn

View File

@ -74,14 +74,14 @@ std::vector<misc::IntervalSet> LL1Analyzer::getDecisionLookahead(ATNState *s) co
return look; return look;
} }
misc::IntervalSet LL1Analyzer::LOOK(ATNState *s, RuleContextRef ctx) const { misc::IntervalSet LL1Analyzer::LOOK(ATNState *s, RuleContext::Ref ctx) const {
return LOOK(s, nullptr, ctx); return LOOK(s, nullptr, ctx);
} }
misc::IntervalSet LL1Analyzer::LOOK(ATNState *s, ATNState *stopState, RuleContextRef ctx) const { misc::IntervalSet LL1Analyzer::LOOK(ATNState *s, ATNState *stopState, RuleContext::Ref ctx) const {
misc::IntervalSet r; misc::IntervalSet r;
bool seeThruPreds = true; // ignore preds; get all lookahead bool seeThruPreds = true; // ignore preds; get all lookahead
PredictionContextRef lookContext = ctx != nullptr ? PredictionContext::fromRuleContext(*s->atn, ctx) : nullptr; PredictionContext::Ref lookContext = ctx != nullptr ? PredictionContext::fromRuleContext(_atn, ctx) : nullptr;
std::set<ATNConfig*> lookBusy; std::set<ATNConfig*> lookBusy;
antlrcpp::BitSet callRuleStack; antlrcpp::BitSet callRuleStack;
@ -90,7 +90,7 @@ misc::IntervalSet LL1Analyzer::LOOK(ATNState *s, ATNState *stopState, RuleContex
return r; return r;
} }
void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContextRef ctx, misc::IntervalSet &look, void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContext::Ref ctx, misc::IntervalSet &look,
std::set<ATNConfig*> &lookBusy, antlrcpp::BitSet &calledRuleStack, bool seeThruPreds, bool addEOF) const { std::set<ATNConfig*> &lookBusy, antlrcpp::BitSet &calledRuleStack, bool seeThruPreds, bool addEOF) const {
ATNConfig *c = new ATNConfig(s, 0, ctx); ATNConfig *c = new ATNConfig(s, 0, ctx);
@ -140,12 +140,12 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContextRef c
for (size_t i = 0; i < n; i++) { for (size_t i = 0; i < n; i++) {
Transition *t = s->transition(i); Transition *t = s->transition(i);
if (typeid(t) == typeid(RuleTransition)) { if (is<RuleTransition *>(t)) {
if (calledRuleStack[(size_t)(static_cast<RuleTransition*>(t))->target->ruleIndex]) { if (calledRuleStack[(size_t)(static_cast<RuleTransition*>(t))->target->ruleIndex]) {
continue; continue;
} }
PredictionContextRef newContext = SingletonPredictionContext::create(ctx, (static_cast<RuleTransition*>(t))->followState->stateNumber); PredictionContext::Ref newContext = SingletonPredictionContext::create(ctx, (static_cast<RuleTransition*>(t))->followState->stateNumber);
auto onExit = finally([&] { auto onExit = finally([&] {
calledRuleStack[(size_t)((static_cast<RuleTransition*>(t))->target->ruleIndex)] = false; calledRuleStack[(size_t)((static_cast<RuleTransition*>(t))->target->ruleIndex)] = false;
}); });
@ -153,7 +153,7 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContextRef c
calledRuleStack.set((size_t)(static_cast<RuleTransition*>(t))->target->ruleIndex); calledRuleStack.set((size_t)(static_cast<RuleTransition*>(t))->target->ruleIndex);
_LOOK(t->target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); _LOOK(t->target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
} else if (dynamic_cast<AbstractPredicateTransition*>(t) != nullptr) { } else if (is<AbstractPredicateTransition *>(t)) {
if (seeThruPreds) { if (seeThruPreds) {
_LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
} else { } else {
@ -161,13 +161,13 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContextRef c
} }
} else if (t->isEpsilon()) { } else if (t->isEpsilon()) {
_LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
} else if (typeid(t) == typeid(WildcardTransition)) { } else if (is<WildcardTransition *>(t)) {
look.addAll(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, _atn.maxTokenType)); look.addAll(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, (int)_atn.maxTokenType));
} else { } else {
misc::IntervalSet set = t->label(); misc::IntervalSet set = t->label();
if (!set.isEmpty()) { if (!set.isEmpty()) {
if (dynamic_cast<NotSetTransition*>(t) != nullptr) { if (is<NotSetTransition*>(t)) {
set = set.complement(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, _atn.maxTokenType)); set = set.complement(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, (int)_atn.maxTokenType));
} }
look.addAll(set); look.addAll(set);
} }

View File

@ -79,7 +79,7 @@ namespace atn {
/// </param> /// </param>
/// <returns> The set of tokens that can follow {@code s} in the ATN in the /// <returns> The set of tokens that can follow {@code s} in the ATN in the
/// specified {@code ctx}. </returns> /// specified {@code ctx}. </returns>
virtual misc::IntervalSet LOOK(ATNState *s, RuleContextRef ctx) const; virtual misc::IntervalSet LOOK(ATNState *s, RuleContext::Ref ctx) const;
/// <summary> /// <summary>
/// Compute set of tokens that can follow {@code s} in the ATN in the /// Compute set of tokens that can follow {@code s} in the ATN in the
@ -98,7 +98,7 @@ namespace atn {
/// </param> /// </param>
/// <returns> The set of tokens that can follow {@code s} in the ATN in the /// <returns> The set of tokens that can follow {@code s} in the ATN in the
/// specified {@code ctx}. </returns> /// specified {@code ctx}. </returns>
virtual misc::IntervalSet LOOK(ATNState *s, ATNState *stopState, RuleContextRef ctx) const; virtual misc::IntervalSet LOOK(ATNState *s, ATNState *stopState, RuleContext::Ref ctx) const;
/// <summary> /// <summary>
/// Compute set of tokens that can follow {@code s} in the ATN in the /// Compute set of tokens that can follow {@code s} in the ATN in the
@ -130,7 +130,7 @@ namespace atn {
/// outermost context is reached. This parameter has no effect if {@code ctx} /// outermost context is reached. This parameter has no effect if {@code ctx}
/// is {@code null}. </param> /// is {@code null}. </param>
protected: protected:
virtual void _LOOK(ATNState *s, ATNState *stopState, PredictionContextRef ctx, misc::IntervalSet &look, virtual void _LOOK(ATNState *s, ATNState *stopState, PredictionContext::Ref ctx, misc::IntervalSet &look,
std::set<ATNConfig*> &lookBusy, antlrcpp::BitSet &calledRuleStack, bool seeThruPreds, bool addEOF) const; std::set<ATNConfig*> &lookBusy, antlrcpp::BitSet &calledRuleStack, bool seeThruPreds, bool addEOF) const;
}; };

View File

@ -37,11 +37,11 @@
using namespace org::antlr::v4::runtime::atn; using namespace org::antlr::v4::runtime::atn;
LexerATNConfig::LexerATNConfig(ATNState *state, int alt, PredictionContextRef context) LexerATNConfig::LexerATNConfig(ATNState *state, int alt, PredictionContext::Ref context)
: ATNConfig(state, alt, context, SemanticContext::NONE), passedThroughNonGreedyDecision(false) { : ATNConfig(state, alt, context, SemanticContext::NONE), passedThroughNonGreedyDecision(false) {
} }
LexerATNConfig::LexerATNConfig(ATNState *state, int alt, PredictionContextRef context, int actionIndex) LexerATNConfig::LexerATNConfig(ATNState *state, int alt, PredictionContext::Ref context, int actionIndex)
: ATNConfig(state, alt, context, SemanticContext::NONE), passedThroughNonGreedyDecision(false) { : ATNConfig(state, alt, context, SemanticContext::NONE), passedThroughNonGreedyDecision(false) {
lexerActionIndex = actionIndex; lexerActionIndex = actionIndex;
} }
@ -56,7 +56,7 @@ LexerATNConfig::LexerATNConfig(LexerATNConfig *c, ATNState *state, int actionInd
lexerActionIndex = actionIndex; lexerActionIndex = actionIndex;
} }
LexerATNConfig::LexerATNConfig(LexerATNConfig *c, ATNState *state, PredictionContextRef context) LexerATNConfig::LexerATNConfig(LexerATNConfig *c, ATNState *state, PredictionContext::Ref context)
: ATNConfig(c, state, context, c->semanticContext), passedThroughNonGreedyDecision(checkNonGreedyDecision(c, state)) { : ATNConfig(c, state, context, c->semanticContext), passedThroughNonGreedyDecision(checkNonGreedyDecision(c, state)) {
lexerActionIndex = c->lexerActionIndex; lexerActionIndex = c->lexerActionIndex;
} }

View File

@ -44,12 +44,12 @@ namespace atn {
/// Capture lexer action we traverse. /// Capture lexer action we traverse.
int lexerActionIndex = -1; int lexerActionIndex = -1;
LexerATNConfig(ATNState *state, int alt, PredictionContextRef context); LexerATNConfig(ATNState *state, int alt, PredictionContext::Ref context);
LexerATNConfig(ATNState *state, int alt, PredictionContextRef context, int actionIndex); LexerATNConfig(ATNState *state, int alt, PredictionContext::Ref context, int actionIndex);
LexerATNConfig(LexerATNConfig *c, ATNState *state); LexerATNConfig(LexerATNConfig *c, ATNState *state);
LexerATNConfig(LexerATNConfig *c, ATNState *state, int actionIndex); LexerATNConfig(LexerATNConfig *c, ATNState *state, int actionIndex);
LexerATNConfig(LexerATNConfig *c, ATNState *state, PredictionContextRef context); LexerATNConfig(LexerATNConfig *c, ATNState *state, PredictionContext::Ref context);
bool hasPassedThroughNonGreedyDecision(); bool hasPassedThroughNonGreedyDecision();

View File

@ -68,12 +68,12 @@ void LexerATNSimulator::SimState::InitializeInstanceFields() {
int LexerATNSimulator::match_calls = 0; int LexerATNSimulator::match_calls = 0;
LexerATNSimulator::LexerATNSimulator(const ATN &atn, const std::vector<dfa::DFA*> &decisionToDFA, LexerATNSimulator::LexerATNSimulator(const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
std::shared_ptr<PredictionContextCache> sharedContextCache) std::shared_ptr<PredictionContextCache> sharedContextCache)
: LexerATNSimulator(nullptr, atn, decisionToDFA, sharedContextCache) { : LexerATNSimulator(nullptr, atn, decisionToDFA, sharedContextCache) {
} }
LexerATNSimulator::LexerATNSimulator(Lexer *recog, const ATN &atn, const std::vector<dfa::DFA*> &decisionToDFA, LexerATNSimulator::LexerATNSimulator(Lexer *recog, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
std::shared_ptr<PredictionContextCache> sharedContextCache) std::shared_ptr<PredictionContextCache> sharedContextCache)
: ATNSimulator(atn, sharedContextCache), _recog(recog), _decisionToDFA(decisionToDFA), prevAccept(new SimState()) { : ATNSimulator(atn, sharedContextCache), _recog(recog), _decisionToDFA(decisionToDFA), prevAccept(new SimState()) {
InitializeInstanceFields(); InitializeInstanceFields();
@ -97,11 +97,11 @@ int LexerATNSimulator::match(CharStream *input, size_t mode) {
_startIndex = (int)input->index(); _startIndex = (int)input->index();
prevAccept->reset(); prevAccept->reset();
dfa::DFA *dfa = _decisionToDFA[mode]; const dfa::DFA &dfa = _decisionToDFA[mode];
if (dfa->s0 == nullptr) { if (dfa.s0 == nullptr) {
return matchATN(input); return matchATN(input);
} else { } else {
return execATN(input, dfa->s0); return execATN(input, dfa.s0);
} }
return -1; return -1;
@ -117,7 +117,7 @@ void LexerATNSimulator::reset() {
} }
int LexerATNSimulator::matchATN(CharStream *input) { int LexerATNSimulator::matchATN(CharStream *input) {
ATNState *startState = (ATNState *)atn.modeToStartState.at(_mode); ATNState *startState = (ATNState *)atn.modeToStartState[_mode];
if (debug) { if (debug) {
std::wcout << L"matchATN mode" << _mode << L" start: " << startState << std::endl; std::wcout << L"matchATN mode" << _mode << L" start: " << startState << std::endl;
@ -131,13 +131,13 @@ int LexerATNSimulator::matchATN(CharStream *input) {
dfa::DFAState *next = addDFAState(s0_closure); dfa::DFAState *next = addDFAState(s0_closure);
if (!suppressEdge) { if (!suppressEdge) {
_decisionToDFA[_mode]->s0 = next; _decisionToDFA[_mode].s0 = next;
} }
int predict = execATN(input, next); int predict = execATN(input, next);
if (debug) { if (debug) {
std::wcout << L"DFA after matchATN: " << _decisionToDFA[old_mode]->toLexerString() << std::endl; std::wcout << L"DFA after matchATN: " << _decisionToDFA[old_mode].toLexerString() << std::endl;
} }
return predict; return predict;
@ -179,7 +179,6 @@ int LexerATNSimulator::execATN(CharStream *input, dfa::DFAState *ds0) {
target = computeTargetState(input, s, t); target = computeTargetState(input, s, t);
} }
std::wstring temp = target->toString();
if (target == ERROR.get()) { if (target == ERROR.get()) {
break; break;
} }
@ -331,7 +330,7 @@ bool LexerATNSimulator::closure(CharStream *input, LexerATNConfig *config, std::
std::wcout << L"closure(" << config->toString(true) << L")" << std::endl; std::wcout << L"closure(" << config->toString(true) << L")" << std::endl;
} }
if (dynamic_cast<RuleStopState*>(config->state) != nullptr) { if (is<RuleStopState *>(config->state)) {
if (debug) { if (debug) {
if (_recog != nullptr) { if (_recog != nullptr) {
std::wcout << L"closure at " << _recog->getRuleNames()[(size_t)config->state->ruleIndex] << L" rule stop " << config << std::endl; std::wcout << L"closure at " << _recog->getRuleNames()[(size_t)config->state->ruleIndex] << L" rule stop " << config << std::endl;
@ -389,7 +388,7 @@ atn::LexerATNConfig *LexerATNSimulator::getEpsilonTarget(CharStream *input, Lexe
switch (t->getSerializationType()) { switch (t->getSerializationType()) {
case Transition::RULE: { case Transition::RULE: {
RuleTransition *ruleTransition = static_cast<RuleTransition*>(t); RuleTransition *ruleTransition = static_cast<RuleTransition*>(t);
PredictionContextRef newContext = SingletonPredictionContext::create(config->context, ruleTransition->followState->stateNumber); PredictionContext::Ref newContext = SingletonPredictionContext::create(config->context, ruleTransition->followState->stateNumber);
c = new LexerATNConfig(config, t->target, newContext); c = new LexerATNConfig(config, t->target, newContext);
} }
break; break;
@ -523,7 +522,7 @@ dfa::DFAState *LexerATNSimulator::addDFAState(std::shared_ptr<ATNConfigSet> conf
dfa::DFAState *proposed = new dfa::DFAState(configs); dfa::DFAState *proposed = new dfa::DFAState(configs);
ATNConfig *firstConfigWithRuleStopState = nullptr; ATNConfig *firstConfigWithRuleStopState = nullptr;
for (auto c : configs->configs) { for (auto c : configs->configs) {
if (dynamic_cast<RuleStopState*>(c->state) != nullptr) { if (is<RuleStopState*>(c->state)) {
firstConfigWithRuleStopState = c; firstConfigWithRuleStopState = c;
break; break;
} }
@ -536,27 +535,27 @@ dfa::DFAState *LexerATNSimulator::addDFAState(std::shared_ptr<ATNConfigSet> conf
proposed->prediction = atn.ruleToTokenType[(size_t)proposed->lexerRuleIndex]; proposed->prediction = atn.ruleToTokenType[(size_t)proposed->lexerRuleIndex];
} }
dfa::DFA *dfa = _decisionToDFA[_mode]; dfa::DFA &dfa = _decisionToDFA[_mode];
{ {
std::lock_guard<std::mutex> lck(mtx); std::lock_guard<std::mutex> lck(mtx);
auto iterator = dfa->states->find(proposed); auto iterator = dfa.states.find(proposed);
if (iterator != dfa->states->end()) { if (iterator != dfa.states.end()) {
return iterator->second; return iterator->second;
} }
dfa::DFAState *newState = proposed; dfa::DFAState *newState = proposed;
newState->stateNumber = (int)dfa->states->size(); newState->stateNumber = (int)dfa.states.size();
configs->setReadonly(true); configs->setReadonly(true);
newState->configs = configs; newState->configs = configs;
(*dfa->states)[newState] = newState; dfa.states[newState] = newState;
return newState; return newState;
} }
} }
dfa::DFA *LexerATNSimulator::getDFA(size_t mode) { dfa::DFA& LexerATNSimulator::getDFA(size_t mode) {
return _decisionToDFA[mode]; return _decisionToDFA[mode];
} }

View File

@ -104,7 +104,7 @@ namespace atn {
int _charPositionInLine; int _charPositionInLine;
public: public:
const std::vector<dfa::DFA*> _decisionToDFA; std::vector<dfa::DFA> &_decisionToDFA;
protected: protected:
size_t _mode; size_t _mode;
@ -115,19 +115,17 @@ namespace atn {
public: public:
static int match_calls; static int match_calls;
LexerATNSimulator(const ATN &atn, const std::vector<dfa::DFA*> &decisionToDFA, std::shared_ptr<PredictionContextCache> sharedContextCache); LexerATNSimulator(const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
LexerATNSimulator(Lexer *recog, const ATN &atn, const std::vector<dfa::DFA*> &decisionToDFA, std::shared_ptr<PredictionContextCache> sharedContextCache);
LexerATNSimulator(Lexer *recog, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
std::shared_ptr<PredictionContextCache> sharedContextCache); std::shared_ptr<PredictionContextCache> sharedContextCache);
virtual void copyState(LexerATNSimulator *simulator); virtual void copyState(LexerATNSimulator *simulator);
virtual int match(CharStream *input, size_t mode); virtual int match(CharStream *input, size_t mode);
virtual void reset() override; virtual void reset() override;
protected: protected:
virtual int matchATN(CharStream *input); virtual int matchATN(CharStream *input);
virtual int execATN(CharStream *input, dfa::DFAState *ds0); virtual int execATN(CharStream *input, dfa::DFAState *ds0);
/// <summary> /// <summary>
@ -221,23 +219,15 @@ namespace atn {
virtual dfa::DFAState *addDFAState(std::shared_ptr<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);
/// <summary>
/// Get the text matched so far for the current token. /// Get the text matched so far for the current token.
/// </summary>
virtual std::wstring getText(CharStream *input); virtual std::wstring getText(CharStream *input);
virtual size_t getLine() const; virtual size_t getLine() const;
virtual void setLine(size_t line); virtual void setLine(size_t line);
virtual int getCharPositionInLine(); virtual int getCharPositionInLine();
virtual void setCharPositionInLine(int charPositionInLine); virtual void setCharPositionInLine(int charPositionInLine);
virtual void consume(CharStream *input); virtual void consume(CharStream *input);
virtual std::wstring getTokenName(int t); virtual std::wstring getTokenName(int t);
private: private:

View File

@ -47,5 +47,5 @@ bool NotSetTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbo
} }
std::wstring NotSetTransition::toString() const { std::wstring NotSetTransition::toString() const {
return L'~' + SetTransition::toString(); return L"NOT_SET " + Transition::toString() + L" { " + SetTransition::toString() + L" }";
} }

View File

@ -61,12 +61,12 @@ using namespace org::antlr::v4::runtime::atn;
using namespace antlrcpp; using namespace antlrcpp;
ParserATNSimulator::ParserATNSimulator(const ATN &atn, const std::vector<dfa::DFA *> &decisionToDFA, ParserATNSimulator::ParserATNSimulator(const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
std::shared_ptr<PredictionContextCache> sharedContextCache) std::shared_ptr<PredictionContextCache> sharedContextCache)
: ParserATNSimulator(nullptr, atn, decisionToDFA, sharedContextCache) { : ParserATNSimulator(nullptr, atn, decisionToDFA, sharedContextCache) {
} }
ParserATNSimulator::ParserATNSimulator(Parser *parser, const ATN &atn, const std::vector<dfa::DFA *> &decisionToDFA, ParserATNSimulator::ParserATNSimulator(Parser *parser, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
std::shared_ptr<PredictionContextCache> sharedContextCache) std::shared_ptr<PredictionContextCache> sharedContextCache)
: ATNSimulator(atn, sharedContextCache), parser(parser), _decisionToDFA(decisionToDFA) { : ATNSimulator(atn, sharedContextCache), parser(parser), _decisionToDFA(decisionToDFA) {
InitializeInstanceFields(); InitializeInstanceFields();
@ -75,7 +75,7 @@ ParserATNSimulator::ParserATNSimulator(Parser *parser, const ATN &atn, const std
void ParserATNSimulator::reset() { void ParserATNSimulator::reset() {
} }
int ParserATNSimulator::adaptivePredict(TokenStream *input, int decision, ParserRuleContextRef outerContext) { int ParserATNSimulator::adaptivePredict(TokenStream *input, int decision, ParserRuleContext::Ref outerContext) {
if (debug || debug_list_atn_decisions) { if (debug || debug_list_atn_decisions) {
std::wcout << L"adaptivePredict decision " << decision << L" exec LA(1)==" << getLookaheadName(input) << L" line " << input->LT(1)->getLine() << L":" << input->LT(1)->getCharPositionInLine() << std::endl; std::wcout << L"adaptivePredict decision " << decision << L" exec LA(1)==" << getLookaheadName(input) << L" line " << input->LT(1)->getLine() << L":" << input->LT(1)->getCharPositionInLine() << std::endl;
} }
@ -83,49 +83,44 @@ int ParserATNSimulator::adaptivePredict(TokenStream *input, int decision, Parser
_input = input; _input = input;
_startIndex = (int)input->index(); _startIndex = (int)input->index();
_outerContext = outerContext; _outerContext = outerContext;
dfa::DFA *dfa = _decisionToDFA[(size_t)decision]; dfa::DFA &dfa = _decisionToDFA[(size_t)decision];
ssize_t m = input->mark(); ssize_t m = input->mark();
size_t index = input->index(); size_t index = input->index();
// Now we are certain to have a specific decision's DFA // Now we are certain to have a specific decision's DFA
// But, do we still need an initial state? // But, do we still need an initial state?
try { auto onExit = finally([&] {
if (dfa->s0 == nullptr) {
if (outerContext == nullptr) {
outerContext = ParserRuleContext::EMPTY;
}
if (debug || debug_list_atn_decisions) {
std::wcout << L"predictATN decision " << dfa->decision << L" exec LA(1)==" << getLookaheadName(input) << L", outerContext=" << outerContext->toString(parser) << std::endl;
}
bool fullCtx = false;
std::shared_ptr<ATNConfigSet> s0_closure = computeStartState(dynamic_cast<ATNState*>(dfa->atnStartState),
ParserRuleContext::EMPTY, fullCtx);
dfa->s0 = addDFAState(dfa, new dfa::DFAState(s0_closure));
}
// We can start with an existing DFA.
int alt = execATN(dfa, dfa->s0, input, index, outerContext);
if (debug) {
std::wcout << "DFA after predictATN: " << dfa->toString(parser->getTokenNames()) << std::endl;
}
return alt;
}
catch (std::exception &e) {
// Do nothing, failed predict
}
mergeCache.clear(); // wack cache after each prediction mergeCache.clear(); // wack cache after each prediction
input->seek(index); input->seek(index);
input->release(m); input->release(m);
});
return 0; if (dfa.s0 == nullptr) {
if (outerContext == nullptr) {
outerContext = std::dynamic_pointer_cast<ParserRuleContext>(RuleContext::EMPTY);
}
if (debug || debug_list_atn_decisions) {
std::wcout << L"predictATN decision " << dfa.decision << L" exec LA(1)==" << getLookaheadName(input) << L", outerContext=" << outerContext->toString(parser) << std::endl;
}
bool fullCtx = false;
std::shared_ptr<ATNConfigSet> s0_closure = computeStartState(dynamic_cast<ATNState*>(dfa.atnStartState),
ParserRuleContext::EMPTY, fullCtx);
dfa.s0 = addDFAState(dfa, new dfa::DFAState(s0_closure));
}
// We can start with an existing DFA.
int alt = execATN(dfa, dfa.s0, input, index, outerContext);
if (debug) {
std::wcout << "DFA after predictATN: " << dfa.toString(parser->getTokenNames()) << std::endl;
}
return alt;
} }
int ParserATNSimulator::execATN(dfa::DFA *dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex, int ParserATNSimulator::execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex,
ParserRuleContextRef outerContext) { ParserRuleContext::Ref outerContext) {
if (debug || debug_list_atn_decisions) { if (debug || debug_list_atn_decisions) {
std::wcout << L"execATN decision " << dfa->decision << L" exec LA(1)==" << getLookaheadName(input) << std::wcout << L"execATN decision " << dfa.decision << L" exec LA(1)==" << getLookaheadName(input) <<
L" line " << input->LT(1)->getLine() << L":" << input->LT(1)->getCharPositionInLine() << std::endl; L" line " << input->LT(1)->getLine() << L":" << input->LT(1)->getCharPositionInLine() << std::endl;
} }
@ -193,7 +188,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;
std::shared_ptr<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,7 +235,7 @@ dfa::DFAState *ParserATNSimulator::getExistingTargetState(dfa::DFAState *previou
return edges[(size_t)t + 1]; return edges[(size_t)t + 1];
} }
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) {
std::shared_ptr<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.get()); addDFAEdge(dfa, previousD, t, ERROR.get());
@ -278,7 +273,7 @@ dfa::DFAState *ParserATNSimulator::computeTargetState(dfa::DFA *dfa, dfa::DFASta
} }
if (D->isAcceptState && D->configs->hasSemanticContext) { if (D->isAcceptState && D->configs->hasSemanticContext) {
predicateDFAState(D, atn.getDecisionState(dfa->decision)); predicateDFAState(D, atn.getDecisionState(dfa.decision));
if (D->predicates.size() != 0) { if (D->predicates.size() != 0) {
D->prediction = ATN::INVALID_ALT_NUMBER; D->prediction = ATN::INVALID_ALT_NUMBER;
} }
@ -297,7 +292,7 @@ void ParserATNSimulator::predicateDFAState(dfa::DFAState *dfaState, DecisionStat
// Update DFA so reach becomes accept state with (predicate,alt) // Update DFA so reach becomes accept state with (predicate,alt)
// pairs if preds found for conflicting alts // pairs if preds found for conflicting alts
BitSet altsToCollectPredsFrom = getConflictingAltsOrUniqueAlt(dfaState->configs); BitSet altsToCollectPredsFrom = getConflictingAltsOrUniqueAlt(dfaState->configs);
std::vector<SemanticContextRef> altToPred = getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState->configs, nalts); std::vector<SemanticContext::Ref> altToPred = getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState->configs, nalts);
if (!altToPred.empty()) { if (!altToPred.empty()) {
dfaState->predicates = getPredicatePredictions(altsToCollectPredsFrom, altToPred); dfaState->predicates = getPredicatePredictions(altsToCollectPredsFrom, altToPred);
dfaState->prediction = ATN::INVALID_ALT_NUMBER; // make sure we use preds dfaState->prediction = ATN::INVALID_ALT_NUMBER; // make sure we use preds
@ -309,8 +304,8 @@ void ParserATNSimulator::predicateDFAState(dfa::DFAState *dfaState, DecisionStat
} }
} }
int ParserATNSimulator::execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D, std::shared_ptr<ATNConfigSet> s0, int ParserATNSimulator::execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, std::shared_ptr<ATNConfigSet> s0,
TokenStream *input, size_t startIndex, ParserRuleContextRef outerContext) { TokenStream *input, size_t startIndex, ParserRuleContext::Ref 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;
} }
@ -448,7 +443,7 @@ std::shared_ptr<ATNConfigSet> ParserATNSimulator::computeReachSet(std::shared_pt
std::wcout << L"testing " << getTokenName(t) << L" at " << c->toString() << std::endl; std::wcout << L"testing " << getTokenName(t) << L" at " << c->toString() << std::endl;
} }
if (dynamic_cast<RuleStopState*>(c->state) != nullptr) { if (is<RuleStopState*>(c->state)) {
assert(c->context->isEmpty()); assert(c->context->isEmpty());
if (fullCtx || t == EOF) { if (fullCtx || t == EOF) {
@ -579,9 +574,9 @@ std::shared_ptr<ATNConfigSet> ParserATNSimulator::removeAllConfigsNotInRuleStopS
return result; return result;
} }
std::shared_ptr<ATNConfigSet> ParserATNSimulator::computeStartState(ATNState *p, RuleContextRef ctx, bool fullCtx) { std::shared_ptr<ATNConfigSet> ParserATNSimulator::computeStartState(ATNState *p, RuleContext::Ref 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); PredictionContext::Ref initialContext = PredictionContext::fromRuleContext(atn, ctx);
std::shared_ptr<ATNConfigSet> configs = std::make_shared<ATNConfigSet>(fullCtx); std::shared_ptr<ATNConfigSet> configs = std::make_shared<ATNConfigSet>(fullCtx);
for (size_t i = 0; i < p->getNumberOfTransitions(); i++) { for (size_t i = 0; i < p->getNumberOfTransitions(); i++) {
@ -595,7 +590,7 @@ std::shared_ptr<ATNConfigSet> ParserATNSimulator::computeStartState(ATNState *p,
} }
atn::ATNState *ParserATNSimulator::getReachableTarget(Transition *trans, int ttype) { atn::ATNState *ParserATNSimulator::getReachableTarget(Transition *trans, int ttype) {
if (trans->matches(ttype, 0, atn.maxTokenType)) { if (trans->matches(ttype, 0, (int)atn.maxTokenType)) {
return trans->target; return trans->target;
} }
@ -603,7 +598,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(const BitSet &ambigAlts, std::vector<SemanticContext::Ref> ParserATNSimulator::getPredsForAmbigAlts(const BitSet &ambigAlts,
std::shared_ptr<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
@ -617,13 +612,13 @@ std::vector<SemanticContextRef> ParserATNSimulator::getPredsForAmbigAlts(const B
* *
* From this, it is clear that NONE||anything==NONE. * From this, it is clear that NONE||anything==NONE.
*/ */
std::vector<SemanticContextRef> altToPred; std::vector<SemanticContext::Ref> altToPred;
for (auto c : configs->configs) { for (auto c : configs->configs) {
if (ambigAlts.test((size_t)c->alt)) { if (ambigAlts.test((size_t)c->alt)) {
if (altToPred.size() <= (size_t)c->alt) if (altToPred.size() <= (size_t)c->alt)
altToPred.resize((size_t)c->alt + 1); altToPred.resize((size_t)c->alt + 1);
altToPred[(size_t)c->alt] = std::make_shared<SemanticContext::OR>(altToPred[(size_t)c->alt], c->semanticContext); altToPred[(size_t)c->alt] = SemanticContext::Or(altToPred[(size_t)c->alt], c->semanticContext);
} }
} }
@ -647,11 +642,11 @@ std::vector<SemanticContextRef> ParserATNSimulator::getPredsForAmbigAlts(const B
} }
std::vector<dfa::DFAState::PredPrediction *> ParserATNSimulator::getPredicatePredictions(const antlrcpp::BitSet &ambigAlts, std::vector<dfa::DFAState::PredPrediction *> ParserATNSimulator::getPredicatePredictions(const antlrcpp::BitSet &ambigAlts,
std::vector<SemanticContextRef> altToPred) { std::vector<SemanticContext::Ref> altToPred) {
std::vector<dfa::DFAState::PredPrediction*> pairs; std::vector<dfa::DFAState::PredPrediction*> pairs;
bool containsPredicate = false; bool containsPredicate = false;
for (size_t i = 1; i < altToPred.size(); i++) { for (size_t i = 1; i < altToPred.size(); i++) {
SemanticContextRef pred = altToPred[i]; SemanticContext::Ref pred = altToPred[i];
// unpredicted is indicated by SemanticContext.NONE // unpredicted is indicated by SemanticContext.NONE
assert(pred != nullptr); assert(pred != nullptr);
@ -672,40 +667,40 @@ std::vector<dfa::DFAState::PredPrediction *> ParserATNSimulator::getPredicatePre
} }
int ParserATNSimulator::getAltThatFinishedDecisionEntryRule(std::shared_ptr<ATNConfigSet> configs) { int ParserATNSimulator::getAltThatFinishedDecisionEntryRule(std::shared_ptr<ATNConfigSet> configs) {
misc::IntervalSet *alts = nullptr; misc::IntervalSet alts;
for (auto c : configs->configs) { for (auto c : configs->configs) {
if (c->reachesIntoOuterContext > 0 || (dynamic_cast<RuleStopState*>(c->state) != nullptr && c->context->hasEmptyPath())) { if (c->reachesIntoOuterContext > 0 || (is<RuleStopState*>(c->state) && c->context->hasEmptyPath())) {
alts->add(c->alt); alts.add(c->alt);
} }
} }
if (alts->size() == 0) { if (alts.size() == 0) {
return ATN::INVALID_ALT_NUMBER; return ATN::INVALID_ALT_NUMBER;
} }
return alts->getMinElement(); return alts.getMinElement();
} }
BitSet ParserATNSimulator::evalSemanticContext(std::vector<dfa::DFAState::PredPrediction*> predPredictions, BitSet ParserATNSimulator::evalSemanticContext(std::vector<dfa::DFAState::PredPrediction*> predPredictions,
ParserRuleContextRef outerContext, bool complete) { ParserRuleContext::Ref outerContext, bool complete) {
BitSet predictions; BitSet predictions;
for (auto pair : predPredictions) { for (auto prediction : predPredictions) {
if (pair->pred == SemanticContext::NONE) { if (prediction->pred == SemanticContext::NONE) {
predictions.set((size_t)pair->alt); predictions.set((size_t)prediction->alt);
if (!complete) { if (!complete) {
break; break;
} }
continue; continue;
} }
bool predicateEvaluationResult = pair->pred->eval(parser, outerContext); bool predicateEvaluationResult = prediction->pred->eval(parser, outerContext);
if (debug || dfa_debug) { if (debug || dfa_debug) {
std::wcout << L"eval pred " << pair << L"=" << predicateEvaluationResult << std::endl; std::wcout << L"eval pred " << prediction->toString() << L" = " << predicateEvaluationResult << std::endl;
} }
if (predicateEvaluationResult) { if (predicateEvaluationResult) {
if (debug || dfa_debug) { if (debug || dfa_debug) {
std::wcout << L"PREDICT " << pair->alt << std::endl; std::wcout << L"PREDICT " << prediction->alt << std::endl;
} }
predictions.set((size_t)pair->alt); predictions.set((size_t)prediction->alt);
if (!complete) { if (!complete) {
break; break;
} }
@ -941,7 +936,7 @@ atn::ATNConfig *ParserATNSimulator::ruleTransition(ATNConfig *config, RuleTransi
} }
atn::ATNState *returnState = t->followState; atn::ATNState *returnState = t->followState;
PredictionContextRef newContext = SingletonPredictionContext::create(config->context, returnState->stateNumber); PredictionContext::Ref newContext = SingletonPredictionContext::create(config->context, returnState->stateNumber);
return new atn::ATNConfig(config, t->target, newContext); return new atn::ATNConfig(config, t->target, newContext);
} }
@ -980,9 +975,9 @@ std::wstring ParserATNSimulator::getLookaheadName(TokenStream *input) {
return getTokenName(input->LA(1)); return getTokenName(input->LA(1));
} }
void ParserATNSimulator::dumpDeadEndConfigs(NoViableAltException *nvae) { void ParserATNSimulator::dumpDeadEndConfigs(NoViableAltException &nvae) {
std::wcerr << L"dead end configs: "; std::wcerr << L"dead end configs: ";
for (auto c : nvae->getDeadEndConfigs()->configs) { for (auto c : nvae.getDeadEndConfigs()->configs) {
std::wstring trans = L"no edges"; std::wstring trans = L"no edges";
if (c->state->getNumberOfTransitions() > 0) { if (c->state->getNumberOfTransitions() > 0) {
Transition *t = c->state->transition(0); Transition *t = c->state->transition(0);
@ -1001,9 +996,9 @@ void ParserATNSimulator::dumpDeadEndConfigs(NoViableAltException *nvae) {
} }
} }
NoViableAltException *ParserATNSimulator::noViableAlt(TokenStream *input, ParserRuleContextRef outerContext, NoViableAltException ParserATNSimulator::noViableAlt(TokenStream *input, ParserRuleContext::Ref outerContext,
std::shared_ptr<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 NoViableAltException(parser, input, input->get(startIndex), input->LT(1), configs, outerContext);
} }
int ParserATNSimulator::getUniqueAlt(std::shared_ptr<ATNConfigSet> configs) { int ParserATNSimulator::getUniqueAlt(std::shared_ptr<ATNConfigSet> configs) {
@ -1018,7 +1013,7 @@ int ParserATNSimulator::getUniqueAlt(std::shared_ptr<ATNConfigSet> configs) {
return alt; return alt;
} }
dfa::DFAState *ParserATNSimulator::addDFAEdge(dfa::DFA *dfa, dfa::DFAState *from, ssize_t t, dfa::DFAState *to) { dfa::DFAState *ParserATNSimulator::addDFAEdge(dfa::DFA &dfa, dfa::DFAState *from, ssize_t t, dfa::DFAState *to) {
if (debug) { if (debug) {
std::wcout << L"EDGE " << from << L" -> " << to << L" upon " << getTokenName(t) << std::endl; std::wcout << L"EDGE " << from << L" -> " << to << L" upon " << getTokenName(t) << std::endl;
} }
@ -1028,17 +1023,15 @@ dfa::DFAState *ParserATNSimulator::addDFAEdge(dfa::DFA *dfa, dfa::DFAState *from
} }
to = addDFAState(dfa, to); // used existing if possible not incoming to = addDFAState(dfa, to); // used existing if possible not incoming
if (from == nullptr || t > atn.maxTokenType) { if (from == nullptr || t > (int)atn.maxTokenType) {
return to; return to;
} }
if (true) { {
std::lock_guard<std::mutex> lck(mtx); std::lock_guard<std::mutex> lck(mtx);
if (from->edges.size() == 0) { if (from->edges.empty())
from->edges = std::vector<dfa::DFAState*>(); from->edges.resize(atn.maxTokenType + 1 + 1);
}
from->edges[(size_t)(t + 1)] = to; // connect from->edges[(size_t)(t + 1)] = to; // connect
} }
@ -1047,30 +1040,31 @@ dfa::DFAState *ParserATNSimulator::addDFAEdge(dfa::DFA *dfa, dfa::DFAState *from
if (parser != nullptr) { if (parser != nullptr) {
names = parser->getTokenNames(); names = parser->getTokenNames();
} }
std::wcout << L"DFA=\n" << dfa->toString(names) << std::endl; std::wcout << L"DFA=\n" << dfa.toString(names) << std::endl;
} }
return to; return to;
} }
dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA *dfa, dfa::DFAState *D) { dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA &dfa, dfa::DFAState *D) {
if (D == ERROR.get()) { if (D == ERROR.get()) {
return D; return D;
} }
if (true) { {
std::lock_guard<std::mutex> lck(mtx); std::lock_guard<std::mutex> lck(mtx);
dfa::DFAState *existing = dfa->states->at(D);
if (existing != nullptr) { auto existing = dfa.states.find(D);
return existing; if (existing != dfa.states.end()) {
return existing->second;
} }
D->stateNumber = (int)dfa->states->size(); D->stateNumber = (int)dfa.states.size();
if (!D->configs->isReadonly()) { if (!D->configs->isReadonly()) {
D->configs->optimizeConfigs(this); D->configs->optimizeConfigs(this);
D->configs->setReadonly(true); D->configs->setReadonly(true);
} }
dfa->states->insert(std::pair<dfa::DFAState*, dfa::DFAState*>(D, D)); dfa.states[D] = D;
if (debug) { if (debug) {
std::cout << L"adding new DFA state: " << D << std::endl; std::cout << L"adding new DFA state: " << D << std::endl;
} }
@ -1078,29 +1072,29 @@ dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA *dfa, dfa::DFAState *D)
} }
} }
void ParserATNSimulator::reportAttemptingFullContext(dfa::DFA *dfa, const antlrcpp::BitSet &conflictingAlts, void ParserATNSimulator::reportAttemptingFullContext(dfa::DFA &dfa, const antlrcpp::BitSet &conflictingAlts,
std::shared_ptr<ATNConfigSet> configs, 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;
} }
if (parser != nullptr) { if (parser != nullptr) {
parser->getErrorListenerDispatch()->reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, conflictingAlts, configs); parser->getErrorListenerDispatch().reportAttemptingFullContext(parser, dfa, startIndex, stopIndex, conflictingAlts, configs);
} }
} }
void ParserATNSimulator::reportContextSensitivity(dfa::DFA *dfa, int prediction, std::shared_ptr<ATNConfigSet> configs, void ParserATNSimulator::reportContextSensitivity(dfa::DFA &dfa, int prediction, std::shared_ptr<ATNConfigSet> configs,
size_t startIndex, 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;
} }
if (parser != nullptr) { if (parser != nullptr) {
parser->getErrorListenerDispatch()->reportContextSensitivity(parser, dfa, startIndex, stopIndex, prediction, configs); parser->getErrorListenerDispatch().reportContextSensitivity(parser, dfa, startIndex, stopIndex, prediction, configs);
} }
} }
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, const antlrcpp::BitSet &ambigAlts, std::shared_ptr<ATNConfigSet> configs) { bool exact, const antlrcpp::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);
@ -1119,11 +1113,11 @@ void ParserATNSimulator::reportAmbiguity(dfa::DFA *dfa, dfa::DFAState *D, size_t
// } // }
// i++; // i++;
// } // }
misc::Interval interval = misc::Interval(startIndex, stopIndex); misc::Interval interval = misc::Interval((int)startIndex, (int)stopIndex);
std::wcout << L"reportAmbiguity " << ambigAlts << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl; std::wcout << L"reportAmbiguity " << ambigAlts << L":" << configs << L", input=" << parser->getTokenStream()->getText(interval) << std::endl;
} }
if (parser != nullptr) { if (parser != nullptr) {
parser->getErrorListenerDispatch()->reportAmbiguity(parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs); parser->getErrorListenerDispatch().reportAmbiguity(parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs);
} }
} }

View File

@ -252,7 +252,7 @@ namespace atn {
Parser *const parser; Parser *const parser;
public: public:
const std::vector<dfa::DFA *> &_decisionToDFA; std::vector<dfa::DFA> &_decisionToDFA;
/// <summary> /// <summary>
/// SLL, LL, or LL + exact ambig detection? </summary> /// SLL, LL, or LL + exact ambig detection? </summary>
@ -276,20 +276,20 @@ namespace atn {
// LAME globals to avoid parameters!!!!! I need these down deep in predTransition // LAME globals to avoid parameters!!!!! I need these down deep in predTransition
TokenStream *_input; TokenStream *_input;
int _startIndex; int _startIndex;
ParserRuleContextRef _outerContext; ParserRuleContext::Ref _outerContext;
/// <summary> /// <summary>
/// Testing only! </summary> /// Testing only! </summary>
public: public:
ParserATNSimulator(const ATN &atn, const std::vector<dfa::DFA *>& decisionToDFA, ParserATNSimulator(const ATN &atn, std::vector<dfa::DFA>& decisionToDFA,
std::shared_ptr<PredictionContextCache> sharedContextCache); std::shared_ptr<PredictionContextCache> sharedContextCache);
ParserATNSimulator(Parser *parser, const ATN &atn, const std::vector<dfa::DFA *> &decisionToDFA, ParserATNSimulator(Parser *parser, const ATN &atn, std::vector<dfa::DFA> &decisionToDFA,
std::shared_ptr<PredictionContextCache> sharedContextCache); std::shared_ptr<PredictionContextCache> sharedContextCache);
virtual void reset() override; virtual void reset() override;
virtual int adaptivePredict(TokenStream *input, int decision, ParserRuleContextRef outerContext); virtual int adaptivePredict(TokenStream *input, int decision, ParserRuleContext::Ref outerContext);
/// <summary> /// <summary>
/// Performs ATN simulation to compute a predicted alternative based /// Performs ATN simulation to compute a predicted alternative based
@ -323,8 +323,8 @@ namespace atn {
/// conflict + preds /// conflict + preds
/// </summary> /// </summary>
protected: protected:
virtual int execATN(dfa::DFA *dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex, virtual int execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex,
ParserRuleContextRef outerContext); ParserRuleContext::Ref outerContext);
/// <summary> /// <summary>
/// Get an existing target state for an edge in the DFA. If the target state /// Get an existing target state for an edge in the DFA. If the target state
@ -349,13 +349,13 @@ namespace atn {
/// <returns> The computed target DFA state for the given input symbol /// <returns> The computed target DFA state for the given input symbol
/// {@code t}. If {@code t} does not lead to a valid DFA state, this method /// {@code t}. If {@code t} does not lead to a valid DFA state, this method
/// returns <seealso cref="#ERROR"/>. </returns> /// returns <seealso cref="#ERROR"/>. </returns>
virtual dfa::DFAState *computeTargetState(dfa::DFA *dfa, dfa::DFAState *previousD, ssize_t t); virtual dfa::DFAState *computeTargetState(dfa::DFA &dfa, dfa::DFAState *previousD, ssize_t t);
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, std::shared_ptr<ATNConfigSet> s0, virtual int execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, std::shared_ptr<ATNConfigSet> s0,
TokenStream *input, size_t startIndex, ParserRuleContextRef outerContext); // how far we got before failing over TokenStream *input, size_t startIndex, ParserRuleContext::Ref outerContext); // how far we got before failing over
virtual std::shared_ptr<ATNConfigSet> computeReachSet(std::shared_ptr<ATNConfigSet> closure, ssize_t t, bool fullCtx); virtual std::shared_ptr<ATNConfigSet> computeReachSet(std::shared_ptr<ATNConfigSet> closure, ssize_t t, bool fullCtx);
@ -380,15 +380,15 @@ namespace atn {
/// 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 std::shared_ptr<ATNConfigSet> removeAllConfigsNotInRuleStopState(std::shared_ptr<ATNConfigSet> configs, bool lookToEndOfRule); virtual std::shared_ptr<ATNConfigSet> removeAllConfigsNotInRuleStopState(std::shared_ptr<ATNConfigSet> configs, bool lookToEndOfRule);
virtual std::shared_ptr<ATNConfigSet> computeStartState(ATNState *p, RuleContextRef ctx, bool fullCtx); virtual std::shared_ptr<ATNConfigSet> computeStartState(ATNState *p, RuleContext::Ref ctx, bool fullCtx);
virtual ATNState *getReachableTarget(Transition *trans, int ttype); virtual ATNState *getReachableTarget(Transition *trans, int ttype);
virtual std::vector<SemanticContextRef> getPredsForAmbigAlts(const antlrcpp::BitSet &ambigAlts, virtual std::vector<SemanticContext::Ref> getPredsForAmbigAlts(const antlrcpp::BitSet &ambigAlts,
std::shared_ptr<ATNConfigSet> configs, size_t nalts); std::shared_ptr<ATNConfigSet> configs, size_t nalts);
virtual std::vector<dfa::DFAState::PredPrediction*> getPredicatePredictions(const antlrcpp::BitSet &ambigAlts, virtual std::vector<dfa::DFAState::PredPrediction*> getPredicatePredictions(const antlrcpp::BitSet &ambigAlts,
std::vector<SemanticContextRef> altToPred); std::vector<SemanticContext::Ref> altToPred);
virtual int getAltThatFinishedDecisionEntryRule(std::shared_ptr<ATNConfigSet> configs); virtual int getAltThatFinishedDecisionEntryRule(std::shared_ptr<ATNConfigSet> configs);
@ -400,7 +400,7 @@ namespace atn {
/// includes pairs with null predicates. /// includes pairs with null predicates.
/// </summary> /// </summary>
virtual antlrcpp::BitSet evalSemanticContext(std::vector<dfa::DFAState::PredPrediction*> predPredictions, virtual antlrcpp::BitSet evalSemanticContext(std::vector<dfa::DFAState::PredPrediction*> predPredictions,
ParserRuleContextRef outerContext, bool complete); ParserRuleContext::Ref outerContext, bool complete);
/* TO_DO: If we are doing predicates, there is no point in pursuing /* TO_DO: If we are doing predicates, there is no point in pursuing
@ -486,10 +486,10 @@ namespace atn {
/// it out for clarity now that alg. works well. We can leave this /// it out for clarity now that alg. works well. We can leave this
/// "dead" code for a bit. /// "dead" code for a bit.
/// </summary> /// </summary>
virtual void dumpDeadEndConfigs(NoViableAltException *nvae); virtual void dumpDeadEndConfigs(NoViableAltException &nvae);
protected: protected:
virtual NoViableAltException *noViableAlt(TokenStream *input, ParserRuleContextRef outerContext, virtual NoViableAltException noViableAlt(TokenStream *input, ParserRuleContext::Ref outerContext,
std::shared_ptr<ATNConfigSet> configs, size_t startIndex); std::shared_ptr<ATNConfigSet> configs, size_t startIndex);
static int getUniqueAlt(std::shared_ptr<ATNConfigSet> configs); static int getUniqueAlt(std::shared_ptr<ATNConfigSet> configs);
@ -513,7 +513,7 @@ namespace atn {
/// <returns> If {@code to} is {@code null}, this method returns {@code null}; /// <returns> If {@code to} is {@code null}, this method returns {@code null};
/// otherwise this method returns the result of calling <seealso cref="#addDFAState"/> /// otherwise this method returns the result of calling <seealso cref="#addDFAState"/>
/// on {@code to} </returns> /// on {@code to} </returns>
virtual dfa::DFAState *addDFAEdge(dfa::DFA *dfa, dfa::DFAState *from, ssize_t t, dfa::DFAState *to); virtual dfa::DFAState *addDFAEdge(dfa::DFA &dfa, dfa::DFAState *from, ssize_t t, dfa::DFAState *to);
/// <summary> /// <summary>
/// Add state {@code D} to the DFA if it is not already present, and return /// Add state {@code D} to the DFA if it is not already present, and return
@ -529,17 +529,17 @@ namespace atn {
/// <returns> The state stored in the DFA. This will be either the existing /// <returns> The state stored in the DFA. This will be either the existing
/// state if {@code D} is already in the DFA, or {@code D} itself if the /// state if {@code D} is already in the DFA, or {@code D} itself if the
/// 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, const antlrcpp::BitSet &conflictingAlts, virtual void reportAttemptingFullContext(dfa::DFA &dfa, const antlrcpp::BitSet &conflictingAlts,
std::shared_ptr<ATNConfigSet> configs, 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, std::shared_ptr<ATNConfigSet> configs, virtual void reportContextSensitivity(dfa::DFA &dfa, int prediction, std::shared_ptr<ATNConfigSet> configs,
size_t startIndex, size_t stopIndex); 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, virtual void reportAmbiguity(dfa::DFA &dfa, dfa::DFAState *D, size_t startIndex, size_t stopIndex, bool exact,
const antlrcpp::BitSet &ambigAlts, std::shared_ptr<ATNConfigSet> configs); const antlrcpp::BitSet &ambigAlts, std::shared_ptr<ATNConfigSet> configs);
public: public:

View File

@ -53,5 +53,5 @@ std::shared_ptr<SemanticContext::PrecedencePredicate> PrecedencePredicateTransit
} }
std::wstring PrecedencePredicateTransition::toString() const { std::wstring PrecedencePredicateTransition::toString() const {
return std::to_wstring(precedence) + std::wstring(L" >= _p"); return L"PRECEDENCE " + Transition::toString() + L" { precedence: " + std::to_wstring(precedence) + L" }";
} }

View File

@ -50,7 +50,7 @@ namespace atn {
virtual bool isEpsilon() const override; virtual bool isEpsilon() const override;
virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override;
std::shared_ptr<SemanticContext::PrecedencePredicate> getPredicate() const; std::shared_ptr<SemanticContext::PrecedencePredicate> getPredicate() const;
virtual std::wstring toString() const; virtual std::wstring toString() const override;
}; };

View File

@ -53,5 +53,8 @@ std::shared_ptr<SemanticContext::Predicate> PredicateTransition::getPredicate()
} }
std::wstring PredicateTransition::toString() const { std::wstring PredicateTransition::toString() const {
return std::wstring(L"pred_") + std::to_wstring(ruleIndex) + std::wstring(L":") + std::to_wstring(predIndex); return L"PREDICATE " + Transition::toString() + L" { ruleIndex: " + std::to_wstring(ruleIndex) +
L", predIndex: " + std::to_wstring(predIndex) + L", isCtxDependent: " + std::to_wstring(isCtxDependent) + L" }";
// Generate and add a predicate context here?
} }

View File

@ -60,7 +60,7 @@ namespace atn {
std::shared_ptr<SemanticContext::Predicate> getPredicate() const; std::shared_ptr<SemanticContext::Predicate> getPredicate() const;
virtual std::wstring toString() const; virtual std::wstring toString() const override;
}; };

View File

@ -53,9 +53,9 @@ const int PredictionContext::INITIAL_HASH;
PredictionContext::PredictionContext(size_t cachedHashCode) : id(globalNodeCount++), cachedHashCode(cachedHashCode) { PredictionContext::PredictionContext(size_t cachedHashCode) : id(globalNodeCount++), cachedHashCode(cachedHashCode) {
} }
PredictionContextRef PredictionContext::fromRuleContext(const ATN &atn, RuleContextRef outerContext) { PredictionContext::Ref PredictionContext::fromRuleContext(const ATN &atn, RuleContext::Ref outerContext) {
if (!outerContext) { if (!outerContext) {
outerContext = std::dynamic_pointer_cast<RuleContext>(RuleContext::EMPTY); outerContext = RuleContext::EMPTY;
} }
// if we are in RuleContext of start rule, s, then PredictionContext // if we are in RuleContext of start rule, s, then PredictionContext
@ -65,9 +65,9 @@ PredictionContextRef PredictionContext::fromRuleContext(const ATN &atn, RuleCont
} }
// If we have a parent, convert it to a PredictionContext graph // If we have a parent, convert it to a PredictionContext graph
PredictionContextRef parent = PredictionContext::fromRuleContext(atn, outerContext->parent.lock()); PredictionContext::Ref parent = PredictionContext::fromRuleContext(atn, outerContext->parent.lock());
ATNState *state = atn.states[(size_t)outerContext->invokingState]; ATNState *state = atn.states.at((size_t)outerContext->invokingState);
RuleTransition *transition = (RuleTransition *)state->transition(0); RuleTransition *transition = (RuleTransition *)state->transition(0);
return SingletonPredictionContext::create(parent, transition->followState->stateNumber); return SingletonPredictionContext::create(parent, transition->followState->stateNumber);
} }
@ -117,8 +117,8 @@ size_t PredictionContext::calculateHashCode(const std::vector<std::weak_ptr<Pred
return MurmurHash::finish(hash, parents.size() + returnStates.size()); return MurmurHash::finish(hash, parents.size() + returnStates.size());
} }
PredictionContextRef PredictionContext::merge(PredictionContextRef a, PredictionContext::Ref PredictionContext::merge(PredictionContext::Ref a,
PredictionContextRef b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) { PredictionContext::Ref b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) {
assert(a && b); assert(a && b);
@ -154,8 +154,8 @@ PredictionContextRef PredictionContext::merge(PredictionContextRef a,
std::dynamic_pointer_cast<ArrayPredictionContext>(b), rootIsWildcard, mergeCache); std::dynamic_pointer_cast<ArrayPredictionContext>(b), rootIsWildcard, mergeCache);
} }
PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionContextRef a, PredictionContext::Ref PredictionContext::mergeSingletons(SingletonPredictionContext::Ref a,
SingletonPredictionContextRef b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) { SingletonPredictionContext::Ref b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) {
if (mergeCache != nullptr) { // Can be null if not given to the ATNState from which this call originates. if (mergeCache != nullptr) { // Can be null if not given to the ATNState from which this call originates.
auto iterator = mergeCache->find({ a.get(), b.get() }); auto iterator = mergeCache->find({ a.get(), b.get() });
@ -168,7 +168,7 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte
} }
} }
PredictionContextRef rootMerge = mergeRoot(a, b, rootIsWildcard); PredictionContext::Ref rootMerge = mergeRoot(a, b, rootIsWildcard);
if (rootMerge) { if (rootMerge) {
if (mergeCache != nullptr) { if (mergeCache != nullptr) {
(*mergeCache)[{ a.get(), b.get() }] = rootMerge; (*mergeCache)[{ a.get(), b.get() }] = rootMerge;
@ -176,10 +176,10 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte
return rootMerge; return rootMerge;
} }
PredictionContextRef parentA = a->parent.lock(); PredictionContext::Ref parentA = a->parent.lock();
PredictionContextRef parentB = b->parent.lock(); PredictionContext::Ref parentB = b->parent.lock();
if (a->returnState == b->returnState) { // a == b if (a->returnState == b->returnState) { // a == b
PredictionContextRef parent = merge(parentA, parentB, rootIsWildcard, mergeCache); PredictionContext::Ref parent = merge(parentA, parentB, rootIsWildcard, mergeCache);
// If parent is same as existing a or b parent or reduced to a parent, return it. // If parent is same as existing a or b parent or reduced to a parent, return it.
if (parent == parentA) { // ax + bx = ax, if a=b if (parent == parentA) { // ax + bx = ax, if a=b
@ -193,7 +193,7 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte
// merge parents x and y, giving array node with x,y then remainders // merge parents x and y, giving array node with x,y then remainders
// of those graphs. dup a, a' points at merged array // of those graphs. dup a, a' points at merged array
// new joined parent so create new singleton pointing to it, a' // new joined parent so create new singleton pointing to it, a'
PredictionContextRef a_ = SingletonPredictionContext::create(parent, a->returnState); PredictionContext::Ref a_ = SingletonPredictionContext::create(parent, a->returnState);
if (mergeCache != nullptr) { if (mergeCache != nullptr) {
(*mergeCache)[{ a.get(), b.get() }] = a_; (*mergeCache)[{ a.get(), b.get() }] = a_;
} }
@ -212,7 +212,7 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte
payloads[1] = a->returnState; payloads[1] = a->returnState;
} }
std::vector<std::weak_ptr<PredictionContext>> parents = { singleParent, singleParent }; std::vector<std::weak_ptr<PredictionContext>> parents = { singleParent, singleParent };
PredictionContextRef a_(new ArrayPredictionContext(parents, payloads)); PredictionContext::Ref a_(new ArrayPredictionContext(parents, payloads));
if (mergeCache != nullptr) { if (mergeCache != nullptr) {
(*mergeCache)[{ a.get(), b.get() }] = a_; (*mergeCache)[{ a.get(), b.get() }] = a_;
} }
@ -222,7 +222,7 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte
// parents differ and can't merge them. Just pack together // parents differ and can't merge them. Just pack together
// into array; can't merge. // into array; can't merge.
// ax + by = [ax,by] // ax + by = [ax,by]
PredictionContextRef a_; PredictionContext::Ref a_;
if (a->returnState > b->returnState) { // sort by payload if (a->returnState > b->returnState) { // sort by payload
std::vector<int> payloads = { b->returnState, a->returnState }; std::vector<int> payloads = { b->returnState, a->returnState };
std::vector<std::weak_ptr<PredictionContext>> parents = { b->parent, a->parent }; std::vector<std::weak_ptr<PredictionContext>> parents = { b->parent, a->parent };
@ -240,7 +240,7 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte
} }
} }
PredictionContextRef PredictionContext::mergeRoot(SingletonPredictionContextRef a, SingletonPredictionContextRef b, PredictionContext::Ref PredictionContext::mergeRoot(SingletonPredictionContext::Ref a, SingletonPredictionContext::Ref b,
bool rootIsWildcard) { bool rootIsWildcard) {
if (rootIsWildcard) { if (rootIsWildcard) {
if (a == EMPTY) { // * + b = * if (a == EMPTY) { // * + b = *
@ -256,20 +256,20 @@ PredictionContextRef PredictionContext::mergeRoot(SingletonPredictionContextRef
if (a == EMPTY) { // $ + x = [$,x] if (a == EMPTY) { // $ + x = [$,x]
std::vector<int> payloads = { b->returnState, EMPTY_RETURN_STATE }; std::vector<int> payloads = { b->returnState, EMPTY_RETURN_STATE };
std::vector<std::weak_ptr<PredictionContext>> parents = { b->parent, EMPTY }; std::vector<std::weak_ptr<PredictionContext>> parents = { b->parent, EMPTY };
PredictionContextRef joined(new ArrayPredictionContext(parents, payloads)); PredictionContext::Ref joined(new ArrayPredictionContext(parents, payloads));
return joined; return joined;
} }
if (b == EMPTY) { // x + $ = [$,x] ($ is always first if present) if (b == EMPTY) { // x + $ = [$,x] ($ is always first if present)
std::vector<int> payloads = { a->returnState, EMPTY_RETURN_STATE }; std::vector<int> payloads = { a->returnState, EMPTY_RETURN_STATE };
std::vector<std::weak_ptr<PredictionContext>> parents = { a->parent, EMPTY }; std::vector<std::weak_ptr<PredictionContext>> parents = { a->parent, EMPTY };
PredictionContextRef joined(new ArrayPredictionContext(parents, payloads)); PredictionContext::Ref joined(new ArrayPredictionContext(parents, payloads));
return joined; return joined;
} }
} }
return nullptr; return nullptr;
} }
PredictionContextRef PredictionContext::mergeArrays(std::shared_ptr<ArrayPredictionContext> a, PredictionContext::Ref PredictionContext::mergeArrays(std::shared_ptr<ArrayPredictionContext> a,
std::shared_ptr<ArrayPredictionContext> b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) { std::shared_ptr<ArrayPredictionContext> b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) {
if (mergeCache != nullptr) { if (mergeCache != nullptr) {
@ -295,8 +295,8 @@ PredictionContextRef PredictionContext::mergeArrays(std::shared_ptr<ArrayPredict
// walk and merge to yield mergedParents, mergedReturnStates // walk and merge to yield mergedParents, mergedReturnStates
while (i < a->returnStates.size() && j < b->returnStates.size()) { while (i < a->returnStates.size() && j < b->returnStates.size()) {
PredictionContextRef a_parent = a->parents[i].lock(); PredictionContext::Ref a_parent = a->parents[i].lock();
PredictionContextRef b_parent = b->parents[j].lock(); PredictionContext::Ref b_parent = b->parents[j].lock();
if (a->returnStates[i] == b->returnStates[j]) { if (a->returnStates[i] == b->returnStates[j]) {
// same payload (stack tops are equal), must yield merged singleton // same payload (stack tops are equal), must yield merged singleton
int payload = a->returnStates[i]; int payload = a->returnStates[i];
@ -308,7 +308,7 @@ PredictionContextRef PredictionContext::mergeArrays(std::shared_ptr<ArrayPredict
mergedReturnStates[k] = payload; mergedReturnStates[k] = payload;
} }
else { // ax+ay -> a'[x,y] else { // ax+ay -> a'[x,y]
PredictionContextRef mergedParent = merge(a_parent, b_parent, rootIsWildcard, mergeCache); PredictionContext::Ref mergedParent = merge(a_parent, b_parent, rootIsWildcard, mergeCache);
mergedParents[k] = mergedParent; mergedParents[k] = mergedParent;
mergedReturnStates[k] = payload; mergedReturnStates[k] = payload;
} }
@ -345,7 +345,7 @@ PredictionContextRef PredictionContext::mergeArrays(std::shared_ptr<ArrayPredict
// trim merged if we combined a few that had same stack tops // trim merged if we combined a few that had same stack tops
if (k < mergedParents.size()) { // write index < last position; trim if (k < mergedParents.size()) { // write index < last position; trim
if (k == 1) { // for just one merged element, return singleton top if (k == 1) { // for just one merged element, return singleton top
PredictionContextRef a_ = SingletonPredictionContext::create(mergedParents[0].lock(), mergedReturnStates[0]); PredictionContext::Ref a_ = SingletonPredictionContext::create(mergedParents[0].lock(), mergedReturnStates[0]);
if (mergeCache != nullptr) { if (mergeCache != nullptr) {
(*mergeCache)[{ a.get(), b.get() }] = a_; (*mergeCache)[{ a.get(), b.get() }] = a_;
} }
@ -382,10 +382,10 @@ PredictionContextRef PredictionContext::mergeArrays(std::shared_ptr<ArrayPredict
} }
bool PredictionContext::combineCommonParents(std::vector<std::weak_ptr<PredictionContext>> &parents) { bool PredictionContext::combineCommonParents(std::vector<std::weak_ptr<PredictionContext>> &parents) {
std::set<PredictionContextRef> uniqueParents; std::set<PredictionContext::Ref> uniqueParents;
for (size_t p = 0; p < parents.size(); p++) { for (size_t p = 0; p < parents.size(); p++) {
PredictionContextRef parent = parents[p].lock(); PredictionContext::Ref parent = parents[p].lock();
// ml: it's assumed that the == operator of PredictionContext kicks in here. // ml: it's assumed that the == operator of PredictionContext kicks in here.
if (uniqueParents.find(parent) == uniqueParents.end()) { // don't replace if (uniqueParents.find(parent) == uniqueParents.end()) { // don't replace
uniqueParents.insert(parent); uniqueParents.insert(parent);
@ -400,7 +400,7 @@ bool PredictionContext::combineCommonParents(std::vector<std::weak_ptr<Predictio
return true; return true;
} }
std::wstring PredictionContext::toDOTString(PredictionContextRef context) { std::wstring PredictionContext::toDOTString(PredictionContext::Ref context) {
if (context == nullptr) { if (context == nullptr) {
return L""; return L"";
} }
@ -408,8 +408,8 @@ std::wstring PredictionContext::toDOTString(PredictionContextRef context) {
std::wstringstream ss; std::wstringstream ss;
ss << L"digraph G {\n" << L"rankdir=LR;\n"; ss << L"digraph G {\n" << L"rankdir=LR;\n";
std::vector<PredictionContextRef> nodes = getAllContextNodes(context); std::vector<PredictionContext::Ref> nodes = getAllContextNodes(context);
std::sort(nodes.begin(), nodes.end(), [](PredictionContextRef o1, PredictionContextRef o2) { std::sort(nodes.begin(), nodes.end(), [](PredictionContext::Ref o1, PredictionContext::Ref o2) {
return o1->id - o2->id; return o1->id - o2->id;
}); });
@ -464,8 +464,8 @@ std::wstring PredictionContext::toDOTString(PredictionContextRef context) {
} }
// The "visited" map is just a temporary structure to control the retrieval process (which is recursive). // The "visited" map is just a temporary structure to control the retrieval process (which is recursive).
PredictionContextRef PredictionContext::getCachedContext(PredictionContextRef context, PredictionContext::Ref PredictionContext::getCachedContext(PredictionContext::Ref context,
std::shared_ptr<PredictionContextCache> contextCache, std::map<PredictionContextRef, PredictionContextRef> &visited) { std::shared_ptr<PredictionContextCache> contextCache, std::map<PredictionContext::Ref, PredictionContext::Ref> &visited) {
if (context->isEmpty()) { if (context->isEmpty()) {
return context; return context;
} }
@ -510,7 +510,7 @@ PredictionContextRef PredictionContext::getCachedContext(PredictionContextRef co
return context; return context;
} }
PredictionContextRef updated; PredictionContext::Ref updated;
if (parents.empty()) { if (parents.empty()) {
updated = EMPTY; updated = EMPTY;
} else if (parents.size() == 1) { } else if (parents.size() == 1) {
@ -526,17 +526,17 @@ PredictionContextRef PredictionContext::getCachedContext(PredictionContextRef co
return updated; return updated;
} }
std::vector<PredictionContextRef> PredictionContext::getAllContextNodes(PredictionContextRef context) { std::vector<PredictionContext::Ref> PredictionContext::getAllContextNodes(PredictionContext::Ref context) {
std::vector<PredictionContextRef> nodes; std::vector<PredictionContext::Ref> nodes;
std::map<PredictionContextRef, PredictionContextRef> visited; std::map<PredictionContext::Ref, PredictionContext::Ref> visited;
getAllContextNodes_(context, nodes, visited); getAllContextNodes_(context, nodes, visited);
return nodes; return nodes;
} }
void PredictionContext::getAllContextNodes_(PredictionContextRef context, void PredictionContext::getAllContextNodes_(PredictionContext::Ref context,
std::vector<PredictionContextRef> &nodes, std::vector<PredictionContext::Ref> &nodes,
std::map<PredictionContextRef, PredictionContextRef> &visited) { std::map<PredictionContext::Ref, PredictionContext::Ref> &visited) {
if (visited.find(context) != visited.end()) { if (visited.find(context) != visited.end()) {
return; // Already done. return; // Already done.
@ -563,7 +563,7 @@ std::vector<std::wstring> PredictionContext::toStrings(Recognizer *recognizer, i
return toStrings(recognizer, EMPTY, currentState); return toStrings(recognizer, EMPTY, currentState);
} }
std::vector<std::wstring> PredictionContext::toStrings(Recognizer *recognizer, PredictionContextRef stop, int currentState) { std::vector<std::wstring> PredictionContext::toStrings(Recognizer *recognizer, PredictionContext::Ref stop, int currentState) {
std::vector<std::wstring> result; std::vector<std::wstring> result;

View File

@ -41,13 +41,16 @@ namespace v4 {
namespace runtime { namespace runtime {
namespace atn { namespace atn {
typedef std::set<PredictionContextRef> PredictionContextCache; // Cannot use PredictionContext::Ref here as this declared below first.
typedef std::set<std::shared_ptr<PredictionContext>> PredictionContextCache;
// For the keys we use raw pointers, as we don't need to access them. // For the keys we use raw pointers, as we don't need to access them.
typedef std::map<std::pair<PredictionContext *, PredictionContext *>, PredictionContextRef> PredictionContextMergeCache; typedef std::map<std::pair<PredictionContext *, PredictionContext *>, std::shared_ptr<PredictionContext>> PredictionContextMergeCache;
class PredictionContext { class PredictionContext {
public: public:
typedef std::shared_ptr<PredictionContext> Ref;
/// Represents $ in local context prediction, which means wildcard. /// Represents $ in local context prediction, which means wildcard.
/// *+x = *. /// *+x = *.
static const std::shared_ptr<EmptyPredictionContext> EMPTY; static const std::shared_ptr<EmptyPredictionContext> EMPTY;
@ -93,7 +96,7 @@ namespace atn {
public: public:
/// Convert a RuleContext tree to a PredictionContext graph. /// Convert a RuleContext tree to a PredictionContext graph.
/// Return EMPTY if outerContext is empty. /// Return EMPTY if outerContext is empty.
static PredictionContextRef fromRuleContext(const ATN &atn,RuleContextRef outerContext); static PredictionContext::Ref fromRuleContext(const ATN &atn, RuleContext::Ref outerContext);
virtual size_t size() const = 0; virtual size_t size() const = 0;
virtual std::weak_ptr<PredictionContext> getParent(size_t index) const = 0; virtual std::weak_ptr<PredictionContext> getParent(size_t index) const = 0;
@ -114,8 +117,8 @@ namespace atn {
public: public:
// dispatch // dispatch
static PredictionContextRef merge(PredictionContextRef a, static PredictionContext::Ref merge(PredictionContext::Ref a,
PredictionContextRef b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache); PredictionContext::Ref b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache);
/// <summary> /// <summary>
/// Merge two <seealso cref="SingletonPredictionContext"/> instances. /// Merge two <seealso cref="SingletonPredictionContext"/> instances.
@ -151,8 +154,8 @@ namespace atn {
/// <param name="rootIsWildcard"> {@code true} if this is a local-context merge, /// <param name="rootIsWildcard"> {@code true} if this is a local-context merge,
/// otherwise false to indicate a full-context merge </param> /// otherwise false to indicate a full-context merge </param>
/// <param name="mergeCache"> </param> /// <param name="mergeCache"> </param>
static PredictionContextRef mergeSingletons(SingletonPredictionContextRef a, SingletonPredictionContextRef b, static PredictionContext::Ref mergeSingletons(std::shared_ptr<SingletonPredictionContext> a,
bool rootIsWildcard, PredictionContextMergeCache *mergeCache); std::shared_ptr<SingletonPredictionContext> b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache);
/// <summary> /// <summary>
/// Handle case where at least one of {@code a} or {@code b} is /// Handle case where at least one of {@code a} or {@code b} is
@ -203,8 +206,8 @@ namespace atn {
/// <param name="b"> the second <seealso cref="SingletonPredictionContext"/> </param> /// <param name="b"> the second <seealso cref="SingletonPredictionContext"/> </param>
/// <param name="rootIsWildcard"> {@code true} if this is a local-context merge, /// <param name="rootIsWildcard"> {@code true} if this is a local-context merge,
/// otherwise false to indicate a full-context merge </param> /// otherwise false to indicate a full-context merge </param>
static PredictionContextRef mergeRoot(SingletonPredictionContextRef a, SingletonPredictionContextRef b, static PredictionContext::Ref mergeRoot(std::shared_ptr<SingletonPredictionContext> a,
bool rootIsWildcard); std::shared_ptr<SingletonPredictionContext> b, bool rootIsWildcard);
/// <summary> /// <summary>
/// Merge two <seealso cref="ArrayPredictionContext"/> instances. /// Merge two <seealso cref="ArrayPredictionContext"/> instances.
@ -235,7 +238,7 @@ namespace atn {
/// <seealso cref="SingletonPredictionContext"/>.<br/> /// <seealso cref="SingletonPredictionContext"/>.<br/>
/// <embed src="images/ArrayMerge_EqualTop.svg" type="image/svg+xml"/> /// <embed src="images/ArrayMerge_EqualTop.svg" type="image/svg+xml"/>
/// </summary> /// </summary>
static PredictionContextRef mergeArrays(std::shared_ptr<ArrayPredictionContext> a, static PredictionContext::Ref mergeArrays(std::shared_ptr<ArrayPredictionContext> a,
std::shared_ptr<ArrayPredictionContext> b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache); std::shared_ptr<ArrayPredictionContext> b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache);
protected: protected:
@ -244,22 +247,22 @@ namespace atn {
static bool combineCommonParents(std::vector<std::weak_ptr<PredictionContext>> &parents); static bool combineCommonParents(std::vector<std::weak_ptr<PredictionContext>> &parents);
public: public:
static std::wstring toDOTString(PredictionContextRef context); static std::wstring toDOTString(PredictionContext::Ref context);
static PredictionContextRef getCachedContext(PredictionContextRef context, static PredictionContext::Ref getCachedContext(PredictionContext::Ref context,
std::shared_ptr<PredictionContextCache> contextCache, std::shared_ptr<PredictionContextCache> contextCache,
std::map<PredictionContextRef, PredictionContextRef> &visited); std::map<PredictionContext::Ref, PredictionContext::Ref> &visited);
// ter's recursive version of Sam's getAllNodes() // ter's recursive version of Sam's getAllNodes()
static std::vector<PredictionContextRef> getAllContextNodes(PredictionContextRef context); static std::vector<PredictionContext::Ref> getAllContextNodes(PredictionContext::Ref context);
static void getAllContextNodes_(PredictionContextRef context, static void getAllContextNodes_(PredictionContext::Ref context,
std::vector<PredictionContextRef> &nodes, std::map<PredictionContextRef, PredictionContextRef> &visited); std::vector<PredictionContext::Ref> &nodes, std::map<PredictionContext::Ref, PredictionContext::Ref> &visited);
virtual std::wstring toString() const; virtual std::wstring toString() const;
virtual std::wstring toString(Recognizer *recog) const; virtual std::wstring toString(Recognizer *recog) const;
std::vector<std::wstring> toStrings(Recognizer *recognizer, int currentState); std::vector<std::wstring> toStrings(Recognizer *recognizer, int currentState);
std::vector<std::wstring> toStrings(Recognizer *recognizer, PredictionContextRef stop, int currentState); std::vector<std::wstring> toStrings(Recognizer *recognizer, PredictionContext::Ref stop, int currentState);
}; };
} // namespace atn } // namespace atn

View File

@ -52,5 +52,6 @@ bool RangeTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol
} }
std::wstring RangeTransition::toString() const { std::wstring RangeTransition::toString() const {
return std::wstring(L"'") + static_cast<wchar_t>(from) + std::wstring(L"'..'") + static_cast<wchar_t>(to) + std::wstring(L"'"); return L"RANGE " + Transition::toString() + L" { from: " + std::to_wstring(from) + L", to: " + std::to_wstring(to) +
L" }";
} }

View File

@ -51,7 +51,7 @@ namespace atn {
virtual misc::IntervalSet label() const override; virtual misc::IntervalSet label() const override;
virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override;
virtual std::wstring toString() const; virtual std::wstring toString() const override;
}; };
} // namespace atn } // namespace atn

View File

@ -29,15 +29,17 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "RuleStartState.h"
#include "RuleTransition.h" #include "RuleTransition.h"
using namespace org::antlr::v4::runtime::atn; using namespace org::antlr::v4::runtime::atn;
// this(ruleStart, ruleIndex, 0, followState); RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, ATNState *followState)
RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, ATNState *followState) : Transition(followState), ruleIndex(ruleIndex), precedence(0) { : RuleTransition(ruleStart, ruleIndex, 0, followState) {
} }
RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, int precedence, ATNState *followState) : Transition(followState), ruleIndex(ruleIndex), precedence(precedence) { RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, int precedence, ATNState *followState)
: Transition(ruleStart), ruleIndex(ruleIndex), precedence(precedence) {
this->followState = followState; this->followState = followState;
} }
@ -52,3 +54,10 @@ bool RuleTransition::isEpsilon() const {
bool RuleTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const { bool RuleTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const {
return false; return false;
} }
std::wstring RuleTransition::toString() const {
std::wstringstream ss;
ss << "RULE " << Transition::toString() << " { ruleIndex: " << ruleIndex << ", precedence: " << precedence <<
", followState: " << std::hex << followState << " }";
return ss.str();
}

Some files were not shown because too many files have changed in this diff Show More