Skip to main content

oak_ejs/language/
mod.rs

1#![doc = include_str!("readme.md")]
2
3use oak_core::language::{Language, LanguageCategory};
4
5/// JavaScript language implementation.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct JavaScriptLanguage {
9    /// Whether to allow JSX syntax.
10    pub jsx: bool,
11    /// Whether to allow TypeScript syntax.
12    pub typescript: bool,
13    /// Whether to allow experimental features.
14    pub experimental: bool,
15    /// Whether to enable strict mode.
16    pub strict_mode: bool,
17    /// ECMAScript version.
18    pub ecma_version: EcmaVersion,
19}
20
21/// ECMAScript version.
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24pub enum EcmaVersion {
25    /// ES3
26    ES3,
27    /// ES5
28    ES5,
29    /// ES2015 (ES6)
30    ES2015,
31    /// ES2016
32    ES2016,
33    /// ES2017
34    ES2017,
35    /// ES2018
36    ES2018,
37    /// ES2019
38    ES2019,
39    /// ES2020
40    ES2020,
41    /// ES2021
42    ES2021,
43    /// ES2022
44    ES2022,
45    /// ES2023
46    ES2023,
47    /// Latest supported version
48    Latest,
49}
50
51impl JavaScriptLanguage {
52    /// Creates a new JavaScript language configuration.
53    pub fn new() -> Self {
54        Self::default()
55    }
56
57    /// Creates a standard JavaScript language instance.
58    pub fn standard() -> Self {
59        Self::default()
60    }
61
62    /// Creates a modern (ES2015+) JavaScript language instance.
63    pub fn modern() -> Self {
64        Self { jsx: false, typescript: false, experimental: false, strict_mode: true, ecma_version: EcmaVersion::Latest }
65    }
66
67    /// Creates a JavaScript language instance with JSX support.
68    pub fn jsx() -> Self {
69        Self { jsx: true, typescript: false, experimental: false, strict_mode: true, ecma_version: EcmaVersion::Latest }
70    }
71
72    /// Creates a TypeScript language instance.
73    pub fn typescript() -> Self {
74        Self { jsx: false, typescript: true, experimental: false, strict_mode: true, ecma_version: EcmaVersion::Latest }
75    }
76}
77
78impl Default for JavaScriptLanguage {
79    fn default() -> Self {
80        Self { jsx: false, typescript: false, experimental: false, strict_mode: false, ecma_version: EcmaVersion::ES2015 }
81    }
82}
83
84impl Language for JavaScriptLanguage {
85    const NAME: &'static str = "javascript";
86    const CATEGORY: LanguageCategory = LanguageCategory::Programming;
87
88    type TokenType = crate::lexer::token_type::JavaScriptTokenType;
89    type ElementType = crate::parser::element_type::JavaScriptElementType;
90    type TypedRoot = crate::ast::JavaScriptRoot;
91}