-
Notifications
You must be signed in to change notification settings - Fork 5
/
phase1.rl
177 lines (143 loc) · 2.87 KB
/
phase1.rl
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* phase1 -- strip comments and merge multi-lines.
*
*/
#include "phase1.h"
#include <stdexcept>
#include <stdint.h>
const unsigned char escape = 0xb6;
/*
* from experimentation, mpw splits on ; after variable expansion;
* this splits before. something stupid like:
* set q '"'; echo {q} ; "
* will not be handled correctly. oh well.
* (should probably just drop that and we can then combine tokenizing w/
* variable expansion)
*/
%%{
machine main;
alphtype unsigned char;
escape = 0xb6;
ws = [ \t];
nl = ('\n' | '\r');
action add_line {
/* strip trailing ws */
while (!scratch.empty() && isspace(scratch.back())) scratch.pop_back();
if (!scratch.empty()) {
std::string tmp = std::move(scratch);
scratch.clear();
if (pipe_to) pipe_to(std::move(tmp));
}
fgoto main;
}
action push_back {
scratch.push_back(fc);
}
action push_back_escape {
scratch.push_back(escape);
scratch.push_back(fc);
}
comment = '#' (any-nl)*;
escape_seq =
escape
(
nl ${ /* esc newline */ line++; }
|
(any-nl) $push_back_escape
)
;
# single-quoted string. only escape \n is special.
# handling is so stupid I'm not going to support it.
sstring =
['] $push_back
( (any-nl-[']) $push_back )*
['] $push_back
$err{
throw std::runtime_error("### MPW Shell - 's must occur in pairs.");
}
;
# todo -- {{variables}}
# same quoting logic as ' string
vstring =
'{' $push_back
( (any-nl-'}') $push_back )*
'}' $push_back
$err{
throw std::runtime_error("### MPW Shell - {s must occur in pairs.");
}
;
# double-quoted string.
# escape \n is ignored. others do nothing.
dstring =
["] $push_back
(
escape_seq
|
vstring
|
(any-escape-nl-["{]) $push_back
)* ["] $push_back
$err{
throw std::runtime_error("### MPW Shell - \"s must occur in pairs.");
}
;
# gobble up all the white space...
coalesce_ws =
ws+
<:
''
%{ if (!scratch.empty() && scratch.back() != ' ') scratch.push_back(' '); }
;
line :=
(
sstring
|
dstring
|
vstring
|
escape_seq
|
coalesce_ws
|
(any-escape-nl-ws-[#'"{]) $push_back
)*
comment?
nl ${ line++; } $add_line
;
main :=
# strip leading whitespace.
ws*
<: # left guard -- higher priority to ws.
any ${ fhold; fgoto line; }
;
}%%
namespace {
%% write data;
}
phase1::phase1() {
%% write init;
}
void phase1::reset() {
%% write init;
scratch.clear();
// line = 1?
}
void phase1::process(const unsigned char *begin, const unsigned char *end, bool final) {
int start_line;
const unsigned char *p = begin;
const unsigned char *pe = end;
const unsigned char *eof = nullptr;
if (final)
eof = pe;
%% write exec;
if (cs == main_error) {
throw std::runtime_error("MPW Shell - Lexer error.");
}
#if 0
if (cs != main_start && final) {
// will this happen?
throw std::runtime_error("MPW Shell - Lexer error.");
}
#endif
}