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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{common::Sp, errors::*};
use std::iter::Peekable;

/// The basic unit of input to the directive parser.
enum Token {
    String(String),
    QuotedString(String),
    Whitespace(String),
}

/// Find the next token at the beginning of the input char stream.
fn next_token<I>(input: &mut Peekable<I>) -> Result<Option<Token>>
where
    I: Iterator<Item = (usize, char)>,
{
    macro_rules! next {
        () => {
            input.next().map(|(_, c)| c)
        };
    }

    macro_rules! peek {
        () => {
            input.peek().map(|(_, c)| c)
        };
    }

    let res = match next!() {
        Some('"') => {
            let mut buffer = String::new();
            loop {
                match next!() {
                    Some('"') => break Some(Token::QuotedString(buffer)),
                    Some('\\') => match next!() {
                        Some('\\') => buffer.push('\\'),
                        Some('n') => buffer.push('\n'),
                        Some('t') => buffer.push('\t'),
                        Some('r') => buffer.push('\r'),
                        Some('"') => buffer.push('"'),
                        Some(c) => bail!("unrecognized escape character \\{}", c),
                        None => bail!("unclosed escape character"),
                    },
                    Some(c) => buffer.push(c),
                    None => bail!("unclosed string literal"),
                }
            }
        }
        Some(c) if c.is_ascii_whitespace() => {
            let mut buffer = String::new();
            buffer.push(c);
            loop {
                match peek!() {
                    Some(c) if c.is_ascii_whitespace() => {
                        buffer.push(*c);
                        input.next();
                    }
                    _ => break,
                }
            }
            Some(Token::Whitespace(buffer))
        }
        Some(c) => {
            let mut buffer = String::new();
            buffer.push(c);
            loop {
                match peek!() {
                    Some('"') | None => break,
                    Some(c) if c.is_ascii_whitespace() => break,

                    Some(c) => {
                        buffer.push(*c);
                        input.next();
                    }
                }
            }
            Some(Token::String(buffer))
        }
        None => None,
    };
    Ok(res)
}

/// Split the input string into tokens with spans.
/// The tokens will later be used to build directives.
fn tokenize_patterns(s: &str) -> Result<Vec<Sp<Token>>> {
    let mut input = s.char_indices().peekable();
    let mut tokens = vec![];
    #[allow(clippy::while_let_loop)]
    loop {
        let start = match input.peek() {
            Some((idx, _)) => *idx,
            None => break,
        };
        let tok = match next_token(&mut input)? {
            Some(tok) => tok,
            None => break,
        };
        let end = match input.peek() {
            Some((idx, _)) => *idx,
            None => s.len(),
        };
        tokens.push(Sp::new(tok, start, end));
    }
    Ok(tokens)
}

/// Specification of an expected text pattern in the output.
///
/// There are two types of directives: positive and negative.
/// A positive directive means the pattern should match some text in the output,
/// while a nagative one considers such match to be an error.
#[derive(Debug, Eq, PartialEq)]
pub enum Directive {
    Check(String),
    Not(String),
}

impl Directive {
    /// Returns if the directive is a positive pattern.
    pub fn is_positive(&self) -> bool {
        match self {
            Self::Check(_) => true,
            Self::Not(_) => false,
        }
    }

    /// Returns if the directive is a negative pattern.
    pub fn is_negative(&self) -> bool {
        match self {
            Self::Check(_) => false,
            Self::Not(_) => true,
        }
    }

    /// Returns the pattern of the directive.
    pub fn pattern_str(&self) -> &str {
        match self {
            Self::Check(s) | Self::Not(s) => s,
        }
    }

    /// Parses the line and extracts one or more directives from it.
    pub fn parse_line(s: &str) -> Result<Vec<Sp<Directive>>> {
        // TODO: rewrite how the offset is counted.
        let mut offset = 0;

        macro_rules! trim {
            ($s: expr) => {{
                let s = $s;
                let mut iter = s.char_indices();
                loop {
                    match iter.next() {
                        Some((idx, c)) => {
                            if !c.is_ascii_whitespace() {
                                offset += idx;
                                break &s[idx..];
                            }
                        }
                        None => break &s[s.len()..],
                    }
                }
            }};
        }

        macro_rules! strip {
            ($s: expr, $pat: expr) => {{
                let pat = $pat;
                let res = $s.strip_prefix(pat);
                if res.is_some() {
                    offset += pat.len();
                }
                res
            }};
        }

        let s =
            strip!(trim!(s), "//").ok_or_else(|| format_err!("directives must start with //"))?;

        let (s, check) = {
            let s = trim!(s);
            if let Some(s) = strip!(s, "check") {
                (s, true)
            } else if let Some(s) = strip!(s, "not") {
                (s, false)
            } else {
                bail!("expects 'check' or 'not' after //")
            }
        };
        let s =
            strip!(trim!(s), ":").ok_or_else(|| format_err!("expects ':' after directive name"))?;

        let directives: Vec<_> = tokenize_patterns(s)?
            .into_iter()
            .filter_map(|Sp { inner, start, end }| match inner {
                Token::String(s) | Token::QuotedString(s) => {
                    let d = if check {
                        Directive::Check(s)
                    } else {
                        Directive::Not(s)
                    };
                    Some(Sp::new(d, start + offset, end + offset))
                }
                _ => None,
            })
            .collect();
        if directives.is_empty() {
            bail!("no directives found in line");
        }
        Ok(directives)
    }
}

impl AsRef<Directive> for Directive {
    fn as_ref(&self) -> &Directive {
        self
    }
}