-
Notifications
You must be signed in to change notification settings - Fork 5
/
mpw_parser.cpp
106 lines (76 loc) · 1.74 KB
/
mpw_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <algorithm>
#include "mpw_parser.h"
#include "phase3_parser.h"
#include "command.h"
#include "error.h"
mpw_parser::mpw_parser(Environment &e, fdmask fds, bool interactive) : _env(e), _fds(fds), _interactive(interactive)
{
_p3 = phase3::make();
_p2.set_next([this](int type, std::string &&s){
_p3->parse(type, std::move(s));
});
_p1.set_next([this](std::string &&s){
_p2.parse(std::move(s));
});
}
mpw_parser::~mpw_parser() {
}
bool mpw_parser::continuation() const {
if (_p1.continuation()) return true;
if (_p2.continuation()) return true;
if (_p3->continuation()) return true;
return false;
}
void mpw_parser::finish() {
_p1.finish();
_p2.finish();
_p3->parse(0, "");
// and now execute the commands...
execute();
}
void mpw_parser::reset() {
_p1.reset();
_p2.reset();
_p3->reset();
_abort = false;
}
void mpw_parser::abort() {
_p1.abort();
_p2.abort();
//_p3->abort();
_p3->reset();
_abort = true;
}
void mpw_parser::parse(const void *begin, const void *end) {
if (_abort) return;
_p1.parse((const unsigned char *)begin, (const unsigned char *)end);
// and execute...
execute();
}
void mpw_parser::execute() {
if (_abort) {
_p3->command_queue.clear();
return;
}
auto commands = std::move(_p3->command_queue);
_p3->command_queue.clear();
std::reverse(commands.begin(), commands.end());
command_ptr cmd;
try {
while (!commands.empty()) {
cmd = std::move(commands.back());
commands.pop_back();
cmd->execute(_env, _fds);
}
} catch (execution_of_input_terminated &ex) {
_env.status(ex.status(), false);
if (_interactive) {
if (!cmd->terminal() || !commands.empty()) {
if (ex.status()) fprintf(stderr, "### %s\n", ex.what());
}
return;
}
_abort = true;
throw;
}
}