oak_json/language/
mod.rs

1use oak_core::language::{Language, LanguageCategory};
2
3/// JSON 语言实现
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct JsonLanguage {
6    /// 是否允许尾随逗号
7    pub trailing_comma: bool,
8    /// 是否允许裸键(不带引号的键)
9    pub bare_keys: bool,
10    /// 是否允许单引号字符串
11    pub single_quotes: bool,
12    /// 是否允许注释
13    pub comments: bool,
14    /// 是否允许十六进制数字
15    pub hex_numbers: bool,
16    /// 是否允许无穷大和 NaN
17    pub infinity_and_nan: bool,
18}
19
20impl JsonLanguage {
21    /// 创建标准 JSON 语言实例
22    pub fn standard() -> Self {
23        Self::default()
24    }
25
26    /// 创建 JSON5 语言实例
27    pub fn json5() -> Self {
28        Self { trailing_comma: true, bare_keys: true, single_quotes: true, comments: true, hex_numbers: true, infinity_and_nan: true }
29    }
30
31    /// 创建宽松 JSON 语言实例
32    pub fn relaxed() -> Self {
33        Self { trailing_comma: true, bare_keys: true, single_quotes: true, comments: true, hex_numbers: true, infinity_and_nan: true }
34    }
35}
36
37impl Default for JsonLanguage {
38    fn default() -> Self {
39        Self { trailing_comma: false, bare_keys: false, single_quotes: false, comments: false, hex_numbers: false, infinity_and_nan: false }
40    }
41}
42
43impl Language for JsonLanguage {
44    const NAME: &'static str = "json";
45    const CATEGORY: LanguageCategory = LanguageCategory::Config;
46
47    type TokenType = crate::kind::JsonSyntaxKind;
48    type ElementType = crate::kind::JsonSyntaxKind;
49    type TypedRoot = crate::ast::JsonRoot;
50}