diff --git a/.gitignore b/.gitignore index 31478d6f7..189e07153 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,4 @@ runtime-testsuite/target tool-testsuite/target runtime/Cpp/demo/generated runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/xcuserdata +runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/project.xcworkspace/xcuserdata diff --git a/runtime/Cpp/demo/Mac/antlr4-cpp-demo/main.cpp b/runtime/Cpp/demo/Mac/antlr4-cpp-demo/main.cpp index 0cd66d1d0..2af0951e7 100644 --- a/runtime/Cpp/demo/Mac/antlr4-cpp-demo/main.cpp +++ b/runtime/Cpp/demo/Mac/antlr4-cpp-demo/main.cpp @@ -12,39 +12,18 @@ #include "TLexer.h" #include "TParser.h" -#include "TestLexer.h" -#include "TestParser.h" - using namespace antlrcpptest; using namespace org::antlr::v4::runtime; int main(int argc, const char * argv[]) { - - ANTLRInputStream input(L"a"); + ANTLRInputStream input(L"divide and conquer"); TLexer lexer(&input); CommonTokenStream tokens(&lexer); TParser parser(&tokens); std::shared_ptr 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 = parser.main(); - - for (auto token : tokens.getTokens()) { - std::wcout << token->toString() << std::endl; - } - std::wcout << tree->toStringTree(&parser) << std::endl; -*/ + return 0; } diff --git a/runtime/Cpp/demo/Mac/antlrcpp Tests/antlrcpp_Tests.mm b/runtime/Cpp/demo/Mac/antlrcpp Tests/antlrcpp_Tests.mm index 1b3e3274a..da02b6b17 100644 --- a/runtime/Cpp/demo/Mac/antlrcpp Tests/antlrcpp_Tests.mm +++ b/runtime/Cpp/demo/Mac/antlrcpp Tests/antlrcpp_Tests.mm @@ -56,7 +56,7 @@ using namespace org::antlr::v4::runtime; - (void)testExample { try { - std::vector decisionToDFA; + std::vector decisionToDFA; atn::ATN atn; org::antlr::v4::runtime::atn::ParserATNSimulator foo(nullptr, atn, decisionToDFA, nullptr); diff --git a/runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/project.pbxproj b/runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/project.pbxproj index 0780a2cd0..c00b7ca3c 100644 --- a/runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/project.pbxproj +++ b/runtime/Cpp/demo/Mac/antlrcpp.xcodeproj/project.pbxproj @@ -21,8 +21,8 @@ 276927251C9ED49100E4EBF8 /* antlrcpp-Prefix.h in Headers */ = {isa = PBXBuildFile; fileRef = 276927231C9ED49100E4EBF8 /* antlrcpp-Prefix.h */; }; 278A66FB1C95838E002D667E /* 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 */; }; - 27BC755E1CB41B0D00AE780B /* TestParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27BC755B1CB41B0D00AE780B /* TestParser.cpp */; }; + 27A23EA31CC2A8D60036D8A3 /* TLexer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27A23EA11CC2A8D60036D8A3 /* TLexer.cpp */; }; + 27A23EA41CC2A8D60036D8A3 /* TLexer.h in Headers */ = {isa = PBXBuildFile; fileRef = 27A23EA21CC2A8D60036D8A3 /* TLexer.h */; }; 27C666AC1C9584050021E494 /* 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 */; }; @@ -501,7 +501,6 @@ 27C66A1F1C958AB30021E494 /* TokenTagToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 27C669FF1C958AB30021E494 /* TokenTagToken.h */; }; 27C66A6A1C9591280021E494 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C66A691C9591280021E494 /* main.cpp */; }; 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 */; }; 27C6E1811C972FFC0079AF06 /* TParserBaseListener.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C6E1771C972FFC0079AF06 /* TParserBaseListener.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 = ""; wrapsLines = 0; }; 276927231C9ED49100E4EBF8 /* antlrcpp-Prefix.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; fileEncoding = 4; name = "antlrcpp-Prefix.h"; path = "../../runtime/antlrcpp-Prefix.h"; sourceTree = ""; }; 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 = ""; }; - 27BC755A1CB41B0D00AE780B /* TestLexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestLexer.h; path = ../generated/TestLexer.h; sourceTree = ""; }; - 27BC755B1CB41B0D00AE780B /* TestParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TestParser.cpp; path = ../generated/TestParser.cpp; sourceTree = ""; }; - 27BC755C1CB41B0D00AE780B /* TestParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestParser.h; path = ../generated/TestParser.h; sourceTree = ""; }; + 27A23EA11CC2A8D60036D8A3 /* TLexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TLexer.cpp; path = ../generated/TLexer.cpp; sourceTree = ""; }; + 27A23EA21CC2A8D60036D8A3 /* TLexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TLexer.h; path = ../generated/TLexer.h; sourceTree = ""; }; 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; }; 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; }; 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; }; - 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; }; 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; }; @@ -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; }; 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; }; - 27C666861C9584050021E494 /* LexerInterpreter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LexerInterpreter.cpp; path = ../../runtime/LexerInterpreter.cpp; sourceTree = SOURCE_ROOT; }; - 27C666871C9584050021E494 /* LexerInterpreter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = LexerInterpreter.h; path = ../../runtime/LexerInterpreter.h; sourceTree = SOURCE_ROOT; }; + 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; 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; }; 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 = ""; }; 27C667821C95846E0021E494 /* PrecedencePredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PrecedencePredicateTransition.cpp; sourceTree = ""; wrapsLines = 0; }; 27C667831C95846E0021E494 /* PrecedencePredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrecedencePredicateTransition.h; sourceTree = ""; }; - 27C667841C95846E0021E494 /* PredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredicateTransition.cpp; sourceTree = ""; }; + 27C667841C95846E0021E494 /* PredicateTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredicateTransition.cpp; sourceTree = ""; wrapsLines = 0; }; 27C667851C95846E0021E494 /* PredicateTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredicateTransition.h; sourceTree = ""; }; 27C667861C95846E0021E494 /* PredictionContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PredictionContext.cpp; sourceTree = ""; wrapsLines = 0; }; 27C667871C95846E0021E494 /* PredictionContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PredictionContext.h; sourceTree = ""; wrapsLines = 0; }; @@ -701,7 +698,7 @@ 27C6678F1C95846E0021E494 /* RuleStartState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleStartState.h; sourceTree = ""; }; 27C667901C95846E0021E494 /* RuleStopState.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleStopState.cpp; sourceTree = ""; }; 27C667911C95846E0021E494 /* RuleStopState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleStopState.h; sourceTree = ""; }; - 27C667921C95846E0021E494 /* RuleTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleTransition.cpp; sourceTree = ""; }; + 27C667921C95846E0021E494 /* RuleTransition.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RuleTransition.cpp; sourceTree = ""; wrapsLines = 0; }; 27C667931C95846E0021E494 /* RuleTransition.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RuleTransition.h; sourceTree = ""; }; 27C667941C95846E0021E494 /* SemanticContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SemanticContext.cpp; sourceTree = ""; wrapsLines = 0; }; 27C667951C95846E0021E494 /* SemanticContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SemanticContext.h; sourceTree = ""; }; @@ -795,8 +792,6 @@ 27C66A691C9591280021E494 /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = ""; }; 27C66A731C9592400021E494 /* TLexer.g4 */ = {isa = PBXFileReference; lastKnownFileType = text; name = TLexer.g4; path = ../../TLexer.g4; sourceTree = ""; }; 27C66A741C9592400021E494 /* TParser.g4 */ = {isa = PBXFileReference; lastKnownFileType = text; name = TParser.g4; path = ../../TParser.g4; sourceTree = ""; }; - 27C6E1711C972FFB0079AF06 /* TLexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TLexer.cpp; path = ../generated/TLexer.cpp; sourceTree = ""; }; - 27C6E1721C972FFC0079AF06 /* TLexer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TLexer.h; path = ../generated/TLexer.h; sourceTree = ""; }; 27C6E1741C972FFC0079AF06 /* TParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TParser.cpp; path = ../generated/TParser.cpp; sourceTree = ""; wrapsLines = 0; }; 27C6E1751C972FFC0079AF06 /* TParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TParser.h; path = ../generated/TParser.h; sourceTree = ""; wrapsLines = 0; }; 27C6E1771C972FFC0079AF06 /* TParserBaseListener.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = TParserBaseListener.cpp; path = ../generated/TParserBaseListener.cpp; sourceTree = ""; }; @@ -848,36 +843,6 @@ /* End PBXFrameworksBuildPhase 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 = ""; - }; - 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 = ""; - }; 27C667461C9584570021E494 /* atn */ = { isa = PBXGroup; children = ( @@ -1088,8 +1053,18 @@ 27C66A5C1C958EB50021E494 /* generated */ = { isa = PBXGroup; children = ( - 27BC75601CB41B2400AE780B /* Advanced */, - 27BC755F1CB41B1100AE780B /* Simple */, + 27A23EA11CC2A8D60036D8A3 /* TLexer.cpp */, + 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; sourceTree = ""; @@ -1425,6 +1400,7 @@ 27C667C41C95846E0021E494 /* ATNDeserializer.h in Headers */, 2747A70C1CA691310030247B /* ConfigLookup.h in Headers */, 27C668731C9584B60021E494 /* DFASerializer.h in Headers */, + 27A23EA41CC2A8D60036D8A3 /* TLexer.h in Headers */, 27C667361C9584050021E494 /* TokenStream.h in Headers */, 27C666E41C9584050021E494 /* Exceptions.h in Headers */, 27C667321C9584050021E494 /* TokenSource.h in Headers */, @@ -1656,9 +1632,6 @@ 27C6E1811C972FFC0079AF06 /* TParserBaseListener.cpp in Sources */, 27C6E1841C972FFC0079AF06 /* TParserVisitor.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; }; @@ -1868,6 +1841,7 @@ 27C667021C9584050021E494 /* LexerNoViableAltException.cpp in Sources */, 27C667EE1C95846E0021E494 /* EmptyPredictionContext.cpp in Sources */, 27C666C61C9584050021E494 /* CharStream.cpp in Sources */, + 27A23EA31CC2A8D60036D8A3 /* TLexer.cpp in Sources */, 27C667CE1C95846E0021E494 /* ATNState.cpp in Sources */, 27C666BE1C9584050021E494 /* BaseErrorListener.cpp in Sources */, 27C6671E1C9584050021E494 /* RecognitionException.cpp in Sources */, @@ -2032,7 +2006,6 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; @@ -2073,7 +2046,6 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; - CLANG_WARN_IMPLICIT_SIGN_CONVERSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; diff --git a/runtime/Cpp/demo/TLexer.g4 b/runtime/Cpp/demo/TLexer.g4 index e3b60166d..ed8558d84 100644 --- a/runtime/Cpp/demo/TLexer.g4 +++ b/runtime/Cpp/demo/TLexer.g4 @@ -36,10 +36,10 @@ INT: Digit+; Digit: [0..9]; ID: LETTER (LETTER | '0'..'9')*; -fragment LETTER : [a-zA-Z\u0080-\u00FF_] ; +fragment LETTER : [a-zA-Z\u0080-\uFFFD] ; LessThan: '<' -> pushMode(Mode1); -GreaterThan: '>' -> popMode; +GreaterThan: '>';// -> popMode; Equal: '='; And: 'and'; @@ -70,663 +70,3 @@ Dot: '.'; mode Mode2; 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'; -*/ \ No newline at end of file diff --git a/runtime/Cpp/demo/TParser.g4 b/runtime/Cpp/demo/TParser.g4 index 1220f3eb8..b132f95cc 100644 --- a/runtime/Cpp/demo/TParser.g4 +++ b/runtime/Cpp/demo/TParser.g4 @@ -56,6 +56,7 @@ conquer: // 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(); } : + stat ; catch [...] { // Replaces the standard exception handling. diff --git a/runtime/Cpp/demo/Test.g4 b/runtime/Cpp/demo/Test.g4 deleted file mode 100644 index c0ee04751..000000000 --- a/runtime/Cpp/demo/Test.g4 +++ /dev/null @@ -1,5 +0,0 @@ -grammar Test; - -main: ID ID; - -ID: [a-z]+; diff --git a/runtime/Cpp/demo/generate.sh b/runtime/Cpp/demo/generate.sh index 5ec0f8035..359568b36 100755 --- a/runtime/Cpp/demo/generate.sh +++ b/runtime/Cpp/demo/generate.sh @@ -26,11 +26,6 @@ set -o errexit # 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 -# 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 -XdbgST TLexer.g4 TParser.g4 #java -cp $CLASSPATH org.antlr.v4.Tool -Dlanguage=Java -listener -visitor -o generated/ TLexer.g4 TParser.g4 diff --git a/runtime/Cpp/runtime/ANTLRErrorListener.h b/runtime/Cpp/runtime/ANTLRErrorListener.h index 1d923b67a..f3cbe3a2a 100755 --- a/runtime/Cpp/runtime/ANTLRErrorListener.h +++ b/runtime/Cpp/runtime/ANTLRErrorListener.h @@ -77,7 +77,7 @@ namespace runtime { /// the reporting of an error. It is null in the case where /// the parser was able to recover in line without exiting the /// surrounding rule. - 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; /// @@ -107,7 +107,7 @@ namespace runtime { /// the potentially ambiguous alternatives /// the ATN configuration set where the ambiguity was /// determined - 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 configs) = 0; /// @@ -130,7 +130,7 @@ namespace runtime { /// represented in {@code configs}. /// the ATN configuration set where the SLL conflict was /// detected - 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 configs) = 0; /// @@ -160,7 +160,7 @@ namespace runtime { /// the unambiguous result of the full-context prediction /// the ATN configuration set where the unambiguous prediction /// was determined - 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 configs) = 0; }; diff --git a/runtime/Cpp/runtime/ANTLRErrorStrategy.h b/runtime/Cpp/runtime/ANTLRErrorStrategy.h index 50f741388..4c1c736da 100755 --- a/runtime/Cpp/runtime/ANTLRErrorStrategy.h +++ b/runtime/Cpp/runtime/ANTLRErrorStrategy.h @@ -31,6 +31,8 @@ #pragma once +#include "Token.h" + namespace org { namespace antlr { namespace v4 { @@ -77,7 +79,7 @@ namespace runtime { /// the parser instance /// if the error strategy was not able to /// recover from the unexpected input symbol - virtual TokenRef recoverInline(Parser *recognizer) = 0; + virtual Token::Ref recoverInline(Parser *recognizer) = 0; /// /// This method is called to recover from exception {@code e}. This method is diff --git a/runtime/Cpp/runtime/ANTLRInputStream.cpp b/runtime/Cpp/runtime/ANTLRInputStream.cpp index a5381f4a0..cee3776fb 100755 --- a/runtime/Cpp/runtime/ANTLRInputStream.cpp +++ b/runtime/Cpp/runtime/ANTLRInputStream.cpp @@ -165,7 +165,7 @@ std::string ANTLRInputStream::getSourceName() const { return name; } -std::wstring ANTLRInputStream::toString() { +std::wstring ANTLRInputStream::toString() const { return data; } diff --git a/runtime/Cpp/runtime/ANTLRInputStream.h b/runtime/Cpp/runtime/ANTLRInputStream.h index 41b810580..1038cd0ff 100755 --- a/runtime/Cpp/runtime/ANTLRInputStream.h +++ b/runtime/Cpp/runtime/ANTLRInputStream.h @@ -90,7 +90,7 @@ namespace runtime { virtual void seek(size_t index) override; virtual std::wstring getText(const misc::Interval &interval) override; virtual std::string getSourceName() const override; - virtual std::wstring toString(); + virtual std::wstring toString() const override; private: void InitializeInstanceFields(); diff --git a/runtime/Cpp/runtime/BailErrorStrategy.cpp b/runtime/Cpp/runtime/BailErrorStrategy.cpp index 241c26350..9e1dee1c4 100755 --- a/runtime/Cpp/runtime/BailErrorStrategy.cpp +++ b/runtime/Cpp/runtime/BailErrorStrategy.cpp @@ -40,7 +40,7 @@ using namespace org::antlr::v4::runtime; void BailErrorStrategy::recover(Parser *recognizer, const RecognitionException &e) { std::exception_ptr exception = std::make_exception_ptr(e); - ParserRuleContextRef context = recognizer->getContext(); + ParserRuleContext::Ref context = recognizer->getContext(); do { context->exception = exception; 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); std::exception_ptr exception = std::make_exception_ptr(e); - ParserRuleContextRef context = recognizer->getContext(); + ParserRuleContext::Ref context = recognizer->getContext(); do { context->exception = exception; if (context->getParent().expired()) diff --git a/runtime/Cpp/runtime/BailErrorStrategy.h b/runtime/Cpp/runtime/BailErrorStrategy.h index abbc3646b..f1d15c9fe 100755 --- a/runtime/Cpp/runtime/BailErrorStrategy.h +++ b/runtime/Cpp/runtime/BailErrorStrategy.h @@ -57,7 +57,7 @@ namespace runtime { /// Make sure we don't attempt to recover inline; if the parser /// successfully recovers, it won't throw an exception. /// - virtual TokenRef recoverInline(Parser *recognizer) override; + virtual Token::Ref recoverInline(Parser *recognizer) override; /// /// Make sure we don't attempt to recover from problems in subrules. diff --git a/runtime/Cpp/runtime/BaseErrorListener.cpp b/runtime/Cpp/runtime/BaseErrorListener.cpp index 499e1a3f4..bc08bc641 100755 --- a/runtime/Cpp/runtime/BaseErrorListener.cpp +++ b/runtime/Cpp/runtime/BaseErrorListener.cpp @@ -34,18 +34,18 @@ 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) { } -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 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 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 configs) { } diff --git a/runtime/Cpp/runtime/BaseErrorListener.h b/runtime/Cpp/runtime/BaseErrorListener.h index cae66144c..2c5e80cbd 100755 --- a/runtime/Cpp/runtime/BaseErrorListener.h +++ b/runtime/Cpp/runtime/BaseErrorListener.h @@ -44,16 +44,16 @@ namespace runtime { 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; - 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 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 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 configs) override; }; diff --git a/runtime/Cpp/runtime/BufferedTokenStream.cpp b/runtime/Cpp/runtime/BufferedTokenStream.cpp index cb9b8c921..c74831be1 100755 --- a/runtime/Cpp/runtime/BufferedTokenStream.cpp +++ b/runtime/Cpp/runtime/BufferedTokenStream.cpp @@ -85,6 +85,8 @@ void BufferedTokenStream::consume() { } 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? if (n > 0) { @@ -101,7 +103,7 @@ size_t BufferedTokenStream::fetch(size_t n) { } for (size_t i = 0; i < n; i++) { - TokenRef t = _tokenSource->nextToken(); + Token::Ref t = _tokenSource->nextToken(); if (is(t)) { (std::dynamic_pointer_cast(t))->setTokenIndex((int)_tokens.size()); } @@ -115,7 +117,7 @@ size_t BufferedTokenStream::fetch(size_t n) { return n; } -TokenRef BufferedTokenStream::get(size_t i) const { +Token::Ref BufferedTokenStream::get(size_t i) const { if (i >= _tokens.size()) { throw IndexOutOfBoundsException(std::string("token index ") + std::to_string(i) + @@ -125,8 +127,8 @@ TokenRef BufferedTokenStream::get(size_t i) const { return _tokens[i]; } -std::vector BufferedTokenStream::get(size_t start, size_t stop) { - std::vector subset; +std::vector BufferedTokenStream::get(size_t start, size_t stop) { + std::vector subset; lazyInit(); @@ -138,7 +140,7 @@ std::vector BufferedTokenStream::get(size_t start, size_t stop) { stop = _tokens.size() - 1; } for (size_t i = start; i <= stop; i++) { - TokenRef t = _tokens[i]; + Token::Ref t = _tokens[i]; if (t->getType() == EOF) { break; } @@ -151,23 +153,23 @@ ssize_t BufferedTokenStream::LA(ssize_t i) { return LT(i)->getType(); } -TokenRef BufferedTokenStream::LB(size_t k) { +Token::Ref BufferedTokenStream::LB(size_t k) { if (k > _p) { return nullptr; } return _tokens[(size_t)(_p - k)]; } -TokenRef BufferedTokenStream::LT(ssize_t k) { +Token::Ref BufferedTokenStream::LT(ssize_t k) { lazyInit(); if (k == 0) { return nullptr; } 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); if (i >= _tokens.size()) { // return EOF token // EOF must be last token @@ -177,7 +179,7 @@ TokenRef BufferedTokenStream::LT(ssize_t k) { return _tokens[i]; } -size_t BufferedTokenStream::adjustSeekIndex(size_t i) { +ssize_t BufferedTokenStream::adjustSeekIndex(size_t i) { return i; } @@ -199,15 +201,15 @@ void BufferedTokenStream::setTokenSource(TokenSource *tokenSource) { _needSetup = true; } -std::vector BufferedTokenStream::getTokens() { +std::vector BufferedTokenStream::getTokens() { return _tokens; } -std::vector BufferedTokenStream::getTokens(int start, int stop) { +std::vector BufferedTokenStream::getTokens(int start, int stop) { return getTokens(start, stop, std::vector()); } -std::vector BufferedTokenStream::getTokens(int start, int stop, const std::vector &types) { +std::vector BufferedTokenStream::getTokens(int start, int stop, const std::vector &types) { lazyInit(); if (start < 0 || stop >= (int)_tokens.size() || stop < 0 || start >= (int)_tokens.size()) { throw IndexOutOfBoundsException(std::string("start ") + @@ -218,7 +220,7 @@ std::vector BufferedTokenStream::getTokens(int start, int stop, const std::to_string(_tokens.size() - 1)); } - std::vector filteredTokens; + std::vector filteredTokens; if (start > stop) { return filteredTokens; @@ -226,7 +228,7 @@ std::vector BufferedTokenStream::getTokens(int start, int stop, const // list = tokens[start:stop]:{T t, t.getType() in types} 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()) { filteredTokens.push_back(tok); @@ -235,7 +237,7 @@ std::vector BufferedTokenStream::getTokens(int start, int stop, const return filteredTokens; } -std::vector BufferedTokenStream::getTokens(int start, int stop, int ttype) { +std::vector BufferedTokenStream::getTokens(int start, int stop, int ttype) { std::vector s; s.push_back(ttype); return getTokens(start, stop, s); @@ -247,7 +249,7 @@ ssize_t BufferedTokenStream::nextTokenOnChannel(size_t i, int channel) { return -1; } - TokenRef token = _tokens[i]; + Token::Ref token = _tokens[i]; while (token->getChannel() != channel) { if (token->getType() == EOF) { return -1; @@ -256,13 +258,13 @@ ssize_t BufferedTokenStream::nextTokenOnChannel(size_t i, int channel) { sync(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 { - if (_tokens[i]->getChannel() == channel) - return (ssize_t)i; + if (_tokens[(size_t)i]->getChannel() == channel) + return i; if (i == 0) return -1; i--; @@ -270,7 +272,7 @@ ssize_t BufferedTokenStream::previousTokenOnChannel(size_t i, int channel) const return -1; } -std::vector BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex, int channel) { +std::vector BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex, int channel) { lazyInit(); if (tokenIndex >= _tokens.size()) { throw IndexOutOfBoundsException(std::to_string(tokenIndex) + " not in 0.." + std::to_string(_tokens.size() - 1)); @@ -289,19 +291,19 @@ std::vector BufferedTokenStream::getHiddenTokensToRight(size_t tokenIn return filterForChannel(from, (size_t)to, channel); } -std::vector BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex) { +std::vector BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex) { return getHiddenTokensToRight(tokenIndex, -1); } -std::vector BufferedTokenStream::getHiddenTokensToLeft(size_t tokenIndex, int channel) { +std::vector BufferedTokenStream::getHiddenTokensToLeft(size_t tokenIndex, int channel) { lazyInit(); if (tokenIndex >= _tokens.size()) { 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) { - return std::vector(); + return std::vector(); } // if none onchannel to left, prevOnChannel=-1 then from=0 size_t from = (size_t)(prevOnChannel + 1); @@ -310,14 +312,14 @@ std::vector BufferedTokenStream::getHiddenTokensToLeft(size_t tokenInd return filterForChannel(from, to, channel); } -std::vector BufferedTokenStream::getHiddenTokensToLeft(size_t tokenIndex) { +std::vector BufferedTokenStream::getHiddenTokensToLeft(size_t tokenIndex) { return getHiddenTokensToLeft(tokenIndex, -1); } -std::vector BufferedTokenStream::filterForChannel(size_t from, size_t to, int channel) { - std::vector hidden; +std::vector BufferedTokenStream::filterForChannel(size_t from, size_t to, int channel) { + std::vector hidden; for (size_t i = from; i <= to; i++) { - TokenRef t = _tokens[i]; + Token::Ref t = _tokens[i]; if (channel == -1) { if (t->getChannel() != Lexer::DEFAULT_TOKEN_CHANNEL) { hidden.push_back(t); @@ -359,7 +361,7 @@ std::wstring BufferedTokenStream::getText(const misc::Interval &interval) { std::wstringstream ss; 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) { break; } @@ -372,7 +374,7 @@ std::wstring BufferedTokenStream::getText(RuleContext *ctx) { 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) { return getText(misc::Interval(start->getTokenIndex(), stop->getTokenIndex())); } diff --git a/runtime/Cpp/runtime/BufferedTokenStream.h b/runtime/Cpp/runtime/BufferedTokenStream.h index 200ff6311..0917110be 100755 --- a/runtime/Cpp/runtime/BufferedTokenStream.h +++ b/runtime/Cpp/runtime/BufferedTokenStream.h @@ -63,57 +63,57 @@ namespace runtime { virtual size_t size() 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. - virtual std::vector get(size_t start, size_t stop); + virtual std::vector get(size_t start, size_t stop); 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. virtual void setTokenSource(TokenSource *tokenSource); - virtual std::vector getTokens(); - virtual std::vector getTokens(int start, int stop); + virtual std::vector getTokens(); + virtual std::vector getTokens(int start, int stop); /// /// 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 /// method looks at both on and off channel tokens. /// - virtual std::vector getTokens(int start, int stop, const std::vector &types); - virtual std::vector getTokens(int start, int stop, int ttype); + virtual std::vector getTokens(int start, int stop, const std::vector &types); + virtual std::vector getTokens(int start, int stop, int ttype); /// Collect all tokens on specified channel to the right of /// 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. - virtual std::vector getHiddenTokensToRight(size_t tokenIndex, int channel); + virtual std::vector getHiddenTokensToRight(size_t tokenIndex, int channel); /// /// 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 /// of EOF. /// - virtual std::vector getHiddenTokensToRight(size_t tokenIndex); + virtual std::vector getHiddenTokensToRight(size_t tokenIndex); /// /// Collect all tokens on specified channel to the left of /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. /// If channel is -1, find any non default channel token. /// - virtual std::vector getHiddenTokensToLeft(size_t tokenIndex, int channel); + virtual std::vector getHiddenTokensToLeft(size_t tokenIndex, int channel); /// /// 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. /// - virtual std::vector getHiddenTokensToLeft(size_t tokenIndex); + virtual std::vector getHiddenTokensToLeft(size_t tokenIndex); virtual std::string getSourceName() const override; virtual std::wstring getText() override; virtual std::wstring getText(const misc::Interval &interval) 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; /// /// Get all tokens from lexer until EOF @@ -126,7 +126,7 @@ namespace runtime { /// chunks of it later. This list captures everything so we can access /// complete input text. // ml: we own the tokens produced by the token factory. - std::vector _tokens; + std::vector _tokens; /// /// The index into of the current token (next token to @@ -160,7 +160,7 @@ namespace runtime { /// The actual number of elements added to the buffer. 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 /// the current stream position by adjusting the target token index of a seek @@ -173,7 +173,7 @@ namespace runtime { /// /// The target token index. /// The adjusted target token index. - virtual size_t adjustSeekIndex(size_t i); + virtual ssize_t adjustSeekIndex(size_t i); void lazyInit(); virtual void setup(); @@ -185,9 +185,9 @@ namespace runtime { /// 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 /// 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 filterForChannel(size_t from, size_t to, int channel); + virtual std::vector filterForChannel(size_t from, size_t to, int channel); private: bool _needSetup; void InitializeInstanceFields(); diff --git a/runtime/Cpp/runtime/CharStream.h b/runtime/Cpp/runtime/CharStream.h index b897f957b..a18fde313 100755 --- a/runtime/Cpp/runtime/CharStream.h +++ b/runtime/Cpp/runtime/CharStream.h @@ -58,6 +58,8 @@ namespace runtime { /// if the stream does not support /// getting the text of the specified interval virtual std::wstring getText(const misc::Interval &interval) = 0; + + virtual std::wstring toString() const = 0; }; } // namespace runtime diff --git a/runtime/Cpp/runtime/CommonTokenStream.cpp b/runtime/Cpp/runtime/CommonTokenStream.cpp index 3c033f46a..c963d4362 100755 --- a/runtime/Cpp/runtime/CommonTokenStream.cpp +++ b/runtime/Cpp/runtime/CommonTokenStream.cpp @@ -43,29 +43,31 @@ CommonTokenStream::CommonTokenStream(TokenSource *tokenSource, int channel) : Bu this->channel = channel; } -size_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 (size_t)nextTokenOnChannel(i, channel); +ssize_t CommonTokenStream::adjustSeekIndex(size_t i) { + return nextTokenOnChannel(i, channel); } -TokenRef CommonTokenStream::LB(size_t k) { +Token::Ref CommonTokenStream::LB(size_t k) { if (k == 0 || k > _p) { - return TokenRef(); + return Token::Ref(); } - size_t i = _p; + ssize_t i = (ssize_t)_p; size_t n = 1; // find k good tokens looking backwards while (n <= k) { // skip off-channel tokens - // XXX ml: also here, no error handling for -1 - i = (size_t)previousTokenOnChannel(i - 1, channel); + i = previousTokenOnChannel(i - 1, channel); n++; } + if (i < 0) { + return nullptr; + } + return _tokens[i]; } -TokenRef CommonTokenStream::LT(ssize_t k) { +Token::Ref CommonTokenStream::LT(ssize_t k) { lazyInit(); if (k == 0) { return nullptr; @@ -74,13 +76,12 @@ TokenRef CommonTokenStream::LT(ssize_t k) { return LB((size_t)-k); } size_t i = _p; - size_t n = 1; // we know tokens[p] is a good one - // find k good tokens - while (n < (size_t)k) { + ssize_t n = 1; // we know tokens[p] is a good one + // find k good tokens + while (n < k) { // skip off-channel tokens, but make sure to not look past EOF if (sync(i + 1)) { - // XXX ml: no error handling either - i = (size_t)nextTokenOnChannel(i + 1, channel); + i = nextTokenOnChannel(i + 1, channel); } n++; } @@ -92,7 +93,7 @@ int CommonTokenStream::getNumberOfOnChannelTokens() { int n = 0; fill(); for (size_t i = 0; i < _tokens.size(); i++) { - TokenRef t = _tokens[i]; + Token::Ref t = _tokens[i]; if (t->getChannel() == channel) { n++; } diff --git a/runtime/Cpp/runtime/CommonTokenStream.h b/runtime/Cpp/runtime/CommonTokenStream.h index c8622d946..7095745b9 100755 --- a/runtime/Cpp/runtime/CommonTokenStream.h +++ b/runtime/Cpp/runtime/CommonTokenStream.h @@ -66,12 +66,12 @@ namespace runtime { CommonTokenStream(TokenSource *tokenSource, int channel); 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: - virtual TokenRef LT(ssize_t k) override; + virtual Token::Ref LT(ssize_t k) override; /// Count EOF just once. virtual int getNumberOfOnChannelTokens(); diff --git a/runtime/Cpp/runtime/ConsoleErrorListener.cpp b/runtime/Cpp/runtime/ConsoleErrorListener.cpp index 43b97c87e..702beb8b2 100755 --- a/runtime/Cpp/runtime/ConsoleErrorListener.cpp +++ b/runtime/Cpp/runtime/ConsoleErrorListener.cpp @@ -33,4 +33,9 @@ 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; +} diff --git a/runtime/Cpp/runtime/ConsoleErrorListener.h b/runtime/Cpp/runtime/ConsoleErrorListener.h index 06994271c..c3f530c8c 100755 --- a/runtime/Cpp/runtime/ConsoleErrorListener.h +++ b/runtime/Cpp/runtime/ConsoleErrorListener.h @@ -40,13 +40,9 @@ namespace runtime { class ConsoleErrorListener : public BaseErrorListener { public: - static ConsoleErrorListener *const INSTANCE; + static ConsoleErrorListener INSTANCE; - void syntaxError(IRecognizer *recognizer, void *offendingSymbol, - int line, int charPositionInLine, const std::wstring &msg, - RecognitionException *e) { - std::wcerr << L"line " << line << L":" << charPositionInLine << L" " << msg; - } + virtual void syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol, size_t line, int charPositionInLine, const std::wstring &msg, std::exception_ptr e) override; }; } // namespace runtime diff --git a/runtime/Cpp/runtime/DefaultErrorStrategy.cpp b/runtime/Cpp/runtime/DefaultErrorStrategy.cpp index 15c7caf82..2ee0d410b 100755 --- a/runtime/Cpp/runtime/DefaultErrorStrategy.cpp +++ b/runtime/Cpp/runtime/DefaultErrorStrategy.cpp @@ -195,7 +195,7 @@ void DefaultErrorStrategy::reportUnwantedToken(Parser *recognizer) { beginErrorCondition(recognizer); - TokenRef t = recognizer->getCurrentToken(); + Token::Ref t = recognizer->getCurrentToken(); std::wstring tokenName = getTokenErrorDisplay(t); misc::IntervalSet expecting = getExpectedTokens(recognizer); @@ -210,16 +210,16 @@ void DefaultErrorStrategy::reportMissingToken(Parser *recognizer) { beginErrorCondition(recognizer); - TokenRef t = recognizer->getCurrentToken(); + Token::Ref t = recognizer->getCurrentToken(); misc::IntervalSet expecting = getExpectedTokens(recognizer); std::wstring msg = std::wstring(L"missing ") + expecting.toString(recognizer->getTokenNames()) + std::wstring(L" at ") + getTokenErrorDisplay(t); recognizer->notifyErrorListeners(t, msg, nullptr); } -TokenRef DefaultErrorStrategy::recoverInline(Parser *recognizer) { +Token::Ref DefaultErrorStrategy::recoverInline(Parser *recognizer) { // SINGLE TOKEN DELETION - TokenRef matchedSymbol = singleTokenDeletion(recognizer); + Token::Ref matchedSymbol = singleTokenDeletion(recognizer); if (matchedSymbol) { // we have deleted the extra token. // now, move past ttype token as if all were ok @@ -253,22 +253,22 @@ bool DefaultErrorStrategy::singleTokenInsertion(Parser *recognizer) { return false; } -TokenRef DefaultErrorStrategy::singleTokenDeletion(Parser *recognizer) { +Token::Ref DefaultErrorStrategy::singleTokenDeletion(Parser *recognizer) { ssize_t nextTokenType = recognizer->getInputStream()->LA(2); misc::IntervalSet expecting = getExpectedTokens(recognizer); if (expecting.contains((int)nextTokenType)) { reportUnwantedToken(recognizer); recognizer->consume(); // simply delete extra token // 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 return matchedSymbol; } return nullptr; } -TokenRef DefaultErrorStrategy::getMissingSymbol(Parser *recognizer) { - TokenRef currentSymbol = recognizer->getCurrentToken(); +Token::Ref DefaultErrorStrategy::getMissingSymbol(Parser *recognizer) { + Token::Ref currentSymbol = recognizer->getCurrentToken(); misc::IntervalSet expecting = getExpectedTokens(recognizer); ssize_t expectedTokenType = expecting.getMinElement(); // get any element std::wstring tokenText; @@ -277,8 +277,8 @@ TokenRef DefaultErrorStrategy::getMissingSymbol(Parser *recognizer) { } else { tokenText = std::wstring(L"getTokenNames()[(size_t)expectedTokenType] + std::wstring(L">"); } - TokenRef current = currentSymbol; - TokenRef lookback = recognizer->getTokenStream()->LT(-1); + Token::Ref current = currentSymbol; + Token::Ref lookback = recognizer->getTokenStream()->LT(-1); if (current->getType() == EOF && lookback != nullptr) { current = lookback; } @@ -291,7 +291,7 @@ misc::IntervalSet DefaultErrorStrategy::getExpectedTokens(Parser *recognizer) { return recognizer->getExpectedTokens(); } -std::wstring DefaultErrorStrategy::getTokenErrorDisplay(TokenRef t) { +std::wstring DefaultErrorStrategy::getTokenErrorDisplay(Token::Ref t) { if (t == nullptr) { return L""; } @@ -306,11 +306,11 @@ std::wstring DefaultErrorStrategy::getTokenErrorDisplay(TokenRef t) { return escapeWSAndQuote(s); } -std::wstring DefaultErrorStrategy::getSymbolText(TokenRef symbol) { +std::wstring DefaultErrorStrategy::getSymbolText(Token::Ref symbol) { return symbol->getText(); } -int DefaultErrorStrategy::getSymbolType(TokenRef symbol) { +int DefaultErrorStrategy::getSymbolType(Token::Ref symbol) { return symbol->getType(); } @@ -324,7 +324,7 @@ std::wstring DefaultErrorStrategy::escapeWSAndQuote(std::wstring &s) { misc::IntervalSet DefaultErrorStrategy::getErrorRecoverySet(Parser *recognizer) { const atn::ATN &atn = recognizer->getInterpreter()->atn; - RuleContextRef ctx = recognizer->getContext(); + RuleContext::Ref ctx = recognizer->getContext(); misc::IntervalSet recoverSet; while (ctx->invokingState >= 0) { // compute what follows who invoked us diff --git a/runtime/Cpp/runtime/DefaultErrorStrategy.h b/runtime/Cpp/runtime/DefaultErrorStrategy.h index bf405b85e..0494b405b 100755 --- a/runtime/Cpp/runtime/DefaultErrorStrategy.h +++ b/runtime/Cpp/runtime/DefaultErrorStrategy.h @@ -305,7 +305,7 @@ namespace runtime { /// in rule {@code atom}. It can assume that you forgot the {@code ')'}. /// public: - virtual TokenRef recoverInline(Parser *recognizer) override; + virtual Token::Ref recoverInline(Parser *recognizer) override; /// /// This method implements the single-token insertion inline error recovery @@ -344,7 +344,7 @@ namespace runtime { /// the successfully matched instance if single-token /// deletion successfully recovers from the mismatched input, otherwise /// {@code null} - virtual TokenRef singleTokenDeletion(Parser *recognizer); + virtual Token::Ref singleTokenDeletion(Parser *recognizer); /// /// 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, /// override this method to create the appropriate tokens. /// - virtual TokenRef getMissingSymbol(Parser *recognizer); + virtual Token::Ref getMissingSymbol(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 /// so that it creates a new Java type. /// - 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); diff --git a/runtime/Cpp/runtime/DiagnosticErrorListener.cpp b/runtime/Cpp/runtime/DiagnosticErrorListener.cpp index d8de5d993..72878e8fa 100755 --- a/runtime/Cpp/runtime/DiagnosticErrorListener.cpp +++ b/runtime/Cpp/runtime/DiagnosticErrorListener.cpp @@ -46,7 +46,7 @@ DiagnosticErrorListener::DiagnosticErrorListener() : exactOnly(true) { 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 configs) { if (exactOnly && !exact) { return; @@ -61,7 +61,7 @@ void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, 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 configs) { std::wstring decision = getDecisionDescription(recognizer, dfa); 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); } -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 configs) { std::wstring decision = getDecisionDescription(recognizer, dfa); 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); } -std::wstring DiagnosticErrorListener::getDecisionDescription(Parser *recognizer, dfa::DFA *dfa) { - int decision = dfa->decision; - int ruleIndex = ((atn::ATNState*)dfa->atnStartState)->ruleIndex; +std::wstring DiagnosticErrorListener::getDecisionDescription(Parser *recognizer, const dfa::DFA &dfa) { + int decision = dfa.decision; + int ruleIndex = ((atn::ATNState*)dfa.atnStartState)->ruleIndex; const std::vector& ruleNames = recognizer->getRuleNames(); if (ruleIndex < 0 || ruleIndex >= (int)ruleNames.size()) { diff --git a/runtime/Cpp/runtime/DiagnosticErrorListener.h b/runtime/Cpp/runtime/DiagnosticErrorListener.h index 049995e5e..eee646d00 100755 --- a/runtime/Cpp/runtime/DiagnosticErrorListener.h +++ b/runtime/Cpp/runtime/DiagnosticErrorListener.h @@ -81,17 +81,17 @@ namespace runtime { /// {@code false} to report all ambiguities. 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 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 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 configs) override; protected: - virtual std::wstring getDecisionDescription(Parser *recognizer, dfa::DFA *dfa); + virtual std::wstring getDecisionDescription(Parser *recognizer, const dfa::DFA &dfa); /// /// Computes the set of conflicting or ambiguous alternatives from a diff --git a/runtime/Cpp/runtime/Lexer.cpp b/runtime/Cpp/runtime/Lexer.cpp index ae6fae35b..f325413fc 100755 --- a/runtime/Cpp/runtime/Lexer.cpp +++ b/runtime/Cpp/runtime/Lexer.cpp @@ -66,7 +66,7 @@ void Lexer::reset() { getInterpreter()->reset(); } -TokenRef Lexer::nextToken() { +Token::Ref Lexer::nextToken() { // Mark start location in char stream so unbuffered streams are // guaranteed at least have text of current token ssize_t tokenStartMarker = _input->mark(); @@ -167,18 +167,18 @@ CharStream* Lexer::getInputStream() { return _input; } -void Lexer::emit(TokenRef token) { +void Lexer::emit(Token::Ref token) { _token = token; } -TokenRef Lexer::emit() { - TokenRef t = std::dynamic_pointer_cast(_factory->create({ this, _input }, _type, _text, _channel, +Token::Ref Lexer::emit() { + Token::Ref t = std::dynamic_pointer_cast(_factory->create({ this, _input }, _type, _text, _channel, _tokenStartCharIndex, getCharIndex() - 1, _tokenStartLine, _tokenStartCharPositionInLine)); emit(t); return t; } -TokenRef Lexer::emitEOF() { +Token::Ref Lexer::emitEOF() { int cpos = getCharPositionInLine(); // The character position for EOF is one beyond the position of // the previous token's last character @@ -186,7 +186,7 @@ TokenRef Lexer::emitEOF() { int n = _token->getStopIndex() - _token->getStartIndex() + 1; cpos = _token->getCharPositionInLine() + n; } - TokenRef eof = std::dynamic_pointer_cast(_factory->create({ this, _input }, EOF, L"", Token::DEFAULT_CHANNEL, + Token::Ref eof = std::dynamic_pointer_cast(_factory->create({ this, _input }, EOF, L"", Token::DEFAULT_CHANNEL, (int)_input->index(), (int)_input->index() - 1, (int)getLine(), cpos)); emit(eof); return eof; @@ -223,11 +223,11 @@ void Lexer::setText(const std::wstring &text) { this->_text = text; } -TokenRef Lexer::getToken() { +Token::Ref Lexer::getToken() { return _token; } -void Lexer::setToken(TokenRef token) { +void Lexer::setToken(Token::Ref token) { _token = token; } @@ -247,9 +247,9 @@ int Lexer::getChannel() { return _channel; } -std::vector Lexer::getAllTokens() { - std::vector tokens; - TokenRef t = nextToken(); +std::vector Lexer::getAllTokens() { + std::vector tokens; + Token::Ref t = nextToken(); while (t->getType() != EOF) { tokens.push_back(t); 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 msg = std::wstring(L"token recognition error at: '") + getErrorDisplay(text) + std::wstring(L"'"); - ANTLRErrorListener *listener = getErrorListenerDispatch(); - listener->syntaxError(this, nullptr, (size_t)_tokenStartLine, _tokenStartCharPositionInLine, msg, std::make_exception_ptr(e)); + ProxyErrorListener &listener = getErrorListenerDispatch(); + listener.syntaxError(this, nullptr, (size_t)_tokenStartLine, _tokenStartCharPositionInLine, msg, std::make_exception_ptr(e)); } std::wstring Lexer::getErrorDisplay(const std::wstring &s) { std::wstringstream ss; - for (size_t i = 0; i < s.length(); i++) { - char c = ((char*)s.c_str())[i]; + for (auto c : s) { ss << getErrorDisplay(c); } return ss.str(); @@ -297,7 +296,7 @@ std::wstring Lexer::getErrorDisplay(int c) { s = L"\\r"; break; default: - s = std::to_wstring(c); + s = c; break; } return s; diff --git a/runtime/Cpp/runtime/Lexer.h b/runtime/Cpp/runtime/Lexer.h index b3fddfab4..8931439a9 100755 --- a/runtime/Cpp/runtime/Lexer.h +++ b/runtime/Cpp/runtime/Lexer.h @@ -70,7 +70,7 @@ namespace runtime { /// emissions, then set this to the last token to be matched or /// something nonnull so that the auto token emit mechanism will not /// emit another token. - TokenRef _token; + Token::Ref _token; /// /// What character index in the stream did the current token start at? @@ -114,7 +114,7 @@ namespace runtime { virtual void reset(); /// 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 /// 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 /// rather than a single variable as this implementation does). /// - virtual void emit(TokenRef token); + virtual void emit(Token::Ref token); /// /// 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 /// custom Token objects or provide a new factory. /// - virtual TokenRef emit(); + virtual Token::Ref emit(); - virtual TokenRef emitEOF(); + virtual Token::Ref emitEOF(); virtual size_t getLine() const override; @@ -191,9 +191,9 @@ namespace runtime { /// /// Override if emitting multiple tokens. - virtual TokenRef getToken(); + virtual Token::Ref getToken(); - virtual void setToken(TokenRef token); + virtual void setToken(Token::Ref token); virtual void setType(int ttype); @@ -207,7 +207,7 @@ namespace runtime { /// Return a list of all Token objects in input char stream. /// Forces load of all tokens. Does not include EOF token. - virtual std::vector getAllTokens(); + virtual std::vector getAllTokens(); virtual void recover(const LexerNoViableAltException &e); diff --git a/runtime/Cpp/runtime/LexerInterpreter.cpp b/runtime/Cpp/runtime/LexerInterpreter.cpp index 7d77df2b9..e2d75e648 100755 --- a/runtime/Cpp/runtime/LexerInterpreter.cpp +++ b/runtime/Cpp/runtime/LexerInterpreter.cpp @@ -39,26 +39,20 @@ using namespace org::antlr::v4::runtime; -LexerInterpreter::LexerInterpreter(const std::wstring &grammarFileName, std::vector *tokenNames, std::vector *ruleNames, std::vector *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 &tokenNames, + const std::vector &ruleNames, const std::vector &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) { throw IllegalArgumentException("The ATN must be a lexer ATN."); } - - for (int i = 0; i < (int)_decisionToDFA.size(); i++) { - _decisionToDFA[(size_t)i] = new dfa::DFA(_atn.getDecisionState(i), i); + for (size_t i = 0; i < (size_t)atn.getNumberOfDecisions(); ++i) { + _decisionToDFA.push_back(dfa::DFA(_atn.getDecisionState((int)i), (int)i)); } _interpreter = new atn::LexerATNSimulator(_atn, _decisionToDFA, _sharedContextCache); - if (tokenNames) { - _tokenNames = *tokenNames; - } - if (ruleNames) { - _ruleNames = *ruleNames; - } - if (modeNames) { - _modeNames = *modeNames; - } } LexerInterpreter::~LexerInterpreter() diff --git a/runtime/Cpp/runtime/LexerInterpreter.h b/runtime/Cpp/runtime/LexerInterpreter.h index af5987320..aac8420e0 100755 --- a/runtime/Cpp/runtime/LexerInterpreter.h +++ b/runtime/Cpp/runtime/LexerInterpreter.h @@ -39,30 +39,28 @@ namespace v4 { namespace runtime { class LexerInterpreter : public Lexer { + public: + LexerInterpreter(const std::wstring &grammarFileName, const std::vector &tokenNames, + const std::vector &ruleNames, const std::vector &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& getTokenNames() const override; + virtual const std::vector& getRuleNames() const override; + virtual const std::vector& getModeNames() const override; + protected: const std::wstring grammarFileName; const atn::ATN &_atn; - std::vector _tokenNames; - std::vector _ruleNames; - std::vector _modeNames; - std::vector _decisionToDFA; + const std::vector &_tokenNames; + const std::vector &_ruleNames; + const std::vector &_modeNames; + std::vector _decisionToDFA; std::shared_ptr _sharedContextCache; - - public: - LexerInterpreter(const std::wstring &grammarFileName, std::vector *tokenNames, std::vector *ruleNames, std::vector *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& getTokenNames() const override; - - virtual const std::vector& getRuleNames() const override; - - virtual const std::vector& getModeNames() const override; }; } // namespace runtime diff --git a/runtime/Cpp/runtime/ListTokenSource.cpp b/runtime/Cpp/runtime/ListTokenSource.cpp index e20a8555f..8dcb32560 100755 --- a/runtime/Cpp/runtime/ListTokenSource.cpp +++ b/runtime/Cpp/runtime/ListTokenSource.cpp @@ -45,7 +45,7 @@ int ListTokenSource::getCharPositionInLine() { } else if (tokens.size() > 0) { // have to calculate the result from the line/column of the previous // token, along with the text of the token. - TokenRef lastToken = tokens.back(); + Token::Ref lastToken = tokens.back(); std::wstring tokenText = lastToken->getText(); if (tokenText != L"") { int lastNewLine = (int)tokenText.rfind(L'\n'); @@ -62,7 +62,7 @@ int ListTokenSource::getCharPositionInLine() { return 0; } -TokenRef ListTokenSource::nextToken() { +Token::Ref ListTokenSource::nextToken() { if (i >= tokens.size()) { if (eofToken == nullptr) { int start = -1; @@ -81,7 +81,7 @@ TokenRef ListTokenSource::nextToken() { return eofToken; } - TokenRef t = tokens[i]; + Token::Ref t = tokens[i]; if (i == tokens.size() - 1 && t->getType() == EOF) { eofToken = t; } @@ -98,7 +98,7 @@ size_t ListTokenSource::getLine() const { } else if (tokens.size() > 0) { // have to calculate the result from the line/column of the previous // token, along with the text of the token. - TokenRef lastToken = tokens.back(); + Token::Ref lastToken = tokens.back(); int line = lastToken->getLine(); std::wstring tokenText = lastToken->getText(); diff --git a/runtime/Cpp/runtime/ListTokenSource.h b/runtime/Cpp/runtime/ListTokenSource.h index 4b04d8d41..416fc7ee2 100755 --- a/runtime/Cpp/runtime/ListTokenSource.h +++ b/runtime/Cpp/runtime/ListTokenSource.h @@ -49,7 +49,7 @@ namespace runtime { /// class ListTokenSource : public TokenSource { protected: - const std::vector tokens; + const std::vector tokens; /// /// The name of the input source. If this value is {@code null}, a call to @@ -71,7 +71,7 @@ namespace runtime { /// /// This field caches the EOF token for the token source. /// - TokenRef eofToken; + Token::Ref eofToken; /// /// This is the backing field for and @@ -115,7 +115,7 @@ namespace runtime { } virtual int getCharPositionInLine() override; - virtual TokenRef nextToken() override; + virtual Token::Ref nextToken() override; virtual size_t getLine() const override; virtual CharStream* getInputStream() override; virtual std::string getSourceName() override; diff --git a/runtime/Cpp/runtime/NoViableAltException.cpp b/runtime/Cpp/runtime/NoViableAltException.cpp index 87034d238..f6172e8a8 100755 --- a/runtime/Cpp/runtime/NoViableAltException.cpp +++ b/runtime/Cpp/runtime/NoViableAltException.cpp @@ -40,12 +40,12 @@ NoViableAltException::NoViableAltException(Parser *recognizer) recognizer->getCurrentToken(), nullptr, recognizer->getContext()) { } -NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input, TokenRef startToken, - TokenRef offendingToken, std::shared_ptr deadEndConfigs, ParserRuleContextRef ctx) +NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input, Token::Ref startToken, + Token::Ref offendingToken, std::shared_ptr deadEndConfigs, ParserRuleContext::Ref ctx) : RecognitionException(recognizer, input, ctx, offendingToken), _deadEndConfigs(deadEndConfigs), _startToken(startToken) { } -TokenRef NoViableAltException::getStartToken() const { +Token::Ref NoViableAltException::getStartToken() const { return _startToken; } diff --git a/runtime/Cpp/runtime/NoViableAltException.h b/runtime/Cpp/runtime/NoViableAltException.h index 12a21d158..ef3f2be6e 100755 --- a/runtime/Cpp/runtime/NoViableAltException.h +++ b/runtime/Cpp/runtime/NoViableAltException.h @@ -47,10 +47,10 @@ namespace runtime { class NoViableAltException : public RecognitionException { public: NoViableAltException(Parser *recognizer); // LL(1) error - NoViableAltException(Parser *recognizer, TokenStream *input, TokenRef startToken, TokenRef offendingToken, - std::shared_ptr deadEndConfigs, ParserRuleContextRef ctx); + NoViableAltException(Parser *recognizer, TokenStream *input, Token::Ref startToken, Token::Ref offendingToken, + std::shared_ptr deadEndConfigs, ParserRuleContext::Ref ctx); - virtual TokenRef getStartToken() const; + virtual Token::Ref getStartToken() const; virtual std::shared_ptr getDeadEndConfigs() const; private: @@ -61,7 +61,7 @@ namespace runtime { /// not be buffering tokens so get a reference to it. (At the /// 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.) - TokenRef _startToken; + Token::Ref _startToken; }; diff --git a/runtime/Cpp/runtime/Parser.cpp b/runtime/Cpp/runtime/Parser.cpp index 5630f795e..0d9bfabb4 100755 --- a/runtime/Cpp/runtime/Parser.cpp +++ b/runtime/Cpp/runtime/Parser.cpp @@ -57,7 +57,7 @@ std::map Parser::bypassAltsAtnCache; Parser::TraceListener::TraceListener(Parser *outerInstance) : outerInstance(outerInstance) { } -void Parser::TraceListener::enterEveryRule(ParserRuleContextRef ctx) { +void Parser::TraceListener::enterEveryRule(ParserRuleContext::Ref ctx) { std::cout << "enter " << antlrcpp::ws2s(outerInstance->getRuleNames()[(size_t)ctx->getRuleIndex()]) << ", LT(1)=" << antlrcpp::ws2s(outerInstance->_input->LT(1)->getText()) @@ -74,7 +74,7 @@ void Parser::TraceListener::visitTerminal(std::shared_ptr no void Parser::TraceListener::visitErrorNode(std::shared_ptr node) { } -void Parser::TraceListener::exitEveryRule(ParserRuleContextRef ctx) { +void Parser::TraceListener::exitEveryRule(ParserRuleContext::Ref ctx) { std::cout << "exit " << antlrcpp::ws2s(outerInstance->getRuleNames()[(size_t)ctx->getRuleIndex()]) << ", 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::INSTANCE = std::make_shared(); -void Parser::TrimToSizeListener::enterEveryRule(ParserRuleContextRef ctx) { +void Parser::TrimToSizeListener::enterEveryRule(ParserRuleContext::Ref ctx) { } void Parser::TrimToSizeListener::visitTerminal(std::shared_ptr node) { @@ -93,7 +93,7 @@ void Parser::TrimToSizeListener::visitTerminal(std::shared_ptr node) { } -void Parser::TrimToSizeListener::exitEveryRule(ParserRuleContextRef ctx) { +void Parser::TrimToSizeListener::exitEveryRule(ParserRuleContext::Ref ctx) { ctx->children.shrink_to_fit(); } @@ -121,8 +121,8 @@ void Parser::reset() { } } -TokenRef Parser::match(int ttype) { - TokenRef t = getCurrentToken(); +Token::Ref Parser::match(int ttype) { + Token::Ref t = getCurrentToken(); if (t->getType() == ttype) { _errHandler->reportMatch(this); consume(); @@ -137,8 +137,8 @@ TokenRef Parser::match(int ttype) { return t; } -TokenRef Parser::matchWildcard() { - TokenRef t = getCurrentToken(); +Token::Ref Parser::matchWildcard() { + Token::Ref t = getCurrentToken(); if (t->getType() > 0) { _errHandler->reportMatch(this); consume(); @@ -286,13 +286,12 @@ TokenStream* Parser::getTokenStream() { } void Parser::setTokenStream(TokenStream *input) { - delete _input; - _input = nullptr; + _input = nullptr; // Just a reference we don't own. reset(); _input = input; } -TokenRef Parser::getCurrentToken() { +Token::Ref Parser::getCurrentToken() { return _input->LT(1); } @@ -300,19 +299,19 @@ void Parser::notifyErrorListeners(const std::wstring &msg) { 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++; int line = -1; int charPositionInLine = -1; line = offendingToken->getLine(); charPositionInLine = offendingToken->getCharPositionInLine(); - ANTLRErrorListener *listener = getErrorListenerDispatch(); - listener->syntaxError(this, offendingToken, (size_t)line, charPositionInLine, msg, e); + ProxyErrorListener &listener = getErrorListenerDispatch(); + listener.syntaxError(this, offendingToken, (size_t)line, charPositionInLine, msg, e); } -TokenRef Parser::consume() { - TokenRef o = getCurrentToken(); +Token::Ref Parser::consume() { + Token::Ref o = getCurrentToken(); if (o->getType() != EOF) { getInputStream()->consume(); } @@ -342,11 +341,11 @@ void Parser::addContextToParseTree() { if (_ctx->parent.expired()) return; - ParserRuleContextRef parent = std::dynamic_pointer_cast(_ctx->parent.lock()); + ParserRuleContext::Ref parent = std::dynamic_pointer_cast(_ctx->parent.lock()); parent->addChild(_ctx); } -void Parser::enterRule(ParserRuleContextRef localctx, int state, int ruleIndex) { +void Parser::enterRule(ParserRuleContext::Ref localctx, int state, int ruleIndex) { setState(state); _ctx = localctx; _ctx->start = _input->LT(1); @@ -368,12 +367,12 @@ void Parser::exitRule() { _ctx = std::dynamic_pointer_cast(_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 // that is previous child of parse tree if (_buildParseTrees && _ctx != localctx) { if (!_ctx->parent.expired()) { - ParserRuleContextRef parent = std::dynamic_pointer_cast(_ctx->parent.lock()); + ParserRuleContext::Ref parent = std::dynamic_pointer_cast(_ctx->parent.lock()); parent->removeLastChild(); parent->addChild(localctx); } @@ -381,11 +380,11 @@ void Parser::enterOuterAlt(ParserRuleContextRef localctx, int altNum) { _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); } -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); _precedenceStack.push_back(precedence); _ctx = localctx; @@ -395,8 +394,8 @@ void Parser::enterRecursionRule(ParserRuleContextRef localctx, int state, int ru } } -void Parser::pushNewRecursionContext(ParserRuleContextRef localctx, int state, int ruleIndex) { - ParserRuleContextRef previous = _ctx; +void Parser::pushNewRecursionContext(ParserRuleContext::Ref localctx, int state, int ruleIndex) { + ParserRuleContext::Ref previous = _ctx; previous->parent = localctx; previous->invokingState = state; 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(); _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 if (_parseListeners.size() > 0) { @@ -436,8 +435,8 @@ void Parser::unrollRecursionContexts(ParserRuleContextRef parentctx) { } } -ParserRuleContextRef Parser::getInvokingContext(int ruleIndex) { - ParserRuleContextRef p = _ctx; +ParserRuleContext::Ref Parser::getInvokingContext(int ruleIndex) { + ParserRuleContext::Ref p = _ctx; while (p) { if (p->getRuleIndex() == ruleIndex) { return p; @@ -446,18 +445,18 @@ ParserRuleContextRef Parser::getInvokingContext(int ruleIndex) { break; p = std::dynamic_pointer_cast(p->parent.lock()); } - return ParserRuleContextRef(); + return ParserRuleContext::Ref(); } -ParserRuleContextRef Parser::getContext() { +ParserRuleContext::Ref Parser::getContext() { return _ctx; } -void Parser::setContext(ParserRuleContextRef ctx) { +void Parser::setContext(ParserRuleContext::Ref ctx) { _ctx = ctx; } -bool Parser::precpred(RuleContextRef localctx, int precedence) { +bool Parser::precpred(RuleContext::Ref localctx, int precedence) { return precedence >= _precedenceStack.back(); } @@ -468,7 +467,7 @@ bool Parser::inContext(const std::wstring &context) { bool Parser::isExpectedToken(int symbol) { const atn::ATN &atn = getInterpreter()->atn; - ParserRuleContextRef ctx = _ctx; + ParserRuleContext::Ref ctx = _ctx; atn::ATNState *s = atn.states[(size_t)getState()]; misc::IntervalSet following = atn.nextTokens(s); @@ -516,7 +515,7 @@ int Parser::getRuleIndex(const std::wstring &ruleName) { return m.at(ruleName); } -ParserRuleContextRef Parser::getRuleContext() { +ParserRuleContext::Ref Parser::getRuleContext() { return _ctx; } @@ -524,7 +523,7 @@ std::vector Parser::getRuleInvocationStack() { return getRuleInvocationStack(_ctx); } -std::vector Parser::getRuleInvocationStack(RuleContextRef p) { +std::vector Parser::getRuleInvocationStack(RuleContext::Ref p) { std::vector ruleNames = getRuleNames(); std::vector stack = std::vector(); while (p) { @@ -549,8 +548,8 @@ std::vector Parser::getDFAStrings() { std::vector s; for (size_t d = 0; d < simulator->_decisionToDFA.size(); d++) { - dfa::DFA *dfa = simulator->_decisionToDFA[d]; - s.push_back(dfa->toString(getTokenNames())); + dfa::DFA &dfa = simulator->_decisionToDFA[d]; + s.push_back(dfa.toString(getTokenNames())); } return s; } @@ -563,14 +562,14 @@ void Parser::dumpDFA() { std::lock_guard lck(mtx); bool seenOne = false; for (size_t d = 0; d < simulator->_decisionToDFA.size(); d++) { - dfa::DFA *dfa = simulator->_decisionToDFA[d]; - if (!dfa->states->empty()) { + dfa::DFA &dfa = simulator->_decisionToDFA[d]; + if (!dfa.states.empty()) { if (seenOne) { std::cout << std::endl; } - std::cout << L"Decision " << dfa->decision << L":" << std::endl; - dfa->toString(getTokenNames()); - std::wcout << dfa->toString(getTokenNames()); + std::cout << L"Decision " << dfa.decision << L":" << std::endl; + dfa.toString(getTokenNames()); + std::wcout << dfa.toString(getTokenNames()); seenOne = true; } } diff --git a/runtime/Cpp/runtime/Parser.h b/runtime/Cpp/runtime/Parser.h index 50cbd8717..a9d7bd596 100755 --- a/runtime/Cpp/runtime/Parser.h +++ b/runtime/Cpp/runtime/Parser.h @@ -51,10 +51,10 @@ namespace runtime { TraceListener(Parser *outerInstance); virtual ~TraceListener() {}; - virtual void enterEveryRule(ParserRuleContextRef ctx) override; + virtual void enterEveryRule(ParserRuleContext::Ref ctx) override; virtual void visitTerminal(std::shared_ptr node) override; virtual void visitErrorNode(std::shared_ptr node) override; - virtual void exitEveryRule(ParserRuleContextRef ctx) override; + virtual void exitEveryRule(ParserRuleContext::Ref ctx) override; private: Parser *const outerInstance; @@ -64,10 +64,10 @@ namespace runtime { public: static const std::shared_ptr INSTANCE; - virtual void enterEveryRule(ParserRuleContextRef ctx) override; + virtual void enterEveryRule(ParserRuleContext::Ref ctx) override; virtual void visitTerminal(std::shared_ptr node) override; virtual void visitErrorNode(std::shared_ptr node) override; - virtual void exitEveryRule(ParserRuleContextRef ctx) override; + virtual void exitEveryRule(ParserRuleContext::Ref ctx) override; }; Parser(TokenStream *input); @@ -94,7 +94,7 @@ namespace runtime { /// if the current input symbol did not match /// {@code ttype} and the error strategy could not recover from the /// mismatched symbol - virtual TokenRef match(int ttype); + virtual Token::Ref match(int ttype); /// /// Match current input symbol as a wildcard. If the symbol type matches @@ -112,7 +112,7 @@ namespace runtime { /// if the current input symbol did not match /// a wildcard and the error strategy could not recover from the mismatched /// symbol - virtual TokenRef matchWildcard(); + virtual Token::Ref matchWildcard(); /// /// Track the objects during the parse and hook @@ -267,11 +267,11 @@ namespace runtime { /// Match needs to return the current input symbol, which gets put /// into the label for the associated token ref; e.g., x=ID. /// - virtual TokenRef getCurrentToken(); + virtual Token::Ref getCurrentToken(); 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); /// /// Consume and return the . @@ -294,37 +294,37 @@ namespace runtime { /// is called on any parse /// listeners. /// - virtual TokenRef consume(); + virtual Token::Ref consume(); /// /// Always called by generated parsers upon entry to a rule. Access field /// get the current context. /// - virtual void enterRule(ParserRuleContextRef localctx, int state, int ruleIndex); + virtual void enterRule(ParserRuleContext::Ref localctx, int state, int ruleIndex); virtual void exitRule(); - virtual void enterOuterAlt(ParserRuleContextRef localctx, int altNum); + virtual void enterOuterAlt(ParserRuleContext::Ref localctx, int altNum); /// @deprecated Use /// instead. - virtual void enterRecursionRule(ParserRuleContextRef localctx, int ruleIndex); - virtual void enterRecursionRule(ParserRuleContextRef localctx, int state, int ruleIndex, int precedence); + virtual void enterRecursionRule(ParserRuleContext::Ref localctx, int ruleIndex); + virtual void enterRecursionRule(ParserRuleContext::Ref localctx, int state, int ruleIndex, int precedence); /// /// Like but for recursive rules. /// - 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 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); @@ -357,7 +357,7 @@ namespace runtime { /// Get a rule's index (i.e., {@code RULE_ruleName} field) or -1 if not found. virtual int getRuleIndex(const std::wstring &ruleName); - virtual ParserRuleContextRef getRuleContext(); + virtual ParserRuleContext::Ref getRuleContext(); /// /// Return List<String> of the rule names in your parser instance @@ -369,7 +369,7 @@ namespace runtime { /// virtual std::vector getRuleInvocationStack(); - virtual std::vector getRuleInvocationStack(RuleContextRef p); + virtual std::vector getRuleInvocationStack(RuleContext::Ref p); /// /// For debugging and other purposes. @@ -390,7 +390,7 @@ namespace runtime { protected: /// The ParserRuleContext object for the currently executing rule. /// 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. /// See also getErrorHandler. diff --git a/runtime/Cpp/runtime/ParserInterpreter.cpp b/runtime/Cpp/runtime/ParserInterpreter.cpp index a5b3fa733..670c5b588 100755 --- a/runtime/Cpp/runtime/ParserInterpreter.cpp +++ b/runtime/Cpp/runtime/ParserInterpreter.cpp @@ -55,7 +55,7 @@ ParserInterpreter::ParserInterpreter(const std::wstring &grammarFileName, const : Parser(input), _grammarFileName(grammarFileName), _tokenNames(tokenNames), _atn(atn), _ruleNames(ruleNames) { 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 @@ -103,7 +103,7 @@ std::wstring ParserInterpreter::getGrammarFileName() const { return _grammarFileName; } -ParserRuleContextRef ParserInterpreter::parse(int startRuleIndex) { +ParserRuleContext::Ref ParserInterpreter::parse(int startRuleIndex) { atn::RuleStartState *startRuleStartState = _atn.ruleToStartState[(size_t)startRuleIndex]; std::shared_ptr 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 }); Parser::enterRecursionRule(localctx, state, ruleIndex, precedence); } @@ -227,7 +227,7 @@ void ParserInterpreter::visitState(atn::ATNState *p) { void ParserInterpreter::visitRuleStopState(atn::ATNState *p) { atn::RuleStartState *ruleStartState = _atn.ruleToStartState[(size_t)p->ruleIndex]; if (ruleStartState->isPrecedenceRule) { - std::pair parentContext = _parentContextStack.top(); + std::pair parentContext = _parentContextStack.top(); _parentContextStack.pop(); unrollRecursionContexts(parentContext.first); diff --git a/runtime/Cpp/runtime/ParserInterpreter.h b/runtime/Cpp/runtime/ParserInterpreter.h index 63d24b958..35be17c6e 100755 --- a/runtime/Cpp/runtime/ParserInterpreter.h +++ b/runtime/Cpp/runtime/ParserInterpreter.h @@ -67,9 +67,9 @@ namespace runtime { virtual std::wstring getGrammarFileName() const override; /// 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: const std::wstring _grammarFileName; @@ -79,10 +79,10 @@ namespace runtime { std::vector _ruleNames; antlrcpp::BitSet _pushRecursionContextStates; - std::vector _decisionToDFA; // not shared like it is for generated parsers + std::vector _decisionToDFA; // not shared like it is for generated parsers std::shared_ptr _sharedContextCache; - std::stack> _parentContextStack; + std::stack> _parentContextStack; virtual atn::ATNState *getATNState(); virtual void visitState(atn::ATNState *p); diff --git a/runtime/Cpp/runtime/ParserRuleContext.cpp b/runtime/Cpp/runtime/ParserRuleContext.cpp index 794205f05..570685ea6 100755 --- a/runtime/Cpp/runtime/ParserRuleContext.cpp +++ b/runtime/Cpp/runtime/ParserRuleContext.cpp @@ -67,7 +67,7 @@ std::shared_ptr ParserRuleContext::addChild(std::shared_ptr< return t; } -RuleContextRef ParserRuleContext::addChild(RuleContextRef ruleInvocation) { +RuleContext::Ref ParserRuleContext::addChild(RuleContext::Ref ruleInvocation) { children.push_back(ruleInvocation); return ruleInvocation; } @@ -78,14 +78,14 @@ void ParserRuleContext::removeLastChild() { } } -std::shared_ptr ParserRuleContext::addChild(TokenRef matchedToken) { +std::shared_ptr ParserRuleContext::addChild(Token::Ref matchedToken) { std::shared_ptr t = std::make_shared(matchedToken); addChild(t); t->parent = shared_from_this(); return t; } -std::shared_ptr ParserRuleContext::addErrorNode(TokenRef badToken) { +std::shared_ptr ParserRuleContext::addErrorNode(Token::Ref badToken) { std::shared_ptr t = std::make_shared(badToken); addChild(t); t->parent = shared_from_this(); @@ -105,7 +105,7 @@ std::shared_ptr ParserRuleContext::getToken(int ttype, std:: for (auto o : children) { if (is(o)) { std::shared_ptr tnode = std::dynamic_pointer_cast(o); - TokenRef symbol = tnode->getSymbol(); + Token::Ref symbol = tnode->getSymbol(); if (symbol->getType() == ttype) { if (j++ == i) { return tnode; @@ -122,7 +122,7 @@ std::vector> ParserRuleContext::getTokens(in for (auto &o : children) { if (is(o)) { std::shared_ptr tnode = std::dynamic_pointer_cast(o); - TokenRef symbol = tnode->getSymbol(); + Token::Ref symbol = tnode->getSymbol(); if (symbol->getType() == ttype) { tokens.push_back(tnode); } @@ -145,11 +145,11 @@ misc::Interval ParserRuleContext::getSourceInterval() { return misc::Interval(start->getTokenIndex(), stop->getTokenIndex()); } -TokenRef ParserRuleContext::getStart() { +Token::Ref ParserRuleContext::getStart() { return start; } -TokenRef ParserRuleContext::getStop() { +Token::Ref ParserRuleContext::getStop() { return stop; } diff --git a/runtime/Cpp/runtime/ParserRuleContext.h b/runtime/Cpp/runtime/ParserRuleContext.h index 898cd9700..511ea4860 100755 --- a/runtime/Cpp/runtime/ParserRuleContext.h +++ b/runtime/Cpp/runtime/ParserRuleContext.h @@ -31,6 +31,7 @@ #pragma once +#include "Token.h" #include "RuleContext.h" #include "CPPUtils.h" @@ -64,14 +65,14 @@ namespace runtime { /// satisfy the superclass interface. /// class ParserRuleContext : public RuleContext { - /// + public: + typedef std::shared_ptr Ref; + /// If we are debugging or building a parse tree for a visitor, /// 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. /// operation because we don't the need to track the details about /// how we parse this rule. - /// - public: std::vector> children; /// @@ -95,7 +96,7 @@ namespace runtime { /// // public List states; - TokenRef start, stop; + Token::Ref start, stop; /// The exception that forced this rule to return. If the rule successfully /// completed, this is "null exception pointer". @@ -117,16 +118,16 @@ namespace runtime { /// Does not set parent link; other add methods do that. virtual std::shared_ptr addChild(std::shared_ptr t); - virtual RuleContextRef addChild(RuleContextRef ruleInvocation); + virtual RuleContext::Ref addChild(RuleContext::Ref ruleInvocation); /// Used by enterOuterAlt to toss out a RuleContext previously added as /// we entered a rule. If we have # label, we will need to remove /// generic ruleContext object. virtual void removeLastChild(); - virtual std::shared_ptr addChild(TokenRef matchedToken); + virtual std::shared_ptr addChild(Token::Ref matchedToken); - virtual std::shared_ptr addErrorNode(TokenRef badToken); + virtual std::shared_ptr addErrorNode(Token::Ref badToken); std::weak_ptr getParent() { return std::dynamic_pointer_cast(getParentReference().lock()); }; @@ -166,8 +167,8 @@ namespace runtime { virtual std::size_t getChildCount() override; virtual misc::Interval getSourceInterval() override; - virtual TokenRef getStart(); - virtual TokenRef getStop(); + virtual Token::Ref getStart(); + virtual Token::Ref getStop(); /// /// Used for rule context info debugging during parse-time, not so much for ATN debugging diff --git a/runtime/Cpp/runtime/ProxyErrorListener.cpp b/runtime/Cpp/runtime/ProxyErrorListener.cpp index 74d5b098f..ca35d6e9b 100755 --- a/runtime/Cpp/runtime/ProxyErrorListener.cpp +++ b/runtime/Cpp/runtime/ProxyErrorListener.cpp @@ -33,23 +33,47 @@ 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 configs) { - for (auto listener : *delegates) { + for (auto listener : _delegates) { 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 configs) { - for (auto listener : *delegates) { + for (auto listener : _delegates) { 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 configs) { - for (auto listener : *delegates) { + for (auto listener : _delegates) { listener->reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs); } } diff --git a/runtime/Cpp/runtime/ProxyErrorListener.h b/runtime/Cpp/runtime/ProxyErrorListener.h index d4e8e3739..5b1347bdf 100755 --- a/runtime/Cpp/runtime/ProxyErrorListener.h +++ b/runtime/Cpp/runtime/ProxyErrorListener.h @@ -44,30 +44,23 @@ namespace runtime { /// listeners. class ProxyErrorListener : public ANTLRErrorListener { private: - std::vector *const delegates; + std::set _delegates; // Not owned. public: - template //where T1 : ANTLRErrorListener - ProxyErrorListener(std::vector *delegates) : delegates(delegates) { - if (delegates == nullptr) { - throw NullPointerException("delegates"); - } - } + void addErrorListener(ANTLRErrorListener *listener); + void removeErrorListener(ANTLRErrorListener *listener); + void removeErrorListeners(); - void syntaxError(IRecognizer *recognizer, TokenRef offendingSymbol, size_t line, int charPositionInLine, - const std::wstring &msg, std::exception_ptr e) override { - for (auto listener : *delegates) { - listener->syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e); - } - } + void syntaxError(IRecognizer *recognizer, Token::Ref offendingSymbol, size_t line, int charPositionInLine, + 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 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 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 configs) override; }; diff --git a/runtime/Cpp/runtime/RecognitionException.cpp b/runtime/Cpp/runtime/RecognitionException.cpp index 7bb70e234..a38cf278e 100755 --- a/runtime/Cpp/runtime/RecognitionException.cpp +++ b/runtime/Cpp/runtime/RecognitionException.cpp @@ -39,12 +39,12 @@ using namespace org::antlr::v4::runtime; RecognitionException::RecognitionException(IRecognizer *recognizer, IntStream *input, - ParserRuleContextRef ctx, TokenRef offendingToken) + ParserRuleContext::Ref ctx, Token::Ref offendingToken) : RecognitionException("", recognizer, input, ctx, offendingToken) { } 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) { InitializeInstanceFields(); if (recognizer != nullptr) { @@ -67,7 +67,7 @@ misc::IntervalSet RecognitionException::getExpectedTokens() const { return misc::IntervalSet::EMPTY_SET; } -RuleContextRef RecognitionException::getCtx() const { +RuleContext::Ref RecognitionException::getCtx() const { return _ctx; } @@ -75,7 +75,7 @@ IntStream* RecognitionException::getInputStream() const { return _input; } -TokenRef RecognitionException::getOffendingToken() const { +Token::Ref RecognitionException::getOffendingToken() const { return _offendingToken; } diff --git a/runtime/Cpp/runtime/RecognitionException.h b/runtime/Cpp/runtime/RecognitionException.h index 13115e3fd..8ab430968 100755 --- a/runtime/Cpp/runtime/RecognitionException.h +++ b/runtime/Cpp/runtime/RecognitionException.h @@ -36,6 +36,7 @@ #include "IRecognizer.h" #include "IntStream.h" #include "RuleContext.h" +#include "ParserRuleContext.h" #include "Token.h" namespace org { @@ -53,20 +54,20 @@ namespace runtime { /// The Recognizer where this exception originated. IRecognizer *_recognizer; IntStream *_input; - RuleContextRef _ctx; + ParserRuleContext::Ref _ctx; /// The current Token when an error occurred. Since not all streams /// support accessing symbols by index, we have to track the Token /// instance itself. - TokenRef _offendingToken; + Token::Ref _offendingToken; int _offendingState; public: - RecognitionException(IRecognizer *recognizer, IntStream *input, ParserRuleContextRef ctx, - TokenRef offendingToken = TokenRef()); + RecognitionException(IRecognizer *recognizer, IntStream *input, ParserRuleContext::Ref ctx, + Token::Ref offendingToken = Token::Ref()); 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 /// occurred. For NoViableAltException and @@ -98,7 +99,7 @@ namespace runtime { /// /// The at the time this exception was thrown. /// If the context is not available, this method returns {@code null}. - virtual RuleContextRef getCtx() const; + virtual RuleContext::Ref getCtx() const; /// /// Gets the input stream which is the symbol source for the recognizer where @@ -111,7 +112,7 @@ namespace runtime { /// available. virtual IntStream* getInputStream() const; - virtual TokenRef getOffendingToken() const; + virtual Token::Ref getOffendingToken() const; /// /// Gets the where this exception occurred. diff --git a/runtime/Cpp/runtime/Recognizer.cpp b/runtime/Cpp/runtime/Recognizer.cpp index 1bfe9143e..f7ebab954 100755 --- a/runtime/Cpp/runtime/Recognizer.cpp +++ b/runtime/Cpp/runtime/Recognizer.cpp @@ -29,10 +29,10 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "ConsoleErrorListener.h" #include "RecognitionException.h" #include "CPPUtils.h" #include "Strings.h" -#include "ProxyErrorListener.h" #include "Token.h" #include "CPPUtils.h" @@ -43,6 +43,11 @@ using namespace org::antlr::v4::runtime; std::map, std::map> Recognizer::_tokenTypeMapCache; std::map, std::map> Recognizer::_ruleIndexMapCache; +Recognizer::Recognizer() { + InitializeInstanceFields(); + _proxListener.addErrorListener(&ConsoleErrorListener::INSTANCE); +} + std::map Recognizer::getTokenTypeMap() { const std::vector& tokenNames = getTokenNames(); if (tokenNames.empty()) { @@ -120,37 +125,30 @@ std::wstring Recognizer::getTokenErrorDisplay(Token *t) { } void Recognizer::addErrorListener(ANTLRErrorListener *listener) { - if (listener == nullptr) { - throw L"listener cannot be null."; - } - - _listeners.push_back(listener); + _proxListener.addErrorListener(listener); } void Recognizer::removeErrorListener(ANTLRErrorListener *listener) { - //_listeners.remove(listener); does this work the same way? - std::vector::iterator it; - it = std::find(_listeners.begin(), _listeners.end(), listener); - _listeners.erase(it); + _proxListener.removeErrorListener(listener); } void Recognizer::removeErrorListeners() { - _listeners.clear(); + _proxListener.removeErrorListeners(); } -ANTLRErrorListener *Recognizer::getErrorListenerDispatch() { - return (ANTLRErrorListener *)new ProxyErrorListener(getErrorListeners()); +ProxyErrorListener& Recognizer::getErrorListenerDispatch() { + return _proxListener; } -bool Recognizer::sempred(RuleContextRef localctx, int ruleIndex, int actionIndex) { +bool Recognizer::sempred(RuleContext::Ref localctx, int ruleIndex, int actionIndex) { return true; } -bool Recognizer::precpred(RuleContextRef localctx, int precedence) { +bool Recognizer::precpred(RuleContext::Ref localctx, int precedence) { return true; } -void Recognizer::action(RuleContextRef localctx, int ruleIndex, int actionIndex) { +void Recognizer::action(RuleContext::Ref localctx, int ruleIndex, int actionIndex) { } int Recognizer::getState() { @@ -165,9 +163,5 @@ void Recognizer::setState(int atnState) { void Recognizer::InitializeInstanceFields() { _stateNumber = -1; _interpreter = nullptr; - _listeners = std::vector(); } -Recognizer::Recognizer() { - InitializeInstanceFields(); -} diff --git a/runtime/Cpp/runtime/Recognizer.h b/runtime/Cpp/runtime/Recognizer.h index 9a7ab4ea8..3eb2b2715 100755 --- a/runtime/Cpp/runtime/Recognizer.h +++ b/runtime/Cpp/runtime/Recognizer.h @@ -31,6 +31,7 @@ #pragma once +#include "ProxyErrorListener.h" #include "IRecognizer.h" namespace org { @@ -40,16 +41,15 @@ namespace runtime { class Recognizer : public IRecognizer { public: - //static const int _EOF = -1; ml: we don't need it IntStream, nor in Token, nor here - Recognizer(); private: static std::map, std::map> _tokenTypeMapCache; static std::map, std::map> _ruleIndexMapCache; - std::vector _listeners; - //Mutex to manage synchronized access for multithreading + ProxyErrorListener _proxListener; // Manages a collection of listeners. + + // Mutex to manage synchronized access for multithreading. std::mutex mtx; protected: @@ -130,19 +130,15 @@ namespace runtime { virtual void removeErrorListeners(); - virtual std::vector *getErrorListeners() { - return &_listeners; - } - - virtual ANTLRErrorListener *getErrorListenerDispatch(); + virtual ProxyErrorListener& getErrorListenerDispatch(); // subclass needs to override these if there are sempreds or actions // 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(); diff --git a/runtime/Cpp/runtime/RuleContext.cpp b/runtime/Cpp/runtime/RuleContext.cpp index 4fa5eec09..c007fd830 100755 --- a/runtime/Cpp/runtime/RuleContext.cpp +++ b/runtime/Cpp/runtime/RuleContext.cpp @@ -37,7 +37,7 @@ using namespace org::antlr::v4::runtime; -const ParserRuleContextRef RuleContext::EMPTY = std::make_shared(); +const RuleContext::Ref RuleContext::EMPTY = std::make_shared(); RuleContext::RuleContext() { InitializeInstanceFields(); @@ -51,7 +51,7 @@ RuleContext::RuleContext(std::weak_ptr parent, int invokingState) { int RuleContext::depth() { int n = 1; - RuleContextRef p = shared_from_this(); + RuleContext::Ref p = shared_from_this(); while (true) { if (p->parent.expired()) break; @@ -69,7 +69,7 @@ misc::Interval RuleContext::getSourceInterval() { return misc::Interval::INVALID; } -RuleContextRef RuleContext::getRuleContext() { +RuleContext::Ref RuleContext::getRuleContext() { return shared_from_this(); } @@ -148,14 +148,14 @@ std::wstring RuleContext::toStringTree() { std::wstring RuleContext::toString(const std::vector &ruleNames) { - return toString(ruleNames, RuleContextRef()); + return toString(ruleNames, RuleContext::Ref()); } -std::wstring RuleContext::toString(const std::vector &ruleNames, RuleContextRef stop) { +std::wstring RuleContext::toString(const std::vector &ruleNames, RuleContext::Ref stop) { std::wstringstream ss; - RuleContextRef parent = shared_from_this(); + RuleContext::Ref parent = shared_from_this(); ss << L"["; while (parent != stop) { if (ruleNames.empty()) { @@ -190,7 +190,7 @@ std::wstring RuleContext::toString(Recognizer *recog) { 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); } diff --git a/runtime/Cpp/runtime/RuleContext.h b/runtime/Cpp/runtime/RuleContext.h index 4a6b996fd..5af69363e 100755 --- a/runtime/Cpp/runtime/RuleContext.h +++ b/runtime/Cpp/runtime/RuleContext.h @@ -60,7 +60,9 @@ namespace runtime { /// class RuleContext : public tree::RuleNode, public std::enable_shared_from_this { public: - static const ParserRuleContextRef EMPTY; + typedef std::shared_ptr Ref; + + static const RuleContext::Ref EMPTY; /// /// What context invoked this rule? @@ -86,7 +88,7 @@ namespace runtime { virtual misc::Interval getSourceInterval() override; - virtual RuleContextRef getRuleContext() override; + virtual RuleContext::Ref getRuleContext() override; virtual std::wstring getText() override; virtual ssize_t getRuleIndex() const; @@ -133,9 +135,9 @@ namespace runtime { std::wstring toString(const std::vector &ruleNames); // 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 &ruleNames, RuleContextRef stop); + virtual std::wstring toString(const std::vector &ruleNames, RuleContext::Ref stop); bool operator == (const RuleContext &other) { return this == &other; } // Simple address comparison. diff --git a/runtime/Cpp/runtime/Token.h b/runtime/Cpp/runtime/Token.h index 9e960007c..98218a72f 100755 --- a/runtime/Cpp/runtime/Token.h +++ b/runtime/Cpp/runtime/Token.h @@ -45,6 +45,8 @@ namespace runtime { /// class Token { public: + typedef std::shared_ptr Ref; + static const size_t INVALID_TYPE = 0; /// diff --git a/runtime/Cpp/runtime/TokenSource.h b/runtime/Cpp/runtime/TokenSource.h index 3e539f144..7d0cc82be 100755 --- a/runtime/Cpp/runtime/TokenSource.h +++ b/runtime/Cpp/runtime/TokenSource.h @@ -31,6 +31,7 @@ #pragma once +#include "Token.h" #include "TokenFactory.h" namespace org { @@ -61,7 +62,7 @@ namespace runtime { /// to the parser. /// public: - virtual TokenRef nextToken() = 0; + virtual Token::Ref nextToken() = 0; /// /// Get the line number for the current position in the input stream. The diff --git a/runtime/Cpp/runtime/TokenStream.h b/runtime/Cpp/runtime/TokenStream.h index ace06562b..ee304df55 100755 --- a/runtime/Cpp/runtime/TokenStream.h +++ b/runtime/Cpp/runtime/TokenStream.h @@ -31,6 +31,7 @@ #pragma once +#include "Token.h" #include "IntStream.h" namespace org { @@ -53,7 +54,7 @@ namespace runtime { public: virtual ~TokenStream(); - virtual TokenRef LT(ssize_t k) = 0; + virtual Token::Ref LT(ssize_t k) = 0; /// /// Gets the at the specified {@code index} in the stream. When @@ -72,7 +73,7 @@ namespace runtime { /// if {code index} is less than 0 /// if the stream does not support /// retrieving the token at the specified index - 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. virtual TokenSource* getTokenSource() const = 0; @@ -160,7 +161,7 @@ namespace runtime { /// /// if this stream does not support /// this method for the specified tokens - virtual std::wstring getText(TokenRef start, TokenRef stop) = 0; + virtual std::wstring getText(Token::Ref start, Token::Ref stop) = 0; }; } // namespace runtime diff --git a/runtime/Cpp/runtime/TokenStreamRewriter.cpp b/runtime/Cpp/runtime/TokenStreamRewriter.cpp index 080cd2741..ea221614b 100755 --- a/runtime/Cpp/runtime/TokenStreamRewriter.cpp +++ b/runtime/Cpp/runtime/TokenStreamRewriter.cpp @@ -287,7 +287,7 @@ std::wstring TokenStreamRewriter::getText(const std::wstring &programName, const while (i <= (size_t)stop && i < tokens->size()) { RewriteOperation *op = indexToOp[i]; 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) { // no operation at that index, just dump token if (t->getType() != EOF) { diff --git a/runtime/Cpp/runtime/UnbufferedCharStream.cpp b/runtime/Cpp/runtime/UnbufferedCharStream.cpp index d6d972f4c..d73983f33 100755 --- a/runtime/Cpp/runtime/UnbufferedCharStream.cpp +++ b/runtime/Cpp/runtime/UnbufferedCharStream.cpp @@ -85,7 +85,7 @@ size_t UnbufferedCharStream::fill(size_t n) { size_t c = nextChar(); add(c); } catch (IOException &ioe) { - throw std::exception(ioe); + std::throw_with_nested(RuntimeException()); } } diff --git a/runtime/Cpp/runtime/UnbufferedTokenStream.cpp b/runtime/Cpp/runtime/UnbufferedTokenStream.cpp index a28fd575a..fa5a21092 100755 --- a/runtime/Cpp/runtime/UnbufferedTokenStream.cpp +++ b/runtime/Cpp/runtime/UnbufferedTokenStream.cpp @@ -54,7 +54,7 @@ UnbufferedTokenStream::UnbufferedTokenStream(TokenSource *tokenSource, int buffe UnbufferedTokenStream::~UnbufferedTokenStream() { } -TokenRef UnbufferedTokenStream::get(size_t i) const +Token::Ref UnbufferedTokenStream::get(size_t i) const { // get absolute index size_t bufferStartIndex = getBufferStartIndex(); if (i < bufferStartIndex || i >= bufferStartIndex + _tokens.size()) { @@ -64,7 +64,7 @@ TokenRef UnbufferedTokenStream::get(size_t i) const return _tokens[i - bufferStartIndex]; } -TokenRef UnbufferedTokenStream::LT(ssize_t i) +Token::Ref UnbufferedTokenStream::LT(ssize_t i) { if (i == -1) { return _lastToken; @@ -104,7 +104,7 @@ std::wstring UnbufferedTokenStream::getText(RuleContext* ctx) 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())); } @@ -156,16 +156,16 @@ size_t UnbufferedTokenStream::fill(size_t n) return i; } - TokenRef t = _tokenSource->nextToken(); + Token::Ref t = _tokenSource->nextToken(); add(t); } return n; } -void UnbufferedTokenStream::add(TokenRef t) +void UnbufferedTokenStream::add(Token::Ref t) { - WritableTokenRef writable = std::dynamic_pointer_cast(t); + WritableToken::Ref writable = std::dynamic_pointer_cast(t); if (writable) { writable->setTokenIndex(int(getBufferStartIndex() + _tokens.size())); } @@ -203,7 +203,7 @@ void UnbufferedTokenStream::release(ssize_t marker) if (_p > 0) { // 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 - std::vector(_tokens.begin() + (ssize_t)_p, _tokens.end()).swap(_tokens); + std::vector(_tokens.begin() + (ssize_t)_p, _tokens.end()).swap(_tokens); _p = 0; } @@ -274,7 +274,7 @@ std::wstring UnbufferedTokenStream::getText(const misc::Interval &interval) std::wstringstream ss; for (size_t i = a; i <= b; i++) { - TokenRef t = _tokens[i]; + Token::Ref t = _tokens[i]; if (i > 0) ss << L", "; ss << t->getText(); diff --git a/runtime/Cpp/runtime/UnbufferedTokenStream.h b/runtime/Cpp/runtime/UnbufferedTokenStream.h index bc151e103..60d0fe542 100755 --- a/runtime/Cpp/runtime/UnbufferedTokenStream.h +++ b/runtime/Cpp/runtime/UnbufferedTokenStream.h @@ -44,8 +44,8 @@ namespace runtime { UnbufferedTokenStream(TokenSource *tokenSource, int bufferSize); virtual ~UnbufferedTokenStream(); - virtual TokenRef get(size_t i) const override; - virtual TokenRef LT(ssize_t i) override; + virtual Token::Ref get(size_t i) const override; + virtual Token::Ref LT(ssize_t i) override; virtual ssize_t LA(ssize_t i) 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() 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; @@ -83,7 +83,7 @@ namespace runtime { /// we start filling at index 0 again. /// - std::vector _tokens; + std::vector _tokens; /// /// 0..n-1 index into of next token. @@ -104,13 +104,13 @@ namespace runtime { /// /// This is the {@code LT(-1)} token for the current position. /// - TokenRef _lastToken; + Token::Ref _lastToken; /// /// When {@code numMarkers > 0}, this is the {@code LT(-1)} token for the /// first token in . Otherwise, this is {@code null}. /// - TokenRef _lastTokenBufferStart; + Token::Ref _lastTokenBufferStart; /// /// 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. /// virtual size_t fill(size_t n); - virtual void add(TokenRef t); + virtual void add(Token::Ref t); size_t getBufferStartIndex() const; diff --git a/runtime/Cpp/runtime/WritableToken.h b/runtime/Cpp/runtime/WritableToken.h index 3e3759787..201ebdb11 100755 --- a/runtime/Cpp/runtime/WritableToken.h +++ b/runtime/Cpp/runtime/WritableToken.h @@ -40,6 +40,8 @@ namespace runtime { class WritableToken : public Token { public: + typedef std::shared_ptr Ref; + virtual void setText(const std::wstring &text) = 0; virtual void setType(int ttype) = 0; diff --git a/runtime/Cpp/runtime/atn/ATN.cpp b/runtime/Cpp/runtime/atn/ATN.cpp index 09c03eaa0..5b5d5901b 100755 --- a/runtime/Cpp/runtime/atn/ATN.cpp +++ b/runtime/Cpp/runtime/atn/ATN.cpp @@ -38,19 +38,21 @@ #include "Recognizer.h" #include "ATNType.h" #include "Exceptions.h" +#include "CPPUtils.h" #include "ATN.h" using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime::atn; +using namespace antlrcpp; 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); return analyzer.LOOK(s, ctx); @@ -66,7 +68,7 @@ misc::IntervalSet ATN::nextTokens(ATNState *s) const { void ATN::addState(ATNState *state) { if (state != nullptr) { - state->atn = this; + //state->atn = this; state->stateNumber = (int)states.size(); } @@ -95,12 +97,12 @@ int ATN::getNumberOfDecisions() const { 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()) { throw IllegalArgumentException("Invalid state number."); } - RuleContextRef ctx = context; + RuleContext::Ref ctx = context; ATNState *s = states.at((size_t)stateNumber); misc::IntervalSet following = nextTokens(s); if (!following.contains(Token::EPSILON)) { @@ -129,3 +131,47 @@ misc::IntervalSet ATN::getExpectedTokens(int stateNumber, RuleContextRef context 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(); +} + diff --git a/runtime/Cpp/runtime/atn/ATN.h b/runtime/Cpp/runtime/atn/ATN.h index e7bb12a30..efd7af245 100755 --- a/runtime/Cpp/runtime/atn/ATN.h +++ b/runtime/Cpp/runtime/atn/ATN.h @@ -31,6 +31,8 @@ #pragma once +#include "RuleContext.h" + namespace org { namespace antlr { namespace v4 { @@ -43,7 +45,7 @@ namespace atn { /// Used for runtime deserialization of ATNs from strings. ATN(); - ATN(ATNType grammarType, int maxTokenType); + ATN(ATNType grammarType, size_t maxTokenType); std::vector states; @@ -62,7 +64,7 @@ namespace atn { ATNType grammarType; /// The maximum value for any symbol recognized by a transition in the ATN. - int maxTokenType; + size_t maxTokenType; /// /// 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 /// restricted to tokens reachable staying within {@code s}'s rule. /// - virtual misc::IntervalSet nextTokens(ATNState *s, RuleContextRef ctx) const; + virtual misc::IntervalSet nextTokens(ATNState *s, RuleContext::Ref ctx) const; /// /// 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. /// if the ATN does not contain a state with /// number {@code stateNumber} - 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 diff --git a/runtime/Cpp/runtime/atn/ATNConfig.cpp b/runtime/Cpp/runtime/atn/ATNConfig.cpp index 6202f02bd..721106fc7 100755 --- a/runtime/Cpp/runtime/atn/ATNConfig.cpp +++ b/runtime/Cpp/runtime/atn/ATNConfig.cpp @@ -36,11 +36,11 @@ 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::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) { 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, SemanticContextRef semanticContext) +ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, SemanticContext::Ref 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::ATNConfig(ATNConfig *c, ATNState *state, PredictionContextRef context) +ATNConfig::ATNConfig(ATNConfig *c, ATNState *state, PredictionContext::Ref context) : 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) { } diff --git a/runtime/Cpp/runtime/atn/ATNConfig.h b/runtime/Cpp/runtime/atn/ATNConfig.h index 576e09615..ef5b1d126 100755 --- a/runtime/Cpp/runtime/atn/ATNConfig.h +++ b/runtime/Cpp/runtime/atn/ATNConfig.h @@ -63,7 +63,7 @@ namespace atn { /// execution of the ATN simulator. /// /// Can be shared between multiple ANTConfig instances. - PredictionContextRef context; + PredictionContext::Ref context; /// We cannot execute predicates dependent upon local context unless /// we know for sure we are in the correct context. Because there is @@ -77,17 +77,17 @@ namespace atn { int reachesIntoOuterContext; /// Can be shared between multiple ATNConfig instances. - SemanticContextRef semanticContext; + SemanticContext::Ref semanticContext; - ATNConfig(ATNState *state, int alt, PredictionContextRef context); - ATNConfig(ATNState *state, int alt, PredictionContextRef context, SemanticContextRef semanticContext); + ATNConfig(ATNState *state, int alt, PredictionContext::Ref context); + ATNConfig(ATNState *state, int alt, PredictionContext::Ref context, SemanticContext::Ref semanticContext); ATNConfig(ATNConfig *c); // dup ATNConfig(ATNConfig *c, ATNState *state); - ATNConfig(ATNConfig *c, ATNState *state, SemanticContextRef semanticContext); - ATNConfig(ATNConfig *c, SemanticContextRef semanticContext); - ATNConfig(ATNConfig *c, ATNState *state, PredictionContextRef context); - ATNConfig(ATNConfig *c, ATNState *state, PredictionContextRef context, SemanticContextRef semanticContext); + ATNConfig(ATNConfig *c, ATNState *state, SemanticContext::Ref semanticContext); + ATNConfig(ATNConfig *c, SemanticContext::Ref semanticContext); + ATNConfig(ATNConfig *c, ATNState *state, PredictionContext::Ref context); + ATNConfig(ATNConfig *c, ATNState *state, PredictionContext::Ref context, SemanticContext::Ref semanticContext); virtual size_t hashCode() const; diff --git a/runtime/Cpp/runtime/atn/ATNConfigSet.cpp b/runtime/Cpp/runtime/atn/ATNConfigSet.cpp index 44ab3edd1..bb9fcbd52 100755 --- a/runtime/Cpp/runtime/atn/ATNConfigSet.cpp +++ b/runtime/Cpp/runtime/atn/ATNConfigSet.cpp @@ -96,7 +96,7 @@ bool ATNConfigSet::add(ATNConfig *config, PredictionContextMergeCache *mergeCach } // a previous (s,i,pi,_), merge with it and save result 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 // since only way to create new graphs is "call rule" and here. We // cache at both places. @@ -124,8 +124,8 @@ std::vector* ATNConfigSet::getStates() { return states; } -std::vector ATNConfigSet::getPredicates() { - std::vector preds; +std::vector ATNConfigSet::getPredicates() { + std::vector preds; for (auto c : configs) { if (c->semanticContext != SemanticContext::NONE) { preds.push_back(c->semanticContext); diff --git a/runtime/Cpp/runtime/atn/ATNConfigSet.h b/runtime/Cpp/runtime/atn/ATNConfigSet.h index 1827923b6..675034f5a 100755 --- a/runtime/Cpp/runtime/atn/ATNConfigSet.h +++ b/runtime/Cpp/runtime/atn/ATNConfigSet.h @@ -106,7 +106,7 @@ namespace atn { virtual std::vector *getStates(); - virtual std::vector getPredicates(); + virtual std::vector getPredicates(); virtual ATNConfig *get(size_t i) const; diff --git a/runtime/Cpp/runtime/atn/ATNDeserializationOptions.cpp b/runtime/Cpp/runtime/atn/ATNDeserializationOptions.cpp index 00cf803c0..f8fa20eb1 100755 --- a/runtime/Cpp/runtime/atn/ATNDeserializationOptions.cpp +++ b/runtime/Cpp/runtime/atn/ATNDeserializationOptions.cpp @@ -35,12 +35,11 @@ using namespace org::antlr::v4::runtime::atn; ATNDeserializationOptions ATNDeserializationOptions::defaultOptions; -ATNDeserializationOptions::ATNDeserializationOptions() -: readOnly(true) { +ATNDeserializationOptions::ATNDeserializationOptions() { + InitializeInstanceFields(); } -ATNDeserializationOptions::ATNDeserializationOptions(ATNDeserializationOptions *options) { - InitializeInstanceFields(); +ATNDeserializationOptions::ATNDeserializationOptions(ATNDeserializationOptions *options) : ATNDeserializationOptions() { this->verifyATN = options->verifyATN; this->generateRuleBypassTransitions = options->generateRuleBypassTransitions; } @@ -83,6 +82,6 @@ void ATNDeserializationOptions::throwIfReadOnly() { void ATNDeserializationOptions::InitializeInstanceFields() { readOnly = false; - verifyATN = false; + verifyATN = true; generateRuleBypassTransitions = false; } diff --git a/runtime/Cpp/runtime/atn/ATNDeserializer.cpp b/runtime/Cpp/runtime/atn/ATNDeserializer.cpp index 6f1658185..f11a8c158 100755 --- a/runtime/Cpp/runtime/atn/ATNDeserializer.cpp +++ b/runtime/Cpp/runtime/atn/ATNDeserializer.cpp @@ -63,27 +63,38 @@ #include "IntervalSet.h" #include "Exceptions.h" +#include "CPPUtils.h" +#include "Strings.h" #include "ATNDeserializer.h" 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 * reflected as change in the unique ID SERIALIZED_UUID. */ -const Guid ATNDeserializer::BASE_SERIALIZED_UUID("33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3"); -const Guid ATNDeserializer::ADDED_PRECEDENCE_TRANSITIONS("1DA0C57D-6C06-438A-9B27-10BCB3CE0F61"); -const Guid ATNDeserializer::ADDED_LEXER_ACTIONS("AADB8D7E-AEEF-4415-AD2B-8204D6CF042E"); -const std::vector ATNDeserializer::SUPPORTED_UUIDS = supportedUUIDsInitializer(); -const Guid ATNDeserializer::SERIALIZED_UUID = ADDED_LEXER_ACTIONS; +Guid ATNDeserializer::BASE_SERIALIZED_UUID("33761B2D-78BB-4A43-8B0B-4F5BEE8AACF3"); +Guid ATNDeserializer::ADDED_PRECEDENCE_TRANSITIONS("1DA0C57D-6C06-438A-9B27-10BCB3CE0F61"); +Guid ATNDeserializer::ADDED_LEXER_ACTIONS("AADB8D7E-AEEF-4415-AD2B-8204D6CF042E"); +std::vector ATNDeserializer::SUPPORTED_UUIDS = { BASE_SERIALIZED_UUID, ADDED_PRECEDENCE_TRANSITIONS }; +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) { + // 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) { @@ -101,7 +112,7 @@ bool ATNDeserializer::isFeatureSupported(const Guid &feature, const Guid &actual ATN ATNDeserializer::deserialize(const std::wstring& input) { // 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]; for (size_t i = 1; i < input.size(); ++i) { data[i] = input[i] - 2; @@ -116,8 +127,8 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) { } Guid uuid = toUUID(data, p); - auto uuidIterator = std::find(SUPPORTED_UUIDS.begin(), SUPPORTED_UUIDS.end(), uuid); p += 8; + auto uuidIterator = std::find(SUPPORTED_UUIDS.begin(), SUPPORTED_UUIDS.end(), uuid); if (uuidIterator == SUPPORTED_UUIDS.end()) { std::string reason = "Could not deserialize ATN with UUID " + uuid.toString() + " (expected " + 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); ATNType grammarType = (ATNType)data[p++]; - int maxTokenType = data[p++]; + size_t maxTokenType = data[p++]; ATN atn(grammarType, maxTokenType); // @@ -146,17 +157,17 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) { } int ruleIndex = data[p++]; - if (ruleIndex == WCHAR_MAX) { + if (ruleIndex == 0xFFFF) { // Not WCHAR_MAX as this can take a different value. ruleIndex = -1; } ATNState *s = stateFactory(stype, ruleIndex); if (stype == ATNState::LOOP_END) { // special case int loopBackStateNumber = data[p++]; - loopBackStateNumbers.push_back({ (LoopEndState*) s, loopBackStateNumber }); - } else if (dynamic_cast(s) != nullptr) { + loopBackStateNumbers.push_back({ (LoopEndState*)s, loopBackStateNumber }); + } else if (is(s)) { int endStateNumber = data[p++]; - endStateNumbers.push_back({ (BlockStartState*) s, endStateNumber }); + endStateNumbers.push_back({ (BlockStartState*)s, endStateNumber }); } atn.addState(s); } @@ -218,7 +229,7 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) { atn.ruleToStopState.resize(nrules); for (ATNState *state : atn.states) { - if (!(dynamic_cast(state) != nullptr)) { + if (!is(state)) { continue; } @@ -242,8 +253,7 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) { std::vector sets; int nsets = data[p++]; for (int i = 0; i < nsets; i++) { - int nintervals = data[p]; - p++; + int nintervals = data[p++]; misc::IntervalSet set; bool containsEof = data[p++] != 0; @@ -279,7 +289,7 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) { for (ATNState *state : atn.states) { for (size_t i = 0; i < state->getNumberOfTransitions(); i++) { Transition *t = state->transition(i); - if (!(dynamic_cast(t) != nullptr)) { + if (!is(t)) { continue; } @@ -289,34 +299,36 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) { } for (ATNState *state : atn.states) { - if (dynamic_cast(state) != nullptr) { + if (is(state)) { + BlockStartState *startState = static_cast(state); + // we need to know the end state to set its start state - if ((static_cast(state))->endState == nullptr) { + if (startState->endState == nullptr) { throw IllegalStateException(); } // block end states can only be associated to a single block start state - if ((static_cast(state))->endState->startState != nullptr) { + if (startState->endState->startState != nullptr) { throw IllegalStateException(); } - (static_cast(state))->endState->startState = static_cast(state); + startState->endState->startState = static_cast(state); } - if (dynamic_cast(state) != nullptr) { - PlusLoopbackState *loopbackState = static_cast(state); + if (is(state)) { + PlusLoopbackState *loopbackState = static_cast(state); for (size_t i = 0; i < loopbackState->getNumberOfTransitions(); i++) { ATNState *target = loopbackState->transition(i)->target; - if (dynamic_cast(target) != nullptr) { - (static_cast(target))->loopBackState = loopbackState; + if (is(target)) { + (static_cast(target))->loopBackState = loopbackState; } } - } else if (dynamic_cast(state) != nullptr) { - StarLoopbackState *loopbackState = static_cast(state); + } else if (is(state)) { + StarLoopbackState *loopbackState = static_cast(state); for (size_t i = 0; i < loopbackState->getNumberOfTransitions(); i++) { ATNState *target = loopbackState->transition(i)->target; - if (dynamic_cast(target) != nullptr) { + if (is(target)) { (static_cast(target))->loopBackState = loopbackState; } } @@ -328,8 +340,11 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) { // size_t ndecisions = (size_t)data[p++]; for (size_t i = 1; i <= ndecisions; i++) { - size_t s = (size_t)data[p++]; - DecisionState *decState = static_cast(atn.states[s]); + size_t s = data[p++]; + DecisionState *decState = dynamic_cast(atn.states[s]); + if (decState == nullptr) + throw IllegalStateException(); + atn.decisionToState.push_back(decState); decState->decision = (int)i - 1; } @@ -340,8 +355,8 @@ ATN ATNDeserializer::deserialize(const std::wstring& input) { if (deserializationOptions.isGenerateRuleBypassTransitions() && atn.grammarType == ATNType::PARSER) { atn.ruleToTokenType.resize(atn.ruleToStartState.size()); - for (std::vector::size_type i = 0; i < atn.ruleToStartState.size(); i++) { - atn.ruleToTokenType[i] = atn.maxTokenType + (int)i + 1; + for (size_t i = 0; i < atn.ruleToStartState.size(); i++) { + atn.ruleToTokenType[i] = int(atn.maxTokenType + i + 1); } for (std::vector::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); - if (dynamic_cast(state) != nullptr) { - checkCondition((static_cast(state))->loopBackState != nullptr); + if (is(state)) { + checkCondition((static_cast(state))->loopBackState != nullptr); } - if (dynamic_cast(state) != nullptr) { + if (is(state)) { StarLoopEntryState *starLoopEntryState = static_cast(state); checkCondition(starLoopEntryState->loopBackState != nullptr); checkCondition(starLoopEntryState->getNumberOfTransitions() == 2); - if (dynamic_cast(starLoopEntryState->transition(0)->target) != nullptr) { - checkCondition(dynamic_cast(starLoopEntryState->transition(1)->target) != nullptr); + if (is(starLoopEntryState->transition(0)->target)) { + checkCondition(static_cast(starLoopEntryState->transition(1)->target) != nullptr); checkCondition(!starLoopEntryState->nonGreedy); - } else if (dynamic_cast(starLoopEntryState->transition(0)->target) != nullptr) { - checkCondition(dynamic_cast(starLoopEntryState->transition(1)->target) != nullptr); + } else if (is(starLoopEntryState->transition(0)->target)) { + checkCondition(is(starLoopEntryState->transition(1)->target)); checkCondition(starLoopEntryState->nonGreedy); } else { throw IllegalStateException(); @@ -461,32 +476,32 @@ void ATNDeserializer::verifyATN(const ATN &atn) { } } - if (dynamic_cast(state) != nullptr) { + if (is(state)) { checkCondition(state->getNumberOfTransitions() == 1); - checkCondition(dynamic_cast(state->transition(0)->target) != nullptr); + checkCondition(is(state->transition(0)->target)); } - if (dynamic_cast(state) != nullptr) { - checkCondition((static_cast(state))->loopBackState != nullptr); + if (is(state)) { + checkCondition((static_cast(state))->loopBackState != nullptr); } - if (dynamic_cast(state) != nullptr) { - checkCondition((static_cast(state))->stopState != nullptr); + if (is(state)) { + checkCondition((static_cast(state))->stopState != nullptr); } - if (dynamic_cast(state) != nullptr) { - checkCondition((static_cast(state))->endState != nullptr); + if (is(state)) { + checkCondition((static_cast(state))->endState != nullptr); } - if (dynamic_cast(state) != nullptr) { - checkCondition((static_cast(state))->startState != nullptr); + if (is(state)) { + checkCondition((static_cast(state))->startState != nullptr); } - if (dynamic_cast(state) != nullptr) { - DecisionState *decisionState = static_cast(state); + if (is(state)) { + DecisionState *decisionState = static_cast(state); checkCondition(decisionState->getNumberOfTransitions() <= 1 || decisionState->decision >= 0); } else { - checkCondition(state->getNumberOfTransitions() <= 1 || dynamic_cast(state) != nullptr); + checkCondition(state->getNumberOfTransitions() <= 1 || is(state)); } } } @@ -501,8 +516,8 @@ void ATNDeserializer::checkCondition(bool condition, const std::string &message) } } -Guid ATNDeserializer::toUUID(const wchar_t *data, int offset) { - return Guid((uint32_t *)data + offset, true); +Guid ATNDeserializer::toUUID(const unsigned short *data, int offset) { + 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, @@ -592,12 +607,3 @@ ATNState *ATNDeserializer::stateFactory(int type, int ruleIndex) { s->ruleIndex = ruleIndex; return s; } - -std::vector ATNDeserializer::supportedUUIDsInitializer() { - std::vector supportedUUIDs; - supportedUUIDs.push_back(BASE_SERIALIZED_UUID); - supportedUUIDs.push_back(ADDED_PRECEDENCE_TRANSITIONS); - supportedUUIDs.push_back(ADDED_LEXER_ACTIONS); - - return supportedUUIDs; -} diff --git a/runtime/Cpp/runtime/atn/ATNDeserializer.h b/runtime/Cpp/runtime/atn/ATNDeserializer.h index 7b4fde46d..17840b6a4 100755 --- a/runtime/Cpp/runtime/atn/ATNDeserializer.h +++ b/runtime/Cpp/runtime/atn/ATNDeserializer.h @@ -41,50 +41,31 @@ namespace atn { class ATNDeserializer { public: - static const size_t SERIALIZED_VERSION; - //static ATNDeserializer(); + static const size_t SERIALIZED_VERSION = 3; - /// - /// This is the earliest supported serialized UUID. - /// - private: - - static Guid const BASE_SERIALIZED_UUID; - /// - /// This UUID indicates an extension of for the - /// addition of precedence predicates. - /// - 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; - /// - /// This list contains all of the currently supported UUIDs, ordered by when - /// the feature first appeared in this branch. - /// - static const std::vector SUPPORTED_UUIDS; - - /// /// This is the current serialized UUID. - /// - public: - static Guid const SERIALIZED_UUID; + static Guid SERIALIZED_UUID; - private: - ATNDeserializationOptions deserializationOptions; - - public: ATNDeserializer(); ATNDeserializer(const ATNDeserializationOptions& dso); - /// + 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 &sets); + + virtual ATNState *stateFactory(int type, int ruleIndex); + + protected: /// Determines if a particular serialized representation of an ATN supports /// a particular feature, identified by the used for serializing /// the ATN at the time the feature was first introduced. - /// + /// /// The marking the first time the feature was /// supported in the serialized ATN. /// The of the actual serialized ATN which is @@ -92,27 +73,29 @@ namespace atn { /// {@code true} if the {@code actualUuid} value represents a /// serialized ATN at or after the feature identified by {@code feature} was /// introduced; otherwise, {@code false}. - protected: 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 &sets); - - virtual ATNState *stateFactory(int type, int ruleIndex); - private: + /// This is the earliest supported serialized UUID. + static Guid BASE_SERIALIZED_UUID; + + /// This UUID indicates an extension of 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 SUPPORTED_UUIDS; + + ATNDeserializationOptions deserializationOptions; + static std::vector supportedUUIDsInitializer(); }; diff --git a/runtime/Cpp/runtime/atn/ATNSerializer.cpp b/runtime/Cpp/runtime/atn/ATNSerializer.cpp index bf4bb1d8a..d34cadfff 100755 --- a/runtime/Cpp/runtime/atn/ATNSerializer.cpp +++ b/runtime/Cpp/runtime/atn/ATNSerializer.cpp @@ -323,7 +323,7 @@ std::vector ATNSerializer::serialize() { //------------------------------------------------------------------------------------------------------------ std::wstring ATNSerializer::decode(const std::wstring &inpdata) { - wchar_t data[inpdata.size()]; + uint16_t data[inpdata.size()]; data[0] = inpdata[0]; // 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)) .append(L":") - .append(ATNState::serializationNames[stype]) + .append(ATNState::serializationNames[(size_t)stype]) .append(L" ") .append(std::to_wstring(ruleIndex)) .append(arg) diff --git a/runtime/Cpp/runtime/atn/ATNSimulator.cpp b/runtime/Cpp/runtime/atn/ATNSimulator.cpp index e22b7447e..66c3a6958 100755 --- a/runtime/Cpp/runtime/atn/ATNSimulator.cpp +++ b/runtime/Cpp/runtime/atn/ATNSimulator.cpp @@ -42,9 +42,6 @@ using namespace org::antlr::v4::runtime::atn; const std::shared_ptr ATNSimulator::ERROR = std::make_shared(INT32_MAX); -ATNSimulator::ATNSimulator() { -} - ATNSimulator::ATNSimulator(const ATN &atn, std::shared_ptr sharedContextCache) : atn(atn), _sharedContextCache(sharedContextCache) { } @@ -53,9 +50,9 @@ std::shared_ptr ATNSimulator::getSharedContextCache() { return _sharedContextCache; } -PredictionContextRef ATNSimulator::getCachedContext(PredictionContextRef context) { +PredictionContext::Ref ATNSimulator::getCachedContext(PredictionContext::Ref context) { std::lock_guard lck(mtx); - std::map visited; + std::map visited; return PredictionContext::getCachedContext(context, _sharedContextCache, visited); } diff --git a/runtime/Cpp/runtime/atn/ATNSimulator.h b/runtime/Cpp/runtime/atn/ATNSimulator.h index 9f7c370e9..ad718e25c 100755 --- a/runtime/Cpp/runtime/atn/ATNSimulator.h +++ b/runtime/Cpp/runtime/atn/ATNSimulator.h @@ -45,18 +45,16 @@ namespace atn { public: virtual ~ATNSimulator() {}; - ATNSimulator(); - /// Must distinguish between missing edge and edge we know leads nowhere. static const std::shared_ptr ERROR; - ATN atn; + const ATN &atn; ATNSimulator(const ATN &atn, std::shared_ptr sharedContextCache); virtual void reset() = 0; virtual std::shared_ptr getSharedContextCache(); - virtual PredictionContextRef getCachedContext(PredictionContextRef context); + virtual PredictionContext::Ref getCachedContext(PredictionContext::Ref context); /// @deprecated Use instead. static ATN deserialize(const std::wstring &data); diff --git a/runtime/Cpp/runtime/atn/ATNState.cpp b/runtime/Cpp/runtime/atn/ATNState.cpp index 5579042a5..c4cb0aace 100755 --- a/runtime/Cpp/runtime/atn/ATNState.cpp +++ b/runtime/Cpp/runtime/atn/ATNState.cpp @@ -32,18 +32,23 @@ #include "ATN.h" #include "Transition.h" #include "IntervalSet.h" +#include "CPPUtils.h" #include "ATNState.h" using namespace org::antlr::v4::runtime::atn; +using namespace antlrcpp; const int ATNState::INITIAL_NUM_TRANSITIONS; const int ATNState::INVALID_STATE_NUMBER; 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 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"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 { - 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 ATNState::getTransitions() { - std::vector arr(transitions); - - return arr; + return transitions; } size_t ATNState::getNumberOfTransitions() { @@ -108,10 +126,3 @@ bool ATNState::onlyHasEpsilonTransitions() { void ATNState::setRuleIndex(int ruleIndex) { this->ruleIndex = ruleIndex; } - -void ATNState::InitializeInstanceFields() { - atn = 0; - stateNumber = INVALID_STATE_NUMBER; - ruleIndex = 0; - epsilonOnlyTransitions = false; -} diff --git a/runtime/Cpp/runtime/atn/ATNState.h b/runtime/Cpp/runtime/atn/ATNState.h index bfc0d9bf3..6df6fa750 100755 --- a/runtime/Cpp/runtime/atn/ATNState.h +++ b/runtime/Cpp/runtime/atn/ATNState.h @@ -122,63 +122,39 @@ namespace atn { LOOP_END = 12 }; - static const wchar_t * serializationNames[]; + static const std::vector serializationNames; - /// - /// Which ATN are we in? - ATN *atn = nullptr; + /// Which ATN are we in? + // ml: just a reference to the owner. Set when the state gets added to an ATN. + //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; - - /// - /// Track the transitions emanating from this ATN state. protected: + /// Track the transitions emanating from this ATN state. std::vector transitions; - /// - /// Used to cache lookahead during parsing, not used during construction public: + /// Used to cache lookahead during parsing, not used during construction. misc::IntervalSet nextTokenWithinRule; virtual size_t hashCode(); bool operator == (const ATNState &other); virtual bool isNonGreedyExitState(); - virtual std::wstring toString() const; - virtual std::vector getTransitions(); - virtual size_t getNumberOfTransitions(); - virtual void addTransition(Transition *e); - virtual void addTransition(int index, Transition *e); - virtual Transition *transition(size_t i); - virtual void setTransition(size_t i, Transition *e); - virtual Transition *removeTransition(int index); - virtual int getStateType() = 0; - bool onlyHasEpsilonTransitions(); - virtual void setRuleIndex(int ruleIndex); - - private: - void InitializeInstanceFields(); - - public: - // ATNState() : transitions(new std::list()) { - // InitializeInstanceFields(); - // } }; } // namespace atn diff --git a/runtime/Cpp/runtime/atn/ATNType.h b/runtime/Cpp/runtime/atn/ATNType.h index 97292c9e5..b9130279f 100755 --- a/runtime/Cpp/runtime/atn/ATNType.h +++ b/runtime/Cpp/runtime/atn/ATNType.h @@ -37,23 +37,10 @@ namespace v4 { namespace runtime { namespace atn { - /// /// Represents the type of recognizer an ATN applies to. - /// - /// @author Sam Harwell - /// enum class ATNType { - - /// - /// A lexer grammar. - /// - LEXER, - - /// - /// A parser grammar. - /// - PARSER, - + LEXER = 0, + PARSER = 1, }; } // namespace atn diff --git a/runtime/Cpp/runtime/atn/ActionTransition.cpp b/runtime/Cpp/runtime/atn/ActionTransition.cpp index daa0c773e..e1c2502d4 100755 --- a/runtime/Cpp/runtime/atn/ActionTransition.cpp +++ b/runtime/Cpp/runtime/atn/ActionTransition.cpp @@ -52,5 +52,6 @@ bool ActionTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbo } 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" }"; } diff --git a/runtime/Cpp/runtime/atn/ActionTransition.h b/runtime/Cpp/runtime/atn/ActionTransition.h index acbe1aa34..4ce99b7e9 100755 --- a/runtime/Cpp/runtime/atn/ActionTransition.h +++ b/runtime/Cpp/runtime/atn/ActionTransition.h @@ -55,7 +55,7 @@ namespace atn { virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; - virtual std::wstring toString() const; + virtual std::wstring toString() const override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/atn/ArrayPredictionContext.cpp b/runtime/Cpp/runtime/atn/ArrayPredictionContext.cpp index e04128dd3..d9f9495ed 100755 --- a/runtime/Cpp/runtime/atn/ArrayPredictionContext.cpp +++ b/runtime/Cpp/runtime/atn/ArrayPredictionContext.cpp @@ -36,7 +36,7 @@ using namespace org::antlr::v4::runtime::atn; -ArrayPredictionContext::ArrayPredictionContext(SingletonPredictionContextRef a) +ArrayPredictionContext::ArrayPredictionContext(SingletonPredictionContext::Ref a) : ArrayPredictionContext({ a->parent }, { a->returnState }) { } diff --git a/runtime/Cpp/runtime/atn/ArrayPredictionContext.h b/runtime/Cpp/runtime/atn/ArrayPredictionContext.h index 3b4d51b07..16c6ab544 100755 --- a/runtime/Cpp/runtime/atn/ArrayPredictionContext.h +++ b/runtime/Cpp/runtime/atn/ArrayPredictionContext.h @@ -52,7 +52,7 @@ namespace atn { /// Sorted for merge, no duplicates; if present, EMPTY_RETURN_STATE is always last. const std::vector returnStates; - ArrayPredictionContext(SingletonPredictionContextRef a); + ArrayPredictionContext(SingletonPredictionContext::Ref a); ArrayPredictionContext(const std::vector> &parents, const std::vector &returnStates); diff --git a/runtime/Cpp/runtime/atn/AtomTransition.cpp b/runtime/Cpp/runtime/atn/AtomTransition.cpp index ca440bb10..4ca3d99b4 100755 --- a/runtime/Cpp/runtime/atn/AtomTransition.cpp +++ b/runtime/Cpp/runtime/atn/AtomTransition.cpp @@ -53,5 +53,5 @@ bool AtomTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) } std::wstring AtomTransition::toString() const { - return std::to_wstring(_label); + return L"ATOM " + Transition::toString() + L" { label: " + std::to_wstring(_label) + L" }"; } diff --git a/runtime/Cpp/runtime/atn/AtomTransition.h b/runtime/Cpp/runtime/atn/AtomTransition.h index e162608dd..e9d5a2077 100755 --- a/runtime/Cpp/runtime/atn/AtomTransition.h +++ b/runtime/Cpp/runtime/atn/AtomTransition.h @@ -54,7 +54,7 @@ namespace atn { virtual misc::IntervalSet label() 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 diff --git a/runtime/Cpp/runtime/atn/DecisionState.cpp b/runtime/Cpp/runtime/atn/DecisionState.cpp index d5e4b0294..5b1216014 100755 --- a/runtime/Cpp/runtime/atn/DecisionState.cpp +++ b/runtime/Cpp/runtime/atn/DecisionState.cpp @@ -37,3 +37,7 @@ void DecisionState::InitializeInstanceFields() { decision = -1; nonGreedy = false; } + +std::wstring DecisionState::toString() const { + return L"DECISION " + ATNState::toString(); +} \ No newline at end of file diff --git a/runtime/Cpp/runtime/atn/DecisionState.h b/runtime/Cpp/runtime/atn/DecisionState.h index 742e67a9e..7ea37472b 100755 --- a/runtime/Cpp/runtime/atn/DecisionState.h +++ b/runtime/Cpp/runtime/atn/DecisionState.h @@ -51,6 +51,8 @@ namespace atn { DecisionState() { InitializeInstanceFields(); } + + virtual std::wstring toString() const override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/atn/EpsilonTransition.cpp b/runtime/Cpp/runtime/atn/EpsilonTransition.cpp index 715771839..0ba76a255 100755 --- a/runtime/Cpp/runtime/atn/EpsilonTransition.cpp +++ b/runtime/Cpp/runtime/atn/EpsilonTransition.cpp @@ -49,5 +49,5 @@ bool EpsilonTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymb } std::wstring EpsilonTransition::toString() const { - return L"epsilon"; + return L"EPSILON " + Transition::toString() + L" {}"; } diff --git a/runtime/Cpp/runtime/atn/EpsilonTransition.h b/runtime/Cpp/runtime/atn/EpsilonTransition.h index 059516383..d8374317d 100755 --- a/runtime/Cpp/runtime/atn/EpsilonTransition.h +++ b/runtime/Cpp/runtime/atn/EpsilonTransition.h @@ -48,7 +48,7 @@ namespace atn { virtual bool isEpsilon() 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 diff --git a/runtime/Cpp/runtime/atn/LL1Analyzer.cpp b/runtime/Cpp/runtime/atn/LL1Analyzer.cpp index bef339bfa..d42bac927 100755 --- a/runtime/Cpp/runtime/atn/LL1Analyzer.cpp +++ b/runtime/Cpp/runtime/atn/LL1Analyzer.cpp @@ -74,14 +74,14 @@ std::vector LL1Analyzer::getDecisionLookahead(ATNState *s) co 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); } -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; 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 lookBusy; antlrcpp::BitSet callRuleStack; @@ -90,7 +90,7 @@ misc::IntervalSet LL1Analyzer::LOOK(ATNState *s, ATNState *stopState, RuleContex 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 &lookBusy, antlrcpp::BitSet &calledRuleStack, bool seeThruPreds, bool addEOF) const { 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++) { Transition *t = s->transition(i); - if (typeid(t) == typeid(RuleTransition)) { + if (is(t)) { if (calledRuleStack[(size_t)(static_cast(t))->target->ruleIndex]) { continue; } - PredictionContextRef newContext = SingletonPredictionContext::create(ctx, (static_cast(t))->followState->stateNumber); + PredictionContext::Ref newContext = SingletonPredictionContext::create(ctx, (static_cast(t))->followState->stateNumber); auto onExit = finally([&] { calledRuleStack[(size_t)((static_cast(t))->target->ruleIndex)] = false; }); @@ -153,7 +153,7 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContextRef c calledRuleStack.set((size_t)(static_cast(t))->target->ruleIndex); _LOOK(t->target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); - } else if (dynamic_cast(t) != nullptr) { + } else if (is(t)) { if (seeThruPreds) { _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } else { @@ -161,13 +161,13 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContextRef c } } else if (t->isEpsilon()) { _LOOK(t->target, stopState, ctx, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); - } else if (typeid(t) == typeid(WildcardTransition)) { - look.addAll(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, _atn.maxTokenType)); + } else if (is(t)) { + look.addAll(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, (int)_atn.maxTokenType)); } else { misc::IntervalSet set = t->label(); if (!set.isEmpty()) { - if (dynamic_cast(t) != nullptr) { - set = set.complement(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, _atn.maxTokenType)); + if (is(t)) { + set = set.complement(misc::IntervalSet::of(Token::MIN_USER_TOKEN_TYPE, (int)_atn.maxTokenType)); } look.addAll(set); } diff --git a/runtime/Cpp/runtime/atn/LL1Analyzer.h b/runtime/Cpp/runtime/atn/LL1Analyzer.h index 14499d5cd..6f5ba77cc 100755 --- a/runtime/Cpp/runtime/atn/LL1Analyzer.h +++ b/runtime/Cpp/runtime/atn/LL1Analyzer.h @@ -79,7 +79,7 @@ namespace atn { /// /// The set of tokens that can follow {@code s} in the ATN in the /// specified {@code ctx}. - virtual misc::IntervalSet LOOK(ATNState *s, RuleContextRef ctx) const; + virtual misc::IntervalSet LOOK(ATNState *s, RuleContext::Ref ctx) const; /// /// Compute set of tokens that can follow {@code s} in the ATN in the @@ -98,7 +98,7 @@ namespace atn { /// /// The set of tokens that can follow {@code s} in the ATN in the /// specified {@code ctx}. - virtual misc::IntervalSet LOOK(ATNState *s, ATNState *stopState, RuleContextRef ctx) const; + virtual misc::IntervalSet LOOK(ATNState *s, ATNState *stopState, RuleContext::Ref ctx) const; /// /// 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} /// is {@code null}. 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 &lookBusy, antlrcpp::BitSet &calledRuleStack, bool seeThruPreds, bool addEOF) const; }; diff --git a/runtime/Cpp/runtime/atn/LexerATNConfig.cpp b/runtime/Cpp/runtime/atn/LexerATNConfig.cpp index ca62dae2f..cb1fc4d2e 100755 --- a/runtime/Cpp/runtime/atn/LexerATNConfig.cpp +++ b/runtime/Cpp/runtime/atn/LexerATNConfig.cpp @@ -37,11 +37,11 @@ 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) { } -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) { lexerActionIndex = actionIndex; } @@ -56,7 +56,7 @@ LexerATNConfig::LexerATNConfig(LexerATNConfig *c, ATNState *state, int actionInd 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)) { lexerActionIndex = c->lexerActionIndex; } diff --git a/runtime/Cpp/runtime/atn/LexerATNConfig.h b/runtime/Cpp/runtime/atn/LexerATNConfig.h index bb05a4863..474ebd2c1 100755 --- a/runtime/Cpp/runtime/atn/LexerATNConfig.h +++ b/runtime/Cpp/runtime/atn/LexerATNConfig.h @@ -44,12 +44,12 @@ namespace atn { /// Capture lexer action we traverse. int lexerActionIndex = -1; - LexerATNConfig(ATNState *state, int alt, PredictionContextRef context); - LexerATNConfig(ATNState *state, int alt, PredictionContextRef context, int actionIndex); + LexerATNConfig(ATNState *state, int alt, PredictionContext::Ref context); + LexerATNConfig(ATNState *state, int alt, PredictionContext::Ref context, int actionIndex); LexerATNConfig(LexerATNConfig *c, ATNState *state); LexerATNConfig(LexerATNConfig *c, ATNState *state, int actionIndex); - LexerATNConfig(LexerATNConfig *c, ATNState *state, PredictionContextRef context); + LexerATNConfig(LexerATNConfig *c, ATNState *state, PredictionContext::Ref context); bool hasPassedThroughNonGreedyDecision(); diff --git a/runtime/Cpp/runtime/atn/LexerATNSimulator.cpp b/runtime/Cpp/runtime/atn/LexerATNSimulator.cpp index 9e7b07957..4f0e54dda 100755 --- a/runtime/Cpp/runtime/atn/LexerATNSimulator.cpp +++ b/runtime/Cpp/runtime/atn/LexerATNSimulator.cpp @@ -68,12 +68,12 @@ void LexerATNSimulator::SimState::InitializeInstanceFields() { int LexerATNSimulator::match_calls = 0; -LexerATNSimulator::LexerATNSimulator(const ATN &atn, const std::vector &decisionToDFA, +LexerATNSimulator::LexerATNSimulator(const ATN &atn, std::vector &decisionToDFA, std::shared_ptr sharedContextCache) : LexerATNSimulator(nullptr, atn, decisionToDFA, sharedContextCache) { } -LexerATNSimulator::LexerATNSimulator(Lexer *recog, const ATN &atn, const std::vector &decisionToDFA, +LexerATNSimulator::LexerATNSimulator(Lexer *recog, const ATN &atn, std::vector &decisionToDFA, std::shared_ptr sharedContextCache) : ATNSimulator(atn, sharedContextCache), _recog(recog), _decisionToDFA(decisionToDFA), prevAccept(new SimState()) { InitializeInstanceFields(); @@ -97,11 +97,11 @@ int LexerATNSimulator::match(CharStream *input, size_t mode) { _startIndex = (int)input->index(); prevAccept->reset(); - dfa::DFA *dfa = _decisionToDFA[mode]; - if (dfa->s0 == nullptr) { + const dfa::DFA &dfa = _decisionToDFA[mode]; + if (dfa.s0 == nullptr) { return matchATN(input); } else { - return execATN(input, dfa->s0); + return execATN(input, dfa.s0); } return -1; @@ -117,7 +117,7 @@ void LexerATNSimulator::reset() { } int LexerATNSimulator::matchATN(CharStream *input) { - ATNState *startState = (ATNState *)atn.modeToStartState.at(_mode); + ATNState *startState = (ATNState *)atn.modeToStartState[_mode]; if (debug) { 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); if (!suppressEdge) { - _decisionToDFA[_mode]->s0 = next; + _decisionToDFA[_mode].s0 = next; } int predict = execATN(input, next); 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; @@ -179,7 +179,6 @@ int LexerATNSimulator::execATN(CharStream *input, dfa::DFAState *ds0) { target = computeTargetState(input, s, t); } - std::wstring temp = target->toString(); if (target == ERROR.get()) { break; } @@ -331,7 +330,7 @@ bool LexerATNSimulator::closure(CharStream *input, LexerATNConfig *config, std:: std::wcout << L"closure(" << config->toString(true) << L")" << std::endl; } - if (dynamic_cast(config->state) != nullptr) { + if (is(config->state)) { if (debug) { if (_recog != nullptr) { 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()) { case Transition::RULE: { RuleTransition *ruleTransition = static_cast(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); } break; @@ -523,7 +522,7 @@ dfa::DFAState *LexerATNSimulator::addDFAState(std::shared_ptr conf dfa::DFAState *proposed = new dfa::DFAState(configs); ATNConfig *firstConfigWithRuleStopState = nullptr; for (auto c : configs->configs) { - if (dynamic_cast(c->state) != nullptr) { + if (is(c->state)) { firstConfigWithRuleStopState = c; break; } @@ -536,27 +535,27 @@ dfa::DFAState *LexerATNSimulator::addDFAState(std::shared_ptr conf proposed->prediction = atn.ruleToTokenType[(size_t)proposed->lexerRuleIndex]; } - dfa::DFA *dfa = _decisionToDFA[_mode]; + dfa::DFA &dfa = _decisionToDFA[_mode]; { std::lock_guard lck(mtx); - auto iterator = dfa->states->find(proposed); - if (iterator != dfa->states->end()) { + auto iterator = dfa.states.find(proposed); + if (iterator != dfa.states.end()) { return iterator->second; } dfa::DFAState *newState = proposed; - newState->stateNumber = (int)dfa->states->size(); + newState->stateNumber = (int)dfa.states.size(); configs->setReadonly(true); newState->configs = configs; - (*dfa->states)[newState] = newState; + dfa.states[newState] = newState; return newState; } } -dfa::DFA *LexerATNSimulator::getDFA(size_t mode) { +dfa::DFA& LexerATNSimulator::getDFA(size_t mode) { return _decisionToDFA[mode]; } diff --git a/runtime/Cpp/runtime/atn/LexerATNSimulator.h b/runtime/Cpp/runtime/atn/LexerATNSimulator.h index 87344bc39..394086183 100755 --- a/runtime/Cpp/runtime/atn/LexerATNSimulator.h +++ b/runtime/Cpp/runtime/atn/LexerATNSimulator.h @@ -104,7 +104,7 @@ namespace atn { int _charPositionInLine; public: - const std::vector _decisionToDFA; + std::vector &_decisionToDFA; protected: size_t _mode; @@ -115,19 +115,17 @@ namespace atn { public: static int match_calls; - LexerATNSimulator(const ATN &atn, const std::vector &decisionToDFA, std::shared_ptr sharedContextCache); - LexerATNSimulator(Lexer *recog, const ATN &atn, const std::vector &decisionToDFA, + LexerATNSimulator(const ATN &atn, std::vector &decisionToDFA, + std::shared_ptr sharedContextCache); + LexerATNSimulator(Lexer *recog, const ATN &atn, std::vector &decisionToDFA, std::shared_ptr sharedContextCache); virtual void copyState(LexerATNSimulator *simulator); - virtual int match(CharStream *input, size_t mode); - virtual void reset() override; protected: virtual int matchATN(CharStream *input); - virtual int execATN(CharStream *input, dfa::DFAState *ds0); /// @@ -221,23 +219,15 @@ namespace atn { virtual dfa::DFAState *addDFAState(std::shared_ptr configs); public: - dfa::DFA *getDFA(size_t mode); + dfa::DFA& getDFA(size_t mode); - /// /// Get the text matched so far for the current token. - /// virtual std::wstring getText(CharStream *input); - virtual size_t getLine() const; - virtual void setLine(size_t line); - virtual int getCharPositionInLine(); - virtual void setCharPositionInLine(int charPositionInLine); - virtual void consume(CharStream *input); - virtual std::wstring getTokenName(int t); private: diff --git a/runtime/Cpp/runtime/atn/NotSetTransition.cpp b/runtime/Cpp/runtime/atn/NotSetTransition.cpp index 75b60235c..740fe2170 100755 --- a/runtime/Cpp/runtime/atn/NotSetTransition.cpp +++ b/runtime/Cpp/runtime/atn/NotSetTransition.cpp @@ -47,5 +47,5 @@ bool NotSetTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbo } std::wstring NotSetTransition::toString() const { - return L'~' + SetTransition::toString(); + return L"NOT_SET " + Transition::toString() + L" { " + SetTransition::toString() + L" }"; } diff --git a/runtime/Cpp/runtime/atn/ParserATNSimulator.cpp b/runtime/Cpp/runtime/atn/ParserATNSimulator.cpp index 838be7e4a..59007b592 100755 --- a/runtime/Cpp/runtime/atn/ParserATNSimulator.cpp +++ b/runtime/Cpp/runtime/atn/ParserATNSimulator.cpp @@ -61,12 +61,12 @@ using namespace org::antlr::v4::runtime::atn; using namespace antlrcpp; -ParserATNSimulator::ParserATNSimulator(const ATN &atn, const std::vector &decisionToDFA, +ParserATNSimulator::ParserATNSimulator(const ATN &atn, std::vector &decisionToDFA, std::shared_ptr sharedContextCache) : ParserATNSimulator(nullptr, atn, decisionToDFA, sharedContextCache) { } -ParserATNSimulator::ParserATNSimulator(Parser *parser, const ATN &atn, const std::vector &decisionToDFA, +ParserATNSimulator::ParserATNSimulator(Parser *parser, const ATN &atn, std::vector &decisionToDFA, std::shared_ptr sharedContextCache) : ATNSimulator(atn, sharedContextCache), parser(parser), _decisionToDFA(decisionToDFA) { InitializeInstanceFields(); @@ -75,7 +75,7 @@ ParserATNSimulator::ParserATNSimulator(Parser *parser, const ATN &atn, const std 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) { 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; _startIndex = (int)input->index(); _outerContext = outerContext; - dfa::DFA *dfa = _decisionToDFA[(size_t)decision]; + dfa::DFA &dfa = _decisionToDFA[(size_t)decision]; ssize_t m = input->mark(); size_t index = input->index(); // Now we are certain to have a specific decision's DFA // But, do we still need an initial state? - try { - 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 s0_closure = computeStartState(dynamic_cast(dfa->atnStartState), - ParserRuleContext::EMPTY, fullCtx); - dfa->s0 = addDFAState(dfa, new dfa::DFAState(s0_closure)); - } + auto onExit = finally([&] { + mergeCache.clear(); // wack cache after each prediction + input->seek(index); + input->release(m); + }); - // 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; + if (dfa.s0 == nullptr) { + if (outerContext == nullptr) { + outerContext = std::dynamic_pointer_cast(RuleContext::EMPTY); } - return alt; - } - catch (std::exception &e) { - // Do nothing, failed predict + 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 s0_closure = computeStartState(dynamic_cast(dfa.atnStartState), + ParserRuleContext::EMPTY, fullCtx); + dfa.s0 = addDFAState(dfa, new dfa::DFAState(s0_closure)); } - mergeCache.clear(); // wack cache after each prediction - input->seek(index); - input->release(m); - - return 0; + // 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, - ParserRuleContextRef outerContext) { +int ParserATNSimulator::execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex, + ParserRuleContext::Ref outerContext) { 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; } @@ -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; } bool fullCtx = true; - std::shared_ptr s0_closure = computeStartState(dfa->atnStartState, outerContext, fullCtx); + std::shared_ptr s0_closure = computeStartState(dfa.atnStartState, outerContext, fullCtx); reportAttemptingFullContext(dfa, conflictingAlts, D->configs, startIndex, input->index()); int alt = execATNWithFullContext(dfa, D, s0_closure, input, startIndex, outerContext); return alt; @@ -240,7 +235,7 @@ dfa::DFAState *ParserATNSimulator::getExistingTargetState(dfa::DFAState *previou 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 reach = computeReachSet(previousD->configs, t, false); if (reach == nullptr) { 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) { - predicateDFAState(D, atn.getDecisionState(dfa->decision)); + predicateDFAState(D, atn.getDecisionState(dfa.decision)); if (D->predicates.size() != 0) { 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) // pairs if preds found for conflicting alts BitSet altsToCollectPredsFrom = getConflictingAltsOrUniqueAlt(dfaState->configs); - std::vector altToPred = getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState->configs, nalts); + std::vector altToPred = getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState->configs, nalts); if (!altToPred.empty()) { dfaState->predicates = getPredicatePredictions(altsToCollectPredsFrom, altToPred); 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 s0, - TokenStream *input, size_t startIndex, ParserRuleContextRef outerContext) { +int ParserATNSimulator::execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, std::shared_ptr s0, + TokenStream *input, size_t startIndex, ParserRuleContext::Ref outerContext) { if (debug || debug_list_atn_decisions) { std::cout << "execATNWithFullContext " << s0 << std::endl; } @@ -448,7 +443,7 @@ std::shared_ptr ParserATNSimulator::computeReachSet(std::shared_pt std::wcout << L"testing " << getTokenName(t) << L" at " << c->toString() << std::endl; } - if (dynamic_cast(c->state) != nullptr) { + if (is(c->state)) { assert(c->context->isEmpty()); if (fullCtx || t == EOF) { @@ -579,9 +574,9 @@ std::shared_ptr ParserATNSimulator::removeAllConfigsNotInRuleStopS return result; } -std::shared_ptr ParserATNSimulator::computeStartState(ATNState *p, RuleContextRef ctx, bool fullCtx) { +std::shared_ptr ParserATNSimulator::computeStartState(ATNState *p, RuleContext::Ref ctx, bool fullCtx) { // 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 configs = std::make_shared(fullCtx); for (size_t i = 0; i < p->getNumberOfTransitions(); i++) { @@ -595,7 +590,7 @@ std::shared_ptr ParserATNSimulator::computeStartState(ATNState *p, } 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; } @@ -603,7 +598,7 @@ atn::ATNState *ParserATNSimulator::getReachableTarget(Transition *trans, int tty } // Note that caller must memory manage the returned value from this function -std::vector ParserATNSimulator::getPredsForAmbigAlts(const BitSet &ambigAlts, +std::vector ParserATNSimulator::getPredsForAmbigAlts(const BitSet &ambigAlts, std::shared_ptr configs, size_t nalts) { // REACH=[1|1|[]|0:0, 1|2|[]|0:1] /* altToPred starts as an array of all null contexts. The entry at index i @@ -617,13 +612,13 @@ std::vector ParserATNSimulator::getPredsForAmbigAlts(const B * * From this, it is clear that NONE||anything==NONE. */ - std::vector altToPred; + std::vector altToPred; for (auto c : configs->configs) { if (ambigAlts.test((size_t)c->alt)) { if (altToPred.size() <= (size_t)c->alt) altToPred.resize((size_t)c->alt + 1); - altToPred[(size_t)c->alt] = std::make_shared(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 ParserATNSimulator::getPredsForAmbigAlts(const B } std::vector ParserATNSimulator::getPredicatePredictions(const antlrcpp::BitSet &ambigAlts, - std::vector altToPred) { + std::vector altToPred) { std::vector pairs; bool containsPredicate = false; for (size_t i = 1; i < altToPred.size(); i++) { - SemanticContextRef pred = altToPred[i]; + SemanticContext::Ref pred = altToPred[i]; // unpredicted is indicated by SemanticContext.NONE assert(pred != nullptr); @@ -672,40 +667,40 @@ std::vector ParserATNSimulator::getPredicatePre } int ParserATNSimulator::getAltThatFinishedDecisionEntryRule(std::shared_ptr configs) { - misc::IntervalSet *alts = nullptr; + misc::IntervalSet alts; for (auto c : configs->configs) { - if (c->reachesIntoOuterContext > 0 || (dynamic_cast(c->state) != nullptr && c->context->hasEmptyPath())) { - alts->add(c->alt); + if (c->reachesIntoOuterContext > 0 || (is(c->state) && c->context->hasEmptyPath())) { + alts.add(c->alt); } } - if (alts->size() == 0) { + if (alts.size() == 0) { return ATN::INVALID_ALT_NUMBER; } - return alts->getMinElement(); + return alts.getMinElement(); } BitSet ParserATNSimulator::evalSemanticContext(std::vector predPredictions, - ParserRuleContextRef outerContext, bool complete) { + ParserRuleContext::Ref outerContext, bool complete) { BitSet predictions; - for (auto pair : predPredictions) { - if (pair->pred == SemanticContext::NONE) { - predictions.set((size_t)pair->alt); + for (auto prediction : predPredictions) { + if (prediction->pred == SemanticContext::NONE) { + predictions.set((size_t)prediction->alt); if (!complete) { break; } continue; } - bool predicateEvaluationResult = pair->pred->eval(parser, outerContext); + bool predicateEvaluationResult = prediction->pred->eval(parser, outerContext); 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 (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) { break; } @@ -941,7 +936,7 @@ atn::ATNConfig *ParserATNSimulator::ruleTransition(ATNConfig *config, RuleTransi } 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); } @@ -980,9 +975,9 @@ std::wstring ParserATNSimulator::getLookaheadName(TokenStream *input) { return getTokenName(input->LA(1)); } -void ParserATNSimulator::dumpDeadEndConfigs(NoViableAltException *nvae) { +void ParserATNSimulator::dumpDeadEndConfigs(NoViableAltException &nvae) { std::wcerr << L"dead end configs: "; - for (auto c : nvae->getDeadEndConfigs()->configs) { + for (auto c : nvae.getDeadEndConfigs()->configs) { std::wstring trans = L"no edges"; if (c->state->getNumberOfTransitions() > 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 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 configs) { @@ -1018,7 +1013,7 @@ int ParserATNSimulator::getUniqueAlt(std::shared_ptr configs) { 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) { 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 - if (from == nullptr || t > atn.maxTokenType) { + if (from == nullptr || t > (int)atn.maxTokenType) { return to; } - if (true) { + { std::lock_guard lck(mtx); - if (from->edges.size() == 0) { - from->edges = std::vector(); - } - + if (from->edges.empty()) + from->edges.resize(atn.maxTokenType + 1 + 1); 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) { 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; } -dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA *dfa, dfa::DFAState *D) { +dfa::DFAState *ParserATNSimulator::addDFAState(dfa::DFA &dfa, dfa::DFAState *D) { if (D == ERROR.get()) { return D; } - if (true) { + { std::lock_guard lck(mtx); - dfa::DFAState *existing = dfa->states->at(D); - if (existing != nullptr) { - return existing; + + auto existing = dfa.states.find(D); + if (existing != dfa.states.end()) { + return existing->second; } - D->stateNumber = (int)dfa->states->size(); + D->stateNumber = (int)dfa.states.size(); if (!D->configs->isReadonly()) { D->configs->optimizeConfigs(this); D->configs->setReadonly(true); } - dfa->states->insert(std::pair(D, D)); + dfa.states[D] = D; if (debug) { 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 configs, size_t startIndex, size_t stopIndex) { if (debug || retry_debug) { 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) { - 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 configs, +void ParserATNSimulator::reportContextSensitivity(dfa::DFA &dfa, int prediction, std::shared_ptr configs, size_t startIndex, size_t stopIndex) { if (debug || retry_debug) { 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) { - 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 configs) { if (debug || retry_debug) { // ParserATNPathFinder finder = new ParserATNPathFinder(parser, atn); @@ -1119,11 +1113,11 @@ void ParserATNSimulator::reportAmbiguity(dfa::DFA *dfa, dfa::DFAState *D, size_t // } // 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; } if (parser != nullptr) { - parser->getErrorListenerDispatch()->reportAmbiguity(parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs); + parser->getErrorListenerDispatch().reportAmbiguity(parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs); } } diff --git a/runtime/Cpp/runtime/atn/ParserATNSimulator.h b/runtime/Cpp/runtime/atn/ParserATNSimulator.h index ab81e04e9..ce5bc1fba 100755 --- a/runtime/Cpp/runtime/atn/ParserATNSimulator.h +++ b/runtime/Cpp/runtime/atn/ParserATNSimulator.h @@ -252,7 +252,7 @@ namespace atn { Parser *const parser; public: - const std::vector &_decisionToDFA; + std::vector &_decisionToDFA; /// /// SLL, LL, or LL + exact ambig detection? @@ -276,20 +276,20 @@ namespace atn { // LAME globals to avoid parameters!!!!! I need these down deep in predTransition TokenStream *_input; int _startIndex; - ParserRuleContextRef _outerContext; + ParserRuleContext::Ref _outerContext; /// /// Testing only! public: - ParserATNSimulator(const ATN &atn, const std::vector& decisionToDFA, + ParserATNSimulator(const ATN &atn, std::vector& decisionToDFA, std::shared_ptr sharedContextCache); - ParserATNSimulator(Parser *parser, const ATN &atn, const std::vector &decisionToDFA, + ParserATNSimulator(Parser *parser, const ATN &atn, std::vector &decisionToDFA, std::shared_ptr sharedContextCache); virtual void reset() override; - virtual int adaptivePredict(TokenStream *input, int decision, ParserRuleContextRef outerContext); + virtual int adaptivePredict(TokenStream *input, int decision, ParserRuleContext::Ref outerContext); /// /// Performs ATN simulation to compute a predicted alternative based @@ -323,8 +323,8 @@ namespace atn { /// conflict + preds /// protected: - virtual int execATN(dfa::DFA *dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex, - ParserRuleContextRef outerContext); + virtual int execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex, + ParserRuleContext::Ref outerContext); /// /// Get an existing target state for an edge in the DFA. If the target state @@ -349,13 +349,13 @@ namespace atn { /// 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 /// 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); // comes back with reach.uniqueAlt set to a valid alt - virtual int execATNWithFullContext(dfa::DFA *dfa, dfa::DFAState *D, std::shared_ptr s0, - TokenStream *input, size_t startIndex, ParserRuleContextRef outerContext); // how far we got before failing over + virtual int execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, std::shared_ptr s0, + TokenStream *input, size_t startIndex, ParserRuleContext::Ref outerContext); // how far we got before failing over virtual std::shared_ptr computeReachSet(std::shared_ptr closure, ssize_t t, bool fullCtx); @@ -380,15 +380,15 @@ namespace atn { /// the configurations from {@code configs} which are in a rule stop state virtual std::shared_ptr removeAllConfigsNotInRuleStopState(std::shared_ptr configs, bool lookToEndOfRule); - virtual std::shared_ptr computeStartState(ATNState *p, RuleContextRef ctx, bool fullCtx); + virtual std::shared_ptr computeStartState(ATNState *p, RuleContext::Ref ctx, bool fullCtx); virtual ATNState *getReachableTarget(Transition *trans, int ttype); - virtual std::vector getPredsForAmbigAlts(const antlrcpp::BitSet &ambigAlts, + virtual std::vector getPredsForAmbigAlts(const antlrcpp::BitSet &ambigAlts, std::shared_ptr configs, size_t nalts); virtual std::vector getPredicatePredictions(const antlrcpp::BitSet &ambigAlts, - std::vector altToPred); + std::vector altToPred); virtual int getAltThatFinishedDecisionEntryRule(std::shared_ptr configs); @@ -400,7 +400,7 @@ namespace atn { /// includes pairs with null predicates. /// virtual antlrcpp::BitSet evalSemanticContext(std::vector predPredictions, - ParserRuleContextRef outerContext, bool complete); + ParserRuleContext::Ref outerContext, bool complete); /* 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 /// "dead" code for a bit. /// - virtual void dumpDeadEndConfigs(NoViableAltException *nvae); + virtual void dumpDeadEndConfigs(NoViableAltException &nvae); protected: - virtual NoViableAltException *noViableAlt(TokenStream *input, ParserRuleContextRef outerContext, + virtual NoViableAltException noViableAlt(TokenStream *input, ParserRuleContext::Ref outerContext, std::shared_ptr configs, size_t startIndex); static int getUniqueAlt(std::shared_ptr configs); @@ -513,7 +513,7 @@ namespace atn { /// If {@code to} is {@code null}, this method returns {@code null}; /// otherwise this method returns the result of calling /// on {@code to} - 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); /// /// Add state {@code D} to the DFA if it is not already present, and return @@ -529,17 +529,17 @@ namespace atn { /// 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 was not already present. - 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 configs, size_t startIndex, size_t stopIndex); - virtual void reportContextSensitivity(dfa::DFA *dfa, int prediction, std::shared_ptr configs, + virtual void reportContextSensitivity(dfa::DFA &dfa, int prediction, std::shared_ptr configs, size_t startIndex, size_t stopIndex); /// /// If context sensitive parsing, we know it's ambiguity not conflict - 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 configs); public: diff --git a/runtime/Cpp/runtime/atn/PrecedencePredicateTransition.cpp b/runtime/Cpp/runtime/atn/PrecedencePredicateTransition.cpp index 0a75ea3e7..f9b17fe2f 100755 --- a/runtime/Cpp/runtime/atn/PrecedencePredicateTransition.cpp +++ b/runtime/Cpp/runtime/atn/PrecedencePredicateTransition.cpp @@ -53,5 +53,5 @@ std::shared_ptr PrecedencePredicateTransit } 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" }"; } diff --git a/runtime/Cpp/runtime/atn/PrecedencePredicateTransition.h b/runtime/Cpp/runtime/atn/PrecedencePredicateTransition.h index 36530eb2d..d10c8c8d4 100755 --- a/runtime/Cpp/runtime/atn/PrecedencePredicateTransition.h +++ b/runtime/Cpp/runtime/atn/PrecedencePredicateTransition.h @@ -50,7 +50,7 @@ namespace atn { virtual bool isEpsilon() const override; virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; std::shared_ptr getPredicate() const; - virtual std::wstring toString() const; + virtual std::wstring toString() const override; }; diff --git a/runtime/Cpp/runtime/atn/PredicateTransition.cpp b/runtime/Cpp/runtime/atn/PredicateTransition.cpp index 1b00f2824..55c5c39cf 100755 --- a/runtime/Cpp/runtime/atn/PredicateTransition.cpp +++ b/runtime/Cpp/runtime/atn/PredicateTransition.cpp @@ -53,5 +53,8 @@ std::shared_ptr PredicateTransition::getPredicate() } 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? } diff --git a/runtime/Cpp/runtime/atn/PredicateTransition.h b/runtime/Cpp/runtime/atn/PredicateTransition.h index 8312827e7..1f03b013f 100755 --- a/runtime/Cpp/runtime/atn/PredicateTransition.h +++ b/runtime/Cpp/runtime/atn/PredicateTransition.h @@ -60,7 +60,7 @@ namespace atn { std::shared_ptr getPredicate() const; - virtual std::wstring toString() const; + virtual std::wstring toString() const override; }; diff --git a/runtime/Cpp/runtime/atn/PredictionContext.cpp b/runtime/Cpp/runtime/atn/PredictionContext.cpp index 3b95bb4b5..f7caf6285 100755 --- a/runtime/Cpp/runtime/atn/PredictionContext.cpp +++ b/runtime/Cpp/runtime/atn/PredictionContext.cpp @@ -53,9 +53,9 @@ const int PredictionContext::INITIAL_HASH; 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) { - outerContext = std::dynamic_pointer_cast(RuleContext::EMPTY); + outerContext = RuleContext::EMPTY; } // 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 - 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); return SingletonPredictionContext::create(parent, transition->followState->stateNumber); } @@ -117,8 +117,8 @@ size_t PredictionContext::calculateHashCode(const std::vector(b), rootIsWildcard, mergeCache); } -PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionContextRef a, - SingletonPredictionContextRef b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) { +PredictionContext::Ref PredictionContext::mergeSingletons(SingletonPredictionContext::Ref a, + SingletonPredictionContext::Ref b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) { 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() }); @@ -168,7 +168,7 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte } } - PredictionContextRef rootMerge = mergeRoot(a, b, rootIsWildcard); + PredictionContext::Ref rootMerge = mergeRoot(a, b, rootIsWildcard); if (rootMerge) { if (mergeCache != nullptr) { (*mergeCache)[{ a.get(), b.get() }] = rootMerge; @@ -176,10 +176,10 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte return rootMerge; } - PredictionContextRef parentA = a->parent.lock(); - PredictionContextRef parentB = b->parent.lock(); + PredictionContext::Ref parentA = a->parent.lock(); + PredictionContext::Ref parentB = b->parent.lock(); 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 == 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 // of those graphs. dup a, a' points at merged array // 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) { (*mergeCache)[{ a.get(), b.get() }] = a_; } @@ -212,7 +212,7 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte payloads[1] = a->returnState; } std::vector> parents = { singleParent, singleParent }; - PredictionContextRef a_(new ArrayPredictionContext(parents, payloads)); + PredictionContext::Ref a_(new ArrayPredictionContext(parents, payloads)); if (mergeCache != nullptr) { (*mergeCache)[{ a.get(), b.get() }] = a_; } @@ -222,7 +222,7 @@ PredictionContextRef PredictionContext::mergeSingletons(SingletonPredictionConte // parents differ and can't merge them. Just pack together // into array; can't merge. // ax + by = [ax,by] - PredictionContextRef a_; + PredictionContext::Ref a_; if (a->returnState > b->returnState) { // sort by payload std::vector payloads = { b->returnState, a->returnState }; std::vector> 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) { if (rootIsWildcard) { if (a == EMPTY) { // * + b = * @@ -256,20 +256,20 @@ PredictionContextRef PredictionContext::mergeRoot(SingletonPredictionContextRef if (a == EMPTY) { // $ + x = [$,x] std::vector payloads = { b->returnState, EMPTY_RETURN_STATE }; std::vector> parents = { b->parent, EMPTY }; - PredictionContextRef joined(new ArrayPredictionContext(parents, payloads)); + PredictionContext::Ref joined(new ArrayPredictionContext(parents, payloads)); return joined; } if (b == EMPTY) { // x + $ = [$,x] ($ is always first if present) std::vector payloads = { a->returnState, EMPTY_RETURN_STATE }; std::vector> parents = { a->parent, EMPTY }; - PredictionContextRef joined(new ArrayPredictionContext(parents, payloads)); + PredictionContext::Ref joined(new ArrayPredictionContext(parents, payloads)); return joined; } } return nullptr; } -PredictionContextRef PredictionContext::mergeArrays(std::shared_ptr a, +PredictionContext::Ref PredictionContext::mergeArrays(std::shared_ptr a, std::shared_ptr b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache) { if (mergeCache != nullptr) { @@ -295,8 +295,8 @@ PredictionContextRef PredictionContext::mergeArrays(std::shared_ptrreturnStates.size() && j < b->returnStates.size()) { - PredictionContextRef a_parent = a->parents[i].lock(); - PredictionContextRef b_parent = b->parents[j].lock(); + PredictionContext::Ref a_parent = a->parents[i].lock(); + PredictionContext::Ref b_parent = b->parents[j].lock(); if (a->returnStates[i] == b->returnStates[j]) { // same payload (stack tops are equal), must yield merged singleton int payload = a->returnStates[i]; @@ -308,7 +308,7 @@ PredictionContextRef PredictionContext::mergeArrays(std::shared_ptr 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; mergedReturnStates[k] = payload; } @@ -345,7 +345,7 @@ PredictionContextRef PredictionContext::mergeArrays(std::shared_ptr> &parents) { - std::set uniqueParents; + std::set uniqueParents; 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. if (uniqueParents.find(parent) == uniqueParents.end()) { // don't replace uniqueParents.insert(parent); @@ -400,7 +400,7 @@ bool PredictionContext::combineCommonParents(std::vector nodes = getAllContextNodes(context); - std::sort(nodes.begin(), nodes.end(), [](PredictionContextRef o1, PredictionContextRef o2) { + std::vector nodes = getAllContextNodes(context); + std::sort(nodes.begin(), nodes.end(), [](PredictionContext::Ref o1, PredictionContext::Ref o2) { 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). -PredictionContextRef PredictionContext::getCachedContext(PredictionContextRef context, - std::shared_ptr contextCache, std::map &visited) { +PredictionContext::Ref PredictionContext::getCachedContext(PredictionContext::Ref context, + std::shared_ptr contextCache, std::map &visited) { if (context->isEmpty()) { return context; } @@ -510,7 +510,7 @@ PredictionContextRef PredictionContext::getCachedContext(PredictionContextRef co return context; } - PredictionContextRef updated; + PredictionContext::Ref updated; if (parents.empty()) { updated = EMPTY; } else if (parents.size() == 1) { @@ -526,17 +526,17 @@ PredictionContextRef PredictionContext::getCachedContext(PredictionContextRef co return updated; } -std::vector PredictionContext::getAllContextNodes(PredictionContextRef context) { - std::vector nodes; - std::map visited; +std::vector PredictionContext::getAllContextNodes(PredictionContext::Ref context) { + std::vector nodes; + std::map visited; getAllContextNodes_(context, nodes, visited); return nodes; } -void PredictionContext::getAllContextNodes_(PredictionContextRef context, - std::vector &nodes, - std::map &visited) { +void PredictionContext::getAllContextNodes_(PredictionContext::Ref context, + std::vector &nodes, + std::map &visited) { if (visited.find(context) != visited.end()) { return; // Already done. @@ -563,7 +563,7 @@ std::vector PredictionContext::toStrings(Recognizer *recognizer, i return toStrings(recognizer, EMPTY, currentState); } -std::vector PredictionContext::toStrings(Recognizer *recognizer, PredictionContextRef stop, int currentState) { +std::vector PredictionContext::toStrings(Recognizer *recognizer, PredictionContext::Ref stop, int currentState) { std::vector result; diff --git a/runtime/Cpp/runtime/atn/PredictionContext.h b/runtime/Cpp/runtime/atn/PredictionContext.h index f6da62fa3..c44e4c0b8 100755 --- a/runtime/Cpp/runtime/atn/PredictionContext.h +++ b/runtime/Cpp/runtime/atn/PredictionContext.h @@ -41,13 +41,16 @@ namespace v4 { namespace runtime { namespace atn { - typedef std::set PredictionContextCache; + // Cannot use PredictionContext::Ref here as this declared below first. + typedef std::set> PredictionContextCache; // For the keys we use raw pointers, as we don't need to access them. - typedef std::map, PredictionContextRef> PredictionContextMergeCache; + typedef std::map, std::shared_ptr> PredictionContextMergeCache; class PredictionContext { public: + typedef std::shared_ptr Ref; + /// Represents $ in local context prediction, which means wildcard. /// *+x = *. static const std::shared_ptr EMPTY; @@ -93,7 +96,7 @@ namespace atn { public: /// Convert a RuleContext tree to a PredictionContext graph. /// 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 std::weak_ptr getParent(size_t index) const = 0; @@ -114,8 +117,8 @@ namespace atn { public: // dispatch - static PredictionContextRef merge(PredictionContextRef a, - PredictionContextRef b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache); + static PredictionContext::Ref merge(PredictionContext::Ref a, + PredictionContext::Ref b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache); /// /// Merge two instances. @@ -151,8 +154,8 @@ namespace atn { /// {@code true} if this is a local-context merge, /// otherwise false to indicate a full-context merge /// - static PredictionContextRef mergeSingletons(SingletonPredictionContextRef a, SingletonPredictionContextRef b, - bool rootIsWildcard, PredictionContextMergeCache *mergeCache); + static PredictionContext::Ref mergeSingletons(std::shared_ptr a, + std::shared_ptr b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache); /// /// Handle case where at least one of {@code a} or {@code b} is @@ -203,8 +206,8 @@ namespace atn { /// the second /// {@code true} if this is a local-context merge, /// otherwise false to indicate a full-context merge - static PredictionContextRef mergeRoot(SingletonPredictionContextRef a, SingletonPredictionContextRef b, - bool rootIsWildcard); + static PredictionContext::Ref mergeRoot(std::shared_ptr a, + std::shared_ptr b, bool rootIsWildcard); /// /// Merge two instances. @@ -235,7 +238,7 @@ namespace atn { /// .
/// ///
- static PredictionContextRef mergeArrays(std::shared_ptr a, + static PredictionContext::Ref mergeArrays(std::shared_ptr a, std::shared_ptr b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache); protected: @@ -244,22 +247,22 @@ namespace atn { static bool combineCommonParents(std::vector> &parents); 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 contextCache, - std::map &visited); + std::map &visited); // ter's recursive version of Sam's getAllNodes() - static std::vector getAllContextNodes(PredictionContextRef context); - static void getAllContextNodes_(PredictionContextRef context, - std::vector &nodes, std::map &visited); + static std::vector getAllContextNodes(PredictionContext::Ref context); + static void getAllContextNodes_(PredictionContext::Ref context, + std::vector &nodes, std::map &visited); virtual std::wstring toString() const; virtual std::wstring toString(Recognizer *recog) const; std::vector toStrings(Recognizer *recognizer, int currentState); - std::vector toStrings(Recognizer *recognizer, PredictionContextRef stop, int currentState); + std::vector toStrings(Recognizer *recognizer, PredictionContext::Ref stop, int currentState); }; } // namespace atn diff --git a/runtime/Cpp/runtime/atn/RangeTransition.cpp b/runtime/Cpp/runtime/atn/RangeTransition.cpp index 4713e5ae2..25dee0018 100755 --- a/runtime/Cpp/runtime/atn/RangeTransition.cpp +++ b/runtime/Cpp/runtime/atn/RangeTransition.cpp @@ -52,5 +52,6 @@ bool RangeTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol } std::wstring RangeTransition::toString() const { - return std::wstring(L"'") + static_cast(from) + std::wstring(L"'..'") + static_cast(to) + std::wstring(L"'"); + return L"RANGE " + Transition::toString() + L" { from: " + std::to_wstring(from) + L", to: " + std::to_wstring(to) + + L" }"; } diff --git a/runtime/Cpp/runtime/atn/RangeTransition.h b/runtime/Cpp/runtime/atn/RangeTransition.h index 630f7babf..79789fe30 100755 --- a/runtime/Cpp/runtime/atn/RangeTransition.h +++ b/runtime/Cpp/runtime/atn/RangeTransition.h @@ -51,7 +51,7 @@ namespace atn { virtual misc::IntervalSet label() 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 diff --git a/runtime/Cpp/runtime/atn/RuleTransition.cpp b/runtime/Cpp/runtime/atn/RuleTransition.cpp index b4bfcec15..7ac36d680 100755 --- a/runtime/Cpp/runtime/atn/RuleTransition.cpp +++ b/runtime/Cpp/runtime/atn/RuleTransition.cpp @@ -29,15 +29,17 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "RuleStartState.h" #include "RuleTransition.h" using namespace org::antlr::v4::runtime::atn; -// this(ruleStart, ruleIndex, 0, followState); -RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, ATNState *followState) : Transition(followState), ruleIndex(ruleIndex), precedence(0) { +RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, ATNState *followState) + : 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; } @@ -52,3 +54,10 @@ bool RuleTransition::isEpsilon() const { bool RuleTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const { 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(); +} diff --git a/runtime/Cpp/runtime/atn/RuleTransition.h b/runtime/Cpp/runtime/atn/RuleTransition.h index 29b6580a5..79f724c88 100755 --- a/runtime/Cpp/runtime/atn/RuleTransition.h +++ b/runtime/Cpp/runtime/atn/RuleTransition.h @@ -39,7 +39,7 @@ namespace v4 { namespace runtime { namespace atn { - class RuleTransition final : public Transition { + class RuleTransition : public Transition { /// /// Ptr to the rule definition object for this rule ref public: @@ -61,6 +61,8 @@ namespace atn { virtual bool isEpsilon() const override; virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; + + virtual std::wstring toString() const override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/atn/SemanticContext.cpp b/runtime/Cpp/runtime/atn/SemanticContext.cpp index 79b872032..d6041ee10 100755 --- a/runtime/Cpp/runtime/atn/SemanticContext.cpp +++ b/runtime/Cpp/runtime/atn/SemanticContext.cpp @@ -54,11 +54,14 @@ size_t SemanticContext::Predicate::hashCode() { } bool SemanticContext::Predicate::operator == (const SemanticContext &other) const { - if (this == &other) { + const Predicate *p = dynamic_cast(&other); + if (p == nullptr) + return false; + + if (this == p) { return true; } - const Predicate *p = dynamic_cast(&other); if (p == nullptr) return false; @@ -86,11 +89,14 @@ size_t SemanticContext::PrecedencePredicate::hashCode() { } bool SemanticContext::PrecedencePredicate::operator == (const SemanticContext &other) const { - if (this == &other) { + const PrecedencePredicate *predicate = dynamic_cast(&other); + if (predicate == nullptr) + return false; + + if (this == predicate) { return true; } - const PrecedencePredicate *predicate = dynamic_cast(&other); return precedence == predicate->precedence; } @@ -99,10 +105,10 @@ std::wstring SemanticContext::PrecedencePredicate::toString() const { } -SemanticContext::AND::AND(SemanticContextRef a, SemanticContextRef b) { +SemanticContext::AND::AND(SemanticContext::Ref a, SemanticContext::Ref b) { if (is(a)) { - const std::vector op = ((AND*)a.get())->opnds; + const std::vector op = ((AND*)a.get())->opnds; for (auto var : op) { opnds.push_back(var); } @@ -111,7 +117,7 @@ SemanticContext::AND::AND(SemanticContextRef a, SemanticContextRef b) { } if (is(b)) { - const std::vector op = ((AND*)b.get())->opnds; + const std::vector op = ((AND*)b.get())->opnds; for (auto var : op) { opnds.push_back(var); } @@ -133,11 +139,14 @@ SemanticContext::AND::AND(SemanticContextRef a, SemanticContextRef b) { } bool SemanticContext::AND::operator == (const SemanticContext &other) const { - if (this == &other) { + const AND *context = dynamic_cast(&other); + if (context == nullptr) + return false; + + if (this == context) { return true; } - const AND *context = dynamic_cast(&other); return opnds == context->opnds; } @@ -156,10 +165,10 @@ std::wstring SemanticContext::AND::toString() const { return tmp; } -SemanticContext::OR::OR(SemanticContextRef a, SemanticContextRef b){ +SemanticContext::OR::OR(SemanticContext::Ref a, SemanticContext::Ref b){ if (is(a)) { - const std::vector op = ((OR*)a.get())->opnds; + const std::vector op = ((OR*)a.get())->opnds; for (auto var : op) { opnds.push_back(var); } @@ -168,7 +177,7 @@ SemanticContext::OR::OR(SemanticContextRef a, SemanticContextRef b){ } if (is(b)) { - const std::vector op = ((OR*)b.get())->opnds; + const std::vector op = ((OR*)b.get())->opnds; for (auto var : op) { opnds.push_back(var); } @@ -188,12 +197,14 @@ SemanticContext::OR::OR(SemanticContextRef a, SemanticContextRef b){ } bool SemanticContext::OR::operator == (const SemanticContext &other) const { - if (this == &other) { + const OR *context = dynamic_cast(&other); + if (context == nullptr) + return false; + + if (this == context) { return true; } - const OR *context = dynamic_cast(&other); - return opnds == context->opnds; } @@ -201,6 +212,14 @@ size_t SemanticContext::OR::hashCode() { return misc::MurmurHash::hashCode(opnds, typeid(OR).hash_code()); } +bool SemanticContext::OR::eval(Recognizer *parser, RuleContext::Ref outerContext) { + for (auto opnd : opnds) { + if (opnd->eval(parser, outerContext)) { + return true; + } + } + return false; +} std::wstring SemanticContext::OR::toString() const { std::wstring tmp; @@ -210,9 +229,9 @@ std::wstring SemanticContext::OR::toString() const { return tmp; } -const SemanticContextRef SemanticContext::NONE = std::make_shared(-1, -1, false); +const SemanticContext::Ref SemanticContext::NONE = std::make_shared(-1, -1, false); -SemanticContextRef SemanticContext::And(SemanticContextRef a, SemanticContextRef b) { +SemanticContext::Ref SemanticContext::And(SemanticContext::Ref a, SemanticContext::Ref b) { if (!a || a == NONE) { return b; } @@ -229,7 +248,7 @@ SemanticContextRef SemanticContext::And(SemanticContextRef a, SemanticContextRef return result; } -SemanticContextRef SemanticContext::Or(SemanticContextRef a, SemanticContextRef b) { +SemanticContext::Ref SemanticContext::Or(SemanticContext::Ref a, SemanticContext::Ref b) { if (!a) { return b; } diff --git a/runtime/Cpp/runtime/atn/SemanticContext.h b/runtime/Cpp/runtime/atn/SemanticContext.h index e3877cf78..fe462855b 100755 --- a/runtime/Cpp/runtime/atn/SemanticContext.h +++ b/runtime/Cpp/runtime/atn/SemanticContext.h @@ -48,8 +48,9 @@ namespace atn { /// SemanticContext within the scope of this outer class. class SemanticContext { public: - //SemanticContext *parent; - static const SemanticContextRef NONE; + typedef std::shared_ptr Ref; + + static const SemanticContext::Ref NONE; virtual size_t hashCode() = 0; virtual std::wstring toString() const = 0; @@ -68,12 +69,12 @@ namespace atn { /// prediction, so we passed in the outer context here in case of context /// dependent predicate evaluation. ///
- virtual bool eval(Recognizer *parser, RuleContextRef outerContext) = 0; + virtual bool eval(Recognizer *parser, RuleContext::Ref outerContext) = 0; - static SemanticContextRef And(SemanticContextRef a, SemanticContextRef b); + static SemanticContext::Ref And(SemanticContext::Ref a, SemanticContext::Ref b); /// See also: ParserATNSimulator::getPredsForAmbigAlts. - static SemanticContextRef Or(SemanticContextRef a, SemanticContextRef b); + static SemanticContext::Ref Or(SemanticContext::Ref a, SemanticContext::Ref b); class Predicate; class PrecedencePredicate; @@ -81,7 +82,7 @@ namespace atn { class OR; private: - template // where T1 : SemanticContextRef + template // where T1 : SemanticContext::Ref static std::vector> filterPrecedencePredicates(const std::vector &collection) { std::vector> result; for (auto context : collection) { @@ -110,8 +111,8 @@ namespace atn { public: Predicate(int ruleIndex, int predIndex, bool isCtxDependent); - virtual bool eval(Recognizer *parser, RuleContextRef outerContext) override { - RuleContextRef localctx; + virtual bool eval(Recognizer *parser, RuleContext::Ref outerContext) override { + RuleContext::Ref localctx; if (isCtxDependent) localctx = outerContext; return parser->sempred(localctx, ruleIndex, predIndex); @@ -132,7 +133,7 @@ namespace atn { public: PrecedencePredicate(int precedence); - virtual bool eval(Recognizer *parser, RuleContextRef outerContext) override { + virtual bool eval(Recognizer *parser, RuleContext::Ref outerContext) override { return parser->precpred(outerContext, precedence); } @@ -144,14 +145,14 @@ namespace atn { class SemanticContext::AND : public SemanticContext { public: - std::vector opnds; + std::vector opnds; - AND(SemanticContextRef a, SemanticContextRef b); + AND(SemanticContext::Ref a, SemanticContext::Ref b); virtual bool operator == (const SemanticContext &other) const override; virtual size_t hashCode() override; - virtual bool eval(Recognizer *parser, RuleContextRef outerContext) override { + virtual bool eval(Recognizer *parser, RuleContext::Ref outerContext) override { for (auto opnd : opnds) { if (!opnd->eval(parser, outerContext)) { return false; @@ -166,22 +167,13 @@ namespace atn { class SemanticContext::OR : public SemanticContext { public: - std::vector opnds; + std::vector opnds; - OR(SemanticContextRef a, SemanticContextRef b); + OR(SemanticContext::Ref a, SemanticContext::Ref b); virtual bool operator == (const SemanticContext &other) const override; virtual size_t hashCode() override; - - virtual bool eval(Recognizer *parser, RuleContextRef outerContext) override { - for (auto opnd : opnds) { - if (opnd->eval(parser, outerContext)) { - return true; - } - } - return false; - } - + virtual bool eval(Recognizer *parser, RuleContext::Ref outerContext) override; virtual std::wstring toString() const override; }; diff --git a/runtime/Cpp/runtime/atn/SetTransition.cpp b/runtime/Cpp/runtime/atn/SetTransition.cpp index 086410729..413fe7bec 100755 --- a/runtime/Cpp/runtime/atn/SetTransition.cpp +++ b/runtime/Cpp/runtime/atn/SetTransition.cpp @@ -54,5 +54,5 @@ bool SetTransition::matches(int symbol, int minVocabSymbol, int maxVocabSymbol) } std::wstring SetTransition::toString() const { - return set.toString(); + return L"SET " + Transition::toString() + L" { set: " + set.toString() + L"}"; } diff --git a/runtime/Cpp/runtime/atn/SetTransition.h b/runtime/Cpp/runtime/atn/SetTransition.h index 356801ea3..bf6cec0ad 100755 --- a/runtime/Cpp/runtime/atn/SetTransition.h +++ b/runtime/Cpp/runtime/atn/SetTransition.h @@ -52,7 +52,7 @@ namespace atn { virtual misc::IntervalSet label() 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 diff --git a/runtime/Cpp/runtime/atn/SingletonPredictionContext.cpp b/runtime/Cpp/runtime/atn/SingletonPredictionContext.cpp index 05981f812..2ef8b7c83 100755 --- a/runtime/Cpp/runtime/atn/SingletonPredictionContext.cpp +++ b/runtime/Cpp/runtime/atn/SingletonPredictionContext.cpp @@ -41,7 +41,7 @@ SingletonPredictionContext::SingletonPredictionContext(std::weak_ptr parent, int returnState) { +SingletonPredictionContext::Ref SingletonPredictionContext::create(std::weak_ptr parent, int returnState) { if (returnState == EMPTY_RETURN_STATE && parent.expired()) { // someone can pass in the bits of an array ctx that mean $ return EMPTY; diff --git a/runtime/Cpp/runtime/atn/SingletonPredictionContext.h b/runtime/Cpp/runtime/atn/SingletonPredictionContext.h index 8bc0d1ef2..8c62a441e 100755 --- a/runtime/Cpp/runtime/atn/SingletonPredictionContext.h +++ b/runtime/Cpp/runtime/atn/SingletonPredictionContext.h @@ -41,12 +41,14 @@ namespace atn { class SingletonPredictionContext : public PredictionContext { public: + typedef std::shared_ptr Ref; + const std::weak_ptr parent; const int returnState; SingletonPredictionContext(std::weak_ptr parent, int returnState); - static SingletonPredictionContextRef create(std::weak_ptr parent, int returnState); + static SingletonPredictionContext::Ref create(std::weak_ptr parent, int returnState); virtual size_t size() const override; virtual std::weak_ptr getParent(size_t index) const override; diff --git a/runtime/Cpp/runtime/atn/Transition.cpp b/runtime/Cpp/runtime/atn/Transition.cpp index 29c259e4d..afb23f783 100755 --- a/runtime/Cpp/runtime/atn/Transition.cpp +++ b/runtime/Cpp/runtime/atn/Transition.cpp @@ -58,3 +58,10 @@ bool Transition::isEpsilon() const { misc::IntervalSet Transition::label() const { return misc::IntervalSet::EMPTY_SET; } + +std::wstring Transition::toString() const { + std::wstringstream ss; + ss << "(Transition " << std::hex << this << ", target: " << std::hex << target << ')'; + + return ss.str(); +} diff --git a/runtime/Cpp/runtime/atn/Transition.h b/runtime/Cpp/runtime/atn/Transition.h index 8b69139f9..e7eabc672 100755 --- a/runtime/Cpp/runtime/atn/Transition.h +++ b/runtime/Cpp/runtime/atn/Transition.h @@ -69,10 +69,12 @@ namespace atn { static const std::vector serializationNames; - /// - /// The target of this transition. + /// The target of this transition. + // ml: this is a reference into the ATN. ATNState *target; + virtual ~Transition() {}; + protected: Transition(ATNState *target); @@ -84,6 +86,8 @@ namespace atn { virtual bool isEpsilon() const; virtual misc::IntervalSet label() const; virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const = 0; + + virtual std::wstring toString() const; }; } // namespace atn diff --git a/runtime/Cpp/runtime/atn/WildcardTransition.cpp b/runtime/Cpp/runtime/atn/WildcardTransition.cpp index 70ac169b1..22213b90f 100755 --- a/runtime/Cpp/runtime/atn/WildcardTransition.cpp +++ b/runtime/Cpp/runtime/atn/WildcardTransition.cpp @@ -47,5 +47,5 @@ bool WildcardTransition::matches(int symbol, int minVocabSymbol, int maxVocabSym } std::wstring WildcardTransition::toString() const { - return L"."; + return L"WILDCARD " + Transition::toString() + L" {}"; } diff --git a/runtime/Cpp/runtime/atn/WildcardTransition.h b/runtime/Cpp/runtime/atn/WildcardTransition.h index beb76b096..3a004cc80 100755 --- a/runtime/Cpp/runtime/atn/WildcardTransition.h +++ b/runtime/Cpp/runtime/atn/WildcardTransition.h @@ -47,7 +47,7 @@ namespace atn { virtual bool matches(int symbol, int minVocabSymbol, int maxVocabSymbol) const override; - virtual std::wstring toString() const; + virtual std::wstring toString() const override; }; } // namespace atn diff --git a/runtime/Cpp/runtime/dfa/DFA.cpp b/runtime/Cpp/runtime/dfa/DFA.cpp index 664c31d4a..e368210be 100755 --- a/runtime/Cpp/runtime/dfa/DFA.cpp +++ b/runtime/Cpp/runtime/dfa/DFA.cpp @@ -37,23 +37,18 @@ using namespace org::antlr::v4::runtime::dfa; -DFA::DFA(atn::DecisionState *atnStartState) : atnStartState(atnStartState), -states(new std::map()), decision(0), s0(nullptr) { +DFA::DFA(atn::DecisionState *atnStartState) : DFA(atnStartState, 0) { } -DFA::DFA(atn::DecisionState *atnStartState, int decision) : atnStartState(atnStartState), -states(new std::map()), decision(decision), s0(nullptr) { +DFA::DFA(atn::DecisionState *atnStartState, int decision) + : atnStartState(atnStartState), decision(decision), s0(nullptr) { } -std::vector DFA::getStates() { - // Get all the keys, which C++ doesn't do natively, ugh - std::map mapints; - std::vector vints; - for(auto imap: mapints) { - vints.push_back(imap.first); - } +std::vector DFA::getStates() { + std::vector result; + for (auto state : states) + result.push_back(state.first); - std::vector result = std::vector(vints); std::sort(result.begin(), result.end(), [](DFAState *o1, DFAState *o2) { return o1->stateNumber - o2->stateNumber; }); @@ -61,26 +56,25 @@ std::vector DFA::getStates() { return result; } - std::wstring DFA::toString() { std::vector tokenNames; return toString(tokenNames); } -std::wstring DFA::toString(const std::vector& tokenNames) { +std::wstring DFA::toString(const std::vector &tokenNames) { if (s0 == nullptr) { return L""; } - DFASerializer *serializer = new DFASerializer(this, tokenNames); + DFASerializer serializer(this, tokenNames); - return serializer->toString(); + return serializer.toString(); } std::wstring DFA::toLexerString() { if (s0 == nullptr) { return L""; } - DFASerializer *serializer = new LexerDFASerializer(this); + LexerDFASerializer serializer(this); - return serializer->toString(); + return serializer.toString(); } diff --git a/runtime/Cpp/runtime/dfa/DFA.h b/runtime/Cpp/runtime/dfa/DFA.h index 36b6365f2..488084e4d 100755 --- a/runtime/Cpp/runtime/dfa/DFA.h +++ b/runtime/Cpp/runtime/dfa/DFA.h @@ -38,15 +38,13 @@ namespace runtime { namespace dfa { class DFA { - /// - /// A set of all DFA states. Use so we can get old state back - /// ( only allows you to see if it's there). - /// public: - /// - /// From which ATN state did we create this DFA? + /// A set of all DFA states. Use a map so we can get old state back. + /// Set only allows you to see if it's there. + + /// From which ATN state did we create this DFA? atn::DecisionState *const atnStartState; - std::map *const states; + std::unordered_map states; DFAState *s0; const int decision; diff --git a/runtime/Cpp/runtime/dfa/DFAState.cpp b/runtime/Cpp/runtime/dfa/DFAState.cpp index 9df502e0d..52a7f8c20 100755 --- a/runtime/Cpp/runtime/dfa/DFAState.cpp +++ b/runtime/Cpp/runtime/dfa/DFAState.cpp @@ -39,7 +39,7 @@ using namespace org::antlr::v4::runtime::dfa; using namespace org::antlr::v4::runtime::atn; -DFAState::PredPrediction::PredPrediction(SemanticContextRef pred, int alt) : pred(pred) { +DFAState::PredPrediction::PredPrediction(SemanticContext::Ref pred, int alt) : pred(pred) { InitializeInstanceFields(); this->alt = alt; } diff --git a/runtime/Cpp/runtime/dfa/DFAState.h b/runtime/Cpp/runtime/dfa/DFAState.h index a7ba79a5e..021c8b723 100755 --- a/runtime/Cpp/runtime/dfa/DFAState.h +++ b/runtime/Cpp/runtime/dfa/DFAState.h @@ -134,19 +134,17 @@ namespace dfa { virtual size_t hashCode() ; - /// - /// Two instances are equal if their ATN configuration sets + /// Two DFAState instances are equal if their ATN configuration sets /// are the same. This method is used to see if a state already exists. - ///

+ /// /// Because the number of alternatives and number of ATN configurations are /// finite, there is a finite number of DFA states that can be processed. /// This is necessary to show that the algorithm terminates. - ///

+ /// /// Cannot test the DFA state numbers here because in - /// we need to know if any other state + /// ParserATNSimulator#addDFAState we need to know if any other state /// exists that has this exact set of ATN configurations. The - /// is irrelevant. - ///

+ /// stateNumber is irrelevant. bool operator == (const DFAState &o); virtual std::wstring toString(); diff --git a/runtime/Cpp/runtime/support/CPPUtils.cpp b/runtime/Cpp/runtime/support/CPPUtils.cpp index 653c9780a..f973fa8f7 100755 --- a/runtime/Cpp/runtime/support/CPPUtils.cpp +++ b/runtime/Cpp/runtime/support/CPPUtils.cpp @@ -32,71 +32,132 @@ namespace antlrcpp { -std::wstring join(std::vector strings, const std::wstring &separator) { - std::wstring str; - bool firstItem = true; - for (std::wstring s : strings) { - if (!firstItem) { - str.append(separator); + std::wstring join(std::vector strings, const std::wstring &separator) { + std::wstring str; + bool firstItem = true; + for (std::wstring s : strings) { + if (!firstItem) { + str.append(separator); + } + firstItem = false; + str.append(s); } - firstItem = false; - str.append(s); + return str; } - return str; -} -std::map toMap(const std::vector &keys) { - std::map result; - for (size_t i = 0; i < keys.size(); ++i) { - result.insert({ keys[i], i }); + std::map toMap(const std::vector &keys) { + std::map result; + for (size_t i = 0; i < keys.size(); ++i) { + result.insert({ keys[i], i }); + } + return result; } - return result; -} -std::wstring escapeWhitespace(std::wstring str, bool escapeSpaces) { - std::wstring result; - for (auto c : str) { - switch (c) { - case L' ': - if (escapeSpaces) { - result += '0xB7'; + std::wstring escapeWhitespace(std::wstring str, bool escapeSpaces) { + std::wstring result; + for (auto c : str) { + switch (c) { + case L' ': + if (escapeSpaces) { + result += '0xB7'; + break; + } else { + // fall through + } + + case L'\n': + result += L"\\n"; break; - } else { - // fall through - } - case L'\n': - result += L"\\n"; - break; + case L'\r': + result += L"\\r"; + break; - case L'\r': - result += L"\\r"; - break; + case L'\t': + result += L"\\t"; + break; - case L'\t': - result += L"\\t"; - break; - - default: - result += c; + default: + result += c; + } } + + return result; } - return result; -} - -std::wstring toHexString(const int t){ - std::wstringstream stream; - stream << std::uppercase << std::hex << t; - return stream.str(); -} - -std::wstring arrayToString(const std::vector &data) { - std::wstring answer; - for (auto sub: data) { - answer += sub; + std::wstring toHexString(const int t){ + std::wstringstream stream; + stream << std::uppercase << std::hex << t; + return stream.str(); } - return answer; -} + std::wstring arrayToString(const std::vector &data) { + std::wstring answer; + for (auto sub: data) { + answer += sub; + } + return answer; + } + + std::wstring replaceString(const std::wstring &s, const std::wstring &from, const std::wstring &to) { + std::wstring::size_type p; + std::wstring ss, res; + + ss = s; + p = ss.find(from); + while (p != std::wstring::npos) + { + if (p > 0) + res.append(ss.substr(0, p)).append(to); + else + res.append(to); + ss = ss.substr(p + from.size()); + p = ss.find(from); + } + res.append(ss); + + return res; + } + + //-------------------------------------------------------------------------------------------------- + + std::vector split(const std::wstring &s, const std::wstring &sep, int count) { + std::vector parts; + std::wstring ss = s; + + std::wstring::size_type p; + + if (s.empty()) + return parts; + + if (count == 0) + count= -1; + + p = ss.find(sep); + while (!ss.empty() && p != std::string::npos && (count < 0 || count > 0)) + { + parts.push_back(ss.substr(0, p)); + ss= ss.substr(p+sep.size()); + + --count; + p= ss.find(sep); + } + parts.push_back(ss); + + return parts; + } + + //-------------------------------------------------------------------------------------------------- + + // Debugging helper. Adds indentation to all lines in the given string. + std::wstring indent(const std::wstring &s, const std::wstring &indentation, bool includingFirst) { + std::vector parts = split(s, L"\n", -1); + for (size_t i = 0; i < parts.size(); ++i) { + if (i == 0 && !includingFirst) + continue; + parts[i].insert(0, indentation); + } + + return join(parts, L"\n"); + } } // namespace antlrcpp \ No newline at end of file diff --git a/runtime/Cpp/runtime/support/CPPUtils.h b/runtime/Cpp/runtime/support/CPPUtils.h index 660b9ee3b..9b47706e6 100644 --- a/runtime/Cpp/runtime/support/CPPUtils.h +++ b/runtime/Cpp/runtime/support/CPPUtils.h @@ -37,6 +37,9 @@ namespace antlrcpp { std::wstring escapeWhitespace(std::wstring str, bool escapeSpaces); std::wstring toHexString(const int t); std::wstring arrayToString(const std::vector &data); + std::wstring replaceString(const std::wstring &s, const std::wstring &from, const std::wstring &to); + std::vector split(const std::wstring &s, const std::wstring &sep, int count); + std::wstring indent(const std::wstring &s, const std::wstring &indentation, bool includingFirst = true); // Using RAII + a lambda to implement a "finally" relacement. template diff --git a/runtime/Cpp/runtime/support/Declarations.h b/runtime/Cpp/runtime/support/Declarations.h index 33c77a7dd..408c1a8e3 100644 --- a/runtime/Cpp/runtime/support/Declarations.h +++ b/runtime/Cpp/runtime/support/Declarations.h @@ -58,7 +58,7 @@ namespace org { class BailErrorStrategy; class BaseErrorListener; class BufferedTokenStream; - class CharStream; typedef std::shared_ptr CharStreamRef; + class CharStream; class CommonToken; class CommonTokenFactory; class CommonTokenStream; @@ -67,7 +67,7 @@ namespace org { class DiagnosticErrorListener; class FailedPredicateException; class InputMismatchException; - class IntStream; typedef std::shared_ptr IntStreamRef; + class IntStream; class InterpreterRuleContext; class IRecognizer; class Lexer; @@ -77,28 +77,25 @@ namespace org { class NoViableAltException; class Parser; class ParserInterpreter; - class ParserRuleContext; typedef std::shared_ptr ParserRuleContextRef; + class ParserRuleContext; class ProxyErrorListener; class RecognitionException; class Recognizer; - class RuleContext; typedef std::shared_ptr RuleContextRef; - class Token; typedef std::shared_ptr TokenRef; + class RuleContext; + class Token; template class TokenFactory; class TokenSource; - class TokenStream; typedef std::shared_ptr TokenStreamRef; + class TokenStream; class TokenStreamRewriter; class UnbufferedCharStream; class UnbufferedTokenStream; - class WritableToken; typedef std::shared_ptr WritableTokenRef; + class WritableToken; namespace misc { class Interval; class IntervalSet; - class JFileChooserConfirmOverwrite; class MurmurHash; - class ObjectEqualityComparator; class ParseCancellationException; - class TestRig; class Utils; } namespace atn { @@ -135,16 +132,16 @@ namespace org { class PlusLoopbackState; class PrecedencePredicateTransition; class PredicateTransition; - class PredictionContext; typedef std::shared_ptr PredictionContextRef; + class PredictionContext; enum class PredictionMode; class PredictionModeClass; class RangeTransition; class RuleStartState; class RuleStopState; class RuleTransition; - class SemanticContext; typedef std::shared_ptr SemanticContextRef; + class SemanticContext; class SetTransition; - class SingletonPredictionContext; typedef std::shared_ptr SingletonPredictionContextRef; + class SingletonPredictionContext; class StarBlockStartState; class StarLoopEntryState; class StarLoopbackState; diff --git a/runtime/Cpp/runtime/support/guid.cpp b/runtime/Cpp/runtime/support/guid.cpp index eb4d6cb9d..25d7bc85d 100755 --- a/runtime/Cpp/runtime/support/guid.cpp +++ b/runtime/Cpp/runtime/support/guid.cpp @@ -81,7 +81,7 @@ Guid::Guid(const unsigned char *bytes) } // create a guid from array of words -Guid::Guid(const uint32_t *bytes, bool reverse) +Guid::Guid(const uint16_t *bytes, bool reverse) { if (reverse) { for (size_t i = 8; i > 0; --i) @@ -182,7 +182,7 @@ bool Guid::operator!=(const Guid &other) const const std::string Guid::toString() const { std::stringstream os; - os << this; + os << *this; return os.str(); } diff --git a/runtime/Cpp/runtime/support/guid.h b/runtime/Cpp/runtime/support/guid.h index 788786aa5..0e4630db6 100755 --- a/runtime/Cpp/runtime/support/guid.h +++ b/runtime/Cpp/runtime/support/guid.h @@ -48,7 +48,7 @@ public: Guid(const unsigned char *bytes); // Create a guid from array of words. - Guid(const uint32_t *bytes, bool reverse); + Guid(const uint16_t *bytes, bool reverse); // create a guid from string Guid(const std::string &fromString); diff --git a/runtime/Cpp/runtime/tree/ErrorNodeImpl.cpp b/runtime/Cpp/runtime/tree/ErrorNodeImpl.cpp index 981c221f1..508b19c42 100755 --- a/runtime/Cpp/runtime/tree/ErrorNodeImpl.cpp +++ b/runtime/Cpp/runtime/tree/ErrorNodeImpl.cpp @@ -37,5 +37,5 @@ using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime::misc; using namespace org::antlr::v4::runtime::tree; -ErrorNodeImpl::ErrorNodeImpl(TokenRef token) : TerminalNodeImpl(token) { +ErrorNodeImpl::ErrorNodeImpl(Token::Ref token) : TerminalNodeImpl(token) { } diff --git a/runtime/Cpp/runtime/tree/ErrorNodeImpl.h b/runtime/Cpp/runtime/tree/ErrorNodeImpl.h index 788397f26..45d40e9d3 100755 --- a/runtime/Cpp/runtime/tree/ErrorNodeImpl.h +++ b/runtime/Cpp/runtime/tree/ErrorNodeImpl.h @@ -50,7 +50,7 @@ namespace tree { ///
class ErrorNodeImpl : public virtual TerminalNodeImpl, public virtual ErrorNode { public: - ErrorNodeImpl(TokenRef token); + ErrorNodeImpl(Token::Ref token); template T accept(ParseTreeVisitor *visitor) { diff --git a/runtime/Cpp/runtime/tree/ParseTreeWalker.cpp b/runtime/Cpp/runtime/tree/ParseTreeWalker.cpp index d54f87ae4..d515a78b2 100755 --- a/runtime/Cpp/runtime/tree/ParseTreeWalker.cpp +++ b/runtime/Cpp/runtime/tree/ParseTreeWalker.cpp @@ -59,13 +59,13 @@ void ParseTreeWalker::walk(std::shared_ptr listener, std::sha } void ParseTreeWalker::enterRule(std::shared_ptr listener, std::shared_ptr r) { - ParserRuleContextRef ctx = std::dynamic_pointer_cast(r->getRuleContext()); + ParserRuleContext::Ref ctx = std::dynamic_pointer_cast(r->getRuleContext()); listener->enterEveryRule(ctx); ctx->enterRule(listener); } void ParseTreeWalker::exitRule(std::shared_ptr listener, std::shared_ptr r) { - ParserRuleContextRef ctx = std::dynamic_pointer_cast(r->getRuleContext()); + ParserRuleContext::Ref ctx = std::dynamic_pointer_cast(r->getRuleContext()); ctx->exitRule(listener); listener->exitEveryRule(ctx); } diff --git a/runtime/Cpp/runtime/tree/RuleNode.h b/runtime/Cpp/runtime/tree/RuleNode.h index d7ea36b9d..f2c39b30c 100755 --- a/runtime/Cpp/runtime/tree/RuleNode.h +++ b/runtime/Cpp/runtime/tree/RuleNode.h @@ -41,7 +41,8 @@ namespace tree { class RuleNode : public ParseTree { public: - virtual runtime::RuleContextRef getRuleContext() = 0; + // Because of cross references (RuleNode <-> RuleContext) we cannot use RuleContext::Ref here. + virtual std::shared_ptr getRuleContext() = 0; }; } // namespace tree diff --git a/runtime/Cpp/runtime/tree/TerminalNode.h b/runtime/Cpp/runtime/tree/TerminalNode.h index 9744e5674..fb0d3026e 100755 --- a/runtime/Cpp/runtime/tree/TerminalNode.h +++ b/runtime/Cpp/runtime/tree/TerminalNode.h @@ -31,6 +31,7 @@ #pragma once +#include "Token.h" #include "ParseTree.h" namespace org { @@ -41,7 +42,7 @@ namespace tree { class TerminalNode : public ParseTree { public: - virtual TokenRef getSymbol() = 0; + virtual Token::Ref getSymbol() = 0; }; } // namespace tree diff --git a/runtime/Cpp/runtime/tree/TerminalNodeImpl.cpp b/runtime/Cpp/runtime/tree/TerminalNodeImpl.cpp index 141511760..f176f3447 100755 --- a/runtime/Cpp/runtime/tree/TerminalNodeImpl.cpp +++ b/runtime/Cpp/runtime/tree/TerminalNodeImpl.cpp @@ -37,11 +37,11 @@ using namespace org::antlr::v4::runtime; using namespace org::antlr::v4::runtime::tree; -TerminalNodeImpl::TerminalNodeImpl(TokenRef symbol) { +TerminalNodeImpl::TerminalNodeImpl(Token::Ref symbol) { this->symbol = symbol; } -TokenRef TerminalNodeImpl::getSymbol() { +Token::Ref TerminalNodeImpl::getSymbol() { return symbol; } diff --git a/runtime/Cpp/runtime/tree/TerminalNodeImpl.h b/runtime/Cpp/runtime/tree/TerminalNodeImpl.h index 8c9898382..80d5a722d 100755 --- a/runtime/Cpp/runtime/tree/TerminalNodeImpl.h +++ b/runtime/Cpp/runtime/tree/TerminalNodeImpl.h @@ -41,12 +41,12 @@ namespace tree { class TerminalNodeImpl : public virtual TerminalNode { public: - TokenRef symbol; + Token::Ref symbol; std::weak_ptr parent; - TerminalNodeImpl(TokenRef symbol); + TerminalNodeImpl(Token::Ref symbol); - virtual TokenRef getSymbol() override; + virtual Token::Ref getSymbol() override; virtual misc::Interval getSourceInterval() override; virtual std::size_t getChildCount() override; diff --git a/runtime/Cpp/runtime/tree/Trees.cpp b/runtime/Cpp/runtime/tree/Trees.cpp index b68085ae2..7bafb6ebb 100755 --- a/runtime/Cpp/runtime/tree/Trees.cpp +++ b/runtime/Cpp/runtime/tree/Trees.cpp @@ -84,7 +84,7 @@ std::wstring Trees::getNodeText(std::shared_ptr t, const std::vector(t)) { return t->toString(); } else if (is(t)) { - TokenRef symbol = (std::static_pointer_cast(t))->getSymbol(); + Token::Ref symbol = (std::static_pointer_cast(t))->getSymbol(); if (symbol != nullptr) { std::wstring s = symbol->getText(); return s; @@ -129,7 +129,7 @@ static void _findAllNodes(std::shared_ptr t, int index, bool findToke nodes.push_back(t); } } else if (!findTokens && is(t)) { - ParserRuleContextRef ctx = std::dynamic_pointer_cast(t); + ParserRuleContext::Ref ctx = std::dynamic_pointer_cast(t); if (ctx->getRuleIndex() == index) { nodes.push_back(t); } diff --git a/runtime/Cpp/runtime/tree/pattern/ParseTreePatternMatcher.cpp b/runtime/Cpp/runtime/tree/pattern/ParseTreePatternMatcher.cpp index f16066b98..be2c142e4 100755 --- a/runtime/Cpp/runtime/tree/pattern/ParseTreePatternMatcher.cpp +++ b/runtime/Cpp/runtime/tree/pattern/ParseTreePatternMatcher.cpp @@ -100,7 +100,7 @@ ParseTreeMatch ParseTreePatternMatcher::match(std::shared_ptr tree, c } ParseTreePattern ParseTreePatternMatcher::compile(const std::wstring &pattern, int patternRuleIndex) { - std::vector tokenList = tokenize(pattern); + std::vector tokenList = tokenize(pattern); ListTokenSource *tokenSrc = new ListTokenSource(tokenList); // XXX: mem leak CommonTokenStream *tokens = new CommonTokenStream(tokenSrc); @@ -108,7 +108,7 @@ ParseTreePattern ParseTreePatternMatcher::compile(const std::wstring &pattern, i _parser->getRuleNames(), _parser->getATNWithBypassAlts(), tokens); try { - ParserRuleContextRef context = parserInterp.parse(patternRuleIndex); + ParserRuleContext::Ref context = parserInterp.parse(patternRuleIndex); return ParseTreePattern(this, pattern, patternRuleIndex, context); } catch (std::exception &e) { std::throw_with_nested("Cannot invoke start rule"); @@ -168,8 +168,8 @@ std::shared_ptr ParseTreePatternMatcher::matchImpl(std::shared_ptr(tree) && is(patternTree)) { - ParserRuleContextRef r1 = std::dynamic_pointer_cast(tree); - ParserRuleContextRef r2 = std::dynamic_pointer_cast(patternTree); + ParserRuleContext::Ref r1 = std::dynamic_pointer_cast(tree); + ParserRuleContext::Ref r2 = std::dynamic_pointer_cast(patternTree); std::shared_ptr mismatchedNode; // (expr ...) and @@ -228,12 +228,12 @@ std::shared_ptr ParseTreePatternMatcher::getRuleTagToken(std::shar return nullptr; } -std::vector ParseTreePatternMatcher::tokenize(const std::wstring &pattern) { +std::vector ParseTreePatternMatcher::tokenize(const std::wstring &pattern) { // split pattern into chunks: sea (raw input) and islands (, ) std::vector chunks = split(pattern); // create token stream from text and tags - std::vector tokens; + std::vector tokens; for (auto chunk : chunks) { if (dynamic_cast(chunk) != nullptr) { TagChunk *tagChunk = static_cast(chunk); @@ -259,7 +259,7 @@ std::vector ParseTreePatternMatcher::tokenize(const std::wstring &patt TextChunk *textChunk = static_cast(chunk); ANTLRInputStream input(textChunk->getText()); _lexer->setInputStream(&input); - TokenRef t = _lexer->nextToken(); + Token::Ref t = _lexer->nextToken(); while (t->getType() != EOF) { tokens.push_back(t); t = _lexer->nextToken(); diff --git a/runtime/Cpp/runtime/tree/pattern/ParseTreePatternMatcher.h b/runtime/Cpp/runtime/tree/pattern/ParseTreePatternMatcher.h index 2c34c0138..8e343dee3 100755 --- a/runtime/Cpp/runtime/tree/pattern/ParseTreePatternMatcher.h +++ b/runtime/Cpp/runtime/tree/pattern/ParseTreePatternMatcher.h @@ -31,6 +31,8 @@ #pragma once +#include "Token.h" + namespace org { namespace antlr { namespace v4 { @@ -165,7 +167,7 @@ namespace pattern { // ---- SUPPORT CODE ---- - virtual std::vector tokenize(const std::wstring &pattern); + virtual std::vector tokenize(const std::wstring &pattern); /// Split " = ;" into 4 chunks for tokenizing by tokenize(). virtual std::vector split(const std::wstring &pattern); diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg index d561a34bb..fd727863c 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Cpp/Cpp.stg @@ -84,7 +84,7 @@ public: private: - static std::vector\ _decisionToDFA; + static std::vector\ _decisionToDFA; static std::shared_ptr\ _sharedContextCache; static std::vector\ _ruleNames; static std::vector\ _tokenNames; @@ -175,7 +175,7 @@ bool ::sempred(std::shared_ptr\ context, int ruleIndex, // Static vars and initialization. -std::vector\ ::_decisionToDFA; +std::vector\ ::_decisionToDFA; std::shared_ptr\ ::_sharedContextCache = std::make_shared\(); // We own the ATN which in turn owns the ATN states. @@ -275,7 +275,7 @@ public: private: - static std::vector\ _decisionToDFA; + static std::vector\ _decisionToDFA; static std::shared_ptr\ _sharedContextCache; static std::vector\ _ruleNames; static std::vector\ _tokenNames; @@ -324,7 +324,7 @@ bool ::sempred(std::shared_ptr\ context, int ruleIndex // Static vars and initialization. -std::vector\ ::_decisionToDFA; +std::vector\ ::_decisionToDFA; std::shared_ptr\ ::_sharedContextCache = std::make_shared\(); // We own the ATN which in turn owns the ATN states. @@ -369,7 +369,7 @@ atn::ATNDeserializer deserializer; _atn = deserializer.deserialize(_serializedATN); 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)); } >> @@ -866,7 +866,7 @@ AddToLabelList(a) ::= << ->.push_back(); >> -TokenLabelType() ::= "Ref" +TokenLabelType() ::= "::Ref" TokenDeclHeader(t) ::= " ;" TokenDecl(t) ::= "" @@ -874,7 +874,7 @@ TokenDecl(t) ::= "" TokenTypeDeclHeader(t) ::= "" TokenTypeDecl(t) ::= "ssize_t ;" -TokenListDeclHeader(t) ::= "std::vector\ ;" +TokenListDeclHeader(t) ::= "std::vector\ ;" TokenListDecl(t) ::= "" RuleContextDeclHeader(r) ::= "std::shared_ptr\<::> ;"