MiniOB 1
MiniOB is one mini database, helping developers to learn how database works.
载入中...
搜索中...
未找到
parser.h
1/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2miniob is licensed under Mulan PSL v2.
3You can use this software according to the terms and conditions of the Mulan PSL v2.
4You may obtain a copy of Mulan PSL v2 at:
5 http://license.coscl.org.cn/MulanPSL2
6THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
7EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
8MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9See the Mulan PSL v2 for more details. */
10
11//
12// Created by Ping Xu(haibarapink@gmail.com)
13//
14#pragma once
15
16#include <cstdlib>
17#include <iostream>
18#include <fstream>
19#include <map>
20#include <ctime>
21
22#include "common/sys/rc.h"
23#include "common/lang/string.h"
24#include "common/lang/string_view.h"
25#include "oblsm/client/cliutil/defs.h"
26
27#ifdef USE_READLINE
28#include "readline/history.h"
29#include "readline/readline.h"
30#endif
31
32#define MAX_MEM_BUFFER_SIZE 8192
33namespace oceanbase {
34
35#ifdef USE_READLINE
36inline const string HISTORY_FILE = string(getenv("HOME")) + "/.oblsm_cli.history";
37inline time_t last_history_write_time = 0;
38
39inline std::string my_readline(const string &prompt)
40{
41 int size = history_length;
42 if (size == 0) {
43 read_history(HISTORY_FILE.c_str());
44
45 std::ofstream historyFile(HISTORY_FILE, std::ios::app);
46 if (historyFile.is_open()) {
47 historyFile.close();
48 }
49 }
50
51 char *line = readline(prompt.c_str());
52 std::string result;
53 if (line != nullptr && line[0] != 0) {
54 result = line;
55 add_history(line);
56
57 if (std::time(nullptr) - last_history_write_time > 5) {
58 write_history(HISTORY_FILE.c_str());
59 }
60 }
61 free(line);
62 return result;
63}
64#else // USE_READLINE
65inline std::string my_readline(const std::string &prompt)
66{
67 std::cout << prompt;
68 std::string buffer;
69 if (std::getline(std::cin, buffer)) {
70 return buffer;
71 } else {
72 return "";
73 }
74}
75#endif // USE_READLINE
76
77enum class TokenType
78{
79 COMMAND,
80 STRING,
81 BOUND,
82 INVALID
83};
84
86{
87public:
88 TokenType token_type;
89 ObLsmCliCmdType cmd;
90 string str;
91
93 {
94#define MAP_COMMAND(cmd) token_map_[string{ObLsmCliUtil::strcmd(ObLsmCliCmdType::cmd)}] = ObLsmCliCmdType::cmd
95 MAP_COMMAND(OPEN);
96 MAP_COMMAND(CLOSE);
97 MAP_COMMAND(SET);
98 MAP_COMMAND(DELETE);
99 MAP_COMMAND(SCAN);
100 MAP_COMMAND(HELP);
101 MAP_COMMAND(EXIT);
102 MAP_COMMAND(GET);
103#undef MAP_COMMAND
104 }
105
106 void init(string_view command)
107 {
108 command_ = command;
109 p_ = 0;
110 }
111
112 RC next();
113
114private:
115 bool out_of_range() { return p_ >= command_.size(); }
116 void skip_blank_space();
117 RC parse_string(string &res);
118
119 std::map<string, ObLsmCliCmdType> token_map_;
120
121 string_view command_;
122 size_t p_;
123};
124
125// Semantic checks
127{
128public:
129 struct Result
130 {
131 ObLsmCliCmdType cmd;
132 string error;
133
134 string args[2];
135 bool bounds[2] = {false, false};
136 };
137 Result result;
138 RC parse(string_view command);
139
140private:
141 ObLsmCliCmdTokenizer tokenizer_;
142};
143
144} // namespace oceanbase
Definition: parser.h:127
Definition: parser.h:86
Definition: parser.h:130