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:
57
58 Token(Kind kind, std::string spelling, uint32_t start)
59 : m_kind(kind), m_spelling(std::move(spelling)), m_start_pos(start) {}
60
61 Kind GetKind() const { return m_kind; }
62
63 std::string GetSpelling() const { return m_spelling; }
64
65 bool Is(Kind kind) const { return m_kind == kind; }
66
67 bool IsNot(Kind kind) const { return m_kind != kind; }
68
69 bool IsOneOf(llvm::ArrayRef<Kind> kinds) const {
70 return llvm::is_contained(kinds, m_kind);
71 }
72
73 uint32_t GetLocation() const { return m_start_pos; }
74
75 static llvm::StringRef GetTokenName(Kind kind);
76
77private:
79 std::string m_spelling;
80 uint32_t m_start_pos; // within entire expression string
81};
82
83/// Class for doing the simple lexing required by DIL.
84class DILLexer {
85public:
86 /// Lexes all the tokens in expr and calls the private constructor
87 /// with the lexed tokens.
88 static llvm::Expected<DILLexer>
89 Create(llvm::StringRef expr, lldb::DILMode mode = lldb::eDILModeFull);
90
91 /// Return the current token to be handled by the DIL parser.
93
94 /// Advance the current token position by N.
95 void Advance(uint32_t N = 1) {
96 if (m_tokens_idx + N >= m_lexed_tokens.size())
97 // N is too large; advance to the end of the lexed tokens.
98 m_tokens_idx = m_lexed_tokens.size() - 1;
99 else
100 m_tokens_idx += N;
101 }
102
103 /// Return the lexed token N positions ahead of the 'current' token
104 /// being handled by the DIL parser.
105 const Token &LookAhead(uint32_t N) {
106 if (m_tokens_idx + N < m_lexed_tokens.size())
107 return m_lexed_tokens[m_tokens_idx + N];
108
109 // Last token should be an 'eof' token.
110 return m_lexed_tokens.back();
111 }
112
113 /// Return the index for the 'current' token being handled by the DIL parser.
114 uint32_t GetCurrentTokenIdx() { return m_tokens_idx; }
115
116 /// Set the index for the 'current' token (to be handled by the parser)
117 /// to a particular position. Used for either committing 'look ahead' parsing
118 /// or rolling back tentative parsing.
119 void ResetTokenIdx(uint32_t new_value) {
120 assert(new_value < m_lexed_tokens.size());
121 m_tokens_idx = new_value;
122 }
123
124 uint32_t NumLexedTokens() { return m_lexed_tokens.size(); }
125
126private:
127 DILLexer(llvm::StringRef dil_expr, std::vector<Token> lexed_tokens)
128 : m_expr(dil_expr), m_lexed_tokens(std::move(lexed_tokens)),
129 m_tokens_idx(0) {}
130
131 static llvm::Expected<Token> Lex(llvm::StringRef expr,
132 llvm::StringRef &remainder);
133
134 // The input string we are lexing & parsing.
135 llvm::StringRef m_expr;
136
137 // Holds all of the tokens lexed so far.
138 std::vector<Token> m_lexed_tokens;
139
140 // Index into m_lexed_tokens; indicates which token the DIL parser is
141 // currently trying to parse/handle.
142 uint32_t m_tokens_idx;
143};
144
145} // namespace lldb_private::dil
146
147namespace llvm {
148
149template <> struct format_provider<lldb_private::dil::Token::Kind> {
150 static void format(const lldb_private::dil::Token::Kind &k, raw_ostream &OS,
151 llvm::StringRef Options) {
152 OS << "'" << lldb_private::dil::Token::GetTokenName(k) << "'";
153 }
154};
155
156template <> struct format_provider<lldb_private::dil::Token> {
157 static void format(const lldb_private::dil::Token &t, raw_ostream &OS,
158 llvm::StringRef Options) {
160 OS << "<'" << t.GetSpelling() << "' ("
162 }
163};
164
165} // namespace llvm
166
167#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:105
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:159
void Advance(uint32_t N=1)
Advance the current token position by N.
Definition DILLexer.h:95
DILLexer(llvm::StringRef dil_expr, std::vector< Token > lexed_tokens)
Definition DILLexer.h:127
uint32_t GetCurrentTokenIdx()
Return the index for the 'current' token being handled by the DIL parser.
Definition DILLexer.h:114
const Token & GetCurrentToken()
Return the current token to be handled by the DIL parser.
Definition DILLexer.h:92
llvm::StringRef m_expr
Definition DILLexer.h:135
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:119
static llvm::Expected< Token > Lex(llvm::StringRef expr, llvm::StringRef &remainder)
Definition DILLexer.cpp:176
std::vector< Token > m_lexed_tokens
Definition DILLexer.h:138
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:67
std::string m_spelling
Definition DILLexer.h:79
static llvm::StringRef GetTokenName(Kind kind)
Definition DILLexer.cpp:21
Token(Kind kind, std::string spelling, uint32_t start)
Definition DILLexer.h:58
bool Is(Kind kind) const
Definition DILLexer.h:65
uint32_t GetLocation() const
Definition DILLexer.h:73
Kind GetKind() const
Definition DILLexer.h:61
std::string GetSpelling() const
Definition DILLexer.h:63
bool IsOneOf(llvm::ArrayRef< Kind > kinds) const
Definition DILLexer.h:69
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:157
static void format(const lldb_private::dil::Token::Kind &k, raw_ostream &OS, llvm::StringRef Options)
Definition DILLexer.h:150