summaryrefslogtreecommitdiffstats
path: root/third_party/rust/naga/src/front/glsl/preprocess_tests.rs
blob: 253a99935ec0b7d76cd39d6c0e8bfa2e63362a25 (plain)
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
use super::preprocess::{Error, LinePreProcessor};
use std::{iter::Enumerate, str::Lines};

#[derive(Clone, Debug)]
pub struct PreProcessor<'a> {
    lines: Enumerate<Lines<'a>>,
    input: String,
    line: usize,
    offset: usize,
    line_pp: LinePreProcessor,
}

impl<'a> PreProcessor<'a> {
    pub fn new(input: &'a str) -> Self {
        let mut lexer = PreProcessor {
            lines: input.lines().enumerate(),
            input: "".to_string(),
            line: 0,
            offset: 0,
            line_pp: LinePreProcessor::new(),
        };
        lexer.next_line();
        lexer
    }

    fn next_line(&mut self) -> bool {
        if let Some((line, input)) = self.lines.next() {
            let mut input = String::from(input);

            while input.ends_with('\\') {
                if let Some((_, next)) = self.lines.next() {
                    input.pop();
                    input.push_str(next);
                } else {
                    break;
                }
            }

            self.input = input;
            self.line = line;
            self.offset = 0;
            true
        } else {
            false
        }
    }

    pub fn process(&mut self) -> Result<String, Error> {
        let mut res = String::new();
        loop {
            let line = &self.line_pp.process_line(&self.input)?;
            if let Some(line) = line {
                res.push_str(line);
            }
            if !self.next_line() {
                break;
            }
            if line.is_some() {
                res.push_str("\n");
            }
        }
        Ok(res)
    }
}

#[test]
fn preprocess() {
    // line continuation
    let mut pp = PreProcessor::new(
        "void main my_\
        func",
    );
    assert_eq!(pp.process().unwrap(), "void main my_func");

    // preserve #version
    let mut pp = PreProcessor::new(
        "#version 450 core\n\
        void main()",
    );
    assert_eq!(pp.process().unwrap(), "#version 450 core\nvoid main()");

    // simple define
    let mut pp = PreProcessor::new(
        "#define FOO 42 \n\
        fun=FOO",
    );
    assert_eq!(pp.process().unwrap(), "\nfun=42");

    // ifdef with else
    let mut pp = PreProcessor::new(
        "#define FOO\n\
        #ifdef FOO\n\
            foo=42\n\
        #endif\n\
        some=17\n\
        #ifdef BAR\n\
            bar=88\n\
        #else\n\
            mm=49\n\
        #endif\n\
        done=1",
    );
    assert_eq!(
        pp.process().unwrap(),
        "\n\
        foo=42\n\
        \n\
        some=17\n\
        \n\
        mm=49\n\
        \n\
        done=1"
    );

    // nested ifdef/ifndef
    let mut pp = PreProcessor::new(
        "#define FOO\n\
        #define BOO\n\
        #ifdef FOO\n\
            foo=42\n\
            #ifdef BOO\n\
                boo=44\n\
            #endif\n\
                ifd=0\n\
            #ifndef XYZ\n\
                nxyz=8\n\
            #endif\n\
        #endif\n\
        some=17\n\
        #ifdef BAR\n\
            bar=88\n\
        #else\n\
            mm=49\n\
        #endif\n\
        done=1",
    );
    assert_eq!(
        pp.process().unwrap(),
        "\n\
        foo=42\n\
        \n\
        boo=44\n\
        \n\
        ifd=0\n\
        \n\
        nxyz=8\n\
        \n\
        some=17\n\
        \n\
        mm=49\n\
        \n\
        done=1"
    );

    // undef
    let mut pp = PreProcessor::new(
        "#define FOO\n\
        #ifdef FOO\n\
            foo=42\n\
        #endif\n\
        some=17\n\
        #undef FOO\n\
        #ifdef FOO\n\
            foo=88\n\
        #else\n\
            nofoo=66\n\
        #endif\n\
        done=1",
    );
    assert_eq!(
        pp.process().unwrap(),
        "\n\
        foo=42\n\
        \n\
        some=17\n\
        \n\
        nofoo=66\n\
        \n\
        done=1"
    );

    // single-line comment
    let mut pp = PreProcessor::new(
        "#define FOO 42//1234\n\
        fun=FOO",
    );
    assert_eq!(pp.process().unwrap(), "\nfun=42");

    // multi-line comments
    let mut pp = PreProcessor::new(
        "#define FOO 52/*/1234\n\
        #define FOO 88\n\
        end of comment*/ /* one more comment */ #define FOO 56\n\
        fun=FOO",
    );
    assert_eq!(pp.process().unwrap(), "\nfun=56");

    // unmatched endif
    let mut pp = PreProcessor::new(
        "#ifdef FOO\n\
        foo=42\n\
        #endif\n\
        #endif",
    );
    assert_eq!(pp.process(), Err(Error::UnmatchedEndif));

    // unmatched else
    let mut pp = PreProcessor::new(
        "#ifdef FOO\n\
        foo=42\n\
        #else\n\
        bar=88\n\
        #else\n\
        bad=true\n\
        #endif",
    );
    assert_eq!(pp.process(), Err(Error::UnmatchedElse));
}