emmylua_parser/parser/
parser_config.rs1use 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 _ => *self
49 .special_like
50 .get(name)
51 .unwrap_or(&SpecialFunction::None),
52 }
53 }
54}
55
56impl<'cache> Default for ParserConfig<'cache> {
57 fn default() -> Self {
58 Self {
59 level: LuaLanguageLevel::Lua54,
60 lexer_config: LexerConfig {
61 language_level: LuaLanguageLevel::Lua54,
62 },
63 node_cache: None,
64 special_like: HashMap::new(),
65 }
66 }
67}
68
69#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub enum SpecialFunction {
71 None,
72 Require,
73 Error,
74 Assert,
75 Type,
76}