Skip to content

Commit 7bb1d89

Browse files
committed
Parser: Remove heavy elements from ParseFunctionOutputs.
1 parent 7a8520a commit 7bb1d89

8 files changed

+75
-92
lines changed

include/parse_function/parallel_function_impl.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ ParallelParseFunctions<NumElements, ParseFuncs...>::operator()() {
5959
auto local_output = func();
6060
}
6161
if (local_output.work_) { // only allow one of them work
62-
if (local_output.len() > max_len_match_output.len()) {
62+
if (local_output.len_ > max_len_match_output.len_) {
6363
max_len_match_output = std::move(local_output);
6464
max_len_match_idx = running_sub_func_idx;
6565
if (!this->valid(
@@ -80,7 +80,6 @@ ParallelParseFunctions<NumElements, ParseFuncs...>::operator()() {
8080
parse_functions_);
8181
if (max_len_match_output.work_) {
8282
this->executed_mask_.set(max_len_match_idx);
83-
output.node_ = std::move(max_len_match_output.node_);
8483
diag::infos() << basic::str::from(
8584
std::string(this->calling_depth(), '>'), " ", this->kName_,
8685
basic::tui::color::Shell::colorize(

include/parse_function/recursive_function_impl.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void RecursiveParseFunctions<ParseFuncs...>::execute(
113113
auto local_output = func();
114114
}
115115
if (local_output.work_) { // only allow one of them work
116-
if (local_output.len() > max_len_match_output.len()) {
116+
if (local_output.len_ > max_len_match_output.len_) {
117117
max_len_match_output = std::move(local_output);
118118
max_len_match_idx = running_sub_func_idx;
119119
if (!this->valid(
@@ -133,7 +133,6 @@ void RecursiveParseFunctions<ParseFuncs...>::execute(
133133
},
134134
parse_functions_);
135135
if (max_len_match_output.work_) {
136-
output.node_ = std::move(max_len_match_output.node_);
137136
diag::infos() << basic::str::from(
138137
std::string(this->calling_depth(), '>'), " ", this->kName_,
139138
basic::tui::color::Shell::colorize(

include/parse_function/serial_function_impl.h

+20-28
Original file line numberDiff line numberDiff line change
@@ -72,43 +72,36 @@ ParseFunctionOutputs SerialParseFunctions<ParseFuncs...>::operator()() {
7272
if (running_sub_func_idx++ < running_sub_func_stack_top_idx) {
7373
return;
7474
}
75-
auto make_try_again =
76-
[&](basic::Vector<4, diag::DiagInputs>&& diag_inputs) {
77-
if ((running_sub_func_idx - 1) == 0) {
78-
flg_continue = false;
79-
return;
80-
}
81-
flg_not_continue = true;
82-
running_sub_func_stack_top_idx =
83-
(running_sub_func_idx - 1) - 1;
84-
func.reset();
85-
auto tmp_output(path_stack.top());
86-
tmp_output.work_ = false;
87-
lps_assert("make_try_again", path_stack.size() > 0);
88-
89-
path_stack.pop();
90-
91-
path_stack.top().concat(std::move(tmp_output), false);
92-
93-
if (!diag_inputs.empty()) {
94-
decltype(tmp_output) tmp_output0;
95-
tmp_output0.diag_inputs_ = std::move(diag_inputs);
96-
path_stack.top().concat(std::move(tmp_output0), false);
97-
}
98-
return;
99-
};
75+
auto make_try_again = [&]() {
76+
if ((running_sub_func_idx - 1) == 0) {
77+
flg_continue = false;
78+
return;
79+
}
80+
flg_not_continue = true;
81+
running_sub_func_stack_top_idx =
82+
(running_sub_func_idx - 1) - 1;
83+
func.reset();
84+
auto tmp_output(path_stack.top());
85+
tmp_output.work_ = false;
86+
lps_assert("make_try_again", path_stack.size() > 0);
87+
88+
path_stack.pop();
89+
90+
path_stack.top().concat(std::move(tmp_output), false);
91+
return;
92+
};
10093
if (flg_continue) {
10194
func.last_token(path_stack.top().last_token_);
10295
func.cur_token(path_stack.top().cur_token_);
10396

10497
if (!func.ok_to_try() || (!func.valid() && !func.opt())) {
105-
make_try_again(basic::Vector<4, diag::DiagInputs>());
98+
make_try_again();
10699
return;
107100
}
108101

109102
auto local_output = func();
110103
if (!local_output.work_ && !func.opt()) {
111-
make_try_again(std::move(local_output.diag_inputs_));
104+
make_try_again();
112105
return;
113106
}
114107

@@ -129,7 +122,6 @@ ParseFunctionOutputs SerialParseFunctions<ParseFuncs...>::operator()() {
129122
}
130123
lps_assert("serial_function_impl", path_stack.size() > 0);
131124
if (path_stack.top().work_) {
132-
path_stack.top().node_ = std::move(node);
133125
diag::infos() << basic::str::from(
134126
std::string(this->calling_depth(), '>'), " ", this->kName_,
135127
basic::tui::color::Shell::colorize(basic::str::from(" ok.\n"),

include/parser.h

+21-42
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ class Context {
4444
using executed_func_type = std::function<void(Context*)>;
4545
void with(const executed_func_type& func) { func(this); }
4646
token::TokenLists& token_lists() { return token_lists_; }
47+
const token::Token& start_token() const { return start_token_; }
48+
void start_token(const token::Token& token) { start_token_ = token; }
49+
size_t len_from_start(const token::Token& cur_token) {
50+
return token_lists_.len(start_token_, cur_token);
51+
}
4752

4853
private:
4954
token::TokenLists token_lists_;
55+
token::Token start_token_;
5056
};
5157

5258
class ContextTrait {
@@ -115,26 +121,19 @@ class Tree {
115121
};
116122

117123
struct ParseFunctionOutputs {
118-
using diag_input_type = basic::Vector<4, diag::DiagInputs>;
119-
using token_list_infos_type =
120-
basic::Vector<16, const token::TokenLists::ele_type*>;
121124
explicit ParseFunctionOutputs() = default;
122125

123-
#define SET_MOVE(A, B) \
124-
(A)->work_ = (B).work_; \
125-
(A)->last_token_ = (B).last_token_; \
126-
(A)->cur_token_ = (B).cur_token_; \
127-
(A)->diag_inputs_ = std::move((B).diag_inputs_); \
128-
(A)->node_ = std::move((B).node_); \
129-
(A)->token_list_infos_ = std::move((B).token_list_infos_);
130-
131-
#define SET(A, B) \
132-
(A)->work_ = (B).work_; \
133-
(A)->last_token_ = (B).last_token_; \
134-
(A)->cur_token_ = (B).cur_token_; \
135-
(A)->diag_inputs_ = (B).diag_inputs_; \
136-
(A)->node_ = (B).node_; \
137-
(A)->token_list_infos_ = (B).token_list_infos_;
126+
#define SET_MOVE(A, B) \
127+
(A)->work_ = (B).work_; \
128+
(A)->last_token_ = (B).last_token_; \
129+
(A)->cur_token_ = (B).cur_token_; \
130+
(A)->len_ = (B).len_;
131+
132+
#define SET(A, B) \
133+
(A)->work_ = (B).work_; \
134+
(A)->last_token_ = (B).last_token_; \
135+
(A)->cur_token_ = (B).cur_token_; \
136+
(A)->len_ = (B).len_;
138137

139138
ParseFunctionOutputs(const ParseFunctionOutputs& other) {
140139
SET(this, other);
@@ -157,38 +156,22 @@ struct ParseFunctionOutputs {
157156
#undef SET_MOVE
158157
#undef SET
159158

160-
[[nodiscard]] size_t len() const {
161-
return token_list_infos_.size();
162-
}
163-
164-
void diag_record(diag::DiagInputs&& a) {
165-
diag_inputs_.append(std::move(a));
166-
}
167-
168159
void concat(ParseFunctionOutputs&& other, bool opt = true) {
169160
if (!opt) {
170-
for (auto& a : other.diag_inputs_) {
171-
typename decltype(diag_inputs_)::ele_type b(std::move(a));
172-
this->diag_inputs_.append(std::move(b));
173-
}
174161
this->work_ = other.work_;
175162
}
176163
if (other.work_) {
177164
this->last_token_ = other.last_token_;
178165
this->cur_token_ = other.cur_token_;
179166
this->work_ = other.work_;
180-
for (auto& a : other.token_list_infos_) {
181-
this->token_list_infos_.append(a);
182-
}
167+
len_ += other.len_;
183168
}
184169
}
185170

186171
bool work_{false};
187172
token::Token last_token_;
188173
token::Token cur_token_;
189-
diag_input_type diag_inputs_;
190-
token_list_infos_type token_list_infos_;
191-
Tree::Node node_;
174+
size_t len_{0};
192175
};
193176

194177
struct ParseFunctionInputs {
@@ -361,8 +344,7 @@ class ParseFunction : public ContextTrait {
361344

362345
if (output.cur_token_.kind() != token_kind) {
363346
if (!func->opt()) {
364-
output.diag_record(
365-
diag::record(output.cur_token_, diag_kind, {output.last_token_}));
347+
//todo(@mxlol233): add diag.
366348
}
367349
output.work_ = false;
368350
return output;
@@ -372,10 +354,7 @@ class ParseFunction : public ContextTrait {
372354
output.last_token_ = output.cur_token_;
373355
output.work_ = true;
374356
output.cur_token_ = next_tok;
375-
if (next_tok.kind() != token::details::TokenKind::unknown) {
376-
output.token_list_infos_.append(
377-
&(func->context()->token_lists().at(next_tok)));
378-
}
357+
++output.len_;
379358
}
380359

381360
return output;

include/src.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ class TokenLists {
233233

234234
const ele_type& last(const Token& tok) const {
235235
lps_assert(kTag, has(tok));
236-
auto info = Info::create(tok);
237236
const auto* ptr = at(tok).last();
238237
if (!ptr) {
239238
static const ele_type kEmptyTok;
@@ -244,7 +243,6 @@ class TokenLists {
244243

245244
const ele_type& next(const Token& tok) const {
246245
lps_assert(kTag, has(tok));
247-
auto info = Info::create(tok);
248246
const auto* ptr = at(tok).next();
249247
if (!ptr) {
250248
static const ele_type kEmptyTok;
@@ -258,6 +256,20 @@ class TokenLists {
258256
return has(info);
259257
}
260258

259+
size_t len(const Token& start, const Token& end) const {
260+
lps_assert(kTag, has(start) && has(end));
261+
const Token* t = &start;
262+
int l = 0;
263+
264+
while (t != nullptr && *t != end) {
265+
auto info = Info::create(*t);
266+
t = at(info).next();
267+
++l;
268+
}
269+
lps_assert(kTag, t != nullptr);
270+
return l;
271+
}
272+
261273
void append(const Token& tok, Info last_tok_info = {0, 0}) {
262274
auto info = Info::create(tok);
263275
if (lists_.contains(tok.file_id())) {

include/token.h

+9
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ struct Token {
241241
} next_visitor_{0, 0};
242242
};
243243

244+
inline bool operator==(const Token& a, const Token& b) {
245+
return (a.file_id() == b.file_id()) && (a.offset() == b.offset()) &&
246+
(a.kind() == b.kind());
247+
}
248+
249+
inline bool operator!=(const Token& a, const Token& b) {
250+
return !(a == b);
251+
}
252+
244253
inline std::ostream& operator<<(std::ostream& s, const Token& tok) {
245254

246255
s << "kind:" << tok.kind();

src/constant_expression.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ bool Preprocessing::lex_conditional_expression(
4141
lexer.lex(next_tok);
4242
if (next_tok.kind() != token::details::TokenKind::unknown) {
4343
context->token_lists().append(next_tok);
44+
context->start_token(next_tok);
45+
params.cur_token_ = next_tok;
46+
parser::details::ConditionalExpression func(context, params);
47+
auto output = func();
48+
int dummy = -1;
4449
}
45-
params.cur_token_ = next_tok;
46-
parser::details::ConditionalExpression func(context, params);
47-
auto output = func();
48-
int dummy = -1;
4950
});
5051
}
5152

src/parser.cc

+4-12
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,10 @@ void Parser::parse(uint32_t file_id) {
4444
params.cur_token_ = next_tok;
4545
if (next_tok.kind() != token::details::TokenKind::unknown) {
4646
context->token_lists().append(next_tok);
47-
}
48-
details::TranslationUnit func(context, params);
49-
auto output = func();
50-
for (const auto& a : output.diag_inputs_) {
51-
diag::doing(a.main_token_, a.kind_, a.context_tokens_);
52-
}
53-
if (output.work_) {
54-
for (const auto& node : output.node_.sub_nodes_) {
55-
if (node->kind_ != details::ParseFunctionKind::kUnknown) {
56-
// todo(@mxlol233): handle the recursive tree nodes...
57-
}
58-
}
47+
context->start_token(next_tok);
48+
details::TranslationUnit func(context, params);
49+
auto output = func();
50+
if (output.work_) {}
5951
}
6052
});
6153
}

0 commit comments

Comments
 (0)