LLDB mainline
DILParser.h
Go to the documentation of this file.
1//===-- DILParser.h ---------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_VALUEOBJECT_DILPARSER_H
10#define LLDB_VALUEOBJECT_DILPARSER_H
11
14#include "lldb/Utility/Status.h"
17#include "llvm/Support/Error.h"
18#include <memory>
19#include <optional>
20#include <string>
21#include <system_error>
22#include <tuple>
23#include <vector>
24
25namespace lldb_private::dil {
26
33
34// The following is modeled on class OptionParseError.
36 : public llvm::ErrorInfo<DILDiagnosticError, DiagnosticError> {
38
39public:
40 using llvm::ErrorInfo<DILDiagnosticError, DiagnosticError>::ErrorInfo;
42 : ErrorInfo(make_error_code(std::errc::invalid_argument)),
43 m_detail(std::move(detail)) {}
44
45 DILDiagnosticError(llvm::StringRef expr, const std::string &message,
46 uint32_t loc, uint16_t err_len = 1);
47
48 std::unique_ptr<CloneableError> Clone() const override {
49 return std::make_unique<DILDiagnosticError>(m_detail);
50 }
51
52 llvm::ArrayRef<DiagnosticDetail> GetDetails() const override {
53 return m_detail;
54 }
55
56 std::string message() const override { return m_detail.rendered; }
57};
58
59/// Pure recursive descent parser for C++ like expressions.
60/// EBNF grammar for the parser is described in lldb/docs/dil-expr-lang.ebnf
61class DILParser {
62public:
63 static llvm::Expected<ASTNodeUP> Parse(llvm::StringRef dil_input_expr,
64 DILLexer lexer,
65 std::shared_ptr<StackFrame> frame_sp,
66 lldb::DynamicValueType use_dynamic,
67 bool use_synthetic, bool fragile_ivar,
68 bool check_ptr_vs_member);
69
70 ~DILParser() = default;
71
72 bool UseSynthetic() { return m_use_synthetic; }
73
74 bool UseFragileIvar() { return m_fragile_ivar; }
75
77
79
80private:
81 explicit DILParser(llvm::StringRef dil_input_expr, DILLexer lexer,
82 std::shared_ptr<StackFrame> frame_sp,
83 lldb::DynamicValueType use_dynamic, bool use_synthetic,
84 bool fragile_ivar, bool check_ptr_vs_member,
85 llvm::Error &error);
86
87 ASTNodeUP Run();
88
93
94 std::string ParseNestedNameSpecifier();
95
96 std::string ParseIdExpression();
97 std::string ParseUnqualifiedId();
98 std::optional<int64_t> ParseIntegerConstant();
103
104 void BailOut(const std::string &error, uint32_t loc, uint16_t err_len);
105
106 void Expect(Token::Kind kind);
107
108 void ExpectOneOf(std::vector<Token::Kind> kinds_vec);
109
110 void TentativeParsingRollback(uint32_t saved_idx) {
111 if (m_error)
112 llvm::consumeError(std::move(m_error));
113 m_dil_lexer.ResetTokenIdx(saved_idx);
114 }
115
116 Token CurToken() { return m_dil_lexer.GetCurrentToken(); }
117
118 // Parser doesn't own the evaluation context. The produced AST may depend on
119 // it (for example, for source locations), so it's expected that expression
120 // context will outlive the parser.
121 std::shared_ptr<StackFrame> m_ctx_scope;
122
123 llvm::StringRef m_input_expr;
124
126
127 // Holds an error if it occures during parsing.
128 llvm::Error &m_error;
129
134}; // class DILParser
135
136} // namespace lldb_private::dil
137
138#endif // LLDB_VALUEOBJECT_DILPARSER_H
static llvm::raw_ostream & error(Stream &strm)
DILDiagnosticError(DiagnosticDetail detail)
Definition DILParser.h:41
std::unique_ptr< CloneableError > Clone() const override
Definition DILParser.h:48
llvm::ArrayRef< DiagnosticDetail > GetDetails() const override
Definition DILParser.h:52
std::string message() const override
Definition DILParser.h:56
Class for doing the simple lexing required by DIL.
Definition DILLexer.h:72
void Expect(Token::Kind kind)
static llvm::Expected< ASTNodeUP > Parse(llvm::StringRef dil_input_expr, DILLexer lexer, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic, bool use_synthetic, bool fragile_ivar, bool check_ptr_vs_member)
Definition DILParser.cpp:46
lldb::DynamicValueType UseDynamic()
Definition DILParser.h:78
void TentativeParsingRollback(uint32_t saved_idx)
Definition DILParser.h:110
ASTNodeUP ParseFloatingPointLiteral()
DILParser(llvm::StringRef dil_input_expr, DILLexer lexer, std::shared_ptr< StackFrame > frame_sp, lldb::DynamicValueType use_dynamic, bool use_synthetic, bool fragile_ivar, bool check_ptr_vs_member, llvm::Error &error)
Definition DILParser.cpp:62
void ExpectOneOf(std::vector< Token::Kind > kinds_vec)
std::shared_ptr< StackFrame > m_ctx_scope
Definition DILParser.h:121
void BailOut(const std::string &error, uint32_t loc, uint16_t err_len)
std::optional< int64_t > ParseIntegerConstant()
lldb::DynamicValueType m_use_dynamic
Definition DILParser.h:130
llvm::StringRef m_input_expr
Definition DILParser.h:123
std::string ParseNestedNameSpecifier()
Class defining the tokens generated by the DIL lexer and used by the DIL parser.
Definition DILLexer.h:24
std::unique_ptr< ASTNode > ASTNodeUP
Definition DILAST.h:68
A compiler-independent representation of an lldb_private::Diagnostic.