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