1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! Java semantic mappings
//!
//! Maps tree-sitter-java node kinds to generic semantic concepts.
//!
//! This module provides complete tree-sitter node kind mappings for Java,
//! covering modern Java features including records (Java 14+), switch expressions
//! (Java 12+), text blocks (Java 15+), and sealed classes (Java 17+).
use super::LanguageSemantics;
/// Java semantic mappings for tree-sitter-java grammar
///
/// Reference: https://github.com/tree-sitter/tree-sitter-java/blob/master/src/node-types.json
pub static JAVA_SEMANTICS: LanguageSemantics = LanguageSemantics {
language: "java",
// =========================================================================
// Node kinds - Complete mappings from tree-sitter-java grammar
// =========================================================================
// Function/method definitions
// - method_declaration: Regular methods
// - constructor_declaration: Class constructors
// - lambda_expression: Lambda expressions (Java 8+)
// - compact_constructor_declaration: Record compact constructors (Java 14+)
function_def_kinds: &[
"method_declaration",
"constructor_declaration",
"lambda_expression",
"compact_constructor_declaration",
],
// Conditional branches
if_kinds: &["if_statement"],
// Loop constructs
// - for_statement: Traditional for(init; cond; update)
// - enhanced_for_statement: for-each loops (for T item : collection)
// - while_statement: while(cond) loops
// - do_statement: do-while loops
loop_kinds: &[
"for_statement",
"enhanced_for_statement",
"while_statement",
"do_statement",
],
// Variable declarations
// - local_variable_declaration: Variables in method bodies
// - field_declaration: Class/instance fields
// - constant_declaration: Interface constants
variable_declaration_kinds: &[
"local_variable_declaration",
"field_declaration",
"constant_declaration",
],
// Assignment expressions
// Note: Java treats += -= etc. as assignment_expression with different operators
assignment_kinds: &["assignment_expression"],
// Augmented assignment (compound assignment)
// In Java these are part of assignment_expression but can be distinguished
// by the operator (+= -= *= /= %= &= |= ^= <<= >>= >>>=)
augmented_assignment_kinds: &["assignment_expression"],
// Return statements
return_kinds: &["return_statement"],
// Call expressions
// - method_invocation: obj.method() or method()
// - object_creation_expression: new ClassName()
// - explicit_constructor_invocation: this() or super() calls
call_kinds: &[
"method_invocation",
"object_creation_expression",
"explicit_constructor_invocation",
],
// Exception handling
// - try_statement: try-catch-finally
// - try_with_resources_statement: try(resource) (Java 7+)
try_catch_kinds: &["try_statement", "try_with_resources_statement"],
// Throw statements
throw_kinds: &["throw_statement"],
// String literals
// - string_literal: Regular "string"
// - text_block: Multi-line """text block""" (Java 15+)
// - character_literal: 'c'
string_literal_kinds: &["string_literal", "text_block", "character_literal"],
// Numeric literals - comprehensive coverage
// - decimal_integer_literal: 123
// - hex_integer_literal: 0xFF
// - octal_integer_literal: 0777
// - binary_integer_literal: 0b1010 (Java 7+)
// - decimal_floating_point_literal: 3.14, 1e10
// - hex_floating_point_literal: 0x1.0p0 (rare)
numeric_literal_kinds: &[
"decimal_integer_literal",
"hex_integer_literal",
"octal_integer_literal",
"binary_integer_literal",
"decimal_floating_point_literal",
"hex_floating_point_literal",
],
// Boolean literals
boolean_literal_kinds: &["true", "false"],
// Null literal
null_literal_kinds: &["null_literal"],
// Parameter kinds
// - formal_parameter: Regular parameters
// - spread_parameter: Varargs (Type... args)
// - receiver_parameter: Explicit this parameter for type annotations
parameter_kinds: &["formal_parameter", "spread_parameter", "receiver_parameter"],
// Class/type definitions
// - class_declaration: Regular classes
// - interface_declaration: Interfaces
// - enum_declaration: Enums
// - record_declaration: Records (Java 14+)
// - annotation_type_declaration: @interface annotations
class_kinds: &[
"class_declaration",
"interface_declaration",
"enum_declaration",
"record_declaration",
"annotation_type_declaration",
],
// Import declarations
import_kinds: &["import_declaration"],
// Block scopes
// - block: { } statement blocks
// - static_initializer: static { } blocks
// - instance_initializer: { } instance init blocks
block_scope_kinds: &["block", "static_initializer", "instance_initializer"],
// Break statements
break_kinds: &["break_statement"],
// Continue statements
continue_kinds: &["continue_statement"],
// Switch constructs
// - switch_expression: Expression form (Java 12+) returns value
// - switch_statement: Statement form (traditional)
switch_kinds: &["switch_expression", "switch_statement"],
// Case clauses in switch
// - switch_block_statement_group: Traditional case: ... break;
// - switch_rule: Arrow syntax case X -> ... (Java 12+)
case_kinds: &["switch_block_statement_group", "switch_rule"],
// Member/property access
// - field_access: obj.field
// - array_access: array[index]
// - method_reference: Class::method (Java 8+)
member_access_kinds: &["field_access", "array_access", "method_reference"],
// Binary expressions
binary_expression_kinds: &["binary_expression"],
// Identifiers
// - identifier: Variable/method names
// - type_identifier: Type names (classes, interfaces)
identifier_kinds: &["identifier", "type_identifier"],
// Unsafe blocks - Java has no unsafe keyword
// Note: JNI (native methods) are separate declarations, not blocks
unsafe_block_kinds: &[],
// Defer-like constructs - Java uses try-with-resources instead
defer_kinds: &[],
// Spawn/async - Java threads are library calls, not syntax
// Note: Virtual threads (Java 21+) also use library APIs
spawn_kinds: &[],
// =========================================================================
// Field names for accessing child nodes in tree-sitter
// =========================================================================
// Condition field for if/while/for/ternary
condition_field: "condition",
// Consequence/then branch
consequence_field: "consequence",
// Alternative/else branch
alternative_field: "alternative",
// Body field for methods, loops, classes
body_field: "body",
// Initializer/value for variable declarations
initializer_field: "value",
// Left operand in binary expressions
left_field: "left",
// Right operand in binary expressions
right_field: "right",
// Name field for declarations
name_field: "name",
// Arguments in method calls
arguments_field: "arguments",
// Value in assignments, returns
value_field: "value",
// Operator in expressions
operator_field: "operator",
// Object/receiver in member access
object_field: "object",
// Property/field being accessed
property_field: "field",
// Function being called (less common in Java, methods use "name")
function_field: "name",
// Parameters in method declarations
parameters_field: "parameters",
// Return type in method declarations
return_type_field: "type",
// Type annotations
type_field: "type",
// Exception handler in try-catch (catch_clause)
handler_field: "handler",
// Finally block in try-catch-finally
finalizer_field: "finalizer",
};
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_java_function_kinds() {
assert!(JAVA_SEMANTICS.is_function_def("method_declaration"));
assert!(JAVA_SEMANTICS.is_function_def("constructor_declaration"));
assert!(JAVA_SEMANTICS.is_function_def("lambda_expression"));
assert!(JAVA_SEMANTICS.is_function_def("compact_constructor_declaration"));
assert!(!JAVA_SEMANTICS.is_function_def("method_invocation"));
}
#[test]
fn test_java_loop_kinds() {
assert!(JAVA_SEMANTICS.is_loop("for_statement"));
assert!(JAVA_SEMANTICS.is_loop("enhanced_for_statement"));
assert!(JAVA_SEMANTICS.is_loop("while_statement"));
assert!(JAVA_SEMANTICS.is_loop("do_statement"));
assert!(!JAVA_SEMANTICS.is_loop("if_statement"));
}
#[test]
fn test_java_null() {
assert!(JAVA_SEMANTICS.is_null_literal("null_literal"));
assert!(!JAVA_SEMANTICS.is_null_literal("nil"));
}
#[test]
fn test_java_try_catch() {
assert!(JAVA_SEMANTICS.is_try_catch("try_statement"));
assert!(JAVA_SEMANTICS.is_try_catch("try_with_resources_statement"));
assert!(JAVA_SEMANTICS.is_throw("throw_statement"));
}
#[test]
fn test_java_class_kinds() {
assert!(JAVA_SEMANTICS.is_class("class_declaration"));
assert!(JAVA_SEMANTICS.is_class("interface_declaration"));
assert!(JAVA_SEMANTICS.is_class("enum_declaration"));
assert!(JAVA_SEMANTICS.is_class("record_declaration"));
assert!(JAVA_SEMANTICS.is_class("annotation_type_declaration"));
}
#[test]
fn test_java_switch_kinds() {
assert!(JAVA_SEMANTICS.is_switch("switch_expression"));
assert!(JAVA_SEMANTICS.is_switch("switch_statement"));
assert!(JAVA_SEMANTICS.is_case("switch_block_statement_group"));
assert!(JAVA_SEMANTICS.is_case("switch_rule"));
}
#[test]
fn test_java_literal_kinds() {
// String literals
assert!(JAVA_SEMANTICS.is_string_literal("string_literal"));
assert!(JAVA_SEMANTICS.is_string_literal("text_block"));
assert!(JAVA_SEMANTICS.is_string_literal("character_literal"));
// Numeric literals
assert!(JAVA_SEMANTICS.is_numeric_literal("decimal_integer_literal"));
assert!(JAVA_SEMANTICS.is_numeric_literal("hex_integer_literal"));
assert!(JAVA_SEMANTICS.is_numeric_literal("binary_integer_literal"));
assert!(JAVA_SEMANTICS.is_numeric_literal("decimal_floating_point_literal"));
// Boolean literals
assert!(JAVA_SEMANTICS.is_boolean_literal("true"));
assert!(JAVA_SEMANTICS.is_boolean_literal("false"));
// Combined literal check
assert!(JAVA_SEMANTICS.is_literal("string_literal"));
assert!(JAVA_SEMANTICS.is_literal("decimal_integer_literal"));
assert!(JAVA_SEMANTICS.is_literal("true"));
assert!(JAVA_SEMANTICS.is_literal("null_literal"));
}
#[test]
fn test_java_member_access() {
assert!(JAVA_SEMANTICS.is_member_access("field_access"));
assert!(JAVA_SEMANTICS.is_member_access("array_access"));
assert!(JAVA_SEMANTICS.is_member_access("method_reference"));
}
#[test]
fn test_java_call_kinds() {
assert!(JAVA_SEMANTICS.is_call("method_invocation"));
assert!(JAVA_SEMANTICS.is_call("object_creation_expression"));
assert!(JAVA_SEMANTICS.is_call("explicit_constructor_invocation"));
}
#[test]
fn test_java_control_flow() {
assert!(JAVA_SEMANTICS.is_control_flow("if_statement"));
assert!(JAVA_SEMANTICS.is_control_flow("for_statement"));
assert!(JAVA_SEMANTICS.is_control_flow("while_statement"));
assert!(JAVA_SEMANTICS.is_control_flow("switch_statement"));
assert!(JAVA_SEMANTICS.is_control_flow("try_statement"));
assert!(JAVA_SEMANTICS.is_control_flow("return_statement"));
assert!(JAVA_SEMANTICS.is_control_flow("break_statement"));
assert!(JAVA_SEMANTICS.is_control_flow("continue_statement"));
assert!(JAVA_SEMANTICS.is_control_flow("throw_statement"));
}
#[test]
fn test_java_no_unsafe_defer_spawn() {
// Java doesn't have these constructs at the syntax level
assert!(!JAVA_SEMANTICS.is_unsafe_block("unsafe_block"));
assert!(!JAVA_SEMANTICS.is_defer("defer_statement"));
assert!(!JAVA_SEMANTICS.is_spawn("go_statement"));
}
#[test]
fn test_java_identifiers() {
assert!(JAVA_SEMANTICS.is_identifier("identifier"));
assert!(JAVA_SEMANTICS.is_identifier("type_identifier"));
}
#[test]
fn test_java_block_scopes() {
assert!(JAVA_SEMANTICS.is_block_scope("block"));
assert!(JAVA_SEMANTICS.is_block_scope("static_initializer"));
assert!(JAVA_SEMANTICS.is_block_scope("instance_initializer"));
}
}