myshell 2.0.0
Loading...
Searching...
No Matches
msh_token.h
Go to the documentation of this file.
1//
2// Created by andrew on 10/9/23.
3//
9#ifndef MYSHELL_MSH_TOKEN_H
10#define MYSHELL_MSH_TOKEN_H
11
12#include <string>
13#include <utility>
14#include <map>
15#include <vector>
16
17struct Token;
18using tokens_t = std::vector<Token>;
19
20enum class TokenType {
21 EMPTY, // TODO! Properly handle <newline> <carriage return> etc.
22 WORD,
23 COMMAND,
24 AMP,
25 AND,
26 PIPE,
27 PIPE_AMP,
28 OR,
29 OUT,
30 OUT_APPEND,
31 IN,
32 OUT_AMP,
33 IN_AMP,
34 AMP_OUT,
35 AMP_APPEND,
36 SEMICOLON,
37 DQSTRING,
38 SQSTRING,
39 VAR_DECL,
40 SUBOPEN,
41 SUBCLOSE,
42 COM_SUB,
43};
44
45extern const std::map<TokenType, int> token_flags;
46
54constexpr int UNSUPPORTED = 1 << 0;
55constexpr int GLOB_EXPAND = 1 << 1;
56constexpr int VAR_EXPAND = 1 << 2;
57constexpr int WORD_LIKE = 1 << 3;
58constexpr int NO_WORD_SPLIT = 1 << 4;
59constexpr int COMMAND_SEPARATOR = 1 << 5;
60constexpr int REDIRECT = 1 << 6;
61constexpr int ASSIGNMENT_WORD = 1 << 7;
74struct Token {
75 TokenType type = TokenType::EMPTY;
76 std::string value;
77 int flags = 0;
78
79 Token() = default;
80
81 explicit Token(TokenType t) : type(t) {
82 flags = token_flags.at(type);
83 }
84
85 Token(TokenType t, std::string value) : type(t), value(std::move(value)) {
86 flags = token_flags.at(type);
87 }
88
89 void set_type(TokenType t) {
90 type = t;
91 flags = token_flags.at(type);
92 }
93
94 [[nodiscard]] bool get_flag(int flag) const {
95 return flags & flag;
96 }
97
98 [[maybe_unused]] void set_flag(int flag) {
99 flags |= flag;
100 }
101
102 [[maybe_unused]] void unset_flag(int flag) {
103 flags &= ~flag;
104 }
105};
106
107#endif //MYSHELL_MSH_TOKEN_H
constexpr int REDIRECT
This token is a redirect. Used for parsing.
Definition msh_token.h:60
constexpr int COMMAND_SEPARATOR
This token separates simple commands. Used for parsing.
Definition msh_token.h:59
constexpr int GLOB_EXPAND
Tells the parser to expand globs in this kind of token.
Definition msh_token.h:55
constexpr int VAR_EXPAND
Tells the parser to expand variables in this kind of token.
Definition msh_token.h:56
constexpr int NO_WORD_SPLIT
This token is not eligible for word splitting.
Definition msh_token.h:58
constexpr int UNSUPPORTED
Unsupported token. Parser will throw an error if it encounters this.
Definition msh_token.h:54
constexpr int ASSIGNMENT_WORD
This token is an assignment word. Used for parsing.
Definition msh_token.h:61
constexpr int WORD_LIKE
This token is a potential argument/command that will be forwarded to argv.
Definition msh_token.h:57
const std::map< TokenType, int > token_flags
The internal token flags table.
Definition msh_internal.cpp:43
Structure representing a token.
Definition msh_token.h:74