emmylua_parser/parser/
parser_config.rs

1use std::collections::HashMap;
2
3use rowan::NodeCache;
4
5use crate::{kind::LuaLanguageLevel, lexer::LexerConfig};
6
7pub struct ParserConfig<'cache> {
8    pub level: LuaLanguageLevel,
9    lexer_config: LexerConfig,
10    node_cache: Option<&'cache mut NodeCache>,
11    special_like: HashMap<String, SpecialFunction>,
12}
13
14impl<'cache> ParserConfig<'cache> {
15    pub fn new(
16        level: LuaLanguageLevel,
17        node_cache: Option<&'cache mut NodeCache>,
18        special_like: HashMap<String, SpecialFunction>,
19    ) -> Self {
20        Self {
21            level,
22            lexer_config: LexerConfig {
23                language_level: level,
24            },
25            node_cache,
26            special_like,
27        }
28    }
29
30    pub fn lexer_config(&self) -> LexerConfig {
31        self.lexer_config
32    }
33
34    pub fn support_local_attrib(&self) -> bool {
35        self.level == LuaLanguageLevel::Lua54
36    }
37
38    pub fn node_cache(&mut self) -> Option<&mut NodeCache> {
39        self.node_cache.as_deref_mut()
40    }
41
42    pub fn get_special_function(&self, name: &str) -> SpecialFunction {
43        match name {
44            "require" => SpecialFunction::Require,
45            "error" => SpecialFunction::Error,
46            "assert" => SpecialFunction::Assert,
47            "type" => SpecialFunction::Type,
48            "setmetatable" => SpecialFunction::Setmatable,
49            _ => *self
50                .special_like
51                .get(name)
52                .unwrap_or(&SpecialFunction::None),
53        }
54    }
55}
56
57impl Default for ParserConfig<'_> {
58    fn default() -> Self {
59        Self {
60            level: LuaLanguageLevel::Lua54,
61            lexer_config: LexerConfig {
62                language_level: LuaLanguageLevel::Lua54,
63            },
64            node_cache: None,
65            special_like: HashMap::new(),
66        }
67    }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub enum SpecialFunction {
72    None,
73    Require,
74    Error,
75    Assert,
76    Type,
77    Setmatable,
78}