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
356
357
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

//! Parsing of Verilog vmem files into the [`Vmem`] representation.
//!
//! See the [srec_vmem] documentation for a description of the file format.
//!
//! To summarise:
//! * Files specify hexadecimal data for sequential addresses.
//! * Start addresses for a run can be specified in hex with '@____'.
//! * Address and data values are separated by whitespace or comments.
//! * C-style '//' and '/* */' comments are supported.
//!
//! [srec_vmem]: https://srecord.sourceforge.net/man/man5/srec_vmem.5.html

use std::num::ParseIntError;

use thiserror::Error;

use super::{Section, Vmem};

pub type ParseResult<T> = Result<T, ParseError>;

/// Errors that can occur when parsing vmem files.
#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum ParseError {
    /// Failure to parse an integer from hexadecimal.
    #[error("failed to parse as hexadecimal integer")]
    ParseInt(#[from] ParseIntError),

    /// An opened comment was not closed.
    #[error("unclosed comment")]
    UnclosedComment,

    /// An address was started with an '@' character, but no address value followed.
    #[error("address is missing a value")]
    AddrMissingValue,

    /// Catch-all for any characters that don't belong in vmem files.
    #[error("unknown character '{0}'")]
    UnknownChar(char),
}
/// Representation of the possible tokens found in vmem files.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Token {
    /// End of file.
    Eof,
    /// Address directive, e.g. `@123abc`.
    Addr(u32),
    /// Data value, e.g. `abc123`.
    Value(u32),
    /// Comments, e.g. `/* comment */` or `// comment`.
    Comment,
    /// Whitespace, including newlines.
    Whitespace,
}

/// Some span of the input text representing a token.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Span {
    token: Token,
    len: usize,
}

/// Parser for vmem files.
pub struct VmemParser;

impl VmemParser {
    /// Parse a complete vmem file from a string.
    pub fn parse(mut s: &str) -> ParseResult<Vmem> {
        // Build up the vmem file as sections.
        let mut vmem = Vmem::default();
        vmem.sections.push(Section::default());

        loop {
            // Parse a token from the input string, and move along by its span.
            let Span { len, token } = Self::token(s)?;
            s = &s[len..];

            match token {
                Token::Eof => break,
                Token::Addr(addr) => {
                    // Add a new section to the `Vmem` at this address.
                    // Here we translate between a "word index" to a byte address.
                    vmem.sections.push(Section {
                        addr: addr * 4,
                        data: Vec::new(),
                    });
                }
                Token::Value(value) => {
                    // Add the value to the current (last added) section's data.
                    let section = vmem.sections.last_mut().unwrap();
                    section.data.push(value)
                }
                // Whitespace and comments are ignored.
                Token::Whitespace => continue,
                Token::Comment => continue,
            }
        }

        Ok(vmem)
    }

    /// Parse a single token from the beginning of a string.
    fn token(s: &str) -> ParseResult<Span> {
        let parsers = [
            Self::parse_eof,
            Self::parse_addr,
            Self::parse_value,
            Self::parse_comment,
            Self::parse_whitespace,
        ];

        // Run each parser in order, stopping when one gets a matching parse.
        let span = parsers.iter().find_map(|p| p(s).transpose());

        // If no parsers succeeded, return an error.
        match span {
            Some(span) => span,
            None => Err(ParseError::UnknownChar(s.chars().next().unwrap())),
        }
    }

    /// Try to parse an EOF from the beginning of a string.
    fn parse_eof(s: &str) -> ParseResult<Option<Span>> {
        // Empty strings give a 0-length `Token::Eof` span.
        match s.is_empty() {
            true => Ok(Some(Span {
                len: 0,
                token: Token::Eof,
            })),
            false => Ok(None),
        }
    }

    /// Try to parse an address from the beginning of a string.
    fn parse_addr(s: &str) -> ParseResult<Option<Span>> {
        // Check for the beginning '@' symbol.
        let Some(addr) = s.strip_prefix('@') else {
            return Ok(None);
        };

        // Find the length of the actual address string.
        let addr_len = match addr.find(|c: char| !c.is_ascii_hexdigit()) {
            Some(0) => return Err(ParseError::AddrMissingValue),
            Some(len) => len,
            None => addr.len(),
        };
        // Ensure the '@' is included in the span's length!
        let len = '@'.len_utf8() + addr_len;

        // Parse from hexadecimal.
        let val = u32::from_str_radix(&addr[..addr_len], 16)?;
        let token = Token::Addr(val);
        let span = Span { token, len };

        Ok(Some(span))
    }

    /// Try parse a value from the beginning of a string.
    fn parse_value(s: &str) -> ParseResult<Option<Span>> {
        // Check for hexadecimal characters in the input.
        let len = match s.find(|c: char| !c.is_ascii_hexdigit()) {
            Some(0) => return Ok(None),
            Some(len) => len,
            None => s.len(),
        };

        let val = u32::from_str_radix(&s[..len], 16)?;
        let token = Token::Value(val);
        let span = Span { token, len };

        Ok(Some(span))
    }

    /// Try parse a comment from the beginning of a string.
    fn parse_comment(s: &str) -> ParseResult<Option<Span>> {
        // Look for commend identifiers and their closers.
        let len = match s {
            s if s.starts_with("//") => s.find('\n').unwrap_or(s.len()),
            s if s.starts_with("/*") => {
                // `find` gives us the _start_ of the `*/`, so include its length as well.
                s.find("*/").ok_or(ParseError::UnclosedComment)? + "*/".len()
            }
            _ => return Ok(None),
        };

        let token = Token::Comment;
        let span = Span { token, len };

        Ok(Some(span))
    }

    /// Try to parse whitespace from the beginning of a string.
    fn parse_whitespace(s: &str) -> ParseResult<Option<Span>> {
        // Check for whitespace at the beginning of the input.
        let len = match s.find(|c: char| !c.is_whitespace()) {
            Some(0) => return Ok(None),
            Some(len) => len,
            None => s.len(),
        };

        let token = Token::Whitespace;
        let span = Span { len, token };

        Ok(Some(span))
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn parse() {
        let input = r#"
            AB
            // comment
            CD EF
            @42
            12 /* comment */ 34
        "#;
        let expected = Vmem {
            sections: vec![
                Section {
                    addr: 0x00,
                    data: vec![0xAB, 0xCD, 0xEF],
                },
                Section {
                    addr: 0x108,
                    data: vec![0x12, 0x34],
                },
            ],
        };

        assert_eq!(VmemParser::parse(input).unwrap(), expected);
    }

    #[test]
    fn token() {
        // Check we can pick out the correct token from a string:
        let expected = [
            ("", Token::Eof, 0),
            ("@ff", Token::Addr(0xff), 3),
            ("ff", Token::Value(0xff), 2),
            ("// X", Token::Comment, 4),
            ("/* X */", Token::Comment, 7),
            (" 	", Token::Whitespace, 2),
        ];

        for (s, token, len) in expected {
            let span = Span { token, len };
            assert_eq!(VmemParser::token(s), Ok(span));
        }

        // Unknown non-token:
        assert_eq!(VmemParser::token("X"), Err(ParseError::UnknownChar('X')));
    }

    #[test]
    fn eof() {
        // Not EOF:
        assert_eq!(VmemParser::parse_eof(" ").unwrap(), None);

        // EOF:
        let expected = Some(Span {
            len: 0,
            token: Token::Eof,
        });
        assert_eq!(VmemParser::parse_eof("").unwrap(), expected);
    }

    #[test]
    fn addr() {
        // No address:
        assert_eq!(VmemParser::parse_addr("/* X */").unwrap(), None);

        let expected = Some(Span {
            len: 9,
            token: Token::Addr(0x0123abcd),
        });
        // Partially an address:
        assert_eq!(VmemParser::parse_addr("@0123ABCD FF").unwrap(), expected);
        // Entirely an address:
        assert_eq!(VmemParser::parse_addr("@0123ABCD").unwrap(), expected);
        // Lower-case hex characters:
        assert_eq!(VmemParser::parse_addr("@0123abcd").unwrap(), expected);

        // u32 overflow:
        assert!(VmemParser::parse_addr("@123456789").is_err());
        // Missing address after '@':
        assert!(VmemParser::parse_addr("@").is_err());
        assert!(VmemParser::parse_addr("@ FF").is_err());
    }

    #[test]
    fn value() {
        // No value:
        assert_eq!(VmemParser::parse_value("/* X */").unwrap(), None);

        let expected = Some(Span {
            len: 8,
            token: Token::Value(0x0123abcd),
        });
        // Partially a value:
        assert_eq!(VmemParser::parse_value("0123ABCD FF").unwrap(), expected);
        // Entirely a value:
        assert_eq!(VmemParser::parse_value("0123ABCD").unwrap(), expected);
        // Lower-case hex characters:
        assert_eq!(VmemParser::parse_value("0123abcd").unwrap(), expected);

        // u32 overflow:
        assert!(VmemParser::parse_value("123456789").is_err());
    }

    #[test]
    fn comment() {
        // No whitespace:
        assert_eq!(VmemParser::parse_comment("FF").unwrap(), None);

        let expected = Some(Span {
            len: 7,
            token: Token::Comment,
        });

        // Partial block comment:
        assert_eq!(VmemParser::parse_comment("/* X */ FF").unwrap(), expected);
        // Entirely a block comment:
        assert_eq!(VmemParser::parse_comment("/* X */").unwrap(), expected);
        // Unclosed block comment:
        assert!(VmemParser::parse_comment("/* X").is_err());

        // Line comment ending in newline:
        assert_eq!(
            VmemParser::parse_comment(concat!("// XXXX", '\n', "FF")).unwrap(),
            expected
        );
        // Line comment ending at EOF:
        assert_eq!(VmemParser::parse_comment("// XXXX").unwrap(), expected);
    }

    #[test]
    fn whitespace() {
        // No whitespace:
        assert_eq!(VmemParser::parse_whitespace("FF").unwrap(), None);

        let expected = Some(Span {
            len: 2,
            token: Token::Whitespace,
        });
        // Partial whitespace:
        assert_eq!(VmemParser::parse_whitespace(" 	FF").unwrap(), expected);
        // Entirely whitespace:
        assert_eq!(VmemParser::parse_whitespace(" 	").unwrap(), expected);
    }
}