1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2024 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use crate::ast::*;
19
use crate::parser::error::*;
20
use crate::parser::primitives::try_literal;
21
use crate::parser::{string, ParseResult};
22
use crate::reader::Reader;
23

            
24
use super::placeholder;
25

            
26
/// Parse a filename.
27
///
28
/// A few characters need to be escaped such as space
29
/// for example: file\ with\ space.txt
30
/// This is very similar to the behaviour in a standard shell.
31
///
32
920
pub fn parse(reader: &mut Reader) -> ParseResult<Template> {
33
920
    let start = reader.cursor();
34
920

            
35
920
    let mut elements = vec![];
36
    loop {
37
1840
        let start = reader.cursor();
38
1840
        match placeholder::parse(reader) {
39
70
            Ok(placeholder) => {
40
70
                let element = TemplateElement::Placeholder(placeholder);
41
70
                elements.push(element);
42
            }
43
1770
            Err(e) => {
44
1770
                if e.recoverable {
45
1770
                    let value = filename_content(reader)?;
46
1770
                    if value.is_empty() {
47
920
                        break;
48
                    }
49
850
                    let encoded = reader.read_from(start.index);
50
850
                    let element = TemplateElement::String { value, encoded };
51
850
                    elements.push(element);
52
                } else {
53
                    return Err(e);
54
                }
55
            }
56
        }
57
    }
58
920
    if elements.is_empty() {
59
10
        let kind = ParseErrorKind::Filename;
60
10
        return Err(ParseError::new(start.pos, false, kind));
61
    }
62
910
    if let Some(TemplateElement::String { encoded, .. }) = elements.first() {
63
845
        if encoded.starts_with('[') {
64
            let kind = ParseErrorKind::Expecting {
65
                value: "filename".to_string(),
66
            };
67
            return Err(ParseError::new(start.pos, false, kind));
68
        }
69
    }
70

            
71
910
    let end = reader.cursor();
72
910
    Ok(Template {
73
910
        delimiter: None,
74
910
        elements,
75
910
        source_info: SourceInfo {
76
910
            start: start.pos,
77
910
            end: end.pos,
78
910
        },
79
910
    })
80
}
81

            
82
1770
fn filename_content(reader: &mut Reader) -> ParseResult<String> {
83
1770
    let mut s = String::new();
84
    loop {
85
2660
        match filename_escaped_char(reader) {
86
20
            Ok(c) => {
87
20
                s.push(c);
88
            }
89
2640
            Err(e) => {
90
2640
                if e.recoverable {
91
2640
                    let s2 = filename_text(reader);
92
2640
                    if s2.is_empty() {
93
1770
                        break;
94
870
                    } else {
95
870
                        s.push_str(&s2);
96
                    }
97
                } else {
98
                    return Err(e);
99
                }
100
            }
101
        }
102
    }
103
1770
    Ok(s)
104
}
105

            
106
2640
fn filename_text(reader: &mut Reader) -> String {
107
2640
    let mut s = String::new();
108
    loop {
109
12045
        let save = reader.cursor();
110
12045
        match reader.read() {
111
5
            None => break,
112
12040
            Some(c) => {
113
12040
                if ['#', ';', '{', '}', ' ', '\n', '\\'].contains(&c) {
114
2635
                    reader.seek(save);
115
2635
                    break;
116
9405
                } else {
117
9405
                    s.push(c);
118
                }
119
            }
120
        }
121
    }
122
2640
    s
123
}
124

            
125
2660
fn filename_escaped_char(reader: &mut Reader) -> ParseResult<char> {
126
2660
    try_literal("\\", reader)?;
127
20
    let start = reader.cursor();
128
20
    match reader.read() {
129
        Some('\\') => Ok('\\'),
130
        Some('b') => Ok('\x08'),
131
        Some('f') => Ok('\x0c'),
132
        Some('n') => Ok('\n'),
133
        Some('r') => Ok('\r'),
134
        Some('t') => Ok('\t'),
135
        Some('#') => Ok('#'),
136
        Some(';') => Ok(';'),
137
20
        Some(' ') => Ok(' '),
138
        Some('{') => Ok('{'),
139
        Some('}') => Ok('}'),
140
        Some('u') => string::unicode(reader),
141
        _ => Err(ParseError::new(
142
            start.pos,
143
            false,
144
            ParseErrorKind::EscapeChar,
145
        )),
146
    }
147
}
148

            
149
#[cfg(test)]
150
mod tests {
151
    use super::*;
152
    use crate::reader::Pos;
153

            
154
    #[test]
155
    fn test_filename() {
156
        let mut reader = Reader::new("data/data.bin");
157
        assert_eq!(
158
            parse(&mut reader).unwrap(),
159
            Template {
160
                delimiter: None,
161
                elements: vec![TemplateElement::String {
162
                    value: "data/data.bin".to_string(),
163
                    encoded: "data/data.bin".to_string()
164
                }],
165
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 14)),
166
            }
167
        );
168
        assert_eq!(reader.cursor().index, 13);
169

            
170
        let mut reader = Reader::new("data.bin");
171
        assert_eq!(
172
            parse(&mut reader).unwrap(),
173
            Template {
174
                //value: String::from("data.bin"),
175
                delimiter: None,
176
                elements: vec![TemplateElement::String {
177
                    value: "data.bin".to_string(),
178
                    encoded: "data.bin".to_string()
179
                }],
180
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 9)),
181
            }
182
        );
183
        assert_eq!(reader.cursor().index, 8);
184
    }
185

            
186
    #[test]
187
    fn test_include_space() {
188
        let mut reader = Reader::new("file\\ with\\ spaces");
189
        assert_eq!(
190
            parse(&mut reader).unwrap(),
191
            Template {
192
                delimiter: None,
193
                elements: vec![TemplateElement::String {
194
                    value: "file with spaces".to_string(),
195
                    encoded: "file\\ with\\ spaces".to_string()
196
                }],
197
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 19)),
198
            }
199
        );
200
        assert_eq!(reader.cursor().index, 18);
201
    }
202

            
203
    #[test]
204
    fn test_escaped_chars() {
205
        let mut reader = Reader::new("filename\\{"); // to the possible escaped chars
206
        assert_eq!(
207
            parse(&mut reader).unwrap(),
208
            Template {
209
                delimiter: None,
210
                elements: vec![TemplateElement::String {
211
                    value: "filename{".to_string(),
212
                    encoded: "filename\\{".to_string()
213
                }],
214
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 11)),
215
            }
216
        );
217
        assert_eq!(reader.cursor().index, 10);
218
    }
219

            
220
    #[test]
221
    fn test_filename_error() {
222
        let mut reader = Reader::new("{");
223
        let error = parse(&mut reader).err().unwrap();
224
        assert_eq!(error.kind, ParseErrorKind::Filename);
225
        assert_eq!(error.pos, Pos { line: 1, column: 1 });
226

            
227
        let mut reader = Reader::new("\\:");
228
        let error = parse(&mut reader).err().unwrap();
229
        assert_eq!(error.kind, ParseErrorKind::EscapeChar);
230
        assert_eq!(error.pos, Pos { line: 1, column: 2 });
231
    }
232

            
233
    #[test]
234
    fn test_filename_with_variables() {
235
        let mut reader = Reader::new("foo_{{bar}}");
236
        assert_eq!(
237
            parse(&mut reader).unwrap(),
238
            Template {
239
                delimiter: None,
240
                elements: vec![
241
                    TemplateElement::String {
242
                        value: "foo_".to_string(),
243
                        encoded: "foo_".to_string(),
244
                    },
245
                    TemplateElement::Placeholder(Placeholder {
246
                        space0: Whitespace {
247
                            value: String::new(),
248
                            source_info: SourceInfo::new(Pos::new(1, 7), Pos::new(1, 7)),
249
                        },
250
                        expr: Expr {
251
                            kind: ExprKind::Variable(Variable {
252
                                name: "bar".to_string(),
253
                                source_info: SourceInfo::new(Pos::new(1, 7), Pos::new(1, 10)),
254
                            }),
255
                            source_info: SourceInfo::new(Pos::new(1, 7), Pos::new(1, 10)),
256
                        },
257
                        space1: Whitespace {
258
                            value: String::new(),
259
                            source_info: SourceInfo::new(Pos::new(1, 10), Pos::new(1, 10)),
260
                        },
261
                    })
262
                ],
263
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 12)),
264
            }
265
        );
266

            
267
        let mut reader = Reader::new("foo_{{bar}}_baz");
268
        assert_eq!(
269
            parse(&mut reader).unwrap(),
270
            Template {
271
                delimiter: None,
272
                elements: vec![
273
                    TemplateElement::String {
274
                        value: "foo_".to_string(),
275
                        encoded: "foo_".to_string(),
276
                    },
277
                    TemplateElement::Placeholder(Placeholder {
278
                        space0: Whitespace {
279
                            value: String::new(),
280
                            source_info: SourceInfo::new(Pos::new(1, 7), Pos::new(1, 7)),
281
                        },
282
                        expr: Expr {
283
                            kind: ExprKind::Variable(Variable {
284
                                name: "bar".to_string(),
285
                                source_info: SourceInfo::new(Pos::new(1, 7), Pos::new(1, 10)),
286
                            }),
287
                            source_info: SourceInfo::new(Pos::new(1, 7), Pos::new(1, 10)),
288
                        },
289
                        space1: Whitespace {
290
                            value: String::new(),
291
                            source_info: SourceInfo::new(Pos::new(1, 10), Pos::new(1, 10)),
292
                        },
293
                    }),
294
                    TemplateElement::String {
295
                        value: "_baz".to_string(),
296
                        encoded: "_baz".to_string(),
297
                    },
298
                ],
299
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 16)),
300
            }
301
        );
302
    }
303
}