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