Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libnixf: report error if extra token is consumed #506

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading