LLDB mainline
DILLexer.h
Go to the documentation of this file.
1//===-- DILLexer.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_DILLEXER_H
10#define LLDB_VALUEOBJECT_DILLEXER_H
11
13#include "llvm/ADT/StringRef.h"
14#include "llvm/Support/Error.h"
15#include "llvm/Support/FormatVariadic.h"
16#include <cstdint>
17#include <memory>
18#include <string>
19#include <vector>
20
21namespace lldb_private::dil {
22
23/// Class defining the tokens generated by the DIL lexer and used by the
24/// DIL parser.
25class Token {
26public:
77
78 Token(Kind kind, std::string spelling, uint32_t start)
79 : m_kind(kind), m_spelling(std::move(spelling)), m_start_pos(start) {}
80
81 Kind GetKind() const { return m_kind; }
82
83 std::string GetSpelling() const { return m_spelling; }
84
85 bool Is(Kind kind) const { return m_kind == kind; }
86
87 bool IsNot(Kind kind) const { return m_kind != kind; }
88
89 bool IsOneOf(llvm::ArrayRef<Kind> kinds) const {
90 return llvm::is_contained(kinds, m_kind);
91 }
92
93 uint32_t GetLocation() const { return m_start_pos; }
94
95 static llvm::StringRef GetTokenName(Kind kind);
96
97private:
99 std::string m_spelling;
100 uint32_t m_start_pos; // within entire expression string
101};
102
103/// Class for doing the simple lexing required by DIL.
104class DILLexer {
105public:
106 /// Lexes all the tokens in expr and calls the private constructor
107 /// with the lexed tokens.
108 static llvm::Expected<DILLexer>
109 Create(llvm::StringRef expr, lldb::DILMode mode = lldb::eDILModeFull);
110
111 /// Return the current token to be handled by the DIL parser.
113
114 /// Advance the current token position by N.
115 void Advance(uint32_t N = 1) {
116 if (m_tokens_idx + N >= m_lexed_tokens.size())
117 // N is too large; advance to the end of the lexed tokens.
118 m_tokens_idx = m_lexed_tokens.size() - 1;
119 else
120 m_tokens_idx += N;
121 }
122
123 /// Return the lexed token N positions ahead of the 'current' token
124 /// being handled by the DIL parser.
125 const Token &LookAhead(uint32_t N) {
126 if (m_tokens_idx + N < m_lexed_tokens.size())
127 return m_lexed_tokens[m_tokens_idx + N];
128
129 // Last token should be an 'eof' token.
130 return m_lexed_tokens.back();
131 }
132
133 /// Return the index for the 'current' token being handled by the DIL parser.
134 uint32_t GetCurrentTokenIdx() { return m_tokens_idx; }
135
136 /// Set the index for the 'current' token (to be handled by the parser)
137 /// to a particular position. Used for either committing 'look ahead' parsing
138 /// or rolling back tentative parsing.
139 void ResetTokenIdx(uint32_t new_value) {
140 assert(new_value < m_lexed_tokens.size());
141 m_tokens_idx = new_value;
142 }
143
144 uint32_t NumLexedTokens() { return m_lexed_tokens.size(); }
145
146private:
147 DILLexer(llvm::StringRef dil_expr, std::vector<Token> lexed_tokens)
148 : m_expr(dil_expr), m_lexed_tokens(std::move(lexed_tokens)),
149 m_tokens_idx(0) {}
150
151 static llvm::Expected<Token> Lex(llvm::StringRef expr,
152 llvm::StringRef &remainder);
153
154 // The input string we are lexing & parsing.
155 llvm::StringRef m_expr;
156
157 // Holds all of the tokens lexed so far.
158 std::vector<Token> m_lexed_tokens;
159
160 // Index into m_lexed_tokens; indicates which token the DIL parser is
161 // currently trying to parse/handle.
162 uint32_t m_tokens_idx;
163};
164
165} // namespace lldb_private::dil
166
167namespace llvm {
168
169template <> struct format_provider<lldb_private::dil::Token::Kind> {
170 static void format(const lldb_private::dil::Token::Kind &k, raw_ostream &OS,
171 llvm::StringRef Options) {
172 OS << "'" << lldb_private::dil::Token::GetTokenName(k) << "'";
173 }
174};
175
176template <> struct format_provider<lldb_private::dil::Token> {
177 static void format(const lldb_private::dil::Token &t, raw_ostream &OS,
178 llvm::StringRef Options) {
180 OS << "<'" << t.GetSpelling() << "' ("
182 }
183};
184
185} // namespace llvm
186
187#endif // LLDB_VALUEOBJECT_DILLEXER_H
const Token & LookAhead(uint32_t N)
Return the lexed token N positions ahead of the 'current' token being handled by the DIL parser.
Definition DILLexer.h:125
static llvm::Expected< DILLexer > Create(llvm::StringRef expr, lldb::DILMode mode=lldb::eDILModeFull)
Lexes all the tokens in expr and calls the private constructor with the lexed tokens.
Definition DILLexer.cpp:199
void Advance(uint32_t N=1)
Advance the current token position by N.
Definition DILLexer.h:115
DILLexer(llvm::StringRef dil_expr, std::vector< Token > lexed_tokens)
Definition DILLexer.h:147
uint32_t GetCurrentTokenIdx()
Return the index for the 'current' token being handled by the DIL parser.
Definition DILLexer.h:134
const Token & GetCurrentToken()
Return the current token to be handled by the DIL parser.
Definition DILLexer.h:112
llvm::StringRef m_expr
Definition DILLexer.h:155
void ResetTokenIdx(uint32_t new_value)
Set the index for the 'current' token (to be handled by the parser) to a particular position.
Definition DILLexer.h:139
static llvm::Expected< Token > Lex(llvm::StringRef expr, llvm::StringRef &remainder)
Definition DILLexer.cpp:216
std::vector< Token > m_lexed_tokens
Definition DILLexer.h:158
Class defining the tokens generated by the DIL lexer and used by the DIL parser.
Definition DILLexer.h:25
bool IsNot(Kind kind) const
Definition DILLexer.h:87
std::string m_spelling
Definition DILLexer.h:99
static llvm::StringRef GetTokenName(Kind kind)
Definition DILLexer.cpp:21
Token(Kind kind, std::string spelling, uint32_t start)
Definition DILLexer.h:78
bool Is(Kind kind) const
Definition DILLexer.h:85
uint32_t GetLocation() const
Definition DILLexer.h:93
Kind GetKind() const
Definition DILLexer.h:81
std::string GetSpelling() const
Definition DILLexer.h:83
bool IsOneOf(llvm::ArrayRef< Kind > kinds) const
Definition DILLexer.h:89
A class that represents a running process on the host machine.
DILMode
Data Inspection Language (DIL) evaluation modes.
@ eDILModeFull
Allowed: everything supported by DIL.
static void format(const lldb_private::dil::Token &t, raw_ostream &OS, llvm::StringRef Options)
Definition DILLexer.h:177
static void format(const lldb_private::dil::Token::Kind &k, raw_ostream &OS, llvm::StringRef Options)
Definition DILLexer.h:170