reqlang_expr/
ast.rs

1//! Abstract syntax tree types
2
3use std::ops::Range;
4
5#[derive(Debug, PartialEq)]
6pub enum Expr {
7    Bool(Box<ExprBool>),
8    Identifier(Box<ExprIdentifier>),
9    Call(Box<ExprCall>),
10    String(Box<ExprString>),
11    Error,
12}
13
14impl Expr {
15    pub fn identifier(identifier: &str) -> Self {
16        Self::Identifier(Box::new(ExprIdentifier::new(identifier)))
17    }
18
19    pub fn string(string: &str) -> Self {
20        Self::String(ExprString::new(string).into())
21    }
22
23    pub fn call(callee: (Expr, Range<usize>), args: Vec<(Expr, Range<usize>)>) -> Self {
24        Self::Call(Box::new(ExprCall { callee, args }))
25    }
26
27    pub fn bool(value: bool) -> Self {
28        Self::Bool(Box::new(ExprBool::new(value)))
29    }
30}
31
32#[derive(Debug, PartialEq)]
33pub struct ExprIdentifier(pub String);
34
35impl ExprIdentifier {
36    pub fn new(identifier: &str) -> Self {
37        Self(identifier.to_string())
38    }
39}
40
41#[derive(Debug, PartialEq)]
42pub struct ExprString(pub String);
43
44impl ExprString {
45    pub fn new(string: &str) -> Self {
46        Self(string.to_string())
47    }
48}
49
50#[derive(Debug, PartialEq)]
51pub struct ExprCall {
52    pub callee: (Expr, Range<usize>),
53    pub args: Vec<(Expr, Range<usize>)>,
54}
55
56#[derive(Debug, PartialEq)]
57pub struct ExprBool(pub bool);
58
59impl ExprBool {
60    pub fn new(value: bool) -> Self {
61        Self(value)
62    }
63}