vcode-analysis 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,274 @@
1
+ """Kotlin 解析数据模型
2
+
3
+ 定义 Kotlin 代码解析结果的数据结构。
4
+ """
5
+
6
+ from dataclasses import dataclass, field
7
+ from typing import Optional, Literal
8
+
9
+
10
+ # ==================== 基础类型 ====================
11
+
12
+ @dataclass
13
+ class KotlinFunctionInfo:
14
+ """Kotlin 函数信息"""
15
+ name: str
16
+ line_start: int
17
+ line_end: int
18
+ return_type: Optional[str]
19
+ parameters: list[dict] # {name, type, default_value}
20
+ modifiers: list[str] # public, private, suspend, inline, etc.
21
+ type_params: list[str] # 泛型参数
22
+ receiver_type: Optional[str] = None # 扩展函数接收者
23
+ is_operator: bool = False
24
+ is_infix: bool = False
25
+ is_tailrec: bool = False
26
+
27
+
28
+ @dataclass
29
+ class KotlinPropertyInfo:
30
+ """Kotlin 属性信息"""
31
+ name: str
32
+ type: str
33
+ line: int
34
+ is_val: bool # val vs var
35
+ modifiers: list[str]
36
+ is_delegate: bool = False # 委托属性
37
+
38
+
39
+ @dataclass
40
+ class KotlinClassInfo:
41
+ """Kotlin 类信息"""
42
+ name: str
43
+ line_start: int
44
+ line_end: int
45
+ class_type: str # class, interface, object, enum class, sealed class, data class
46
+ type_params: list[str] # 泛型参数
47
+ primary_constructor_params: list[dict]
48
+ extends: Optional[str]
49
+ implements: list[str]
50
+ modifiers: list[str]
51
+ annotations: list[str]
52
+ functions: list[KotlinFunctionInfo]
53
+ properties: list[KotlinPropertyInfo]
54
+
55
+
56
+ @dataclass
57
+ class KotlinImportInfo:
58
+ """Kotlin 导入信息"""
59
+ path: str
60
+ alias: Optional[str] # as alias
61
+ line: int
62
+
63
+
64
+ # ==================== Sealed 类层次结构 ====================
65
+
66
+ @dataclass
67
+ class KotlinSealedClassInfo(KotlinClassInfo):
68
+ """Sealed 类信息"""
69
+ is_sealed: bool = True
70
+ permitted_subclasses: list[str] = field(default_factory=list) # 允许的子类列表
71
+ when_expressions: list['KotlinWhenExpression'] = field(default_factory=list)
72
+
73
+
74
+ @dataclass
75
+ class KotlinWhenExpression:
76
+ """When 表达式信息"""
77
+ subject_type: Optional[str] # 被检查的类型
78
+ subject_variable: Optional[str] # 被检查的变量名
79
+ branches: list[dict] # [{pattern: str, is_else: bool, line: int}]
80
+ is_exhaustive: Optional[bool] = None # 是否穷尽(需要类型推断)
81
+ line_start: int = 0
82
+ line_end: int = 0
83
+
84
+
85
+ # ==================== 协程与 Flow ====================
86
+
87
+ @dataclass
88
+ class KotlinCoroutineInfo:
89
+ """协程信息"""
90
+ function_name: str
91
+ is_suspend: bool
92
+ coroutine_scope: Optional[str] = None # CoroutineScope, lifecycleScope 等
93
+ flow_type: Optional[str] = None # Flow, StateFlow, SharedFlow
94
+ dispatcher: Optional[str] = None # IO, Default, Main, Unconfined
95
+ builder_type: Optional[str] = None # launch, async, runBlocking
96
+ line: int = 0
97
+
98
+
99
+ @dataclass
100
+ class KotlinFlowOperator:
101
+ """Flow 操作符信息"""
102
+ name: str # map, filter, collect 等
103
+ input_type: Optional[str] = None
104
+ output_type: Optional[str] = None
105
+ is_terminal: bool = False # 是否为终端操作符 (collect, first, single 等)
106
+ line: int = 0
107
+
108
+
109
+ # Flow 终端操作符列表
110
+ TERMINAL_FLOW_OPERATORS = {
111
+ 'collect', 'collectLatest', 'collectIndexed', 'collectWhile',
112
+ 'first', 'firstOrNull', 'last', 'lastOrNull',
113
+ 'single', 'singleOrNull', 'toList', 'toSet', 'count',
114
+ 'reduce', 'fold', 'launchIn', 'onEach'
115
+ }
116
+
117
+
118
+ # ==================== 类型系统特性 ====================
119
+
120
+ @dataclass
121
+ class KotlinTypeAlias:
122
+ """类型别名"""
123
+ name: str
124
+ underlying_type: str
125
+ type_params: list[str] = field(default_factory=list)
126
+ line: int = 0
127
+
128
+
129
+ @dataclass
130
+ class KotlinValueClass:
131
+ """值类(内联类)"""
132
+ name: str
133
+ wrapped_type: str
134
+ type_params: list[str] = field(default_factory=list)
135
+ line: int = 0
136
+
137
+
138
+ @dataclass
139
+ class KotlinGenericType:
140
+ """泛型信息"""
141
+ name: str
142
+ variance: Literal['in', 'out', 'none'] = 'none'
143
+ bounds: list[str] = field(default_factory=list) # 类型上界
144
+ is_reified: bool = False
145
+
146
+
147
+ # ==================== 属性委托 ====================
148
+
149
+ @dataclass
150
+ class KotlinDelegatedProperty:
151
+ """委托属性"""
152
+ name: str
153
+ delegate_type: str # lazy, observable, vetoable, custom
154
+ delegate_expression: Optional[str] = None
155
+ is_val: bool = True
156
+ line: int = 0
157
+
158
+
159
+ @dataclass
160
+ class KotlinPropertyDelegate:
161
+ """属性委托类型"""
162
+ name: str # lazy, observable, vetoable, by viewModels() 等
163
+ is_standard: bool = True # 是否为标准库委托
164
+ provides_getter: bool = True
165
+ provides_setter: bool = False
166
+
167
+
168
+ # 标准委托类型
169
+ STANDARD_DELEGATES = {
170
+ 'lazy': KotlinPropertyDelegate('lazy', True, True, False),
171
+ 'observable': KotlinPropertyDelegate('observable', True, True, True),
172
+ 'vetoable': KotlinPropertyDelegate('vetoable', True, True, True),
173
+ 'notNull': KotlinPropertyDelegate('notNull', True, True, True),
174
+ 'lateinit': KotlinPropertyDelegate('lateinit', False, True, True),
175
+ }
176
+
177
+
178
+ # ==================== 扩展函数和属性 ====================
179
+
180
+ @dataclass
181
+ class KotlinExtensionFunction(KotlinFunctionInfo):
182
+ """扩展函数信息"""
183
+ receiver_type: str = "" # 扩展接收者类型
184
+ dispatch_receiver: Optional[str] = None # 分发接收者(如果在类内)
185
+
186
+
187
+ @dataclass
188
+ class KotlinExtensionProperty:
189
+ """扩展属性信息"""
190
+ name: str
191
+ receiver_type: str
192
+ property_type: str
193
+ is_val: bool
194
+ line: int
195
+
196
+
197
+ # ==================== 其他 Kotlin 特性 ====================
198
+
199
+ @dataclass
200
+ class KotlinObjectDeclaration:
201
+ """Object 声明(单例)"""
202
+ name: str
203
+ extends: Optional[str]
204
+ implements: list[str]
205
+ is_companion: bool
206
+ line_start: int
207
+ line_end: int
208
+
209
+
210
+ @dataclass
211
+ class KotlinEnumClassInfo(KotlinClassInfo):
212
+ """枚举类信息"""
213
+ entries: list[dict] = field(default_factory=list) # [{name, params, line}]
214
+
215
+
216
+ @dataclass
217
+ class KotlinDataClassInfo(KotlinClassInfo):
218
+ """数据类信息"""
219
+ component_functions: list[str] = field(default_factory=list) # component1, component2...
220
+ copy_function_params: list[dict] = field(default_factory=list)
221
+
222
+
223
+ # ==================== 解析结果 ====================
224
+
225
+ @dataclass
226
+ class KotlinASTResult:
227
+ """Kotlin AST 解析结果"""
228
+ file_path: str
229
+ success: bool
230
+ error: Optional[str] = None
231
+ parse_mode: str = "fast" # fast, precise, auto
232
+
233
+ # 基础结构信息
234
+ package: Optional[str] = None
235
+ imports: list[KotlinImportInfo] = field(default_factory=list)
236
+ classes: list[KotlinClassInfo] = field(default_factory=list)
237
+ functions: list[KotlinFunctionInfo] = field(default_factory=list)
238
+ properties: list[KotlinPropertyInfo] = field(default_factory=list)
239
+
240
+ # Kotlin 特有结构
241
+ sealed_classes: list[KotlinSealedClassInfo] = field(default_factory=list)
242
+ when_expressions: list[KotlinWhenExpression] = field(default_factory=list)
243
+ coroutines: list[KotlinCoroutineInfo] = field(default_factory=list)
244
+ flow_operators: list[KotlinFlowOperator] = field(default_factory=list)
245
+ type_aliases: list[KotlinTypeAlias] = field(default_factory=list)
246
+ value_classes: list[KotlinValueClass] = field(default_factory=list)
247
+ delegated_properties: list[KotlinDelegatedProperty] = field(default_factory=list)
248
+ extension_functions: list[KotlinExtensionFunction] = field(default_factory=list)
249
+ extension_properties: list[KotlinExtensionProperty] = field(default_factory=list)
250
+ object_declarations: list[KotlinObjectDeclaration] = field(default_factory=list)
251
+ enum_classes: list[KotlinEnumClassInfo] = field(default_factory=list)
252
+ data_classes: list[KotlinDataClassInfo] = field(default_factory=list)
253
+
254
+ # 统计信息
255
+ total_lines: int = 0
256
+
257
+ def merge_from(self, other: 'KotlinASTResult'):
258
+ """合并另一个解析结果(用于增量解析)"""
259
+ self.imports.extend(other.imports)
260
+ self.classes.extend(other.classes)
261
+ self.functions.extend(other.functions)
262
+ self.properties.extend(other.properties)
263
+ self.sealed_classes.extend(other.sealed_classes)
264
+ self.when_expressions.extend(other.when_expressions)
265
+ self.coroutines.extend(other.coroutines)
266
+ self.flow_operators.extend(other.flow_operators)
267
+ self.type_aliases.extend(other.type_aliases)
268
+ self.value_classes.extend(other.value_classes)
269
+ self.delegated_properties.extend(other.delegated_properties)
270
+ self.extension_functions.extend(other.extension_functions)
271
+ self.extension_properties.extend(other.extension_properties)
272
+ self.object_declarations.extend(other.object_declarations)
273
+ self.enum_classes.extend(other.enum_classes)
274
+ self.data_classes.extend(other.data_classes)
@@ -0,0 +1,146 @@
1
+ """Kotlin 正则表达式模式定义
2
+
3
+ 定义用于快速解析 Kotlin 代码的正则表达式模式。
4
+ """
5
+
6
+ # 基础模式
7
+ KOTLIN_PATTERNS = {
8
+ # 包声明
9
+ "package": r'^package\s+([\w.]+)',
10
+
11
+ # 导入
12
+ "import": r'^import\s+([\w.]+)(?:\s+as\s+(\w+))?',
13
+
14
+ # 类声明(基础)
15
+ "class": r'(?:(public|private|protected|internal|open|abstract|final|sealed|data|enum|annotation|inner|companion)\s+)*'
16
+ r'(class|interface|object)\s+(\w+)(?:<([^>]+)>)?(?:\s*\(([^)]*)\))?(?:\s*:\s*([^{]+?))?(?:\s*\{|$)',
17
+
18
+ # 函数声明
19
+ "function": r'(?:(public|private|protected|internal|open|abstract|final|suspend|inline|tailrec|operator|infix)\s+)*'
20
+ r'fun\s+(?:<([^>]+)>\s+)?(?:([^\s.]+)\.)?(\w+)\s*\(([^)]*)\)(?:\s*:\s*([^{=\n]+))?',
21
+
22
+ # 属性声明
23
+ "property": r'(?:(public|private|protected|internal|open|override|lateinit|const)\s+)*'
24
+ r'(val|var)\s+(\w+)\s*(?::\s*([^\s=]+))?(?:\s*=|\s*\{|\s*$)',
25
+
26
+ # 注解
27
+ "annotation": r'@(\w+)(?:\([^)]*\))?',
28
+ }
29
+
30
+ # Kotlin 特有模式
31
+ KOTLIN_ADVANCED_PATTERNS = {
32
+ # ==================== Sealed 类 ====================
33
+
34
+ # Sealed 类声明
35
+ "sealed_class": r'(?:sealed\s+class\s+(\w+)(?:<([^>]+)>)?)',
36
+
37
+ # Sealed 接口声明 (Kotlin 1.5+)
38
+ "sealed_interface": r'(?:sealed\s+interface\s+(\w+)(?:<([^>]+)>)?)',
39
+
40
+ # When 表达式
41
+ "when_expression": r'when\s*(?:\(([^)]*)\))?\s*\{',
42
+
43
+ # When 分支
44
+ "when_branch": r'([^\n]->|is\s+\w+|else)\s*->',
45
+
46
+ # ==================== 协程与 Flow ====================
47
+
48
+ # Suspend 函数
49
+ "suspend_function": r'suspend\s+fun\s+(\w+)\s*\(([^)]*)\)(?:\s*:\s*([^{=\n]+))?',
50
+
51
+ # Flow 类型
52
+ "flow_type": r'(?:Flow|StateFlow|MutableStateFlow|SharedFlow|MutableSharedFlow)<([^>]+)>',
53
+
54
+ # 协程构建器
55
+ "coroutine_builder": r'(launch|async|runBlocking|withContext|coroutineScope|supervisorScope)\s*(?:\{|<|.)',
56
+
57
+ # Flow 操作符
58
+ "flow_operator": r'\.(map|filter|collect|onEach|onStart|onCompletion|catch|combine|zip|flatMapLatest|flatMapMerge|transform|take|drop|distinctUntilChanged|debounce|throttle|sample|conflate|buffer|flowOn)\s*\(',
59
+
60
+ # Dispatcher
61
+ "dispatcher": r'Dispatchers\.(Default|IO|Main|Unconfined)|Dispatcher\.(Default|IO|Main|Unconfined)',
62
+
63
+ # 协程作用域
64
+ "coroutine_scope": r'(lifecycleScope|viewModelScope|GlobalScope|CoroutineScope|rememberCoroutineScope)',
65
+
66
+ # ==================== 类型系统 ====================
67
+
68
+ # 类型别名
69
+ "type_alias": r'typealias\s+(\w+)(?:<([^>]+)>)?\s*=\s*(.+?)(?:\s*$)',
70
+
71
+ # 值类 (Value class / 内联类)
72
+ "value_class": r'(?:value\s+class|inline\s+class)\s+(\w+)(?:\s*\(([^)]*)\))?',
73
+
74
+ # 泛型变型
75
+ "variance": r'(?:class|interface)\s+\w+<(in|out)\s+(\w+)>',
76
+
77
+ # 泛型约束
78
+ "type_constraint": r'where\s+(\w+)\s*:\s*([^,\n]+)',
79
+
80
+ # 可空类型
81
+ "nullable_type": r'(\w+)(?:<[^>]+>)?\?',
82
+
83
+ # 平台类型 (Java 互操作)
84
+ "platform_type": r'(\w+)!!',
85
+
86
+ # ==================== 属性委托 ====================
87
+
88
+ # 委托属性
89
+ "delegated_property": r'(val|var)\s+(\w+)\s*(?::\s*([^\s=]+))?\s+by\s+(.+?)(?:\s*$|\s*\n)',
90
+
91
+ # 标准委托
92
+ "standard_delegate": r'(lazy|observable|vetoable|delegates)\s*(?:<[^>]+>)?\s*\{',
93
+
94
+ # 属性引用
95
+ "property_reference": r'::(\w+)',
96
+
97
+ # 提供委托
98
+ "provide_delegate": r'provideDelegate\s*\(',
99
+
100
+ # ==================== 其他 Kotlin 特性 ====================
101
+
102
+ # 扩展函数
103
+ "extension_function": r'fun\s+([^\s.]+)\.(\w+)\s*\(([^)]*)\)(?:\s*:\s*([^{=\n]+))?',
104
+
105
+ # 扩展属性
106
+ "extension_property": r'(val|var)\s+([^\s.]+)\.(\w+)',
107
+
108
+ # 数据类
109
+ "data_class": r'data\s+class\s+(\w+)(?:<([^>]+)>)?\s*\(([^)]*)\)',
110
+
111
+ # 枚举类
112
+ "enum_class": r'enum\s+class\s+(\w+)(?:\s*\:(.+?))?',
113
+
114
+ # 枚举条目
115
+ "enum_entry": r'^\s*(\w+)\s*(?:\(([^)]*)\))?(?:,|\s*$)',
116
+
117
+ # Object 声明
118
+ "object_declaration": r'object\s+(\w+)(?:\s*:\s*([^{]+?))?(?:\s*\{|$)',
119
+
120
+ # Companion object
121
+ "companion_object": r'companion\s+object\s*(?::\s*(\w+))?\s*\{',
122
+
123
+ # 初始化块
124
+ "init_block": r'init\s*\{',
125
+
126
+ # 尾递归函数
127
+ "tailrec_function": r'tailrec\s+fun\s+(\w+)',
128
+
129
+ # 操作符重载
130
+ "operator_function": r'operator\s+fun\s+(\w+)',
131
+
132
+ # 中缀函数
133
+ "infix_function": r'infix\s+fun\s+(\w+)',
134
+
135
+ # 内联函数
136
+ "inline_function": r'(?:inline|noinline|crossinline)\s+fun',
137
+
138
+ # 具体化类型参数
139
+ "reified_type": r'inline\s+fun\s+<reified\s+(\w+)>',
140
+
141
+ # 契约
142
+ "contract": r'contract\s*\{',
143
+ }
144
+
145
+ # 合并所有模式
146
+ ALL_KOTLIN_PATTERNS = {**KOTLIN_PATTERNS, **KOTLIN_ADVANCED_PATTERNS}