Debian Codegraph @

src/lex.rs

raw symbols SCIP
1 use crate::common;
2
3 /// Syntax kinds for tokens and composite nodes in deb822 files.
4 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
5 #[allow(non_camel_case_types)]
6 #[allow(clippy::upper_case_acronyms)]
7 #[repr(u16)]
8 pub enum SyntaxKind {
9 /// Field name/key
10 KEY = 0,
11 /// Field value
12 VALUE,
13 /// Colon separator between key and value
14 COLON,
15 /// Indentation (spaces or tabs)
16 INDENT,
17 /// Newline character
18 NEWLINE,
19 /// Whitespace (spaces or tabs, but not newlines)
20 WHITESPACE,
21 /// Comment line (starts with #)
22 COMMENT,
23 /// Error token for invalid syntax
24 ERROR,
25
26 // composite nodes
27 /// The entire file
28 ROOT,
29 /// A deb822 paragraph (collection of entries)
30 PARAGRAPH,
31 /// A single key-value pair
32 ENTRY,
33 /// An empty line (separates paragraphs)
34 EMPTY_LINE,
35 }
36
37 /// Convert our `SyntaxKind` into the rowan `SyntaxKind`.
38 impl From<SyntaxKind> for rowan::SyntaxKind {
39 fn from(kind: SyntaxKind) -> Self {
40 Self(kind as u16)
41 }
42 }
43
44 fn lex_(mut input: &str, mut start_of_line: bool) -> impl Iterator<Item = (SyntaxKind, &str)> {
45 let mut colon_count = if start_of_line { 0 } else { 1 };
46 let mut indent = 0;
47
48 std::iter::from_fn(move || {
49 if let Some(c) = input.chars().next() {
50 match c {
51 ':' if colon_count == 0 && indent == 0 => {
52 colon_count += 1;
53 let char_len = c.len_utf8();
54 input = &input[char_len..];
55 Some((SyntaxKind::COLON, ":"))
56 }
57 _ if common::is_newline(c) => {
58 let char_len = c.len_utf8();
59 let (nl, remaining) = input.split_at(char_len);
60 input = remaining;
61 start_of_line = true;
62 colon_count = 0;
63 indent = 0;
64 Some((SyntaxKind::NEWLINE, nl))
65 }
66 _ if common::is_indent(c) => {
67 let end_pos = input.find(|c| !common::is_indent(c)).unwrap_or(input.len());
68 let (whitespace, remaining) = input.split_at(end_pos);
69 input = remaining;
70 if start_of_line {
71 indent = whitespace.len();
72 Some((SyntaxKind::INDENT, whitespace))
73 } else {
74 Some((SyntaxKind::WHITESPACE, whitespace))
75 }
76 }
77 '#' if start_of_line => {
78 let (comment, remaining) =
79 input.split_at(input.find(common::is_newline).unwrap_or(input.len()));
80 input = remaining;
81 start_of_line = true;
82 colon_count = 0;
83 Some((SyntaxKind::COMMENT, comment))
84 }
85 _ if common::is_valid_initial_key_char(c) && start_of_line && indent == 0 => {
86 let (key, remaining) = input.split_at(
87 input
88 .find(|c| !common::is_valid_key_char(c))
89 .unwrap_or(input.len()),
90 );
91 input = remaining;
92 start_of_line = false;
93 Some((SyntaxKind::KEY, key))
94 }
95 _ if !start_of_line || indent > 0 => {
96 let (value, remaining) =
97 input.split_at(input.find(common::is_newline).unwrap_or(input.len()));
98 input = remaining;
99 Some((SyntaxKind::VALUE, value))
100 }
101 _ => {
102 let char_len = c.len_utf8();
103 let (text, remaining) = input.split_at(char_len);
104 input = remaining;
105 Some((SyntaxKind::ERROR, text))
106 }
107 }
108 } else {
109 None
110 }
111 })
112 }
113
114 pub(crate) fn lex(input: &str) -> impl Iterator<Item = (SyntaxKind, &str)> {
115 lex_(input, true)
116 }
117
118 pub(crate) fn lex_inline(input: &str) -> impl Iterator<Item = (SyntaxKind, &str)> {
119 lex_(input, false)
120 }
121
122 #[cfg(test)]
123 mod tests {
124 use super::SyntaxKind::*;
125 #[test]
126 fn test_empty() {
127 assert_eq!(super::lex("").collect::<Vec<_>>(), vec![]);
128 }
129
130 #[test]
131 fn test_simple() {
132 assert_eq!(
133 super::lex(
134 r#"Source: syncthing-gtk
135 Maintainer: Jelmer Vernooij <[email protected]>
136 Section: net
137
138 # This is the first binary package:
139
140 Package: syncthing-gtk
141 Architecture: all
142 Depends:
143 foo,
144 bar,
145 blah (= 1.0)
146 Description: a package
147 with a loooong
148 .
149 long
150 .
151 description
152 "#
153 )
154 .collect::<Vec<_>>(),
155 vec![
156 (KEY, "Source"),
157 (COLON, ":"),
158 (WHITESPACE, " "),
159 (VALUE, "syncthing-gtk"),
160 (NEWLINE, "\n"),
161 (KEY, "Maintainer"),
162 (COLON, ":"),
163 (WHITESPACE, " "),
164 (VALUE, "Jelmer Vernooij <[email protected]>"),
165 (NEWLINE, "\n"),
166 (KEY, "Section"),
167 (COLON, ":"),
168 (WHITESPACE, " "),
169 (VALUE, "net "),
170 (NEWLINE, "\n"),
171 (NEWLINE, "\n"),
172 (COMMENT, "# This is the first binary package:"),
173 (NEWLINE, "\n"),
174 (NEWLINE, "\n"),
175 (KEY, "Package"),
176 (COLON, ":"),
177 (WHITESPACE, " "),
178 (VALUE, "syncthing-gtk"),
179 (NEWLINE, "\n"),
180 (KEY, "Architecture"),
181 (COLON, ":"),
182 (WHITESPACE, " "),
183 (VALUE, "all"),
184 (NEWLINE, "\n"),
185 (KEY, "Depends"),
186 (COLON, ":"),
187 (WHITESPACE, " "),
188 (NEWLINE, "\n"),
189 (INDENT, " "),
190 (VALUE, "foo,"),
191 (NEWLINE, "\n"),
192 (INDENT, " "),
193 (VALUE, "bar,"),
194 (NEWLINE, "\n"),
195 (INDENT, " "),
196 (VALUE, "blah (= 1.0)"),
197 (NEWLINE, "\n"),
198 (KEY, "Description"),
199 (COLON, ":"),
200 (WHITESPACE, " "),
201 (VALUE, "a package"),
202 (NEWLINE, "\n"),
203 (INDENT, " "),
204 (VALUE, "with a loooong"),
205 (NEWLINE, "\n"),
206 (INDENT, " "),
207 (VALUE, "."),
208 (NEWLINE, "\n"),
209 (INDENT, " "),
210 (VALUE, "long"),
211 (NEWLINE, "\n"),
212 (INDENT, " "),
213 (VALUE, "."),
214 (NEWLINE, "\n"),
215 (INDENT, " "),
216 (VALUE, "description"),
217 (NEWLINE, "\n")
218 ]
219 );
220 }
221
222 #[test]
223 fn test_apt() {
224 let text = r#"Package: cvsd
225 Binary: cvsd
226 Version: 1.0.24
227 Maintainer: Arthur de Jong <[email protected]>
228 Build-Depends: debhelper (>= 9), po-debconf
229 Architecture: any
230 Standards-Version: 3.9.3
231 Format: 3.0 (native)
232 Files:
233 b7a7d67a02974c52c408fdb5e118406d 890 cvsd_1.0.24.dsc
234 b73ee40774c3086cb8490cdbb96ac883 258139 cvsd_1.0.24.tar.gz
235 Vcs-Browser: http://arthurdejong.org/viewvc/cvsd/
236 # A comment
237 Vcs-Cvs: :pserver:[email protected]:/arthur/
238 Checksums-Sha256:
239 a7bb7a3aacee19cd14ce5c26cb86e348b1608e6f1f6e97c6ea7c58efa440ac43 890 cvsd_1.0.24.dsc
240 46bc517760c1070ae408693b89603986b53e6f068ae6bdc744e2e830e46b8cba 258139 cvsd_1.0.24.tar.gz
241 Homepage: http://arthurdejong.org/cvsd/
242 Package-List:
243 cvsd deb vcs optional
244 Directory: pool/main/c/cvsd
245 Priority: source
246 Section: vcs
247
248 "#;
249 let tokens = super::lex(text);
250 assert_eq!(
251 tokens
252 .collect::<Vec<_>>(),
253 vec![
254 (KEY, "Package"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "cvsd"), (NEWLINE, "\n"),
255 (KEY, "Binary"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "cvsd"), (NEWLINE, "\n"),
256 (KEY, "Version"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "1.0.24"), (NEWLINE, "\n"),
257 (KEY, "Maintainer"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "Arthur de Jong <[email protected]>"), (NEWLINE, "\n"),
258 (KEY, "Build-Depends"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "debhelper (>= 9), po-debconf"), (NEWLINE, "\n"),
259 (KEY, "Architecture"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "any"), (NEWLINE, "\n"),
260 (KEY, "Standards-Version"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "3.9.3"), (NEWLINE, "\n"),
261 (KEY, "Format"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "3.0 (native)"), (NEWLINE, "\n"),
262 (KEY, "Files"), (COLON, ":"), (NEWLINE, "\n"), (INDENT, " "), (VALUE, "b7a7d67a02974c52c408fdb5e118406d 890 cvsd_1.0.24.dsc"), (NEWLINE, "\n"), (INDENT, " "), (VALUE, "b73ee40774c3086cb8490cdbb96ac883 258139 cvsd_1.0.24.tar.gz"), (NEWLINE, "\n"),
263 (KEY, "Vcs-Browser"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "http://arthurdejong.org/viewvc/cvsd/"), (NEWLINE, "\n"),
264 (COMMENT, "# A comment"), (NEWLINE, "\n"),
265 (KEY, "Vcs-Cvs"), (COLON, ":"), (WHITESPACE, " "), (VALUE, ":pserver:[email protected]:/arthur/"), (NEWLINE, "\n"),
266 (KEY, "Checksums-Sha256"), (COLON, ":"), (NEWLINE, "\n"), (INDENT, " "), (VALUE, "a7bb7a3aacee19cd14ce5c26cb86e348b1608e6f1f6e97c6ea7c58efa440ac43 890 cvsd_1.0.24.dsc"), (NEWLINE, "\n"), (INDENT, " "), (VALUE, "46bc517760c1070ae408693b89603986b53e6f068ae6bdc744e2e830e46b8cba 258139 cvsd_1.0.24.tar.gz"), (NEWLINE, "\n"),
267 (KEY, "Homepage"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "http://arthurdejong.org/cvsd/"), (NEWLINE, "\n"),
268 (KEY, "Package-List"), (COLON, ":"), (NEWLINE, "\n"), (INDENT, " "), (VALUE, "cvsd deb vcs optional"), (NEWLINE, "\n"),
269 (KEY, "Directory"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "pool/main/c/cvsd"), (NEWLINE, "\n"),
270 (KEY, "Priority"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "source"), (NEWLINE, "\n"),
271 (KEY, "Section"), (COLON, ":"), (WHITESPACE, " "), (VALUE, "vcs"), (NEWLINE, "\n"), (NEWLINE, "\n")
272 ]
273 );
274 }
275
276 #[test]
277 fn test_lex_inline() {
278 let text = r"syncthing-gtk";
279 let tokens = super::lex_inline(text);
280 assert_eq!(tokens.collect::<Vec<_>>(), vec![(VALUE, "syncthing-gtk")]);
281 }
282
283 #[test]
284 fn test_lex_odd_key_characters() {
285 let text = "foo-bar: baz\n";
286
287 let tokens = super::lex(text);
288
289 assert_eq!(
290 tokens.collect::<Vec<_>>(),
291 vec![
292 (KEY, "foo-bar"),
293 (COLON, ":"),
294 (WHITESPACE, " "),
295 (VALUE, "baz"),
296 (NEWLINE, "\n")
297 ]
298 );
299 }
300
301 #[test]
302 fn test_lex_substvar_continuation() {
303 let text = "Depends: python3-bcrypt,\n ${shlibs:Depends},\nSuggests: foo\n";
304 let tokens = super::lex(text).collect::<Vec<_>>();
305 assert_eq!(
306 tokens,
307 vec![
308 (KEY, "Depends"),
309 (COLON, ":"),
310 (WHITESPACE, " "),
311 (VALUE, "python3-bcrypt,"),
312 (NEWLINE, "\n"),
313 (INDENT, " "),
314 (VALUE, "${shlibs:Depends},"),
315 (NEWLINE, "\n"),
316 (KEY, "Suggests"),
317 (COLON, ":"),
318 (WHITESPACE, " "),
319 (VALUE, "foo"),
320 (NEWLINE, "\n"),
321 ]
322 );
323 }
324
325 #[test]
326 fn test_lex_continuation_starting_with_colon() {
327 // Test continuation line that STARTS with a colon
328 // This was the bug reported in issue #315
329 let text = "Package: test\nDescription: short\n :value\n";
330 let tokens = super::lex(text).collect::<Vec<_>>();
331
332 assert_eq!(
333 tokens,
334 vec![
335 (KEY, "Package"),
336 (COLON, ":"),
337 (WHITESPACE, " "),
338 (VALUE, "test"),
339 (NEWLINE, "\n"),
340 (KEY, "Description"),
341 (COLON, ":"),
342 (WHITESPACE, " "),
343 (VALUE, "short"),
344 (NEWLINE, "\n"),
345 (INDENT, " "),
346 (VALUE, ":value"),
347 (NEWLINE, "\n")
348 ]
349 );
350 }
351 }