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