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::function;
21
use crate::parser::ParseResult;
22
use crate::reader::Reader;
23

            
24
/// Parse an expression
25
///
26
/// Currently, an expression can only be found inside a placeholder
27
2155
pub fn parse(reader: &mut Reader) -> ParseResult<Expr> {
28
2155
    let start = reader.cursor().pos;
29
2155
    let save_state = reader.cursor();
30
2155
    let kind = match function::parse(reader) {
31
20
        Ok(function) => ExprKind::Function(function),
32
2135
        Err(e) => {
33
2135
            if e.recoverable {
34
2135
                reader.seek(save_state);
35
2135
                let variable = variable_name(reader)?;
36
2130
                ExprKind::Variable(variable)
37
            } else {
38
                return Err(e);
39
            }
40
        }
41
    };
42
2150
    let end = reader.cursor().pos;
43
2150
    let source_info = SourceInfo::new(start, end);
44
2150
    Ok(Expr { source_info, kind })
45
}
46

            
47
2135
fn variable_name(reader: &mut Reader) -> ParseResult<Variable> {
48
2135
    let start = reader.cursor();
49
16377
    let name = reader.read_while(|c| c.is_alphanumeric() || c == '_' || c == '-');
50
2135
    if name.is_empty() {
51
5
        return Err(ParseError::new(
52
5
            start.pos,
53
5
            false,
54
5
            ParseErrorKind::TemplateVariable,
55
5
        ));
56
    }
57
2130
    Ok(Variable {
58
2130
        name,
59
2130
        source_info: SourceInfo::new(start.pos, reader.cursor().pos),
60
2130
    })
61
}
62

            
63
#[cfg(test)]
64
mod tests {
65
    use super::*;
66
    use crate::reader::Pos;
67

            
68
    #[test]
69
    fn test_variable() {
70
        let mut reader = Reader::new("name");
71
        assert_eq!(
72
            variable_name(&mut reader).unwrap(),
73
            Variable {
74
                name: String::from("name"),
75
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 5)),
76
            }
77
        );
78

            
79
        let mut reader = Reader::new("my-id");
80
        assert_eq!(
81
            variable_name(&mut reader).unwrap(),
82
            Variable {
83
                name: String::from("my-id"),
84
                source_info: SourceInfo::new(Pos::new(1, 1), Pos::new(1, 6)),
85
            }
86
        );
87
    }
88
}