diff --git a/libnixf/src/Parse/ParseSupport.cpp b/libnixf/src/Parse/ParseSupport.cpp index 0fa24caa7..995ddf3d8 100644 --- a/libnixf/src/Parse/ParseSupport.cpp +++ b/libnixf/src/Parse/ParseSupport.cpp @@ -3,6 +3,7 @@ #include "Parser.h" +#include "nixf/Basic/TokenKinds.h" #include "nixf/Parse/Parser.h" using namespace nixf; @@ -39,6 +40,16 @@ std::shared_ptr nixf::parse(std::string_view Src, return P.parse(); } +std::shared_ptr 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(); diff --git a/libnixf/src/Parse/Parser.h b/libnixf/src/Parse/Parser.h index 5587e2999..ad75770d0 100644 --- a/libnixf/src/Parse/Parser.h +++ b/libnixf/src/Parse/Parser.h @@ -334,7 +334,8 @@ class Parser { /// \endcode std::shared_ptr parseExprWith(); - std::shared_ptr parse() { return parseExpr(); } + /// Top-level parsing. + std::shared_ptr parse(); }; } // namespace nixf diff --git a/libnixf/test/Parse/ParseExpr.cpp b/libnixf/test/Parse/ParseExpr.cpp index 324811b00..5dc456aa4 100644 --- a/libnixf/test/Parse/ParseExpr.cpp +++ b/libnixf/test/Parse/ParseExpr.cpp @@ -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 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;