From 8280beb5646a1c271fabba9c061cc5c5b6a61e14 Mon Sep 17 00:00:00 2001 From: Mike Lischke Date: Sun, 2 Oct 2016 16:51:57 +0200 Subject: [PATCH] Refactoring of further int values, mostly converting them to size_t. - Switched most symbolic signed constants to unsigned variants. Redefined EOF in particular to become (size)-1, to avoid having to use signed token type values. - Introduced INVALID_INDEX for all previous -1 values to indicate e.g. not found indexes etc. - Added 2 helpers to convert between symbolic and numeric form (mostly for intervals and toString()). - Removed many no longer needed type casts to size_t. - Updated templates for these changes. - Limited runtime tests to C++ tests only, to see how Travis CI copes with that. --- doc/building-antlr.md | 2 +- runtime-testsuite/pom.xml | 6 +- .../v4/test/runtime/cpp/BaseCppTest.java | 10 +- .../antlrcpp.xcodeproj/project.pbxproj | 8 +- runtime/Cpp/runtime/src/ANTLRErrorListener.h | 4 +- runtime/Cpp/runtime/src/ANTLRInputStream.cpp | 8 +- runtime/Cpp/runtime/src/ANTLRInputStream.h | 4 +- runtime/Cpp/runtime/src/BaseErrorListener.cpp | 4 +- runtime/Cpp/runtime/src/BaseErrorListener.h | 4 +- .../Cpp/runtime/src/BufferedTokenStream.cpp | 34 ++-- runtime/Cpp/runtime/src/BufferedTokenStream.h | 8 +- runtime/Cpp/runtime/src/CommonToken.cpp | 45 +++--- runtime/Cpp/runtime/src/CommonToken.h | 44 ++--- .../Cpp/runtime/src/CommonTokenFactory.cpp | 6 +- runtime/Cpp/runtime/src/CommonTokenFactory.h | 6 +- .../Cpp/runtime/src/ConsoleErrorListener.cpp | 2 +- .../Cpp/runtime/src/ConsoleErrorListener.h | 2 +- .../Cpp/runtime/src/DefaultErrorStrategy.cpp | 30 ++-- .../Cpp/runtime/src/DefaultErrorStrategy.h | 2 +- .../runtime/src/DiagnosticErrorListener.cpp | 18 +-- .../Cpp/runtime/src/DiagnosticErrorListener.h | 2 +- .../runtime/src/FailedPredicateException.cpp | 6 +- .../runtime/src/FailedPredicateException.h | 8 +- runtime/Cpp/runtime/src/IRecognizer.h | 2 +- runtime/Cpp/runtime/src/IntStream.h | 4 +- .../runtime/src/InterpreterRuleContext.cpp | 5 +- .../Cpp/runtime/src/InterpreterRuleContext.h | 6 +- runtime/Cpp/runtime/src/Lexer.cpp | 33 ++-- runtime/Cpp/runtime/src/Lexer.h | 43 +++-- runtime/Cpp/runtime/src/LexerInterpreter.cpp | 4 +- .../runtime/src/LexerNoViableAltException.cpp | 2 +- runtime/Cpp/runtime/src/ListTokenSource.cpp | 12 +- runtime/Cpp/runtime/src/ListTokenSource.h | 2 +- runtime/Cpp/runtime/src/Parser.cpp | 56 +++---- runtime/Cpp/runtime/src/Parser.h | 19 ++- runtime/Cpp/runtime/src/ParserInterpreter.cpp | 28 ++-- runtime/Cpp/runtime/src/ParserInterpreter.h | 10 +- runtime/Cpp/runtime/src/ParserRuleContext.cpp | 6 +- runtime/Cpp/runtime/src/ParserRuleContext.h | 6 +- .../Cpp/runtime/src/ProxyErrorListener.cpp | 4 +- runtime/Cpp/runtime/src/ProxyErrorListener.h | 4 +- .../Cpp/runtime/src/RecognitionException.cpp | 4 +- .../Cpp/runtime/src/RecognitionException.h | 6 +- runtime/Cpp/runtime/src/Recognizer.cpp | 28 ++-- runtime/Cpp/runtime/src/Recognizer.h | 18 +-- runtime/Cpp/runtime/src/RuleContext.cpp | 16 +- runtime/Cpp/runtime/src/RuleContext.h | 6 +- runtime/Cpp/runtime/src/Token.h | 58 +++---- runtime/Cpp/runtime/src/TokenFactory.h | 10 +- runtime/Cpp/runtime/src/TokenSource.h | 4 +- .../Cpp/runtime/src/TokenStreamRewriter.cpp | 50 +++--- runtime/Cpp/runtime/src/TokenStreamRewriter.h | 30 ++-- .../Cpp/runtime/src/UnbufferedCharStream.cpp | 19 +-- .../Cpp/runtime/src/UnbufferedCharStream.h | 2 +- .../Cpp/runtime/src/UnbufferedTokenStream.cpp | 6 +- .../Cpp/runtime/src/UnbufferedTokenStream.h | 2 +- runtime/Cpp/runtime/src/Vocabulary.cpp | 29 ++-- runtime/Cpp/runtime/src/Vocabulary.h | 14 +- runtime/Cpp/runtime/src/WritableToken.h | 10 +- runtime/Cpp/runtime/src/antlr4-common.h | 1 + runtime/Cpp/runtime/src/atn/ATN.cpp | 22 +-- runtime/Cpp/runtime/src/atn/ATN.h | 10 +- runtime/Cpp/runtime/src/atn/ATNConfig.cpp | 2 +- runtime/Cpp/runtime/src/atn/ATNConfigSet.cpp | 2 +- .../Cpp/runtime/src/atn/ATNDeserializer.cpp | 81 +++++----- runtime/Cpp/runtime/src/atn/ATNDeserializer.h | 6 +- runtime/Cpp/runtime/src/atn/ATNSerializer.cpp | 153 +++++++++--------- runtime/Cpp/runtime/src/atn/ATNSerializer.h | 2 +- runtime/Cpp/runtime/src/atn/ATNState.cpp | 15 +- runtime/Cpp/runtime/src/atn/ATNState.h | 16 +- .../Cpp/runtime/src/atn/ActionTransition.cpp | 10 +- .../Cpp/runtime/src/atn/ActionTransition.h | 12 +- runtime/Cpp/runtime/src/atn/AmbiguityInfo.cpp | 2 +- runtime/Cpp/runtime/src/atn/AmbiguityInfo.h | 2 +- .../src/atn/ArrayPredictionContext.cpp | 4 +- .../runtime/src/atn/ArrayPredictionContext.h | 6 +- .../Cpp/runtime/src/atn/AtomTransition.cpp | 6 +- runtime/Cpp/runtime/src/atn/AtomTransition.h | 8 +- .../src/atn/ContextSensitivityInfo.cpp | 2 +- .../runtime/src/atn/ContextSensitivityInfo.h | 2 +- .../Cpp/runtime/src/atn/DecisionEventInfo.cpp | 2 +- .../Cpp/runtime/src/atn/DecisionEventInfo.h | 4 +- .../src/atn/EmptyPredictionContext.cpp | 2 +- .../runtime/src/atn/EmptyPredictionContext.h | 2 +- .../Cpp/runtime/src/atn/EpsilonTransition.cpp | 10 +- .../Cpp/runtime/src/atn/EpsilonTransition.h | 10 +- runtime/Cpp/runtime/src/atn/ErrorInfo.cpp | 2 +- runtime/Cpp/runtime/src/atn/ErrorInfo.h | 2 +- runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp | 14 +- runtime/Cpp/runtime/src/atn/LL1Analyzer.h | 2 +- .../Cpp/runtime/src/atn/LexerATNConfig.cpp | 2 +- .../Cpp/runtime/src/atn/LexerATNSimulator.cpp | 76 ++++----- .../Cpp/runtime/src/atn/LexerATNSimulator.h | 52 +++--- .../runtime/src/atn/LexerActionExecutor.cpp | 4 +- .../Cpp/runtime/src/atn/LexerActionExecutor.h | 2 +- .../Cpp/runtime/src/atn/NotSetTransition.cpp | 4 +- .../Cpp/runtime/src/atn/NotSetTransition.h | 4 +- .../runtime/src/atn/ParserATNSimulator.cpp | 72 +++++---- .../Cpp/runtime/src/atn/ParserATNSimulator.h | 28 ++-- .../src/atn/PrecedencePredicateTransition.cpp | 4 +- .../src/atn/PrecedencePredicateTransition.h | 4 +- .../Cpp/runtime/src/atn/PredicateEvalInfo.cpp | 2 +- .../Cpp/runtime/src/atn/PredicateEvalInfo.h | 4 +- .../runtime/src/atn/PredicateTransition.cpp | 6 +- .../Cpp/runtime/src/atn/PredicateTransition.h | 10 +- .../Cpp/runtime/src/atn/PredictionContext.cpp | 32 ++-- .../Cpp/runtime/src/atn/PredictionContext.h | 22 +-- .../Cpp/runtime/src/atn/PredictionMode.cpp | 12 +- runtime/Cpp/runtime/src/atn/PredictionMode.h | 6 +- .../runtime/src/atn/ProfilingATNSimulator.cpp | 16 +- .../runtime/src/atn/ProfilingATNSimulator.h | 15 +- .../Cpp/runtime/src/atn/RangeTransition.cpp | 6 +- runtime/Cpp/runtime/src/atn/RangeTransition.h | 10 +- .../Cpp/runtime/src/atn/RuleTransition.cpp | 8 +- runtime/Cpp/runtime/src/atn/RuleTransition.h | 12 +- .../Cpp/runtime/src/atn/SemanticContext.cpp | 10 +- runtime/Cpp/runtime/src/atn/SemanticContext.h | 6 +- runtime/Cpp/runtime/src/atn/SetTransition.cpp | 6 +- runtime/Cpp/runtime/src/atn/SetTransition.h | 4 +- .../src/atn/SingletonPredictionContext.cpp | 11 +- .../src/atn/SingletonPredictionContext.h | 8 +- runtime/Cpp/runtime/src/atn/Transition.h | 27 ++-- .../runtime/src/atn/WildcardTransition.cpp | 4 +- .../Cpp/runtime/src/atn/WildcardTransition.h | 4 +- runtime/Cpp/runtime/src/dfa/DFA.cpp | 2 +- runtime/Cpp/runtime/src/dfa/DFA.h | 4 +- runtime/Cpp/runtime/src/dfa/DFASerializer.cpp | 2 +- runtime/Cpp/runtime/src/dfa/DFAState.h | 10 +- runtime/Cpp/runtime/src/misc/Interval.cpp | 27 ++-- runtime/Cpp/runtime/src/misc/Interval.h | 23 +-- runtime/Cpp/runtime/src/misc/IntervalSet.cpp | 97 ++++++----- runtime/Cpp/runtime/src/misc/IntervalSet.h | 67 ++++---- runtime/Cpp/runtime/src/support/Any.h | 2 +- runtime/Cpp/runtime/src/support/BitSet.h | 6 +- .../Cpp/runtime/src/tree/TerminalNodeImpl.cpp | 2 +- runtime/Cpp/runtime/src/tree/Trees.cpp | 14 +- runtime/Cpp/runtime/src/tree/Trees.h | 6 +- .../tree/pattern/ParseTreePatternMatcher.cpp | 6 +- .../runtime/src/tree/pattern/RuleTagToken.cpp | 22 +-- .../runtime/src/tree/pattern/RuleTagToken.h | 72 ++------- runtime/Cpp/runtime/src/tree/xpath/XPath.cpp | 2 +- .../Cpp/runtime/src/tree/xpath/XPathLexer.cpp | 6 +- .../Cpp/runtime/src/tree/xpath/XPathLexer.h | 4 +- .../tree/xpath/XPathLexerErrorListener.cpp | 2 +- .../src/tree/xpath/XPathLexerErrorListener.h | 2 +- .../src/tree/xpath/XPathRuleElement.cpp | 2 +- .../runtime/src/tree/xpath/XPathRuleElement.h | 4 +- .../src/tree/xpath/XPathTokenElement.cpp | 2 +- .../src/tree/xpath/XPathTokenElement.h | 4 +- .../v4/tool/templates/codegen/Cpp/Cpp.stg | 60 +++---- 150 files changed, 1060 insertions(+), 1118 deletions(-) diff --git a/doc/building-antlr.md b/doc/building-antlr.md index ac520fba0..d46609621 100644 --- a/doc/building-antlr.md +++ b/doc/building-antlr.md @@ -113,7 +113,7 @@ antlr4-tool-testsuite/4.5.2-SNAPSHOT/antlr4-tool-testsuite-4.5.2-SNAPSHOT.jar Note that ANTLR is written in itself, which is why maven downloads antlr4-4.5.jar for boostrapping 4.5.2-SNAPSHOT purposes. ## Building without testing -To build without running the tests (saves more than 30 mins), do this: +To build without running the tests (saves a lot of time), do this: ```bash mvn -DskipTests install diff --git a/runtime-testsuite/pom.xml b/runtime-testsuite/pom.xml index 655da6974..43e36535f 100644 --- a/runtime-testsuite/pom.xml +++ b/runtime-testsuite/pom.xml @@ -70,12 +70,12 @@ 2.12.4 - **/csharp/Test*.java - **/cpp/Test*.java + **/cpp/TestParserErrors.java + diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java index f36914e9e..82ad4e842 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java @@ -946,18 +946,14 @@ public abstract class BaseCppTest { + "#include \"antlr4-runtime.h\"\n" + "#include \".h\"\n" + "#include \".h\"\n" - + "#include \".h\"\n" - + "#include \".h\"\n" - + "\n" - + "#include \"support/StringUtils.h\"\n" + "\n" + "using namespace antlr4;\n" + "\n" + "class TreeShapeListener : public tree::ParseTreeListener {\n" + "public:\n" - + " void visitTerminal(tree::TerminalNode *node) override {}\n" - + " void visitErrorNode(tree::ErrorNode *node) override {}\n" - + " void exitEveryRule(ParserRuleContext *ctx) override {}\n" + + " void visitTerminal(tree::TerminalNode *) override {}\n" + + " void visitErrorNode(tree::ErrorNode *) override {}\n" + + " void exitEveryRule(ParserRuleContext *) override {}\n" + " void enterEveryRule(ParserRuleContext *ctx) override {\n" + " for (auto child : ctx->children) {\n" + " auto parent = child->parent.lock();\n" diff --git a/runtime/Cpp/runtime/antlrcpp.xcodeproj/project.pbxproj b/runtime/Cpp/runtime/antlrcpp.xcodeproj/project.pbxproj index 0efa3653f..0e6d8b4e1 100644 --- a/runtime/Cpp/runtime/antlrcpp.xcodeproj/project.pbxproj +++ b/runtime/Cpp/runtime/antlrcpp.xcodeproj/project.pbxproj @@ -911,7 +911,7 @@ 276E5C201CDB57AA003FF4B4 /* ATNConfigSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNConfigSet.h; sourceTree = ""; wrapsLines = 0; }; 276E5C211CDB57AA003FF4B4 /* ATNDeserializationOptions.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNDeserializationOptions.cpp; sourceTree = ""; }; 276E5C221CDB57AA003FF4B4 /* ATNDeserializationOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNDeserializationOptions.h; sourceTree = ""; }; - 276E5C231CDB57AA003FF4B4 /* ATNDeserializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNDeserializer.cpp; sourceTree = ""; }; + 276E5C231CDB57AA003FF4B4 /* ATNDeserializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ATNDeserializer.cpp; sourceTree = ""; wrapsLines = 0; }; 276E5C241CDB57AA003FF4B4 /* ATNDeserializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNDeserializer.h; sourceTree = ""; }; 276E5C251CDB57AA003FF4B4 /* ATNSerializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = ATNSerializer.cpp; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.cpp; }; 276E5C261CDB57AA003FF4B4 /* ATNSerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ATNSerializer.h; sourceTree = ""; }; @@ -969,7 +969,7 @@ 276E5C5D1CDB57AA003FF4B4 /* LexerSkipAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerSkipAction.h; sourceTree = ""; }; 276E5C5E1CDB57AA003FF4B4 /* LexerTypeAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LexerTypeAction.cpp; sourceTree = ""; }; 276E5C5F1CDB57AA003FF4B4 /* LexerTypeAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LexerTypeAction.h; sourceTree = ""; }; - 276E5C601CDB57AA003FF4B4 /* LL1Analyzer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LL1Analyzer.cpp; sourceTree = ""; }; + 276E5C601CDB57AA003FF4B4 /* LL1Analyzer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LL1Analyzer.cpp; sourceTree = ""; wrapsLines = 0; }; 276E5C611CDB57AA003FF4B4 /* LL1Analyzer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LL1Analyzer.h; sourceTree = ""; wrapsLines = 0; }; 276E5C621CDB57AA003FF4B4 /* LookaheadEventInfo.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LookaheadEventInfo.cpp; sourceTree = ""; wrapsLines = 0; }; 276E5C631CDB57AA003FF4B4 /* LookaheadEventInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LookaheadEventInfo.h; sourceTree = ""; wrapsLines = 0; }; @@ -1059,7 +1059,7 @@ 276E5CB91CDB57AA003FF4B4 /* FailedPredicateException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FailedPredicateException.h; sourceTree = ""; }; 276E5CBA1CDB57AA003FF4B4 /* InputMismatchException.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InputMismatchException.cpp; sourceTree = ""; }; 276E5CBB1CDB57AA003FF4B4 /* InputMismatchException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InputMismatchException.h; sourceTree = ""; }; - 276E5CBC1CDB57AA003FF4B4 /* InterpreterRuleContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InterpreterRuleContext.cpp; sourceTree = ""; }; + 276E5CBC1CDB57AA003FF4B4 /* InterpreterRuleContext.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = InterpreterRuleContext.cpp; sourceTree = ""; wrapsLines = 0; }; 276E5CBD1CDB57AA003FF4B4 /* InterpreterRuleContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InterpreterRuleContext.h; sourceTree = ""; }; 276E5CBE1CDB57AA003FF4B4 /* IntStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IntStream.cpp; sourceTree = ""; }; 276E5CBF1CDB57AA003FF4B4 /* IntStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntStream.h; sourceTree = ""; }; @@ -1074,7 +1074,7 @@ 276E5CC81CDB57AA003FF4B4 /* ListTokenSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ListTokenSource.h; sourceTree = ""; wrapsLines = 0; }; 276E5CCA1CDB57AA003FF4B4 /* Interval.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Interval.cpp; sourceTree = ""; }; 276E5CCB1CDB57AA003FF4B4 /* Interval.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Interval.h; sourceTree = ""; }; - 276E5CCC1CDB57AA003FF4B4 /* IntervalSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IntervalSet.cpp; sourceTree = ""; }; + 276E5CCC1CDB57AA003FF4B4 /* IntervalSet.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = IntervalSet.cpp; sourceTree = ""; wrapsLines = 0; }; 276E5CCD1CDB57AA003FF4B4 /* IntervalSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IntervalSet.h; sourceTree = ""; }; 276E5CCE1CDB57AA003FF4B4 /* MurmurHash.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MurmurHash.cpp; sourceTree = ""; }; 276E5CCF1CDB57AA003FF4B4 /* MurmurHash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MurmurHash.h; sourceTree = ""; }; diff --git a/runtime/Cpp/runtime/src/ANTLRErrorListener.h b/runtime/Cpp/runtime/src/ANTLRErrorListener.h index 428c8fea6..c5f8841b4 100755 --- a/runtime/Cpp/runtime/src/ANTLRErrorListener.h +++ b/runtime/Cpp/runtime/src/ANTLRErrorListener.h @@ -77,7 +77,7 @@ namespace antlr4 { /// the parser was able to recover in line without exiting the /// surrounding rule. virtual void syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, - int charPositionInLine, const std::string &msg, std::exception_ptr e) = 0; + size_t charPositionInLine, const std::string &msg, std::exception_ptr e) = 0; /** * This method is called by the parser when a full-context prediction @@ -187,7 +187,7 @@ namespace antlr4 { * was determined */ virtual void reportContextSensitivity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, - int prediction, atn::ATNConfigSet *configs) = 0; + size_t prediction, atn::ATNConfigSet *configs) = 0; }; } // namespace antlr4 diff --git a/runtime/Cpp/runtime/src/ANTLRInputStream.cpp b/runtime/Cpp/runtime/src/ANTLRInputStream.cpp index bb82f62a8..43d7c3033 100755 --- a/runtime/Cpp/runtime/src/ANTLRInputStream.cpp +++ b/runtime/Cpp/runtime/src/ANTLRInputStream.cpp @@ -92,7 +92,7 @@ void ANTLRInputStream::consume() { } } -ssize_t ANTLRInputStream::LA(ssize_t i) { +size_t ANTLRInputStream::LA(ssize_t i) { if (i == 0) { return 0; // undefined } @@ -112,7 +112,7 @@ ssize_t ANTLRInputStream::LA(ssize_t i) { return _data[(size_t)(position + i - 1)]; } -ssize_t ANTLRInputStream::LT(ssize_t i) { +size_t ANTLRInputStream::LT(ssize_t i) { return LA(i); } @@ -149,8 +149,8 @@ std::string ANTLRInputStream::getText(const Interval &interval) { return ""; } - size_t start = (size_t)interval.a; - size_t stop = (size_t)interval.b; + size_t start = interval.a; + size_t stop = interval.b; if (stop >= _data.size()) { diff --git a/runtime/Cpp/runtime/src/ANTLRInputStream.h b/runtime/Cpp/runtime/src/ANTLRInputStream.h index 5be610112..38f059697 100755 --- a/runtime/Cpp/runtime/src/ANTLRInputStream.h +++ b/runtime/Cpp/runtime/src/ANTLRInputStream.h @@ -67,8 +67,8 @@ namespace antlr4 { /// touched. virtual void reset(); virtual void consume() override; - virtual ssize_t LA(ssize_t i) override; - virtual ssize_t LT(ssize_t i); + virtual size_t LA(ssize_t i) override; + virtual size_t LT(ssize_t i); /// /// Return the current input symbol index 0..n where n indicates the diff --git a/runtime/Cpp/runtime/src/BaseErrorListener.cpp b/runtime/Cpp/runtime/src/BaseErrorListener.cpp index 8b5425c9b..719b09dcc 100755 --- a/runtime/Cpp/runtime/src/BaseErrorListener.cpp +++ b/runtime/Cpp/runtime/src/BaseErrorListener.cpp @@ -35,7 +35,7 @@ using namespace antlr4; void BaseErrorListener::syntaxError(IRecognizer * /*recognizer*/, Token * /*offendingSymbol*/, size_t /*line*/, - int /*charPositionInLine*/, const std::string &/*msg*/, std::exception_ptr /*e*/) { + size_t /*charPositionInLine*/, const std::string &/*msg*/, std::exception_ptr /*e*/) { } void BaseErrorListener::reportAmbiguity(Parser * /*recognizer*/, const dfa::DFA &/*dfa*/, size_t /*startIndex*/, @@ -47,5 +47,5 @@ void BaseErrorListener::reportAttemptingFullContext(Parser * /*recognizer*/, con } void BaseErrorListener::reportContextSensitivity(Parser * /*recognizer*/, const dfa::DFA &/*dfa*/, size_t /*startIndex*/, - size_t /*stopIndex*/, int /*prediction*/, atn::ATNConfigSet * /*configs*/) { + size_t /*stopIndex*/, size_t /*prediction*/, atn::ATNConfigSet * /*configs*/) { } diff --git a/runtime/Cpp/runtime/src/BaseErrorListener.h b/runtime/Cpp/runtime/src/BaseErrorListener.h index c2ecb74b3..5c6678a48 100755 --- a/runtime/Cpp/runtime/src/BaseErrorListener.h +++ b/runtime/Cpp/runtime/src/BaseErrorListener.h @@ -46,7 +46,7 @@ namespace antlr4 { */ class ANTLR4CPP_PUBLIC BaseErrorListener : public ANTLRErrorListener { - virtual void syntaxError(IRecognizer *recognizer, Token * offendingSymbol, size_t line, int charPositionInLine, + virtual void syntaxError(IRecognizer *recognizer, Token * offendingSymbol, size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override; virtual void reportAmbiguity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact, @@ -56,7 +56,7 @@ namespace antlr4 { const antlrcpp::BitSet &conflictingAlts, atn::ATNConfigSet *configs) override; virtual void reportContextSensitivity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, - int prediction, atn::ATNConfigSet *configs) override; + size_t prediction, atn::ATNConfigSet *configs) override; }; } // namespace antlr4 diff --git a/runtime/Cpp/runtime/src/BufferedTokenStream.cpp b/runtime/Cpp/runtime/src/BufferedTokenStream.cpp index 81e6aa86a..31bc1520e 100755 --- a/runtime/Cpp/runtime/src/BufferedTokenStream.cpp +++ b/runtime/Cpp/runtime/src/BufferedTokenStream.cpp @@ -169,7 +169,7 @@ std::vector BufferedTokenStream::get(size_t start, size_t stop) { return subset; } -ssize_t BufferedTokenStream::LA(ssize_t i) { +size_t BufferedTokenStream::LA(ssize_t i) { return LT(i)->getType(); } @@ -229,13 +229,13 @@ std::vector BufferedTokenStream::getTokens() { return result; } -std::vector BufferedTokenStream::getTokens(int start, int stop) { - return getTokens(start, stop, std::vector()); +std::vector BufferedTokenStream::getTokens(size_t start, size_t stop) { + return getTokens(start, stop, std::vector()); } -std::vector BufferedTokenStream::getTokens(int start, int stop, const std::vector &types) { +std::vector BufferedTokenStream::getTokens(size_t start, size_t stop, const std::vector &types) { lazyInit(); - if (start < 0 || stop >= (int)_tokens.size() || stop < 0 || start >= (int)_tokens.size()) { + if (stop >= _tokens.size() || start >= _tokens.size()) { throw IndexOutOfBoundsException(std::string("start ") + std::to_string(start) + std::string(" or stop ") + @@ -250,7 +250,7 @@ std::vector BufferedTokenStream::getTokens(int start, int stop, const s return filteredTokens; } - for (size_t i = (size_t)start; i <= (size_t)stop; i++) { + for (size_t i = start; i <= stop; i++) { Token *tok = _tokens[i].get(); if (types.empty() || std::find(types.begin(), types.end(), tok->getType()) != types.end()) { @@ -260,8 +260,8 @@ std::vector BufferedTokenStream::getTokens(int start, int stop, const s return filteredTokens; } -std::vector BufferedTokenStream::getTokens(int start, int stop, int ttype) { - std::vector s; +std::vector BufferedTokenStream::getTokens(size_t start, size_t stop, size_t ttype) { + std::vector s; s.push_back(ttype); return getTokens(start, stop, s); } @@ -311,7 +311,7 @@ std::vector BufferedTokenStream::getHiddenTokensToRight(size_t tokenInd } ssize_t nextOnChannel = nextTokenOnChannel(tokenIndex + 1, Lexer::DEFAULT_TOKEN_CHANNEL); - ssize_t to; + size_t to; size_t from = tokenIndex + 1; // if none onchannel to right, nextOnChannel=-1 so set to = last token if (nextOnChannel == -1) { @@ -320,7 +320,7 @@ std::vector BufferedTokenStream::getHiddenTokensToRight(size_t tokenInd to = nextOnChannel; } - return filterForChannel(from, (size_t)to, channel); + return filterForChannel(from, to, channel); } std::vector BufferedTokenStream::getHiddenTokensToRight(size_t tokenIndex) { @@ -386,22 +386,22 @@ std::string BufferedTokenStream::getSourceName() const std::string BufferedTokenStream::getText() { lazyInit(); fill(); - return getText(misc::Interval(0, (int)size() - 1)); + return getText(misc::Interval(0, size() - 1)); } std::string BufferedTokenStream::getText(const misc::Interval &interval) { - int start = interval.a; - int stop = interval.b; - if (start < 0 || stop < 0) { + size_t start = interval.a; + size_t stop = interval.b; + if (start == INVALID_INDEX || stop == INVALID_INDEX) { return ""; } lazyInit(); - if (stop >= (int)_tokens.size()) { - stop = (int)_tokens.size() - 1; + if (stop >= _tokens.size()) { + stop = _tokens.size() - 1; } std::stringstream ss; - for (size_t i = (size_t)start; i <= (size_t)stop; i++) { + for (size_t i = start; i <= stop; i++) { Token *t = _tokens[i].get(); if (t->getType() == Token::EOF) { break; diff --git a/runtime/Cpp/runtime/src/BufferedTokenStream.h b/runtime/Cpp/runtime/src/BufferedTokenStream.h index 8d72b9c03..3a88d027f 100755 --- a/runtime/Cpp/runtime/src/BufferedTokenStream.h +++ b/runtime/Cpp/runtime/src/BufferedTokenStream.h @@ -70,21 +70,21 @@ namespace antlr4 { /// Get all tokens from start..stop inclusively. virtual std::vector get(size_t start, size_t stop); - virtual ssize_t LA(ssize_t i) override; + virtual size_t LA(ssize_t i) override; virtual Token* 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(size_t start, size_t 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(size_t start, size_t stop, const std::vector &types); + virtual std::vector getTokens(size_t start, size_t stop, size_t 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 diff --git a/runtime/Cpp/runtime/src/CommonToken.cpp b/runtime/Cpp/runtime/src/CommonToken.cpp index 5fe82df21..36420e6b1 100755 --- a/runtime/Cpp/runtime/src/CommonToken.cpp +++ b/runtime/Cpp/runtime/src/CommonToken.cpp @@ -38,16 +38,18 @@ #include "CommonToken.h" using namespace antlr4; +using namespace antlr4::misc; + using namespace antlrcpp; const std::pair CommonToken::EMPTY_SOURCE; -CommonToken::CommonToken(int type) { +CommonToken::CommonToken(size_t type) { InitializeInstanceFields(); _type = type; } -CommonToken::CommonToken(std::pair source, int type, int channel, int start, int stop) { +CommonToken::CommonToken(std::pair source, size_t type, size_t channel, size_t start, size_t stop) { InitializeInstanceFields(); _source = source; _type = type; @@ -60,7 +62,7 @@ CommonToken::CommonToken(std::pair source, int type, } } -CommonToken::CommonToken(int type, const std::string &text) { +CommonToken::CommonToken(size_t type, const std::string &text) { InitializeInstanceFields(); _type = type; _channel = DEFAULT_CHANNEL; @@ -87,11 +89,11 @@ CommonToken::CommonToken(Token *oldToken) { } } -int CommonToken::getType() const { +size_t CommonToken::getType() const { return _type; } -void CommonToken::setLine(int line) { +void CommonToken::setLine(size_t line) { _line = line; } @@ -105,7 +107,7 @@ std::string CommonToken::getText() const { return ""; } size_t n = input->size(); - if ((size_t)_start < n && (size_t)_stop < n) { + if (_start < n && _stop < n) { return input->getText(misc::Interval(_start, _stop)); } else { return ""; @@ -116,15 +118,15 @@ void CommonToken::setText(const std::string &text) { _text = text; } -int CommonToken::getLine() const { +size_t CommonToken::getLine() const { return _line; } -int CommonToken::getCharPositionInLine() const { +size_t CommonToken::getCharPositionInLine() const { return _charPositionInLine; } -void CommonToken::setCharPositionInLine(int charPositionInLine) { +void CommonToken::setCharPositionInLine(size_t charPositionInLine) { _charPositionInLine = charPositionInLine; } @@ -132,35 +134,35 @@ size_t CommonToken::getChannel() const { return _channel; } -void CommonToken::setChannel(int channel) { +void CommonToken::setChannel(size_t channel) { _channel = channel; } -void CommonToken::setType(int type) { +void CommonToken::setType(size_t type) { _type = type; } -int CommonToken::getStartIndex() const { +size_t CommonToken::getStartIndex() const { return _start; } -void CommonToken::setStartIndex(int start) { +void CommonToken::setStartIndex(size_t start) { _start = start; } -int CommonToken::getStopIndex() const { +size_t CommonToken::getStopIndex() const { return _stop; } -void CommonToken::setStopIndex(int stop) { +void CommonToken::setStopIndex(size_t stop) { _stop = stop; } -int CommonToken::getTokenIndex() const { +size_t CommonToken::getTokenIndex() const { return _index; } -void CommonToken::setTokenIndex(int index) { +void CommonToken::setTokenIndex(size_t index) { _index = index; } @@ -188,8 +190,9 @@ std::string CommonToken::toString() const { txt = ""; } - ss << "[@" << getTokenIndex() << "," << _start << ":" << _stop << "='" << txt << "',<" << _type << ">" << channelStr - << "," << _line << ":" << getCharPositionInLine() << "]"; + ss << "[@" << symbolToNumeric(getTokenIndex()) << "," << symbolToNumeric(_start) << ":" << symbolToNumeric(_stop) + << "='" << txt << "',<" << symbolToNumeric(_type) << ">" << channelStr << "," << _line << ":" + << getCharPositionInLine() << "]"; return ss.str(); } @@ -197,9 +200,9 @@ std::string CommonToken::toString() const { void CommonToken::InitializeInstanceFields() { _type = 0; _line = 0; - _charPositionInLine = -1; + _charPositionInLine = INVALID_INDEX; _channel = DEFAULT_CHANNEL; - _index = -1; + _index = INVALID_INDEX; _start = 0; _stop = 0; _source = EMPTY_SOURCE; diff --git a/runtime/Cpp/runtime/src/CommonToken.h b/runtime/Cpp/runtime/src/CommonToken.h index 199c19119..45303c7f8 100755 --- a/runtime/Cpp/runtime/src/CommonToken.h +++ b/runtime/Cpp/runtime/src/CommonToken.h @@ -46,18 +46,18 @@ namespace antlr4 { /** * This is the backing field for {@link #getType} and {@link #setType}. */ - int _type; + size_t _type; /** * This is the backing field for {@link #getLine} and {@link #setLine}. */ - int _line; + size_t _line; /** * This is the backing field for {@link #getCharPositionInLine} and * {@link #setCharPositionInLine}. */ - int _charPositionInLine; // set to invalid position + size_t _charPositionInLine; // set to invalid position /** * This is the backing field for {@link #getChannel} and @@ -90,19 +90,19 @@ namespace antlr4 { * This is the backing field for {@link #getTokenIndex} and * {@link #setTokenIndex}. */ - int _index; + size_t _index; /** * This is the backing field for {@link #getStartIndex} and * {@link #setStartIndex}. */ - int _start; + size_t _start; /** * This is the backing field for {@link #getStopIndex} and * {@link #setStopIndex}. */ - int _stop; + size_t _stop; public: /** @@ -110,8 +110,8 @@ namespace antlr4 { * * @param type The token type. */ - CommonToken(int type); - CommonToken(std::pair source, int type, int channel, int start, int stop); + CommonToken(size_t type); + CommonToken(std::pair source, size_t type, size_t channel, size_t start, size_t stop); /** * Constructs a new {@link CommonToken} with the specified token type and @@ -120,7 +120,7 @@ namespace antlr4 { * @param type The token type. * @param text The text of the token. */ - CommonToken(int type, const std::string &text); + CommonToken(size_t type, const std::string &text); /** * Constructs a new {@link CommonToken} as a copy of another {@link Token}. @@ -137,7 +137,7 @@ namespace antlr4 { */ CommonToken(Token *oldToken); - virtual int getType() const override; + virtual size_t getType() const override; /** * Explicitly set the text for this token. If {code text} is not @@ -151,25 +151,25 @@ namespace antlr4 { virtual void setText(const std::string &text) override; virtual std::string getText() const override; - virtual void setLine(int line) override; - virtual int getLine() const override; + virtual void setLine(size_t line) override; + virtual size_t getLine() const override; - virtual int getCharPositionInLine() const override; - virtual void setCharPositionInLine(int charPositionInLine) override; + virtual size_t getCharPositionInLine() const override; + virtual void setCharPositionInLine(size_t charPositionInLine) override; virtual size_t getChannel() const override; - virtual void setChannel(int channel) override; + virtual void setChannel(size_t channel) override; - virtual void setType(int type) override; + virtual void setType(size_t type) override; - virtual int getStartIndex() const override; - virtual void setStartIndex(int start); + virtual size_t getStartIndex() const override; + virtual void setStartIndex(size_t start); - virtual int getStopIndex() const override; - virtual void setStopIndex(int stop); + virtual size_t getStopIndex() const override; + virtual void setStopIndex(size_t stop); - virtual int getTokenIndex() const override; - virtual void setTokenIndex(int index) override; + virtual size_t getTokenIndex() const override; + virtual void setTokenIndex(size_t index) override; virtual TokenSource *getTokenSource() const override; virtual CharStream *getInputStream() const override; diff --git a/runtime/Cpp/runtime/src/CommonTokenFactory.cpp b/runtime/Cpp/runtime/src/CommonTokenFactory.cpp index b2a3d5e21..0fe87b2ea 100755 --- a/runtime/Cpp/runtime/src/CommonTokenFactory.cpp +++ b/runtime/Cpp/runtime/src/CommonTokenFactory.cpp @@ -45,8 +45,8 @@ CommonTokenFactory::CommonTokenFactory(bool copyText) : copyText(copyText) { CommonTokenFactory::CommonTokenFactory() : CommonTokenFactory(false) { } -std::unique_ptr CommonTokenFactory::create(std::pair source, int type, - const std::string &text, int channel, int start, int stop, int line, int charPositionInLine) { +std::unique_ptr CommonTokenFactory::create(std::pair source, size_t type, + const std::string &text, size_t channel, size_t start, size_t stop, size_t line, size_t charPositionInLine) { std::unique_ptr t(new CommonToken(source, type, channel, start, stop)); t->setLine(line); @@ -60,6 +60,6 @@ std::unique_ptr CommonTokenFactory::create(std::pair CommonTokenFactory::create(int type, const std::string &text) { +std::unique_ptr CommonTokenFactory::create(size_t type, const std::string &text) { return std::unique_ptr(new CommonToken(type, text)); } diff --git a/runtime/Cpp/runtime/src/CommonTokenFactory.h b/runtime/Cpp/runtime/src/CommonTokenFactory.h index 0454672a1..3bc248df6 100755 --- a/runtime/Cpp/runtime/src/CommonTokenFactory.h +++ b/runtime/Cpp/runtime/src/CommonTokenFactory.h @@ -91,10 +91,10 @@ namespace antlr4 { */ CommonTokenFactory(); - virtual std::unique_ptr create(std::pair source, int type, - const std::string &text, int channel, int start, int stop, int line, int charPositionInLine) override; + virtual std::unique_ptr create(std::pair source, size_t type, + const std::string &text, size_t channel, size_t start, size_t stop, size_t line, size_t charPositionInLine) override; - virtual std::unique_ptr create(int type, const std::string &text) override; + virtual std::unique_ptr create(size_t type, const std::string &text) override; }; } // namespace antlr4 diff --git a/runtime/Cpp/runtime/src/ConsoleErrorListener.cpp b/runtime/Cpp/runtime/src/ConsoleErrorListener.cpp index 161e76d56..3ac7208d4 100755 --- a/runtime/Cpp/runtime/src/ConsoleErrorListener.cpp +++ b/runtime/Cpp/runtime/src/ConsoleErrorListener.cpp @@ -36,6 +36,6 @@ using namespace antlr4; ConsoleErrorListener ConsoleErrorListener::INSTANCE; void ConsoleErrorListener::syntaxError(IRecognizer * /*recognizer*/, Token * /*offendingSymbol*/, - size_t line, int charPositionInLine, const std::string &msg, std::exception_ptr /*e*/) { + size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr /*e*/) { std::cerr << "line " << line << ":" << charPositionInLine << " " << msg << std::endl; } diff --git a/runtime/Cpp/runtime/src/ConsoleErrorListener.h b/runtime/Cpp/runtime/src/ConsoleErrorListener.h index 5805b37df..450e6b97e 100755 --- a/runtime/Cpp/runtime/src/ConsoleErrorListener.h +++ b/runtime/Cpp/runtime/src/ConsoleErrorListener.h @@ -54,7 +54,7 @@ namespace antlr4 { * line line:charPositionInLine msg * */ - virtual void syntaxError(IRecognizer *recognizer, Token * offendingSymbol, size_t line, int charPositionInLine, + virtual void syntaxError(IRecognizer *recognizer, Token * offendingSymbol, size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override; }; diff --git a/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp b/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp index 59b57e32e..9c35185d3 100755 --- a/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp +++ b/runtime/Cpp/runtime/src/DefaultErrorStrategy.cpp @@ -46,6 +46,8 @@ #include "DefaultErrorStrategy.h" using namespace antlr4; +using namespace antlr4::atn; + using namespace antlrcpp; DefaultErrorStrategy::DefaultErrorStrategy() { @@ -113,7 +115,7 @@ void DefaultErrorStrategy::recover(Parser *recognizer, std::exception_ptr /*e*/) } void DefaultErrorStrategy::sync(Parser *recognizer) { - atn::ATNState *s = recognizer->getInterpreter()->atn.states[(size_t)recognizer->getState()]; + atn::ATNState *s = recognizer->getInterpreter()->atn.states[recognizer->getState()]; // If already recovering, don't try to sync if (inErrorRecoveryMode(recognizer)) { @@ -121,10 +123,10 @@ void DefaultErrorStrategy::sync(Parser *recognizer) { } TokenStream *tokens = recognizer->getTokenStream(); - ssize_t la = tokens->LA(1); + size_t la = tokens->LA(1); // try cheaper subset first; might get lucky. seems to shave a wee bit off - if (recognizer->getATN().nextTokens(s).contains((int)la) || la == Token::EOF) { + if (recognizer->getATN().nextTokens(s).contains(la) || la == Token::EOF) { return; } @@ -183,7 +185,7 @@ void DefaultErrorStrategy::reportInputMismatch(Parser *recognizer, const InputMi } void DefaultErrorStrategy::reportFailedPredicate(Parser *recognizer, const FailedPredicateException &e) { - const std::string& ruleName = recognizer->getRuleNames()[(size_t)recognizer->getContext()->getRuleIndex()]; + const std::string& ruleName = recognizer->getRuleNames()[recognizer->getContext()->getRuleIndex()]; std::string msg = "rule " + ruleName + " " + e.what(); recognizer->notifyErrorListeners(e.getOffendingToken(), msg, std::make_exception_ptr(e)); } @@ -243,11 +245,11 @@ bool DefaultErrorStrategy::singleTokenInsertion(Parser *recognizer) { // if current token is consistent with what could come after current // ATN state, then we know we're missing a token; error recovery // is free to conjure up and insert the missing token - atn::ATNState *currentState = recognizer->getInterpreter()->atn.states[(size_t)recognizer->getState()]; + atn::ATNState *currentState = recognizer->getInterpreter()->atn.states[recognizer->getState()]; atn::ATNState *next = currentState->transition(0)->target; const atn::ATN &atn = recognizer->getInterpreter()->atn; misc::IntervalSet expectingAtLL2 = atn.nextTokens(next, recognizer->getContext()); - if (expectingAtLL2.contains((int)currentSymbolType)) { + if (expectingAtLL2.contains(currentSymbolType)) { reportMissingToken(recognizer); return true; } @@ -255,9 +257,9 @@ bool DefaultErrorStrategy::singleTokenInsertion(Parser *recognizer) { } Token* DefaultErrorStrategy::singleTokenDeletion(Parser *recognizer) { - ssize_t nextTokenType = recognizer->getInputStream()->LA(2); + size_t nextTokenType = recognizer->getInputStream()->LA(2); misc::IntervalSet expecting = getExpectedTokens(recognizer); - if (expecting.contains((int)nextTokenType)) { + if (expecting.contains(nextTokenType)) { reportUnwantedToken(recognizer); recognizer->consume(); // simply delete extra token // we want to return the token we're actually matching @@ -271,7 +273,7 @@ Token* DefaultErrorStrategy::singleTokenDeletion(Parser *recognizer) { Token* DefaultErrorStrategy::getMissingSymbol(Parser *recognizer) { Token *currentSymbol = recognizer->getCurrentToken(); misc::IntervalSet expecting = getExpectedTokens(recognizer); - ssize_t expectedTokenType = expecting.getMinElement(); // get any element + size_t expectedTokenType = expecting.getMinElement(); // get any element std::string tokenText; if (expectedTokenType == Token::EOF) { tokenText = ""; @@ -315,7 +317,7 @@ std::string DefaultErrorStrategy::getSymbolText(Token *symbol) { return symbol->getText(); } -int DefaultErrorStrategy::getSymbolType(Token *symbol) { +size_t DefaultErrorStrategy::getSymbolType(Token *symbol) { return symbol->getType(); } @@ -331,9 +333,9 @@ misc::IntervalSet DefaultErrorStrategy::getErrorRecoverySet(Parser *recognizer) const atn::ATN &atn = recognizer->getInterpreter()->atn; RuleContext *ctx = recognizer->getContext().get(); misc::IntervalSet recoverSet; - while (ctx->invokingState >= 0) { + while (ctx->invokingState != ATNState::INVALID_STATE_NUMBER) { // compute what follows who invoked us - atn::ATNState *invokingState = atn.states[(size_t)ctx->invokingState]; + atn::ATNState *invokingState = atn.states[ctx->invokingState]; atn::RuleTransition *rt = dynamic_cast(invokingState->transition(0)); misc::IntervalSet follow = atn.nextTokens(rt->followState); recoverSet.addAll(follow); @@ -348,8 +350,8 @@ misc::IntervalSet DefaultErrorStrategy::getErrorRecoverySet(Parser *recognizer) } void DefaultErrorStrategy::consumeUntil(Parser *recognizer, const misc::IntervalSet &set) { - ssize_t ttype = recognizer->getInputStream()->LA(1); - while (ttype != Token::EOF && !set.contains((int)ttype)) { + size_t ttype = recognizer->getInputStream()->LA(1); + while (ttype != Token::EOF && !set.contains(ttype)) { recognizer->consume(); ttype = recognizer->getInputStream()->LA(1); } diff --git a/runtime/Cpp/runtime/src/DefaultErrorStrategy.h b/runtime/Cpp/runtime/src/DefaultErrorStrategy.h index 731b6a947..f13b57a64 100755 --- a/runtime/Cpp/runtime/src/DefaultErrorStrategy.h +++ b/runtime/Cpp/runtime/src/DefaultErrorStrategy.h @@ -382,7 +382,7 @@ namespace antlr4 { virtual std::string getSymbolText(Token *symbol); - virtual int getSymbolType(Token *symbol); + virtual size_t getSymbolType(Token *symbol); virtual std::string escapeWSAndQuote(const std::string &s) const; diff --git a/runtime/Cpp/runtime/src/DiagnosticErrorListener.cpp b/runtime/Cpp/runtime/src/DiagnosticErrorListener.cpp index 08ba4e144..5ec71e825 100755 --- a/runtime/Cpp/runtime/src/DiagnosticErrorListener.cpp +++ b/runtime/Cpp/runtime/src/DiagnosticErrorListener.cpp @@ -54,7 +54,7 @@ void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, const dfa::DFA std::string decision = getDecisionDescription(recognizer, dfa); antlrcpp::BitSet conflictingAlts = getConflictingAlts(ambigAlts, configs); - std::string text = recognizer->getTokenStream()->getText(misc::Interval((int)startIndex, (int)stopIndex)); + std::string text = recognizer->getTokenStream()->getText(misc::Interval(startIndex, stopIndex)); std::string message = "reportAmbiguity d=" + decision + ": ambigAlts=" + conflictingAlts.toString() + ", input='" + text + "'"; @@ -64,29 +64,29 @@ void DiagnosticErrorListener::reportAmbiguity(Parser *recognizer, const dfa::DFA void DiagnosticErrorListener::reportAttemptingFullContext(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, const antlrcpp::BitSet &/*conflictingAlts*/, atn::ATNConfigSet * /*configs*/) { std::string decision = getDecisionDescription(recognizer, dfa); - std::string text = recognizer->getTokenStream()->getText(misc::Interval((int)startIndex, (int)stopIndex)); + std::string text = recognizer->getTokenStream()->getText(misc::Interval(startIndex, stopIndex)); std::string message = "reportAttemptingFullContext d=" + decision + ", input='" + text + "'"; recognizer->notifyErrorListeners(message); } void DiagnosticErrorListener::reportContextSensitivity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, - size_t stopIndex, int /*prediction*/, atn::ATNConfigSet * /*configs*/) { + size_t stopIndex, size_t /*prediction*/, atn::ATNConfigSet * /*configs*/) { std::string decision = getDecisionDescription(recognizer, dfa); - std::string text = recognizer->getTokenStream()->getText(misc::Interval((int)startIndex, (int)stopIndex)); + std::string text = recognizer->getTokenStream()->getText(misc::Interval(startIndex, stopIndex)); std::string message = "reportContextSensitivity d=" + decision + ", input='" + text + "'"; recognizer->notifyErrorListeners(message); } std::string DiagnosticErrorListener::getDecisionDescription(Parser *recognizer, const dfa::DFA &dfa) { - int decision = dfa.decision; - int ruleIndex = ((atn::ATNState*)dfa.atnStartState)->ruleIndex; + size_t decision = dfa.decision; + size_t ruleIndex = ((atn::ATNState*)dfa.atnStartState)->ruleIndex; const std::vector& ruleNames = recognizer->getRuleNames(); - if (ruleIndex < 0 || ruleIndex >= (int)ruleNames.size()) { + if (ruleIndex == INVALID_INDEX || ruleIndex >= ruleNames.size()) { return std::to_string(decision); } - std::string ruleName = ruleNames[(size_t)ruleIndex]; + std::string ruleName = ruleNames[ruleIndex]; if (ruleName == "" || ruleName.empty()) { return std::to_string(decision); } @@ -103,7 +103,7 @@ antlrcpp::BitSet DiagnosticErrorListener::getConflictingAlts(const antlrcpp::Bit antlrcpp::BitSet result; for (auto &config : configs->configs) { - result.set((size_t)config->alt); + result.set(config->alt); } return result; diff --git a/runtime/Cpp/runtime/src/DiagnosticErrorListener.h b/runtime/Cpp/runtime/src/DiagnosticErrorListener.h index 37371ba6f..a331cd511 100755 --- a/runtime/Cpp/runtime/src/DiagnosticErrorListener.h +++ b/runtime/Cpp/runtime/src/DiagnosticErrorListener.h @@ -85,7 +85,7 @@ namespace antlr4 { const antlrcpp::BitSet &conflictingAlts, atn::ATNConfigSet *configs) override; virtual void reportContextSensitivity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, - int prediction, atn::ATNConfigSet *configs) override; + size_t prediction, atn::ATNConfigSet *configs) override; protected: virtual std::string getDecisionDescription(Parser *recognizer, const dfa::DFA &dfa); diff --git a/runtime/Cpp/runtime/src/FailedPredicateException.cpp b/runtime/Cpp/runtime/src/FailedPredicateException.cpp index ff2834a50..439a495c9 100755 --- a/runtime/Cpp/runtime/src/FailedPredicateException.cpp +++ b/runtime/Cpp/runtime/src/FailedPredicateException.cpp @@ -51,7 +51,7 @@ FailedPredicateException::FailedPredicateException(Parser *recognizer, const std : RecognitionException(!message.empty() ? message : "failed predicate: " + predicate + "?", recognizer, recognizer->getInputStream(), recognizer->getContext(), recognizer->getCurrentToken()) { - atn::ATNState *s = recognizer->getInterpreter()->atn.states[(size_t)recognizer->getState()]; + atn::ATNState *s = recognizer->getInterpreter()->atn.states[recognizer->getState()]; atn::Transition *transition = s->transition(0); if (is(transition)) { _ruleIndex = ((atn::PredicateTransition *)transition)->ruleIndex; @@ -65,11 +65,11 @@ FailedPredicateException::FailedPredicateException(Parser *recognizer, const std _predicate = predicate; } -int FailedPredicateException::getRuleIndex() { +size_t FailedPredicateException::getRuleIndex() { return _ruleIndex; } -int FailedPredicateException::getPredIndex() { +size_t FailedPredicateException::getPredIndex() { return _predicateIndex; } diff --git a/runtime/Cpp/runtime/src/FailedPredicateException.h b/runtime/Cpp/runtime/src/FailedPredicateException.h index eb9970fdf..a5d5b03be 100755 --- a/runtime/Cpp/runtime/src/FailedPredicateException.h +++ b/runtime/Cpp/runtime/src/FailedPredicateException.h @@ -45,13 +45,13 @@ namespace antlr4 { FailedPredicateException(Parser *recognizer, const std::string &predicate); FailedPredicateException(Parser *recognizer, const std::string &predicate, const std::string &message); - virtual int getRuleIndex(); - virtual int getPredIndex(); + virtual size_t getRuleIndex(); + virtual size_t getPredIndex(); virtual std::string getPredicate(); private: - int _ruleIndex; - int _predicateIndex; + size_t _ruleIndex; + size_t _predicateIndex; std::string _predicate; }; diff --git a/runtime/Cpp/runtime/src/IRecognizer.h b/runtime/Cpp/runtime/src/IRecognizer.h index 18e32ce9c..c172f5644 100644 --- a/runtime/Cpp/runtime/src/IRecognizer.h +++ b/runtime/Cpp/runtime/src/IRecognizer.h @@ -37,7 +37,7 @@ namespace antlr4 { public: virtual ~IRecognizer() {}; - virtual int getState() = 0; + virtual size_t getState() const = 0; // Get the ATN used by the recognizer for prediction. virtual const atn::ATN& getATN() const = 0; diff --git a/runtime/Cpp/runtime/src/IntStream.h b/runtime/Cpp/runtime/src/IntStream.h index b2024e7cd..84629411a 100755 --- a/runtime/Cpp/runtime/src/IntStream.h +++ b/runtime/Cpp/runtime/src/IntStream.h @@ -53,7 +53,7 @@ namespace antlr4 { /// class ANTLR4CPP_PUBLIC IntStream { public: - static const ssize_t EOF = -1; + static const size_t EOF = (size_t)-1; /// The value returned by when the end of the stream is /// reached. @@ -124,7 +124,7 @@ namespace antlr4 { /// /// if the stream does not support /// retrieving the value of the specified symbol - virtual ssize_t LA(ssize_t i) = 0; + virtual size_t LA(ssize_t i) = 0; /// /// A mark provides a guarantee that operations will be diff --git a/runtime/Cpp/runtime/src/InterpreterRuleContext.cpp b/runtime/Cpp/runtime/src/InterpreterRuleContext.cpp index d914f2402..2a213dda2 100755 --- a/runtime/Cpp/runtime/src/InterpreterRuleContext.cpp +++ b/runtime/Cpp/runtime/src/InterpreterRuleContext.cpp @@ -36,10 +36,11 @@ using namespace antlr4; InterpreterRuleContext::InterpreterRuleContext() { } -InterpreterRuleContext::InterpreterRuleContext(std::weak_ptr parent, int invokingStateNumber, ssize_t ruleIndex) +InterpreterRuleContext::InterpreterRuleContext(std::weak_ptr parent, size_t invokingStateNumber, + size_t ruleIndex) : ParserRuleContext(parent, invokingStateNumber), _ruleIndex(ruleIndex) { } -ssize_t InterpreterRuleContext::getRuleIndex() const { +size_t InterpreterRuleContext::getRuleIndex() const { return _ruleIndex; } diff --git a/runtime/Cpp/runtime/src/InterpreterRuleContext.h b/runtime/Cpp/runtime/src/InterpreterRuleContext.h index 9759d1021..c8fd1dac4 100755 --- a/runtime/Cpp/runtime/src/InterpreterRuleContext.h +++ b/runtime/Cpp/runtime/src/InterpreterRuleContext.h @@ -59,13 +59,13 @@ namespace antlr4 { * @param invokingStateNumber The invoking state number. * @param ruleIndex The rule index for the current context. */ - InterpreterRuleContext(std::weak_ptr parent, int invokingStateNumber, ssize_t ruleIndex); + InterpreterRuleContext(std::weak_ptr parent, size_t invokingStateNumber, size_t ruleIndex); - virtual ssize_t getRuleIndex() const override; + virtual size_t getRuleIndex() const override; protected: /** This is the backing field for {@link #getRuleIndex}. */ - const ssize_t _ruleIndex = -1; + const size_t _ruleIndex = INVALID_INDEX; }; } // namespace antlr4 diff --git a/runtime/Cpp/runtime/src/Lexer.cpp b/runtime/Cpp/runtime/src/Lexer.cpp index 7ee339ffb..cc0b8bc35 100755 --- a/runtime/Cpp/runtime/src/Lexer.cpp +++ b/runtime/Cpp/runtime/src/Lexer.cpp @@ -96,13 +96,13 @@ std::unique_ptr Lexer::nextToken() { token.reset(); channel = Token::DEFAULT_CHANNEL; - tokenStartCharIndex = (int)_input->index(); + tokenStartCharIndex = _input->index(); tokenStartCharPositionInLine = getInterpreter()->getCharPositionInLine(); - tokenStartLine = (int)getInterpreter()->getLine(); + tokenStartLine = getInterpreter()->getLine(); _text = ""; do { type = Token::INVALID_TYPE; - int ttype; + size_t ttype; try { ttype = getInterpreter()->match(_input, mode); } catch (LexerNoViableAltException &e) { @@ -184,16 +184,15 @@ void Lexer::emit(std::unique_ptr newToken) { } Token* Lexer::emit() { - emit(_factory->create({ this, _input }, (int)type, _text, channel, - tokenStartCharIndex, getCharIndex() - 1, (int)tokenStartLine, tokenStartCharPositionInLine)); + emit(_factory->create({ this, _input }, type, _text, channel, + tokenStartCharIndex, getCharIndex() - 1, tokenStartLine, tokenStartCharPositionInLine)); return token.get(); } Token* Lexer::emitEOF() { - int cpos = getCharPositionInLine(); + size_t cpos = getCharPositionInLine(); size_t line = getLine(); - emit(_factory->create({ this, _input }, EOF, "", Token::DEFAULT_CHANNEL, (int)_input->index(), - (int)_input->index() - 1, (int)line, cpos)); + emit(_factory->create({ this, _input }, EOF, "", Token::DEFAULT_CHANNEL, _input->index(), _input->index() - 1, line, cpos)); return token.get(); } @@ -201,7 +200,7 @@ size_t Lexer::getLine() const { return getInterpreter()->getLine(); } -int Lexer::getCharPositionInLine() { +size_t Lexer::getCharPositionInLine() { return getInterpreter()->getCharPositionInLine(); } @@ -209,12 +208,12 @@ void Lexer::setLine(size_t line) { getInterpreter()->setLine(line); } -void Lexer::setCharPositionInLine(int charPositionInLine) { +void Lexer::setCharPositionInLine(size_t charPositionInLine) { getInterpreter()->setCharPositionInLine(charPositionInLine); } -int Lexer::getCharIndex() { - return (int)_input->index(); +size_t Lexer::getCharIndex() { + return _input->index(); } std::string Lexer::getText() { @@ -236,19 +235,19 @@ void Lexer::setToken(std::unique_ptr newToken) { token = std::move(newToken); } -void Lexer::setType(ssize_t ttype) { +void Lexer::setType(size_t ttype) { type = ttype; } -ssize_t Lexer::getType() { +size_t Lexer::getType() { return type; } -void Lexer::setChannel(int newChannel) { +void Lexer::setChannel(size_t newChannel) { channel = newChannel; } -int Lexer::getChannel() { +size_t Lexer::getChannel() { return channel; } @@ -271,7 +270,7 @@ void Lexer::recover(const LexerNoViableAltException &/*e*/) { void Lexer::notifyListeners(const LexerNoViableAltException & /*e*/) { ++_syntaxErrors; - std::string text = _input->getText(misc::Interval(tokenStartCharIndex, (int)_input->index())); + std::string text = _input->getText(misc::Interval(tokenStartCharIndex, _input->index())); std::string msg = std::string("token recognition error at: '") + getErrorDisplay(text) + std::string("'"); ProxyErrorListener &listener = getErrorListenerDispatch(); diff --git a/runtime/Cpp/runtime/src/Lexer.h b/runtime/Cpp/runtime/src/Lexer.h index de9e7cb6b..36bb5600c 100755 --- a/runtime/Cpp/runtime/src/Lexer.h +++ b/runtime/Cpp/runtime/src/Lexer.h @@ -44,12 +44,12 @@ namespace antlr4 { /// of speed. class ANTLR4CPP_PUBLIC Lexer : public Recognizer, public TokenSource { public: - static const int DEFAULT_MODE = 0; - static const int MORE = -2; - static const int SKIP = -3; + static const size_t DEFAULT_MODE = 0; + static const size_t MORE = (size_t)-2; + static const size_t SKIP = (size_t)-3; static const size_t DEFAULT_TOKEN_CHANNEL = Token::DEFAULT_CHANNEL; - static const int HIDDEN = Token::HIDDEN_CHANNEL; + static const size_t HIDDEN = Token::HIDDEN_CHANNEL; static const size_t MIN_CHAR_VALUE = 0; static const size_t MAX_CHAR_VALUE = 0x10FFFF; @@ -78,29 +78,24 @@ namespace antlr4 { /// Needed, for example, to get the text for current token. Set at /// the start of nextToken. /// - int tokenStartCharIndex; + size_t tokenStartCharIndex; /// /// The line on which the first character of the token resides size_t tokenStartLine; - /// - /// The character position of first character within the line - int tokenStartCharPositionInLine; + /// The character position of first character within the line. + size_t tokenStartCharPositionInLine; - /// /// Once we see EOF on char stream, next token will be EOF. - /// If you have DONE : EOF ; then you see DONE EOF. - /// + /// If you have DONE : EOF ; then you see DONE EOF. bool hitEOF; - /// - /// The channel number for the current token - int channel; + /// The channel number for the current token. + size_t channel; - /// - /// The token type for the current token - ssize_t type; + /// The token type for the current token. + size_t type; // Use the vector as a stack. std::vector modeStack; @@ -156,14 +151,14 @@ namespace antlr4 { virtual size_t getLine() const override; - virtual int getCharPositionInLine() override; + virtual size_t getCharPositionInLine() override; virtual void setLine(size_t line); - virtual void setCharPositionInLine(int charPositionInLine); + virtual void setCharPositionInLine(size_t charPositionInLine); /// What is the index of the current character of lookahead? - virtual int getCharIndex(); + virtual size_t getCharIndex(); /// Return the text matched so far for the current token or any /// text override. @@ -178,13 +173,13 @@ namespace antlr4 { virtual void setToken(std::unique_ptr newToken); - virtual void setType(ssize_t ttype); + virtual void setType(size_t ttype); - virtual ssize_t getType(); + virtual size_t getType(); - virtual void setChannel(int newChannel); + virtual void setChannel(size_t newChannel); - virtual int getChannel(); + virtual size_t getChannel(); virtual const std::vector& getModeNames() const = 0; diff --git a/runtime/Cpp/runtime/src/LexerInterpreter.cpp b/runtime/Cpp/runtime/src/LexerInterpreter.cpp index af87643ff..0092f4d57 100755 --- a/runtime/Cpp/runtime/src/LexerInterpreter.cpp +++ b/runtime/Cpp/runtime/src/LexerInterpreter.cpp @@ -60,8 +60,8 @@ LexerInterpreter::LexerInterpreter(const std::string &grammarFileName, const dfa _tokenNames.push_back(vocabulary.getDisplayName(i)); } - for (size_t i = 0; i < (size_t)atn.getNumberOfDecisions(); ++i) { - _decisionToDFA.push_back(dfa::DFA(_atn.getDecisionState((int)i), (int)i)); + for (size_t i = 0; i < atn.getNumberOfDecisions(); ++i) { + _decisionToDFA.push_back(dfa::DFA(_atn.getDecisionState(i), i)); } _interpreter = new atn::LexerATNSimulator(_atn, _decisionToDFA, _sharedContextCache); /* mem-check: deleted in d-tor */ } diff --git a/runtime/Cpp/runtime/src/LexerNoViableAltException.cpp b/runtime/Cpp/runtime/src/LexerNoViableAltException.cpp index 3fc056301..de0ad4856 100755 --- a/runtime/Cpp/runtime/src/LexerNoViableAltException.cpp +++ b/runtime/Cpp/runtime/src/LexerNoViableAltException.cpp @@ -54,7 +54,7 @@ atn::ATNConfigSet* LexerNoViableAltException::getDeadEndConfigs() { std::string LexerNoViableAltException::toString() { std::string symbol; if (_startIndex < getInputStream()->size()) { - symbol = ((CharStream *)getInputStream())->getText(misc::Interval((int)_startIndex, (int)_startIndex)); + symbol = ((CharStream *)getInputStream())->getText(misc::Interval(_startIndex, _startIndex)); symbol = antlrcpp::escapeWhitespace(symbol, false); } std::string format = "LexerNoViableAltException('" + symbol + "')"; diff --git a/runtime/Cpp/runtime/src/ListTokenSource.cpp b/runtime/Cpp/runtime/src/ListTokenSource.cpp index 69b1985fe..c49eb4d3f 100755 --- a/runtime/Cpp/runtime/src/ListTokenSource.cpp +++ b/runtime/Cpp/runtime/src/ListTokenSource.cpp @@ -50,19 +50,19 @@ ListTokenSource::ListTokenSource(std::vector> tokens_, co // Check if there is an eof token and create one if not. if (tokens.back()->getType() != Token::EOF) { Token *lastToken = tokens.back().get(); - int start = -1; - int previousStop = lastToken->getStopIndex(); - if (previousStop != -1) { + size_t start = INVALID_INDEX; + size_t previousStop = lastToken->getStopIndex(); + if (previousStop != INVALID_INDEX) { start = previousStop + 1; } - int stop = std::max(-1, start - 1); + size_t stop = std::max(INVALID_INDEX, start - 1); tokens.emplace_back((_factory->create({ this, getInputStream() }, Token::EOF, "EOF", Token::DEFAULT_CHANNEL, start, stop, (int)lastToken->getLine(), lastToken->getCharPositionInLine()))); } } -int ListTokenSource::getCharPositionInLine() { +size_t ListTokenSource::getCharPositionInLine() { if (i < tokens.size()) { return tokens[i]->getCharPositionInLine(); } @@ -78,7 +78,7 @@ std::unique_ptr ListTokenSource::nextToken() { size_t ListTokenSource::getLine() const { if (i < tokens.size()) { - return (size_t)tokens[i]->getLine(); + return tokens[i]->getLine(); } return 1; diff --git a/runtime/Cpp/runtime/src/ListTokenSource.h b/runtime/Cpp/runtime/src/ListTokenSource.h index f1a2d3fe0..ce6989844 100755 --- a/runtime/Cpp/runtime/src/ListTokenSource.h +++ b/runtime/Cpp/runtime/src/ListTokenSource.h @@ -94,7 +94,7 @@ namespace antlr4 { /// if {@code tokens} is {@code null} ListTokenSource(std::vector> tokens_, const std::string &sourceName_); - virtual int getCharPositionInLine() override; + virtual size_t getCharPositionInLine() override; virtual std::unique_ptr nextToken() override; virtual size_t getLine() const override; virtual CharStream* getInputStream() override; diff --git a/runtime/Cpp/runtime/src/Parser.cpp b/runtime/Cpp/runtime/src/Parser.cpp index fb6517cea..348b9bc72 100755 --- a/runtime/Cpp/runtime/src/Parser.cpp +++ b/runtime/Cpp/runtime/src/Parser.cpp @@ -52,6 +52,8 @@ #include "Parser.h" using namespace antlr4; +using namespace antlr4::atn; + using namespace antlrcpp; std::map, atn::ATN> Parser::bypassAltsAtnCache; @@ -60,20 +62,20 @@ Parser::TraceListener::TraceListener(Parser *outerInstance) : outerInstance(oute } void Parser::TraceListener::enterEveryRule(ParserRuleContext *ctx) { - std::cout << "enter " << outerInstance->getRuleNames()[(size_t)ctx->getRuleIndex()] + std::cout << "enter " << outerInstance->getRuleNames()[ctx->getRuleIndex()] << ", LT(1)=" << outerInstance->_input->LT(1)->getText() << std::endl; } void Parser::TraceListener::visitTerminal(tree::TerminalNode *node) { std::cout << "consume " << node->getSymbol() << " rule " - << outerInstance->getRuleNames()[(size_t)outerInstance->getContext()->getRuleIndex()] << std::endl; + << outerInstance->getRuleNames()[outerInstance->getContext()->getRuleIndex()] << std::endl; } void Parser::TraceListener::visitErrorNode(tree::ErrorNode * /*node*/) { } void Parser::TraceListener::exitEveryRule(ParserRuleContext *ctx) { - std::cout << "exit " << outerInstance->getRuleNames()[(size_t)ctx->getRuleIndex()] + std::cout << "exit " << outerInstance->getRuleNames()[ctx->getRuleIndex()] << ", LT(1)=" << outerInstance->_input->LT(1)->getText() << std::endl; } @@ -118,7 +120,7 @@ void Parser::reset() { } } -Token* Parser::match(int ttype) { +Token* Parser::match(size_t ttype) { Token *t = getCurrentToken(); if (t->getType() == ttype) { if (ttype == EOF) { @@ -128,7 +130,7 @@ Token* Parser::match(int ttype) { consume(); } else { t = _errHandler->recoverInline(this); - if (_buildParseTrees && t->getTokenIndex() == -1) { + if (_buildParseTrees && t->getTokenIndex() == INVALID_INDEX) { // we must have conjured up a new token during single token insertion // if it's not the current symbol _ctx->addErrorNode(t); @@ -144,7 +146,7 @@ Token* Parser::matchWildcard() { consume(); } else { t = _errHandler->recoverInline(this); - if (_buildParseTrees && t->getTokenIndex() == -1) { + if (_buildParseTrees && t->getTokenIndex() == INVALID_INDEX) { // we must have conjured up a new token during single token insertion // if it's not the current symbol _ctx->addErrorNode(t); @@ -301,13 +303,11 @@ void Parser::notifyErrorListeners(const std::string &msg) { void Parser::notifyErrorListeners(Token *offendingToken, const std::string &msg, std::exception_ptr e) { _syntaxErrors++; - int line = -1; - int charPositionInLine = -1; - line = offendingToken->getLine(); - charPositionInLine = offendingToken->getCharPositionInLine(); + size_t line = offendingToken->getLine(); + size_t charPositionInLine = offendingToken->getCharPositionInLine(); ProxyErrorListener &listener = getErrorListenerDispatch(); - listener.syntaxError(this, offendingToken, (size_t)line, charPositionInLine, msg, e); + listener.syntaxError(this, offendingToken, line, charPositionInLine, msg, e); } Token* Parser::consume() { @@ -345,7 +345,7 @@ void Parser::addContextToParseTree() { parent->addChild(_ctx); } -void Parser::enterRule(Ref const& localctx, int state, int /*ruleIndex*/) { +void Parser::enterRule(Ref const& localctx, size_t state, size_t /*ruleIndex*/) { setState(state); _ctx = localctx; _ctx->start = _input->LT(1); @@ -396,21 +396,21 @@ int Parser::getPrecedence() const { return _precedenceStack.back(); } -void Parser::enterRecursionRule(Ref const& localctx, int ruleIndex) { - enterRecursionRule(localctx, getATN().ruleToStartState[(size_t)ruleIndex]->stateNumber, ruleIndex, 0); +void Parser::enterRecursionRule(Ref const& localctx, size_t ruleIndex) { + enterRecursionRule(localctx, getATN().ruleToStartState[ruleIndex]->stateNumber, ruleIndex, 0); } -void Parser::enterRecursionRule(Ref const& localctx, int state, int /*ruleIndex*/, int precedence) { +void Parser::enterRecursionRule(Ref const& localctx, size_t state, size_t /*ruleIndex*/, int precedence) { setState(state); _precedenceStack.push_back(precedence); _ctx = localctx; _ctx->start = _input->LT(1); - if (_parseListeners.size() > 0) { + if (!_parseListeners.empty()) { triggerEnterRuleEvent(); // simulates rule entry for left-recursive rules } } -void Parser::pushNewRecursionContext(Ref const& localctx, int state, int /*ruleIndex*/) { +void Parser::pushNewRecursionContext(Ref const& localctx, size_t state, size_t /*ruleIndex*/) { Ref previous = _ctx; previous->parent = localctx; previous->invokingState = state; @@ -451,7 +451,7 @@ void Parser::unrollRecursionContexts(Ref const& parentctx) { } } -Ref Parser::getInvokingContext(int ruleIndex) { +Ref Parser::getInvokingContext(size_t ruleIndex) { Ref p = _ctx; while (p) { if (p->getRuleIndex() == ruleIndex) { @@ -481,10 +481,10 @@ bool Parser::inContext(const std::string &/*context*/) { return false; } -bool Parser::isExpectedToken(int symbol) { +bool Parser::isExpectedToken(size_t symbol) { const atn::ATN &atn = getInterpreter()->atn; Ref ctx = _ctx; - atn::ATNState *s = atn.states[(size_t)getState()]; + atn::ATNState *s = atn.states[getState()]; misc::IntervalSet following = atn.nextTokens(s); if (following.contains(symbol)) { @@ -495,8 +495,8 @@ bool Parser::isExpectedToken(int symbol) { return false; } - while (ctx && ctx->invokingState >= 0 && following.contains(Token::EPSILON)) { - atn::ATNState *invokingState = atn.states[(size_t)ctx->invokingState]; + while (ctx && ctx->invokingState != ATNState::INVALID_STATE_NUMBER && following.contains(Token::EPSILON)) { + atn::ATNState *invokingState = atn.states[ctx->invokingState]; atn::RuleTransition *rt = static_cast(invokingState->transition(0)); following = atn.nextTokens(rt->followState); if (following.contains(symbol)) { @@ -523,15 +523,15 @@ misc::IntervalSet Parser::getExpectedTokens() { misc::IntervalSet Parser::getExpectedTokensWithinCurrentRule() { const atn::ATN &atn = getInterpreter()->atn; - atn::ATNState *s = atn.states[(size_t)getState()]; + atn::ATNState *s = atn.states[getState()]; return atn.nextTokens(s); } -ssize_t Parser::getRuleIndex(const std::string &ruleName) { +size_t Parser::getRuleIndex(const std::string &ruleName) { const std::map &m = getRuleIndexMap(); auto iterator = m.find(ruleName); if (iterator == m.end()) { - return -1; + return INVALID_INDEX; } return iterator->second; } @@ -550,11 +550,11 @@ std::vector Parser::getRuleInvocationStack(Ref const& RuleContext *run = p.get(); while (run != nullptr) { // compute what follows who invoked us - ssize_t ruleIndex = run->getRuleIndex(); - if (ruleIndex < 0) { + size_t ruleIndex = run->getRuleIndex(); + if (ruleIndex == INVALID_INDEX ) { stack.push_back("n/a"); } else { - stack.push_back(ruleNames[(size_t)ruleIndex]); + stack.push_back(ruleNames[ruleIndex]); } if (p->parent.expired()) break; diff --git a/runtime/Cpp/runtime/src/Parser.h b/runtime/Cpp/runtime/src/Parser.h index 129d628e1..985035b9d 100755 --- a/runtime/Cpp/runtime/src/Parser.h +++ b/runtime/Cpp/runtime/src/Parser.h @@ -92,7 +92,7 @@ namespace antlr4 { /// if the current input symbol did not match /// {@code ttype} and the error strategy could not recover from the /// mismatched symbol - virtual Token* match(int ttype); + virtual Token* match(size_t ttype); /// /// Match current input symbol as a wildcard. If the symbol type matches @@ -298,7 +298,7 @@ namespace antlr4 { /// Always called by generated parsers upon entry to a rule. Access field /// get the current context. /// - virtual void enterRule(Ref const& localctx, int state, int ruleIndex); + virtual void enterRule(Ref const& localctx, size_t state, size_t ruleIndex); virtual void exitRule(); @@ -314,15 +314,15 @@ namespace antlr4 { /// @deprecated Use /// instead. - virtual void enterRecursionRule(Ref const& localctx, int ruleIndex); - virtual void enterRecursionRule(Ref const& localctx, int state, int ruleIndex, int precedence); + virtual void enterRecursionRule(Ref const& localctx, size_t ruleIndex); + virtual void enterRecursionRule(Ref const& localctx, size_t state, size_t ruleIndex, int precedence); /** Like {@link #enterRule} but for recursive rules. * Make the current context the child of the incoming localctx. */ - virtual void pushNewRecursionContext(Ref const& localctx, int state, int ruleIndex); + virtual void pushNewRecursionContext(Ref const& localctx, size_t state, size_t ruleIndex); virtual void unrollRecursionContexts(Ref const& parentctx); - virtual Ref getInvokingContext(int ruleIndex); + virtual Ref getInvokingContext(size_t ruleIndex); virtual Ref getContext(); virtual void setContext(Ref const& ctx); virtual bool precpred(Ref const& localctx, int precedence) override; @@ -341,7 +341,7 @@ namespace antlr4 { /// the symbol type to check /// {@code true} if {@code symbol} can follow the current state in /// the ATN, otherwise {@code false}. - virtual bool isExpectedToken(int symbol); + virtual bool isExpectedToken(size_t symbol); bool isMatchedEOF() const; @@ -355,9 +355,8 @@ namespace antlr4 { virtual misc::IntervalSet getExpectedTokensWithinCurrentRule(); - /// - /// Get a rule's index (i.e., {@code RULE_ruleName} field) or -1 if not found. - virtual ssize_t getRuleIndex(const std::string &ruleName); + /// Get a rule's index (i.e., {@code RULE_ruleName} field) or INVALID_INDEX if not found. + virtual size_t getRuleIndex(const std::string &ruleName); virtual Ref getRuleContext(); diff --git a/runtime/Cpp/runtime/src/ParserInterpreter.cpp b/runtime/Cpp/runtime/src/ParserInterpreter.cpp index e28414375..7c43a53d5 100755 --- a/runtime/Cpp/runtime/src/ParserInterpreter.cpp +++ b/runtime/Cpp/runtime/src/ParserInterpreter.cpp @@ -55,6 +55,8 @@ using namespace antlr4; using namespace antlr4::atn; +using namespace antlr4::misc; + using namespace antlrcpp; ParserInterpreter::ParserInterpreter(const std::string &grammarFileName, const std::vector& tokenNames, @@ -71,7 +73,7 @@ ParserInterpreter::ParserInterpreter(const std::string &grammarFileName, const d } // init decision DFA - for (int i = 0; i < atn.getNumberOfDecisions(); ++i) { + for (size_t i = 0; i < atn.getNumberOfDecisions(); ++i) { atn::DecisionState *decisionState = atn.getDecisionState(i); _decisionToDFA.push_back(dfa::DFA(decisionState, i)); } @@ -110,8 +112,8 @@ std::string ParserInterpreter::getGrammarFileName() const { return _grammarFileName; } -Ref ParserInterpreter::parse(int startRuleIndex) { - atn::RuleStartState *startRuleStartState = _atn.ruleToStartState[(size_t)startRuleIndex]; +Ref ParserInterpreter::parse(size_t startRuleIndex) { + atn::RuleStartState *startRuleStartState = _atn.ruleToStartState[startRuleIndex]; _rootContext = createInterpreterRuleContext(std::weak_ptr(), atn::ATNState::INVALID_STATE_NUMBER, startRuleIndex); @@ -158,7 +160,7 @@ Ref ParserInterpreter::parse(int startRuleIndex) { } } -void ParserInterpreter::enterRecursionRule(Ref const& localctx, int state, int ruleIndex, int precedence) { +void ParserInterpreter::enterRecursionRule(Ref const& localctx, size_t state, size_t ruleIndex, int precedence) { _parentContextStack.push({ _ctx, localctx->invokingState }); Parser::enterRecursionRule(localctx, state, ruleIndex, precedence); } @@ -178,11 +180,11 @@ Ref ParserInterpreter::getRootContext() { } atn::ATNState* ParserInterpreter::getATNState() { - return _atn.states[(size_t)getState()]; + return _atn.states[getState()]; } void ParserInterpreter::visitState(atn::ATNState *p) { - int predictedAlt = 1; + size_t predictedAlt = 1; if (is(p)) { predictedAlt = visitDecisionState(dynamic_cast(p)); } @@ -221,7 +223,7 @@ void ParserInterpreter::visitState(atn::ATNState *p) { case atn::Transition::RULE: { atn::RuleStartState *ruleStartState = (atn::RuleStartState*)(transition->target); - int ruleIndex = ruleStartState->ruleIndex; + size_t ruleIndex = ruleStartState->ruleIndex; Ref newctx = createInterpreterRuleContext(_ctx, p->stateNumber, ruleIndex); if (ruleStartState->isLeftRecursiveRule) { enterRecursionRule(newctx, ruleStartState->stateNumber, ruleIndex, ((atn::RuleTransition*)(transition))->precedence); @@ -262,8 +264,8 @@ void ParserInterpreter::visitState(atn::ATNState *p) { setState(transition->target->stateNumber); } -int ParserInterpreter::visitDecisionState(DecisionState *p) { - int predictedAlt = 1; +size_t ParserInterpreter::visitDecisionState(DecisionState *p) { + size_t predictedAlt = 1; if (p->getNumberOfTransitions() > 1) { getErrorHandler()->sync(this); int decision = p->decision; @@ -278,12 +280,12 @@ int ParserInterpreter::visitDecisionState(DecisionState *p) { } Ref ParserInterpreter::createInterpreterRuleContext(std::weak_ptr parent, - int invokingStateNumber, int ruleIndex) { + size_t invokingStateNumber, size_t ruleIndex) { return std::make_shared(parent, invokingStateNumber, ruleIndex); } void ParserInterpreter::visitRuleStopState(atn::ATNState *p) { - atn::RuleStartState *ruleStartState = _atn.ruleToStartState[(size_t)p->ruleIndex]; + atn::RuleStartState *ruleStartState = _atn.ruleToStartState[p->ruleIndex]; if (ruleStartState->isLeftRecursiveRule) { std::pair, int> parentContext = _parentContextStack.top(); _parentContextStack.pop(); @@ -294,7 +296,7 @@ void ParserInterpreter::visitRuleStopState(atn::ATNState *p) { exitRule(); } - atn::RuleTransition *ruleTransition = static_cast(_atn.states[(size_t)getState()]->transition(0)); + atn::RuleTransition *ruleTransition = static_cast(_atn.states[getState()]->transition(0)); setState(ruleTransition->followState->stateNumber); } @@ -307,7 +309,7 @@ void ParserInterpreter::recover(RecognitionException &e) { if (is(&e)) { InputMismatchException &ime = (InputMismatchException&)e; Token *tok = e.getOffendingToken(); - int expectedTokenType = ime.getExpectedTokens().getMinElement(); // get any element + size_t expectedTokenType = ime.getExpectedTokens().getMinElement(); // get any element _errorToken = getTokenFactory()->create({ tok->getTokenSource(), tok->getTokenSource()->getInputStream() }, expectedTokenType, tok->getText(), Token::DEFAULT_CHANNEL, -1, -1, // invalid start/stop tok->getLine(), tok->getCharPositionInLine()); diff --git a/runtime/Cpp/runtime/src/ParserInterpreter.h b/runtime/Cpp/runtime/src/ParserInterpreter.h index d92b0b3cd..f29ca8506 100755 --- a/runtime/Cpp/runtime/src/ParserInterpreter.h +++ b/runtime/Cpp/runtime/src/ParserInterpreter.h @@ -75,9 +75,9 @@ namespace antlr4 { virtual std::string getGrammarFileName() const override; /// Begin parsing at startRuleIndex - virtual Ref parse(int startRuleIndex); + virtual Ref parse(size_t startRuleIndex); - virtual void enterRecursionRule(Ref const& localctx, int state, int ruleIndex, int precedence) override; + virtual void enterRecursionRule(Ref const& localctx, size_t state, size_t ruleIndex, int precedence) override; /** Override this parser interpreters normal decision-making process @@ -181,13 +181,13 @@ namespace antlr4 { * a decision state (instance of DecisionState). It gives an opportunity * for subclasses to track interesting things. */ - int visitDecisionState(atn::DecisionState *p); + size_t visitDecisionState(atn::DecisionState *p); /** Provide simple "factory" for InterpreterRuleContext's. * @since 4.5.1 */ - Ref createInterpreterRuleContext(std::weak_ptr parent, int invokingStateNumber, - int ruleIndex); + Ref createInterpreterRuleContext(std::weak_ptr parent, + size_t invokingStateNumber, size_t ruleIndex); virtual void visitRuleStopState(atn::ATNState *p); diff --git a/runtime/Cpp/runtime/src/ParserRuleContext.cpp b/runtime/Cpp/runtime/src/ParserRuleContext.cpp index fda87b9b9..c32db5ea8 100755 --- a/runtime/Cpp/runtime/src/ParserRuleContext.cpp +++ b/runtime/Cpp/runtime/src/ParserRuleContext.cpp @@ -54,7 +54,7 @@ void ParserRuleContext::copyFrom(Ref const& ctx) { this->stop = ctx->stop; } -ParserRuleContext::ParserRuleContext(std::weak_ptr parent, int invokingStateNumber) +ParserRuleContext::ParserRuleContext(std::weak_ptr parent, size_t invokingStateNumber) : RuleContext(parent, invokingStateNumber) { } @@ -94,7 +94,7 @@ Ref ParserRuleContext::addErrorNode(Token *badToken) { return t; } -Ref ParserRuleContext::getToken(int ttype, std::size_t i) { +Ref ParserRuleContext::getToken(size_t ttype, size_t i) { if (i >= children.size()) { return nullptr; } @@ -115,7 +115,7 @@ Ref ParserRuleContext::getToken(int ttype, std::size_t i) { return nullptr; } -std::vector> ParserRuleContext::getTokens(int ttype) { +std::vector> ParserRuleContext::getTokens(size_t ttype) { std::vector> tokens; for (auto &o : children) { if (is(o)) { diff --git a/runtime/Cpp/runtime/src/ParserRuleContext.h b/runtime/Cpp/runtime/src/ParserRuleContext.h index 1382a9f0e..87325384f 100755 --- a/runtime/Cpp/runtime/src/ParserRuleContext.h +++ b/runtime/Cpp/runtime/src/ParserRuleContext.h @@ -99,7 +99,7 @@ namespace antlr4 { */ virtual void copyFrom(Ref const& ctx); - ParserRuleContext(std::weak_ptr parent, int invokingStateNumber); + ParserRuleContext(std::weak_ptr parent, size_t invokingStateNumber); // Double dispatch methods for listeners @@ -118,9 +118,9 @@ namespace antlr4 { virtual Ref addChild(Token *matchedToken); virtual Ref addErrorNode(Token *badToken); - virtual Ref getToken(int ttype, std::size_t i); + virtual Ref getToken(size_t ttype, std::size_t i); - virtual std::vector> getTokens(int ttype); + virtual std::vector> getTokens(size_t ttype); template Ref getRuleContext(size_t i) { diff --git a/runtime/Cpp/runtime/src/ProxyErrorListener.cpp b/runtime/Cpp/runtime/src/ProxyErrorListener.cpp index 3af950d9b..d5744d67c 100755 --- a/runtime/Cpp/runtime/src/ProxyErrorListener.cpp +++ b/runtime/Cpp/runtime/src/ProxyErrorListener.cpp @@ -50,7 +50,7 @@ void ProxyErrorListener::removeErrorListeners() { } void ProxyErrorListener::syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, - int charPositionInLine, const std::string &msg, std::exception_ptr e) { + size_t charPositionInLine, const std::string &msg, std::exception_ptr e) { for (auto listener : _delegates) { listener->syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e); @@ -72,7 +72,7 @@ void ProxyErrorListener::reportAttemptingFullContext(Parser *recognizer, const d } void ProxyErrorListener::reportContextSensitivity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, - int prediction, atn::ATNConfigSet *configs) { + size_t prediction, atn::ATNConfigSet *configs) { for (auto listener : _delegates) { listener->reportContextSensitivity(recognizer, dfa, startIndex, stopIndex, prediction, configs); } diff --git a/runtime/Cpp/runtime/src/ProxyErrorListener.h b/runtime/Cpp/runtime/src/ProxyErrorListener.h index d5bf60297..28ee630ba 100755 --- a/runtime/Cpp/runtime/src/ProxyErrorListener.h +++ b/runtime/Cpp/runtime/src/ProxyErrorListener.h @@ -48,7 +48,7 @@ namespace antlr4 { void removeErrorListener(ANTLRErrorListener *listener); void removeErrorListeners(); - void syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, int charPositionInLine, + void syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override; virtual void reportAmbiguity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, bool exact, @@ -58,7 +58,7 @@ namespace antlr4 { const antlrcpp::BitSet &conflictingAlts, atn::ATNConfigSet *configs) override; virtual void reportContextSensitivity(Parser *recognizer, const dfa::DFA &dfa, size_t startIndex, size_t stopIndex, - int prediction, atn::ATNConfigSet *configs) override; + size_t prediction, atn::ATNConfigSet *configs) override; }; } // namespace antlr4 diff --git a/runtime/Cpp/runtime/src/RecognitionException.cpp b/runtime/Cpp/runtime/src/RecognitionException.cpp index 43153089e..561065013 100755 --- a/runtime/Cpp/runtime/src/RecognitionException.cpp +++ b/runtime/Cpp/runtime/src/RecognitionException.cpp @@ -53,11 +53,11 @@ RecognitionException::RecognitionException(const std::string &message, IRecogniz } } -int RecognitionException::getOffendingState() const { +size_t RecognitionException::getOffendingState() const { return _offendingState; } -void RecognitionException::setOffendingState(int offendingState) { +void RecognitionException::setOffendingState(size_t offendingState) { _offendingState = offendingState; } diff --git a/runtime/Cpp/runtime/src/RecognitionException.h b/runtime/Cpp/runtime/src/RecognitionException.h index bcd818778..405142f71 100755 --- a/runtime/Cpp/runtime/src/RecognitionException.h +++ b/runtime/Cpp/runtime/src/RecognitionException.h @@ -52,7 +52,7 @@ namespace antlr4 { /// instance itself. Token *_offendingToken; - int _offendingState; + size_t _offendingState; public: RecognitionException(IRecognizer *recognizer, IntStream *input, Ref const& ctx, @@ -68,10 +68,10 @@ namespace antlr4 { /// edge we couldn't match. /// /// If the state number is not known, this method returns -1. - virtual int getOffendingState() const; + virtual size_t getOffendingState() const; protected: - void setOffendingState(int offendingState); + void setOffendingState(size_t offendingState); /// Gets the set of input symbols which could potentially follow the /// previously matched symbol at the time this exception was thrown. diff --git a/runtime/Cpp/runtime/src/Recognizer.cpp b/runtime/Cpp/runtime/src/Recognizer.cpp index 8f24f1051..8a50366cf 100755 --- a/runtime/Cpp/runtime/src/Recognizer.cpp +++ b/runtime/Cpp/runtime/src/Recognizer.cpp @@ -43,8 +43,9 @@ #include "Recognizer.h" using namespace antlr4; +using namespace antlr4::atn; -std::map> Recognizer::_tokenTypeMapCache; +std::map> Recognizer::_tokenTypeMapCache; std::map, std::map> Recognizer::_ruleIndexMapCache; Recognizer::Recognizer() { @@ -57,11 +58,11 @@ dfa::Vocabulary const& Recognizer::getVocabulary() const { return vocabulary; } -std::map Recognizer::getTokenTypeMap() { +std::map Recognizer::getTokenTypeMap() { const dfa::Vocabulary& vocabulary = getVocabulary(); std::lock_guard lck(mtx); - std::map result; + std::map result; auto iterator = _tokenTypeMapCache.find(&vocabulary); if (iterator != _tokenTypeMapCache.end()) { result = iterator->second; @@ -102,8 +103,8 @@ std::map Recognizer::getRuleIndexMap() { return result; } -ssize_t Recognizer::getTokenType(const std::string &tokenName) { - const std::map &map = getTokenTypeMap(); +size_t Recognizer::getTokenType(const std::string &tokenName) { + const std::map &map = getTokenTypeMap(); auto iterator = map.find(tokenName); if (iterator == map.end()) return Token::INVALID_TYPE; @@ -125,9 +126,9 @@ void Recognizer::setInterpreter(atn::ATNSimulator *interpreter) { std::string Recognizer::getErrorHeader(RecognitionException *e) { // We're having issues with cross header dependencies, these two classes will need to be // rewritten to remove that. - int line = e->getOffendingToken()->getLine(); - int charPositionInLine = e->getOffendingToken()->getCharPositionInLine(); - return std::string("line ") + std::to_string(line) + std::string(":") + std::to_string(charPositionInLine); + size_t line = e->getOffendingToken()->getLine(); + size_t charPositionInLine = e->getOffendingToken()->getCharPositionInLine(); + return std::string("line ") + std::to_string(line) + ":" + std::to_string(charPositionInLine); } @@ -167,7 +168,7 @@ ProxyErrorListener& Recognizer::getErrorListenerDispatch() { return _proxListener; } -bool Recognizer::sempred(Ref const& /*localctx*/, int /*ruleIndex*/, int /*actionIndex*/) { +bool Recognizer::sempred(Ref const& /*localctx*/, size_t /*ruleIndex*/, size_t /*actionIndex*/) { return true; } @@ -175,20 +176,19 @@ bool Recognizer::precpred(Ref const& /*localctx*/, int /*precedence return true; } -void Recognizer::action(Ref const& /*localctx*/, int /*ruleIndex*/, int /*actionIndex*/) { +void Recognizer::action(Ref const& /*localctx*/, size_t /*ruleIndex*/, size_t /*actionIndex*/) { } -int Recognizer::getState() { +size_t Recognizer::getState() const { return _stateNumber; } -void Recognizer::setState(int atnState) { +void Recognizer::setState(size_t atnState) { _stateNumber = atnState; - // if ( traceATNStates ) _ctx.trace(atnState); } void Recognizer::InitializeInstanceFields() { - _stateNumber = -1; + _stateNumber = ATNState::INVALID_STATE_NUMBER; _interpreter = nullptr; } diff --git a/runtime/Cpp/runtime/src/Recognizer.h b/runtime/Cpp/runtime/src/Recognizer.h index a3a75030e..44e0d1d36 100755 --- a/runtime/Cpp/runtime/src/Recognizer.h +++ b/runtime/Cpp/runtime/src/Recognizer.h @@ -38,7 +38,7 @@ namespace antlr4 { class ANTLR4CPP_PUBLIC Recognizer : public IRecognizer { public: - static const ssize_t EOF = -1; + static const size_t EOF = (size_t)-1; Recognizer(); virtual ~Recognizer() {}; @@ -65,7 +65,7 @@ namespace antlr4 { ///

/// Used for XPath and tree pattern compilation. /// - virtual std::map getTokenTypeMap(); + virtual std::map getTokenTypeMap(); ///

/// Get a map from rule names to rule indexes. @@ -74,7 +74,7 @@ namespace antlr4 { /// virtual std::map getRuleIndexMap(); - virtual ssize_t getTokenType(const std::string &tokenName); + virtual size_t getTokenType(const std::string &tokenName); /// /// If this recognizer was generated, it will have a serialized ATN @@ -144,13 +144,13 @@ namespace antlr4 { // subclass needs to override these if there are sempreds or actions // that the ATN interp needs to execute - virtual bool sempred(Ref const& localctx, int ruleIndex, int actionIndex); + virtual bool sempred(Ref const& localctx, size_t ruleIndex, size_t actionIndex); virtual bool precpred(Ref const& localctx, int precedence); - virtual void action(Ref const& localctx, int ruleIndex, int actionIndex); + virtual void action(Ref const& localctx, size_t ruleIndex, size_t actionIndex); - int getState(); + virtual size_t getState() const override; /// /// Indicate that the recognizer has changed internal state that is @@ -160,7 +160,7 @@ namespace antlr4 { /// invoking rules. Combine this and we have complete ATN /// configuration information. /// - void setState(int atnState); + void setState(size_t atnState); virtual IntStream* getInputStream() = 0; @@ -175,7 +175,7 @@ namespace antlr4 { atn::ATNSimulator *_interpreter; // Set and deleted in descendants (or the profiler). private: - static std::map> _tokenTypeMapCache; + static std::map> _tokenTypeMapCache; static std::map, std::map> _ruleIndexMapCache; ProxyErrorListener _proxListener; // Manages a collection of listeners. @@ -183,7 +183,7 @@ namespace antlr4 { // Mutex to manage synchronized access for multithreading. std::recursive_mutex mtx; - int _stateNumber; + size_t _stateNumber; void InitializeInstanceFields(); diff --git a/runtime/Cpp/runtime/src/RuleContext.cpp b/runtime/Cpp/runtime/src/RuleContext.cpp index 7c6ccf15d..1295fcc42 100755 --- a/runtime/Cpp/runtime/src/RuleContext.cpp +++ b/runtime/Cpp/runtime/src/RuleContext.cpp @@ -33,17 +33,19 @@ #include "misc/Interval.h" #include "Parser.h" #include "atn/ATN.h" +#include "atn/ATNState.h" #include "tree/ParseTreeVisitor.h" #include "RuleContext.h" using namespace antlr4; +using namespace antlr4::atn; RuleContext::RuleContext() { InitializeInstanceFields(); } -RuleContext::RuleContext(std::weak_ptr parent, int invokingState) { +RuleContext::RuleContext(std::weak_ptr parent, size_t invokingState) { InitializeInstanceFields(); this->parent = parent; this->invokingState = invokingState; @@ -62,7 +64,7 @@ int RuleContext::depth() { } bool RuleContext::isEmpty() { - return invokingState == -1; + return invokingState == ATNState::INVALID_STATE_NUMBER; } misc::Interval RuleContext::getSourceInterval() { @@ -91,8 +93,8 @@ std::string RuleContext::getText() { return ss.str(); } -ssize_t RuleContext::getRuleIndex() const { - return -1; +size_t RuleContext::getRuleIndex() const { + return INVALID_INDEX; } int RuleContext::getAltNumber() const { @@ -135,9 +137,9 @@ std::string RuleContext::toString(const std::vector &ruleNames, Ref ss << currentParent->invokingState; } } else { - ssize_t ruleIndex = currentParent->getRuleIndex(); + size_t ruleIndex = currentParent->getRuleIndex(); - std::string ruleName = (ruleIndex >= 0 && ruleIndex < (ssize_t)ruleNames.size()) ? ruleNames[(size_t)ruleIndex] : std::to_string(ruleIndex); + std::string ruleName = (ruleIndex < ruleNames.size()) ? ruleNames[ruleIndex] : std::to_string(ruleIndex); ss << ruleName; } @@ -169,5 +171,5 @@ std::string RuleContext::toString(Recognizer *recog, Ref const& sto } void RuleContext::InitializeInstanceFields() { - invokingState = -1; + invokingState = INVALID_INDEX; } diff --git a/runtime/Cpp/runtime/src/RuleContext.h b/runtime/Cpp/runtime/src/RuleContext.h index 47a4f5a0a..1814afb00 100755 --- a/runtime/Cpp/runtime/src/RuleContext.h +++ b/runtime/Cpp/runtime/src/RuleContext.h @@ -90,11 +90,11 @@ namespace antlr4 { /// What state invoked the rule associated with this context? /// The "return address" is the followState of invokingState /// If parent is null, this should be -1 and this context object represents the start rule. - int invokingState; + size_t invokingState; RuleContext(); - RuleContext(std::weak_ptr parent, int invokingState); + RuleContext(std::weak_ptr parent, size_t invokingState); virtual int depth(); @@ -108,7 +108,7 @@ namespace antlr4 { virtual Ref getRuleContext() override; virtual std::string getText() override; - virtual ssize_t getRuleIndex() const; + virtual size_t getRuleIndex() const; /** For rule associated with this parse tree internal node, return * the outer alternative number used to match the input. Default diff --git a/runtime/Cpp/runtime/src/Token.h b/runtime/Cpp/runtime/src/Token.h index 3fd91e24d..aac595382 100755 --- a/runtime/Cpp/runtime/src/Token.h +++ b/runtime/Cpp/runtime/src/Token.h @@ -44,9 +44,9 @@ namespace antlr4 { /// During lookahead operations, this "token" signifies we hit rule end ATN state /// and did not follow it despite needing to. - static const ssize_t EPSILON = -2; + static const size_t EPSILON = (size_t)-2; static const size_t MIN_USER_TOKEN_TYPE = 1; - static const ssize_t EOF = IntStream::EOF; + static const size_t EOF = IntStream::EOF; virtual ~Token() {}; @@ -72,64 +72,44 @@ namespace antlr4 { */ static const size_t MIN_USER_CHANNEL_VALUE = 2; - /// /// Get the text of the token. - /// virtual std::string getText() const = 0; - /// - /// Get the token type of the token - virtual int getType() const = 0; + /// Get the token type of the token + virtual size_t getType() const = 0; - /// - /// The line number on which the 1st character of this token was matched, - /// line=1..n - /// - virtual int getLine() const = 0; + /// The line number on which the 1st character of this token was matched, line=1..n + virtual size_t getLine() const = 0; - /// /// The index of the first character of this token relative to the - /// beginning of the line at which it occurs, 0..n-1 - /// - virtual int getCharPositionInLine() const = 0; + /// beginning of the line at which it occurs, 0..n-1 + virtual size_t getCharPositionInLine() const = 0; - /// /// Return the channel this token. Each token can arrive at the parser - /// on a different channel, but the parser only "tunes" to a single channel. - /// The parser ignores everything not on DEFAULT_CHANNEL. - /// + /// on a different channel, but the parser only "tunes" to a single channel. + /// The parser ignores everything not on DEFAULT_CHANNEL. virtual size_t getChannel() const = 0; - /// /// An index from 0..n-1 of the token object in the input stream. - /// This must be valid in order to print token streams and - /// use TokenRewriteStream. + /// This must be valid in order to print token streams and + /// use TokenRewriteStream. /// - /// Return -1 to indicate that this token was conjured up since - /// it doesn't have a valid index. - /// - virtual int getTokenIndex() const = 0; + /// Return INVALID_INDEX to indicate that this token was conjured up since + /// it doesn't have a valid index. + virtual size_t getTokenIndex() const = 0; - /// /// The starting character index of the token - /// This method is optional; return -1 if not implemented. - /// - virtual int getStartIndex() const = 0; + /// This method is optional; return INVALID_INDEX if not implemented. + virtual size_t getStartIndex() const = 0; - /// /// The last character index of the token. - /// This method is optional; return -1 if not implemented. - /// - virtual int getStopIndex() const = 0; + /// This method is optional; return INVALID_INDEX if not implemented. + virtual size_t getStopIndex() const = 0; - /// /// Gets the which created this token. - /// virtual TokenSource *getTokenSource() const = 0; - /// /// Gets the from which this token was derived. - /// virtual CharStream *getInputStream() const = 0; virtual std::string toString() const = 0; diff --git a/runtime/Cpp/runtime/src/TokenFactory.h b/runtime/Cpp/runtime/src/TokenFactory.h index fd6740ee9..4c0e4cce9 100755 --- a/runtime/Cpp/runtime/src/TokenFactory.h +++ b/runtime/Cpp/runtime/src/TokenFactory.h @@ -42,13 +42,13 @@ namespace antlr4 { virtual ~TokenFactory() {}; /// This is the method used to create tokens in the lexer and in the - /// error handling strategy. If text!=null, than the start and stop positions - /// are wiped to -1 in the text override is set in the CommonToken. - virtual std::unique_ptr create(std::pair source, int type, const std::string &text, - int channel, int start, int stop, int line, int charPositionInLine) = 0; + /// error handling strategy. If text!=null, than the start and stop positions + /// are wiped to -1 in the text override is set in the CommonToken. + virtual std::unique_ptr create(std::pair source, size_t type, const std::string &text, + size_t channel, size_t start, size_t stop, size_t line, size_t charPositionInLine) = 0; /// Generically useful - virtual std::unique_ptr create(int type, const std::string &text) = 0; + virtual std::unique_ptr create(size_t type, const std::string &text) = 0; }; } // namespace antlr4 diff --git a/runtime/Cpp/runtime/src/TokenSource.h b/runtime/Cpp/runtime/src/TokenSource.h index 853cc56c0..8b3b8c607 100755 --- a/runtime/Cpp/runtime/src/TokenSource.h +++ b/runtime/Cpp/runtime/src/TokenSource.h @@ -73,8 +73,8 @@ namespace antlr4 { /// stream. The first character on a line has position 0. /// /// The line number for the current position in the input stream, or - /// -1 if the current token source does not track character positions. - virtual int getCharPositionInLine() = 0; + /// (sze_t)-1 if the current token source does not track character positions. + virtual size_t getCharPositionInLine() = 0; /// /// Get the from which this token source is currently diff --git a/runtime/Cpp/runtime/src/TokenStreamRewriter.cpp b/runtime/Cpp/runtime/src/TokenStreamRewriter.cpp index d58fbd203..34cb1d209 100755 --- a/runtime/Cpp/runtime/src/TokenStreamRewriter.cpp +++ b/runtime/Cpp/runtime/src/TokenStreamRewriter.cpp @@ -42,13 +42,15 @@ using namespace antlrcpp; using antlr4::misc::Interval; -TokenStreamRewriter::RewriteOperation::RewriteOperation(TokenStreamRewriter *outerInstance, size_t index) : outerInstance(outerInstance) { +TokenStreamRewriter::RewriteOperation::RewriteOperation(TokenStreamRewriter *outerInstance, size_t index) + : outerInstance(outerInstance) { InitializeInstanceFields(); this->index = index; } -TokenStreamRewriter::RewriteOperation::RewriteOperation(TokenStreamRewriter *outerInstance, size_t index, const std::string& text) : outerInstance(outerInstance) { +TokenStreamRewriter::RewriteOperation::RewriteOperation(TokenStreamRewriter *outerInstance, size_t index, + const std::string& text) : outerInstance(outerInstance) { InitializeInstanceFields(); this->index = index; @@ -125,11 +127,11 @@ TokenStream *TokenStreamRewriter::getTokenStream() { return tokens; } -void TokenStreamRewriter::rollback(int instructionIndex) { +void TokenStreamRewriter::rollback(size_t instructionIndex) { rollback(DEFAULT_PROGRAM_NAME, instructionIndex); } -void TokenStreamRewriter::rollback(const std::string &programName, int instructionIndex) { +void TokenStreamRewriter::rollback(const std::string &programName, size_t instructionIndex) { std::vector is = _programs[programName]; if (is.size() > 0) { _programs.insert({ programName, std::vector(is.begin() + MIN_TOKEN_INDEX, is.begin() + instructionIndex) }); @@ -153,7 +155,7 @@ void TokenStreamRewriter::insertAfter(size_t index, const std::string& text) { } void TokenStreamRewriter::insertAfter(const std::string &programName, Token *t, const std::string& text) { - insertAfter(programName, (size_t)t->getTokenIndex(), text); + insertAfter(programName, t->getTokenIndex(), text); } void TokenStreamRewriter::insertAfter(const std::string &programName, size_t index, const std::string& text) { @@ -170,13 +172,13 @@ void TokenStreamRewriter::insertBefore(size_t index, const std::string& text) { } void TokenStreamRewriter::insertBefore(const std::string &programName, Token *t, const std::string& text) { - insertBefore(programName, (size_t)t->getTokenIndex(), text); + insertBefore(programName, t->getTokenIndex(), text); } void TokenStreamRewriter::insertBefore(const std::string &programName, size_t index, const std::string& text) { RewriteOperation *op = new InsertBeforeOp(this, index, text); /* mem-check: deleted in d-tor */ std::vector &rewrites = getProgram(programName); - op->instructionIndex = (int)rewrites.size(); + op->instructionIndex = rewrites.size(); rewrites.push_back(op); } @@ -203,12 +205,12 @@ void TokenStreamRewriter::replace(const std::string &programName, size_t from, s } RewriteOperation *op = new ReplaceOp(this, from, to, text); /* mem-check: deleted in d-tor */ std::vector &rewrites = getProgram(programName); - op->instructionIndex = (int)rewrites.size(); + op->instructionIndex = rewrites.size(); rewrites.push_back(op); } void TokenStreamRewriter::replace(const std::string &programName, Token *from, Token *to, const std::string& text) { - replace(programName, (size_t)from->getTokenIndex(), (size_t)to->getTokenIndex(), text); + replace(programName, from->getTokenIndex(), to->getTokenIndex(), text); } void TokenStreamRewriter::Delete(size_t index) { @@ -235,18 +237,18 @@ void TokenStreamRewriter::Delete(const std::string &programName, Token *from, To replace(programName, from, to, nullptr); } -int TokenStreamRewriter::getLastRewriteTokenIndex() { +size_t TokenStreamRewriter::getLastRewriteTokenIndex() { return getLastRewriteTokenIndex(DEFAULT_PROGRAM_NAME); } -int TokenStreamRewriter::getLastRewriteTokenIndex(const std::string &programName) { +size_t TokenStreamRewriter::getLastRewriteTokenIndex(const std::string &programName) { if (_lastRewriteTokenIndexes.find(programName) == _lastRewriteTokenIndexes.end()) { - return -1; + return INVALID_INDEX; } return _lastRewriteTokenIndexes[programName]; } -void TokenStreamRewriter::setLastRewriteTokenIndex(const std::string &programName, int i) { +void TokenStreamRewriter::setLastRewriteTokenIndex(const std::string &programName, size_t i) { _lastRewriteTokenIndexes.insert({ programName, i }); } @@ -265,11 +267,11 @@ std::vector TokenStreamRewriter::initial } std::string TokenStreamRewriter::getText() { - return getText(DEFAULT_PROGRAM_NAME, Interval(0, (int)tokens->size() - 1)); + return getText(DEFAULT_PROGRAM_NAME, Interval(0, tokens->size() - 1)); } std::string TokenStreamRewriter::getText(std::string programName) { - return getText(programName, Interval(0, (int)tokens->size() - 1)); + return getText(programName, Interval(0, tokens->size() - 1)); } std::string TokenStreamRewriter::getText(const Interval &interval) { @@ -278,14 +280,14 @@ std::string TokenStreamRewriter::getText(const Interval &interval) { std::string TokenStreamRewriter::getText(const std::string &programName, const Interval &interval) { std::vector rewrites = _programs[programName]; - int start = interval.a; - int stop = interval.b; + size_t start = interval.a; + size_t stop = interval.b; // ensure start/end are in range - if (stop > (int)tokens->size() - 1) { - stop = (int)tokens->size() - 1; + if (stop > tokens->size() - 1) { + stop = tokens->size() - 1; } - if (start < 0) { + if (start == INVALID_INDEX) { start = 0; } @@ -298,8 +300,8 @@ std::string TokenStreamRewriter::getText(const std::string &programName, const I std::unordered_map indexToOp = reduceToSingleOperationPerIndex(rewrites); // Walk buffer, executing instructions and emitting tokens - size_t i = (size_t)start; - while (i <= (size_t)stop && i < tokens->size()) { + size_t i = start; + while (i <= stop && i < tokens->size()) { RewriteOperation *op = indexToOp[i]; indexToOp.erase(i); // remove so any left have index size-1 Token *t = tokens->get(i); @@ -318,7 +320,7 @@ std::string TokenStreamRewriter::getText(const std::string &programName, const I // include stuff after end if it's last index in buffer // So, if they did an insertAfter(lastValidIndex, "foo"), include // foo if end==lastValidIndex. - if (stop == (int)tokens->size() - 1) { + if (stop == tokens->size() - 1) { // Scan any remaining operations after last token // should be included (they will be inserts). for (auto op : indexToOp) { @@ -405,7 +407,7 @@ std::unordered_map TokenStreamRe // whole token buffer so no lazy eval issue with any templates iop->text = catOpText(&iop->text, &prevIop->text); // delete redundant prior insert - rewrites[(size_t)prevIop->instructionIndex] = nullptr; + rewrites[prevIop->instructionIndex] = nullptr; } } // look for replaces where iop.index is in range; error diff --git a/runtime/Cpp/runtime/src/TokenStreamRewriter.h b/runtime/Cpp/runtime/src/TokenStreamRewriter.h index 2799b0fe6..5358113bd 100755 --- a/runtime/Cpp/runtime/src/TokenStreamRewriter.h +++ b/runtime/Cpp/runtime/src/TokenStreamRewriter.h @@ -112,22 +112,22 @@ namespace antlr4 { class ANTLR4CPP_PUBLIC TokenStreamRewriter { public: static const std::string DEFAULT_PROGRAM_NAME; - static const int PROGRAM_INIT_SIZE = 100; - static const int MIN_TOKEN_INDEX = 0; + static const size_t PROGRAM_INIT_SIZE = 100; + static const size_t MIN_TOKEN_INDEX = 0; TokenStreamRewriter(TokenStream *tokens); virtual ~TokenStreamRewriter(); TokenStream *getTokenStream(); - virtual void rollback(int instructionIndex); + virtual void rollback(size_t instructionIndex); /// /// Rollback the instruction stream for a program so that /// the indicated instruction (via instructionIndex) is no /// longer in the stream. UNTESTED! /// - virtual void rollback(const std::string &programName, int instructionIndex); + virtual void rollback(const std::string &programName, size_t instructionIndex); virtual void deleteProgram(); @@ -158,7 +158,7 @@ namespace antlr4 { virtual void Delete(const std::string &programName, size_t from, size_t to); virtual void Delete(const std::string &programName, Token *from, Token *to); - virtual int getLastRewriteTokenIndex(); + virtual size_t getLastRewriteTokenIndex(); /// Return the text from the original tokens altered per the /// instructions given to this rewriter. @@ -189,22 +189,20 @@ namespace antlr4 { TokenStreamRewriter *const outerInstance; public: - /// - /// What index into rewrites List are we? + /// What index into rewrites List are we? virtual ~RewriteOperation() {}; - /// - /// Token buffer index. + + /// Token buffer index. size_t index; std::string text; RewriteOperation(TokenStreamRewriter *outerInstance, size_t index); - RewriteOperation(TokenStreamRewriter *outerInstance, size_t index, const std::string& text); - /// + /// Execute the rewrite operation by possibly adding to the buffer. /// Return the index of the next token to operate on. - /// - int instructionIndex; + + size_t instructionIndex; virtual size_t execute(std::string *buf); @@ -249,9 +247,9 @@ namespace antlr4 { /// /// Map String (program name) -> Integer index - std::map _lastRewriteTokenIndexes; - virtual int getLastRewriteTokenIndex(const std::string &programName); - virtual void setLastRewriteTokenIndex(const std::string &programName, int i); + std::map _lastRewriteTokenIndexes; + virtual size_t getLastRewriteTokenIndex(const std::string &programName); + virtual void setLastRewriteTokenIndex(const std::string &programName, size_t i); virtual std::vector& getProgram(const std::string &name); /// diff --git a/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp b/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp index a4ab2055d..052903d83 100755 --- a/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp +++ b/runtime/Cpp/runtime/src/UnbufferedCharStream.cpp @@ -37,6 +37,7 @@ using namespace antlrcpp; using namespace antlr4; +using namespace antlr4::misc; UnbufferedCharStream::UnbufferedCharStream(std::wistream &input) : _input(input) { InitializeInstanceFields(); @@ -108,7 +109,7 @@ void UnbufferedCharStream::add(char32_t c) { _data += c; } -ssize_t UnbufferedCharStream::LA(ssize_t i) { +size_t UnbufferedCharStream::LA(ssize_t i) { if (i == -1) { // special case return _lastChar; } @@ -122,11 +123,7 @@ ssize_t UnbufferedCharStream::LA(ssize_t i) { return EOF; } - ssize_t c = _data[(size_t)index]; - if (c == EOF) { - return EOF; - } - return c; + return _data[(size_t)index]; } ssize_t UnbufferedCharStream::mark() { @@ -199,24 +196,24 @@ std::string UnbufferedCharStream::getSourceName() const { } std::string UnbufferedCharStream::getText(const misc::Interval &interval) { - if (interval.a < 0 || interval.b < interval.a - 1) { + if (interval.a < 0 || interval.b >= interval.a - 1) { throw IllegalArgumentException("invalid interval"); } size_t bufferStartIndex = getBufferStartIndex(); if (!_data.empty() && _data.back() == 0xFFFF) { - if ((size_t)(interval.a + interval.length()) > bufferStartIndex + _data.size()) { + if (interval.a + interval.length() > bufferStartIndex + _data.size()) { throw IllegalArgumentException("the interval extends past the end of the stream"); } } - if ((size_t)interval.a < bufferStartIndex || (size_t)interval.b >= bufferStartIndex + _data.size()) { + if (interval.a < (ssize_t)bufferStartIndex || interval.b >= ssize_t(bufferStartIndex + _data.size())) { throw UnsupportedOperationException("interval " + interval.toString() + " outside buffer: " + std::to_string(bufferStartIndex) + ".." + std::to_string(bufferStartIndex + _data.size() - 1)); } // convert from absolute to local index - size_t i = (size_t)interval.a - bufferStartIndex; - return utfConverter.to_bytes(_data.substr(i, (size_t)interval.length())); + size_t i = interval.a - bufferStartIndex; + return utfConverter.to_bytes(_data.substr(i, interval.length())); } size_t UnbufferedCharStream::getBufferStartIndex() const { diff --git a/runtime/Cpp/runtime/src/UnbufferedCharStream.h b/runtime/Cpp/runtime/src/UnbufferedCharStream.h index 2e8333672..f65e5db13 100755 --- a/runtime/Cpp/runtime/src/UnbufferedCharStream.h +++ b/runtime/Cpp/runtime/src/UnbufferedCharStream.h @@ -47,7 +47,7 @@ namespace antlr4 { UnbufferedCharStream(std::wistream &input); virtual void consume() override; - virtual ssize_t LA(ssize_t i) override; + virtual size_t LA(ssize_t i) override; /// /// Return a marker that we can release later. diff --git a/runtime/Cpp/runtime/src/UnbufferedTokenStream.cpp b/runtime/Cpp/runtime/src/UnbufferedTokenStream.cpp index 1f6e0df71..68af4f8d6 100755 --- a/runtime/Cpp/runtime/src/UnbufferedTokenStream.cpp +++ b/runtime/Cpp/runtime/src/UnbufferedTokenStream.cpp @@ -85,7 +85,7 @@ Token* UnbufferedTokenStream::LT(ssize_t i) return _tokens[(size_t)index].get(); } -ssize_t UnbufferedTokenStream::LA(ssize_t i) +size_t UnbufferedTokenStream::LA(ssize_t i) { return LT(i)->getType(); } @@ -262,8 +262,8 @@ std::string UnbufferedTokenStream::getText(const misc::Interval &interval) size_t bufferStartIndex = getBufferStartIndex(); size_t bufferStopIndex = bufferStartIndex + _tokens.size() - 1; - size_t start = (size_t)interval.a; - size_t stop = (size_t)interval.b; + size_t start = interval.a; + size_t stop = interval.b; if (start < bufferStartIndex || stop > bufferStopIndex) { throw UnsupportedOperationException(std::string("interval ") + interval.toString() + " not in token buffer window: " + std::to_string(bufferStartIndex) + ".." + std::to_string(bufferStopIndex)); diff --git a/runtime/Cpp/runtime/src/UnbufferedTokenStream.h b/runtime/Cpp/runtime/src/UnbufferedTokenStream.h index 7ab64876d..a7753876f 100755 --- a/runtime/Cpp/runtime/src/UnbufferedTokenStream.h +++ b/runtime/Cpp/runtime/src/UnbufferedTokenStream.h @@ -46,7 +46,7 @@ namespace antlr4 { virtual Token* get(size_t i) const override; virtual Token* LT(ssize_t i) override; - virtual ssize_t LA(ssize_t i) override; + virtual size_t LA(ssize_t i) override; virtual TokenSource* getTokenSource() const override; diff --git a/runtime/Cpp/runtime/src/Vocabulary.cpp b/runtime/Cpp/runtime/src/Vocabulary.cpp index b05f70588..2b32bf92b 100755 --- a/runtime/Cpp/runtime/src/Vocabulary.cpp +++ b/runtime/Cpp/runtime/src/Vocabulary.cpp @@ -35,8 +35,7 @@ using namespace antlr4::dfa; -const std::vector Vocabulary::EMPTY_NAMES; -const Vocabulary Vocabulary::EMPTY_VOCABULARY = Vocabulary(EMPTY_NAMES, EMPTY_NAMES, EMPTY_NAMES); +const Vocabulary Vocabulary::EMPTY_VOCABULARY; Vocabulary::Vocabulary(const std::vector &literalNames, const std::vector &symbolicNames) : Vocabulary(literalNames, symbolicNames, {}) { @@ -44,10 +43,8 @@ Vocabulary::Vocabulary(const std::vector &literalNames, const std:: Vocabulary::Vocabulary(const std::vector &literalNames, const std::vector &symbolicNames, const std::vector &displayNames) - : _literalNames(!literalNames.empty() ? literalNames : EMPTY_NAMES), - _symbolicNames(!symbolicNames.empty() ? symbolicNames : EMPTY_NAMES), - _displayNames(!displayNames.empty() ? displayNames : EMPTY_NAMES), - _maxTokenType(std::max((int)_displayNames.size(), std::max((int)_literalNames.size(), (int)_symbolicNames.size())) - 1) { + : _literalNames(literalNames), _symbolicNames(symbolicNames), _displayNames(displayNames), + _maxTokenType(std::max(_displayNames.size(), std::max(_literalNames.size(), _symbolicNames.size())) - 1) { // See note here on -1 part: https://github.com/antlr/antlr4/pull/1146 } @@ -84,32 +81,32 @@ Vocabulary Vocabulary::fromTokenNames(const std::vector &tokenNames return Vocabulary(literalNames, symbolicNames, tokenNames); } -int Vocabulary::getMaxTokenType() const { +size_t Vocabulary::getMaxTokenType() const { return _maxTokenType; } -std::string Vocabulary::getLiteralName(ssize_t tokenType) const { - if (tokenType >= 0 && tokenType < (int)_literalNames.size()) { +std::string Vocabulary::getLiteralName(size_t tokenType) const { + if (tokenType < _literalNames.size()) { return _literalNames[tokenType]; } return ""; } -std::string Vocabulary::getSymbolicName(ssize_t tokenType) const { - if (tokenType >= 0 && tokenType < (int)_symbolicNames.size()) { - return _symbolicNames[tokenType]; - } - +std::string Vocabulary::getSymbolicName(size_t tokenType) const { if (tokenType == Token::EOF) { return "EOF"; } + if (tokenType < _symbolicNames.size()) { + return _symbolicNames[tokenType]; + } + return ""; } -std::string Vocabulary::getDisplayName(ssize_t tokenType) const { - if (tokenType >= 0 && tokenType < (int)_displayNames.size()) { +std::string Vocabulary::getDisplayName(size_t tokenType) const { + if (tokenType < _displayNames.size()) { std::string displayName = _displayNames[tokenType]; if (!displayName.empty()) { return displayName; diff --git a/runtime/Cpp/runtime/src/Vocabulary.h b/runtime/Cpp/runtime/src/Vocabulary.h index 2903f08af..830e782f3 100755 --- a/runtime/Cpp/runtime/src/Vocabulary.h +++ b/runtime/Cpp/runtime/src/Vocabulary.h @@ -50,6 +50,8 @@ namespace dfa { /// except . static const Vocabulary EMPTY_VOCABULARY; + Vocabulary() {}; + /// /// Constructs a new instance of from the specified /// literal and symbolic token names. @@ -101,7 +103,7 @@ namespace dfa { /// Returns the highest token type value. It can be used to iterate from /// zero to that number, inclusively, thus querying all stored entries. /// the highest token type value - virtual int getMaxTokenType() const; + virtual size_t getMaxTokenType() const; /// /// Gets the string literal associated with a token type. The string returned @@ -138,7 +140,7 @@ namespace dfa { /// /// The string literal associated with the specified token type, or /// {@code null} if no string literal is associated with the type. - virtual std::string getLiteralName(ssize_t tokenType) const; + virtual std::string getLiteralName(size_t tokenType) const; /// /// Gets the symbolic name associated with a token type. The string returned @@ -182,7 +184,7 @@ namespace dfa { /// /// The symbolic name associated with the specified token type, or /// {@code null} if no symbolic name is associated with the type. - virtual std::string getSymbolicName(ssize_t tokenType) const; + virtual std::string getSymbolicName(size_t tokenType) const; /// /// Gets the display name of a token type. @@ -203,15 +205,13 @@ namespace dfa { /// /// The display name of the token type, for use in error reporting or /// other user-visible messages which reference specific token types. - virtual std::string getDisplayName(ssize_t tokenType) const; + virtual std::string getDisplayName(size_t tokenType) const; private: - static std::vector const EMPTY_NAMES; - std::vector const _literalNames; std::vector const _symbolicNames; std::vector const _displayNames; - const int _maxTokenType; + const size_t _maxTokenType = 0; }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/WritableToken.h b/runtime/Cpp/runtime/src/WritableToken.h index 3b74c4b7b..f17200c79 100755 --- a/runtime/Cpp/runtime/src/WritableToken.h +++ b/runtime/Cpp/runtime/src/WritableToken.h @@ -38,11 +38,11 @@ namespace antlr4 { class ANTLR4CPP_PUBLIC WritableToken : public Token { public: virtual void setText(const std::string &text) = 0; - virtual void setType(int ttype) = 0; - virtual void setLine(int line) = 0; - virtual void setCharPositionInLine(int pos) = 0; - virtual void setChannel(int channel) = 0; - virtual void setTokenIndex(int index) = 0; + virtual void setType(size_t ttype) = 0; + virtual void setLine(size_t line) = 0; + virtual void setCharPositionInLine(size_t pos) = 0; + virtual void setChannel(size_t channel) = 0; + virtual void setTokenIndex(size_t index) = 0; }; } // namespace antlr4 diff --git a/runtime/Cpp/runtime/src/antlr4-common.h b/runtime/Cpp/runtime/src/antlr4-common.h index 9d65885e1..1189359b4 100644 --- a/runtime/Cpp/runtime/src/antlr4-common.h +++ b/runtime/Cpp/runtime/src/antlr4-common.h @@ -143,4 +143,5 @@ #undef EOF #endif +#define INVALID_INDEX (size_t)-1 template using Ref = std::shared_ptr; diff --git a/runtime/Cpp/runtime/src/atn/ATN.cpp b/runtime/Cpp/runtime/src/atn/ATN.cpp index 7242c618c..89dcfeb23 100755 --- a/runtime/Cpp/runtime/src/atn/ATN.cpp +++ b/runtime/Cpp/runtime/src/atn/ATN.cpp @@ -129,8 +129,8 @@ void ATN::addState(ATNState *state) { } void ATN::removeState(ATNState *state) { - delete states.at((size_t)state->stateNumber);// just free mem, don't shift states in list - states.at((size_t)state->stateNumber) = nullptr; + delete states.at(state->stateNumber);// just free mem, don't shift states in list + states.at(state->stateNumber) = nullptr; } int ATN::defineDecisionState(DecisionState *s) { @@ -139,24 +139,24 @@ int ATN::defineDecisionState(DecisionState *s) { return s->decision; } -DecisionState *ATN::getDecisionState(int decision) const { +DecisionState *ATN::getDecisionState(size_t decision) const { if (!decisionToState.empty()) { - return decisionToState[(size_t)decision]; + return decisionToState[decision]; } return nullptr; } -int ATN::getNumberOfDecisions() const { - return (int)decisionToState.size(); +size_t ATN::getNumberOfDecisions() const { + return decisionToState.size(); } -misc::IntervalSet ATN::getExpectedTokens(int stateNumber, RuleContext *context) const { - if (stateNumber < 0 || stateNumber >= (int)states.size()) { +misc::IntervalSet ATN::getExpectedTokens(size_t stateNumber, RuleContext *context) const { + if (stateNumber == ATNState::INVALID_STATE_NUMBER || stateNumber >= states.size()) { throw IllegalArgumentException("Invalid state number."); } RuleContext *ctx = context; - ATNState *s = states.at((size_t)stateNumber); + ATNState *s = states.at(stateNumber); misc::IntervalSet following = nextTokens(s); if (!following.contains(Token::EPSILON)) { return following; @@ -165,8 +165,8 @@ misc::IntervalSet ATN::getExpectedTokens(int stateNumber, RuleContext *context) misc::IntervalSet expected; expected.addAll(following); expected.remove(Token::EPSILON); - while (ctx && ctx->invokingState >= 0 && following.contains(Token::EPSILON)) { - ATNState *invokingState = states.at((size_t)ctx->invokingState); + while (ctx && ctx->invokingState != ATNState::INVALID_STATE_NUMBER && following.contains(Token::EPSILON)) { + ATNState *invokingState = states.at(ctx->invokingState); RuleTransition *rt = static_cast(invokingState->transition(0)); following = nextTokens(rt->followState); expected.addAll(following); diff --git a/runtime/Cpp/runtime/src/atn/ATN.h b/runtime/Cpp/runtime/src/atn/ATN.h index 0164083fd..d1f028a40 100755 --- a/runtime/Cpp/runtime/src/atn/ATN.h +++ b/runtime/Cpp/runtime/src/atn/ATN.h @@ -38,7 +38,7 @@ namespace atn { class ANTLR4CPP_PUBLIC ATN { public: - static const int INVALID_ALT_NUMBER = 0; + static const size_t INVALID_ALT_NUMBER = 0; /// Used for runtime deserialization of ATNs from strings. ATN(); @@ -72,7 +72,7 @@ namespace atn { /// /// deserialization option was specified; otherwise, this is {@code null}. /// - std::vector ruleToTokenType; + std::vector ruleToTokenType; /// For lexer ATNs, this is an array of {@link LexerAction} objects which may /// be referenced by action transitions in the ATN. @@ -104,9 +104,9 @@ namespace atn { virtual int defineDecisionState(DecisionState *s); - virtual DecisionState *getDecisionState(int decision) const; + virtual DecisionState *getDecisionState(size_t decision) const; - virtual int getNumberOfDecisions() const; + virtual size_t getNumberOfDecisions() const; /// /// Computes the set of input symbols which could follow ATN state number @@ -126,7 +126,7 @@ 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, RuleContext *context) const; + virtual misc::IntervalSet getExpectedTokens(size_t stateNumber, RuleContext *context) const; std::string toString() const; }; diff --git a/runtime/Cpp/runtime/src/atn/ATNConfig.cpp b/runtime/Cpp/runtime/src/atn/ATNConfig.cpp index 942dbeb59..80aa1a773 100755 --- a/runtime/Cpp/runtime/src/atn/ATNConfig.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNConfig.cpp @@ -77,7 +77,7 @@ ATNConfig::~ATNConfig() { size_t ATNConfig::hashCode() const { size_t hashCode = misc::MurmurHash::initialize(7); - hashCode = misc::MurmurHash::update(hashCode, (size_t)state->stateNumber); + hashCode = misc::MurmurHash::update(hashCode, state->stateNumber); hashCode = misc::MurmurHash::update(hashCode, (size_t)alt); hashCode = misc::MurmurHash::update(hashCode, context); hashCode = misc::MurmurHash::update(hashCode, semanticContext); diff --git a/runtime/Cpp/runtime/src/atn/ATNConfigSet.cpp b/runtime/Cpp/runtime/src/atn/ATNConfigSet.cpp index 677b0d900..0fea3628e 100755 --- a/runtime/Cpp/runtime/src/atn/ATNConfigSet.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNConfigSet.cpp @@ -238,7 +238,7 @@ std::string ATNConfigSet::toString() { size_t ATNConfigSet::getHash(ATNConfig *c) { size_t hashCode = 7; - hashCode = 31 * hashCode + (size_t)c->state->stateNumber; + hashCode = 31 * hashCode + c->state->stateNumber; hashCode = 31 * hashCode + (size_t)c->alt; hashCode = 31 * hashCode + c->semanticContext->hashCode(); return hashCode; diff --git a/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp b/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp index bed0c74c0..8503068d7 100755 --- a/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNDeserializer.cpp @@ -163,18 +163,18 @@ ATN ATNDeserializer::deserialize(const std::vector& input) { // // STATES // - std::vector> loopBackStateNumbers; - std::vector> endStateNumbers; - int nstates = data[p++]; - for (int i = 0; i < nstates; i++) { - int stype = data[p++]; + std::vector> loopBackStateNumbers; + std::vector> endStateNumbers; + size_t nstates = data[p++]; + for (size_t i = 0; i < nstates; i++) { + size_t stype = data[p++]; // ignore bad type of states if (stype == ATNState::ATN_INVALID_TYPE) { atn.addState(nullptr); continue; } - int ruleIndex = data[p++]; + size_t ruleIndex = data[p++]; if (ruleIndex == 0xFFFF) { // Max Unicode char limit imposed by ANTLR. ruleIndex = -1; } @@ -196,29 +196,29 @@ ATN ATNDeserializer::deserialize(const std::vector& input) { } for (auto &pair : endStateNumbers) { - pair.first->endState = (BlockEndState*)atn.states[(size_t)pair.second]; + pair.first->endState = (BlockEndState*)atn.states[pair.second]; } - int numNonGreedyStates = data[p++]; - for (int i = 0; i < numNonGreedyStates; i++) { - int stateNumber = data[p++]; + size_t numNonGreedyStates = data[p++]; + for (size_t i = 0; i < numNonGreedyStates; i++) { + size_t stateNumber = data[p++]; // The serialized ATN must be specifying the right states, so that the // cast below is correct. - ((DecisionState *)atn.states[(size_t)stateNumber])->nonGreedy = true; + ((DecisionState *)atn.states[stateNumber])->nonGreedy = true; } if (supportsPrecedencePredicates) { - int numPrecedenceStates = data[p++]; - for (int i = 0; i < numPrecedenceStates; i++) { - int stateNumber = data[p++]; - ((RuleStartState *)atn.states[(size_t)stateNumber])->isLeftRecursiveRule = true; + size_t numPrecedenceStates = data[p++]; + for (size_t i = 0; i < numPrecedenceStates; i++) { + size_t stateNumber = data[p++]; + ((RuleStartState *)atn.states[stateNumber])->isLeftRecursiveRule = true; } } // // RULES // - size_t nrules = (size_t)data[p++]; + size_t nrules = data[p++]; if (atn.grammarType == ATNType::LEXER) { atn.ruleToTokenType.resize(nrules); } @@ -229,7 +229,7 @@ ATN ATNDeserializer::deserialize(const std::vector& input) { RuleStartState *startState = (RuleStartState*)atn.states[s]; atn.ruleToStartState.push_back(startState); if (atn.grammarType == ATNType::LEXER) { - int tokenType = data[p++]; + size_t tokenType = data[p++]; if (tokenType == 0xFFFF) { tokenType = Token::EOF; } @@ -252,16 +252,16 @@ ATN ATNDeserializer::deserialize(const std::vector& input) { } RuleStopState *stopState = static_cast(state); - atn.ruleToStopState[(size_t)state->ruleIndex] = stopState; - atn.ruleToStartState[(size_t)state->ruleIndex]->stopState = stopState; + atn.ruleToStopState[state->ruleIndex] = stopState; + atn.ruleToStartState[state->ruleIndex]->stopState = stopState; } // // MODES // - int nmodes = data[p++]; - for (int i = 0; i < nmodes; i++) { - size_t s = (size_t)data[p++]; + size_t nmodes = data[p++]; + for (size_t i = 0; i < nmodes; i++) { + size_t s = data[p++]; atn.modeToStartState.push_back(static_cast(atn.states[s])); } @@ -291,12 +291,12 @@ ATN ATNDeserializer::deserialize(const std::vector& input) { // int nedges = data[p++]; for (int i = 0; i < nedges; i++) { - int src = data[p]; - int trg = data[p + 1]; - int ttype = data[p + 2]; - int arg1 = data[p + 3]; - int arg2 = data[p + 4]; - int arg3 = data[p + 5]; + size_t src = data[p]; + size_t trg = data[p + 1]; + size_t ttype = data[p + 2]; + size_t arg1 = data[p + 3]; + size_t arg2 = data[p + 4]; + size_t arg3 = data[p + 5]; Transition *trans = edgeFactory(atn, ttype, src, trg, arg1, arg2, arg3, sets); ATNState *srcState = atn.states[(size_t)src]; srcState->addTransition(trans); @@ -312,7 +312,7 @@ ATN ATNDeserializer::deserialize(const std::vector& input) { } RuleTransition *ruleTransition = static_cast(t); - int outermostPrecedenceReturn = -1; + size_t outermostPrecedenceReturn = INVALID_INDEX; if (atn.ruleToStartState[ruleTransition->target->ruleIndex]->isLeftRecursiveRule) { if (ruleTransition->precedence == 0) { outermostPrecedenceReturn = ruleTransition->target->ruleIndex; @@ -405,10 +405,10 @@ ATN ATNDeserializer::deserialize(const std::vector& input) { continue; } - int ruleIndex = static_cast(transition)->ruleIndex; - int actionIndex = static_cast(transition)->actionIndex; + size_t ruleIndex = static_cast(transition)->ruleIndex; + size_t actionIndex = static_cast(transition)->actionIndex; Ref lexerAction = std::make_shared(ruleIndex, actionIndex); - state->setTransition(i, new ActionTransition(transition->target, ruleIndex, (int)atn.lexerActions.size(), false)); /* mem-check freed in ATNState d-tor */ + state->setTransition(i, new ActionTransition(transition->target, ruleIndex, atn.lexerActions.size(), false)); /* mem-check freed in ATNState d-tor */ atn.lexerActions.push_back(lexerAction); } } @@ -447,7 +447,7 @@ ATN ATNDeserializer::deserialize(const std::vector& input) { // wrap from the beginning of the rule to the StarLoopEntryState endState = nullptr; for (ATNState *state : atn.states) { - if (state->ruleIndex != (int)i) { + if (state->ruleIndex != i) { continue; } @@ -617,10 +617,11 @@ Guid ATNDeserializer::toUUID(const unsigned short *data, int offset) { } /* mem check: all created instances are freed in the d-tor of the ATNState they are added to. */ -Transition *ATNDeserializer::edgeFactory(const ATN &atn, int type, int /*src*/, int trg, int arg1, int arg2, int arg3, +Transition *ATNDeserializer::edgeFactory(const ATN &atn, size_t type, size_t /*src*/, size_t trg, size_t arg1, + size_t arg2, size_t arg3, const std::vector &sets) { - ATNState *target = atn.states[(size_t)trg]; + ATNState *target = atn.states[trg]; switch (type) { case Transition::EPSILON : return new EpsilonTransition(target); @@ -631,11 +632,11 @@ Transition *ATNDeserializer::edgeFactory(const ATN &atn, int type, int /*src*/, return new RangeTransition(target, arg1, arg2); } case Transition::RULE : - return new RuleTransition(static_cast(atn.states[(size_t)arg1]), arg2, arg3, target); + return new RuleTransition(static_cast(atn.states[arg1]), arg2, (int)arg3, target); case Transition::PREDICATE : return new PredicateTransition(target, arg1, arg2, arg3 != 0); case Transition::PRECEDENCE: - return new PrecedencePredicateTransition(target, arg1); + return new PrecedencePredicateTransition(target, (int)arg1); case Transition::ATOM : if (arg3 != 0) { return new AtomTransition(target, Token::EOF); @@ -645,9 +646,9 @@ Transition *ATNDeserializer::edgeFactory(const ATN &atn, int type, int /*src*/, case Transition::ACTION : return new ActionTransition(target, arg1, arg2, arg3 != 0); case Transition::SET : - return new SetTransition(target, sets[(size_t)arg1]); + return new SetTransition(target, sets[arg1]); case Transition::NOT_SET : - return new NotSetTransition(target, sets[(size_t)arg1]); + return new NotSetTransition(target, sets[arg1]); case Transition::WILDCARD : return new WildcardTransition(target); } @@ -656,7 +657,7 @@ Transition *ATNDeserializer::edgeFactory(const ATN &atn, int type, int /*src*/, } /* mem check: all created instances are freed in the d-tor of the ATN. */ -ATNState* ATNDeserializer::stateFactory(int type, int ruleIndex) { +ATNState* ATNDeserializer::stateFactory(size_t type, size_t ruleIndex) { ATNState *s; switch (type) { case ATNState::ATN_INVALID_TYPE: diff --git a/runtime/Cpp/runtime/src/atn/ATNDeserializer.h b/runtime/Cpp/runtime/src/atn/ATNDeserializer.h index e9c923684..a63a2da7f 100755 --- a/runtime/Cpp/runtime/src/atn/ATNDeserializer.h +++ b/runtime/Cpp/runtime/src/atn/ATNDeserializer.h @@ -57,10 +57,10 @@ namespace atn { static void checkCondition(bool condition); static void checkCondition(bool condition, const std::string &message); - static Transition *edgeFactory(const ATN &atn, int type, int src, int trg, int arg1, int arg2, int arg3, - const std::vector &sets); + static Transition *edgeFactory(const ATN &atn, size_t type, size_t src, size_t trg, size_t arg1, size_t arg2, + size_t arg3, const std::vector &sets); - static ATNState *stateFactory(int type, int ruleIndex); + static ATNState *stateFactory(size_t type, size_t ruleIndex); protected: /// Determines if a particular serialized representation of an ATN supports diff --git a/runtime/Cpp/runtime/src/atn/ATNSerializer.cpp b/runtime/Cpp/runtime/src/atn/ATNSerializer.cpp index e779fa023..e4452b998 100755 --- a/runtime/Cpp/runtime/src/atn/ATNSerializer.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNSerializer.cpp @@ -83,15 +83,15 @@ std::vector ATNSerializer::serialize() { // convert grammar type to ATN const to avoid dependence on ANTLRParser data.push_back((size_t)atn->grammarType); - data.push_back((size_t)atn->maxTokenType); + data.push_back(atn->maxTokenType); size_t nedges = 0; std::unordered_map setIndices; std::vector sets; // dump states, count edges and collect sets while doing so - std::vector nonGreedyStates; - std::vector precedenceStates; + std::vector nonGreedyStates; + std::vector precedenceStates; data.push_back(atn->states.size()); for (ATNState *s : atn->states) { if (s == nullptr) { // might be optimized away @@ -110,18 +110,18 @@ std::vector ATNSerializer::serialize() { data.push_back((size_t)stateType); - if (s->ruleIndex == -1) { + if (s->ruleIndex == INVALID_INDEX) { data.push_back(0xFFFF); } else { - data.push_back((size_t)s->ruleIndex); + data.push_back(s->ruleIndex); } if (s->getStateType() == ATNState::LOOP_END) { - data.push_back((size_t)(static_cast(s))->loopBackState->stateNumber); + data.push_back((static_cast(s))->loopBackState->stateNumber); } else if (is(s)) { - data.push_back((size_t)(static_cast(s))->endState->stateNumber); + data.push_back((static_cast(s))->endState->stateNumber); } if (s->getStateType() != ATNState::RULE_STOP) { @@ -132,7 +132,7 @@ std::vector ATNSerializer::serialize() { for (size_t i = 0; i < s->getNumberOfTransitions(); i++) { Transition *t = s->transition(i); - int edgeType = t->getSerializationType(); + Transition::SerializationType edgeType = t->getSerializationType(); if (edgeType == Transition::SET || edgeType == Transition::NOT_SET) { SetTransition *st = static_cast(t); if (setIndices.find(st->set) == setIndices.end()) { @@ -146,26 +146,26 @@ std::vector ATNSerializer::serialize() { // non-greedy states data.push_back(nonGreedyStates.size()); for (size_t i = 0; i < nonGreedyStates.size(); i++) { - data.push_back((size_t)nonGreedyStates.at(i)); + data.push_back(nonGreedyStates.at(i)); } // precedence states data.push_back(precedenceStates.size()); for (size_t i = 0; i < precedenceStates.size(); i++) { - data.push_back((size_t)precedenceStates.at(i)); + data.push_back(precedenceStates.at(i)); } size_t nrules = atn->ruleToStartState.size(); data.push_back(nrules); for (size_t r = 0; r < nrules; r++) { ATNState *ruleStartState = atn->ruleToStartState[r]; - data.push_back((size_t)ruleStartState->stateNumber); + data.push_back(ruleStartState->stateNumber); if (atn->grammarType == ATNType::LEXER) { if (atn->ruleToTokenType[r] == Token::EOF) { data.push_back(0xFFFF); } else { - data.push_back((size_t)atn->ruleToTokenType[r]); + data.push_back(atn->ruleToTokenType[r]); } } } @@ -174,7 +174,7 @@ std::vector ATNSerializer::serialize() { data.push_back(nmodes); if (nmodes > 0) { for (const auto &modeStartState : atn->modeToStartState) { - data.push_back((size_t)modeStartState->stateNumber); + data.push_back(modeStartState->stateNumber); } } @@ -182,7 +182,7 @@ std::vector ATNSerializer::serialize() { data.push_back(nsets); for (auto set : sets) { bool containsEof = set.contains(Token::EOF); - if (containsEof && set.getIntervals().at(0).b == Token::EOF) { + if (containsEof && set.getIntervals().at(0).b == -1) { data.push_back(set.getIntervals().size() - 1); } else { @@ -191,18 +191,18 @@ std::vector ATNSerializer::serialize() { data.push_back(containsEof ? 1 : 0); for (auto &interval : set.getIntervals()) { - if (interval.a == Token::EOF) { - if (interval.b == Token::EOF) { + if (interval.a == -1) { + if (interval.b == -1) { continue; } else { data.push_back(0); } } else { - data.push_back((size_t)interval.a); + data.push_back(interval.a); } - data.push_back((size_t)interval.b); + data.push_back(interval.b); } } @@ -220,16 +220,16 @@ std::vector ATNSerializer::serialize() { for (size_t i = 0; i < s->getNumberOfTransitions(); i++) { Transition *t = s->transition(i); - if (atn->states[(size_t)t->target->stateNumber] == nullptr) { + if (atn->states[t->target->stateNumber] == nullptr) { throw IllegalStateException("Cannot serialize a transition to a removed state."); } - int src = s->stateNumber; - int trg = t->target->stateNumber; - int edgeType = t->getSerializationType(); - int arg1 = 0; - int arg2 = 0; - int arg3 = 0; + size_t src = s->stateNumber; + size_t trg = t->target->stateNumber; + Transition::SerializationType edgeType = t->getSerializationType(); + size_t arg1 = 0; + size_t arg2 = 0; + size_t arg3 = 0; switch (edgeType) { case Transition::RULE: trg = (static_cast(t))->followState->stateNumber; @@ -253,8 +253,8 @@ std::vector ATNSerializer::serialize() { } break; case Transition::RANGE: - arg1 = (int)(static_cast(t))->from; - arg2 = (int)(static_cast(t))->to; + arg1 = (static_cast(t))->from; + arg2 = (static_cast(t))->to; if (arg1 == Token::EOF) { arg1 = 0; arg3 = 1; @@ -262,7 +262,7 @@ std::vector ATNSerializer::serialize() { break; case Transition::ATOM: - arg1 = (int)(static_cast(t))->_label; + arg1 = (static_cast(t))->_label; if (arg1 == Token::EOF) { arg1 = 0; arg3 = 1; @@ -274,7 +274,7 @@ std::vector ATNSerializer::serialize() { ActionTransition *at = static_cast(t); arg1 = at->ruleIndex; arg2 = at->actionIndex; - if (arg2 == -1) { + if (arg2 == INVALID_INDEX) { arg2 = 0xFFFF; } @@ -288,23 +288,24 @@ std::vector ATNSerializer::serialize() { case Transition::NOT_SET: arg1 = setIndices[(static_cast(t))->set]; break; - case Transition::WILDCARD: + + default: break; } - data.push_back((size_t)src); - data.push_back((size_t)trg); - data.push_back((size_t)edgeType); - data.push_back((size_t)arg1); - data.push_back((size_t)arg2); - data.push_back((size_t)arg3); + data.push_back(src); + data.push_back(trg); + data.push_back(edgeType); + data.push_back(arg1); + data.push_back(arg2); + data.push_back(arg3); } } size_t ndecisions = atn->decisionToState.size(); data.push_back(ndecisions); for (DecisionState *decStartState : atn->decisionToState) { - data.push_back((size_t)decStartState->stateNumber); + data.push_back(decStartState->stateNumber); } // LEXER ACTIONS @@ -405,7 +406,7 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { std::string buf; int p = 0; - size_t version = (size_t)data[p++]; + size_t version = data[p++]; if (version != ATNDeserializer::SERIALIZED_VERSION) { std::string reason = "Could not deserialize ATN with version " + std::to_string(version) + "(expected " + std::to_string(ATNDeserializer::SERIALIZED_VERSION) + ")."; @@ -425,11 +426,11 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { buf.append("max type ").append(std::to_string(maxType)).append("\n"); int nstates = data[p++]; for (int i = 0; i < nstates; i++) { - int stype = data[p++]; + size_t stype = data[p++]; if (stype == ATNState::ATN_INVALID_TYPE) { // ignore bad type of states continue; } - int ruleIndex = data[p++]; + size_t ruleIndex = data[p++]; if (ruleIndex == 0xFFFF) { ruleIndex = -1; } @@ -447,13 +448,13 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { } buf.append(std::to_string(i)) .append(":") - .append(ATNState::serializationNames[(size_t)stype]) + .append(ATNState::serializationNames[stype]) .append(" ") .append(std::to_string(ruleIndex)) .append(arg) .append("\n"); } - int numNonGreedyStates = data[p++]; + size_t numNonGreedyStates = data[p++]; p += numNonGreedyStates; // Instead of that useless loop below. /* for (int i = 0; i < numNonGreedyStates; i++) { @@ -461,7 +462,7 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { } */ - int numPrecedenceStates = data[p++]; + size_t numPrecedenceStates = data[p++]; p += numPrecedenceStates; /* for (int i = 0; i < numPrecedenceStates; i++) { @@ -469,11 +470,11 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { } */ - int nrules = data[p++]; - for (int i = 0; i < nrules; i++) { - int s = data[p++]; + size_t nrules = data[p++]; + for (size_t i = 0; i < nrules; i++) { + size_t s = data[p++]; if (atn->grammarType == ATNType::LEXER) { - int arg1 = data[p++]; + size_t arg1 = data[p++]; buf.append("rule ") .append(std::to_string(i)) .append(":") @@ -490,25 +491,25 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { .append("\n"); } } - int nmodes = data[p++]; - for (int i = 0; i < nmodes; i++) { - int s = data[p++]; + size_t nmodes = data[p++]; + for (size_t i = 0; i < nmodes; i++) { + size_t s = data[p++]; buf.append("mode ") .append(std::to_string(i)) .append(":") .append(std::to_string(s)) .append("\n"); } - int nsets = data[p++]; - for (int i = 0; i < nsets; i++) { - int nintervals = data[p++]; + size_t nsets = data[p++]; + for (size_t i = 0; i < nsets; i++) { + size_t nintervals = data[p++]; buf.append(std::to_string(i)).append(":"); bool containsEof = data[p++] != 0; if (containsEof) { buf.append(getTokenName(Token::EOF)); } - for (int j = 0; j < nintervals; j++) { + for (size_t j = 0; j < nintervals; j++) { if (containsEof || j > 0) { buf.append(", "); } @@ -520,19 +521,19 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { } buf.append("\n"); } - int nedges = data[p++]; - for (int i = 0; i < nedges; i++) { - int src = data[p]; - int trg = data[p + 1]; - int ttype = data[p + 2]; - int arg1 = data[p + 3]; - int arg2 = data[p + 4]; - int arg3 = data[p + 5]; + size_t nedges = data[p++]; + for (size_t i = 0; i < nedges; i++) { + size_t src = data[p]; + size_t trg = data[p + 1]; + size_t ttype = data[p + 2]; + size_t arg1 = data[p + 3]; + size_t arg2 = data[p + 4]; + size_t arg3 = data[p + 5]; buf.append(std::to_string(src)) .append("->") .append(std::to_string(trg)) .append(" ") - .append(Transition::serializationNames[(size_t)ttype]) + .append(Transition::serializationNames[ttype]) .append(" ") .append(std::to_string(arg1)) .append(",") @@ -542,10 +543,10 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { .append("\n"); p += 6; } - int ndecisions = data[p++]; - for (int i = 0; i < ndecisions; i++) { - int s = data[p++]; - buf.append(std::to_string(i)).append(":").append(std::to_string(s)).append("\n"); + size_t ndecisions = data[p++]; + for (size_t i = 0; i < ndecisions; i++) { + size_t s = data[p++]; + buf += std::to_string(i) + ":" + std::to_string(s) + "\n"; } if (atn->grammarType == ATNType::LEXER) { @@ -564,12 +565,12 @@ std::string ATNSerializer::decode(const std::wstring &inpdata) { return buf; } -std::string ATNSerializer::getTokenName(ssize_t t) { - if (t == -1) { +std::string ATNSerializer::getTokenName(size_t t) { + if (t == Token::EOF) { return "EOF"; } - if (atn->grammarType == ATNType::LEXER && t >= 0 && t <= 0xFFFF) { + if (atn->grammarType == ATNType::LEXER && t <= 0x10FFFF) { switch (t) { case '\n': return "'\\n'"; @@ -587,19 +588,19 @@ std::string ATNSerializer::getTokenName(ssize_t t) { return "'\\''"; default: std::string s_hex = antlrcpp::toHexString((int)t); - if (s_hex >= "0" && s_hex <= "7F" && - !iscntrl((int)t)) { + if (s_hex >= "0" && s_hex <= "7F" && !iscntrl((int)t)) { return "'" + std::to_string(t) + "'"; } - // turn on the bit above max "\uFFFF" value so that we pad with zeros - // then only take last 4 digits - std::string hex = antlrcpp::toHexString((int)t | 0x10000).substr(1, 4); + + // turn on the bit above max "\u10FFFF" value so that we pad with zeros + // then only take last 6 digits + std::string hex = antlrcpp::toHexString((int)t | 0x1000000).substr(1, 6); std::string unicodeStr = std::string("'\\u") + hex + std::string("'"); return unicodeStr; } } - if (_tokenNames.size() > 0 && t >= 0 && t < (ssize_t)_tokenNames.size()) { + if (_tokenNames.size() > 0 && t < _tokenNames.size()) { return _tokenNames[(size_t)t]; } diff --git a/runtime/Cpp/runtime/src/atn/ATNSerializer.h b/runtime/Cpp/runtime/src/atn/ATNSerializer.h index 1eb2a85f5..517c364c3 100755 --- a/runtime/Cpp/runtime/src/atn/ATNSerializer.h +++ b/runtime/Cpp/runtime/src/atn/ATNSerializer.h @@ -69,7 +69,7 @@ namespace atn { virtual std::vector serialize(); virtual std::string decode(const std::wstring& data); - virtual std::string getTokenName(ssize_t t); + virtual std::string getTokenName(size_t t); /// Used by Java target to encode short/int array as chars in string. static std::wstring getSerializedAsString(ATN *atn); diff --git a/runtime/Cpp/runtime/src/atn/ATNState.cpp b/runtime/Cpp/runtime/src/atn/ATNState.cpp index 4a3175e5e..637729c6b 100755 --- a/runtime/Cpp/runtime/src/atn/ATNState.cpp +++ b/runtime/Cpp/runtime/src/atn/ATNState.cpp @@ -39,9 +39,6 @@ using namespace antlr4::atn; using namespace antlrcpp; -const int ATNState::INITIAL_NUM_TRANSITIONS = 4; -const int ATNState::INVALID_STATE_NUMBER = -1; - ATNState::ATNState() { } @@ -58,7 +55,7 @@ const std::vector ATNState::serializationNames = { }; size_t ATNState::hashCode() { - return (size_t)stateNumber; + return stateNumber; } bool ATNState::operator == (const ATNState &other) { @@ -72,10 +69,10 @@ bool ATNState::isNonGreedyExitState() { std::string ATNState::toString() const { std::stringstream ss; ss << "(ATNState " << std::hex << this << std::dec << ") {" << std::endl; - if (stateNumber < 0 || stateNumber >= (int)serializationNames.size()) + if (stateNumber == INVALID_STATE_NUMBER || stateNumber >= serializationNames.size()) ss << " state: INVALID "; else - ss << " state: " << serializationNames[(size_t)stateNumber]; + ss << " state: " << serializationNames[stateNumber]; ss << " (" << stateNumber << ")" << std::endl; ss << " ruleIndex: " << ruleIndex << std::endl << " epsilonOnlyTransitions: " << epsilonOnlyTransitions << std::endl; ss << " transistions (" << transitions.size() << "):" << std::endl; @@ -100,7 +97,7 @@ void ATNState::addTransition(Transition *e) { addTransition((int)transitions.size(), e); } -void ATNState::addTransition(int index, Transition *e) { +void ATNState::addTransition(size_t index, Transition *e) { if (transitions.empty()) { epsilonOnlyTransitions = e->isEpsilon(); } else if (epsilonOnlyTransitions != e->isEpsilon()) { @@ -119,7 +116,7 @@ void ATNState::setTransition(size_t i, Transition *e) { transitions[i] = e; } -Transition *ATNState::removeTransition(int index) { +Transition *ATNState::removeTransition(size_t index) { transitions.erase(transitions.begin() + index); return nullptr; } @@ -128,6 +125,6 @@ bool ATNState::onlyHasEpsilonTransitions() { return epsilonOnlyTransitions; } -void ATNState::setRuleIndex(int index) { +void ATNState::setRuleIndex(size_t index) { ruleIndex = index; } diff --git a/runtime/Cpp/runtime/src/atn/ATNState.h b/runtime/Cpp/runtime/src/atn/ATNState.h index ed2306a1f..01402a025 100755 --- a/runtime/Cpp/runtime/src/atn/ATNState.h +++ b/runtime/Cpp/runtime/src/atn/ATNState.h @@ -102,8 +102,8 @@ namespace atn { virtual ~ATNState(); - static const int INITIAL_NUM_TRANSITIONS; - static const int INVALID_STATE_NUMBER; + static const size_t INITIAL_NUM_TRANSITIONS = 4; + static const size_t INVALID_STATE_NUMBER = (size_t)-1; enum { ATN_INVALID_TYPE = 0, @@ -124,10 +124,8 @@ namespace atn { static const std::vector serializationNames; /// 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 + size_t stateNumber = INVALID_STATE_NUMBER; + size_t ruleIndex = 0; // at runtime, we don't have Rule objects bool epsilonOnlyTransitions = false; protected: @@ -146,13 +144,13 @@ namespace atn { virtual std::vector getTransitions(); virtual size_t getNumberOfTransitions(); virtual void addTransition(Transition *e); - virtual void addTransition(int index, Transition *e); + virtual void addTransition(size_t index, Transition *e); virtual Transition *transition(size_t i); virtual void setTransition(size_t i, Transition *e); - virtual Transition *removeTransition(int index); + virtual Transition* removeTransition(size_t index); virtual int getStateType() = 0; bool onlyHasEpsilonTransitions(); - virtual void setRuleIndex(int index); + virtual void setRuleIndex(size_t index); }; diff --git a/runtime/Cpp/runtime/src/atn/ActionTransition.cpp b/runtime/Cpp/runtime/src/atn/ActionTransition.cpp index 818a05ef3..3adabab5a 100755 --- a/runtime/Cpp/runtime/src/atn/ActionTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/ActionTransition.cpp @@ -33,15 +33,15 @@ using namespace antlr4::atn; -ActionTransition::ActionTransition(ATNState *target, int ruleIndex) - : Transition(target), ruleIndex(ruleIndex), actionIndex(0), isCtxDependent(false) { +ActionTransition::ActionTransition(ATNState *target, size_t ruleIndex) + : Transition(target), ruleIndex(ruleIndex), actionIndex(INVALID_INDEX), isCtxDependent(false) { } -ActionTransition::ActionTransition(ATNState *target, int ruleIndex, int actionIndex, bool isCtxDependent) +ActionTransition::ActionTransition(ATNState *target, size_t ruleIndex, size_t actionIndex, bool isCtxDependent) : Transition(target), ruleIndex(ruleIndex), actionIndex(actionIndex), isCtxDependent(isCtxDependent) { } -int ActionTransition::getSerializationType() const { +Transition::SerializationType ActionTransition::getSerializationType() const { return ACTION; } @@ -49,7 +49,7 @@ bool ActionTransition::isEpsilon() const { return true; // we are to be ignored by analysis 'cept for predicates } -bool ActionTransition::matches(ssize_t /*symbol*/, ssize_t /*minVocabSymbol*/, ssize_t /*maxVocabSymbol*/) const { +bool ActionTransition::matches(size_t /*symbol*/, size_t /*minVocabSymbol*/, size_t /*maxVocabSymbol*/) const { return false; } diff --git a/runtime/Cpp/runtime/src/atn/ActionTransition.h b/runtime/Cpp/runtime/src/atn/ActionTransition.h index b4c8740fb..74bfa00e6 100755 --- a/runtime/Cpp/runtime/src/atn/ActionTransition.h +++ b/runtime/Cpp/runtime/src/atn/ActionTransition.h @@ -38,19 +38,19 @@ namespace atn { class ANTLR4CPP_PUBLIC ActionTransition final : public Transition { public: - const int ruleIndex; - const int actionIndex; + const size_t ruleIndex; + const size_t actionIndex; const bool isCtxDependent; // e.g., $i ref in action - ActionTransition(ATNState *target, int ruleIndex); //this(target, ruleIndex, -1, false); + ActionTransition(ATNState *target, size_t ruleIndex); - ActionTransition(ATNState *target, int ruleIndex, int actionIndex, bool isCtxDependent); + ActionTransition(ATNState *target, size_t ruleIndex, size_t actionIndex, bool isCtxDependent); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; virtual bool isEpsilon() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/atn/AmbiguityInfo.cpp b/runtime/Cpp/runtime/src/atn/AmbiguityInfo.cpp index 3695037a6..ab8d96e5a 100755 --- a/runtime/Cpp/runtime/src/atn/AmbiguityInfo.cpp +++ b/runtime/Cpp/runtime/src/atn/AmbiguityInfo.cpp @@ -34,7 +34,7 @@ using namespace antlr4; using namespace antlr4::atn; -AmbiguityInfo::AmbiguityInfo(int decision, ATNConfigSet *configs, const antlrcpp::BitSet &ambigAlts, +AmbiguityInfo::AmbiguityInfo(size_t decision, ATNConfigSet *configs, const antlrcpp::BitSet &ambigAlts, TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx) : DecisionEventInfo(decision, configs, input, startIndex, stopIndex, fullCtx) { diff --git a/runtime/Cpp/runtime/src/atn/AmbiguityInfo.h b/runtime/Cpp/runtime/src/atn/AmbiguityInfo.h index b1a62a52f..603eeb3f0 100755 --- a/runtime/Cpp/runtime/src/atn/AmbiguityInfo.h +++ b/runtime/Cpp/runtime/src/atn/AmbiguityInfo.h @@ -86,7 +86,7 @@ namespace atn { /// {@code true} if the ambiguity was identified during LL /// prediction; otherwise, {@code false} if the ambiguity was identified /// during SLL prediction - AmbiguityInfo(int decision, ATNConfigSet *configs, const antlrcpp::BitSet &ambigAlts, TokenStream *input, + AmbiguityInfo(size_t decision, ATNConfigSet *configs, const antlrcpp::BitSet &ambigAlts, TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx); }; diff --git a/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp b/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp index df2905fbe..6b581cec2 100755 --- a/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp +++ b/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp @@ -41,7 +41,7 @@ ArrayPredictionContext::ArrayPredictionContext(Ref c } ArrayPredictionContext::ArrayPredictionContext(std::vector> parents_, - std::vector const& returnStates) + std::vector const& returnStates) : PredictionContext(calculateHashCode(parents_, returnStates)), parents(makeRef(parents_)), returnStates(returnStates) { assert(parents.size() > 0); assert(returnStates.size() > 0); @@ -60,7 +60,7 @@ std::weak_ptr ArrayPredictionContext::getParent(size_t index) return parents[index]; } -int ArrayPredictionContext::getReturnState(size_t index) const { +size_t ArrayPredictionContext::getReturnState(size_t index) const { return returnStates[index]; } diff --git a/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.h b/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.h index 502db019d..a115fa4a8 100755 --- a/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.h +++ b/runtime/Cpp/runtime/src/atn/ArrayPredictionContext.h @@ -49,17 +49,17 @@ namespace atn { const std::vector> parents; /// Sorted for merge, no duplicates; if present, EMPTY_RETURN_STATE is always last. - const std::vector returnStates; + const std::vector returnStates; ArrayPredictionContext(Ref const& a); ArrayPredictionContext(std::vector> parents_, - std::vector const& returnStates); + std::vector const& returnStates); virtual ~ArrayPredictionContext() {}; virtual bool isEmpty() const override; virtual size_t size() const override; virtual std::weak_ptr getParent(size_t index) const override; - virtual int getReturnState(size_t index) const override; + virtual size_t getReturnState(size_t index) const override; bool operator == (const PredictionContext &o) const override; virtual std::string toString() const override; diff --git a/runtime/Cpp/runtime/src/atn/AtomTransition.cpp b/runtime/Cpp/runtime/src/atn/AtomTransition.cpp index f639f2b06..55900443b 100755 --- a/runtime/Cpp/runtime/src/atn/AtomTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/AtomTransition.cpp @@ -37,10 +37,10 @@ using namespace antlr4::misc; using namespace antlr4::atn; -AtomTransition::AtomTransition(ATNState *target, ssize_t label) : Transition(target), _label(label) { +AtomTransition::AtomTransition(ATNState *target, size_t label) : Transition(target), _label(label) { } -int AtomTransition::getSerializationType() const { +Transition::SerializationType AtomTransition::getSerializationType() const { return ATOM; } @@ -48,7 +48,7 @@ IntervalSet AtomTransition::label() const { return IntervalSet::of((int)_label); } -bool AtomTransition::matches(ssize_t symbol, ssize_t /*minVocabSymbol*/, ssize_t /*maxVocabSymbol*/) const { +bool AtomTransition::matches(size_t symbol, size_t /*minVocabSymbol*/, size_t /*maxVocabSymbol*/) const { return _label == symbol; } diff --git a/runtime/Cpp/runtime/src/atn/AtomTransition.h b/runtime/Cpp/runtime/src/atn/AtomTransition.h index 25c961b91..00c290600 100755 --- a/runtime/Cpp/runtime/src/atn/AtomTransition.h +++ b/runtime/Cpp/runtime/src/atn/AtomTransition.h @@ -40,14 +40,14 @@ namespace atn { class ANTLR4CPP_PUBLIC AtomTransition final : public Transition { public: /// The token type or character value; or, signifies special label. - const ssize_t _label; + const size_t _label; - AtomTransition(ATNState *target, ssize_t label); + AtomTransition(ATNState *target, size_t label); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; virtual misc::IntervalSet label() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.cpp b/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.cpp index 1b93a20d1..ca0b8094d 100755 --- a/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.cpp +++ b/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.cpp @@ -34,7 +34,7 @@ using namespace antlr4; using namespace antlr4::atn; -ContextSensitivityInfo::ContextSensitivityInfo(int decision, ATNConfigSet *configs, TokenStream *input, +ContextSensitivityInfo::ContextSensitivityInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex) : DecisionEventInfo(decision, configs, input, startIndex, stopIndex, true) { } diff --git a/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.h b/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.h index 4cefb046c..9c84f188c 100755 --- a/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.h +++ b/runtime/Cpp/runtime/src/atn/ContextSensitivityInfo.h @@ -66,7 +66,7 @@ namespace atn { /// The start index for the current prediction /// The index at which the context sensitivity was /// identified during full-context prediction - ContextSensitivityInfo(int decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex); + ContextSensitivityInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex); }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/DecisionEventInfo.cpp b/runtime/Cpp/runtime/src/atn/DecisionEventInfo.cpp index 39748aa83..b1664d447 100755 --- a/runtime/Cpp/runtime/src/atn/DecisionEventInfo.cpp +++ b/runtime/Cpp/runtime/src/atn/DecisionEventInfo.cpp @@ -34,7 +34,7 @@ using namespace antlr4; using namespace antlr4::atn; -DecisionEventInfo::DecisionEventInfo(int decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, +DecisionEventInfo::DecisionEventInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx) : decision(decision), configs(configs), input(input), startIndex(startIndex), stopIndex(stopIndex), fullCtx(fullCtx) { } diff --git a/runtime/Cpp/runtime/src/atn/DecisionEventInfo.h b/runtime/Cpp/runtime/src/atn/DecisionEventInfo.h index bc68d51a3..d9f8b0a54 100755 --- a/runtime/Cpp/runtime/src/atn/DecisionEventInfo.h +++ b/runtime/Cpp/runtime/src/atn/DecisionEventInfo.h @@ -57,7 +57,7 @@ namespace atn { /// The invoked decision number which this event is related to. /// /// - const int decision; + const size_t decision; /// /// The configuration set containing additional information relevant to the @@ -88,7 +88,7 @@ namespace atn { /// const bool fullCtx; - DecisionEventInfo(int decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, + DecisionEventInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx); }; diff --git a/runtime/Cpp/runtime/src/atn/EmptyPredictionContext.cpp b/runtime/Cpp/runtime/src/atn/EmptyPredictionContext.cpp index 5856ccdac..9db539cae 100755 --- a/runtime/Cpp/runtime/src/atn/EmptyPredictionContext.cpp +++ b/runtime/Cpp/runtime/src/atn/EmptyPredictionContext.cpp @@ -48,7 +48,7 @@ std::weak_ptr EmptyPredictionContext::getParent(size_t /*inde return std::weak_ptr(); } -int EmptyPredictionContext::getReturnState(size_t /*index*/) const { +size_t EmptyPredictionContext::getReturnState(size_t /*index*/) const { return returnState; } diff --git a/runtime/Cpp/runtime/src/atn/EmptyPredictionContext.h b/runtime/Cpp/runtime/src/atn/EmptyPredictionContext.h index 852950ffc..bf16c4948 100755 --- a/runtime/Cpp/runtime/src/atn/EmptyPredictionContext.h +++ b/runtime/Cpp/runtime/src/atn/EmptyPredictionContext.h @@ -43,7 +43,7 @@ namespace atn { virtual bool isEmpty() const override; virtual size_t size() const override; virtual std::weak_ptr getParent(size_t index) const override; - virtual int getReturnState(size_t index) const override; + virtual size_t getReturnState(size_t index) const override; virtual std::string toString() const override; virtual bool operator == (const PredictionContext &o) const override; diff --git a/runtime/Cpp/runtime/src/atn/EpsilonTransition.cpp b/runtime/Cpp/runtime/src/atn/EpsilonTransition.cpp index 146ff946e..40e396afe 100755 --- a/runtime/Cpp/runtime/src/atn/EpsilonTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/EpsilonTransition.cpp @@ -33,18 +33,18 @@ using namespace antlr4::atn; -EpsilonTransition::EpsilonTransition(ATNState *target) : EpsilonTransition(target, -1) { +EpsilonTransition::EpsilonTransition(ATNState *target) : EpsilonTransition(target, INVALID_INDEX) { } -EpsilonTransition::EpsilonTransition(ATNState *target, int outermostPrecedenceReturn) +EpsilonTransition::EpsilonTransition(ATNState *target, size_t outermostPrecedenceReturn) : Transition(target), _outermostPrecedenceReturn(outermostPrecedenceReturn) { } -int EpsilonTransition::outermostPrecedenceReturn() { +size_t EpsilonTransition::outermostPrecedenceReturn() { return _outermostPrecedenceReturn; } -int EpsilonTransition::getSerializationType() const { +Transition::SerializationType EpsilonTransition::getSerializationType() const { return EPSILON; } @@ -52,7 +52,7 @@ bool EpsilonTransition::isEpsilon() const { return true; } -bool EpsilonTransition::matches(ssize_t /*symbol*/, ssize_t /*minVocabSymbol*/, ssize_t /*maxVocabSymbol*/) const { +bool EpsilonTransition::matches(size_t /*symbol*/, size_t /*minVocabSymbol*/, size_t /*maxVocabSymbol*/) const { return false; } diff --git a/runtime/Cpp/runtime/src/atn/EpsilonTransition.h b/runtime/Cpp/runtime/src/atn/EpsilonTransition.h index 7fa440108..4320a50a7 100755 --- a/runtime/Cpp/runtime/src/atn/EpsilonTransition.h +++ b/runtime/Cpp/runtime/src/atn/EpsilonTransition.h @@ -39,7 +39,7 @@ namespace atn { class ANTLR4CPP_PUBLIC EpsilonTransition final : public Transition { public: EpsilonTransition(ATNState *target); - EpsilonTransition(ATNState *target, int outermostPrecedenceReturn); + EpsilonTransition(ATNState *target, size_t outermostPrecedenceReturn); /** * @return the rule index of a precedence rule for which this transition is @@ -49,16 +49,16 @@ namespace atn { * @see ParserATNSimulator#applyPrecedenceFilter(ATNConfigSet) * @since 4.4.1 */ - int outermostPrecedenceReturn(); - virtual int getSerializationType() const override; + size_t outermostPrecedenceReturn(); + virtual SerializationType getSerializationType() const override; virtual bool isEpsilon() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; virtual std::string toString() const override; private: - const int _outermostPrecedenceReturn; + const size_t _outermostPrecedenceReturn; // A rule index. }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/ErrorInfo.cpp b/runtime/Cpp/runtime/src/atn/ErrorInfo.cpp index 74440b5a8..996c7a35c 100755 --- a/runtime/Cpp/runtime/src/atn/ErrorInfo.cpp +++ b/runtime/Cpp/runtime/src/atn/ErrorInfo.cpp @@ -36,6 +36,6 @@ using namespace antlr4; using namespace antlr4::atn; -ErrorInfo::ErrorInfo(int decision, ATNConfigSet *configs, TokenStream *input, int startIndex, int stopIndex, bool fullCtx) +ErrorInfo::ErrorInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx) : DecisionEventInfo(decision, configs, input, startIndex, stopIndex, fullCtx) { } diff --git a/runtime/Cpp/runtime/src/atn/ErrorInfo.h b/runtime/Cpp/runtime/src/atn/ErrorInfo.h index ee2178551..d1e0b5fc2 100755 --- a/runtime/Cpp/runtime/src/atn/ErrorInfo.h +++ b/runtime/Cpp/runtime/src/atn/ErrorInfo.h @@ -61,7 +61,7 @@ namespace atn { /// {@code true} if the syntax error was identified during LL /// prediction; otherwise, {@code false} if the syntax error was identified /// during SLL prediction - ErrorInfo(int decision, ATNConfigSet *configs, TokenStream *input, int startIndex, int stopIndex, + ErrorInfo(size_t decision, ATNConfigSet *configs, TokenStream *input, size_t startIndex, size_t stopIndex, bool fullCtx); }; diff --git a/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp b/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp index 15d3e9717..14e8c7c25 100755 --- a/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp +++ b/runtime/Cpp/runtime/src/atn/LL1Analyzer.cpp @@ -124,16 +124,16 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, Ref if (ctx != PredictionContext::EMPTY) { // run thru all possible stack tops in ctx for (size_t i = 0; i < ctx->size(); i++) { - ATNState *returnState = _atn.states[(size_t)ctx->getReturnState(i)]; + ATNState *returnState = _atn.states[ctx->getReturnState(i)]; - bool removed = calledRuleStack.test((size_t)returnState->ruleIndex); + bool removed = calledRuleStack.test(returnState->ruleIndex); auto onExit = finally([removed, &calledRuleStack, returnState] { if (removed) { - calledRuleStack.set((size_t)returnState->ruleIndex); + calledRuleStack.set(returnState->ruleIndex); } }); - calledRuleStack[(size_t)returnState->ruleIndex] = false; + calledRuleStack[returnState->ruleIndex] = false; _LOOK(returnState, stopState, ctx->getParent(i).lock(), look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } return; @@ -145,16 +145,16 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, Ref Transition *t = s->transition(i); if (is(t)) { - if (calledRuleStack[(size_t)(static_cast(t))->target->ruleIndex]) { + if (calledRuleStack[(static_cast(t))->target->ruleIndex]) { continue; } Ref newContext = SingletonPredictionContext::create(ctx, (static_cast(t))->followState->stateNumber); auto onExit = finally([t, &calledRuleStack] { - calledRuleStack[(size_t)((static_cast(t))->target->ruleIndex)] = false; + calledRuleStack[(static_cast(t))->target->ruleIndex] = false; }); - calledRuleStack.set((size_t)(static_cast(t))->target->ruleIndex); + calledRuleStack.set((static_cast(t))->target->ruleIndex); _LOOK(t->target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF); } else if (is(t)) { diff --git a/runtime/Cpp/runtime/src/atn/LL1Analyzer.h b/runtime/Cpp/runtime/src/atn/LL1Analyzer.h index c081b0cf7..64612157e 100755 --- a/runtime/Cpp/runtime/src/atn/LL1Analyzer.h +++ b/runtime/Cpp/runtime/src/atn/LL1Analyzer.h @@ -43,7 +43,7 @@ namespace atn { public: /// Special value added to the lookahead sets to indicate that we hit /// a predicate during analysis if {@code seeThruPreds==false}. - static const int HIT_PRED = Token::INVALID_TYPE; + static const size_t HIT_PRED = Token::INVALID_TYPE; const atn::ATN &_atn; diff --git a/runtime/Cpp/runtime/src/atn/LexerATNConfig.cpp b/runtime/Cpp/runtime/src/atn/LexerATNConfig.cpp index e47281738..f67a8d55e 100755 --- a/runtime/Cpp/runtime/src/atn/LexerATNConfig.cpp +++ b/runtime/Cpp/runtime/src/atn/LexerATNConfig.cpp @@ -77,7 +77,7 @@ bool LexerATNConfig::hasPassedThroughNonGreedyDecision() { size_t LexerATNConfig::hashCode() const { size_t hashCode = misc::MurmurHash::initialize(7); - hashCode = misc::MurmurHash::update(hashCode, (size_t)state->stateNumber); + hashCode = misc::MurmurHash::update(hashCode, state->stateNumber); hashCode = misc::MurmurHash::update(hashCode, (size_t)alt); hashCode = misc::MurmurHash::update(hashCode, context); hashCode = misc::MurmurHash::update(hashCode, semanticContext); diff --git a/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp index 2569a8118..023e115ce 100755 --- a/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/LexerATNSimulator.cpp @@ -90,7 +90,7 @@ void LexerATNSimulator::copyState(LexerATNSimulator *simulator) { _startIndex = simulator->_startIndex; } -int LexerATNSimulator::match(CharStream *input, size_t mode) { +size_t LexerATNSimulator::match(CharStream *input, size_t mode) { match_calls++; _mode = mode; ssize_t mark = input->mark(); @@ -99,7 +99,7 @@ int LexerATNSimulator::match(CharStream *input, size_t mode) { input->release(mark); }); - _startIndex = (int)input->index(); + _startIndex = input->index(); _prevAccept.reset(); const dfa::DFA &dfa = _decisionToDFA[mode]; if (dfa.s0 == nullptr) { @@ -108,12 +108,12 @@ int LexerATNSimulator::match(CharStream *input, size_t mode) { return execATN(input, dfa.s0); } - return -1; + return Token::EOF; } void LexerATNSimulator::reset() { _prevAccept.reset(); - _startIndex = -1; + _startIndex = 0; _line = 1; _charPositionInLine = 0; _mode = Lexer::DEFAULT_MODE; @@ -127,7 +127,7 @@ void LexerATNSimulator::clearDFA() { } } -int LexerATNSimulator::matchATN(CharStream *input) { +size_t LexerATNSimulator::matchATN(CharStream *input) { ATNState *startState = (ATNState *)atn.modeToStartState[_mode]; std::unique_ptr s0_closure = computeStartState(input, startState); @@ -140,19 +140,19 @@ int LexerATNSimulator::matchATN(CharStream *input) { _decisionToDFA[_mode].s0 = next; } - int predict = execATN(input, next); + size_t predict = execATN(input, next); return predict; } -int LexerATNSimulator::execATN(CharStream *input, dfa::DFAState *ds0) { +size_t LexerATNSimulator::execATN(CharStream *input, dfa::DFAState *ds0) { if (ds0->isAcceptState) { // allow zero-length tokens // ml: in Java code this method uses 3 params. The first is a member var of the class anyway (_prevAccept), so why pass it here? captureSimState(input, ds0); } - ssize_t t = input->LA(1); + size_t t = input->LA(1); dfa::DFAState *s = ds0; // s is current/from DFA state while (true) { // while more work @@ -204,12 +204,12 @@ int LexerATNSimulator::execATN(CharStream *input, dfa::DFAState *ds0) { return failOrAccept(input, s->configs.get(), t); } -dfa::DFAState *LexerATNSimulator::getExistingTargetState(dfa::DFAState *s, ssize_t t) { - if (s->edges.size() == 0 || t < MIN_DFA_EDGE || t > MAX_DFA_EDGE) { +dfa::DFAState *LexerATNSimulator::getExistingTargetState(dfa::DFAState *s, size_t t) { + if (s->edges.empty()|| /*t < MIN_DFA_EDGE ||*/ t > MAX_DFA_EDGE) { // MIN_DFA_EDGE is 0, hence code gives a warning, if left in. return nullptr; } - dfa::DFAState *target = s->edges[(size_t)(t - MIN_DFA_EDGE)]; + dfa::DFAState *target = s->edges[t - MIN_DFA_EDGE]; #if DEBUG_ATN == 1 if (target != nullptr) { std::cout << std::string("reuse state ") << s->stateNumber << std::string(" edge to ") << target->stateNumber << std::endl; @@ -219,7 +219,7 @@ dfa::DFAState *LexerATNSimulator::getExistingTargetState(dfa::DFAState *s, ssize return target; } -dfa::DFAState *LexerATNSimulator::computeTargetState(CharStream *input, dfa::DFAState *s, ssize_t t) { +dfa::DFAState *LexerATNSimulator::computeTargetState(CharStream *input, dfa::DFAState *s, size_t t) { OrderedATNConfigSet *reach = new OrderedATNConfigSet(); /* mem-check: deleted on error or managed by new DFA state. */ // if we don't find an existing DFA state @@ -242,22 +242,22 @@ dfa::DFAState *LexerATNSimulator::computeTargetState(CharStream *input, dfa::DFA return addDFAEdge(s, t, reach); } -int LexerATNSimulator::failOrAccept(CharStream *input, ATNConfigSet *reach, ssize_t t) { +size_t LexerATNSimulator::failOrAccept(CharStream *input, ATNConfigSet *reach, size_t t) { if (_prevAccept.dfaState != nullptr) { Ref lexerActionExecutor = _prevAccept.dfaState->lexerActionExecutor; - accept(input, lexerActionExecutor, _startIndex, (size_t)_prevAccept.index, _prevAccept.line, (size_t)_prevAccept.charPos); + accept(input, lexerActionExecutor, _startIndex, _prevAccept.index, _prevAccept.line, _prevAccept.charPos); return _prevAccept.dfaState->prediction; } else { // if no accept and EOF is first char, return EOF - if (t == Token::EOF && input->index() == (size_t)_startIndex) { + if (t == Token::EOF && input->index() == _startIndex) { return Token::EOF; } - throw LexerNoViableAltException(_recog, input, (size_t)_startIndex, reach); + throw LexerNoViableAltException(_recog, input, _startIndex, reach); } } -void LexerATNSimulator::getReachableConfigSet(CharStream *input, ATNConfigSet *closure_, ATNConfigSet *reach, ssize_t t) { +void LexerATNSimulator::getReachableConfigSet(CharStream *input, ATNConfigSet *closure_, ATNConfigSet *reach, size_t t) { // this is used to skip processing for configs which have a lower priority // than a config that already reached an accept state for the same rule int skipAlt = ATN::INVALID_ALT_NUMBER; @@ -279,7 +279,7 @@ void LexerATNSimulator::getReachableConfigSet(CharStream *input, ATNConfigSet *c if (target != nullptr) { Ref lexerActionExecutor = std::static_pointer_cast(c)->getLexerActionExecutor(); if (lexerActionExecutor != nullptr) { - lexerActionExecutor = lexerActionExecutor->fixOffsetBeforeMatch((int)input->index() - _startIndex); + lexerActionExecutor = lexerActionExecutor->fixOffsetBeforeMatch((int)input->index() - (int)_startIndex); } bool treatEofAsEpsilon = t == Token::EOF; @@ -297,7 +297,7 @@ void LexerATNSimulator::getReachableConfigSet(CharStream *input, ATNConfigSet *c } } -void LexerATNSimulator::accept(CharStream *input, const Ref &lexerActionExecutor, int /*startIndex*/, +void LexerATNSimulator::accept(CharStream *input, const Ref &lexerActionExecutor, size_t /*startIndex*/, size_t index, size_t line, size_t charPos) { #if DEBUG_ATN == 1 std::cout << "ACTION "; @@ -314,8 +314,8 @@ void LexerATNSimulator::accept(CharStream *input, const Ref } } -atn::ATNState *LexerATNSimulator::getReachableTarget(Transition *trans, ssize_t t) { - if (trans->matches((int)t, std::numeric_limits::min(), std::numeric_limits::max())) { +atn::ATNState *LexerATNSimulator::getReachableTarget(Transition *trans, size_t t) { + if (trans->matches(t, Lexer::MIN_CHAR_VALUE, Lexer::MAX_CHAR_VALUE)) { return trans->target; } @@ -343,7 +343,7 @@ bool LexerATNSimulator::closure(CharStream *input, const Ref &co if (is(config->state)) { #if DEBUG_ATN == 1 if (_recog != nullptr) { - std::cout << "closure at " << _recog->getRuleNames()[(size_t)config->state->ruleIndex] << " rule stop " << config << std::endl; + std::cout << "closure at " << _recog->getRuleNames()[config->state->ruleIndex] << " rule stop " << config << std::endl; } else { std::cout << "closure at rule stop " << config << std::endl; } @@ -363,7 +363,7 @@ bool LexerATNSimulator::closure(CharStream *input, const Ref &co for (size_t i = 0; i < config->context->size(); i++) { if (config->context->getReturnState(i) != PredictionContext::EMPTY_RETURN_STATE) { std::weak_ptr newContext = config->context->getParent(i); // "pop" return state - ATNState *returnState = atn.states[(size_t)config->context->getReturnState(i)]; + ATNState *returnState = atn.states[config->context->getReturnState(i)]; Ref c = std::make_shared(config, returnState, newContext.lock()); currentAltReachedAcceptState = closure(input, c, configs, currentAltReachedAcceptState, speculative, treatEofAsEpsilon); } @@ -479,12 +479,15 @@ Ref LexerATNSimulator::getEpsilonTarget(CharStream *input, const } break; + + default: // To silence the compiler. Other transition types are not used here. + break; } return c; } -bool LexerATNSimulator::evaluatePredicate(CharStream *input, int ruleIndex, int predIndex, bool speculative) { +bool LexerATNSimulator::evaluatePredicate(CharStream *input, size_t ruleIndex, size_t predIndex, bool speculative) { // assume true if no recognizer was provided if (_recog == nullptr) { return true; @@ -494,7 +497,7 @@ bool LexerATNSimulator::evaluatePredicate(CharStream *input, int ruleIndex, int return _recog->sempred(nullptr, ruleIndex, predIndex); } - int savedCharPositionInLine = _charPositionInLine; + size_t savedCharPositionInLine = _charPositionInLine; size_t savedLine = _line; size_t index = input->index(); ssize_t marker = input->mark(); @@ -511,13 +514,13 @@ bool LexerATNSimulator::evaluatePredicate(CharStream *input, int ruleIndex, int } void LexerATNSimulator::captureSimState(CharStream *input, dfa::DFAState *dfaState) { - _prevAccept.index = (int)input->index(); + _prevAccept.index = input->index(); _prevAccept.line = _line; _prevAccept.charPos = _charPositionInLine; _prevAccept.dfaState = dfaState; } -dfa::DFAState *LexerATNSimulator::addDFAEdge(dfa::DFAState *from, ssize_t t, ATNConfigSet *q) { +dfa::DFAState *LexerATNSimulator::addDFAEdge(dfa::DFAState *from, size_t t, ATNConfigSet *q) { /* leading to this call, ATNConfigSet.hasSemanticContext is used as a * marker indicating dynamic predicate evaluation makes this edge * dependent on the specific input sequence, so the static edge in the @@ -542,8 +545,8 @@ dfa::DFAState *LexerATNSimulator::addDFAEdge(dfa::DFAState *from, ssize_t t, ATN return to; } -void LexerATNSimulator::addDFAEdge(dfa::DFAState *p, ssize_t t, dfa::DFAState *q) { - if (t < MIN_DFA_EDGE || t > MAX_DFA_EDGE) { +void LexerATNSimulator::addDFAEdge(dfa::DFAState *p, size_t t, dfa::DFAState *q) { + if (/*t < MIN_DFA_EDGE ||*/ t > MAX_DFA_EDGE) { // MIN_DFA_EDGE is 0 // Only track edges within the DFA bounds return; } @@ -599,7 +602,7 @@ dfa::DFA& LexerATNSimulator::getDFA(size_t mode) { std::string LexerATNSimulator::getText(CharStream *input) { // index is first lookahead char, don't include. - return input->getText(misc::Interval((int)_startIndex, (int)input->index() - 1)); + return input->getText(misc::Interval(_startIndex, input->index() - 1)); } size_t LexerATNSimulator::getLine() const { @@ -610,16 +613,16 @@ void LexerATNSimulator::setLine(size_t line) { _line = line; } -int LexerATNSimulator::getCharPositionInLine() { +size_t LexerATNSimulator::getCharPositionInLine() { return _charPositionInLine; } -void LexerATNSimulator::setCharPositionInLine(int charPositionInLine) { +void LexerATNSimulator::setCharPositionInLine(size_t charPositionInLine) { _charPositionInLine = charPositionInLine; } void LexerATNSimulator::consume(CharStream *input) { - ssize_t curChar = input->LA(1); + size_t curChar = input->LA(1); if (curChar == '\n') { _line++; _charPositionInLine = 0; @@ -629,16 +632,15 @@ void LexerATNSimulator::consume(CharStream *input) { input->consume(); } -std::string LexerATNSimulator::getTokenName(int t) { - if (t == -1) { +std::string LexerATNSimulator::getTokenName(size_t t) { + if (t == Token::EOF) { return "EOF"; } - //if ( atn.g!=null ) return atn.g.getTokenDisplayName(t); return std::string("'") + static_cast(t) + std::string("'"); } void LexerATNSimulator::InitializeInstanceFields() { - _startIndex = -1; + _startIndex = 0; _line = 1; _charPositionInLine = 0; _mode = antlr4::Lexer::DEFAULT_MODE; diff --git a/runtime/Cpp/runtime/src/atn/LexerATNSimulator.h b/runtime/Cpp/runtime/src/atn/LexerATNSimulator.h index c5d5b0aec..5c299ade0 100755 --- a/runtime/Cpp/runtime/src/atn/LexerATNSimulator.h +++ b/runtime/Cpp/runtime/src/atn/LexerATNSimulator.h @@ -46,9 +46,9 @@ namespace atn { virtual ~SimState() {}; protected: - int index; + size_t index; size_t line; - int charPos; + size_t charPos; dfa::DFAState *dfaState; virtual void reset(); friend class LexerATNSimulator; @@ -64,9 +64,10 @@ namespace atn { public: - static const int MIN_DFA_EDGE = 0; - static const int MAX_DFA_EDGE = 127; // forces unicode to stay in ATN + static const size_t MIN_DFA_EDGE = 0; + static const size_t MAX_DFA_EDGE = 127; // forces unicode to stay in ATN + protected: /// /// When we hit an accept state in either the DFA or the ATN, we /// have to notify the character stream to start buffering characters @@ -83,24 +84,19 @@ namespace atn { /// then the ATN does the accept and the DFA simulator that invoked it /// can simply return the predicted token type. /// - protected: Lexer *const _recog; - /// /// The current token's starting index into the character stream. /// Shared across DFA to ATN simulation in case the ATN fails and the /// DFA did not have a previous accept state. In this case, we use the /// ATN-generated exception object. - /// - int _startIndex; + size_t _startIndex; - /// - /// line number 1..n within the input + /// line number 1..n within the input. size_t _line; - /// - /// The index of the character relative to the beginning of the line 0..n-1 - int _charPositionInLine; + /// The index of the character relative to the beginning of the line 0..n-1. + size_t _charPositionInLine; public: std::vector &_decisionToDFA; @@ -120,14 +116,14 @@ namespace atn { PredictionContextCache &sharedContextCache); virtual void copyState(LexerATNSimulator *simulator); - virtual int match(CharStream *input, size_t mode); + virtual size_t match(CharStream *input, size_t mode); virtual void reset() override; virtual void clearDFA() override; protected: - virtual int matchATN(CharStream *input); - virtual int execATN(CharStream *input, dfa::DFAState *ds0); + virtual size_t matchATN(CharStream *input); + virtual size_t execATN(CharStream *input, dfa::DFAState *ds0); /// /// Get an existing target state for an edge in the DFA. If the target state @@ -139,7 +135,7 @@ namespace atn { /// The existing target DFA state for the given input symbol /// {@code t}, or {@code null} if the target state for this edge is not /// already cached - virtual dfa::DFAState *getExistingTargetState(dfa::DFAState *s, ssize_t t); + virtual dfa::DFAState *getExistingTargetState(dfa::DFAState *s, size_t t); /// /// Compute a target state for an edge in the DFA, and attempt to add the @@ -152,9 +148,9 @@ 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(CharStream *input, dfa::DFAState *s, ssize_t t); + virtual dfa::DFAState *computeTargetState(CharStream *input, dfa::DFAState *s, size_t t); - virtual int failOrAccept(CharStream *input, ATNConfigSet *reach, ssize_t t); + virtual size_t failOrAccept(CharStream *input, ATNConfigSet *reach, size_t t); /// /// Given a starting configuration set, figure out all ATN configurations @@ -162,12 +158,12 @@ namespace atn { /// parameter. /// void getReachableConfigSet(CharStream *input, ATNConfigSet *closure_, // closure_ as we have a closure() already - ATNConfigSet *reach, ssize_t t); + ATNConfigSet *reach, size_t t); - virtual void accept(CharStream *input, const Ref &lexerActionExecutor, int startIndex, size_t index, + virtual void accept(CharStream *input, const Ref &lexerActionExecutor, size_t startIndex, size_t index, size_t line, size_t charPos); - virtual ATNState *getReachableTarget(Transition *trans, ssize_t t); + virtual ATNState *getReachableTarget(Transition *trans, size_t t); virtual std::unique_ptr computeStartState(CharStream *input, ATNState *p); @@ -207,11 +203,11 @@ namespace atn { /// /// {@code true} if the specified predicate evaluates to /// {@code true}. - virtual bool evaluatePredicate(CharStream *input, int ruleIndex, int predIndex, bool speculative); + virtual bool evaluatePredicate(CharStream *input, size_t ruleIndex, size_t predIndex, bool speculative); virtual void captureSimState(CharStream *input, dfa::DFAState *dfaState); - virtual dfa::DFAState* addDFAEdge(dfa::DFAState *from, ssize_t t, ATNConfigSet *q); - virtual void addDFAEdge(dfa::DFAState *p, ssize_t t, dfa::DFAState *q); + virtual dfa::DFAState* addDFAEdge(dfa::DFAState *from, size_t t, ATNConfigSet *q); + virtual void addDFAEdge(dfa::DFAState *p, size_t t, dfa::DFAState *q); /// /// Add a new DFA state if there isn't one with this set of @@ -228,10 +224,10 @@ namespace atn { virtual std::string getText(CharStream *input); virtual size_t getLine() const; virtual void setLine(size_t line); - virtual int getCharPositionInLine(); - virtual void setCharPositionInLine(int charPositionInLine); + virtual size_t getCharPositionInLine(); + virtual void setCharPositionInLine(size_t charPositionInLine); virtual void consume(CharStream *input); - virtual std::string getTokenName(int t); + virtual std::string getTokenName(size_t t); private: void InitializeInstanceFields(); diff --git a/runtime/Cpp/runtime/src/atn/LexerActionExecutor.cpp b/runtime/Cpp/runtime/src/atn/LexerActionExecutor.cpp index 14f63bb79..126de21cc 100755 --- a/runtime/Cpp/runtime/src/atn/LexerActionExecutor.cpp +++ b/runtime/Cpp/runtime/src/atn/LexerActionExecutor.cpp @@ -79,7 +79,7 @@ std::vector> LexerActionExecutor::getLexerActions() const { return _lexerActions; } -void LexerActionExecutor::execute(Lexer *lexer, CharStream *input, int startIndex) { +void LexerActionExecutor::execute(Lexer *lexer, CharStream *input, size_t startIndex) { bool requiresSeek = false; size_t stopIndex = input->index(); @@ -93,7 +93,7 @@ void LexerActionExecutor::execute(Lexer *lexer, CharStream *input, int startInde int offset = (std::static_pointer_cast(lexerAction))->getOffset(); input->seek(startIndex + offset); lexerAction = std::static_pointer_cast(lexerAction)->getAction(); - requiresSeek = (size_t)(startIndex + offset) != stopIndex; + requiresSeek = (startIndex + offset) != stopIndex; } else if (lexerAction->isPositionDependent()) { input->seek(stopIndex); requiresSeek = false; diff --git a/runtime/Cpp/runtime/src/atn/LexerActionExecutor.h b/runtime/Cpp/runtime/src/atn/LexerActionExecutor.h index f707e57b2..026e56297 100755 --- a/runtime/Cpp/runtime/src/atn/LexerActionExecutor.h +++ b/runtime/Cpp/runtime/src/atn/LexerActionExecutor.h @@ -121,7 +121,7 @@ namespace atn { /// The token start index. This value may be passed to /// to set the {@code input} position to the beginning /// of the token. - virtual void execute(Lexer *lexer, CharStream *input, int startIndex); + virtual void execute(Lexer *lexer, CharStream *input, size_t startIndex); virtual size_t hashCode() const; virtual bool operator == (const LexerActionExecutor &obj) const; diff --git a/runtime/Cpp/runtime/src/atn/NotSetTransition.cpp b/runtime/Cpp/runtime/src/atn/NotSetTransition.cpp index 490975a1e..ba87d81f5 100755 --- a/runtime/Cpp/runtime/src/atn/NotSetTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/NotSetTransition.cpp @@ -39,11 +39,11 @@ using namespace antlr4::atn; NotSetTransition::NotSetTransition(ATNState *target, const misc::IntervalSet &set) : SetTransition(target, set) { } -int NotSetTransition::getSerializationType() const { +Transition::SerializationType NotSetTransition::getSerializationType() const { return NOT_SET; } -bool NotSetTransition::matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const { +bool NotSetTransition::matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const { return symbol >= minVocabSymbol && symbol <= maxVocabSymbol && !SetTransition::matches(symbol, minVocabSymbol, maxVocabSymbol); } diff --git a/runtime/Cpp/runtime/src/atn/NotSetTransition.h b/runtime/Cpp/runtime/src/atn/NotSetTransition.h index 765e7043e..07e6353af 100755 --- a/runtime/Cpp/runtime/src/atn/NotSetTransition.h +++ b/runtime/Cpp/runtime/src/atn/NotSetTransition.h @@ -40,9 +40,9 @@ namespace atn { public: NotSetTransition(ATNState *target, const misc::IntervalSet &set); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp index fe9d178e1..6ee16cbec 100755 --- a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp @@ -87,16 +87,17 @@ void ParserATNSimulator::clearDFA() { } } -int ParserATNSimulator::adaptivePredict(TokenStream *input, int decision, Ref const& outerContext) { +size_t ParserATNSimulator::adaptivePredict(TokenStream *input, size_t decision, Ref const& outerContext) { + #if DEBUG_ATN == 1 || DEBUG_LIST_ATN_DECISIONS == 1 std::cout << "adaptivePredict decision " << decision << " exec LA(1)==" << getLookaheadName(input) << " line " << input->LT(1)->getLine() << ":" << input->LT(1)->getCharPositionInLine() << std::endl; #endif _input = input; - _startIndex = (int)input->index(); + _startIndex = input->index(); _outerContext = outerContext; - dfa::DFA &dfa = decisionToDFA[(size_t)decision]; + dfa::DFA &dfa = decisionToDFA[decision]; _dfa = &dfa; ssize_t m = input->mark(); @@ -151,13 +152,14 @@ int ParserATNSimulator::adaptivePredict(TokenStream *input, int decision, Ref outerContext) { +size_t ParserATNSimulator::execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex, + Ref outerContext) { + #if DEBUG_ATN == 1 || DEBUG_LIST_ATN_DECISIONS == 1 std::cout << "execATN decision " << dfa.decision << " exec LA(1)==" << getLookaheadName(input) << " line " << input->LT(1)->getLine() << ":" << input->LT(1)->getCharPositionInLine() << std::endl; @@ -169,7 +171,7 @@ int ParserATNSimulator::execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *i std::cout << "s0 = " << s0 << std::endl; #endif - ssize_t t = input->LA(1); + size_t t = input->LA(1); while (true) { // while more work dfa::DFAState *D = getExistingTargetState(previousD, t); @@ -189,7 +191,7 @@ int ParserATNSimulator::execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *i // will get error no matter what. NoViableAltException e = noViableAlt(input, outerContext, previousD->configs.get(), startIndex); input->seek(startIndex); - int alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previousD->configs.get(), outerContext); + size_t alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previousD->configs.get(), outerContext); if (alt != ATN::INVALID_ALT_NUMBER) { return alt; } @@ -206,8 +208,8 @@ int ParserATNSimulator::execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *i #endif size_t conflictIndex = input->index(); - if (conflictIndex != (size_t)startIndex) { - input->seek((size_t)startIndex); + if (conflictIndex != startIndex) { + input->seek(startIndex); } conflictingAlts = evalSemanticContext(D->predicates, outerContext, true); @@ -233,7 +235,7 @@ int ParserATNSimulator::execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *i bool fullCtx = true; Ref s0_closure = computeStartState(dfa.atnStartState, outerContext, fullCtx); reportAttemptingFullContext(dfa, conflictingAlts, D->configs.get(), startIndex, input->index()); - int alt = execATNWithFullContext(dfa, D, s0_closure.get(), input, startIndex, outerContext); + size_t alt = execATNWithFullContext(dfa, D, s0_closure.get(), input, startIndex, outerContext); return alt; } @@ -269,7 +271,7 @@ int ParserATNSimulator::execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *i } } -dfa::DFAState *ParserATNSimulator::getExistingTargetState(dfa::DFAState *previousD, ssize_t t) { +dfa::DFAState *ParserATNSimulator::getExistingTargetState(dfa::DFAState *previousD, size_t t) { auto iterator = previousD->edges.find(t); if (iterator == previousD->edges.end()) { return nullptr; @@ -278,7 +280,7 @@ dfa::DFAState *ParserATNSimulator::getExistingTargetState(dfa::DFAState *previou return iterator->second; } -dfa::DFAState *ParserATNSimulator::computeTargetState(dfa::DFA &dfa, dfa::DFAState *previousD, ssize_t t) { +dfa::DFAState *ParserATNSimulator::computeTargetState(dfa::DFA &dfa, dfa::DFAState *previousD, size_t t) { std::unique_ptr reach = computeReachSet(previousD->configs.get(), t, false); if (reach == nullptr) { addDFAEdge(dfa, previousD, t, ERROR.get()); @@ -338,7 +340,7 @@ void ParserATNSimulator::predicateDFAState(dfa::DFAState *dfaState, DecisionStat } } -int ParserATNSimulator::execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, ATNConfigSet *s0, +size_t ParserATNSimulator::execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, ATNConfigSet *s0, TokenStream *input, size_t startIndex, Ref const& outerContext) { bool fullCtx = true; @@ -347,8 +349,8 @@ int ParserATNSimulator::execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, std::unique_ptr reach; ATNConfigSet *previous = s0; input->seek(startIndex); - ssize_t t = input->LA(1); - int predictedAlt; + size_t t = input->LA(1); + size_t predictedAlt; while (true) { reach = computeReachSet(previous, t, fullCtx); @@ -364,7 +366,7 @@ int ParserATNSimulator::execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, // will get error no matter what. NoViableAltException e = noViableAlt(input, outerContext, previous, startIndex); input->seek(startIndex); - int alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext); + size_t alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext); if (alt != ATN::INVALID_ALT_NUMBER) { return alt; } @@ -441,12 +443,12 @@ int ParserATNSimulator::execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, the fact that we should predict alternative 1. We just can't say for sure that there is an ambiguity without looking further. */ - reportAmbiguity(dfa, D, (size_t)startIndex, input->index(), foundExactAmbig, reach->getAlts(), reach.get()); + reportAmbiguity(dfa, D, startIndex, input->index(), foundExactAmbig, reach->getAlts(), reach.get()); return predictedAlt; } -std::unique_ptr ParserATNSimulator::computeReachSet(ATNConfigSet *closure_, ssize_t t, bool fullCtx) { +std::unique_ptr ParserATNSimulator::computeReachSet(ATNConfigSet *closure_, size_t t, bool fullCtx) { std::unique_ptr intermediate(new ATNConfigSet(fullCtx)); @@ -585,7 +587,7 @@ ATNConfigSet* ParserATNSimulator::removeAllConfigsNotInRuleStopState(ATNConfigSe if (lookToEndOfRule && config->state->onlyHasEpsilonTransitions()) { misc::IntervalSet nextTokens = atn.nextTokens(config->state); if (nextTokens.contains(Token::EPSILON)) { - ATNState *endOfRuleState = atn.ruleToStopState[(size_t)config->state->ruleIndex]; + ATNState *endOfRuleState = atn.ruleToStopState[config->state->ruleIndex]; result->add(std::make_shared(config, endOfRuleState), &mergeCache); } } @@ -610,7 +612,7 @@ std::unique_ptr ParserATNSimulator::computeStartState(ATNState *p, } std::unique_ptr ParserATNSimulator::applyPrecedenceFilter(ATNConfigSet *configs) { - std::map> statesFromAlt1; + std::map> statesFromAlt1; std::unique_ptr configSet(new ATNConfigSet(configs->fullCtx)); for (Ref &config : configs->configs) { // handle alt 1 first @@ -657,8 +659,8 @@ std::unique_ptr ParserATNSimulator::applyPrecedenceFilter(ATNConfi return configSet; } -atn::ATNState* ParserATNSimulator::getReachableTarget(Transition *trans, int ttype) { - if (trans->matches(ttype, 0, (int)atn.maxTokenType)) { +atn::ATNState* ParserATNSimulator::getReachableTarget(Transition *trans, size_t ttype) { + if (trans->matches(ttype, 0, atn.maxTokenType)) { return trans->target; } @@ -733,13 +735,13 @@ std::vector ParserATNSimulator::getPredicatePre return pairs; } -int ParserATNSimulator::getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(ATNConfigSet *configs, +size_t ParserATNSimulator::getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(ATNConfigSet *configs, Ref const& outerContext) { std::pair sets = splitAccordingToSemanticValidity(configs, outerContext); std::unique_ptr semValidConfigs(sets.first); std::unique_ptr semInvalidConfigs(sets.second); - int alt = getAltThatFinishedDecisionEntryRule(semValidConfigs.get()); + size_t alt = getAltThatFinishedDecisionEntryRule(semValidConfigs.get()); if (alt != ATN::INVALID_ALT_NUMBER) { // semantically/syntactically viable path exists return alt; } @@ -753,7 +755,7 @@ int ParserATNSimulator::getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule( return ATN::INVALID_ALT_NUMBER; } -int ParserATNSimulator::getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs) { +size_t ParserATNSimulator::getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs) { misc::IntervalSet alts; for (auto &c : configs->configs) { if (c->getOuterContextDepth() > 0 || (is(c->state) && c->context->hasEmptyPath())) { @@ -852,13 +854,13 @@ void ParserATNSimulator::closureCheckingStopState(Ref const& config, } else { // we have no context info, just chase follow links (if greedy) #if DEBUG_ATN == 1 - std::cout << "FALLING off rule " << getRuleName((size_t)config->state->ruleIndex) << std::endl; + std::cout << "FALLING off rule " << getRuleName(config->state->ruleIndex) << std::endl; #endif closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth, treatEofAsEpsilon); } continue; } - ATNState *returnState = atn.states[(size_t)config->context->getReturnState(i)]; + ATNState *returnState = atn.states[config->context->getReturnState(i)]; std::weak_ptr newContext = config->context->getParent(i); // "pop" return state Ref c = std::make_shared(returnState, config->alt, newContext.lock(), config->semanticContext); // While we have context to pop back from, we may have @@ -927,7 +929,7 @@ void ParserATNSimulator::closure_(Ref const& config, ATNConfigSet *co closureBusy.insert(c); if (_dfa != nullptr && _dfa->isPrecedenceDfa()) { - int outermostPrecedenceReturn = dynamic_cast(t)->outermostPrecedenceReturn(); + size_t outermostPrecedenceReturn = dynamic_cast(t)->outermostPrecedenceReturn(); if (outermostPrecedenceReturn == _dfa->atnStartState->ruleIndex) { c->setPrecedenceFilterSuppressed(true); } @@ -1024,7 +1026,7 @@ Ref ParserATNSimulator::precedenceTransition(Ref const& co // the config sets. It also obviates the need to test predicates // later during conflict resolution. size_t currentPosition = _input->index(); - _input->seek((size_t)_startIndex); + _input->seek(_startIndex); bool predSucceeds = evalSemanticContext(pt->getPredicate(), _outerContext, config->alt, fullCtx); _input->seek(currentPosition); if (predSucceeds) { @@ -1063,7 +1065,7 @@ Ref ParserATNSimulator::predTransition(Ref const& config, // the config sets. It also obviates the need to test predicates // later during conflict resolution. size_t currentPosition = _input->index(); - _input->seek((size_t)_startIndex); + _input->seek(_startIndex); bool predSucceeds = evalSemanticContext(pt->getPredicate(), _outerContext, config->alt, fullCtx); _input->seek(currentPosition); if (predSucceeds) { @@ -1086,7 +1088,7 @@ Ref ParserATNSimulator::predTransition(Ref const& config, Ref ParserATNSimulator::ruleTransition(Ref const& config, RuleTransition *t) { #if DEBUG_DFA == 1 - std::cout << "CALL rule " << getRuleName((size_t)t->target->ruleIndex) << ", ctx=" << config->context << std::endl; + std::cout << "CALL rule " << getRuleName(t->target->ruleIndex) << ", ctx=" << config->context << std::endl; #endif atn::ATNState *returnState = t->followState; @@ -1109,7 +1111,7 @@ BitSet ParserATNSimulator::getConflictingAltsOrUniqueAlt(ATNConfigSet *configs) return conflictingAlts; } -std::string ParserATNSimulator::getTokenName(ssize_t t) { +std::string ParserATNSimulator::getTokenName(size_t t) { if (t == Token::EOF) { return "EOF"; } @@ -1236,10 +1238,10 @@ void ParserATNSimulator::reportAttemptingFullContext(dfa::DFA &dfa, const antlrc } } -void ParserATNSimulator::reportContextSensitivity(dfa::DFA &dfa, int prediction, ATNConfigSet *configs, +void ParserATNSimulator::reportContextSensitivity(dfa::DFA &dfa, size_t prediction, ATNConfigSet *configs, size_t startIndex, size_t stopIndex) { #if DEBUG_DFA == 1 || RETRY_DEBUG == 1 - misc::Interval interval = misc::Interval((int)startIndex, (int)stopIndex); + misc::Interval interval = misc::Interval(startIndex, stopIndex); std::cout << "reportContextSensitivity decision=" << dfa.decision << ":" << configs << ", input=" << parser->getTokenStream()->getText(interval) << std::endl; #endif diff --git a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h index 6cb881647..d36373cb4 100755 --- a/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h +++ b/runtime/Cpp/runtime/src/atn/ParserATNSimulator.h @@ -297,7 +297,7 @@ namespace atn { // LAME globals to avoid parameters!!!!! I need these down deep in predTransition TokenStream *_input; - int _startIndex; + size_t _startIndex; Ref _outerContext; dfa::DFA *_dfa; // Reference into the decisionToDFA vector. @@ -311,7 +311,7 @@ namespace atn { virtual void reset() override; virtual void clearDFA() override; - virtual int adaptivePredict(TokenStream *input, int decision, Ref const& outerContext); + virtual size_t adaptivePredict(TokenStream *input, size_t decision, Ref const& outerContext); /// /// Performs ATN simulation to compute a predicted alternative based @@ -345,8 +345,8 @@ namespace atn { /// conflict + preds /// protected: - virtual int execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex, - Ref outerContext); + virtual size_t execATN(dfa::DFA &dfa, dfa::DFAState *s0, TokenStream *input, size_t startIndex, + Ref outerContext); /// /// Get an existing target state for an edge in the DFA. If the target state @@ -358,7 +358,7 @@ namespace atn { /// The existing target DFA state for the given input symbol /// {@code t}, or {@code null} if the target state for this edge is not /// already cached - virtual dfa::DFAState* getExistingTargetState(dfa::DFAState *previousD, ssize_t t); + virtual dfa::DFAState* getExistingTargetState(dfa::DFAState *previousD, size_t t); /// /// Compute a target state for an edge in the DFA, and attempt to add the @@ -371,15 +371,15 @@ 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, size_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, ATNConfigSet *s0, + virtual size_t execATNWithFullContext(dfa::DFA &dfa, dfa::DFAState *D, ATNConfigSet *s0, TokenStream *input, size_t startIndex, Ref const& outerContext); // how far we got before failing over - virtual std::unique_ptr computeReachSet(ATNConfigSet *closure, ssize_t t, bool fullCtx); + virtual std::unique_ptr computeReachSet(ATNConfigSet *closure, size_t t, bool fullCtx); /// /// Return a configuration set containing only the configurations from @@ -573,7 +573,7 @@ namespace atn { */ std::unique_ptr applyPrecedenceFilter(ATNConfigSet *configs); - virtual ATNState *getReachableTarget(Transition *trans, int ttype); + virtual ATNState *getReachableTarget(Transition *trans, size_t ttype); virtual std::vector> getPredsForAmbigAlts(const antlrcpp::BitSet &ambigAlts, ATNConfigSet *configs, size_t nalts); @@ -627,10 +627,10 @@ namespace atn { * {@link ATN#INVALID_ALT_NUMBER} if a suitable alternative was not * identified and {@link #adaptivePredict} should report an error instead. */ - int getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(ATNConfigSet *configs, - Ref const& outerContext); + size_t getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(ATNConfigSet *configs, + Ref const& outerContext); - virtual int getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs); + virtual size_t getAltThatFinishedDecisionEntryRule(ATNConfigSet *configs); /** Walk the list of configurations and split them according to * those that have preds evaluating to true/false. If no pred, assume @@ -773,7 +773,7 @@ namespace atn { virtual antlrcpp::BitSet getConflictingAltsOrUniqueAlt(ATNConfigSet *configs); public: - virtual std::string getTokenName(ssize_t t); + virtual std::string getTokenName(size_t t); virtual std::string getLookaheadName(TokenStream *input); @@ -830,7 +830,7 @@ namespace atn { virtual void reportAttemptingFullContext(dfa::DFA &dfa, const antlrcpp::BitSet &conflictingAlts, ATNConfigSet *configs, size_t startIndex, size_t stopIndex); - virtual void reportContextSensitivity(dfa::DFA &dfa, int prediction, ATNConfigSet *configs, + virtual void reportContextSensitivity(dfa::DFA &dfa, size_t prediction, ATNConfigSet *configs, size_t startIndex, size_t stopIndex); /// If context sensitive parsing, we know it's ambiguity not conflict. diff --git a/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.cpp b/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.cpp index d4091603f..a8a13ac7f 100755 --- a/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.cpp @@ -37,7 +37,7 @@ PrecedencePredicateTransition::PrecedencePredicateTransition(ATNState *target, i : AbstractPredicateTransition(target), precedence(precedence) { } -int PrecedencePredicateTransition::getSerializationType() const { +Transition::SerializationType PrecedencePredicateTransition::getSerializationType() const { return PRECEDENCE; } @@ -45,7 +45,7 @@ bool PrecedencePredicateTransition::isEpsilon() const { return true; } -bool PrecedencePredicateTransition::matches(ssize_t /*symbol*/, ssize_t /*minVocabSymbol*/, ssize_t /*maxVocabSymbol*/) const { +bool PrecedencePredicateTransition::matches(size_t /*symbol*/, size_t /*minVocabSymbol*/, size_t /*maxVocabSymbol*/) const { return false; } diff --git a/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.h b/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.h index 8c414699f..80e010c9a 100755 --- a/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.h +++ b/runtime/Cpp/runtime/src/atn/PrecedencePredicateTransition.h @@ -43,9 +43,9 @@ namespace atn { PrecedencePredicateTransition(ATNState *target, int precedence); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; virtual bool isEpsilon() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; Ref getPredicate() const; virtual std::string toString() const override; diff --git a/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.cpp b/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.cpp index 1d1f399fe..36cbcfb08 100755 --- a/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.cpp +++ b/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.cpp @@ -36,7 +36,7 @@ using namespace antlr4; using namespace antlr4::atn; -PredicateEvalInfo::PredicateEvalInfo(int decision, TokenStream *input, int startIndex, int stopIndex, +PredicateEvalInfo::PredicateEvalInfo(size_t decision, TokenStream *input, size_t startIndex, size_t stopIndex, Ref const& semctx, bool evalResult, int predictedAlt, bool fullCtx) : DecisionEventInfo(decision, nullptr, input, startIndex, stopIndex, fullCtx), semctx(semctx), predictedAlt(predictedAlt), evalResult(evalResult) { diff --git a/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.h b/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.h index e01cd66a4..4efa0b268 100755 --- a/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.h +++ b/runtime/Cpp/runtime/src/atn/PredicateEvalInfo.h @@ -80,8 +80,8 @@ namespace atn { /// /// /// - PredicateEvalInfo(int decision, TokenStream *input, int startIndex, int stopIndex, Ref const& semctx, - bool evalResult, int predictedAlt, bool fullCtx); + PredicateEvalInfo(size_t decision, TokenStream *input, size_t startIndex, size_t stopIndex, + Ref const& semctx, bool evalResult, int predictedAlt, bool fullCtx); }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/PredicateTransition.cpp b/runtime/Cpp/runtime/src/atn/PredicateTransition.cpp index b38625add..5e118bd7b 100755 --- a/runtime/Cpp/runtime/src/atn/PredicateTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/PredicateTransition.cpp @@ -33,10 +33,10 @@ using namespace antlr4::atn; -PredicateTransition::PredicateTransition(ATNState *target, int ruleIndex, int predIndex, bool isCtxDependent) : AbstractPredicateTransition(target), ruleIndex(ruleIndex), predIndex(predIndex), isCtxDependent(isCtxDependent) { +PredicateTransition::PredicateTransition(ATNState *target, size_t ruleIndex, size_t predIndex, bool isCtxDependent) : AbstractPredicateTransition(target), ruleIndex(ruleIndex), predIndex(predIndex), isCtxDependent(isCtxDependent) { } -int PredicateTransition::getSerializationType() const { +Transition::SerializationType PredicateTransition::getSerializationType() const { return PREDICATE; } @@ -44,7 +44,7 @@ bool PredicateTransition::isEpsilon() const { return true; } -bool PredicateTransition::matches(ssize_t /*symbol*/, ssize_t /*minVocabSymbol*/, ssize_t /*maxVocabSymbol*/) const { +bool PredicateTransition::matches(size_t /*symbol*/, size_t /*minVocabSymbol*/, size_t /*maxVocabSymbol*/) const { return false; } diff --git a/runtime/Cpp/runtime/src/atn/PredicateTransition.h b/runtime/Cpp/runtime/src/atn/PredicateTransition.h index 73560c640..6de664220 100755 --- a/runtime/Cpp/runtime/src/atn/PredicateTransition.h +++ b/runtime/Cpp/runtime/src/atn/PredicateTransition.h @@ -44,16 +44,16 @@ namespace atn { /// multiple ATN configurations into a single DFA state. class ANTLR4CPP_PUBLIC PredicateTransition final : public AbstractPredicateTransition { public: - const int ruleIndex; - const int predIndex; + const size_t ruleIndex; + const size_t predIndex; const bool isCtxDependent; // e.g., $i ref in pred - PredicateTransition(ATNState *target, int ruleIndex, int predIndex, bool isCtxDependent); + PredicateTransition(ATNState *target, size_t ruleIndex, size_t predIndex, bool isCtxDependent); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; virtual bool isEpsilon() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; Ref getPredicate() const; diff --git a/runtime/Cpp/runtime/src/atn/PredictionContext.cpp b/runtime/Cpp/runtime/src/atn/PredictionContext.cpp index c5507675f..1ed1f07c2 100755 --- a/runtime/Cpp/runtime/src/atn/PredictionContext.cpp +++ b/runtime/Cpp/runtime/src/atn/PredictionContext.cpp @@ -46,7 +46,7 @@ using namespace antlr4::atn; using namespace antlrcpp; -int PredictionContext::globalNodeCount = 0; +size_t PredictionContext::globalNodeCount = 0; const Ref PredictionContext::EMPTY = std::make_shared(); PredictionContext::PredictionContext(size_t cachedHashCode) : id(globalNodeCount++), cachedHashCode(cachedHashCode) { @@ -69,7 +69,7 @@ Ref PredictionContext::fromRuleContext(const ATN &atn, const // If we have a parent, convert it to a PredictionContext graph Ref parent = PredictionContext::fromRuleContext(atn, std::dynamic_pointer_cast(outerContext->parent.lock())); - ATNState *state = atn.states.at((size_t)outerContext->invokingState); + ATNState *state = atn.states.at(outerContext->invokingState); RuleTransition *transition = (RuleTransition *)state->transition(0); return SingletonPredictionContext::create(parent, transition->followState->stateNumber); } @@ -96,16 +96,16 @@ size_t PredictionContext::calculateEmptyHashCode() { return hash; } -size_t PredictionContext::calculateHashCode(std::weak_ptr parent, int returnState) { +size_t PredictionContext::calculateHashCode(std::weak_ptr parent, size_t returnState) { size_t hash = MurmurHash::initialize(INITIAL_HASH); hash = MurmurHash::update(hash, parent.lock()); - hash = MurmurHash::update(hash, (size_t)returnState); + hash = MurmurHash::update(hash, returnState); hash = MurmurHash::finish(hash, 2); return hash; } size_t PredictionContext::calculateHashCode(const std::vector> &parents, - const std::vector &returnStates) { + const std::vector &returnStates) { size_t hash = MurmurHash::initialize(INITIAL_HASH); for (auto parent : parents) { @@ -116,7 +116,7 @@ size_t PredictionContext::calculateHashCode(const std::vector PredictionContext::mergeSingletons(const Refparent; } if (!singleParent.expired()) { // parents are same, sort payloads and use same parent - std::vector payloads = { a->returnState, b->returnState }; + std::vector payloads = { a->returnState, b->returnState }; if (a->returnState > b->returnState) { payloads[0] = b->returnState; payloads[1] = a->returnState; @@ -234,11 +234,11 @@ Ref PredictionContext::mergeSingletons(const Ref a_; if (a->returnState > b->returnState) { // sort by payload - std::vector payloads = { b->returnState, a->returnState }; + std::vector payloads = { b->returnState, a->returnState }; std::vector> parents = { b->parent, a->parent }; a_ = std::make_shared(parents, payloads); } else { - std::vector payloads = {a->returnState, b->returnState}; + std::vector payloads = {a->returnState, b->returnState}; std::vector> parents = { a->parent, b->parent }; a_ = std::make_shared(parents, payloads); } @@ -264,13 +264,13 @@ Ref PredictionContext::mergeRoot(const Ref payloads = { b->returnState, EMPTY_RETURN_STATE }; + std::vector payloads = { b->returnState, EMPTY_RETURN_STATE }; std::vector> parents = { b->parent, EMPTY }; Ref joined = std::make_shared(parents, payloads); return joined; } if (b == EMPTY) { // x + $ = [$,x] ($ is always first if present) - std::vector payloads = { a->returnState, EMPTY_RETURN_STATE }; + std::vector payloads = { a->returnState, EMPTY_RETURN_STATE }; std::vector> parents = { a->parent, EMPTY }; Ref joined = std::make_shared(parents, payloads); return joined; @@ -298,7 +298,7 @@ Ref PredictionContext::mergeArrays(const Ref mergedReturnStates(a->returnStates.size() + b->returnStates.size()); + std::vector mergedReturnStates(a->returnStates.size() + b->returnStates.size()); std::vector> mergedParents(a->returnStates.size() + b->returnStates.size()); // walk and merge to yield mergedParents, mergedReturnStates @@ -307,7 +307,7 @@ Ref PredictionContext::mergeArrays(const Ref b_parent = b->parents[j]; if (a->returnStates[i] == b->returnStates[j]) { // same payload (stack tops are equal), must yield merged singleton - int payload = a->returnStates[i]; + size_t payload = a->returnStates[i]; // $+$ = $ bool both$ = payload == EMPTY_RETURN_STATE && a_parent && b_parent; bool ax_ax = (a_parent && b_parent) && a_parent == b_parent; // ax+ax -> ax @@ -585,7 +585,7 @@ std::vector PredictionContext::toStrings(Recognizer *recognizer, co size_t offset = 0; bool last = true; PredictionContext *p = this; - int stateNumber = currentState; + size_t stateNumber = currentState; std::stringstream ss; ss << "["; @@ -615,8 +615,8 @@ std::vector PredictionContext::toStrings(Recognizer *recognizer, co } const ATN &atn = recognizer->getATN(); - ATNState *s = atn.states[(size_t)stateNumber]; - std::string ruleName = recognizer->getRuleNames()[(size_t)s->ruleIndex]; + ATNState *s = atn.states[stateNumber]; + std::string ruleName = recognizer->getRuleNames()[s->ruleIndex]; ss << ruleName; } else if (p->getReturnState(index) != EMPTY_RETURN_STATE) { if (!p->isEmpty()) { diff --git a/runtime/Cpp/runtime/src/atn/PredictionContext.h b/runtime/Cpp/runtime/src/atn/PredictionContext.h index 1f83c55f8..54286ba27 100755 --- a/runtime/Cpp/runtime/src/atn/PredictionContext.h +++ b/runtime/Cpp/runtime/src/atn/PredictionContext.h @@ -53,14 +53,17 @@ namespace atn { /// Represents $ in an array in full context mode, when $ /// doesn't mean wildcard: $ + x = [$,x]. Here, /// $ = EMPTY_RETURN_STATE. - static const int EMPTY_RETURN_STATE = INT_MAX; + // ml: originally Integer.MAX_VALUE, which would be (size_t)-1 for us, but this is already used in places where + // -1 is converted to unsigned, so we use a different value here. Any value does the job provided it doesn't + // conflict with real return states. + static const size_t EMPTY_RETURN_STATE = (size_t)-10; private: - static const int INITIAL_HASH = 1; + static const size_t INITIAL_HASH = 1; public: - static int globalNodeCount; - const int id; + static size_t globalNodeCount; + const size_t id; /// /// Stores the computed hash code of this . The hash @@ -96,7 +99,7 @@ namespace atn { virtual size_t size() const = 0; virtual std::weak_ptr getParent(size_t index) const = 0; - virtual int getReturnState(size_t index) const = 0; + virtual size_t getReturnState(size_t index) const = 0; virtual bool operator == (const PredictionContext &o) const = 0; virtual bool operator != (const PredictionContext &o) const; @@ -108,13 +111,14 @@ namespace atn { protected: static size_t calculateEmptyHashCode(); - static size_t calculateHashCode(std::weak_ptr parent, int returnState); - static size_t calculateHashCode(const std::vector> &parents, const std::vector &returnStates); + static size_t calculateHashCode(std::weak_ptr parent, size_t returnState); + static size_t calculateHashCode(const std::vector> &parents, + const std::vector &returnStates); public: // dispatch - static Ref merge(const Ref &a, - const Ref &b, bool rootIsWildcard, PredictionContextMergeCache *mergeCache); + static Ref merge(const Ref &a, const Ref &b, + bool rootIsWildcard, PredictionContextMergeCache *mergeCache); /// /// Merge two instances. diff --git a/runtime/Cpp/runtime/src/atn/PredictionMode.cpp b/runtime/Cpp/runtime/src/atn/PredictionMode.cpp index 7687eb0e2..e95fceeb9 100755 --- a/runtime/Cpp/runtime/src/atn/PredictionMode.cpp +++ b/runtime/Cpp/runtime/src/atn/PredictionMode.cpp @@ -49,7 +49,7 @@ struct AltAndContextConfigHasher */ size_t operator () (ATNConfig *o) const { size_t hashCode = misc::MurmurHash::initialize(7); - hashCode = misc::MurmurHash::update(hashCode, (size_t)o->state->stateNumber); + hashCode = misc::MurmurHash::update(hashCode, o->state->stateNumber); hashCode = misc::MurmurHash::update(hashCode, o->context); return misc::MurmurHash::finish(hashCode, 2); } @@ -118,7 +118,7 @@ bool PredictionModeClass::allConfigsInRuleStopStates(ATNConfigSet *configs) { return true; } -int PredictionModeClass::resolvesToJustOneViableAlt(const std::vector& altsets) { +size_t PredictionModeClass::resolvesToJustOneViableAlt(const std::vector& altsets) { return getSingleViableAlt(altsets); } @@ -158,7 +158,7 @@ bool PredictionModeClass::allSubsetsEqual(const std::vector& a return true; } -int PredictionModeClass::getUniqueAlt(const std::vector& altsets) { +size_t PredictionModeClass::getUniqueAlt(const std::vector& altsets) { antlrcpp::BitSet all = getAlts(altsets); if (all.count() == 1) { return all.nextSetBit(0); @@ -211,12 +211,12 @@ bool PredictionModeClass::hasStateAssociatedWithOneAlt(ATNConfigSet *configs) { return false; } -int PredictionModeClass::getSingleViableAlt(const std::vector& altsets) { +size_t PredictionModeClass::getSingleViableAlt(const std::vector& altsets) { antlrcpp::BitSet viableAlts; for (antlrcpp::BitSet alts : altsets) { - int minAlt = alts.nextSetBit(0); + size_t minAlt = alts.nextSetBit(0); - viableAlts.set((size_t)minAlt); + viableAlts.set(minAlt); if (viableAlts.count() > 1) // more than 1 viable alt { return ATN::INVALID_ALT_NUMBER; diff --git a/runtime/Cpp/runtime/src/atn/PredictionMode.h b/runtime/Cpp/runtime/src/atn/PredictionMode.h index 10755047a..3b9799bec 100755 --- a/runtime/Cpp/runtime/src/atn/PredictionMode.h +++ b/runtime/Cpp/runtime/src/atn/PredictionMode.h @@ -366,7 +366,7 @@ namespace atn { * we need exact ambiguity detection when the sets look like * {@code A={{1,2}}} or {@code {{1,2},{1,2}}}, etc...

*/ - static int resolvesToJustOneViableAlt(const std::vector &altsets); + static size_t resolvesToJustOneViableAlt(const std::vector &altsets); /// /// Determines if every alternative subset in {@code altsets} contains more @@ -415,7 +415,7 @@ namespace atn { /// . /// /// a collection of alternative subsets - static int getUniqueAlt(const std::vector &altsets); + static size_t getUniqueAlt(const std::vector &altsets); /// /// Gets the complete set of represented alternatives for a collection of @@ -455,7 +455,7 @@ namespace atn { static bool hasStateAssociatedWithOneAlt(ATNConfigSet *configs); - static int getSingleViableAlt(const std::vector &altsets); + static size_t getSingleViableAlt(const std::vector &altsets); }; } // namespace atn diff --git a/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.cpp b/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.cpp index 7dcc95663..27c04c042 100755 --- a/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.cpp +++ b/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.cpp @@ -53,16 +53,16 @@ ProfilingATNSimulator::ProfilingATNSimulator(Parser *parser) } } -int ProfilingATNSimulator::adaptivePredict(TokenStream *input, int decision, Ref const& outerContext) { +size_t ProfilingATNSimulator::adaptivePredict(TokenStream *input, size_t decision, Ref const& outerContext) { auto onExit = finally([this](){ - _currentDecision = -1; + _currentDecision = 0; // Originally -1, but that makes no sense (index into a vector and init value is also 0). }); _sllStopIndex = -1; _llStopIndex = -1; _currentDecision = decision; high_resolution_clock::time_point start = high_resolution_clock::now(); // expensive but useful info - int alt = ParserATNSimulator::adaptivePredict(input, decision, outerContext); + size_t alt = ParserATNSimulator::adaptivePredict(input, decision, outerContext); high_resolution_clock::time_point stop = high_resolution_clock::now(); _decisions[decision].timeInPrediction += duration_cast(stop - start).count(); _decisions[decision].invocations++; @@ -88,7 +88,7 @@ int ProfilingATNSimulator::adaptivePredict(TokenStream *input, int decision, Ref return alt; } -DFAState* ProfilingATNSimulator::getExistingTargetState(DFAState *previousD, ssize_t t) { +DFAState* ProfilingATNSimulator::getExistingTargetState(DFAState *previousD, size_t t) { // this method is called after each time the input position advances // during SLL prediction _sllStopIndex = (int)_input->index(); @@ -107,13 +107,13 @@ DFAState* ProfilingATNSimulator::getExistingTargetState(DFAState *previousD, ssi return existingTargetState; } -DFAState* ProfilingATNSimulator::computeTargetState(DFA &dfa, DFAState *previousD, ssize_t t) { +DFAState* ProfilingATNSimulator::computeTargetState(DFA &dfa, DFAState *previousD, size_t t) { DFAState *state = ParserATNSimulator::computeTargetState(dfa, previousD, t); _currentState = state; return state; } -std::unique_ptr ProfilingATNSimulator::computeReachSet(ATNConfigSet *closure, ssize_t t, bool fullCtx) { +std::unique_ptr ProfilingATNSimulator::computeReachSet(ATNConfigSet *closure, size_t t, bool fullCtx) { if (fullCtx) { // this method is called after each time the input position advances // during full context prediction @@ -162,7 +162,7 @@ void ProfilingATNSimulator::reportAttemptingFullContext(DFA &dfa, const BitSet & ParserATNSimulator::reportAttemptingFullContext(dfa, conflictingAlts, configs, startIndex, stopIndex); } -void ProfilingATNSimulator::reportContextSensitivity(DFA &dfa, int prediction, ATNConfigSet *configs, +void ProfilingATNSimulator::reportContextSensitivity(DFA &dfa, size_t prediction, ATNConfigSet *configs, size_t startIndex, size_t stopIndex) { if (prediction != conflictingAltResolvedBySLL) { _decisions[_currentDecision].contextSensitivities.push_back( @@ -174,7 +174,7 @@ void ProfilingATNSimulator::reportContextSensitivity(DFA &dfa, int prediction, A void ProfilingATNSimulator::reportAmbiguity(DFA &dfa, DFAState *D, size_t startIndex, size_t stopIndex, bool exact, const BitSet &ambigAlts, ATNConfigSet *configs) { - int prediction; + size_t prediction; if (ambigAlts.count() > 0) { prediction = ambigAlts.nextSetBit(0); } else { diff --git a/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.h b/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.h index 2a707ed9e..fa0008162 100755 --- a/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.h +++ b/runtime/Cpp/runtime/src/atn/ProfilingATNSimulator.h @@ -41,19 +41,18 @@ namespace atn { public: ProfilingATNSimulator(Parser *parser); - virtual int adaptivePredict(TokenStream *input, int decision, Ref const& outerContext) override; + virtual size_t adaptivePredict(TokenStream *input, size_t decision, Ref const& outerContext) override; virtual std::vector getDecisionInfo() const; virtual dfa::DFAState* getCurrentState() const; protected: std::vector _decisions; - int _numDecisions = 0; int _sllStopIndex = 0; int _llStopIndex = 0; - int _currentDecision = 0; + size_t _currentDecision = 0; dfa::DFAState *_currentState; /// @@ -68,16 +67,16 @@ namespace atn { /// It may in fact still be a context sensitivity but we don't know by looking at the /// minimum alternatives for the current input. /// - int conflictingAltResolvedBySLL = 0; + size_t conflictingAltResolvedBySLL = 0; - virtual dfa::DFAState* getExistingTargetState(dfa::DFAState *previousD, ssize_t t) override; - virtual dfa::DFAState* computeTargetState(dfa::DFA &dfa, dfa::DFAState *previousD, ssize_t t) override; - virtual std::unique_ptr computeReachSet(ATNConfigSet *closure, ssize_t t, bool fullCtx) override; + virtual dfa::DFAState* getExistingTargetState(dfa::DFAState *previousD, size_t t) override; + virtual dfa::DFAState* computeTargetState(dfa::DFA &dfa, dfa::DFAState *previousD, size_t t) override; + virtual std::unique_ptr computeReachSet(ATNConfigSet *closure, size_t t, bool fullCtx) override; virtual bool evalSemanticContext(Ref const& pred, Ref const& parserCallStack, int alt, bool fullCtx) override; virtual void reportAttemptingFullContext(dfa::DFA &dfa, const antlrcpp::BitSet &conflictingAlts, ATNConfigSet *configs, size_t startIndex, size_t stopIndex) override; - virtual void reportContextSensitivity(dfa::DFA &dfa, int prediction, ATNConfigSet *configs, + virtual void reportContextSensitivity(dfa::DFA &dfa, size_t prediction, ATNConfigSet *configs, size_t startIndex, size_t stopIndex) override; virtual void reportAmbiguity(dfa::DFA &dfa, dfa::DFAState *D, size_t startIndex, size_t stopIndex, bool exact, const antlrcpp::BitSet &ambigAlts, ATNConfigSet *configs) override; diff --git a/runtime/Cpp/runtime/src/atn/RangeTransition.cpp b/runtime/Cpp/runtime/src/atn/RangeTransition.cpp index 5b03eec39..afe9630ca 100755 --- a/runtime/Cpp/runtime/src/atn/RangeTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/RangeTransition.cpp @@ -36,10 +36,10 @@ using namespace antlr4; using namespace antlr4::atn; -RangeTransition::RangeTransition(ATNState *target, ssize_t from, ssize_t to) : Transition(target), from(from), to(to) { +RangeTransition::RangeTransition(ATNState *target, size_t from, size_t to) : Transition(target), from(from), to(to) { } -int RangeTransition::getSerializationType() const { +Transition::SerializationType RangeTransition::getSerializationType() const { return RANGE; } @@ -47,7 +47,7 @@ misc::IntervalSet RangeTransition::label() const { return misc::IntervalSet::of((int)from, (int)to); } -bool RangeTransition::matches(ssize_t symbol, ssize_t /*minVocabSymbol*/, ssize_t /*maxVocabSymbol*/) const { +bool RangeTransition::matches(size_t symbol, size_t /*minVocabSymbol*/, size_t /*maxVocabSymbol*/) const { return symbol >= from && symbol <= to; } diff --git a/runtime/Cpp/runtime/src/atn/RangeTransition.h b/runtime/Cpp/runtime/src/atn/RangeTransition.h index 1ca9021f2..3103e702e 100755 --- a/runtime/Cpp/runtime/src/atn/RangeTransition.h +++ b/runtime/Cpp/runtime/src/atn/RangeTransition.h @@ -38,15 +38,15 @@ namespace atn { class ANTLR4CPP_PUBLIC RangeTransition final : public Transition { public: - const ssize_t from; - const ssize_t to; + const size_t from; + const size_t to; - RangeTransition(ATNState *target, ssize_t from, ssize_t to); + RangeTransition(ATNState *target, size_t from, size_t to); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; virtual misc::IntervalSet label() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/atn/RuleTransition.cpp b/runtime/Cpp/runtime/src/atn/RuleTransition.cpp index 643ebcbc6..c955ce36c 100755 --- a/runtime/Cpp/runtime/src/atn/RuleTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/RuleTransition.cpp @@ -34,16 +34,16 @@ using namespace antlr4::atn; -RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, ATNState *followState) +RuleTransition::RuleTransition(RuleStartState *ruleStart, size_t ruleIndex, ATNState *followState) : RuleTransition(ruleStart, ruleIndex, 0, followState) { } -RuleTransition::RuleTransition(RuleStartState *ruleStart, int ruleIndex, int precedence, ATNState *followState) +RuleTransition::RuleTransition(RuleStartState *ruleStart, size_t ruleIndex, int precedence, ATNState *followState) : Transition(ruleStart), ruleIndex(ruleIndex), precedence(precedence) { this->followState = followState; } -int RuleTransition::getSerializationType() const { +Transition::SerializationType RuleTransition::getSerializationType() const { return RULE; } @@ -51,7 +51,7 @@ bool RuleTransition::isEpsilon() const { return true; } -bool RuleTransition::matches(ssize_t /*symbol*/, ssize_t /*minVocabSymbol*/, ssize_t /*maxVocabSymbol*/) const { +bool RuleTransition::matches(size_t /*symbol*/, size_t /*minVocabSymbol*/, size_t /*maxVocabSymbol*/) const { return false; } diff --git a/runtime/Cpp/runtime/src/atn/RuleTransition.h b/runtime/Cpp/runtime/src/atn/RuleTransition.h index 999d5e5ff..572b48091 100755 --- a/runtime/Cpp/runtime/src/atn/RuleTransition.h +++ b/runtime/Cpp/runtime/src/atn/RuleTransition.h @@ -40,7 +40,7 @@ namespace atn { /// /// Ptr to the rule definition object for this rule ref public: - const int ruleIndex; // no Rule object at runtime + const size_t ruleIndex; // no Rule object at runtime const int precedence; @@ -49,15 +49,15 @@ namespace atn { ATNState *followState; /// @deprecated Use - /// instead. - RuleTransition(RuleStartState *ruleStart, int ruleIndex, ATNState *followState); + /// instead. + RuleTransition(RuleStartState *ruleStart, size_t ruleIndex, ATNState *followState); - RuleTransition(RuleStartState *ruleStart, int ruleIndex, int precedence, ATNState *followState); + RuleTransition(RuleStartState *ruleStart, size_t ruleIndex, int precedence, ATNState *followState); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; virtual bool isEpsilon() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/atn/SemanticContext.cpp b/runtime/Cpp/runtime/src/atn/SemanticContext.cpp index eed50f359..b068f080b 100755 --- a/runtime/Cpp/runtime/src/atn/SemanticContext.cpp +++ b/runtime/Cpp/runtime/src/atn/SemanticContext.cpp @@ -41,10 +41,10 @@ using namespace antlrcpp; //------------------ Predicate ----------------------------------------------------------------------------------------- -SemanticContext::Predicate::Predicate() : Predicate(-1, -1, false) { +SemanticContext::Predicate::Predicate() : Predicate(INVALID_INDEX, INVALID_INDEX, false) { } -SemanticContext::Predicate::Predicate(int ruleIndex, int predIndex, bool isCtxDependent) +SemanticContext::Predicate::Predicate(size_t ruleIndex, size_t predIndex, bool isCtxDependent) : ruleIndex(ruleIndex), predIndex(predIndex), isCtxDependent(isCtxDependent) { } @@ -58,8 +58,8 @@ bool SemanticContext::Predicate::eval(Recognizer *parser, Ref const size_t SemanticContext::Predicate::hashCode() const { size_t hashCode = misc::MurmurHash::initialize(); - hashCode = misc::MurmurHash::update(hashCode, (size_t)ruleIndex); - hashCode = misc::MurmurHash::update(hashCode, (size_t)predIndex); + hashCode = misc::MurmurHash::update(hashCode, ruleIndex); + hashCode = misc::MurmurHash::update(hashCode, predIndex); hashCode = misc::MurmurHash::update(hashCode, isCtxDependent ? 1 : 0); hashCode = misc::MurmurHash::finish(hashCode, 3); return hashCode; @@ -335,7 +335,7 @@ std::string SemanticContext::OR::toString() const { //------------------ SemanticContext ----------------------------------------------------------------------------------- -const Ref SemanticContext::NONE = std::make_shared(-1, -1, false); +const Ref SemanticContext::NONE = std::make_shared(INVALID_INDEX, INVALID_INDEX, false); bool SemanticContext::operator != (const SemanticContext &other) const { return !(*this == other); diff --git a/runtime/Cpp/runtime/src/atn/SemanticContext.h b/runtime/Cpp/runtime/src/atn/SemanticContext.h index c5ef39b65..1bcd40b56 100755 --- a/runtime/Cpp/runtime/src/atn/SemanticContext.h +++ b/runtime/Cpp/runtime/src/atn/SemanticContext.h @@ -126,15 +126,15 @@ namespace atn { class ANTLR4CPP_PUBLIC SemanticContext::Predicate : public SemanticContext { public: - const int ruleIndex; - const int predIndex; + const size_t ruleIndex; + const size_t predIndex; const bool isCtxDependent; // e.g., $i ref in pred protected: Predicate(); public: - Predicate(int ruleIndex, int predIndex, bool isCtxDependent); + Predicate(size_t ruleIndex, size_t predIndex, bool isCtxDependent); virtual bool eval(Recognizer *parser, Ref const& parserCallStack) override; virtual size_t hashCode() const override; diff --git a/runtime/Cpp/runtime/src/atn/SetTransition.cpp b/runtime/Cpp/runtime/src/atn/SetTransition.cpp index 4b3f83e71..c2fbe5a38 100755 --- a/runtime/Cpp/runtime/src/atn/SetTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/SetTransition.cpp @@ -41,7 +41,7 @@ SetTransition::SetTransition(ATNState *target, const misc::IntervalSet &aSet) : Transition(target), set(aSet.isEmpty() ? misc::IntervalSet::of(Token::INVALID_TYPE) : aSet) { } -int SetTransition::getSerializationType() const { +Transition::SerializationType SetTransition::getSerializationType() const { return SET; } @@ -49,8 +49,8 @@ misc::IntervalSet SetTransition::label() const { return set; } -bool SetTransition::matches(ssize_t symbol, ssize_t /*minVocabSymbol*/, ssize_t /*maxVocabSymbol*/) const { - return set.contains((int)symbol); +bool SetTransition::matches(size_t symbol, size_t /*minVocabSymbol*/, size_t /*maxVocabSymbol*/) const { + return set.contains(symbol); } std::string SetTransition::toString() const { diff --git a/runtime/Cpp/runtime/src/atn/SetTransition.h b/runtime/Cpp/runtime/src/atn/SetTransition.h index 475ac849f..ea0fb5f75 100755 --- a/runtime/Cpp/runtime/src/atn/SetTransition.h +++ b/runtime/Cpp/runtime/src/atn/SetTransition.h @@ -44,10 +44,10 @@ namespace atn { SetTransition(ATNState *target, const misc::IntervalSet &set); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; virtual misc::IntervalSet label() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.cpp b/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.cpp index 1d742c68a..524206bb3 100755 --- a/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.cpp +++ b/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.cpp @@ -35,14 +35,15 @@ using namespace antlr4::atn; -SingletonPredictionContext::SingletonPredictionContext(std::weak_ptr parent, int returnState) +SingletonPredictionContext::SingletonPredictionContext(std::weak_ptr parent, size_t returnState) : PredictionContext(!parent.expired() ? calculateHashCode(parent, returnState) : calculateEmptyHashCode()), parent(parent.lock()), returnState(returnState) { assert(returnState != ATNState::INVALID_STATE_NUMBER); } Ref SingletonPredictionContext::create(std::weak_ptr parent, - int returnState) { + size_t returnState) { + if (returnState == EMPTY_RETURN_STATE && parent.expired()) { // someone can pass in the bits of an array ctx that mean $ return std::dynamic_pointer_cast(EMPTY); @@ -56,13 +57,13 @@ size_t SingletonPredictionContext::size() const { std::weak_ptr SingletonPredictionContext::getParent(size_t index) const { assert(index == 0); - ((void)(index)); // Make Release build happy + ((void)(index)); // Make Release build happy. return parent; } -int SingletonPredictionContext::getReturnState(size_t index) const { +size_t SingletonPredictionContext::getReturnState(size_t index) const { assert(index == 0); - ((void)(index)); // Make Release build happy + ((void)(index)); // Make Release build happy. return returnState; } diff --git a/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.h b/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.h index 9ee93ca89..1ac01ae37 100755 --- a/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.h +++ b/runtime/Cpp/runtime/src/atn/SingletonPredictionContext.h @@ -44,16 +44,16 @@ namespace atn { // which got this one as parent with a null reference) we use a shared_ptr here instead, to keep those left alone // parent contexts alive. const Ref parent; - const int returnState; + const size_t returnState; - SingletonPredictionContext(std::weak_ptr parent, int returnState); + SingletonPredictionContext(std::weak_ptr parent, size_t returnState); virtual ~SingletonPredictionContext() {}; - static Ref create(std::weak_ptr parent, int returnState); + static Ref create(std::weak_ptr parent, size_t returnState); virtual size_t size() const override; virtual std::weak_ptr getParent(size_t index) const override; - virtual int getReturnState(size_t index) const override; + virtual size_t getReturnState(size_t index) const override; virtual bool operator == (const PredictionContext &o) const override; virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/atn/Transition.h b/runtime/Cpp/runtime/src/atn/Transition.h index c43e1c34c..b086f52af 100755 --- a/runtime/Cpp/runtime/src/atn/Transition.h +++ b/runtime/Cpp/runtime/src/atn/Transition.h @@ -52,17 +52,18 @@ namespace atn { class ANTLR4CPP_PUBLIC Transition { public: // constants for serialization - static const int EPSILON = 1; - static const int RANGE = 2; - static const int RULE = 3; - static const int PREDICATE = 4; // e.g., {isType(input.LT(1))}? - static const int ATOM = 5; - static const int ACTION = 6; - static const int SET = 7; // ~(A|B) or ~atom, wildcard, which convert to next 2 - static const int NOT_SET = 8; - static const int WILDCARD = 9; - static const int PRECEDENCE = 10; - + enum SerializationType { + EPSILON = 1, + RANGE = 2, + RULE = 3, + PREDICATE = 4, // e.g., {isType(input.LT(1))}? + ATOM = 5, + ACTION = 6, + SET = 7, // ~(A|B) or ~atom, wildcard, which convert to next 2 + NOT_SET = 8, + WILDCARD = 9, + PRECEDENCE = 10, + }; static const std::vector serializationNames; @@ -76,7 +77,7 @@ namespace atn { Transition(ATNState *target); public: - virtual int getSerializationType() const = 0; + virtual SerializationType getSerializationType() const = 0; /** * Determines if the transition is an "epsilon" transition. @@ -89,7 +90,7 @@ namespace atn { */ virtual bool isEpsilon() const; virtual misc::IntervalSet label() const; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const = 0; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const = 0; virtual std::string toString() const; }; diff --git a/runtime/Cpp/runtime/src/atn/WildcardTransition.cpp b/runtime/Cpp/runtime/src/atn/WildcardTransition.cpp index b2fb39665..54a5b3854 100755 --- a/runtime/Cpp/runtime/src/atn/WildcardTransition.cpp +++ b/runtime/Cpp/runtime/src/atn/WildcardTransition.cpp @@ -38,11 +38,11 @@ using namespace antlr4::atn; WildcardTransition::WildcardTransition(ATNState *target) : Transition(target) { } -int WildcardTransition::getSerializationType() const { +Transition::SerializationType WildcardTransition::getSerializationType() const { return WILDCARD; } -bool WildcardTransition::matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const { +bool WildcardTransition::matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const { return symbol >= minVocabSymbol && symbol <= maxVocabSymbol; } diff --git a/runtime/Cpp/runtime/src/atn/WildcardTransition.h b/runtime/Cpp/runtime/src/atn/WildcardTransition.h index bb9399f36..255b2cb93 100755 --- a/runtime/Cpp/runtime/src/atn/WildcardTransition.h +++ b/runtime/Cpp/runtime/src/atn/WildcardTransition.h @@ -40,9 +40,9 @@ namespace atn { public: WildcardTransition(ATNState *target); - virtual int getSerializationType() const override; + virtual SerializationType getSerializationType() const override; - virtual bool matches(ssize_t symbol, ssize_t minVocabSymbol, ssize_t maxVocabSymbol) const override; + virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override; virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/dfa/DFA.cpp b/runtime/Cpp/runtime/src/dfa/DFA.cpp index 0d36d649d..f96119588 100755 --- a/runtime/Cpp/runtime/src/dfa/DFA.cpp +++ b/runtime/Cpp/runtime/src/dfa/DFA.cpp @@ -44,7 +44,7 @@ using namespace antlrcpp; DFA::DFA(atn::DecisionState *atnStartState) : DFA(atnStartState, 0) { } -DFA::DFA(atn::DecisionState *atnStartState, int decision) +DFA::DFA(atn::DecisionState *atnStartState, size_t decision) : atnStartState(atnStartState), s0(nullptr), decision(decision) { _precedenceDfa = false; diff --git a/runtime/Cpp/runtime/src/dfa/DFA.h b/runtime/Cpp/runtime/src/dfa/DFA.h index bac93cd53..435ad5c99 100755 --- a/runtime/Cpp/runtime/src/dfa/DFA.h +++ b/runtime/Cpp/runtime/src/dfa/DFA.h @@ -45,10 +45,10 @@ namespace dfa { atn::DecisionState *const atnStartState; std::unordered_set states; // States are owned by this class. DFAState *s0; - const int decision; + const size_t decision; DFA(atn::DecisionState *atnStartState); - DFA(atn::DecisionState *atnStartState, int decision); + DFA(atn::DecisionState *atnStartState, size_t decision); DFA(const DFA &other); DFA(DFA &&other); virtual ~DFA(); diff --git a/runtime/Cpp/runtime/src/dfa/DFASerializer.cpp b/runtime/Cpp/runtime/src/dfa/DFASerializer.cpp index 0bd62bd52..d269338eb 100755 --- a/runtime/Cpp/runtime/src/dfa/DFASerializer.cpp +++ b/runtime/Cpp/runtime/src/dfa/DFASerializer.cpp @@ -70,7 +70,7 @@ std::string DFASerializer::getEdgeLabel(size_t i) const { } std::string DFASerializer::getStateString(DFAState *s) const { - size_t n = (size_t)s->stateNumber; + size_t n = s->stateNumber; const std::string baseStateStr = std::string(s->isAcceptState ? ":" : "") + "s" + std::to_string(n) + (s->requiresFullContext ? "^" : ""); diff --git a/runtime/Cpp/runtime/src/dfa/DFAState.h b/runtime/Cpp/runtime/src/dfa/DFAState.h index 6c56d377a..c0a62ae6c 100755 --- a/runtime/Cpp/runtime/src/dfa/DFAState.h +++ b/runtime/Cpp/runtime/src/dfa/DFAState.h @@ -85,16 +85,14 @@ namespace dfa { /// maps to {@code edges[0]}. // ml: this is a sparse list, so we use a map instead of a vector. // Watch out: we no longer have the -1 offset, as it isn't needed anymore. - std::unordered_map edges; + std::unordered_map edges; bool isAcceptState; - /// /// if accept state, what ttype do we match or alt do we predict? - /// This is set to when {@code !=null} or - /// . - /// - int prediction; + /// This is set to when {@code !=null} or + /// . + size_t prediction; Ref lexerActionExecutor; diff --git a/runtime/Cpp/runtime/src/misc/Interval.cpp b/runtime/Cpp/runtime/src/misc/Interval.cpp index 03b3c5f90..af412d321 100755 --- a/runtime/Cpp/runtime/src/misc/Interval.cpp +++ b/runtime/Cpp/runtime/src/misc/Interval.cpp @@ -33,17 +33,24 @@ using namespace antlr4::misc; -Interval const Interval::INVALID; -int Interval::creates = 0; -int Interval::misses = 0; -int Interval::hits = 0; -int Interval::outOfRange = 0; - -Interval::Interval() : Interval(-1, -2) { +size_t antlr4::misc::numericToSymbol(ssize_t v) { + return (size_t)v; } -Interval::Interval(int a_, int b_, bool autoExtend) { +ssize_t antlr4::misc::symbolToNumeric(size_t v) { + return (ssize_t)v; +} + +Interval const Interval::INVALID; + +Interval::Interval() : Interval(-1L, -2L) { +} + +Interval::Interval(size_t a_, size_t b_) : Interval(symbolToNumeric(a_), symbolToNumeric(b_)) { +} + +Interval::Interval(ssize_t a_, ssize_t b_, bool autoExtend) { a = a_; b = b_; @@ -53,11 +60,11 @@ Interval::Interval(int a_, int b_, bool autoExtend) { } } -int Interval::length() const { +size_t Interval::length() const { if (b < a) { return 0; } - return b - a + 1; + return size_t(b - a + 1); } bool Interval::operator == (const Interval &other) const { diff --git a/runtime/Cpp/runtime/src/misc/Interval.h b/runtime/Cpp/runtime/src/misc/Interval.h index ea132c3aa..c14e057ad 100755 --- a/runtime/Cpp/runtime/src/misc/Interval.h +++ b/runtime/Cpp/runtime/src/misc/Interval.h @@ -36,28 +36,29 @@ namespace antlr4 { namespace misc { + // Helpers to convert certain unsigned symbols (e.g. Token::EOF) to their original numeric value (e.g. -1) + // and vice version. This is needed mostly for intervals to keep their original order and for toString() + // methods to print the original numeric value (e.g. for tests). + size_t numericToSymbol(ssize_t v); + ssize_t symbolToNumeric(size_t v); + /// An immutable inclusive interval a..b class ANTLR4CPP_PUBLIC Interval { public: static const Interval INVALID; - int a; - int b; - - static int creates; - static int misses; - static int hits; - static int outOfRange; + // Must stay signed to guarantee the correct sort order. + ssize_t a; + ssize_t b; Interval(); - Interval(int a_, int b_, bool autoExtend = false); // Automatically extend a value of 0xFFFF to 0x10FFFF. + explicit Interval(size_t a_, size_t b_); // For unsigned -> signed mappings. + Interval(ssize_t a_, ssize_t b_, bool autoExtend = false); // Automatically extend a value of 0xFFFF to 0x10FFFF. virtual ~Interval() {}; - /// /// return number of elements between a and b inclusively. x..x is length 1. /// if b < a, then length is 0. 9..10 has length 2. - /// - virtual int length() const; + virtual size_t length() const; bool operator == (const Interval &other) const; diff --git a/runtime/Cpp/runtime/src/misc/IntervalSet.cpp b/runtime/Cpp/runtime/src/misc/IntervalSet.cpp index 666c4ad9e..09b4721e3 100755 --- a/runtime/Cpp/runtime/src/misc/IntervalSet.cpp +++ b/runtime/Cpp/runtime/src/misc/IntervalSet.cpp @@ -72,11 +72,11 @@ IntervalSet::IntervalSet(int n, ...) : IntervalSet() { } } -IntervalSet IntervalSet::of(int a) { +IntervalSet IntervalSet::of(ssize_t a) { return IntervalSet({ Interval(a, a) }); } -IntervalSet IntervalSet::of(int a, int b, bool autoExtend) { +IntervalSet IntervalSet::of(ssize_t a, ssize_t b, bool autoExtend) { return IntervalSet({ Interval(a, b, autoExtend) }); } @@ -87,14 +87,14 @@ void IntervalSet::clear() { _intervals.clear(); } -void IntervalSet::add(int el) { +void IntervalSet::add(ssize_t el) { if (_readonly) { throw IllegalStateException("can't alter read only IntervalSet"); } add(el, el); } -void IntervalSet::add(int a, int b, bool autoExtend) { +void IntervalSet::add(ssize_t a, ssize_t b, bool autoExtend) { add(Interval(a, b, autoExtend)); } @@ -167,7 +167,7 @@ IntervalSet& IntervalSet::addAll(const IntervalSet &set) { return *this; } -IntervalSet IntervalSet::complement(int minElement, int maxElement) const { +IntervalSet IntervalSet::complement(ssize_t minElement, ssize_t maxElement) const { return complement(IntervalSet::of(minElement, maxElement)); } @@ -301,9 +301,14 @@ IntervalSet IntervalSet::And(const IntervalSet &other) const { return intersection; } -bool IntervalSet::contains(int el) const { +bool IntervalSet::contains(size_t el) const { + return contains(symbolToNumeric(el)); +} + +bool IntervalSet::contains(ssize_t el) const { if (_intervals.empty()) return false; + if (el < _intervals[0].a) // list is sorted and el is before first interval; not here return false; @@ -319,7 +324,7 @@ bool IntervalSet::isEmpty() const { return _intervals.empty(); } -int IntervalSet::getSingleElement() const { +ssize_t IntervalSet::getSingleElement() const { if (_intervals.size() == 1) { if (_intervals[0].a == _intervals[0].b) { return _intervals[0].a; @@ -329,7 +334,7 @@ int IntervalSet::getSingleElement() const { return Token::INVALID_TYPE; // XXX: this value is 0, but 0 is a valid interval range, how can that work? } -int IntervalSet::getMaxElement() const { +ssize_t IntervalSet::getMaxElement() const { if (_intervals.empty()) { return Token::INVALID_TYPE; } @@ -337,7 +342,7 @@ int IntervalSet::getMaxElement() const { return _intervals.back().b; } -int IntervalSet::getMinElement() const { +ssize_t IntervalSet::getMinElement() const { if (_intervals.empty()) { return Token::INVALID_TYPE; } @@ -352,8 +357,8 @@ std::vector IntervalSet::getIntervals() const { size_t IntervalSet::hashCode() const { size_t hash = MurmurHash::initialize(); for (auto &interval : _intervals) { - hash = MurmurHash::update(hash, (size_t)interval.a); - hash = MurmurHash::update(hash, (size_t)interval.b); + hash = MurmurHash::update(hash, interval.a); + hash = MurmurHash::update(hash, interval.b); } return MurmurHash::finish(hash, _intervals.size() * 2); @@ -390,10 +395,10 @@ std::string IntervalSet::toString(bool elemAreChar) const { ss << ", "; firstEntry = false; - int a = interval.a; - int b = interval.b; + ssize_t a = interval.a; + ssize_t b = interval.b; if (a == b) { - if (a == Token::EOF) { + if (a == -1) { ss << ""; } else if (elemAreChar) { ss << "'" << static_cast(a) << "'"; @@ -436,8 +441,8 @@ std::string IntervalSet::toString(const dfa::Vocabulary &vocabulary) const { ss << ", "; firstEntry = false; - ssize_t a = (ssize_t)interval.a; - ssize_t b = (ssize_t)interval.b; + ssize_t a = interval.a; + ssize_t b = interval.b; if (a == b) { ss << elementName(vocabulary, a); } else { @@ -461,9 +466,9 @@ std::string IntervalSet::elementName(const std::vector &tokenNames, } std::string IntervalSet::elementName(const dfa::Vocabulary &vocabulary, ssize_t a) const { - if (a == Token::EOF) { + if (a == -1) { return ""; - } else if (a == Token::EPSILON) { + } else if (a == -2) { return ""; } else { return vocabulary.getDisplayName(a); @@ -478,38 +483,38 @@ size_t IntervalSet::size() const { return result; } -std::vector IntervalSet::toList() const { - std::vector result; +std::vector IntervalSet::toList() const { + std::vector result; for (auto &interval : _intervals) { - size_t a = (size_t)interval.a; - size_t b = (size_t)interval.b; - for (size_t v = a; v <= b; v++) { - result.push_back((int)v); + ssize_t a = interval.a; + ssize_t b = interval.b; + for (ssize_t v = a; v <= b; v++) { + result.push_back(v); } } return result; } -std::set IntervalSet::toSet() const { - std::set result; +std::set IntervalSet::toSet() const { + std::set result; for (auto &interval : _intervals) { - size_t a = (size_t)interval.a; - size_t b = (size_t)interval.b; - for (size_t v = a; v <= b; v++) { - result.insert((int)v); + ssize_t a = interval.a; + ssize_t b = interval.b; + for (ssize_t v = a; v <= b; v++) { + result.insert(v); } } return result; } -int IntervalSet::get(int i) const { +ssize_t IntervalSet::get(size_t i) const { size_t index = 0; for (auto &interval : _intervals) { - size_t a = (size_t)interval.a; - size_t b = (size_t)interval.b; - for (size_t v = a; v <= b; v++) { - if (index == (size_t)i) { - return (int)v; + ssize_t a = interval.a; + ssize_t b = interval.b; + for (ssize_t v = a; v <= b; v++) { + if (index == i) { + return v; } index++; } @@ -517,36 +522,40 @@ int IntervalSet::get(int i) const { return -1; } -void IntervalSet::remove(int el) { +void IntervalSet::remove(size_t el) { + remove(symbolToNumeric(el)); +} + +void IntervalSet::remove(ssize_t el) { if (_readonly) { throw IllegalStateException("can't alter read only IntervalSet"); } for (size_t i = 0; i < _intervals.size(); ++i) { Interval &interval = _intervals[i]; - size_t a = (size_t)interval.a; - size_t b = (size_t)interval.b; - if ((size_t)el < a) { + ssize_t a = interval.a; + ssize_t b = interval.b; + if (el < a) { break; // list is sorted and el is before this interval; not here } // if whole interval x..x, rm - if ((size_t)el == a && (size_t)el == b) { + if (el == a && el == b) { _intervals.erase(_intervals.begin() + (long)i); break; } // if on left edge x..b, adjust left - if ((size_t)el == a) { + if (el == a) { interval.a++; break; } // if on right edge a..x, adjust right - if ((size_t)el == b) { + if (el == b) { interval.b--; break; } // if in middle a..x..b, split interval - if ((size_t)el > a && (size_t)el < b) { // found in this interval + if (el > a && el < b) { // found in this interval size_t oldb = (size_t)interval.b; interval.b = el - 1; // [a..x-1] add(el + 1, (int)oldb); // add [x+1..b] diff --git a/runtime/Cpp/runtime/src/misc/IntervalSet.h b/runtime/Cpp/runtime/src/misc/IntervalSet.h index f9db094bf..2b74d9620 100755 --- a/runtime/Cpp/runtime/src/misc/IntervalSet.h +++ b/runtime/Cpp/runtime/src/misc/IntervalSet.h @@ -66,31 +66,25 @@ namespace misc { virtual ~IntervalSet() {} - /// - /// Create a set with a single element, el. - static IntervalSet of(int a); + /// Create a set with a single element, el. + static IntervalSet of(ssize_t a); - /// - /// Create a set with all ints within range [a..b] (inclusive) - static IntervalSet of(int a, int b, bool autoExtend = false); + /// Create a set with all ints within range [a..b] (inclusive) + static IntervalSet of(ssize_t a, ssize_t b, bool autoExtend = false); virtual void clear(); - /// /// Add a single element to the set. An isolated element is stored - /// as a range el..el. - /// - virtual void add(int el); + /// as a range el..el. + virtual void add(ssize_t el); - /// /// Add interval; i.e., add all integers from a to b to set. - /// If b - virtual void add(int a, int b, bool autoExtend = false); + /// If b /// Return a new set with the intersection of this set with other. Because - /// the intervals are sorted, we can use an iterator for each list and - /// just walk them together. This is roughly O(min(n,m)) for interval - /// list lengths n and m. - /// + /// the intervals are sorted, we can use an iterator for each list and + /// just walk them together. This is roughly O(min(n,m)) for interval + /// list lengths n and m. virtual IntervalSet And(const IntervalSet &other) const; - /// - /// Is el in any range of this set? - virtual bool contains(int el) const; + /// Is el in any range of this set? + virtual bool contains(size_t el) const; // For mapping of e.g. Token::EOF to -1 etc. + virtual bool contains(ssize_t el) const; /// return true if this set has no members virtual bool isEmpty() const; /// /// If this set is a single integer, return it otherwise Token.INVALID_TYPE - virtual int getSingleElement() const; + virtual ssize_t getSingleElement() const; /** * Returns the maximum value contained in the set. @@ -149,7 +141,7 @@ namespace misc { * @return the maximum value contained in the set. If the set is empty, this * method returns {@link Token#INVALID_TYPE}. */ - virtual int getMaxElement() const; + virtual ssize_t getMaxElement() const; /** * Returns the minimum value contained in the set. @@ -157,7 +149,7 @@ namespace misc { * @return the minimum value contained in the set. If the set is empty, this * method returns {@link Token#INVALID_TYPE}. */ - virtual int getMinElement() const; + virtual ssize_t getMinElement() const; /// /// Return a list of Interval objects. @@ -187,16 +179,15 @@ namespace misc { public: virtual size_t size() const; - virtual std::vector toList() const; - virtual std::set toSet() const; + virtual std::vector toList() const; + virtual std::set toSet() const; - /// /// Get the ith element of ordered set. Used only by RandomPhrase so - /// don't bother to implement if you're not doing that for a new - /// ANTLR code gen target. - /// - virtual int get(int i) const; - virtual void remove(int el); + /// don't bother to implement if you're not doing that for a new + /// ANTLR code gen target. + virtual ssize_t get(size_t i) const; + virtual void remove(size_t el); // For mapping of e.g. Token::EOF to -1 etc. + virtual void remove(ssize_t el); virtual bool isReadOnly() const; virtual void setReadOnly(bool readonly); diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index d137bbbfc..9a845383e 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -133,7 +133,7 @@ private: template struct Derived : Base { - template Derived(U&& value) : value(std::forward(value)) { + template Derived(U&& value_) : value(std::forward(value_)) { } T value; diff --git a/runtime/Cpp/runtime/src/support/BitSet.h b/runtime/Cpp/runtime/src/support/BitSet.h index d3d4eda22..8bb6c417d 100644 --- a/runtime/Cpp/runtime/src/support/BitSet.h +++ b/runtime/Cpp/runtime/src/support/BitSet.h @@ -36,14 +36,14 @@ namespace antlrcpp { class ANTLR4CPP_PUBLIC BitSet : public std::bitset<1024> { public: - int nextSetBit(size_t pos) const { + size_t nextSetBit(size_t pos) const { for (size_t i = pos; i < size(); i++){ if (test(i)) { - return (int)i; + return i; } } - return -1; + return INVALID_INDEX; } // Prints a list of every index for which the bitset contains a bit in true. diff --git a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp index 91bcb9940..3be248e99 100755 --- a/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp +++ b/runtime/Cpp/runtime/src/tree/TerminalNodeImpl.cpp @@ -50,7 +50,7 @@ misc::Interval TerminalNodeImpl::getSourceInterval() { return misc::Interval::INVALID; } - int tokenIndex = symbol->getTokenIndex(); + size_t tokenIndex = symbol->getTokenIndex(); return misc::Interval(tokenIndex, tokenIndex); } diff --git a/runtime/Cpp/runtime/src/tree/Trees.cpp b/runtime/Cpp/runtime/src/tree/Trees.cpp index ac4d0a46f..64de898a1 100755 --- a/runtime/Cpp/runtime/src/tree/Trees.cpp +++ b/runtime/Cpp/runtime/src/tree/Trees.cpp @@ -117,7 +117,7 @@ std::string Trees::getNodeText(Ref const& t, const std::vector(t)->getRuleContext()->getRuleIndex(); if (ruleIndex < 0) return "Invalid Rule Index"; - std::string ruleName = ruleNames[(size_t)ruleIndex]; + std::string ruleName = ruleNames[ruleIndex]; int altNumber = std::static_pointer_cast(t)->getAltNumber(); if (altNumber != atn::ATN::INVALID_ALT_NUMBER) { return ruleName + ":" + std::to_string(altNumber); @@ -156,7 +156,7 @@ std::vector> Trees::getAncestors(Ref const& t) { } template -static void _findAllNodes(Ref const& t, int index, bool findTokens, std::vector &nodes) { +static void _findAllNodes(Ref const& t, size_t index, bool findTokens, std::vector &nodes) { // check this node (the root) first if (findTokens && is(t)) { Ref tnode = std::dynamic_pointer_cast(t); @@ -190,15 +190,15 @@ bool Trees::isAncestorOf(Ref const& t, Ref const& u) { return false; } -std::vector> Trees::findAllTokenNodes(Ref const& t, int ttype) { +std::vector> Trees::findAllTokenNodes(Ref const& t, size_t ttype) { return findAllNodes(t, ttype, true); } -std::vector> Trees::findAllRuleNodes(Ref const& t, int ruleIndex) { +std::vector> Trees::findAllRuleNodes(Ref const& t, size_t ruleIndex) { return findAllNodes(t, ruleIndex, false); } -std::vector> Trees::findAllNodes(Ref const& t, int index, bool findTokens) { +std::vector> Trees::findAllNodes(Ref const& t, size_t index, bool findTokens) { std::vector> nodes; _findAllNodes>(t, index, findTokens, nodes); return nodes; @@ -234,8 +234,8 @@ Ref Trees::getRootOfSubtreeEnclosingRegion(Ref con if (is(t)) { Ref r = std::static_pointer_cast(t); - if ((int)startTokenIndex >= r->getStart()->getTokenIndex() && // is range fully contained in t? - (r->getStop() == nullptr || (int)stopTokenIndex <= r->getStop()->getTokenIndex())) { + if (startTokenIndex >= r->getStart()->getTokenIndex() && // is range fully contained in t? + (r->getStop() == nullptr || stopTokenIndex <= r->getStop()->getTokenIndex())) { // note: r.getStop()==null likely implies that we bailed out of parser and there's nothing to the right return r; } diff --git a/runtime/Cpp/runtime/src/tree/Trees.h b/runtime/Cpp/runtime/src/tree/Trees.h index 15e885963..aafec42e9 100755 --- a/runtime/Cpp/runtime/src/tree/Trees.h +++ b/runtime/Cpp/runtime/src/tree/Trees.h @@ -68,9 +68,9 @@ namespace tree { * @since 4.5.1 */ static bool isAncestorOf(Ref const& t, Ref const& u); - static std::vector> findAllTokenNodes(Ref const& t, int ttype); - static std::vector> findAllRuleNodes(Ref const& t, int ruleIndex); - static std::vector> findAllNodes(Ref const& t, int index, bool findTokens); + static std::vector> findAllTokenNodes(Ref const& t, size_t ttype); + static std::vector> findAllRuleNodes(Ref const& t, size_t ruleIndex); + static std::vector> findAllNodes(Ref const& t, size_t index, bool findTokens); /** Get all descendents; includes t itself. * diff --git a/runtime/Cpp/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp b/runtime/Cpp/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp index 9ec42c50c..b0817c6ed 100755 --- a/runtime/Cpp/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp +++ b/runtime/Cpp/runtime/src/tree/pattern/ParseTreePatternMatcher.cpp @@ -268,11 +268,11 @@ std::vector> ParseTreePatternMatcher::tokenize(const std: } tokens.emplace_back(new TokenTagToken(tagChunk.getTag(), (int)ttype, tagChunk.getLabel())); } else if (islower(tagChunk.getTag()[0])) { - ssize_t ruleIndex = _parser->getRuleIndex(tagChunk.getTag()); - if (ruleIndex == -1) { + size_t ruleIndex = _parser->getRuleIndex(tagChunk.getTag()); + if (ruleIndex == INVALID_INDEX) { throw IllegalArgumentException("Unknown rule " + tagChunk.getTag() + " in pattern: " + pattern); } - int ruleImaginaryTokenType = _parser->getATNWithBypassAlts().ruleToTokenType[(size_t)ruleIndex]; + size_t ruleImaginaryTokenType = _parser->getATNWithBypassAlts().ruleToTokenType[ruleIndex]; tokens.emplace_back(new RuleTagToken(tagChunk.getTag(), ruleImaginaryTokenType, tagChunk.getLabel())); } else { throw IllegalArgumentException("invalid tag: " + tagChunk.getTag() + " in pattern: " + pattern); diff --git a/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.cpp b/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.cpp index 629d75453..edc137ad6 100755 --- a/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.cpp +++ b/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.cpp @@ -38,7 +38,7 @@ using namespace antlr4::tree::pattern; RuleTagToken::RuleTagToken(const std::string &/*ruleName*/, int _bypassTokenType) : bypassTokenType(_bypassTokenType) { } -RuleTagToken::RuleTagToken(const std::string &ruleName, int bypassTokenType, const std::string &label) +RuleTagToken::RuleTagToken(const std::string &ruleName, size_t bypassTokenType, const std::string &label) : ruleName(ruleName), bypassTokenType(bypassTokenType), label(label) { if (ruleName.empty()) { throw IllegalArgumentException("ruleName cannot be null or empty."); @@ -66,28 +66,28 @@ std::string RuleTagToken::getText() const { return std::string("<") + ruleName + std::string(">"); } -int RuleTagToken::getType() const { +size_t RuleTagToken::getType() const { return bypassTokenType; } -int RuleTagToken::getLine() const { +size_t RuleTagToken::getLine() const { return 0; } -int RuleTagToken::getCharPositionInLine() const { - return -1; +size_t RuleTagToken::getCharPositionInLine() const { + return INVALID_INDEX; } -int RuleTagToken::getTokenIndex() const { - return -1; +size_t RuleTagToken::getTokenIndex() const { + return INVALID_INDEX; } -int RuleTagToken::getStartIndex() const { - return -1; +size_t RuleTagToken::getStartIndex() const { + return INVALID_INDEX; } -int RuleTagToken::getStopIndex() const { - return -1; +size_t RuleTagToken::getStopIndex() const { + return INVALID_INDEX; } antlr4::TokenSource *RuleTagToken::getTokenSource() const { diff --git a/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.h b/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.h index d22b21429..8e4b5ec24 100755 --- a/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.h +++ b/runtime/Cpp/runtime/src/tree/pattern/RuleTagToken.h @@ -48,16 +48,15 @@ namespace pattern { /// private: const std::string ruleName; - /// + /// The token type for the current token. This is the token type assigned to /// the bypass alternative for the rule during ATN deserialization. - /// - const int bypassTokenType; - /// + const size_t bypassTokenType; + /// This is the backing field for . - /// const std::string label; + public: /// /// Constructs a new instance of with the specified rule /// name and bypass token type and no label. @@ -67,8 +66,6 @@ namespace pattern { /// /// if {@code ruleName} is {@code null} /// or empty. - public: - RuleTagToken(const std::string &ruleName, int bypassTokenType); //this(ruleName, bypassTokenType, nullptr); /// @@ -82,7 +79,7 @@ namespace pattern { /// /// if {@code ruleName} is {@code null} /// or empty. - RuleTagToken(const std::string &ruleName, int bypassTokenType, const std::string &label); + RuleTagToken(const std::string &ruleName, size_t bypassTokenType, const std::string &label); /// /// Gets the name of the rule associated with this rule tag. @@ -112,69 +109,32 @@ namespace pattern { /// virtual std::string getText() const override; - /// - /// {@inheritDoc} - ///

/// Rule tag tokens have types assigned according to the rule bypass /// transitions created during ATN deserialization. - ///

- virtual int getType() const override; + virtual size_t getType() const override; - /// - /// {@inheritDoc} - ///

/// The implementation for always returns 0. - ///

- virtual int getLine() const override; + virtual size_t getLine() const override; - /// - /// {@inheritDoc} - ///

- /// The implementation for always returns -1. - ///

- virtual int getCharPositionInLine() const override; + /// The implementation for always returns INVALID_INDEX. + virtual size_t getCharPositionInLine() const override; - /// - /// {@inheritDoc} - ///

- /// The implementation for always returns -1. - ///

- virtual int getTokenIndex() const override; + /// The implementation for always returns INVALID_INDEX. + virtual size_t getTokenIndex() const override; - /// - /// {@inheritDoc} - ///

- /// The implementation for always returns -1. - ///

- virtual int getStartIndex() const override; + /// The implementation for always returns INVALID_INDEX. + virtual size_t getStartIndex() const override; - /// - /// {@inheritDoc} - ///

- /// The implementation for always returns -1. - ///

- virtual int getStopIndex() const override; + /// The implementation for always returns INVALID_INDEX. + virtual size_t getStopIndex() const override; - /// - /// {@inheritDoc} - ///

/// The implementation for always returns {@code null}. - ///

virtual TokenSource *getTokenSource() const override; - /// - /// {@inheritDoc} - ///

/// The implementation for always returns {@code null}. - ///

virtual CharStream *getInputStream() const override; - /// - /// {@inheritDoc} - ///

- /// The implementation for returns a string of the form - /// {@code ruleName:bypassTokenType}. - ///

+ /// The implementation for returns a string of the form {@code ruleName:bypassTokenType}. virtual std::string toString() const override; }; diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp b/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp index 39cf7f387..c81d51273 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp +++ b/runtime/Cpp/runtime/src/tree/xpath/XPath.cpp @@ -66,7 +66,7 @@ std::vector XPath::split(const std::string &path) { try { tokenStream.fill(); } catch (LexerNoViableAltException &) { - int pos = lexer.getCharPositionInLine(); + size_t pos = lexer.getCharPositionInLine(); std::string msg = "Invalid tokens or characters at index " + std::to_string(pos) + " in path '" + path + "'"; throw IllegalArgumentException(msg); } diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.cpp b/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.cpp index c4c28bd29..177c99255 100644 --- a/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.cpp +++ b/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.cpp @@ -44,7 +44,7 @@ const atn::ATN& XPathLexer::getATN() const { } -void XPathLexer::action(Ref const& context, int ruleIndex, int actionIndex) { +void XPathLexer::action(Ref const& context, size_t ruleIndex, size_t actionIndex) { switch (ruleIndex) { case 4: IDAction(std::dynamic_pointer_cast(context), actionIndex); break; @@ -53,7 +53,7 @@ void XPathLexer::action(Ref const& context, int ruleIndex, int acti } } -void XPathLexer::IDAction(Ref const& /*context*/, int actionIndex) { +void XPathLexer::IDAction(Ref const& /*context*/, size_t actionIndex) { switch (actionIndex) { case 0: if (isupper(getText()[0])) @@ -158,7 +158,7 @@ XPathLexer::Initializer::Initializer() { atn::ATNDeserializer deserializer; _atn = deserializer.deserialize(_serializedATN); - for (int i = 0; i < _atn.getNumberOfDecisions(); i++) { + for (size_t i = 0; i < _atn.getNumberOfDecisions(); i++) { _decisionToDFA.push_back(dfa::DFA(_atn.getDecisionState(i), i)); } } diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.h b/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.h index d853760b1..083be55a0 100644 --- a/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.h +++ b/runtime/Cpp/runtime/src/tree/xpath/XPathLexer.h @@ -29,7 +29,7 @@ public: virtual const std::vector getSerializedATN() const override; virtual const atn::ATN& getATN() const override; - virtual void action(Ref const& context, int ruleIndex, int actionIndex) override; + virtual void action(Ref const& context, size_t ruleIndex, size_t actionIndex) override; private: static std::vector _decisionToDFA; @@ -46,7 +46,7 @@ private: // Individual action functions triggered by action() above. - void IDAction(Ref const& context, int actionIndex); + void IDAction(Ref const& context, size_t actionIndex); // Individual semantic predicate functions triggered by sempred() above. diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.cpp b/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.cpp index 7aaf22c52..7f4dee9f2 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.cpp +++ b/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.cpp @@ -35,5 +35,5 @@ using namespace antlr4; using namespace antlr4::tree::xpath; void XPathLexerErrorListener::syntaxError(IRecognizer * /*recognizer*/, Token * /*offendingSymbol*/, - size_t /*line*/, int /*charPositionInLine*/, const std::string &/*msg*/, std::exception_ptr /*e*/) { + size_t /*line*/, size_t /*charPositionInLine*/, const std::string &/*msg*/, std::exception_ptr /*e*/) { } diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.h b/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.h index 18ff4e893..ac32f05e3 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.h +++ b/runtime/Cpp/runtime/src/tree/xpath/XPathLexerErrorListener.h @@ -40,7 +40,7 @@ namespace xpath { class ANTLR4CPP_PUBLIC XPathLexerErrorListener : public BaseErrorListener { public: virtual void syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, - int charPositionInLine, const std::string &msg, std::exception_ptr e) override; + size_t charPositionInLine, const std::string &msg, std::exception_ptr e) override; }; } // namespace xpath diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.cpp b/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.cpp index 9e5738284..d423b285b 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.cpp +++ b/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.cpp @@ -37,7 +37,7 @@ using namespace antlr4::tree; using namespace antlr4::tree::xpath; -XPathRuleElement::XPathRuleElement(const std::string &ruleName, int ruleIndex) : XPathElement(ruleName) { +XPathRuleElement::XPathRuleElement(const std::string &ruleName, size_t ruleIndex) : XPathElement(ruleName) { _ruleIndex = ruleIndex; } diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.h b/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.h index f234b9dab..d98764301 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.h +++ b/runtime/Cpp/runtime/src/tree/xpath/XPathRuleElement.h @@ -39,12 +39,12 @@ namespace xpath { class ANTLR4CPP_PUBLIC XPathRuleElement : public XPathElement { public: - XPathRuleElement(const std::string &ruleName, int ruleIndex); + XPathRuleElement(const std::string &ruleName, size_t ruleIndex); virtual std::vector> evaluate(const Ref &t) override; protected: - int _ruleIndex = 0; + size_t _ruleIndex = 0; }; } // namespace xpath diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.cpp b/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.cpp index 54cdd0cca..e4a16ab44 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.cpp +++ b/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.cpp @@ -40,7 +40,7 @@ using namespace antlr4; using namespace antlr4::tree; using namespace antlr4::tree::xpath; -XPathTokenElement::XPathTokenElement(const std::string &tokenName, int tokenType) : XPathElement(tokenName) { +XPathTokenElement::XPathTokenElement(const std::string &tokenName, size_t tokenType) : XPathElement(tokenName) { _tokenType = tokenType; } diff --git a/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.h b/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.h index 88e37dc42..b39a280c4 100755 --- a/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.h +++ b/runtime/Cpp/runtime/src/tree/xpath/XPathTokenElement.h @@ -39,12 +39,12 @@ namespace xpath { class ANTLR4CPP_PUBLIC XPathTokenElement : public XPathElement { public: - XPathTokenElement(const std::string &tokenName, int tokenType); + XPathTokenElement(const std::string &tokenName, size_t tokenType); virtual std::vector> evaluate(const std::shared_ptr &t) override; protected: - int _tokenType = 0; + size_t _tokenType = 0; }; } // namespace xpath 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 239427dd6..a7c2ff8aa 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 @@ -78,10 +78,10 @@ public: virtual const atn::ATN& getATN() const override; - virtual void action(Ref\ const& context, int ruleIndex, int actionIndex) override; + virtual void action(Ref\ const& context, size_t ruleIndex, size_t actionIndex) override; - virtual bool sempred(Ref\ const& _localctx, int ruleIndex, int predicateIndex) override; + virtual bool sempred(Ref\ const& _localctx, size_t ruleIndex, size_t predicateIndex) override; private: @@ -151,7 +151,7 @@ const atn::ATN& ::getATN() const { -void ::action(Ref\ const& context, int ruleIndex, int actionIndex) { +void ::action(Ref\ const& context, size_t ruleIndex, size_t actionIndex) { switch (ruleIndex) { : Action(std::dynamic_pointer_cast\<\>(context), actionIndex); break;}; separator="\n"> @@ -162,7 +162,7 @@ void ::action(Ref\ const& context, int ruleIndex, int a -bool ::sempred(Ref\ const& context, int ruleIndex, int predicateIndex) { +bool ::sempred(Ref\ const& context, size_t ruleIndex, size_t predicateIndex) { switch (ruleIndex) { : return Sempred(std::dynamic_pointer_cast\<\>(context), predicateIndex);}; separator="\n"> @@ -227,11 +227,11 @@ std::vector\ ::_tokenNames; >> RuleActionFunctionHeader(r, actions) ::= << -void Action(Ref\<\> const& context, int actionIndex); +void Action(Ref\<\> const& context, size_t actionIndex); >> RuleActionFunction(r, actions) ::= << -void ::Action(Ref\<\> const& context, int actionIndex) { +void ::Action(Ref\<\> const& context, size_t actionIndex) { switch (actionIndex) { : break;}; separator="\n"> @@ -243,13 +243,13 @@ void ::Action(Ref\<\> const& context, >> RuleSempredFunctionHeader(r, actions) ::= << -bool Sempred(Ref\<\> const& _localctx, int predicateIndex); +bool Sempred(Ref\<\> const& _localctx, size_t predicateIndex); >> RuleSempredFunction(r, actions) ::= << -bool ::Sempred(Ref\<\> const& _localctx, int predicateIndex) { +bool ::Sempred(Ref\<\> const& _localctx, size_t predicateIndex) { switch (predicateIndex) { : return }; separator=";\n">; @@ -296,7 +296,7 @@ public: - virtual bool sempred(Ref\ const& _localctx, int ruleIndex, int predicateIndex) override; + virtual bool sempred(Ref\ const& _localctx, size_t ruleIndex, size_t predicateIndex) override; @@ -348,7 +348,7 @@ dfa::Vocabulary& ::getVocabulary() const { -bool ::sempred(Ref\ const& context, int ruleIndex, int predicateIndex) { +bool ::sempred(Ref\ const& context, size_t ruleIndex, size_t predicateIndex) { switch (ruleIndex) { : return Sempred(std::dynamic_pointer_cast\<\>(context), predicateIndex);}; separator="\n"> @@ -431,7 +431,7 @@ _serializedATN = { atn::ATNDeserializer deserializer; _atn = deserializer.deserialize(_serializedATN); -for (int i = 0; i \< _atn.getNumberOfDecisions(); i++) { +for (size_t i = 0; i \< _atn.getNumberOfDecisions(); i++) { _decisionToDFA.push_back(dfa::DFA(_atn.getDecisionState(i), i)); } >> @@ -458,7 +458,7 @@ Ref\<::\> ::( - int alt; + size_t alt; @@ -495,21 +495,21 @@ Ref\<::\> ::(::\> ::(int precedence}>) { Ref\ parentContext = _ctx; - int parentState = getState(); + size_t parentState = getState(); Ref\<::\> _localctx = std::make_shared\<\>(_ctx, parentState}>); Ref\<::\> previousContext = _localctx; - int startState = ; + size_t startState = ; enterRecursionRule(_localctx, , ::Rule, precedence); auto onExit = finally([=] { - + unrollRecursionContexts(parentContext); }); try { - int alt; + size_t alt; @@ -527,15 +527,15 @@ StructDeclHeader(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, class : public ParserRuleContext, { public: ;}; separator="\n"> - (std::weak_ptr\ parent, int invokingState); - (std::weak_ptr\ parent, int invokingState}>); + (std::weak_ptr\ parent, size_t invokingState); + (std::weak_ptr\ parent, size_t invokingState}>); () { } void copyFrom(Ref\<\> const& context); using ParserRuleContext::copyFrom; - virtual ssize_t getRuleIndex() const override; + virtual size_t getRuleIndex() const override; }; separator = "\n"> @@ -548,19 +548,19 @@ StructDecl(struct, ctorAttrs, attrs, getters, dispatchMethods, interfaces, exten //----------------- ------------------------------------------------------------------ -::::(std::weak_ptr\ parent, int invokingState) +::::(std::weak_ptr\ parent, size_t invokingState) : ParserRuleContext(parent, invokingState) { } -::::(std::weak_ptr\ parent, int invokingState}>) +::::(std::weak_ptr\ parent, size_t invokingState}>) : ParserRuleContext(parent, invokingState) { = ;}; separator="\n"> } }; separator = "\n"> -ssize_t ::::getRuleIndex() const { +size_t ::::getRuleIndex() const { return ::Rule; } @@ -761,7 +761,7 @@ TestSetInline(s) ::= << // Java language spec 15.19 - shift operators mask operands rather than overflow to 0... need range test testShiftInRange(shiftAmount) ::= << -(( & ~(ssize_t)0x3f) == 0) +(( & ~(size_t)0x3f) == 0) >> // produces smaller bytecode only when bits.ttypes contains more than two items @@ -948,7 +948,7 @@ TokenDeclHeader(t) ::= " = nullptr;" TokenDecl(t) ::= "" TokenTypeDeclHeader(t) ::= "" -TokenTypeDecl(t) ::= "ssize_t = 0;" +TokenTypeDecl(t) ::= "size_t = 0;" TokenListDeclHeader(t) ::= "std::vector\ ;" TokenListDecl(t) ::= "" @@ -975,10 +975,10 @@ std::vector\\> ::::() >> -ContextTokenListIndexedGetterDeclHeader(t) ::= "Ref\ (int i);" +ContextTokenListIndexedGetterDeclHeader(t) ::= "Ref\ (size_t i);" ContextTokenListIndexedGetterDecl(t) ::= << -Ref\ ::::(int i) { - return getToken(::, (size_t)i); +Ref\ ::::(size_t i) { + return getToken(::, i); } >> @@ -1000,10 +1000,10 @@ std::vector\::\>> ::::> -ContextRuleListIndexedGetterDeclHeader(r) ::= "Ref\<> (int i);" +ContextRuleListIndexedGetterDeclHeader(r) ::= "Ref\<> (size_t i);" ContextRuleListIndexedGetterDecl(r) ::= << -Ref\<::\> ::::(int i) { - return getRuleContext\<::\>((size_t)i); +Ref\<::\> ::::(size_t i) { + return getRuleContext\<::\>(i); } >>