Skip to content

Commit

Permalink
libnixf: diagnose non-associative operators (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc authored Sep 18, 2024
1 parent 28a4190 commit f9d141f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions libnixf/src/Basic/diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ class Diagnostic(TypedDict):
"severity": "Error",
"message": "extra `@` for lambda arg",
},
{
"sname": "parse-operator-noassoc",
"cname": "OperatorNotAssociative",
"severity": "Error",
"message": "operator is non-associative",
},
{
"sname": "let-dynamic",
"cname": "LetDynamic",
Expand Down
5 changes: 4 additions & 1 deletion libnixf/src/Parse/ParseOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Parser.h"

#include "nixf/Basic/Diagnostic.h"
#include "nixf/Basic/Nodes/Op.h"

#include <cassert>
Expand Down Expand Up @@ -115,7 +116,9 @@ std::shared_ptr<Expr> Parser::parseExprOpBP(unsigned LeftRBP) {
if (LeftRBP > LBP)
return Prefix;
if (LeftRBP == LBP) {
// TODO: noassoc
// Report error, operator OP and expr_op is not associative.
Diags.emplace_back(Diagnostic::DK_OperatorNotAssociative,
Tok.range());
}
consume();
assert(LastToken && "consume() should have set LastToken");
Expand Down
13 changes: 13 additions & 0 deletions libnixf/test/Parse/ParseOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Parser.h"

#include "nixf/Basic/Diagnostic.h"
#include "nixf/Basic/Nodes/Op.h"

namespace {
Expand Down Expand Up @@ -71,4 +72,16 @@ TEST(Parser, OpHasAttr_empty) {
ASSERT_EQ(Diags.size(), 0);
}

TEST(Parser, Op_NonAssociative) {
auto Src = R"(1 == 1 == 1)"sv;

std::vector<Diagnostic> Diags;
Parser P(Src, Diags);
auto AST = P.parseExpr();

ASSERT_TRUE(AST);
ASSERT_EQ(Diags.size(), 1);
ASSERT_EQ(Diags[0].kind(), Diagnostic::DK_OperatorNotAssociative);
}

} // namespace

0 comments on commit f9d141f

Please sign in to comment.