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