Skip to content

Commit

Permalink
libnixf: report error if extra token is consumed (#506)
Browse files Browse the repository at this point in the history
Fixes: #483
  • Loading branch information
inclyc authored May 23, 2024
1 parent 02b17ab commit 1027307
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions libnixf/src/Parse/ParseSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Parser.h"

#include "nixf/Basic/TokenKinds.h"
#include "nixf/Parse/Parser.h"

using namespace nixf;
Expand Down Expand Up @@ -39,6 +40,16 @@ std::shared_ptr<Node> nixf::parse(std::string_view Src,
return P.parse();
}

std::shared_ptr<Expr> nixf::Parser::parse() {
auto Expr = parseExpr();
if (Token Tok = peek(); Tok.kind() != tok::tok_eof) {
// TODO: maybe we'd like to have multiple expressions in a single file.
// Report an error.
Diags.emplace_back(Diagnostic::DK_UnexpectedText, Tok.range());
}
return Expr;
}

void Parser::resetLookAheadBuf() {
if (!LookAheadBuf.empty()) {
Token Tok = LookAheadBuf.front();
Expand Down
3 changes: 2 additions & 1 deletion libnixf/src/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ class Parser {
/// \endcode
std::shared_ptr<ExprWith> parseExprWith();

std::shared_ptr<Expr> parse() { return parseExpr(); }
/// Top-level parsing.
std::shared_ptr<Expr> parse();
};

} // namespace nixf
12 changes: 12 additions & 0 deletions libnixf/test/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ namespace {
using namespace nixf;
using namespace std::string_view_literals;

TEST(Parser, ParseEnd) {
auto Src = R"({a = 1;},)"sv;

std::vector<Diagnostic> Diags;
Parser P(Src, Diags);

P.parse();

// See that we have diagnostic at the last token ","
ASSERT_EQ(Diags.size(), 1);
}

TEST(Parser, SelectOK) {
auto Src = R"(a.b.c)"sv;

Expand Down

0 comments on commit 1027307

Please sign in to comment.