luau_parser/impl/
bracketed.rs

1//! All `impl` blocks for [`Bracketed`].
2
3use luau_lexer::prelude::{Error, Lexer, Symbol, Token, TokenType};
4use std::ops::{Deref, DerefMut};
5
6use crate::{
7    types::{Bracketed, Parse, ParseWithArgs, Print},
8    utils::get_token_type_display_extended,
9};
10
11impl<T: Default> Bracketed<T> {
12    /// The actual parsing logic.
13    fn parse<F>(
14        parse: F,
15        opening_bracket: Token,
16        lexer: &mut Lexer,
17        errors: &mut Vec<Error>,
18        (error_message, stop_at): (&str, Symbol),
19    ) -> Option<Self>
20    where
21        F: FnOnce(Token, &mut Lexer, &mut Vec<Error>) -> Option<T>,
22    {
23        let token = lexer.next_token();
24        if token == TokenType::Symbol(stop_at) {
25            return Some(Self {
26                opening_bracket,
27                item: T::default(),
28                closing_bracket: token,
29            });
30        }
31
32        let Some(item) = parse(token, lexer, errors) else {
33            let state = lexer.save_state();
34            errors.push(Error::new(
35                state.lexer_position(),
36                error_message.to_string(),
37                Some(state.lexer_position()),
38            ));
39
40            return None;
41        };
42
43        next_token_recoverable_with_condition!(
44            lexer,
45            closing_bracket,
46            closing_bracket.token_type == TokenType::Symbol(stop_at),
47            TokenType::Symbol(stop_at),
48            errors,
49            format!(
50                "Expected {}",
51                get_token_type_display_extended(&TokenType::Symbol(stop_at))
52            )
53        );
54
55        Some(Self {
56            opening_bracket,
57            item,
58            closing_bracket,
59        })
60    }
61}
62
63impl<T: Parse + Default> ParseWithArgs<(&str, Symbol)> for Bracketed<T> {
64    #[inline]
65    fn parse_with(
66        opening_bracket: Token,
67        lexer: &mut Lexer,
68        errors: &mut Vec<Error>,
69        (error_message, stop_at): (&str, Symbol),
70    ) -> Option<Self> {
71        Self::parse(
72            |token: Token, lexer: &mut Lexer, errors: &mut Vec<Error>| {
73                T::parse(token, lexer, errors)
74            },
75            opening_bracket,
76            lexer,
77            errors,
78            (error_message, stop_at),
79        )
80    }
81}
82impl<A, T> ParseWithArgs<(&str, Symbol, A)> for Bracketed<T>
83where
84    T: ParseWithArgs<A> + Default,
85{
86    #[inline]
87    fn parse_with(
88        opening_bracket: Token,
89        lexer: &mut Lexer,
90        errors: &mut Vec<Error>,
91        (error_message, stop_at, args): (&str, Symbol, A),
92    ) -> Option<Self> {
93        Self::parse(
94            |token: Token, lexer: &mut Lexer, errors: &mut Vec<Error>| {
95                T::parse_with(token, lexer, errors, args)
96            },
97            opening_bracket,
98            lexer,
99            errors,
100            (error_message, stop_at),
101        )
102    }
103}
104
105impl<T> Deref for Bracketed<T> {
106    type Target = T;
107
108    fn deref(&self) -> &Self::Target {
109        &self.item
110    }
111}
112
113impl<T> DerefMut for Bracketed<T> {
114    fn deref_mut(&mut self) -> &mut Self::Target {
115        &mut self.item
116    }
117}
118
119impl<T: Print> Print for Bracketed<T> {
120    fn print_final_trivia(&self) -> String {
121        self.closing_bracket.print_final_trivia()
122    }
123
124    fn print_without_final_trivia(&self) -> String {
125        self.opening_bracket.print_without_final_trivia()
126            + &self.item.print_without_final_trivia()
127            + &self.closing_bracket.print_without_final_trivia()
128    }
129}