Skip to main content

nargo_type_check/modules/
typescript.rs

1//! TypeScript 语句处理模块
2//!
3//! 包含 TypeScript 特有的语句处理相关的代码
4
5use regex::Regex;
6use std::collections::HashMap;
7
8use crate::modules::types::{Type, TypeEnv};
9
10/// TypeScript 语句处理器
11pub trait TypeScriptStmtHandler {
12    /// 处理 TypeScript 特有的语句
13    ///
14    /// # 参数
15    ///
16    /// * `stmt_str` - 语句字符串
17    fn handle_typescript_stmt(&mut self, stmt_str: &str);
18
19    /// 处理接口定义
20    ///
21    /// # 参数
22    ///
23    /// * `name` - 接口名称
24    /// * `members` - 接口成员
25    /// * `extends` - 继承的接口
26    fn handle_interface_def(&mut self, name: String, members: HashMap<String, Type>, extends: Vec<String>);
27
28    /// 处理类型别名
29    ///
30    /// # 参数
31    ///
32    /// * `name` - 类型别名名称
33    /// * `ty` - 类型
34    fn handle_type_alias(&mut self, name: String, ty: Type);
35
36    /// 处理泛型类型
37    ///
38    /// # 参数
39    ///
40    /// * `name` - 类型名称
41    /// * `type_args` - 类型参数
42    ///
43    /// # 返回
44    ///
45    /// 处理后的类型
46    fn handle_generic_type(&self, name: String, type_args: Vec<Type>) -> Type;
47
48    /// 处理联合类型
49    ///
50    /// # 参数
51    ///
52    /// * `types` - 联合的类型
53    ///
54    /// # 返回
55    ///
56    /// 联合类型
57    fn handle_union_type(&self, types: Vec<Type>) -> Type;
58
59    /// 处理交叉类型
60    ///
61    /// # 参数
62    ///
63    /// * `types` - 交叉的类型
64    ///
65    /// # 返回
66    ///
67    /// 交叉类型
68    fn handle_intersection_type(&self, types: Vec<Type>) -> Type;
69
70    /// 处理条件类型
71    ///
72    /// # 参数
73    ///
74    /// * `check_type` - 检查类型 (T)
75    /// * `extends_type` - 扩展类型 (U)
76    /// * `true_type` - 条件为真时的类型 (X)
77    /// * `false_type` - 条件为假时的类型 (Y)
78    ///
79    /// # 返回
80    ///
81    /// 条件类型
82    fn handle_conditional_type(&self, check_type: Type, extends_type: Type, true_type: Type, false_type: Type) -> Type;
83
84    /// 处理映射类型
85    ///
86    /// # 参数
87    ///
88    /// * `key_var` - 键变量名 (K)
89    /// * `source_type` - 源类型 (T)
90    /// * `mapped_type` - 映射后的类型
91    ///
92    /// # 返回
93    ///
94    /// 映射类型
95    fn handle_mapped_type(&self, key_var: String, source_type: Type, mapped_type: Type) -> Type;
96
97    /// 处理索引访问类型
98    ///
99    /// # 参数
100    ///
101    /// * `object_type` - 对象类型 (T)
102    /// * `index_type` - 索引类型 (K)
103    ///
104    /// # 返回
105    ///
106    /// 索引访问类型
107    fn handle_index_access_type(&self, object_type: Type, index_type: Type) -> Type;
108
109    /// 处理 KeyOf 类型
110    ///
111    /// # 参数
112    ///
113    /// * `target_type` - 目标类型 (T)
114    ///
115    /// # 返回
116    ///
117    /// KeyOf 类型
118    fn handle_keyof_type(&self, target_type: Type) -> Type;
119
120    /// 处理字面量类型
121    ///
122    /// # 参数
123    ///
124    /// * `literal` - 字面量值
125    ///
126    /// # 返回
127    ///
128    /// 字面量类型
129    fn handle_literal_type(&self, literal: String) -> Type;
130
131    /// 处理 this 类型
132    ///
133    /// # 参数
134    ///
135    /// * `this_type` - this 类型
136    ///
137    /// # 返回
138    ///
139    /// this 类型
140    fn handle_this_type(&self, this_type: Type) -> Type;
141
142    /// 处理省略 this 参数类型
143    ///
144    /// # 参数
145    ///
146    /// * `target_type` - 目标类型
147    ///
148    /// # 返回
149    ///
150    /// 省略 this 参数后的类型
151    fn handle_omit_this_parameter_type(&self, target_type: Type) -> Type;
152
153    /// 处理 this 参数类型
154    ///
155    /// # 参数
156    ///
157    /// * `target_type` - 目标类型
158    ///
159    /// # 返回
160    ///
161    /// this 参数类型
162    fn handle_this_parameter_type(&self, target_type: Type) -> Type;
163
164    /// 处理接口定义语句
165    ///
166    /// # 参数
167    ///
168    /// * `stmt_str` - 接口定义语句
169    fn handle_interface_stmt(&mut self, stmt_str: &str);
170
171    /// 解析接口成员
172    ///
173    /// # 参数
174    ///
175    /// * `members_str` - 接口成员字符串
176    ///
177    /// # 返回
178    ///
179    /// 接口成员映射
180    fn parse_interface_members(&self, members_str: &str) -> HashMap<String, Type>;
181
182    /// 处理类型别名语句
183    ///
184    /// # 参数
185    ///
186    /// * `stmt_str` - 类型别名语句
187    fn handle_type_alias_stmt(&mut self, stmt_str: &str);
188
189    /// 解析类型字符串
190    ///
191    /// # 参数
192    ///
193    /// * `type_str` - 类型字符串
194    ///
195    /// # 返回
196    ///
197    /// 解析后的类型
198    fn parse_type(&self, type_str: &str) -> Type;
199}