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