Skip to main content

oak_json/language/
mod.rs

1#![doc = include_str!("readme.md")]
2use oak_core::language::{Language, LanguageCategory};
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6/// JSON language implementation
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9pub struct JsonLanguage {
10    /// Whether to allow trailing commas in objects and arrays
11    pub trailing_comma: bool,
12    /// Whether to allow bare keys (unquoted keys) in objects
13    pub bare_keys: bool,
14    /// Whether to allow single-quoted strings
15    pub single_quotes: bool,
16    /// Whether to allow comments (both line and block)
17    pub comments: bool,
18    /// Whether to allow hexadecimal numbers (e.g., 0xDEADBEEF)
19    pub hex_numbers: bool,
20    /// Whether to allow Infinity, -Infinity, and NaN
21    pub infinity_and_nan: bool,
22}
23
24impl JsonLanguage {
25    /// Creates a new JSON language instance with default settings.
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    /// Creates a standard JSON language instance (no extensions).
31    pub fn standard() -> Self {
32        Self::default()
33    }
34
35    /// Creates a JSON5 language instance with all extensions enabled.
36    pub fn json5() -> Self {
37        Self { trailing_comma: true, bare_keys: true, single_quotes: true, comments: true, hex_numbers: true, infinity_and_nan: true }
38    }
39
40    /// Creates a relaxed JSON language instance with all extensions enabled.
41    pub fn relaxed() -> Self {
42        Self { trailing_comma: true, bare_keys: true, single_quotes: true, comments: true, hex_numbers: true, infinity_and_nan: true }
43    }
44}
45
46impl Default for JsonLanguage {
47    fn default() -> Self {
48        Self { trailing_comma: false, bare_keys: false, single_quotes: false, comments: false, hex_numbers: false, infinity_and_nan: false }
49    }
50}
51
52impl Language for JsonLanguage {
53    const NAME: &'static str = "json";
54    const CATEGORY: LanguageCategory = LanguageCategory::Config;
55
56    type TokenType = crate::lexer::token_type::JsonTokenType;
57    type ElementType = crate::parser::element_type::JsonElementType;
58    type TypedRoot = crate::ast::JsonRoot;
59}