luau-syntax 0.732.0

Luau lexer, parser, AST, CST, and source utilities
Documentation
use super::{BlockContext, FunctionContext, MatchRecoveryStop};
use crate::ast::{Local, StatementClass};
use crate::ast_names::{AstName, AstNameDenseHasher};
use crate::cst::{CstNode, CstNodeMap};
use crate::parser::ParseError;
use luau_common::DenseHashMap;

#[derive(Debug, Default)]
pub(super) struct DiagnosticState {
    pub(super) errors: Vec<ParseError>,
    pub(super) error_limit_reached: bool,
}

#[derive(Debug)]
pub(super) struct ContextState {
    pub(super) recursion_counter: usize,
    pub(super) blocks: Vec<BlockContext>,
    pub(super) end_mismatch_suspect: Option<BlockContext>,
    pub(super) root_function: FunctionContext,
    pub(super) functions: Vec<FunctionContext>,
}

impl Default for ContextState {
    fn default() -> Self {
        Self {
            recursion_counter: 0,
            blocks: Vec::new(),
            end_mismatch_suspect: None,
            root_function: FunctionContext {
                loop_depth: 0,
                vararg: true,
            },
            functions: Vec::with_capacity(8),
        }
    }
}

#[derive(Debug)]
pub(super) struct LocalState<'ast> {
    pub(super) classes_within_module:
        DenseHashMap<AstName<'ast>, Option<&'ast StatementClass<'ast>>, AstNameDenseHasher>,
    pub(super) map: DenseHashMap<AstName<'ast>, Option<&'ast Local<'ast>>, AstNameDenseHasher>,
    pub(super) stack: Vec<&'ast Local<'ast>>,
    pub(super) scope_offsets: Vec<usize>,
    pub(super) type_function_local_depth: Option<usize>,
}

impl<'ast> LocalState<'ast> {
    pub(super) fn new(
        map: DenseHashMap<AstName<'ast>, Option<&'ast Local<'ast>>, AstNameDenseHasher>,
        stack: Vec<&'ast Local<'ast>>,
    ) -> Self {
        Self {
            classes_within_module: DenseHashMap::new(AstName::empty_key()),
            map,
            scope_offsets: vec![stack.len()],
            stack,
            type_function_local_depth: None,
        }
    }
}

#[derive(Debug)]
pub(super) struct RecoveryState {
    pub(super) match_recovery_stops: [usize; MatchRecoveryStop::COUNT],
}

impl Default for RecoveryState {
    fn default() -> Self {
        Self {
            match_recovery_stops: [0; MatchRecoveryStop::COUNT],
        }
    }
}

#[derive(Debug, Default)]
pub(super) struct CstState<'ast> {
    enabled: bool,
    nodes: CstNodeMap<'ast>,
}

impl<'ast> CstState<'ast> {
    pub(super) fn new(enabled: bool) -> Self {
        Self {
            enabled,
            nodes: CstNodeMap::default(),
        }
    }

    pub(super) fn enabled(&self) -> bool {
        self.enabled
    }

    pub(super) fn into_nodes(self) -> CstNodeMap<'ast> {
        self.nodes
    }

    pub(super) fn node(&self, cst: impl FnOnce() -> CstNode<'ast>) -> Option<CstNode<'ast>> {
        self.enabled.then(cst)
    }

    pub(super) fn get(&self, node: impl crate::cst::AstNodeKeySource) -> Option<&CstNode<'ast>> {
        if self.enabled {
            self.nodes.get(node)
        } else {
            None
        }
    }

    pub(super) fn insert(
        &mut self,
        node: impl crate::cst::AstNodeKeySource,
        cst: impl Into<Option<CstNode<'ast>>>,
    ) {
        if self.enabled
            && let Some(cst) = cst.into()
        {
            self.nodes.insert(node, cst);
        }
    }
}