This repository was archived by the owner on Sep 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstructerrorstrategy.cpp
More file actions
116 lines (97 loc) · 4.69 KB
/
cstructerrorstrategy.cpp
File metadata and controls
116 lines (97 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "cstructerrorstrategy.h"
#include "FailedPredicateException.h"
#include "InputMismatchException.h"
#include "NoViableAltException.h"
#include "ParserRuleContext.h"
CStructErrorStrategy::CStructErrorStrategy() {}
void CStructErrorStrategy::reportError(antlr4::Parser *recognizer,
const antlr4::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)) {
return; // don't report spurious errors
}
beginErrorCondition(recognizer);
if (antlrcpp::is<const antlr4::NoViableAltException *>(&e)) {
reportNoViableAlternative(
recognizer, static_cast<const antlr4::NoViableAltException &>(e));
} else if (antlrcpp::is<const antlr4::InputMismatchException *>(&e)) {
reportInputMismatch(
recognizer, static_cast<const antlr4::InputMismatchException &>(e));
} else if (antlrcpp::is<const antlr4::FailedPredicateException *>(&e)) {
reportFailedPredicate(
recognizer,
static_cast<const antlr4::FailedPredicateException &>(e));
} else if (antlrcpp::is<const antlr4::RecognitionException *>(&e)) {
// other unknown exceptions?
recognizer->notifyErrorListeners(e.getOffendingToken(), e.what(),
std::current_exception());
}
}
void CStructErrorStrategy::reportNoViableAlternative(
antlr4::Parser *recognizer, const antlr4::NoViableAltException &e) {
antlr4::TokenStream *tokens = recognizer->getTokenStream();
QString input;
if (tokens != nullptr) {
if (e.getStartToken()->getType() == antlr4::Token::EOF) {
input = tr("<EOF>");
} else {
input = QString::fromStdString(escapeWSAndQuote(
tokens->getText(e.getStartToken(), e.getOffendingToken())));
}
} else {
input = tr("<unknown input>");
}
auto msg = tr("no viable alternative at input %1").arg(input);
recognizer->notifyErrorListeners(e.getOffendingToken(), msg.toStdString(),
std::make_exception_ptr(e));
}
void CStructErrorStrategy::reportInputMismatch(
antlr4::Parser *recognizer, const antlr4::InputMismatchException &e) {
auto msg = tr("mismatched input %1 expecting %2")
.arg(QString::fromStdString(
getTokenErrorDisplay(e.getOffendingToken())),
QString::fromStdString(e.getExpectedTokens().toString(
recognizer->getVocabulary())));
recognizer->notifyErrorListeners(e.getOffendingToken(), msg.toStdString(),
std::make_exception_ptr(e));
}
void CStructErrorStrategy::reportFailedPredicate(
antlr4::Parser *recognizer, const antlr4::FailedPredicateException &e) {
const std::string &ruleName =
recognizer->getRuleNames()[recognizer->getContext()->getRuleIndex()];
auto msg = tr("rule %1 failed predicate: %2?")
.arg(QString::fromStdString(ruleName),
QString::fromStdString(
const_cast<antlr4::FailedPredicateException &>(e)
.getPredicate()));
recognizer->notifyErrorListeners(e.getOffendingToken(), msg.toStdString(),
std::make_exception_ptr(e));
}
void CStructErrorStrategy::reportUnwantedToken(antlr4::Parser *recognizer) {
if (inErrorRecoveryMode(recognizer)) {
return;
}
beginErrorCondition(recognizer);
antlr4::Token *t = recognizer->getCurrentToken();
std::string tokenName = getTokenErrorDisplay(t);
antlr4::misc::IntervalSet expecting = getExpectedTokens(recognizer);
auto msg = tr("extraneous input %1 expecting %2")
.arg(QString::fromStdString(tokenName),
QString::fromStdString(
expecting.toString(recognizer->getVocabulary())));
recognizer->notifyErrorListeners(t, msg.toStdString(), nullptr);
}
void CStructErrorStrategy::reportMissingToken(antlr4::Parser *recognizer) {
if (inErrorRecoveryMode(recognizer)) {
return;
}
beginErrorCondition(recognizer);
antlr4::Token *t = recognizer->getCurrentToken();
antlr4::misc::IntervalSet expecting = getExpectedTokens(recognizer);
std::string expectedText = expecting.toString(recognizer->getVocabulary());
auto msg = tr("missing %1 at %2")
.arg(QString::fromStdString(expectedText),
QString::fromStdString(getTokenErrorDisplay(t)));
recognizer->notifyErrorListeners(t, msg.toStdString(), nullptr);
}