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
use crate::lexer::token::TokenKind;
use crate::parser::ast::GroupUseStatement;
use crate::parser::ast::Statement;
use crate::parser::ast::Use;
use crate::parser::ast::UseKind;
use crate::parser::ast::UseStatement;
use crate::parser::error;
use crate::parser::error::ParseResult;
use crate::parser::internal::identifiers;
use crate::parser::internal::utils;
use crate::parser::state::State;

pub fn use_statement(state: &mut State) -> ParseResult<Statement> {
    state.stream.next();

    let kind = match state.stream.current().kind {
        TokenKind::Function => {
            state.stream.next();
            UseKind::Function
        }
        TokenKind::Const => {
            state.stream.next();
            UseKind::Const
        }
        _ => UseKind::Normal,
    };

    if state.stream.peek().kind == TokenKind::LeftBrace {
        let prefix = identifiers::full_name(state)?;
        state.stream.next();
        let mut uses = Vec::new();
        while state.stream.current().kind != TokenKind::RightBrace {
            let use_kind = match state.stream.current().kind {
                TokenKind::Function => {
                    if kind != UseKind::Normal {
                        return Err(error::unexpected_token(
                            vec!["an identifier".to_string()],
                            state.stream.current(),
                        ));
                    }

                    state.stream.next();
                    Some(UseKind::Function)
                }
                TokenKind::Const => {
                    if kind != UseKind::Normal {
                        return Err(error::unexpected_token(
                            vec!["an identifier".to_string()],
                            state.stream.current(),
                        ));
                    }

                    state.stream.next();
                    Some(UseKind::Const)
                }
                _ => None,
            };

            let name = identifiers::full_type_name(state)?;
            let mut alias = None;
            if state.stream.current().kind == TokenKind::As {
                state.stream.next();
                alias = Some(identifiers::type_identifier(state)?);
            }

            uses.push(Use {
                name,
                kind: use_kind,
                alias,
            });

            if state.stream.current().kind == TokenKind::Comma {
                state.stream.next();
                continue;
            }
        }

        utils::skip_right_brace(state)?;
        utils::skip_semicolon(state)?;

        Ok(Statement::GroupUse(GroupUseStatement {
            prefix,
            kind,
            uses,
        }))
    } else {
        let mut uses = Vec::new();
        while !state.stream.is_eof() {
            let name = identifiers::full_type_name(state)?;
            let mut alias = None;
            if state.stream.current().kind == TokenKind::As {
                state.stream.next();
                alias = Some(identifiers::type_identifier(state)?);
            }

            uses.push(Use {
                name,
                kind: None,
                alias,
            });

            if state.stream.current().kind == TokenKind::Comma {
                state.stream.next();
                continue;
            }

            utils::skip_semicolon(state)?;
            break;
        }

        Ok(Statement::Use(UseStatement { uses, kind }))
    }
}