Debian Codegraph @

debian/rust-vendor/anstyle-parse/src/state/codegen.rs

raw
1 use super::{pack, unpack, Action, State};
2
3 use snapbox::file;
4 use vte_generate_state_changes::generate_state_changes;
5
6 #[test]
7 fn table() {
8 let mut content = vec![];
9 generate_table(&mut content).unwrap();
10
11 let content = String::from_utf8(content).unwrap();
12 let content = codegenrs::rustfmt(&content, None).unwrap();
13 snapbox::assert_data_eq!(content, file!["table.rs"].raw());
14 }
15
16 #[allow(clippy::write_literal)]
17 fn generate_table(file: &mut impl std::io::Write) -> std::io::Result<()> {
18 writeln!(
19 file,
20 "// This file is @generated by {}",
21 std::file!().replace('\\', "/")
22 )?;
23 writeln!(file)?;
24 writeln!(
25 file,
26 "{}",
27 r#"#[rustfmt::skip]
28 pub(crate) const STATE_CHANGES: [[u8; 256]; 16] = ["#
29 )?;
30
31 for (state, entries) in STATE_CHANGES.iter().enumerate() {
32 writeln!(file, " // {:?}", State::try_from(state as u8).unwrap())?;
33 write!(file, " [")?;
34 let mut last_entry = None;
35 for packed in entries {
36 let (next_state, action) = unpack(*packed);
37 if last_entry != Some(packed) {
38 writeln!(file)?;
39 writeln!(file, " // {next_state:?} {action:?}")?;
40 write!(file, " ")?;
41 }
42 write!(file, "0x{packed:0>2x}, ")?;
43 last_entry = Some(packed);
44 }
45 writeln!(file)?;
46 writeln!(file, " ],")?;
47 }
48
49 writeln!(file, "{}", r#"];"#)?;
50 Ok(())
51 }
52
53 /// This is the state change table. It's indexed first by current state and then by the next
54 /// character in the pty stream.
55 pub(crate) static STATE_CHANGES: [[u8; 256]; 16] = state_changes();
56 generate_state_changes!(state_changes, {
57 Anywhere {
58 0x18 => (Ground, Execute),
59 0x1a => (Ground, Execute),
60 0x1b => (Escape, Nop),
61 },
62
63 Ground {
64 0x00..=0x17 => (Anywhere, Execute),
65 0x19 => (Anywhere, Execute),
66 0x1c..=0x1f => (Anywhere, Execute),
67 0x20..=0x7f => (Anywhere, Print),
68 0x80..=0x8f => (Anywhere, Execute),
69 0x91..=0x9a => (Anywhere, Execute),
70 0x9c => (Anywhere, Execute),
71 // Beginning of UTF-8 2 byte sequence
72 0xc2..=0xdf => (Utf8, BeginUtf8),
73 // Beginning of UTF-8 3 byte sequence
74 0xe0..=0xef => (Utf8, BeginUtf8),
75 // Beginning of UTF-8 4 byte sequence
76 0xf0..=0xf4 => (Utf8, BeginUtf8),
77 },
78
79 Escape {
80 0x00..=0x17 => (Anywhere, Execute),
81 0x19 => (Anywhere, Execute),
82 0x1c..=0x1f => (Anywhere, Execute),
83 0x7f => (Anywhere, Ignore),
84 0x20..=0x2f => (EscapeIntermediate, Collect),
85 0x30..=0x4f => (Ground, EscDispatch),
86 0x51..=0x57 => (Ground, EscDispatch),
87 0x59 => (Ground, EscDispatch),
88 0x5a => (Ground, EscDispatch),
89 0x5c => (Ground, EscDispatch),
90 0x60..=0x7e => (Ground, EscDispatch),
91 0x5b => (CsiEntry, Nop),
92 0x5d => (OscString, Nop),
93 0x50 => (DcsEntry, Nop),
94 0x58 => (SosPmApcString, Nop),
95 0x5e => (SosPmApcString, Nop),
96 0x5f => (SosPmApcString, Nop),
97 },
98
99 EscapeIntermediate {
100 0x00..=0x17 => (Anywhere, Execute),
101 0x19 => (Anywhere, Execute),
102 0x1c..=0x1f => (Anywhere, Execute),
103 0x20..=0x2f => (Anywhere, Collect),
104 0x7f => (Anywhere, Ignore),
105 0x30..=0x7e => (Ground, EscDispatch),
106 },
107
108 CsiEntry {
109 0x00..=0x17 => (Anywhere, Execute),
110 0x19 => (Anywhere, Execute),
111 0x1c..=0x1f => (Anywhere, Execute),
112 0x7f => (Anywhere, Ignore),
113 0x20..=0x2f => (CsiIntermediate, Collect),
114 0x30..=0x39 => (CsiParam, Param),
115 0x3a..=0x3b => (CsiParam, Param),
116 0x3c..=0x3f => (CsiParam, Collect),
117 0x40..=0x7e => (Ground, CsiDispatch),
118 },
119
120 CsiIgnore {
121 0x00..=0x17 => (Anywhere, Execute),
122 0x19 => (Anywhere, Execute),
123 0x1c..=0x1f => (Anywhere, Execute),
124 0x20..=0x3f => (Anywhere, Ignore),
125 0x7f => (Anywhere, Ignore),
126 0x40..=0x7e => (Ground, Nop),
127 },
128
129 CsiParam {
130 0x00..=0x17 => (Anywhere, Execute),
131 0x19 => (Anywhere, Execute),
132 0x1c..=0x1f => (Anywhere, Execute),
133 0x30..=0x39 => (Anywhere, Param),
134 0x3a..=0x3b => (Anywhere, Param),
135 0x7f => (Anywhere, Ignore),
136 0x3c..=0x3f => (CsiIgnore, Nop),
137 0x20..=0x2f => (CsiIntermediate, Collect),
138 0x40..=0x7e => (Ground, CsiDispatch),
139 },
140
141 CsiIntermediate {
142 0x00..=0x17 => (Anywhere, Execute),
143 0x19 => (Anywhere, Execute),
144 0x1c..=0x1f => (Anywhere, Execute),
145 0x20..=0x2f => (Anywhere, Collect),
146 0x7f => (Anywhere, Ignore),
147 0x30..=0x3f => (CsiIgnore, Nop),
148 0x40..=0x7e => (Ground, CsiDispatch),
149 },
150
151 DcsEntry {
152 0x00..=0x17 => (Anywhere, Ignore),
153 0x19 => (Anywhere, Ignore),
154 0x1c..=0x1f => (Anywhere, Ignore),
155 0x7f => (Anywhere, Ignore),
156 0x20..=0x2f => (DcsIntermediate, Collect),
157 0x30..=0x39 => (DcsParam, Param),
158 0x3a..=0x3b => (DcsParam, Param),
159 0x3c..=0x3f => (DcsParam, Collect),
160 0x40..=0x7e => (DcsPassthrough, Nop),
161 },
162
163 DcsIntermediate {
164 0x00..=0x17 => (Anywhere, Ignore),
165 0x19 => (Anywhere, Ignore),
166 0x1c..=0x1f => (Anywhere, Ignore),
167 0x20..=0x2f => (Anywhere, Collect),
168 0x7f => (Anywhere, Ignore),
169 0x30..=0x3f => (DcsIgnore, Nop),
170 0x40..=0x7e => (DcsPassthrough, Nop),
171 },
172
173 DcsIgnore {
174 0x00..=0x17 => (Anywhere, Ignore),
175 0x19 => (Anywhere, Ignore),
176 0x1c..=0x1f => (Anywhere, Ignore),
177 0x20..=0x7f => (Anywhere, Ignore),
178 0x9c => (Ground, Nop),
179 },
180
181 DcsParam {
182 0x00..=0x17 => (Anywhere, Ignore),
183 0x19 => (Anywhere, Ignore),
184 0x1c..=0x1f => (Anywhere, Ignore),
185 0x30..=0x39 => (Anywhere, Param),
186 0x3a..=0x3b => (Anywhere, Param),
187 0x7f => (Anywhere, Ignore),
188 0x3c..=0x3f => (DcsIgnore, Nop),
189 0x20..=0x2f => (DcsIntermediate, Collect),
190 0x40..=0x7e => (DcsPassthrough, Nop),
191 },
192
193 DcsPassthrough {
194 0x00..=0x17 => (Anywhere, Put),
195 0x19 => (Anywhere, Put),
196 0x1c..=0x1f => (Anywhere, Put),
197 0x20..=0x7e => (Anywhere, Put),
198 0x7f => (Anywhere, Ignore),
199 0x9c => (Ground, Nop),
200 },
201
202 SosPmApcString {
203 0x00..=0x17 => (Anywhere, Ignore),
204 0x19 => (Anywhere, Ignore),
205 0x1c..=0x1f => (Anywhere, Ignore),
206 0x20..=0x7f => (Anywhere, Ignore),
207 0x9c => (Ground, Nop),
208 },
209
210 OscString {
211 0x00..=0x06 => (Anywhere, Ignore),
212 0x07 => (Ground, Nop),
213 0x08..=0x17 => (Anywhere, Ignore),
214 0x19 => (Anywhere, Ignore),
215 0x1c..=0x1f => (Anywhere, Ignore),
216 0x20..=0xff => (Anywhere, OscPut),
217 }
218 });