luau-syntax 0.732.0

Luau lexer, parser, AST, CST, and source utilities
Documentation
use super::*;

impl<'ast> Parser<'_, 'ast, '_, '_> {
    pub(in crate::parser) fn error_statement(
        &mut self,
        location: Location,
        message_index: usize,
    ) -> Statement<'ast> {
        self.arena.alloc_statement_node(StatementError::new(
            location,
            false,
            self.empty_expression_slice(),
            self.empty_statement_slice(),
            message_index,
        ))
    }

    pub(in crate::parser) fn error_type(&mut self, message_index: usize) -> TypeKind<'ast> {
        let types = self.empty_type_slice();
        TypeKind::Error {
            types,
            missing: false,
            message_index,
        }
    }

    pub(in crate::parser) fn report_missing_type_error(
        &mut self,
        parse_error: ParseError,
        ast_location: Location,
    ) -> Type<'ast> {
        let message_index = self.report_parse_error(parse_error);
        let types = self.empty_type_slice();
        self.alloc_type(
            ast_location,
            TypeKind::Error {
                types,
                missing: true,
                message_index,
            },
        )
    }

    pub(in crate::parser) fn intern_name(&mut self, name: &str) -> AstName<'ast> {
        self.lexer.intern_name_bytes(name.as_bytes())
    }

    pub(in crate::parser) fn intern_name_bytes(&mut self, name: &[u8]) -> AstName<'ast> {
        self.lexer.intern_name_bytes(name)
    }

    pub(in crate::parser) fn string_property_name(&mut self, name: &[u8]) -> AstName<'ast> {
        AstName::from_bytes(self.arena.alloc_bytes(name))
    }

    pub(in crate::parser) fn name_message(
        &self,
        prefix: &[u8],
        name: AstName<'_>,
        suffix: &[u8],
    ) -> ParseMessage {
        let name = name.bytes();
        let mut message = Vec::with_capacity(prefix.len() + name.len() + suffix.len());
        message.extend_from_slice(prefix);
        message.extend_from_slice(name);
        message.extend_from_slice(suffix);
        ParseMessage::from(message)
    }

    pub(in crate::parser) fn alloc_expression(
        &mut self,
        expression: ExpressionInit<'ast>,
    ) -> Expression<'ast> {
        self.arena
            .alloc_expression_kind(expression.location, expression.kind)
    }

    pub(in crate::parser) fn alloc_type(
        &mut self,
        location: Location,
        annotation: TypeKind<'ast>,
    ) -> Type<'ast> {
        self.arena.alloc_type(location, annotation)
    }

    pub(in crate::parser) fn alloc_type_pack(
        &mut self,
        location: Location,
        annotation: TypePackKind<'ast>,
    ) -> TypePack<'ast> {
        self.arena.alloc_type_pack(location, annotation)
    }

    pub(in crate::parser) fn empty_expression_slice(&self) -> &'ast [Expression<'ast>] {
        self.arena.alloc_slice_copy(&[])
    }

    pub(in crate::parser) fn empty_statement_slice(&self) -> &'ast [Statement<'ast>] {
        self.arena.alloc_slice_copy(&[])
    }

    pub(in crate::parser) fn empty_type_slice(&self) -> &'ast [Type<'ast>] {
        self.arena.alloc_slice_copy(&[])
    }

    pub(in crate::parser) fn alloc_type_slice(
        &mut self,
        annotations: Vec<Type<'ast>>,
    ) -> &'ast [Type<'ast>] {
        self.arena.alloc_slice_copy(&annotations)
    }

    pub(in crate::parser) fn explicit_type_pack(
        &mut self,
        types: Vec<Type<'ast>>,
        tail: Option<TypePack<'ast>>,
    ) -> TypePackKind<'ast> {
        TypePackKind::Explicit {
            type_list: TypeList {
                types: self.alloc_type_slice(types),
                tail_type: tail,
            },
        }
    }

    pub(in crate::parser) fn ast_string(&self, bytes: &[u8]) -> AstString<'ast> {
        self.arena.alloc_ast_string(bytes)
    }
}