1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2026 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::{Expr, ExprKind, SourceInfo, Variable};
19
use crate::reader::Reader;
20

            
21
use super::error::{ParseError, ParseErrorKind};
22
use super::{ParseResult, function};
23

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

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