Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 4221d1e

Browse files
author
Hendrik van Antwerpen
committed
Do imports the Java way
1 parent eb0bfb9 commit 4221d1e

File tree

6 files changed

+115
-83
lines changed

6 files changed

+115
-83
lines changed

languages/tree-sitter-stack-graphs-java/src/stack-graphs.tsg

Lines changed: 72 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -64,80 +64,54 @@ attribute node_symbol = node => symbol = (source-text node), source_n
6464
node @decl.lexical_scope
6565
}
6666

67-
(import_declaration
68-
(scoped_identifier
69-
name: (identifier) @scope_name)
70-
) @import {
71-
72-
node @scope_name.value
73-
attr (@scope_name.value) node_definition = @scope_name
74-
75-
edge @import.defs -> @scope_name.value
76-
}
77-
78-
(import_declaration
79-
(scoped_identifier
80-
scope: (scoped_identifier
81-
name: (identifier) @name))) @import {
82-
node @name.value
83-
attr (@name.value) node_definition = @name
84-
85-
edge @import.defs -> @name.value
86-
}
87-
88-
; Import rules lifted mostly from typescript
89-
(program [(import_declaration)]* @import) @prog {
90-
var proj_scope = ROOT_NODE
91-
92-
if (not (eq PROJECT_NAME "")) {
93-
; project definition
94-
node proj_def__ns
95-
attr (proj_def__ns) pop_symbol = "%Proj"
96-
edge proj_scope -> proj_def__ns
97-
;
98-
node proj_def
99-
attr (proj_def) pop_symbol = PROJECT_NAME
100-
edge proj_def__ns -> proj_def
101-
;
102-
set proj_scope = proj_def
103-
}
104-
105-
var mod_scope = proj_scope
106-
if (not (is-empty @import)) {
107-
; module definition
108-
let mod_name = (path-filestem FILE_PATH)
109-
let mod_path = (path-normalize (path-join (path-dir FILE_PATH) mod_name))
110-
;
111-
node mod_def__ns
112-
attr (mod_def__ns) pop_symbol = "%M"
113-
edge mod_scope -> mod_def__ns
114-
set mod_scope = mod_def__ns
115-
;
116-
scan mod_path {
117-
"index$" {
118-
; skip last component for index files
119-
}
120-
"([^/]+)/?" {
121-
node mod_def
122-
attr (mod_def) pop_symbol = $1
123-
edge mod_scope -> mod_def
124-
set mod_scope = mod_def
125-
}
126-
}
127-
; make the last one a definition
128-
attr (mod_scope) is_definition, source_node = @prog, empty_source_span
67+
(program
68+
(package_declaration
69+
(identifier)@pkg_name)? @package) @prog {
70+
if none @package {
71+
edge ROOT_NODE -> @prog.defs
12972
} else {
130-
; expose definitions via project scope
131-
edge mod_scope -> @prog.defs
73+
node pkg_def
74+
attr (pkg_def) node_definition = @pkg_name
75+
edge pkg_def -> @prog.defs
76+
edge ROOT_NODE -> pkg_def
13277
}
13378
}
13479

135-
(import_declaration
136-
(identifier) @name) @import {
137-
node @name.value
138-
attr (@name.value) node_definition = @name
80+
(import_declaration (_) @ref) @import {
81+
edge @ref.lexical_scope -> @import.lexical_scope
82+
}
83+
84+
;; X
85+
(identifier) @name {
86+
node @name.lexical_scope
87+
node @name.type
13988

140-
edge @import.defs -> @name.value
89+
node @name.ref
90+
attr (@name.ref) node_reference = @name
91+
edge @name.ref -> @name.lexical_scope
92+
}
93+
94+
;; _.X
95+
(scoped_identifier scope: (_) @scope name: (_) @name) @scoped_name {
96+
node @scoped_name.lexical_scope
97+
98+
edge @scope.lexical_scope -> @scoped_name.lexical_scope
99+
100+
edge @name.lexical_scope -> @scope.ref
101+
102+
node @scoped_name.ref
103+
edge @scoped_name.ref -> @name.ref
104+
}
105+
106+
[
107+
(import_declaration (identifier) @scope @name)
108+
(import_declaration (scoped_identifier scope: (_) @scope name: (identifier) @name))
109+
] @import {
110+
node def
111+
attr (def) node_definition = @name
112+
edge def -> @name.ref
113+
114+
edge @import.defs -> def
141115
}
142116

143117
;;;;;;;;;;;;;;;;;;;;
@@ -869,9 +843,6 @@ attribute node_symbol = node => symbol = (source-text node), source_n
869843
}
870844

871845
(resource . (identifier) @name .) @this {
872-
node @name.lexical_scope
873-
node @name.type
874-
875846
node ref
876847
node ref__typeof
877848

@@ -1087,24 +1058,45 @@ attribute node_symbol = node => symbol = (source-text node), source_n
10871058
; Expressions have a lexical scope (propagated to child expressions) and a type. The lexical scope is used for lexical name lookup, while types enable type-based name lookup (for field accesses, method lookup, and so forth).
10881059

10891060
[
1090-
(array_access)
10911061
(array_initializer)
1092-
(expression)
1093-
(field_access)
1062+
(assignment_expression)
1063+
(binary_expression)
1064+
(instanceof_expression)
1065+
(lambda_expression)
1066+
(ternary_expression)
1067+
(update_expression)
1068+
(decimal_integer_literal)
1069+
(hex_integer_literal)
1070+
(octal_integer_literal)
1071+
(binary_integer_literal)
1072+
(decimal_floating_point_literal)
1073+
(hex_floating_point_literal)
1074+
(true)
1075+
(false)
1076+
(character_literal)
1077+
(string_literal)
1078+
(null_literal)
1079+
(class_literal)
1080+
(this)
1081+
; (identifier)
10941082
(parenthesized_expression)
1095-
(primary_expression)
1083+
(object_creation_expression)
1084+
(field_access)
1085+
(array_access)
1086+
(method_invocation)
1087+
(method_reference)
1088+
(array_creation_expression)
1089+
(unary_expression)
1090+
(cast_expression)
1091+
(switch_expression)
10961092
(super)
1097-
(this)
10981093
] @expr
10991094
{
11001095
node @expr.type
11011096
node @expr.lexical_scope
11021097
}
11031098

11041099
(assignment_expression left: (identifier) @name) @this {
1105-
; FIXME: can we combine this with primary_expression/identifier?
1106-
node @name.lexical_scope
1107-
node @name.type
11081100
node member
11091101
node implicit_this
11101102
node implicit_this__typeof

languages/tree-sitter-stack-graphs-java/test/imports_and_exports/import_method.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
public class Importer {
55
public static void main(String[] args) {
66
Foo.bar();
7-
// ^ defined: 15
7+
// ^ defined: 16
88

99
}
1010
}
1111

1212
/* --- path: foo/Foo.java ---*/
13+
package foo;
1314

1415
public class Foo {
1516
public static void bar() {
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/*--- path: Importer.java ---*/
22
import foo.Foo;
3+
// ^ defined: 14
34

45
public class Importer {
5-
public static Foo main(String[] args) {
6-
// ^ defined: 2, 12
6+
public Foo test() {
7+
// ^ defined: 2, 14
78
}
89
}
910

1011
/* --- path: foo/Foo.java ---*/
12+
package foo;
1113

1214
public class Foo {
1315
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*--- path: Importer.java ---*/
2+
public class Importer {
3+
public Foo test() {
4+
// ^ defined: 9
5+
}
6+
}
7+
8+
/* --- path: Foo.java ---*/
9+
public class Foo {
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*--- path: Importer.java ---*/
2+
public class Importer {
3+
public static void main(String[] args) {
4+
Foo.bar();
5+
// ^ defined: 12
6+
7+
}
8+
}
9+
10+
/* --- path: Foo.java ---*/
11+
public class Foo {
12+
public static void bar() {
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*--- path: Importer.java ---*/
2+
public class Importer {
3+
public static void main(String[] args) {
4+
bar();
5+
// ^ defined:
6+
}
7+
}
8+
9+
/* --- path: Foo.java ---*/
10+
public class Foo {
11+
public static void bar() {
12+
}
13+
}

0 commit comments

Comments
 (0)