BashSpark
Loading...
Searching...
No Matches
token_holder.h
Go to the documentation of this file.
1
30#pragma once
31
32#include <cstdint>
33
36
37namespace bs {
48 public:
56 ifakestream &oIstream,
57 std::vector<shell_token> vTokens
58 ) : m_oIstream(oIstream),
59 m_vTokens(std::move(vTokens)) {
60 }
61
67 [[nodiscard]] const shell_token *current() const noexcept {
68 if (m_nPos >= m_vTokens.size() || m_nPos < 0) return nullptr;
69 return &m_vTokens[m_nPos];
70 }
71
77 [[nodiscard]] const shell_token *next() const noexcept {
78 if (m_nPos + 1 >= m_vTokens.size() || m_nPos + 1 < 0) return nullptr;
79 return &m_vTokens[m_nPos + 1];
80 }
81
87 [[nodiscard]] const shell_token *previous() const noexcept {
88 if (m_nPos >= m_vTokens.size() + 1 || m_nPos <= 0) return nullptr;
89 return &m_vTokens[m_nPos - 1];
90 }
91
97 const shell_token *get() noexcept {
98 ++m_nPos;
99 if (m_nPos >= m_vTokens.size()) return nullptr;
100 return &m_vTokens[m_nPos];
101 }
102
108 void put_back() noexcept {
109 --m_nPos;
110 }
111
117 [[nodiscard]] std::size_t pos() const noexcept {
118 if (m_nPos >= m_vTokens.size() || m_nPos < 0) return m_oIstream.size();
119 return m_nPos;
120 }
121
128 [[nodiscard]] bool is(const shell_token_type nType) const noexcept {
129 if (m_nPos >= m_vTokens.size() || m_nPos < 0) return false;
130 return m_vTokens[m_nPos].m_nType == nType;
131 }
132
139 [[nodiscard]] bool is_next(const shell_token_type nType) const noexcept {
140 if (m_nPos + 1 >= m_vTokens.size()) return false;
141 return m_vTokens[m_nPos + 1].m_nType == nType;
142 }
143
149 [[nodiscard]] shell_keyword keyword() const noexcept;
150
157 [[nodiscard]] bool keyword(const shell_keyword nKeyword) const noexcept {
158 return this->keyword() == nKeyword;
159 }
160
161 private:
163 ifakestream &m_oIstream;
165 std::vector<shell_token> m_vTokens;
167 std::intptr_t m_nPos = -1;
168 };
169
170 inline shell_keyword token_holder::keyword() const noexcept {
171 // Get current and next
172 const auto pCurrent = this->current();
173 if (pCurrent == nullptr) return shell_keyword::SK_NONE;
174 const auto pNext = this->next();
175
176 // Finish if next is null
177 if (pNext == nullptr) {
178 if (pCurrent->m_nType == shell_token_type::TK_WORD)
179 return get_keyword_id(pCurrent->m_sTokenText);
181 }
182
183 // Check next token
184 if (
185 const auto nTypeNext = pNext->m_nType;
186 nTypeNext != shell_token_type::TK_SPACE
194 ) {
196 }
197
198 if (pCurrent->m_nType == shell_token_type::TK_WORD)
199 return get_keyword_id(pCurrent->m_sTokenText);
201 }
202}
A class for input stream behavior with a character type.
Definition fakestream.h:70
ALWAYS_INLINE std::size_t size() const noexcept
Gets the stream data size.
Definition fakestream.h:191
A class that manages a collection of shell tokens for processing.
Definition token_holder.h:47
shell_keyword keyword() const noexcept
Retrieves the current shell keyword.
Definition token_holder.h:170
const shell_token * next() const noexcept
Retrieves the next token.
Definition token_holder.h:77
std::size_t pos() const noexcept
Retrieves the current position of the token holder.
Definition token_holder.h:117
bool is_next(const shell_token_type nType) const noexcept
Checks if the next token is of a specified type.
Definition token_holder.h:139
void put_back() noexcept
Moves the position back by one token.
Definition token_holder.h:108
const shell_token * previous() const noexcept
Retrieves the previous token.
Definition token_holder.h:87
const shell_token * current() const noexcept
Retrieves the current token.
Definition token_holder.h:67
token_holder(ifakestream &oIstream, std::vector< shell_token > vTokens)
Constructs a token_holder instance.
Definition token_holder.h:55
bool is(const shell_token_type nType) const noexcept
Checks if the current token is of a specified type.
Definition token_holder.h:128
const shell_token * get() noexcept
Retrieves and advances to the next token.
Definition token_holder.h:97
BashSpark main namespace.
Definition command.h:39
shell_token_type
Enumeration of types of tokens that can be parsed from shell commands.
Definition shell_tokenizer.h:44
@ TK_CMD_SEPARATOR
Represents command separators (e.g., ;).
@ TK_OPEN_PARENTHESIS
Represents an open parenthesis ('(').
@ TK_CLOSE_SQR_BRACKETS
Represents a close square bracket (']').
@ TK_OPEN_BRACKETS
Represents an open bracket ('{').
@ TK_CLOSE_PARENTHESIS
Represents a close parenthesis (')').
@ TK_WORD
Represents a word in the command.
@ TK_SPACE
Represents whitespace in the command.
@ TK_CLOSE_BRACKETS
Represents a close bracket ('}').
@ TK_OPEN_SQR_BRACKETS
Represents an open square bracket ('[').
shell_keyword get_keyword_id(const std::string_view &oString)
Identifies shell special keywords.
Definition shell_tools.h:152
shell_keyword
Enumeration of shell keywords for scripting.
Definition shell_keyword.h:39
@ SK_NONE
No keyword (default value).
Defines enum bs::shell_keyword.
Defines class bs::shell_tokenizer.
Represents a token generated during command parsing.
Definition shell_tokenizer.h:82