luau_parser/impl/
name.rs

1//! All `impl` blocks for [`Name`].
2
3use luau_lexer::prelude::{Lexer, ParseError, Symbol, Token, TokenType};
4use smol_str::SmolStr;
5
6use crate::types::{Name, Parse, Pointer, TryParse, TypeValue};
7
8impl Name {
9    /// An error name that should be used when a name failed to parse but must exist.
10    pub const ERROR: Self = Self {
11        name: Token::empty(TokenType::Identifier(SmolStr::new_inline("*error*"))),
12        colon: None,
13        r#type: None,
14    };
15}
16
17impl Parse for Name {
18    fn parse(name: Token, lexer: &mut Lexer, errors: &mut Vec<ParseError>) -> Option<Self> {
19        if !matches!(
20            name.token_type,
21            TokenType::Identifier(_) | TokenType::PartialKeyword(_)
22        ) {
23            return None;
24        }
25
26        maybe_next_token!(lexer, colon, TokenType::Symbol(Symbol::Colon));
27
28        let r#type = if colon.is_some() {
29            Pointer::<TypeValue>::try_parse(lexer, errors)
30        } else {
31            None
32        };
33
34        Some(Self {
35            name,
36            colon,
37            r#type,
38        })
39    }
40}
41impl TryParse for Name {}