15 D << std::move(As) +
" expression";
20void Parser::pushState(ParserState NewState) {
25void Parser::popState() {
30Parser::StateRAII Parser::withState(ParserState NewState) {
35Parser::SyncRAII Parser::withSync(TokenKind Kind) {
return {*
this, Kind}; }
38 std::vector<Diagnostic> &Diags) {
45 if (
Token Tok = peek(); Tok.
kind() != tok::tok_eof) {
48 Diags.emplace_back(Diagnostic::DK_UnexpectedText, Tok.range());
53void Parser::resetLookAheadBuf() {
54 if (!LookAheadBuf.empty()) {
55 Token Tok = LookAheadBuf.front();
63Token Parser::peek(std::size_t N) {
64 while (N >= LookAheadBuf.size()) {
65 switch (State.top()) {
67 LookAheadBuf.emplace_back(Lex.lex());
70 LookAheadBuf.emplace_back(Lex.lexString());
73 LookAheadBuf.emplace_back(Lex.lexIndString());
76 LookAheadBuf.emplace_back(Lex.lexPath());
80 return LookAheadBuf[N];
83std::optional<LexerCursorRange> Parser::consumeAsUnknown() {
84 LexerCursor Begin = peek().
lCur();
85 bool Consumed =
false;
86 for (Token Tok = peek(); Tok.
kind() != tok_eof; Tok = peek()) {
87 if (SyncTokens.contains(Tok.
kind()))
94 assert(LastToken &&
"LastToken should be set after consume()");
95 return LexerCursorRange{Begin, LastToken->rCur()};
98Parser::ExpectResult Parser::expect(TokenKind Kind) {
99 auto Sync = withSync(Kind);
100 if (Token Tok = peek(); Tok.
kind() == Kind) {
105 if (removeUnexpected()) {
106 if (Token Tok = peek(); Tok.
kind() == Kind) {
113 LexerCursor Insert = LastToken ? LastToken->rCur() : peek().lCur();
115 Diags.emplace_back(Diagnostic::DK_Expected, LexerCursorRange(Insert));
116 D << std::string(tok::spelling(Kind));
117 D.
fix(
"insert " + std::string(tok::spelling(Kind)))
Fix & fix(std::string Message)
Fix & edit(TextEdit Edit)
A point in the source file.
void setCur(const LexerCursor &NewCur)
Reset the cursor at source offset (zero-based indexing)
std::shared_ptr< Expr > parse()
Top-level parsing.
std::shared_ptr< Expr > parseExpr()
static TextEdit mkInsertion(LexerCursor P, std::string NewText)
A token. With it's kind, and the range in source code.
tok::TokenKind kind() const
Diagnostic & diagNullExpr(std::vector< Diagnostic > &Diags, LexerCursor Loc, std::string As)
std::shared_ptr< Node > parse(std::string_view Src, std::vector< Diagnostic > &Diags)
Parse a string.
Parser for the Nix expression language.