Skip to content

Commit

Permalink
fix: Temporary workaround to parse post increments #118
Browse files Browse the repository at this point in the history
This is a temporary workaround to accept a larger subset of
increments expressions.
  • Loading branch information
robertoraggi committed Aug 14, 2023
1 parent 1592673 commit 29611ee
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
47 changes: 43 additions & 4 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,29 @@

namespace cxx {

static auto getFunctionDeclaratorHelper(DeclaratorAST* declarator)
namespace {

class RecordingDiagnosticsClient : public DiagnosticsClient {
public:
void reset() { messages_.clear(); }

void reportTo(DiagnosticsClient* client) {
for (const auto& message : messages_) {
client->report(message);
}
}

auto messages() const -> const std::vector<Diagnostic>& { return messages_; }

void report(const Diagnostic& message) override {
messages_.push_back(message);
}

private:
std::vector<Diagnostic> messages_;
};

auto getFunctionDeclaratorHelper(DeclaratorAST* declarator)
-> std::pair<FunctionDeclaratorAST*, bool> {
if (!declarator) return std::make_pair(nullptr, false);

Expand Down Expand Up @@ -64,11 +86,13 @@ static auto getFunctionDeclaratorHelper(DeclaratorAST* declarator)
return std::make_pair(nullptr, false);
}

static auto getFunctionDeclarator(DeclaratorAST* declarator)
auto getFunctionDeclarator(DeclaratorAST* declarator)
-> FunctionDeclaratorAST* {
return get<0>(getFunctionDeclaratorHelper(declarator));
}

} // namespace

Parser::Parser(TranslationUnit* unit) : unit(unit) {
control = unit->control();
cursor_ = 1;
Expand Down Expand Up @@ -1945,6 +1969,8 @@ auto Parser::parse_unop_expression(ExpressionAST*& yyast) -> bool {

if (!parse_cast_expression(ast->expression)) {
parse_error("expected an expression");
rewind(opLoc);
return false;
}

return true;
Expand Down Expand Up @@ -2289,7 +2315,18 @@ auto Parser::parse_delete_expression(ExpressionAST*& yyast) -> bool {
auto Parser::parse_cast_expression(ExpressionAST*& yyast) -> bool {
const auto start = currentLocation();

if (parse_cast_expression_helper(yyast)) return true;
RecordingDiagnosticsClient diags;

auto savedDiagnosticClient = unit->changeDiagnosticsClient(&diags);

auto parsedCastExpression = parse_cast_expression_helper(yyast);

unit->changeDiagnosticsClient(savedDiagnosticClient);

if (parsedCastExpression) {
diags.reportTo(unit->diagnosticsClient());
return true;
}

rewind(start);

Expand All @@ -2311,7 +2348,9 @@ auto Parser::parse_cast_expression_helper(ExpressionAST*& yyast) -> bool {

ExpressionAST* expression = nullptr;

if (!parse_cast_expression(expression)) return false;
if (!parse_cast_expression(expression)) {
return false;
}

auto ast = new (pool) CastExpressionAST();
yyast = ast;
Expand Down
10 changes: 10 additions & 0 deletions src/parser/cxx/translation_unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ TranslationUnit::TranslationUnit(Control* control,

TranslationUnit::~TranslationUnit() = default;

auto TranslationUnit::diagnosticsClient() const -> DiagnosticsClient* {
return diagnosticsClient_;
}

auto TranslationUnit::changeDiagnosticsClient(
DiagnosticsClient* diagnosticsClient) -> DiagnosticsClient* {
std::swap(diagnosticsClient_, diagnosticsClient);
return diagnosticsClient;
}

void TranslationUnit::setSource(std::string source, std::string fileName) {
fileName_ = std::move(fileName);
preprocessor_->preprocess(std::move(source), fileName_, tokens_);
Expand Down
7 changes: 4 additions & 3 deletions src/parser/cxx/translation_unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ class TranslationUnit {

[[nodiscard]] auto arena() const -> Arena* { return arena_.get(); }

[[nodiscard]] auto diagnosticsClient() const -> DiagnosticsClient* {
return diagnosticsClient_;
}
[[nodiscard]] auto diagnosticsClient() const -> DiagnosticsClient*;

auto changeDiagnosticsClient(DiagnosticsClient* diagnosticsClient)
-> DiagnosticsClient*;

[[nodiscard]] auto ast() const -> UnitAST* { return ast_; }

Expand Down
3 changes: 0 additions & 3 deletions tests/unit_tests/parser/postfix_expr_conflicts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ auto decr(int x) -> int { return x--; }
auto call(int (*f)(int, int), int x) { return (f)(x, x); }
auto subscript(int a[], int n) -> int { return a[n]; }

// expected-error@1 {{expected an expression}}
auto incr_1(int x) -> int { return (x)++; }

// expected-error@1 {{expected an expression}}
auto decr_1(int x) -> int { return (x)--; }

// expected-error@1 {{expected lambda declarator}}
Expand Down

0 comments on commit 29611ee

Please sign in to comment.