-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
355 lines (323 loc) · 12 KB
/
build.rs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* Copyright (c) 2024, National Research Foundation (SARAO)
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy
* of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use enum_map::{enum_map, EnumMap};
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::rc::Rc;
use katcp_codec_fsm::{Action, MessageType, State};
/// (state, char) entry in the state machine.
///
/// This is separate from the definition in src/parse.rs because it needs to
/// construct the fast_table at runtime.
#[derive(Clone, Default, Eq, PartialEq, Hash)]
struct Entry {
/// Action to apply
action: Action,
/// Next state
state: State,
/// Whether to create a new argument before applying the action
create_argument: bool,
/// Following characters that can be merged into the action
fast_table: Option<Rc<EnumMap<u8, bool>>>,
}
impl Entry {
/// Construct a new entry.
///
/// The fast_table is omitted; these are filled in later.
fn new_full(action: Action, state: State, create_argument: bool) -> Self {
Self {
action,
state,
create_argument,
fast_table: None,
}
}
/// Construct a new entry that does not start a new argument.
fn new(action: Action, state: State) -> Self {
Self::new_full(action, state, false)
}
/// Construct an entry that signals an error.
fn error() -> Self {
Self::new(Action::Error, State::Error)
}
}
/// Generic helper for building the transition table for one state.
///
/// The callback is invoked for every [u8] value. The rules for `' '`
/// and `\n` are copied over those for `\t` and `\r` respectively.
fn make_table(callback: impl Fn(u8) -> Entry) -> EnumMap<u8, Entry> {
let mut table = EnumMap::default();
for ch in 0..=255u8 {
table[ch] = callback(ch);
}
// Simplify the callers by applying some generic rules
if table[b'\n'].state == State::Error {
table[b'\n'].state = State::ErrorEndOfLine;
}
assert!(matches!(
table[b'\n'].state,
State::EndOfLine | State::ErrorEndOfLine | State::Start
));
table[b'\t'] = table[b' '].clone();
table[b'\r'] = table[b'\n'].clone();
table
}
/// Create a transition table for an error state.
fn make_error() -> EnumMap<u8, Entry> {
make_table(|_| Entry::error())
}
/// Create the transition table for [State::Start].
fn make_start() -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b' ' => Entry::new(Action::Nothing, State::Empty),
b'?' => Entry::new(Action::SetType(MessageType::Request), State::BeforeName),
b'!' => Entry::new(Action::SetType(MessageType::Reply), State::BeforeName),
b'#' => Entry::new(Action::SetType(MessageType::Inform), State::BeforeName),
b'\n' => Entry::new(Action::ResetLineLength, State::Start),
_ => Entry::error(),
})
}
/// Create the transition table for [State::Empty].
fn make_empty() -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b' ' => Entry::new(Action::Nothing, State::Empty),
b'\n' => Entry::new(Action::ResetLineLength, State::Start),
_ => Entry::error(),
})
}
/// Create the transition table for [State::BeforeName].
fn make_before_name() -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b'A'..=b'Z' | b'a'..=b'z' => Entry::new(Action::Name, State::Name),
_ => Entry::error(),
})
}
/// Create the transition table for [State::Name].
fn make_name() -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' => Entry::new(Action::Name, State::Name),
b' ' => Entry::new(Action::Nothing, State::BeforeArgument),
b'[' => Entry::new(Action::Nothing, State::BeforeId),
b'\n' => Entry::new(Action::Nothing, State::EndOfLine),
_ => Entry::error(),
})
}
/// Create the transition table for [State::BeforeId].
fn make_before_id() -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b'1'..=b'9' => Entry::new(Action::Id, State::Id),
_ => Entry::error(),
})
}
/// Create the transition table for [State::Id].
fn make_id() -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b'0'..=b'9' => Entry::new(Action::Id, State::Id),
b']' => Entry::new(Action::Nothing, State::AfterId),
_ => Entry::error(),
})
}
/// Create the transition table for [State::AfterId].
fn make_after_id() -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b' ' => Entry::new(Action::Nothing, State::BeforeArgument),
b'\n' => Entry::new(Action::Nothing, State::EndOfLine),
_ => Entry::error(),
})
}
/// Create the transition table for [State::BeforeArgument] or [State::Argument].
///
/// If `create_argument` is true, a non-space character will start a new
/// argument. This should be done for [State::BeforeArgument].
fn make_argument(create_argument: bool) -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b' ' => Entry::new(Action::Nothing, State::BeforeArgument),
b'\n' => Entry::new(Action::Nothing, State::EndOfLine),
b'\\' => Entry::new_full(Action::Nothing, State::ArgumentEscape, create_argument),
b'\0' | b'\x1B' => Entry::error(),
_ => Entry::new_full(Action::Argument, State::Argument, create_argument),
})
}
/// Create the transition table for [State::ArgumentEscape].
fn make_argument_escape() -> EnumMap<u8, Entry> {
make_table(|ch| match ch {
b'@' => Entry::new(Action::Nothing, State::Argument),
b'\\' => Entry::new(Action::ArgumentEscaped(b'\\'), State::Argument),
b'_' => Entry::new(Action::ArgumentEscaped(b' '), State::Argument),
b'0' => Entry::new(Action::ArgumentEscaped(b'\0'), State::Argument),
b'n' => Entry::new(Action::ArgumentEscaped(b'\n'), State::Argument),
b'r' => Entry::new(Action::ArgumentEscaped(b'\r'), State::Argument),
b'e' => Entry::new(Action::ArgumentEscaped(b'\x1B'), State::Argument),
b't' => Entry::new(Action::ArgumentEscaped(b'\t'), State::Argument),
_ => Entry::error(),
})
}
/// Fill in the [Entry::fast_table] slots.
fn build_fast_tables(table: &mut EnumMap<State, EnumMap<u8, Entry>>) {
type ActionDisc = std::mem::Discriminant<Action>;
let mut cache: HashMap<(State, ActionDisc), Rc<EnumMap<u8, bool>>> = HashMap::new();
// Rust borrowing rules complicate this looping. We need to mutate
// the table, which we can't do if we're borrowing it for iteration.
let states: Vec<State> = table
.iter()
.map(|(state, _)| state)
.filter(|state| !state.is_terminal())
.collect();
for src_state in states {
for ch in 0..=255u8 {
let entry = &table[src_state][ch];
if entry.state.is_terminal() || !entry.action.is_mergeable() {
continue;
}
let state = entry.state;
let key = (state, std::mem::discriminant(&entry.action));
// Lifetime of `entry` ends here, leaving `table` accessible
let fast_table = cache.entry(key).or_insert_with(|| {
let mut result = EnumMap::default();
for ch2 in 0..=255u8 {
let entry = &table[state][ch2];
result[ch2] = entry.state == state
&& std::mem::discriminant(&entry.action) == key.1
&& !entry.create_argument;
}
Rc::new(result)
});
if fast_table.values().any(|x| *x) {
table[src_state][ch].fast_table = Some(fast_table.clone());
}
}
}
}
/// Build the parser table.
fn parser_table() -> EnumMap<State, EnumMap<u8, Entry>> {
let mut table = enum_map! {
State::Start => make_start(),
State::Empty => make_empty(),
State::BeforeName => make_before_name(),
State::Name => make_name(),
State::BeforeId => make_before_id(),
State::Id => make_id(),
State::AfterId => make_after_id(),
State::BeforeArgument => make_argument(true),
State::Argument => make_argument(false),
State::ArgumentEscape => make_argument_escape(),
State::Error => make_error(),
State::EndOfLine => make_error(),
State::ErrorEndOfLine => make_error(),
};
build_fast_tables(&mut table);
table
}
fn write_parser_tables(w: &mut impl Write) -> Result<(), std::io::Error> {
let table = parser_table();
// First write each unique fast table.
let mut fast_table_names: HashMap<Rc<EnumMap<u8, bool>>, String> = HashMap::new();
let mut counter = 0;
for row in table.values() {
for entry in row.values() {
if let Some(fast) = &entry.fast_table {
let old_counter = counter;
let name = fast_table_names.entry(fast.clone()).or_insert_with(|| {
let name = format!("FAST_TABLE{counter}");
counter += 1;
name.to_owned()
});
if counter != old_counter {
// This is a new entry
writeln!(w, "const {name}: EnumMap<u8, bool> = EnumMap::from_array([")?;
for i in 0..=255u8 {
writeln!(w, " {},", fast[i])?;
}
writeln!(w, "]);")?;
}
}
}
}
// Now write the entries.
writeln!(
w,
"pub(crate) const PARSER_TABLE: EnumMap<State, EnumMap<u8, Entry>> = EnumMap::from_array(["
)?;
for row in table.values() {
writeln!(w, " EnumMap::from_array([")?;
for entry in row.values() {
writeln!(w, " Entry {{")?;
writeln!(w, " action: Action::{:?},", entry.action)?;
writeln!(w, " state: State::{:?},", entry.state)?;
writeln!(
w,
" create_argument: {:?},",
entry.create_argument
)?;
if let Some(fast_table) = &entry.fast_table {
let name = &fast_table_names[fast_table];
writeln!(w, " fast_table: Some(&{name}),")?;
} else {
writeln!(w, " fast_table: None,")?;
}
writeln!(w, " }},")?;
}
writeln!(w, " ]),")?;
}
writeln!(w, "]);")?;
Ok(())
}
fn escape(c: u8) -> u8 {
match c {
b'\r' => b'r',
b'\n' => b'n',
b'\t' => b't',
b'\x1B' => b'e',
b'\0' => b'0',
b'\\' => b'\\',
b' ' => b'_',
_ => 0, // Marker for not needing an escape
}
}
fn write_format_tables(w: &mut impl Write) -> Result<(), std::io::Error> {
writeln!(
w,
"pub(crate) const ESCAPE_SYMBOL: EnumMap<u8, u8> = EnumMap::from_array(["
)?;
for i in 0..=255u8 {
let value = escape(i);
writeln!(w, " {value},")?;
}
writeln!(w, "]);")?;
writeln!(
w,
"pub(crate) const ESCAPE_FLAG: EnumMap<u8, bool> = EnumMap::from_array(["
)?;
for i in 0..=255u8 {
let value = escape(i) != 0;
writeln!(w, " {value},")?;
}
writeln!(w, "]);")?;
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir);
let tables_path = out_path.join("tables.rs");
let mut tables_writer = fs::File::create(tables_path)?;
write_parser_tables(&mut tables_writer)?;
write_format_tables(&mut tables_writer)?;
drop(tables_writer);
println!("cargo:rerun-if-changed=build.rs");
Ok(())
}