forked from jasder/antlr
Applied consistent exception model and fixed is<> helper.
- Exceptions are now consistently thrown by value and captured by reference. C++11 exception_ptr and nested_exception are used when exception references are neeeded or when implementing the equivalent of Java's nesting. - The is<> helper didn't handle properly (const) references, which is now explicitly handled. Added new unit tests for that. - Fixed a number of places where a catch all was used to implement a "finally" (which hides exceptions). - Changed exceptions to hold (temporary) raw pointers instead of shared pointers, as otherwise it is tried to free wrapped pointers which might just be references to static objects. Might later be updated again when we continue with removing raw pointers. - Some smaller fixes. - The generated simple parser now runs through without any error (yet, it doesn't do anything useful). - ANTLR C++ target template: - Added getListener and genVisitor bool members to ANTLR's LexerFile + ParserFile classes, so can use them in the template. - Made addition of listener #include dependent on the new genListener member, which allows to run parser generation without listeners/visitors.
This commit is contained in:
parent
091c40899c
commit
7f8ad7bd2d
|
@ -99,9 +99,10 @@ using namespace org::antlr::v4::runtime::misc;
|
|||
try {
|
||||
stream.consume();
|
||||
XCTFail();
|
||||
} catch (const IllegalStateException &e) {
|
||||
} catch (IllegalStateException &e) {
|
||||
// Expected.
|
||||
XCTAssertEqual(e.getMessage(), "cannot consume EOF");
|
||||
std::string message = e.what();
|
||||
XCTAssertEqual(message, "cannot consume EOF");
|
||||
}
|
||||
|
||||
XCTAssertEqual(stream.index(), text.size());
|
||||
|
|
|
@ -66,6 +66,7 @@ using namespace antlrcpp;
|
|||
|
||||
{
|
||||
A a; B b; C c; D d;
|
||||
const A &cc = d;
|
||||
XCTAssert(is<A>(b));
|
||||
XCTAssertFalse(is<B>(a));
|
||||
XCTAssert(is<A>(c));
|
||||
|
@ -73,6 +74,24 @@ using namespace antlrcpp;
|
|||
XCTAssert(is<A>(d));
|
||||
XCTAssert(is<C>(d));
|
||||
XCTAssertFalse(is<B>(d));
|
||||
XCTAssert(is<C>(cc));
|
||||
|
||||
auto isA = [&](const A &aa) { XCTAssert(is<A>(aa)); };
|
||||
isA(a);
|
||||
isA(b);
|
||||
isA(c);
|
||||
isA(d);
|
||||
|
||||
auto isC = [&](const A &aa, bool mustBeTrue) {
|
||||
if (mustBeTrue)
|
||||
XCTAssert(is<C>(aa));
|
||||
else
|
||||
XCTAssertFalse(is<C>(aa));
|
||||
};
|
||||
isC(a, false);
|
||||
isC(b, false);
|
||||
isC(c, true);
|
||||
isC(d, true);
|
||||
}
|
||||
{
|
||||
A *a = new A(); B *b = new B(); C *c = new C(); D *d = new D();
|
||||
|
|
|
@ -61,7 +61,7 @@ using namespace org::antlr::v4::runtime;
|
|||
|
||||
org::antlr::v4::runtime::atn::ParserATNSimulator foo(nullptr, atn, decisionToDFA, nullptr);
|
||||
}
|
||||
catch (std::exception e) {
|
||||
catch (std::exception &e) {
|
||||
|
||||
XCTAssert(NO, @"Fail");
|
||||
}
|
||||
|
|
|
@ -31,6 +31,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "RecognitionException.h"
|
||||
|
||||
namespace antlrcpp {
|
||||
class BitSet;
|
||||
}
|
||||
|
@ -42,7 +44,7 @@ namespace runtime {
|
|||
|
||||
/// How to emit recognition errors (an interface in Java).
|
||||
class ANTLRErrorListener {
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Upon syntax error, notify any interested parties. This is not how to
|
||||
/// recover from errors or compute error messages. <seealso cref="ANTLRErrorStrategy"/>
|
||||
|
@ -75,9 +77,8 @@ namespace runtime {
|
|||
/// the reporting of an error. It is null in the case where
|
||||
/// the parser was able to recover in line without exiting the
|
||||
/// surrounding rule. </param>
|
||||
public:
|
||||
virtual void syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, int charPositionInLine,
|
||||
const std::wstring &msg, RecognitionException *e) = 0;
|
||||
const std::wstring &msg, std::exception_ptr e) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// This method is called by the parser when a full-context prediction
|
||||
|
|
|
@ -90,7 +90,7 @@ namespace runtime {
|
|||
/// <param name="e"> the recognition exception to recover from </param>
|
||||
/// <exception cref="RecognitionException"> if the error strategy could not recover from
|
||||
/// the recognition exception </exception>
|
||||
virtual void recover(Parser *recognizer, RecognitionException *e) = 0;
|
||||
virtual void recover(Parser *recognizer, const RecognitionException &e) = 0;
|
||||
|
||||
/// <summary>
|
||||
/// This method provides the error handler with an opportunity to handle
|
||||
|
@ -137,7 +137,7 @@ namespace runtime {
|
|||
/// </summary>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
/// <param name="e"> the recognition exception to report </param>
|
||||
virtual void reportError(Parser *recognizer, RecognitionException *e) = 0;
|
||||
virtual void reportError(Parser *recognizer, const RecognitionException &e) = 0;
|
||||
};
|
||||
|
||||
} // namespace runtime
|
||||
|
|
|
@ -38,25 +38,33 @@
|
|||
|
||||
using namespace org::antlr::v4::runtime;
|
||||
|
||||
void BailErrorStrategy::recover(Parser *recognizer, RecognitionException *e) {
|
||||
void BailErrorStrategy::recover(Parser *recognizer, const RecognitionException &e) {
|
||||
std::exception_ptr exception = std::make_exception_ptr(e);
|
||||
for (ParserRuleContext *context = recognizer->getContext(); context != nullptr; context = context->getParent()) {
|
||||
context->exception = e;
|
||||
context->exception = exception;
|
||||
}
|
||||
|
||||
throw ParseCancellationException(e);
|
||||
try {
|
||||
std::rethrow_exception(exception); // Throw the exception to be able to catch and rethrow nested.
|
||||
} catch (RecognitionException &inner) {
|
||||
std::throw_with_nested(ParseCancellationException());
|
||||
}
|
||||
}
|
||||
|
||||
Token *BailErrorStrategy::recoverInline(Parser *recognizer) {
|
||||
|
||||
InputMismatchException *e = new InputMismatchException(recognizer);
|
||||
InputMismatchException e(recognizer);
|
||||
for (ParserRuleContext *context = recognizer->getContext();
|
||||
context != nullptr;
|
||||
context = context->getParent()) {
|
||||
context->exception = e;
|
||||
context->exception = std::make_exception_ptr(e);
|
||||
}
|
||||
|
||||
throw ParseCancellationException(e);
|
||||
|
||||
try {
|
||||
throw e;
|
||||
} catch (InputMismatchException &inner) {
|
||||
std::throw_with_nested(ParseCancellationException());
|
||||
}
|
||||
}
|
||||
|
||||
void BailErrorStrategy::sync(Parser *recognizer) {
|
||||
|
|
|
@ -51,7 +51,7 @@ namespace runtime {
|
|||
/// original <seealso cref="RecognitionException"/>.
|
||||
/// </summary>
|
||||
public:
|
||||
virtual void recover(Parser *recognizer, RecognitionException *e) override;
|
||||
virtual void recover(Parser *recognizer, const RecognitionException &e) override;
|
||||
|
||||
/// <summary>
|
||||
/// Make sure we don't attempt to recover inline; if the parser
|
||||
|
|
|
@ -30,11 +30,12 @@
|
|||
*/
|
||||
|
||||
#include "BaseErrorListener.h"
|
||||
#include "RecognitionException.h"
|
||||
|
||||
using namespace org::antlr::v4::runtime;
|
||||
|
||||
void BaseErrorListener::syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, int charPositionInLine,
|
||||
const std::wstring &msg, RecognitionException *e) {
|
||||
const std::wstring &msg, std::exception_ptr e) {
|
||||
}
|
||||
|
||||
void BaseErrorListener::reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex,
|
||||
|
|
|
@ -45,7 +45,7 @@ namespace runtime {
|
|||
class BaseErrorListener : public ANTLRErrorListener {
|
||||
|
||||
virtual void syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, int charPositionInLine,
|
||||
const std::wstring &msg, RecognitionException *e) override;
|
||||
const std::wstring &msg, std::exception_ptr e) override;
|
||||
|
||||
virtual void reportAmbiguity(Parser *recognizer, dfa::DFA *dfa, size_t startIndex, size_t stopIndex, bool exact,
|
||||
antlrcpp::BitSet *ambigAlts, atn::ATNConfigSet *configs) override;
|
||||
|
|
|
@ -84,7 +84,7 @@ CommonToken::CommonToken(Token *oldToken) {
|
|||
}
|
||||
}
|
||||
|
||||
int CommonToken::getType() {
|
||||
int CommonToken::getType() const {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
|
|
@ -77,7 +77,7 @@ namespace runtime {
|
|||
|
||||
CommonToken(Token *oldToken);
|
||||
|
||||
virtual int getType() override;
|
||||
virtual int getType() const override;
|
||||
|
||||
virtual void setLine(int line) override;
|
||||
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
#include "DefaultErrorStrategy.h"
|
||||
|
||||
using namespace org::antlr::v4::runtime;
|
||||
using namespace antlrcpp;
|
||||
|
||||
void DefaultErrorStrategy::reset(Parser *recognizer) {
|
||||
endErrorCondition(recognizer);
|
||||
|
@ -67,32 +68,30 @@ void DefaultErrorStrategy::reportMatch(Parser *recognizer) {
|
|||
endErrorCondition(recognizer);
|
||||
}
|
||||
|
||||
void DefaultErrorStrategy::reportError(Parser *recognizer, RecognitionException *e) {
|
||||
void DefaultErrorStrategy::reportError(Parser *recognizer, const RecognitionException &e) {
|
||||
// if we've already reported an error and have not matched a token
|
||||
// yet successfully, don't report any errors.
|
||||
if (inErrorRecoveryMode(recognizer)) {
|
||||
// System.err.print("[SPURIOUS] ");
|
||||
return; // don't report spurious errors
|
||||
}
|
||||
|
||||
beginErrorCondition(recognizer);
|
||||
if (dynamic_cast<NoViableAltException*>(e) != nullptr) {
|
||||
reportNoViableAlternative(recognizer, static_cast<NoViableAltException*>(e));
|
||||
} else if (dynamic_cast<InputMismatchException*>(e) != nullptr) {
|
||||
reportInputMismatch(recognizer, static_cast<InputMismatchException*>(e));
|
||||
} else if (dynamic_cast<FailedPredicateException*>(e) != nullptr) {
|
||||
reportFailedPredicate(recognizer, dynamic_cast<FailedPredicateException*>(e));
|
||||
if (is<NoViableAltException>(e)) {
|
||||
reportNoViableAlternative(recognizer, (NoViableAltException&)e);
|
||||
} else if (is<const InputMismatchException>(e)) {
|
||||
reportInputMismatch(recognizer, (InputMismatchException&)e);
|
||||
} else if (is<const FailedPredicateException>(e)) {
|
||||
reportFailedPredicate(recognizer, (FailedPredicateException&)e);
|
||||
} else {
|
||||
|
||||
// This is really bush league, I hate libraries that gratuitiously print
|
||||
// stuff out
|
||||
std::cerr << std::string("unknown recognition error type: ") + typeid(e).name();
|
||||
|
||||
recognizer->notifyErrorListeners(e->getOffendingToken().get(), antlrcpp::s2ws(e->what()), e);
|
||||
// This is really bush league, I hate libraries that gratuitiously print stuff out.
|
||||
std::cerr << std::string("unknown recognition error type: ") << typeid(e).name() << std::endl;
|
||||
|
||||
recognizer->notifyErrorListeners(e.getOffendingToken(), antlrcpp::s2ws(e.what()), std::make_exception_ptr(e));
|
||||
}
|
||||
}
|
||||
|
||||
void DefaultErrorStrategy::recover(Parser *recognizer, RecognitionException *e) {
|
||||
void DefaultErrorStrategy::recover(Parser *recognizer, const RecognitionException &e) {
|
||||
if (lastErrorIndex == (int)recognizer->getInputStream()->index() && lastErrorStates != nullptr &&
|
||||
lastErrorStates->contains(recognizer->getState())) {
|
||||
|
||||
|
@ -160,31 +159,32 @@ void DefaultErrorStrategy::sync(Parser *recognizer) {
|
|||
}
|
||||
}
|
||||
|
||||
void DefaultErrorStrategy::reportNoViableAlternative(Parser *recognizer, NoViableAltException *e) {
|
||||
void DefaultErrorStrategy::reportNoViableAlternative(Parser *recognizer, const NoViableAltException &e) {
|
||||
TokenStream *tokens = recognizer->getInputStream();
|
||||
std::wstring input;
|
||||
if (tokens != nullptr) {
|
||||
if (e->getStartToken()->getType() == EOF) {
|
||||
if (e.getStartToken()->getType() == EOF) {
|
||||
input = L"<EOF>";
|
||||
} else {
|
||||
input = tokens->getText(e->getStartToken().get(), e->getOffendingToken().get());
|
||||
input = tokens->getText(e.getStartToken(), e.getOffendingToken());
|
||||
}
|
||||
} else {
|
||||
input = L"<unknown input>";
|
||||
}
|
||||
std::wstring msg = std::wstring(L"no viable alternative at input ") + escapeWSAndQuote(input);
|
||||
recognizer->notifyErrorListeners(e->getOffendingToken().get(), msg, e);
|
||||
recognizer->notifyErrorListeners(e.getOffendingToken(), msg, std::make_exception_ptr(e));
|
||||
}
|
||||
|
||||
void DefaultErrorStrategy::reportInputMismatch(Parser *recognizer, InputMismatchException *e) {
|
||||
std::wstring msg = std::wstring(L"mismatched input ") + getTokenErrorDisplay(e->getOffendingToken().get()) + std::wstring(L" expecting ") + e->getExpectedTokens().toString(recognizer->getTokenNames());
|
||||
recognizer->notifyErrorListeners(e->getOffendingToken().get(), msg, e);
|
||||
void DefaultErrorStrategy::reportInputMismatch(Parser *recognizer, const InputMismatchException &e) {
|
||||
std::wstring msg = std::wstring(L"mismatched input ") + getTokenErrorDisplay(e.getOffendingToken()) +
|
||||
std::wstring(L" expecting ") + e.getExpectedTokens().toString(recognizer->getTokenNames());
|
||||
recognizer->notifyErrorListeners(e.getOffendingToken(), msg, std::make_exception_ptr(e));
|
||||
}
|
||||
|
||||
void DefaultErrorStrategy::reportFailedPredicate(Parser *recognizer, FailedPredicateException *e) {
|
||||
void DefaultErrorStrategy::reportFailedPredicate(Parser *recognizer, const FailedPredicateException &e) {
|
||||
const std::wstring& ruleName = recognizer->getRuleNames()[(size_t)recognizer->ctx->getRuleIndex()];
|
||||
std::wstring msg = std::wstring(L"rule ") + ruleName + std::wstring(L" ") + antlrcpp::s2ws(e->getMessage());
|
||||
recognizer->notifyErrorListeners(e->getOffendingToken().get(), msg, e);
|
||||
std::wstring msg = std::wstring(L"rule ") + ruleName + std::wstring(L" ") + antlrcpp::s2ws(e.what());
|
||||
recognizer->notifyErrorListeners(e.getOffendingToken(), msg, std::make_exception_ptr(e));
|
||||
}
|
||||
|
||||
void DefaultErrorStrategy::reportUnwantedToken(Parser *recognizer) {
|
||||
|
|
|
@ -127,7 +127,7 @@ namespace runtime {
|
|||
/// the exception</li>
|
||||
/// </ul>
|
||||
/// </summary>
|
||||
virtual void reportError(Parser *recognizer, RecognitionException *e) override;
|
||||
virtual void reportError(Parser *recognizer, const RecognitionException &e) override;
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
|
@ -136,7 +136,7 @@ namespace runtime {
|
|||
/// until we find one in the resynchronization set--loosely the set of tokens
|
||||
/// that can follow the current rule.
|
||||
/// </summary>
|
||||
virtual void recover(Parser *recognizer, RecognitionException *e) override;
|
||||
virtual void recover(Parser *recognizer, const RecognitionException &e) override;
|
||||
|
||||
/// <summary>
|
||||
/// The default implementation of <seealso cref="ANTLRErrorStrategy#sync"/> makes sure
|
||||
|
@ -195,7 +195,7 @@ namespace runtime {
|
|||
/// <param name="recognizer"> the parser instance </param>
|
||||
/// <param name="e"> the recognition exception </param>
|
||||
protected:
|
||||
virtual void reportNoViableAlternative(Parser *recognizer, NoViableAltException *e);
|
||||
virtual void reportNoViableAlternative(Parser *recognizer, const NoViableAltException &e);
|
||||
|
||||
/// <summary>
|
||||
/// This is called by <seealso cref="#reportError"/> when the exception is an
|
||||
|
@ -205,7 +205,7 @@ namespace runtime {
|
|||
/// </seealso>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
/// <param name="e"> the recognition exception </param>
|
||||
virtual void reportInputMismatch(Parser *recognizer, InputMismatchException *e);
|
||||
virtual void reportInputMismatch(Parser *recognizer, const InputMismatchException &e);
|
||||
|
||||
/// <summary>
|
||||
/// This is called by <seealso cref="#reportError"/> when the exception is a
|
||||
|
@ -215,7 +215,7 @@ namespace runtime {
|
|||
/// </seealso>
|
||||
/// <param name="recognizer"> the parser instance </param>
|
||||
/// <param name="e"> the recognition exception </param>
|
||||
virtual void reportFailedPredicate(Parser *recognizer, FailedPredicateException *e);
|
||||
virtual void reportFailedPredicate(Parser *recognizer, const FailedPredicateException &e);
|
||||
|
||||
/// <summary>
|
||||
/// This method is called to report a syntax error which requires the removal
|
||||
|
|
|
@ -32,18 +32,8 @@
|
|||
|
||||
using namespace org::antlr::v4::runtime;
|
||||
|
||||
RuntimeException::RuntimeException(RuntimeException *cause) : RuntimeException("", cause) {
|
||||
}
|
||||
|
||||
RuntimeException::RuntimeException(const std::string &msg, RuntimeException *cause)
|
||||
: std::exception(), _message(msg), _cause(cause) {
|
||||
}
|
||||
|
||||
std::string RuntimeException::getMessage() const {
|
||||
return _message;
|
||||
}
|
||||
std::shared_ptr<RuntimeException> RuntimeException::getCause() const {
|
||||
return _cause;
|
||||
RuntimeException::RuntimeException(const std::string &msg)
|
||||
: std::exception(), _message(msg) {
|
||||
}
|
||||
|
||||
const char* RuntimeException::what() const noexcept {
|
||||
|
@ -52,17 +42,7 @@ const char* RuntimeException::what() const noexcept {
|
|||
|
||||
//------------------ IOException ---------------------------------------------------------------------------------------
|
||||
|
||||
IOException::IOException(RuntimeException *cause) : IOException("", cause) {
|
||||
}
|
||||
|
||||
IOException::IOException(const std::string &msg, RuntimeException *cause) : std::exception(), _message(msg), _cause(cause) {
|
||||
}
|
||||
|
||||
std::string IOException::getMessage() const {
|
||||
return _message;
|
||||
}
|
||||
std::shared_ptr<RuntimeException> IOException::getCause() const {
|
||||
return _cause;
|
||||
IOException::IOException(const std::string &msg) : std::exception(), _message(msg) {
|
||||
}
|
||||
|
||||
const char* IOException::what() const noexcept {
|
||||
|
|
|
@ -39,52 +39,40 @@ namespace runtime {
|
|||
class RuntimeException : public std::exception {
|
||||
private:
|
||||
std::string _message;
|
||||
std::shared_ptr<RuntimeException> _cause; // Optionally assigned if this exception is wrapping another one.
|
||||
|
||||
public:
|
||||
RuntimeException(RuntimeException *cause = nullptr);
|
||||
RuntimeException(const std::string &msg, RuntimeException *cause = nullptr);
|
||||
|
||||
std::string getMessage() const;
|
||||
std::shared_ptr<RuntimeException> getCause() const;
|
||||
RuntimeException(const std::string &msg = "");
|
||||
|
||||
virtual const char* what() const noexcept override;
|
||||
};
|
||||
|
||||
class IllegalStateException : public RuntimeException {
|
||||
public:
|
||||
IllegalStateException(RuntimeException *cause = nullptr) : IllegalStateException("", cause) {};
|
||||
IllegalStateException(const std::string &msg, RuntimeException *cause = nullptr) : RuntimeException(msg, cause) {};
|
||||
IllegalStateException(const std::string &msg = "") : RuntimeException(msg) {};
|
||||
};
|
||||
|
||||
class IllegalArgumentException : public RuntimeException {
|
||||
public:
|
||||
IllegalArgumentException(RuntimeException *cause = nullptr) : IllegalArgumentException("", cause) {};
|
||||
IllegalArgumentException(const std::string &msg, RuntimeException *cause = nullptr) : RuntimeException(msg, cause) {};
|
||||
IllegalArgumentException(const std::string &msg = "") : RuntimeException(msg) {};
|
||||
};
|
||||
|
||||
class NullPointerException : public RuntimeException {
|
||||
public:
|
||||
NullPointerException(RuntimeException *cause = nullptr) : NullPointerException("", cause) {};
|
||||
NullPointerException(const std::string &msg, RuntimeException *cause = nullptr) : RuntimeException(msg, cause) {};
|
||||
NullPointerException(const std::string &msg = "") : RuntimeException(msg) {};
|
||||
};
|
||||
|
||||
class IndexOutOfBoundsException : public RuntimeException {
|
||||
public:
|
||||
IndexOutOfBoundsException(RuntimeException *cause = nullptr) : IndexOutOfBoundsException("", cause) {};
|
||||
IndexOutOfBoundsException(const std::string &msg, RuntimeException *cause = nullptr) : RuntimeException(msg, cause) {};
|
||||
IndexOutOfBoundsException(const std::string &msg = "") : RuntimeException(msg) {};
|
||||
};
|
||||
|
||||
class UnsupportedOperationException : public RuntimeException {
|
||||
public:
|
||||
UnsupportedOperationException(RuntimeException *cause = nullptr) : UnsupportedOperationException("", cause) {};
|
||||
UnsupportedOperationException(const std::string &msg, RuntimeException *cause = nullptr) : RuntimeException(msg, cause) {};
|
||||
UnsupportedOperationException(const std::string &msg = "") : RuntimeException(msg) {};
|
||||
};
|
||||
|
||||
class EmptyStackException : public RuntimeException {
|
||||
public:
|
||||
EmptyStackException(RuntimeException *cause = nullptr) : EmptyStackException("", cause) {};
|
||||
EmptyStackException(const std::string &msg, RuntimeException *cause = nullptr) : RuntimeException(msg, cause) {};
|
||||
EmptyStackException(const std::string &msg = "") : RuntimeException(msg) {};
|
||||
};
|
||||
|
||||
// IOException is not a runtime exception (in the java hierarchy).
|
||||
|
@ -92,28 +80,21 @@ namespace runtime {
|
|||
class IOException : public std::exception {
|
||||
private:
|
||||
std::string _message;
|
||||
std::shared_ptr<RuntimeException> _cause; // Optionally assigned if this exception is wrapping another one.
|
||||
|
||||
public:
|
||||
IOException(RuntimeException *cause = nullptr);
|
||||
IOException(const std::string &msg, RuntimeException *cause = nullptr);
|
||||
|
||||
std::string getMessage() const;
|
||||
std::shared_ptr<RuntimeException> getCause() const;
|
||||
IOException(const std::string &msg = "");
|
||||
|
||||
virtual const char* what() const noexcept override;
|
||||
};
|
||||
|
||||
class CancellationException : public IllegalStateException {
|
||||
public:
|
||||
CancellationException(RuntimeException *cause = nullptr) : CancellationException("", cause) {};
|
||||
CancellationException(const std::string &msg, RuntimeException *cause = nullptr) : IllegalStateException(msg, cause) {};
|
||||
CancellationException(const std::string &msg = "") : IllegalStateException(msg) {};
|
||||
};
|
||||
|
||||
class ParseCancellationException : public CancellationException {
|
||||
public:
|
||||
ParseCancellationException(RuntimeException *cause = nullptr) : ParseCancellationException("", cause) {};
|
||||
ParseCancellationException(const std::string &msg, RuntimeException *cause = nullptr) : CancellationException(msg, cause) {};
|
||||
ParseCancellationException(const std::string &msg = "") : CancellationException(msg) {};
|
||||
};
|
||||
|
||||
} // namespace runtime
|
||||
|
|
|
@ -106,7 +106,7 @@ Token *Lexer::nextToken() {
|
|||
int ttype;
|
||||
try {
|
||||
ttype = getInterpreter<atn::LexerATNSimulator>()->match(_input, (size_t)_mode);
|
||||
} catch (LexerNoViableAltException *e) {
|
||||
} catch (LexerNoViableAltException &e) {
|
||||
notifyListeners(e); // report error
|
||||
recover(e);
|
||||
ttype = SKIP;
|
||||
|
@ -271,19 +271,19 @@ std::vector<Token*> Lexer::getAllTokens() {
|
|||
return tokens;
|
||||
}
|
||||
|
||||
void Lexer::recover(LexerNoViableAltException *e) {
|
||||
void Lexer::recover(const LexerNoViableAltException &e) {
|
||||
if (_input->LA(1) != EOF) {
|
||||
// skip a char and try again
|
||||
getInterpreter<atn::LexerATNSimulator>()->consume(_input);
|
||||
}
|
||||
}
|
||||
|
||||
void Lexer::notifyListeners(LexerNoViableAltException *e) {
|
||||
void Lexer::notifyListeners(const LexerNoViableAltException &e) {
|
||||
std::wstring text = _input->getText(misc::Interval(_tokenStartCharIndex, (int)_input->index()));
|
||||
std::wstring msg = std::wstring(L"token recognition error at: '") + getErrorDisplay(text) + std::wstring(L"'");
|
||||
|
||||
ANTLRErrorListener *listener = getErrorListenerDispatch();
|
||||
listener->syntaxError(this, nullptr, (size_t)_tokenStartLine, _tokenStartCharPositionInLine, msg, e);
|
||||
listener->syntaxError(this, nullptr, (size_t)_tokenStartLine, _tokenStartCharPositionInLine, msg, std::make_exception_ptr(e));
|
||||
}
|
||||
|
||||
std::wstring Lexer::getErrorDisplay(const std::wstring &s) {
|
||||
|
|
|
@ -217,9 +217,9 @@ namespace runtime {
|
|||
/// </summary>
|
||||
virtual std::vector<Token*> getAllTokens();
|
||||
|
||||
virtual void recover(LexerNoViableAltException *e);
|
||||
virtual void recover(const LexerNoViableAltException &e);
|
||||
|
||||
virtual void notifyListeners(LexerNoViableAltException *e);
|
||||
virtual void notifyListeners(const LexerNoViableAltException &e);
|
||||
|
||||
virtual std::wstring getErrorDisplay(const std::wstring &s);
|
||||
|
||||
|
|
|
@ -46,14 +46,14 @@ size_t LexerNoViableAltException::getStartIndex() {
|
|||
return _startIndex;
|
||||
}
|
||||
|
||||
std::shared_ptr<atn::ATNConfigSet> LexerNoViableAltException::getDeadEndConfigs() {
|
||||
atn::ATNConfigSet* LexerNoViableAltException::getDeadEndConfigs() {
|
||||
return _deadEndConfigs;
|
||||
}
|
||||
|
||||
std::wstring LexerNoViableAltException::toString() {
|
||||
std::wstring symbol;
|
||||
if (_startIndex < getInputStream()->size()) {
|
||||
symbol = ((CharStream *)getInputStream().get())->getText(misc::Interval((int)_startIndex, (int)_startIndex));
|
||||
symbol = ((CharStream *)getInputStream())->getText(misc::Interval((int)_startIndex, (int)_startIndex));
|
||||
symbol = antlrcpp::escapeWhitespace(symbol, false);
|
||||
}
|
||||
std::wstring format = L"LexerNoViableAltException('" + symbol + L"')";
|
||||
|
|
|
@ -44,7 +44,7 @@ namespace runtime {
|
|||
LexerNoViableAltException(Lexer *lexer, CharStream *input, size_t startIndex, atn::ATNConfigSet *deadEndConfigs);
|
||||
|
||||
virtual size_t getStartIndex();
|
||||
virtual std::shared_ptr<atn::ATNConfigSet> getDeadEndConfigs();
|
||||
virtual atn::ATNConfigSet* getDeadEndConfigs();
|
||||
virtual std::wstring toString();
|
||||
|
||||
private:
|
||||
|
@ -52,7 +52,7 @@ namespace runtime {
|
|||
const size_t _startIndex;
|
||||
|
||||
/// Which configurations did we try at input.index() that couldn't match input.LA(1)?
|
||||
std::shared_ptr<atn::ATNConfigSet> _deadEndConfigs;
|
||||
atn::ATNConfigSet* _deadEndConfigs;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -42,13 +42,13 @@ NoViableAltException::NoViableAltException(Parser *recognizer)
|
|||
|
||||
NoViableAltException::NoViableAltException(Parser *recognizer, TokenStream *input, Token *startToken,
|
||||
Token *offendingToken, atn::ATNConfigSet *deadEndConfigs, ParserRuleContext *ctx)
|
||||
: RecognitionException(recognizer, input, ctx, offendingToken), deadEndConfigs(deadEndConfigs), startToken(startToken) {
|
||||
: RecognitionException(recognizer, input, ctx, offendingToken), _deadEndConfigs(deadEndConfigs), _startToken(startToken) {
|
||||
}
|
||||
|
||||
std::shared_ptr<Token> NoViableAltException::getStartToken() {
|
||||
return startToken;
|
||||
Token* NoViableAltException::getStartToken() const {
|
||||
return _startToken;
|
||||
}
|
||||
|
||||
std::shared_ptr<atn::ATNConfigSet> NoViableAltException::getDeadEndConfigs() {
|
||||
return deadEndConfigs;
|
||||
atn::ATNConfigSet* NoViableAltException::getDeadEndConfigs() const {
|
||||
return _deadEndConfigs;
|
||||
}
|
||||
|
|
|
@ -45,24 +45,24 @@ namespace runtime {
|
|||
/// of the offending input and also knows where the parser was
|
||||
/// in the various paths when the error. Reported by reportNoViableAlternative()
|
||||
class NoViableAltException : public RecognitionException {
|
||||
private:
|
||||
/// Which configurations did we try at input.index() that couldn't match input.LT(1)?
|
||||
std::shared_ptr<atn::ATNConfigSet> deadEndConfigs;
|
||||
|
||||
/// The token object at the start index; the input stream might
|
||||
/// not be buffering tokens so get a reference to it. (At the
|
||||
/// time the error occurred, of course the stream needs to keep a
|
||||
/// buffer all of the tokens but later we might not have access to those.)
|
||||
std::shared_ptr<Token> startToken;
|
||||
|
||||
public:
|
||||
NoViableAltException(Parser *recognizer); // LL(1) error
|
||||
NoViableAltException(Parser *recognizer, TokenStream *input, Token *startToken, Token *offendingToken,
|
||||
atn::ATNConfigSet *deadEndConfigs, ParserRuleContext *ctx);
|
||||
|
||||
virtual std::shared_ptr<Token> getStartToken();
|
||||
virtual std::shared_ptr<atn::ATNConfigSet> getDeadEndConfigs();
|
||||
virtual Token* getStartToken() const;
|
||||
virtual atn::ATNConfigSet* getDeadEndConfigs() const;
|
||||
|
||||
private:
|
||||
/// Which configurations did we try at input.index() that couldn't match input.LT(1)?
|
||||
atn::ATNConfigSet* _deadEndConfigs;
|
||||
|
||||
/// The token object at the start index; the input stream might
|
||||
/// not be buffering tokens so get a reference to it. (At the
|
||||
/// time the error occurred, of course the stream needs to keep a
|
||||
/// buffer all of the tokens but later we might not have access to those.)
|
||||
Token* _startToken;
|
||||
|
||||
};
|
||||
|
||||
} // namespace runtime
|
||||
|
|
|
@ -308,7 +308,7 @@ void Parser::notifyErrorListeners(const std::wstring &msg) {
|
|||
notifyErrorListeners(getCurrentToken(), msg, nullptr);
|
||||
}
|
||||
|
||||
void Parser::notifyErrorListeners(Token *offendingToken, const std::wstring &msg, RecognitionException *e) {
|
||||
void Parser::notifyErrorListeners(Token *offendingToken, const std::wstring &msg, std::exception_ptr e) {
|
||||
_syntaxErrors++;
|
||||
int line = -1;
|
||||
int charPositionInLine = -1;
|
||||
|
@ -599,5 +599,6 @@ void Parser::InitializeInstanceFields() {
|
|||
_precedenceStack.push_back(0);
|
||||
_buildParseTrees = true;
|
||||
_syntaxErrors = 0;
|
||||
_input = nullptr;
|
||||
}
|
||||
|
||||
|
|
|
@ -277,7 +277,7 @@ namespace runtime {
|
|||
|
||||
void notifyErrorListeners(const std::wstring &msg);
|
||||
|
||||
virtual void notifyErrorListeners(Token *offendingToken, const std::wstring &msg, RecognitionException *e);
|
||||
virtual void notifyErrorListeners(Token *offendingToken, const std::wstring &msg, std::exception_ptr e);
|
||||
|
||||
/// <summary>
|
||||
/// Consume and return the <seealso cref="#getCurrentToken current symbol"/>.
|
||||
|
|
|
@ -96,11 +96,9 @@ namespace runtime {
|
|||
|
||||
Token *start, *stop;
|
||||
|
||||
/// <summary>
|
||||
/// The exception that forced this rule to return. If the rule successfully
|
||||
/// completed, this is {@code null}.
|
||||
/// </summary>
|
||||
RecognitionException *exception;
|
||||
/// completed, this is "null exception pointer".
|
||||
std::exception_ptr exception;
|
||||
|
||||
ParserRuleContext();
|
||||
virtual ~ParserRuleContext() {}
|
||||
|
|
|
@ -55,7 +55,7 @@ namespace runtime {
|
|||
}
|
||||
|
||||
void syntaxError(IRecognizer *recognizer, Token *offendingSymbol, size_t line, int charPositionInLine,
|
||||
const std::wstring &msg, RecognitionException *e) override {
|
||||
const std::wstring &msg, std::exception_ptr e) override {
|
||||
for (auto listener : *delegates) {
|
||||
listener->syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ RecognitionException::RecognitionException(const std::string &message, IRecogniz
|
|||
}
|
||||
}
|
||||
|
||||
int RecognitionException::getOffendingState() {
|
||||
int RecognitionException::getOffendingState() const {
|
||||
return _offendingState;
|
||||
}
|
||||
|
||||
|
@ -59,26 +59,26 @@ void RecognitionException::setOffendingState(int offendingState) {
|
|||
_offendingState = offendingState;
|
||||
}
|
||||
|
||||
misc::IntervalSet RecognitionException::getExpectedTokens() {
|
||||
misc::IntervalSet RecognitionException::getExpectedTokens() const {
|
||||
if (_recognizer) {
|
||||
return _recognizer->getATN().getExpectedTokens(_offendingState, _ctx.get());
|
||||
return _recognizer->getATN().getExpectedTokens(_offendingState, _ctx);
|
||||
}
|
||||
return misc::IntervalSet::EMPTY_SET;
|
||||
}
|
||||
|
||||
std::shared_ptr<RuleContext> RecognitionException::getCtx() {
|
||||
RuleContext* RecognitionException::getCtx() const {
|
||||
return _ctx;
|
||||
}
|
||||
|
||||
std::shared_ptr<IntStream> RecognitionException::getInputStream() {
|
||||
IntStream* RecognitionException::getInputStream() const {
|
||||
return _input;
|
||||
}
|
||||
|
||||
std::shared_ptr<Token> RecognitionException::getOffendingToken() {
|
||||
Token* RecognitionException::getOffendingToken() const {
|
||||
return _offendingToken;
|
||||
}
|
||||
|
||||
std::shared_ptr<IRecognizer> RecognitionException::getRecognizer() {
|
||||
IRecognizer* RecognitionException::getRecognizer() const {
|
||||
return _recognizer;
|
||||
}
|
||||
|
||||
|
|
|
@ -51,14 +51,14 @@ namespace runtime {
|
|||
class RecognitionException : public RuntimeException {
|
||||
private:
|
||||
/// The Recognizer where this exception originated.
|
||||
std::shared_ptr<IRecognizer> _recognizer;
|
||||
std::shared_ptr<IntStream> _input;
|
||||
std::shared_ptr<RuleContext> _ctx;
|
||||
IRecognizer *_recognizer;
|
||||
IntStream *_input;
|
||||
RuleContext *_ctx;
|
||||
|
||||
/// The current Token when an error occurred. Since not all streams
|
||||
/// support accessing symbols by index, we have to track the Token
|
||||
/// instance itself.
|
||||
std::shared_ptr<Token> _offendingToken;
|
||||
Token *_offendingToken;
|
||||
|
||||
int _offendingState;
|
||||
|
||||
|
@ -74,7 +74,7 @@ namespace runtime {
|
|||
/// edge we couldn't match.
|
||||
///
|
||||
/// If the state number is not known, this method returns -1.
|
||||
virtual int getOffendingState();
|
||||
virtual int getOffendingState() const;
|
||||
|
||||
protected:
|
||||
void setOffendingState(int offendingState);
|
||||
|
@ -88,7 +88,7 @@ namespace runtime {
|
|||
/// @returns The set of token types that could potentially follow the current
|
||||
/// state in the ATN, or an empty set if the information is not available.
|
||||
public:
|
||||
virtual misc::IntervalSet getExpectedTokens();
|
||||
virtual misc::IntervalSet getExpectedTokens() const;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <seealso cref="RuleContext"/> at the time this exception was thrown.
|
||||
|
@ -97,7 +97,7 @@ namespace runtime {
|
|||
/// </summary>
|
||||
/// <returns> The <seealso cref="RuleContext"/> at the time this exception was thrown.
|
||||
/// If the context is not available, this method returns {@code null}. </returns>
|
||||
virtual std::shared_ptr<RuleContext> getCtx();
|
||||
virtual RuleContext* getCtx() const;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the input stream which is the symbol source for the recognizer where
|
||||
|
@ -108,9 +108,9 @@ namespace runtime {
|
|||
/// <returns> The input stream which is the symbol source for the recognizer
|
||||
/// where this exception was thrown, or {@code null} if the stream is not
|
||||
/// available. </returns>
|
||||
virtual std::shared_ptr<IntStream> getInputStream();
|
||||
virtual IntStream* getInputStream() const;
|
||||
|
||||
virtual std::shared_ptr<Token> getOffendingToken();
|
||||
virtual Token* getOffendingToken() const;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <seealso cref="Recognizer"/> where this exception occurred.
|
||||
|
@ -119,7 +119,7 @@ namespace runtime {
|
|||
/// </summary>
|
||||
/// <returns> The recognizer where this exception occurred, or {@code null} if
|
||||
/// the recognizer is not available. </returns>
|
||||
virtual std::shared_ptr<IRecognizer> getRecognizer();
|
||||
virtual IRecognizer* getRecognizer() const;
|
||||
|
||||
private:
|
||||
void InitializeInstanceFields();
|
||||
|
|
|
@ -80,7 +80,7 @@ namespace runtime {
|
|||
|
||||
/// <summary>
|
||||
/// Get the token type of the token </summary>
|
||||
virtual int getType() = 0;
|
||||
virtual int getType() const = 0;
|
||||
|
||||
/// <summary>
|
||||
/// The line number on which the 1st character of this token was matched,
|
||||
|
|
|
@ -84,7 +84,7 @@ size_t UnbufferedCharStream::fill(size_t n) {
|
|||
try {
|
||||
size_t c = nextChar();
|
||||
add(c);
|
||||
} catch (IOException ioe) {
|
||||
} catch (IOException &ioe) {
|
||||
throw std::exception(ioe);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
|
||||
using namespace org::antlr::v4::runtime;
|
||||
using namespace org::antlr::v4::runtime::atn;
|
||||
using namespace antlrcpp;
|
||||
|
||||
LL1Analyzer::LL1Analyzer(const ATN &atn) : _atn(atn) {
|
||||
}
|
||||
|
@ -122,17 +123,14 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContextRef c
|
|||
ATNState *returnState = _atn.states[(size_t)ctx->getReturnState(i)];
|
||||
|
||||
bool removed = calledRuleStack.data.test((size_t)returnState->ruleIndex);
|
||||
try {
|
||||
calledRuleStack.data[(size_t)returnState->ruleIndex] = false;
|
||||
_LOOK(returnState, stopState, ctx->getParent(i).lock(), look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
|
||||
}
|
||||
catch(...) {
|
||||
// Just move to the next steps as a "finally" clause
|
||||
}
|
||||
if (removed) {
|
||||
calledRuleStack.set((size_t)returnState->ruleIndex);
|
||||
auto onExit = finally([&] {
|
||||
if (removed) {
|
||||
calledRuleStack.set((size_t)returnState->ruleIndex);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
calledRuleStack.data[(size_t)returnState->ruleIndex] = false;
|
||||
_LOOK(returnState, stopState, ctx->getParent(i).lock(), look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -148,15 +146,12 @@ void LL1Analyzer::_LOOK(ATNState *s, ATNState *stopState, PredictionContextRef c
|
|||
}
|
||||
|
||||
PredictionContextRef newContext = SingletonPredictionContext::create(ctx, (static_cast<RuleTransition*>(t))->followState->stateNumber);
|
||||
auto onExit = finally([&] {
|
||||
calledRuleStack.data[(size_t)((static_cast<RuleTransition*>(t))->target->ruleIndex)] = false;
|
||||
});
|
||||
|
||||
try {
|
||||
calledRuleStack.set((size_t)(static_cast<RuleTransition*>(t))->target->ruleIndex);
|
||||
_LOOK(t->target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
|
||||
}
|
||||
catch(...) {
|
||||
// Just move to the next steps as a "finally" clause
|
||||
}
|
||||
calledRuleStack.data[(size_t)((static_cast<RuleTransition*>(t))->target->ruleIndex)] = false;
|
||||
calledRuleStack.set((size_t)(static_cast<RuleTransition*>(t))->target->ruleIndex);
|
||||
_LOOK(t->target, stopState, newContext, look, lookBusy, calledRuleStack, seeThruPreds, addEOF);
|
||||
|
||||
} else if (dynamic_cast<AbstractPredicateTransition*>(t) != nullptr) {
|
||||
if (seeThruPreds) {
|
||||
|
|
|
@ -50,6 +50,7 @@
|
|||
|
||||
using namespace org::antlr::v4::runtime;
|
||||
using namespace org::antlr::v4::runtime::atn;
|
||||
using namespace antlrcpp;
|
||||
|
||||
void LexerATNSimulator::SimState::reset() {
|
||||
index = -1;
|
||||
|
@ -89,19 +90,20 @@ int LexerATNSimulator::match(CharStream *input, size_t mode) {
|
|||
match_calls++;
|
||||
_mode = mode;
|
||||
ssize_t mark = input->mark();
|
||||
try {
|
||||
_startIndex = (int)input->index();
|
||||
prevAccept->reset();
|
||||
dfa::DFA *dfa = _decisionToDFA[mode];
|
||||
if (dfa->s0 == nullptr) {
|
||||
return matchATN(input);
|
||||
} else {
|
||||
return execATN(input, dfa->s0);
|
||||
}
|
||||
}
|
||||
catch(...) {
|
||||
|
||||
auto onExit = finally([=] {
|
||||
input->release(mark);
|
||||
});
|
||||
|
||||
_startIndex = (int)input->index();
|
||||
prevAccept->reset();
|
||||
dfa::DFA *dfa = _decisionToDFA[mode];
|
||||
if (dfa->s0 == nullptr) {
|
||||
return matchATN(input);
|
||||
} else {
|
||||
return execATN(input, dfa->s0);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -448,16 +450,16 @@ bool LexerATNSimulator::evaluatePredicate(CharStream *input, int ruleIndex, int
|
|||
size_t savedLine = _line;
|
||||
size_t index = input->index();
|
||||
ssize_t marker = input->mark();
|
||||
try {
|
||||
consume(input);
|
||||
return _recog->sempred(nullptr, ruleIndex, predIndex);
|
||||
} catch(...) {
|
||||
|
||||
auto onExit = finally([&] {
|
||||
_charPositionInLine = savedCharPositionInLine;
|
||||
_line = savedLine;
|
||||
input->seek(index);
|
||||
input->release(marker);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
consume(input);
|
||||
return _recog->sempred(nullptr, ruleIndex, predIndex);
|
||||
}
|
||||
|
||||
void LexerATNSimulator::captureSimState(SimState *settings, CharStream *input, dfa::DFAState *dfaState) {
|
||||
|
|
|
@ -111,7 +111,7 @@ int ParserATNSimulator::adaptivePredict(TokenStream *input, int decision, Parser
|
|||
}
|
||||
return alt;
|
||||
}
|
||||
catch (std::exception e) {
|
||||
catch (std::exception &e) {
|
||||
// Do nothing, failed predict
|
||||
}
|
||||
|
||||
|
|
|
@ -54,8 +54,8 @@ namespace antlrcpp {
|
|||
|
||||
// Convenience functions to avoid lengthy dynamic_cast() != nullptr checks in many places.
|
||||
template <typename T1, typename T2>
|
||||
bool is(T2 obj) { // For value types.
|
||||
return dynamic_cast<T1 *>(&obj) != nullptr;
|
||||
bool is(T2 &obj) { // For value types.
|
||||
return dynamic_cast<typename std::add_const<T1>::type *>(&obj) != nullptr;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2>
|
||||
|
|
|
@ -65,7 +65,7 @@ std::wstring RuleTagToken::getText() {
|
|||
return std::wstring(L"<") + ruleName + std::wstring(L">");
|
||||
}
|
||||
|
||||
int RuleTagToken::getType() {
|
||||
int RuleTagToken::getType() const {
|
||||
return bypassTokenType;
|
||||
}
|
||||
|
||||
|
|
|
@ -121,7 +121,7 @@ namespace pattern {
|
|||
/// Rule tag tokens have types assigned according to the rule bypass
|
||||
/// transitions created during ATN deserialization.
|
||||
/// </summary>
|
||||
virtual int getType() override;
|
||||
virtual int getType() const override;
|
||||
|
||||
/// <summary>
|
||||
/// {@inheritDoc}
|
||||
|
|
|
@ -343,7 +343,7 @@ std::vector\<std::wstring> <parser.name>::_tokenNames = {
|
|||
<atn>
|
||||
}
|
||||
|
||||
<parser.name>Parser::Initializer <parser.name>Parser::_init;
|
||||
<parser.name>::Initializer <parser.name>::_init;
|
||||
>>
|
||||
|
||||
SerializedATNHeader(model) ::= <<
|
||||
|
@ -384,7 +384,7 @@ RuleFunction(currentRule, code, locals, ruleCtx, altLabelCtxs, namedActions, fin
|
|||
<ruleCtx>
|
||||
<! TODO: untested !><altLabelCtxs: {l | <altLabelCtxs.(l)>}; separator = "\n">
|
||||
std::shared_ptr\<<parser.name>::<currentRule.ctxType>\> <parser.name>::<currentRule.name>(<currentRule.args; separator = ",">) {
|
||||
std::shared_ptr\<<currentRule.ctxType>\> result(new <currentRule.ctxType>(ctx, getState()<currentRule.args:{a | , <a.name>}>));
|
||||
std::shared_ptr\<<currentRule.ctxType>\> result = std::make_shared\<<currentRule.ctxType>\>(ctx, getState()<currentRule.args:{a | , <a.name>}>);
|
||||
enterRule(result.get(), <currentRule.startState>, <parser.name>Rule::Rule<currentRule.name; format = "cap">);
|
||||
<namedActions.init>
|
||||
<! TODO: untested !> <locals; separator = "\n">
|
||||
|
@ -404,10 +404,10 @@ std::shared_ptr\<<parser.name>::<currentRule.ctxType>\> <parser.name>::<currentR
|
|||
<if (exceptions)>
|
||||
<exceptions; separator="\n">
|
||||
<else>
|
||||
catch (RecognitionException *re) {
|
||||
result->exception = re;
|
||||
_errHandler->reportError(this, re);
|
||||
_errHandler->recover(this, re);
|
||||
catch (RecognitionException &e) {
|
||||
result->exception = std::make_exception_ptr(e);
|
||||
_errHandler->reportError(this, e);
|
||||
_errHandler->recover(this, e);
|
||||
}
|
||||
<endif>
|
||||
|
||||
|
@ -433,7 +433,7 @@ std::shared_ptr\<<parser.name>::<currentRule.ctxType>\> <parser.name>::<currentR
|
|||
std::shared_ptr\<<parser.name>::<currentRule.ctxType>\> <parser.name>::<currentRule.name>(int precedence<currentRule.args:{a | , <a>}>) {
|
||||
ParserRuleContext *parentContext = ctx;
|
||||
int parentState = getState();
|
||||
std::shared_ptr\<<parser.name>::<currentRule.ctxType>\> result(new <currentRule.ctxType>(ctx, parentState<currentRule.args: {a | , <a.name>}>));
|
||||
std::shared_ptr\<<parser.name>::<currentRule.ctxType>\> result = std::make_shared\<<currentRule.ctxType>\>(ctx, parentState<currentRule.args: {a | , <a.name>}>);
|
||||
std::shared_ptr\<<parser.name>::<currentRule.ctxType>\> previousContext = result;
|
||||
int startState = <currentRule.startState>;
|
||||
enterRecursionRule(result.get(), <currentRule.startState>, <parser.name>Rule::Rule<currentRule.name; format = "cap">, precedence);
|
||||
|
@ -451,10 +451,10 @@ std::shared_ptr\<<parser.name>::<currentRule.ctxType>\> <parser.name>::<currentR
|
|||
<! TODO: untested !><postamble; separator = "\n">
|
||||
<namedActions.after>
|
||||
}
|
||||
catch (RecognitionException *re) {
|
||||
result->exception = re;
|
||||
_errHandler->reportError(this, re);
|
||||
_errHandler->recover(this, re);
|
||||
catch (RecognitionException &e) {
|
||||
result->exception = std::make_exception_ptr(e);
|
||||
_errHandler->reportError(this, e);
|
||||
_errHandler->recover(this, e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -538,7 +538,7 @@ AltLabelStructDecl(struct, attrs, getters, dispatchMethods) ::= <<
|
|||
|
||||
CodeBlockForOuterMostAltHeader(currentOuterMostAltCodeBlock, locals, preamble, ops) ::= "<! Required to exist, but unused. !>"
|
||||
CodeBlockForOuterMostAlt(currentOuterMostAltCodeBlock, locals, preamble, ops) ::= <<
|
||||
<if (currentOuterMostAltCodeBlock.altLabel)>result.reset(new <parser.name>::<currentOuterMostAltCodeBlock.altLabel; format = "cap">Context(result.get()));<endif>
|
||||
<if (currentOuterMostAltCodeBlock.altLabel)>result = std::make_shared\<<parser.name>::<currentOuterMostAltCodeBlock.altLabel; format = "cap">Context>(result.get());<endif>
|
||||
enterOuterAlt(result.get(), <currentOuterMostAltCodeBlock.alt.altNum>);
|
||||
<CodeBlockForAlt(currentAltCodeBlock = currentOuterMostAltCodeBlock, ...)>
|
||||
>>
|
||||
|
@ -989,19 +989,19 @@ recRuleSetReturnAction(src,name) ::= "recRuleSetReturnAction(src,name) $<name>=$
|
|||
recRuleSetStopToken() ::= "ctx->stop = _input->LT(-1);"
|
||||
|
||||
recRuleAltStartAction(ruleName, ctxName, label) ::= <<
|
||||
result.reset(new <ctxName>Context(parentContext, parentState));
|
||||
result = std::make_shared\<<ctxName>Context>(parentContext, parentState);
|
||||
<if (label)>result-><label> = previousContext;<endif>
|
||||
pushNewRecursionContext(result.get(), startState, <parser.name>Rule::Rule<ruleName; format = "cap">);
|
||||
>>
|
||||
|
||||
recRuleLabeledAltStartAction(ruleName, currentAltLabel, label) ::= <<recRuleLabeledAltStartAction
|
||||
result.reset(new <currentAltLabel; format = "cap">Context(new <ruleName; format="cap">Context(_parentctx, _parentState)));
|
||||
result = std::make_shared\<<currentAltLabel; format = "cap">Context>(new <ruleName; format="cap">Context(_parentctx, _parentState));
|
||||
<if(label)>((<currentAltLabel; format="cap">Context*)result).<label> = previousContext;<endif>
|
||||
pushNewRecursionContext(result, _startState, RULE_<ruleName>);
|
||||
>>
|
||||
|
||||
recRuleReplaceContext(ctxName) ::= <<recRuleReplaceContext
|
||||
result.reset(new <ctxName>Context(_localctx));
|
||||
result = std::make_shared\<<ctxName>Context>(_localctx);
|
||||
ctx = result;
|
||||
previousContext = result;
|
||||
>>
|
||||
|
|
|
@ -67,7 +67,7 @@ LexerFile(file, lexer, namedActions) ::= <<
|
|||
#include "LexerATNSimulator.h"
|
||||
#include "ATNDeserializer.h"
|
||||
|
||||
#include "<lexer.name>.h"
|
||||
#include "<file.lexer.name>.h"
|
||||
|
||||
using namespace org::antlr::v4::runtime;
|
||||
|
||||
|
@ -86,7 +86,6 @@ ParserFileHeader(file, parser, namedActions) ::= <<
|
|||
#include "ParserRuleContext.h"
|
||||
#include "PredictionContext.h"
|
||||
#include "CPPUtils.h"
|
||||
//#include "<file.grammarName>Visitor.h"
|
||||
|
||||
using namespace org::antlr::v4::runtime;
|
||||
|
||||
|
@ -95,7 +94,7 @@ using namespace org::antlr::v4::runtime;
|
|||
<if (file.genPackage)>namespace <file.genPackage> {<endif>
|
||||
|
||||
template \<typename T>
|
||||
class <parser.name>Visitor;
|
||||
class <file.parser.name>Visitor;
|
||||
|
||||
<parser>
|
||||
|
||||
|
@ -116,9 +115,9 @@ ParserFile(file, parser, namedActions) ::= <<
|
|||
#include "ATNDeserializer.h"
|
||||
|
||||
#include "ParseTreeListener.h"
|
||||
#include "<file.factory.grammar.name>Listener.h"
|
||||
<if (file.genListener)>#include "<file.parser.name>Listener.h"<endif>
|
||||
|
||||
#include "<parser.name>.h"
|
||||
#include "<file.parser.name>.h"
|
||||
|
||||
using namespace antlrcpp;
|
||||
<if (file.genPackage)>using namespace <file.genPackage>;<endif>
|
||||
|
|
|
@ -39,6 +39,8 @@ import java.util.Map;
|
|||
|
||||
public class LexerFile extends OutputFile {
|
||||
public String genPackage; // from -package cmd-line
|
||||
public boolean genListener; // from -listener cmd-line
|
||||
public boolean genVisitor; // from -visitor cmd-line
|
||||
@ModelElement public Lexer lexer;
|
||||
@ModelElement public Map<String, Action> namedActions;
|
||||
|
||||
|
@ -51,5 +53,7 @@ public class LexerFile extends OutputFile {
|
|||
namedActions.put(name, new Action(factory, ast));
|
||||
}
|
||||
genPackage = factory.getGrammar().tool.genPackage;
|
||||
genListener = factory.getGrammar().tool.gen_listener;
|
||||
genVisitor = factory.getGrammar().tool.gen_visitor;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,8 @@ import java.util.Map;
|
|||
/** */
|
||||
public class ParserFile extends OutputFile {
|
||||
public String genPackage; // from -package cmd-line
|
||||
public boolean genListener; // from -listener cmd-line
|
||||
public boolean genVisitor; // from -visitor cmd-line
|
||||
@ModelElement public Parser parser;
|
||||
@ModelElement public Map<String, Action> namedActions;
|
||||
|
||||
|
@ -52,5 +54,7 @@ public class ParserFile extends OutputFile {
|
|||
namedActions.put(name, new Action(factory, ast));
|
||||
}
|
||||
genPackage = factory.getGrammar().tool.genPackage;
|
||||
genListener = factory.getGrammar().tool.gen_listener;
|
||||
genVisitor = factory.getGrammar().tool.gen_visitor;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue