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
use specmc_base::{
    ensure_tokens,
    parse::{Identifier, Parse, ParseError},
};

use crate::base::{BaseType, FieldList};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Type {
    BaseType(BaseType),
    CustomType(Identifier),
}
impl Parse for Type {
    fn parse(tokens: &mut Vec<String>) -> Result<Self, ParseError> {
        if let Ok(base_type) = BaseType::parse(tokens) {
            Ok(Type::BaseType(base_type))
        } else {
            Ok(Type::CustomType(Identifier::parse(tokens)?))
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct CustomType {
    pub name: Identifier,
    pub fields: FieldList,
}
impl Parse for CustomType {
    fn parse(tokens: &mut Vec<String>) -> Result<Self, ParseError> {
        ensure_tokens!(tokens, "type");
        let name: Identifier = Identifier::parse(tokens)?;
        ensure_tokens!(tokens, "{");
        let fields: FieldList = FieldList::parse(tokens)?;
        ensure_tokens!(tokens, "}");

        Ok(CustomType { name, fields })
    }
}

#[cfg(test)]
mod tests {
    use specmc_base::tokenize;

    use crate::{
        base::{Field, IntegerType},
        test_parse,
    };

    use super::*;

    #[test]
    fn test_type() {
        let mut tokens: Vec<String> = tokenize!("bool i32 TestType");

        test_parse!(tokens, Type, Ok(Type::BaseType(BaseType::Bool)));
        test_parse!(
            tokens,
            Type,
            Ok(Type::BaseType(BaseType::Integer(IntegerType::I32)))
        );
        test_parse!(
            tokens,
            Type,
            Ok(Type::CustomType(Identifier("TestType".to_string())))
        );

        assert!(tokens.is_empty());
        test_parse!(tokens, Type, Err(ParseError::EndOfFile));
    }

    #[test]
    fn test_custom_type() {
        let mut tokens: Vec<String> = tokenize!(
            "
            type TestType {
                i32 a
                bool b
                if (b) {
                    i32 c
                }
            }
            "
        );

        test_parse!(
            tokens,
            CustomType,
            Ok(CustomType {
                name: Identifier("TestType".to_string()),
                fields: FieldList(vec![
                    Field {
                        ty: Type::BaseType(BaseType::Integer(IntegerType::I32)),
                        name: Identifier("a".to_string()),
                        value: None,
                        condition: None
                    },
                    Field {
                        ty: Type::BaseType(BaseType::Bool),
                        name: Identifier("b".to_string()),
                        value: None,
                        condition: None
                    },
                    Field {
                        ty: Type::BaseType(BaseType::Integer(IntegerType::I32)),
                        name: Identifier("c".to_string()),
                        value: None,
                        condition: Some("( b )".to_string())
                    }
                ])
            })
        );

        assert!(tokens.is_empty());
        test_parse!(tokens, CustomType, Err(ParseError::EndOfFile));
    }
}